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){
38 register unsigned int bit;
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 */
52 /* read MISO on trailing edge */
61 //! Enable SPI writing
62 void spiflash_wrten(){
65 P5OUT&=~SS; //Drop !SS to begin transaction.
66 spitrans8(0x04);//Write Disable
67 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.
118 //! Read a block to a buffer.
119 void spiflash_pokeblock(unsigned long adr,
126 //while(spiflash_status()&0x01);//minor performance impact
128 //Are these necessary?
129 //spiflash_setstatus(0x02);
132 CLRSS; //Drop !SS to begin transaction.
133 spitrans8(0x02); //Poke command.
136 spitrans8((adr&0xFF0000)>>16);
137 spitrans8((adr&0xFF00)>>8);
142 SETSS; //Raise !SS to end transaction.
144 while(spiflash_status()&0x01);//minor performance impact
149 //! Write many blocks to the SPI Flash.
150 void spiflash_pokeblocks(unsigned long adr,
153 long off=0;//offset of this block
154 int blen;//length of this block
156 spiflash_setstatus(0x02);
160 //calculate block length
161 blen=(len-off>0x100?0x100:len-off);
163 spiflash_pokeblock(adr+off,
173 //! Peek some blocks.
174 void spiflash_peek(unsigned char app,
178 P5OUT&=~SS; //Drop !SS to begin transaction.
179 spitrans8(0x03);//Flash Read Command
180 len=3;//write 3 byte pointer
182 spitrans8(cmddata[i]);
186 txhead(app,verb,len);
189 serial_tx(spitrans8(0));
191 P5OUT|=SS; //Raise !SS to end transaction.
194 //! Handles a monitor command.
195 void spihandle(unsigned char app,
200 //Raise !SS to end transaction, just in case we forgot.
205 //PEEK and POKE might come later.
208 P5OUT&=~SS; //Drop !SS to begin transaction.
210 cmddata[i]=spitrans8(cmddata[i]);
211 P5OUT|=SS; //Raise !SS to end transaction.
212 txdata(app,verb,len);
216 case SPI_JEDEC://Grab 3-byte JEDEC ID.
217 P5OUT&=~SS; //Drop !SS to begin transaction.
219 len=3; //Length is variable in some chips, 3 minimum.
221 cmddata[i]=spitrans8(cmddata[i]);
222 txdata(app,verb,len);
223 P5OUT|=SS; //Raise !SS to end transaction.
227 case PEEK://Grab 128 bytes from an SPI Flash ROM
228 spiflash_peek(app,verb,len);
232 case POKE://Poke up bytes from an SPI Flash ROM.
233 spiflash_pokeblocks(cmddatalong[0],//adr
237 txdata(app,verb,len);
241 case SPI_ERASE://Erase the SPI Flash ROM.
243 P5OUT&=~SS; //Drop !SS to begin transaction.
244 spitrans8(0xC7);//Chip Erase
245 P5OUT|=SS; //Raise !SS to end transaction.
248 while(spiflash_status()&0x01)//while busy