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 //! Write many blocks to the SPI Flash.
119 void spiflash_pokeblocks(unsigned long adr,
122 long off=0;//offset of this block
123 int blen;//length of this block
125 spiflash_setstatus(0x02);
129 //calculate block length
130 blen=(len-off>0x100?0x100:len-off);
132 spiflash_pokeblock(adr+off,
140 //! Read a block to a buffer.
141 void spiflash_pokeblock(unsigned long adr,
148 //while(spiflash_status()&0x01);//minor performance impact
150 //Are these necessary?
151 //spiflash_setstatus(0x02);
154 CLRSS; //Drop !SS to begin transaction.
155 spitrans8(0x02); //Poke command.
158 spitrans8((adr&0xFF0000)>>16);
159 spitrans8((adr&0xFF00)>>8);
164 SETSS; //Raise !SS to end transaction.
166 while(spiflash_status()&0x01);//minor performance impact
171 //! Peek some blocks.
172 void spiflash_peek(unsigned char app,
176 P5OUT&=~SS; //Drop !SS to begin transaction.
177 spitrans8(0x03);//Flash Read Command
178 len=3;//write 3 byte pointer
180 spitrans8(cmddata[i]);
184 txhead(app,verb,len);
187 serial_tx(spitrans8(0));
189 P5OUT|=SS; //Raise !SS to end transaction.
192 //! Handles a monitor command.
193 void spihandle(unsigned char app,
198 //Raise !SS to end transaction, just in case we forgot.
203 //PEEK and POKE might come later.
206 P5OUT&=~SS; //Drop !SS to begin transaction.
208 cmddata[i]=spitrans8(cmddata[i]);
209 P5OUT|=SS; //Raise !SS to end transaction.
210 txdata(app,verb,len);
214 case SPI_JEDEC://Grab 3-byte JEDEC ID.
215 P5OUT&=~SS; //Drop !SS to begin transaction.
217 len=3; //Length is variable in some chips, 3 minimum.
219 cmddata[i]=spitrans8(cmddata[i]);
220 txdata(app,verb,len);
221 P5OUT|=SS; //Raise !SS to end transaction.
225 case PEEK://Grab 128 bytes from an SPI Flash ROM
226 spiflash_peek(app,verb,len);
230 case POKE://Poke up bytes from an SPI Flash ROM.
231 spiflash_pokeblocks(cmddatalong[0],//adr
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