1 //GoodFET SPI Application
4 //Higher level left to client application.
21 //This could be more accurate.
22 //Does it ever need to be?
24 #define SPIDELAY(x) delay(x)
26 #define SETMOSI P5OUT|=MOSI
27 #define CLRMOSI P5OUT&=~MOSI
28 #define SETCLK P5OUT|=SCK
29 #define CLRCLK P5OUT&=~SCK
30 #define READMISO (P5IN&MISO?1:0)
34 //! Set up the pins for SPI mode.
40 //Begin a new transaction.
46 //! Read and write an SPI bit.
47 unsigned char spitrans8(unsigned char byte){
49 //This function came from the SPI Wikipedia article.
52 for (bit = 0; bit < 8; bit++) {
53 /* write MOSI on trailing edge of previous clock */
60 /* half a clock cycle before leading/rising edge */
64 /* half a clock cycle before trailing/falling edge */
67 /* read MISO on trailing edge */
76 //! Enable SPI writing
77 void spiflash_wrten(){
78 P5OUT&=~SS; //Drop !SS to begin transaction.
79 spitrans8(0x04);//Write Disable
80 P5OUT|=SS; //Raise !SS to end transaction.
81 P5OUT&=~SS; //Drop !SS to begin transaction.
82 spitrans8(0x06);//Write Enable
83 P5OUT|=SS; //Raise !SS to end transaction.
87 //! Grab the SPI flash status byte.
88 unsigned char spiflash_status(){
90 P5OUT|=SS; //Raise !SS to end transaction.
91 P5OUT&=~SS; //Drop !SS to begin transaction.
92 spitrans8(0x05);//GET STATUS
94 P5OUT|=SS; //Raise !SS to end transaction.
99 //! Grab the SPI flash status byte.
100 void spiflash_setstatus(unsigned char c){
101 P5OUT&=~SS; //Drop !SS to begin transaction.
102 spitrans8(0x01);//SET STATUS
104 P5OUT|=SS; //Raise !SS to end transaction.
108 //! Peek some blocks.
109 void spiflash_peek(unsigned char app,
112 register char blocks=(len>3?cmddata[3]:1);
115 P5OUT&=~SS; //Drop !SS to begin transaction.
116 spitrans8(0x03);//Flash Read Command
117 len=3;//write 3 byte pointer
119 spitrans8(cmddata[i]);
122 len=0x80;//128 byte chunk, repeated for each block
125 serial_tx(len); //multiplied by block count.
129 serial_tx(spitrans8(0));
133 cmddata[i]=spitrans8(0);
134 txdata(app,verb,len);
137 P5OUT|=SS; //Raise !SS to end transaction.
140 //! Handles a monitor command.
141 void spihandle(unsigned char app,
147 //Raise !SS to end transaction, just in case we forgot.
151 //PEEK and POKE might come later.
154 P5OUT&=~SS; //Drop !SS to begin transaction.
156 cmddata[i]=spitrans8(cmddata[i]);
157 P5OUT|=SS; //Raise !SS to end transaction.
158 txdata(app,verb,len);
162 case SPI_JEDEC://Grab 3-byte JEDEC ID.
163 P5OUT&=~SS; //Drop !SS to begin transaction.
167 cmddata[i]=spitrans8(cmddata[i]);
168 txdata(app,verb,len);
169 P5OUT|=SS; //Raise !SS to end transaction.
173 case PEEK://Grab 128 bytes from an SPI Flash ROM
174 spiflash_peek(app,verb,len);
178 case POKE://Poke up bytes from an SPI Flash ROM.
179 spiflash_setstatus(0x02);
182 P5OUT&=~SS; //Drop !SS to begin transaction.
183 spitrans8(0x02); //Poke command.
185 //First three bytes are address, then data.
187 spitrans8(cmddata[i]);
188 P5OUT|=SS; //Raise !SS to end transaction.
190 while(spiflash_status()&0x01)//while busy
194 txdata(app,verb,len);
198 case SPI_ERASE://Erase the SPI Flash ROM.
200 P5OUT&=~SS; //Drop !SS to begin transaction.
201 spitrans8(0xC7);//Chip Erase
202 P5OUT|=SS; //Raise !SS to end transaction.