Beginning support for the second UART.
[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   //Begin a new transaction.
40   P5OUT&=~SS; 
41   P5OUT|=SS;
42 }
43
44 //! Read and write an SPI bit.
45 unsigned char spitrans8(unsigned char byte){
46   unsigned int bit;
47   //This function came from the SPI Wikipedia article.
48   //Minor alterations.
49   
50   for (bit = 0; bit < 8; bit++) {
51     /* write MOSI on trailing edge of previous clock */
52     if (byte & 0x80)
53       SETMOSI;
54     else
55       CLRMOSI;
56     byte <<= 1;
57  
58     /* half a clock cycle before leading/rising edge */
59     SPIDELAY(SPISPEED/2);
60     SETCLK;
61  
62     /* half a clock cycle before trailing/falling edge */
63     SPIDELAY(SPISPEED/2);
64  
65     /* read MISO on trailing edge */
66     byte |= READMISO;
67     CLRCLK;
68   }
69   
70   return byte;
71 }
72
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.
81 }
82
83 //! Grab the SPI flash status byte.
84 unsigned char spiflash_status(){
85   unsigned char c;
86   P5OUT|=SS;  //Raise !SS to end transaction.
87   P5OUT&=~SS; //Drop !SS to begin transaction.
88   spitrans8(0x05);//GET STATUS
89   c=spitrans8(0xFF);
90   P5OUT|=SS;  //Raise !SS to end transaction.
91   return c;
92 }
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
97   spitrans8(c);
98   P5OUT|=SS;  //Raise !SS to end transaction.
99   return c;
100 }
101
102 //! Handles a monitor command.
103 void spihandle(unsigned char app,
104                unsigned char verb,
105                unsigned char len){
106   unsigned char i;
107   
108   
109   //Raise !SS to end transaction, just in case we forgot.
110   P5OUT|=SS;  
111   
112   switch(verb){
113     //PEEK and POKE might come later.
114   case READ:
115   case WRITE:
116     P5OUT&=~SS; //Drop !SS to begin transaction.
117     for(i=0;i<len;i++)
118       cmddata[i]=spitrans8(cmddata[i]);
119     P5OUT|=SS;  //Raise !SS to end transaction.
120     txdata(app,verb,len);
121     break;
122   case SPI_JEDEC://Grab 3-byte JEDEC ID.
123     P5OUT&=~SS; //Drop !SS to begin transaction.
124     spitrans8(0x9f);
125     len=3;
126     for(i=0;i<len;i++)
127       cmddata[i]=spitrans8(cmddata[i]);
128     txdata(app,verb,len);
129     P5OUT|=SS;  //Raise !SS to end transaction.
130     break;
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
135     for(i=0;i<len;i++)
136       spitrans8(cmddata[i]);
137     len=0x80;//128 byte chunk
138     for(i=0;i<len;i++)
139       cmddata[i]=spitrans8(0);
140     P5OUT|=SS;  //Raise !SS to end transaction.
141     txdata(app,verb,len);
142     break;
143   case POKE://Poke up bytes from an SPI Flash ROM.
144     spiflash_setstatus(0x02);
145     spiflash_wrten();
146     
147     P5OUT&=~SS; //Drop !SS to begin transaction.
148     spitrans8(0x02); //Poke command.
149     
150     //First three bytes are address, then data.
151     for(i=0;i<len;i++)
152       spitrans8(cmddata[i]);
153     P5OUT|=SS;  //Raise !SS to end transaction.
154     while(spiflash_status()&0x01);//while busy
155     txdata(app,verb,len);
156     break;
157   case SPI_ERASE://Erase the SPI Flash ROM.
158     spiflash_wrten();
159     P5OUT&=~SS; //Drop !SS to begin transaction.
160     spitrans8(0xC7);//Chip Erase
161     P5OUT|=SS;  //Raise !SS to end transaction.
162     txdata(app,verb,0);
163     break;
164   case SETUP:
165     spisetup();
166     txdata(app,verb,0);
167     break;
168   }
169   
170 }