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.
39 //Begin a new transaction.
44 //! Read and write an SPI bit.
45 unsigned char spitrans8(unsigned char byte){
47 //This function came from the SPI Wikipedia article.
50 for (bit = 0; bit < 8; bit++) {
51 /* write MOSI on trailing edge of previous clock */
58 /* half a clock cycle before leading/rising edge */
62 /* half a clock cycle before trailing/falling edge */
65 /* read MISO on trailing edge */
73 //! Enable SPI writing
74 void spiflash_wrten(){
75 P5OUT&=~SS; //Drop !SS to begin transaction.
76 spitrans8(0x04);//Write Disable
77 P5OUT|=SS; //Raise !SS to end transaction.
78 P5OUT&=~SS; //Drop !SS to begin transaction.
79 spitrans8(0x06);//Write Enable
80 P5OUT|=SS; //Raise !SS to end transaction.
83 //! Grab the SPI flash status byte.
84 unsigned char spiflash_status(){
86 P5OUT|=SS; //Raise !SS to end transaction.
87 P5OUT&=~SS; //Drop !SS to begin transaction.
88 spitrans8(0x05);//GET STATUS
90 P5OUT|=SS; //Raise !SS to end transaction.
93 //! Grab the SPI flash status byte.
94 void spiflash_setstatus(unsigned char c){
95 P5OUT&=~SS; //Drop !SS to begin transaction.
96 spitrans8(0x01);//SET STATUS
98 P5OUT|=SS; //Raise !SS to end transaction.
102 //! Peek some blocks.
103 void spiflash_peek(unsigned char app,
106 register char blocks=(len>3?cmddata[3]:1);
109 P5OUT&=~SS; //Drop !SS to begin transaction.
110 spitrans8(0x03);//Flash Read Command
111 len=3;//write 3 byte pointer
113 spitrans8(cmddata[i]);
116 len=0x80;//128 byte chunk, repeated for each block
119 serial_tx(len); //multiplied by block count.
123 serial_tx(spitrans8(0));
127 cmddata[i]=spitrans8(0);
128 txdata(app,verb,len);
131 P5OUT|=SS; //Raise !SS to end transaction.
134 //! Handles a monitor command.
135 void spihandle(unsigned char app,
141 //Raise !SS to end transaction, just in case we forgot.
145 //PEEK and POKE might come later.
148 P5OUT&=~SS; //Drop !SS to begin transaction.
150 cmddata[i]=spitrans8(cmddata[i]);
151 P5OUT|=SS; //Raise !SS to end transaction.
152 txdata(app,verb,len);
154 case SPI_JEDEC://Grab 3-byte JEDEC ID.
155 P5OUT&=~SS; //Drop !SS to begin transaction.
159 cmddata[i]=spitrans8(cmddata[i]);
160 txdata(app,verb,len);
161 P5OUT|=SS; //Raise !SS to end transaction.
163 case PEEK://Grab 128 bytes from an SPI Flash ROM
165 spiflash_peek(app,verb,len);
167 case POKE://Poke up bytes from an SPI Flash ROM.
168 spiflash_setstatus(0x02);
171 P5OUT&=~SS; //Drop !SS to begin transaction.
172 spitrans8(0x02); //Poke command.
174 //First three bytes are address, then data.
176 spitrans8(cmddata[i]);
177 P5OUT|=SS; //Raise !SS to end transaction.
178 while(spiflash_status()&0x01);//while busy
179 txdata(app,verb,len);
181 case SPI_ERASE://Erase the SPI Flash ROM.
183 P5OUT&=~SS; //Drop !SS to begin transaction.
184 spitrans8(0xC7);//Chip Erase
185 P5OUT|=SS; //Raise !SS to end transaction.