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(){
70 P5OUT&=~SS; //Drop !SS to begin transaction.
71 spitrans8(0x04);//Write Disable
72 P5OUT|=SS; //Raise !SS to end transaction.
74 P5OUT&=~SS; //Drop !SS to begin transaction.
75 spitrans8(0x06);//Write Enable
76 P5OUT|=SS; //Raise !SS to end transaction.
80 //! Grab the SPI flash status byte.
81 unsigned char spiflash_status(){
83 P5OUT|=SS; //Raise !SS to end transaction.
84 P5OUT&=~SS; //Drop !SS to begin transaction.
85 spitrans8(0x05);//GET STATUS
87 P5OUT|=SS; //Raise !SS to end transaction.
92 //! Grab the SPI flash status byte.
93 void spiflash_setstatus(unsigned char c){
95 CLRSS; //Drop !SS to begin transaction.
96 spitrans8(0x01);//SET STATUS
98 SETSS; //Raise !SS to end transaction.
103 //! Read a block to a buffer.
104 void spiflash_peekblock(unsigned long adr,
110 CLRSS; //Drop !SS to begin transaction.
111 spitrans8(0x03);//Flash Read Command
114 spitrans8((adr&0xFF0000)>>16);
115 spitrans8((adr&0xFF00)>>8);
120 SETSS; //Raise !SS to end transaction.
124 //! Read a block to a buffer.
125 void spiflash_pokeblock(unsigned long adr,
132 spiflash_setstatus(0x02);
135 CLRSS; //Drop !SS to begin transaction.
136 spitrans8(0x02); //Poke command.
139 spitrans8((adr&0xFF0000)>>16);
140 spitrans8((adr&0xFF00)>>8);
145 SETSS; //Raise !SS to end transaction.
147 while(spiflash_status()&0x01);
153 //! Peek some blocks.
154 void spiflash_peek(unsigned char app,
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]);
166 txhead(app,verb,len);
169 serial_tx(spitrans8(0));
171 P5OUT|=SS; //Raise !SS to end transaction.
174 //! Handles a monitor command.
175 void spihandle(unsigned char app,
180 //Raise !SS to end transaction, just in case we forgot.
185 //PEEK and POKE might come later.
188 P5OUT&=~SS; //Drop !SS to begin transaction.
190 cmddata[i]=spitrans8(cmddata[i]);
191 P5OUT|=SS; //Raise !SS to end transaction.
192 txdata(app,verb,len);
196 case SPI_JEDEC://Grab 3-byte JEDEC ID.
197 P5OUT&=~SS; //Drop !SS to begin transaction.
199 len=3; //Length is variable in some chips, 3 minimum.
201 cmddata[i]=spitrans8(cmddata[i]);
202 txdata(app,verb,len);
203 P5OUT|=SS; //Raise !SS to end transaction.
207 case PEEK://Grab 128 bytes from an SPI Flash ROM
208 spiflash_peek(app,verb,len);
212 case POKE://Poke up bytes from an SPI Flash ROM.
213 spiflash_setstatus(0x02);
216 P5OUT&=~SS; //Drop !SS to begin transaction.
217 spitrans8(0x02); //Poke command.
219 //First three bytes are address, then data.
221 spitrans8(cmddata[i]);
222 P5OUT|=SS; //Raise !SS to end transaction.
225 while(spiflash_status()&0x01)
230 txdata(app,verb,len);
234 case SPI_ERASE://Erase the SPI Flash ROM.
236 P5OUT&=~SS; //Drop !SS to begin transaction.
237 spitrans8(0xC7);//Chip Erase
238 P5OUT|=SS; //Raise !SS to end transaction.
241 while(spiflash_status()&0x01)//while busy