1 //GoodFET SPI Application
4 //Higher level left to client application.
15 //This could be more accurate.
16 //Does it ever need to be?
22 //! Set up the pins for SPI mode.
28 //Begin a new transaction.
34 //! Read and write an SPI byte.
35 unsigned char spitrans8(unsigned char byte){
37 //This function came from the SPI Wikipedia article.
40 for (bit = 0; bit < 8; bit++) {
41 /* write MOSI on trailing edge of previous clock */
48 /* half a clock cycle before leading/rising edge */
52 /* half a clock cycle before trailing/falling edge */
55 /* read MISO on trailing edge */
64 //! Enable SPI writing
65 void spiflash_wrten(){
67 P5OUT&=~SS; //Drop !SS to begin transaction.
68 spitrans8(0x04);//Write Disable
69 P5OUT|=SS; //Raise !SS to end transaction.
70 P5OUT&=~SS; //Drop !SS to begin transaction.
71 spitrans8(0x06);//Write Enable
72 P5OUT|=SS; //Raise !SS to end transaction.
76 //! Grab the SPI flash status byte.
77 unsigned char spiflash_status(){
79 P5OUT|=SS; //Raise !SS to end transaction.
80 P5OUT&=~SS; //Drop !SS to begin transaction.
81 spitrans8(0x05);//GET STATUS
83 P5OUT|=SS; //Raise !SS to end transaction.
88 //! Grab the SPI flash status byte.
89 void spiflash_setstatus(unsigned char c){
91 CLRSS; //Drop !SS to begin transaction.
92 spitrans8(0x01);//SET STATUS
94 SETSS; //Raise !SS to end transaction.
99 //! Read a block to a buffer.
100 void spiflash_peekblock(unsigned long adr,
106 CLRSS; //Drop !SS to begin transaction.
107 spitrans8(0x03);//Flash Read Command
110 spitrans8((adr&0xFF0000)>>16);
111 spitrans8((adr&0xFF00)>>8);
116 SETSS; //Raise !SS to end transaction.
120 //! Read a block to a buffer.
121 void spiflash_pokeblock(unsigned long adr,
128 spiflash_setstatus(0x02);
131 CLRSS; //Drop !SS to begin transaction.
132 spitrans8(0x02); //Poke command.
135 spitrans8((adr&0xFF0000)>>16);
136 spitrans8((adr&0xFF00)>>8);
141 SETSS; //Raise !SS to end transaction.
143 //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.
192 //PEEK and POKE might come later.
195 P5OUT&=~SS; //Drop !SS to begin transaction.
197 cmddata[i]=spitrans8(cmddata[i]);
198 P5OUT|=SS; //Raise !SS to end transaction.
199 txdata(app,verb,len);
203 case SPI_JEDEC://Grab 3-byte JEDEC ID.
204 P5OUT&=~SS; //Drop !SS to begin transaction.
208 cmddata[i]=spitrans8(cmddata[i]);
209 txdata(app,verb,len);
210 P5OUT|=SS; //Raise !SS to end transaction.
214 case PEEK://Grab 128 bytes from an SPI Flash ROM
215 spiflash_peek(app,verb,len);
219 case POKE://Poke up bytes from an SPI Flash ROM.
220 spiflash_setstatus(0x02);
223 P5OUT&=~SS; //Drop !SS to begin transaction.
224 spitrans8(0x02); //Poke command.
226 //First three bytes are address, then data.
228 spitrans8(cmddata[i]);
229 P5OUT|=SS; //Raise !SS to end transaction.
232 while(spiflash_status()&0x01)//while busy
236 txdata(app,verb,len);
240 case SPI_ERASE://Erase the SPI Flash ROM.
242 P5OUT&=~SS; //Drop !SS to begin transaction.
243 spitrans8(0xC7);//Chip Erase
244 P5OUT|=SS; //Raise !SS to end transaction.
247 while(spiflash_status()&0x01)//while busy