2 \author Travis Goodspeed
6 //Higher level left to client application.
17 //This could be more accurate.
18 //Does it ever need to be?
24 //! Set up the pins for SPI mode.
30 //Begin a new transaction.
36 //! Read and write an SPI byte.
37 unsigned char spitrans8(unsigned char byte){
39 //This function came from the SPI Wikipedia article.
42 for (bit = 0; bit < 8; bit++) {
43 /* write MOSI on trailing edge of previous clock */
50 /* half a clock cycle before leading/rising edge */
54 /* half a clock cycle before trailing/falling edge */
57 /* read MISO on trailing edge */
66 //! Enable SPI writing
67 void spiflash_wrten(){
69 P5OUT&=~SS; //Drop !SS to begin transaction.
70 spitrans8(0x04);//Write Disable
71 P5OUT|=SS; //Raise !SS to end transaction.
72 P5OUT&=~SS; //Drop !SS to begin transaction.
73 spitrans8(0x06);//Write Enable
74 P5OUT|=SS; //Raise !SS to end transaction.
78 //! Grab the SPI flash status byte.
79 unsigned char spiflash_status(){
81 P5OUT|=SS; //Raise !SS to end transaction.
82 P5OUT&=~SS; //Drop !SS to begin transaction.
83 spitrans8(0x05);//GET STATUS
85 P5OUT|=SS; //Raise !SS to end transaction.
90 //! Grab the SPI flash status byte.
91 void spiflash_setstatus(unsigned char c){
93 CLRSS; //Drop !SS to begin transaction.
94 spitrans8(0x01);//SET STATUS
96 SETSS; //Raise !SS to end transaction.
101 //! Read a block to a buffer.
102 void spiflash_peekblock(unsigned long adr,
108 CLRSS; //Drop !SS to begin transaction.
109 spitrans8(0x03);//Flash Read Command
112 spitrans8((adr&0xFF0000)>>16);
113 spitrans8((adr&0xFF00)>>8);
118 SETSS; //Raise !SS to end transaction.
122 //! Read a block to a buffer.
123 void spiflash_pokeblock(unsigned long adr,
130 spiflash_setstatus(0x02);
133 CLRSS; //Drop !SS to begin transaction.
134 spitrans8(0x02); //Poke command.
137 spitrans8((adr&0xFF0000)>>16);
138 spitrans8((adr&0xFF00)>>8);
143 SETSS; //Raise !SS to end transaction.
145 //while(spiflash_status()&0x01);
151 //! Peek some blocks.
152 void spiflash_peek(unsigned char app,
155 register char blocks=(len>3?cmddata[3]:1);
158 P5OUT&=~SS; //Drop !SS to begin transaction.
159 spitrans8(0x03);//Flash Read Command
160 len=3;//write 3 byte pointer
162 spitrans8(cmddata[i]);
165 len=0x80;//128 byte chunk, repeated for each block
168 serial_tx(len); //multiplied by block count.
172 serial_tx(spitrans8(0));
176 cmddata[i]=spitrans8(0);
177 txdata(app,verb,len);
180 P5OUT|=SS; //Raise !SS to end transaction.
183 //! Handles a monitor command.
184 void spihandle(unsigned char app,
189 //Raise !SS to end transaction, just in case we forgot.
194 //PEEK and POKE might come later.
197 P5OUT&=~SS; //Drop !SS to begin transaction.
199 cmddata[i]=spitrans8(cmddata[i]);
200 P5OUT|=SS; //Raise !SS to end transaction.
201 txdata(app,verb,len);
205 case SPI_JEDEC://Grab 3-byte JEDEC ID.
206 P5OUT&=~SS; //Drop !SS to begin transaction.
210 cmddata[i]=spitrans8(cmddata[i]);
211 txdata(app,verb,len);
212 P5OUT|=SS; //Raise !SS to end transaction.
216 case PEEK://Grab 128 bytes from an SPI Flash ROM
217 spiflash_peek(app,verb,len);
221 case POKE://Poke up bytes from an SPI Flash ROM.
222 spiflash_setstatus(0x02);
225 P5OUT&=~SS; //Drop !SS to begin transaction.
226 spitrans8(0x02); //Poke command.
228 //First three bytes are address, then data.
230 spitrans8(cmddata[i]);
231 P5OUT|=SS; //Raise !SS to end transaction.
234 while(spiflash_status()&0x01)
238 txdata(app,verb,len);
242 case SPI_ERASE://Erase the SPI Flash ROM.
244 P5OUT&=~SS; //Drop !SS to begin transaction.
245 spitrans8(0xC7);//Chip Erase
246 P5OUT|=SS; //Raise !SS to end transaction.
249 while(spiflash_status()&0x01)//while busy