SPI Flash Erase command.
[goodfet] / firmware / apps / spi / spi.c
1 //GoodFET SPI Application
2 //Handles basic I/O
3
4 //Higher level left to client application.
5
6 #include "platform.h"
7 #include "command.h"
8
9 #include <signal.h>
10 #include <io.h>
11 #include <iomacros.h>
12
13
14 //Pins and I/O
15 #define SS   BIT0
16 #define MOSI BIT1
17 #define MISO BIT2
18 #define SCK  BIT3
19
20 //This could be more accurate.
21 //Does it ever need to be?
22 #define SPISPEED 0
23 #define SPIDELAY(x) delay(x)
24
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)
30
31
32
33 //! Set up the pins for SPI mode.
34 void spisetup(){
35   P5OUT|=SS;
36   P5DIR|=MOSI+SCK+SS;
37   P5DIR&=~MISO;
38 }
39
40 //! Read and write an SPI bit.
41 unsigned char spitrans8(unsigned char byte){
42   unsigned int bit;
43   //This function came from the SPI Wikipedia article.
44   //Minor alterations.
45   
46   for (bit = 0; bit < 8; bit++) {
47     /* write MOSI on trailing edge of previous clock */
48     if (byte & 0x80)
49       SETMOSI;
50     else
51       CLRMOSI;
52     byte <<= 1;
53  
54     /* half a clock cycle before leading/rising edge */
55     SPIDELAY(SPISPEED/2);
56     SETCLK;
57  
58     /* half a clock cycle before trailing/falling edge */
59     SPIDELAY(SPISPEED/2);
60  
61     /* read MISO on trailing edge */
62     byte |= READMISO;
63     CLRCLK;
64   }
65   
66   return byte;
67 }
68
69 //! Enable SPI writing
70 void spiflash_wrten(){
71     P5OUT&=~SS; //Drop !SS to begin transaction.
72     spitrans8(0x06);//Chip Erase
73     P5OUT|=SS;  //Raise !SS to end transaction.
74 }
75
76 //! Handles a monitor command.
77 void spihandle(unsigned char app,
78                unsigned char verb,
79                unsigned char len){
80   unsigned char i;
81   switch(verb){
82     //PEEK and POKE might come later.
83   case READ:
84   case WRITE:
85     P5OUT&=~SS; //Drop !SS to begin transaction.
86     for(i=0;i<len;i++)
87       cmddata[i]=spitrans8(cmddata[i]);
88     P5OUT|=SS;  //Raise !SS to end transaction.
89     txdata(app,verb,len);
90     break;
91   case SPI_JEDEC://Grab 3-byte JEDEC ID.
92     P5OUT&=~SS; //Drop !SS to begin transaction.
93     spitrans8(0x9f);
94     len=3;
95     for(i=0;i<len;i++)
96       cmddata[i]=spitrans8(cmddata[i]);
97     txdata(app,verb,len);
98     P5OUT|=SS;  //Raise !SS to end transaction.
99     break;
100   case PEEK://Grab 128 bytes from an SPI Flash ROM
101     P5OUT&=~SS; //Drop !SS to begin transaction.
102     spitrans8(0x03);//Flash Read Command
103     len=3;//write 3 byte pointer
104     for(i=0;i<len;i++)
105       spitrans8(cmddata[i]);
106     len=0x80;//128 byte chunk
107     for(i=0;i<len;i++)
108       cmddata[i]=spitrans8(0);
109     txdata(app,verb,len);
110     P5OUT|=SS;  //Raise !SS to end transaction.
111     break;
112   case POKE://Poke up bytes from an SPI Flash ROM.
113     
114     break;
115   case SPI_ERASE://Erase the SPI Flash ROM.
116     spiflash_wrten();
117     P5OUT&=~SS; //Drop !SS to begin transaction.
118     spitrans8(0xC7);//Chip Erase
119     P5OUT|=SS;  //Raise !SS to end transaction.
120     txdata(app,verb,0);
121     break;
122   case SETUP:
123     spisetup();
124     txdata(app,verb,0);
125     break;
126   }
127 }