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.
40 //! Read and write an SPI bit.
41 unsigned char spitrans8(unsigned char byte){
43 //This function came from the SPI Wikipedia article.
46 for (bit = 0; bit < 8; bit++) {
47 /* write MOSI on trailing edge of previous clock */
54 /* half a clock cycle before leading/rising edge */
58 /* half a clock cycle before trailing/falling edge */
61 /* read MISO on trailing edge */
69 //! Enable SPI writing
70 void spiflash_wrten(){
71 P5OUT&=~SS; //Drop !SS to begin transaction.
72 spitrans8(0x04);//Write Disable
73 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.
79 //! Handles a monitor command.
80 void spihandle(unsigned char app,
85 //PEEK and POKE might come later.
88 P5OUT&=~SS; //Drop !SS to begin transaction.
90 cmddata[i]=spitrans8(cmddata[i]);
91 P5OUT|=SS; //Raise !SS to end transaction.
94 case SPI_JEDEC://Grab 3-byte JEDEC ID.
95 P5OUT&=~SS; //Drop !SS to begin transaction.
99 cmddata[i]=spitrans8(cmddata[i]);
100 txdata(app,verb,len);
101 P5OUT|=SS; //Raise !SS to end transaction.
103 case PEEK://Grab 128 bytes from an SPI Flash ROM
104 P5OUT&=~SS; //Drop !SS to begin transaction.
105 spitrans8(0x03);//Flash Read Command
106 len=3;//write 3 byte pointer
108 spitrans8(cmddata[i]);
109 len=0x80;//128 byte chunk
111 cmddata[i]=spitrans8(0);
112 txdata(app,verb,len);
113 P5OUT|=SS; //Raise !SS to end transaction.
115 case POKE://Poke up bytes from an SPI Flash ROM.
117 P5OUT&=~SS; //Drop !SS to begin transaction.
118 spitrans8(0x02); //Poke command.
120 //First three bytes are address, then data.
122 spitrans8(cmddata[i]);
123 P5OUT&=~SS; //Drop !SS to begin transaction.
124 txdata(app,verb,len);
126 case SPI_ERASE://Erase the SPI Flash ROM.
128 P5OUT&=~SS; //Drop !SS to begin transaction.
129 spitrans8(0xC7);//Chip Erase
130 P5OUT|=SS; //Raise !SS to end transaction.