1 //GoodFET SPI Application
4 //Higher level left to client application.
15 //This could be more accurate.
16 //Does it ever need to be?
18 #define SPIDELAY(x) delay(x)
21 //! Set up the pins for SPI mode.
27 //Begin a new transaction.
33 //! Read and write an SPI byte.
34 unsigned char spitrans8(unsigned char byte){
36 //This function came from the SPI Wikipedia article.
39 for (bit = 0; bit < 8; bit++) {
40 /* write MOSI on trailing edge of previous clock */
47 /* half a clock cycle before leading/rising edge */
51 /* half a clock cycle before trailing/falling edge */
54 /* read MISO on trailing edge */
63 //! Enable SPI writing
64 void spiflash_wrten(){
66 P5OUT&=~SS; //Drop !SS to begin transaction.
67 spitrans8(0x04);//Write Disable
68 P5OUT|=SS; //Raise !SS to end transaction.
69 P5OUT&=~SS; //Drop !SS to begin transaction.
70 spitrans8(0x06);//Write Enable
71 P5OUT|=SS; //Raise !SS to end transaction.
75 //! Grab the SPI flash status byte.
76 unsigned char spiflash_status(){
78 P5OUT|=SS; //Raise !SS to end transaction.
79 P5OUT&=~SS; //Drop !SS to begin transaction.
80 spitrans8(0x05);//GET STATUS
82 P5OUT|=SS; //Raise !SS to end transaction.
87 //! Grab the SPI flash status byte.
88 void spiflash_setstatus(unsigned char c){
90 CLRSS; //Drop !SS to begin transaction.
91 spitrans8(0x01);//SET STATUS
93 SETSS; //Raise !SS to end transaction.
98 //! Read a block to a buffer.
99 void spiflash_peekblock(unsigned long adr,
105 CLRSS; //Drop !SS to begin transaction.
106 spitrans8(0x03);//Flash Read Command
109 spitrans8((adr&0xFF0000)>>16);
110 spitrans8((adr&0xFF00)>>8);
115 SETSS; //Raise !SS to end transaction.
119 //! Read a block to a buffer.
120 void spiflash_pokeblock(unsigned long adr,
127 spiflash_setstatus(0x02);
130 CLRSS; //Drop !SS to begin transaction.
131 spitrans8(0x02); //Poke command.
134 spitrans8((adr&0xFF0000)>>16);
135 spitrans8((adr&0xFF00)>>8);
140 SETSS; //Raise !SS to end transaction.
142 while(spiflash_status()&0x01)
149 //! Peek some blocks.
150 void spiflash_peek(unsigned char app,
153 register char blocks=(len>3?cmddata[3]:1);
156 P5OUT&=~SS; //Drop !SS to begin transaction.
157 spitrans8(0x03);//Flash Read Command
158 len=3;//write 3 byte pointer
160 spitrans8(cmddata[i]);
163 len=0x80;//128 byte chunk, repeated for each block
166 serial_tx(len); //multiplied by block count.
170 serial_tx(spitrans8(0));
174 cmddata[i]=spitrans8(0);
175 txdata(app,verb,len);
178 P5OUT|=SS; //Raise !SS to end transaction.
181 //! Handles a monitor command.
182 void spihandle(unsigned char app,
187 //Raise !SS to end transaction, just in case we forgot.
191 //PEEK and POKE might come later.
194 P5OUT&=~SS; //Drop !SS to begin transaction.
196 cmddata[i]=spitrans8(cmddata[i]);
197 P5OUT|=SS; //Raise !SS to end transaction.
198 txdata(app,verb,len);
202 case SPI_JEDEC://Grab 3-byte JEDEC ID.
203 P5OUT&=~SS; //Drop !SS to begin transaction.
207 cmddata[i]=spitrans8(cmddata[i]);
208 txdata(app,verb,len);
209 P5OUT|=SS; //Raise !SS to end transaction.
213 case PEEK://Grab 128 bytes from an SPI Flash ROM
214 spiflash_peek(app,verb,len);
218 case POKE://Poke up bytes from an SPI Flash ROM.
219 spiflash_setstatus(0x02);
222 P5OUT&=~SS; //Drop !SS to begin transaction.
223 spitrans8(0x02); //Poke command.
225 //First three bytes are address, then data.
227 spitrans8(cmddata[i]);
228 P5OUT|=SS; //Raise !SS to end transaction.
231 while(spiflash_status()&0x01)//while busy
235 txdata(app,verb,len);
239 case SPI_ERASE://Erase the SPI Flash ROM.
241 P5OUT&=~SS; //Drop !SS to begin transaction.
242 spitrans8(0xC7);//Chip Erase
243 P5OUT|=SS; //Raise !SS to end transaction.
246 while(spiflash_status()&0x01)//while busy