1 //GoodFET SPI Application
4 //Higher level left to client application.
20 //This could be more accurate.
21 //Does it ever need to be?
23 #define SPIDELAY(x) delay(x)
25 #define SETMOSI P5OUT|=MOSI
26 #define CLRMOSI P5OUT&=~MOSI
27 #define SETCLK P5OUT|=SCK
28 #define CLRCLK P5OUT&=~SCK
29 #define READMISO (P5IN&MISO?1:0)
33 //! Set up the pins for SPI mode.
39 //Begin a new transaction.
44 //! Read and write an SPI bit.
45 unsigned char spitrans8(unsigned char byte){
47 //This function came from the SPI Wikipedia article.
50 for (bit = 0; bit < 8; bit++) {
51 /* write MOSI on trailing edge of previous clock */
58 /* half a clock cycle before leading/rising edge */
62 /* half a clock cycle before trailing/falling edge */
65 /* read MISO on trailing edge */
73 //! Enable SPI writing
74 void spiflash_wrten(){
75 P5OUT&=~SS; //Drop !SS to begin transaction.
76 spitrans8(0x04);//Write Disable
77 P5OUT|=SS; //Raise !SS to end transaction.
78 P5OUT&=~SS; //Drop !SS to begin transaction.
79 spitrans8(0x06);//Write Enable
80 P5OUT|=SS; //Raise !SS to end transaction.
83 //! Grab the SPI flash status byte.
84 unsigned char spiflash_status(){
86 P5OUT|=SS; //Raise !SS to end transaction.
87 P5OUT&=~SS; //Drop !SS to begin transaction.
88 spitrans8(0x05);//GET STATUS
90 P5OUT|=SS; //Raise !SS to end transaction.
93 //! Grab the SPI flash status byte.
94 void spiflash_setstatus(unsigned char c){
95 P5OUT&=~SS; //Drop !SS to begin transaction.
96 spitrans8(0x01);//SET STATUS
98 P5OUT|=SS; //Raise !SS to end transaction.
102 //! Handles a monitor command.
103 void spihandle(unsigned char app,
109 //Raise !SS to end transaction, just in case we forgot.
113 //PEEK and POKE might come later.
116 P5OUT&=~SS; //Drop !SS to begin transaction.
118 cmddata[i]=spitrans8(cmddata[i]);
119 P5OUT|=SS; //Raise !SS to end transaction.
120 txdata(app,verb,len);
122 case SPI_JEDEC://Grab 3-byte JEDEC ID.
123 P5OUT&=~SS; //Drop !SS to begin transaction.
127 cmddata[i]=spitrans8(cmddata[i]);
128 txdata(app,verb,len);
129 P5OUT|=SS; //Raise !SS to end transaction.
131 case PEEK://Grab 128 bytes from an SPI Flash ROM
132 P5OUT&=~SS; //Drop !SS to begin transaction.
133 spitrans8(0x03);//Flash Read Command
134 len=3;//write 3 byte pointer
136 spitrans8(cmddata[i]);
137 len=0x80;//128 byte chunk
139 cmddata[i]=spitrans8(0);
140 P5OUT|=SS; //Raise !SS to end transaction.
141 txdata(app,verb,len);
143 case POKE://Poke up bytes from an SPI Flash ROM.
144 spiflash_setstatus(0x02);
147 P5OUT&=~SS; //Drop !SS to begin transaction.
148 spitrans8(0x02); //Poke command.
150 //First three bytes are address, then data.
152 spitrans8(cmddata[i]);
153 P5OUT|=SS; //Raise !SS to end transaction.
154 while(spiflash_status()&0x01);//while busy
155 txdata(app,verb,len);
157 case SPI_ERASE://Erase the SPI Flash ROM.
159 P5OUT&=~SS; //Drop !SS to begin transaction.
160 spitrans8(0xC7);//Chip Erase
161 P5OUT|=SS; //Raise !SS to end transaction.