1 //GoodFET SPI Application
4 //Higher level left to client application.
20 //This could be more accurate.
21 //Does it ever need to be?
23 #define SPIDELAY(x) delay(x)
25 #define SETMOSI P5OUT|=MOSI
26 #define CLRMOSI P5OUT&=~MOSI
27 #define SETCLK P5OUT|=SCK
28 #define CLRCLK P5OUT&=~SCK
29 #define READMISO (P5IN&MISO?1:0)
33 //! Set up the pins for SPI mode.
40 //! Read and write an SPI bit.
41 unsigned char spitrans8(unsigned char byte){
43 //This function came from the SPI Wikipedia article.
46 for (bit = 0; bit < 8; bit++) {
47 /* write MOSI on trailing edge of previous clock */
54 /* half a clock cycle before leading/rising edge */
58 /* half a clock cycle before trailing/falling edge */
61 /* read MISO on trailing edge */
69 //! Enable SPI writing
70 void spiflash_wrten(){
71 P5OUT&=~SS; //Drop !SS to begin transaction.
72 spitrans8(0x06);//Chip Erase
73 P5OUT|=SS; //Raise !SS to end transaction.
76 //! Handles a monitor command.
77 void spihandle(unsigned char app,
82 //PEEK and POKE might come later.
85 P5OUT&=~SS; //Drop !SS to begin transaction.
87 cmddata[i]=spitrans8(cmddata[i]);
88 P5OUT|=SS; //Raise !SS to end transaction.
91 case SPI_JEDEC://Grab 3-byte JEDEC ID.
92 P5OUT&=~SS; //Drop !SS to begin transaction.
96 cmddata[i]=spitrans8(cmddata[i]);
98 P5OUT|=SS; //Raise !SS to end transaction.
100 case PEEK://Grab 128 bytes from an SPI Flash ROM
101 P5OUT&=~SS; //Drop !SS to begin transaction.
102 spitrans8(0x03);//Flash Read Command
103 len=3;//write 3 byte pointer
105 spitrans8(cmddata[i]);
106 len=0x80;//128 byte chunk
108 cmddata[i]=spitrans8(0);
109 txdata(app,verb,len);
110 P5OUT|=SS; //Raise !SS to end transaction.
112 case POKE://Poke up bytes from an SPI Flash ROM.
115 case SPI_ERASE://Erase the SPI Flash ROM.
117 P5OUT&=~SS; //Drop !SS to begin transaction.
118 spitrans8(0xC7);//Chip Erase
119 P5OUT|=SS; //Raise !SS to end transaction.