forked from jimmo/numato-mimasv2-pic-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi_flash.c
317 lines (262 loc) · 9.02 KB
/
spi_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <xc.h>
#include <stdint.h>
#include <stdio.h>
#include "usb_device_cdc.h"
#include "spi.h"
#include "spi_flash.h"
#define SI_PORT PORTCbits.RC7
#define SO_PORT PORTBbits.RB4
#define CS_PORT PORTCbits.RC2
#define CLK_PORT PORTBbits.RB6
#define PROGB_PORT PORTCbits.RC1
#define CDONE_PORT PORTCbits.RC0
#define INITB_PORT PORTCbits.RC5
#define SI_LATCH LATCbits.LATC7
#define SO_LATCH LATBbits.LATB4
#define CS_LATCH LATCbits.LATC2
#define CLK_LATCH LATBbits.LATB6
#define PROGB_LATCH LATCbits.LATC1
#define CDONE_LATCH LATCbits.LATC0
#define INITB_LATCH LATCbits.LATC5
#define SI_TRIS TRISCbits.TRISC7
#define SO_TRIS TRISBbits.TRISB4
#define CS_TRIS TRISCbits.TRISC2
#define CLK_TRIS TRISBbits.TRISB6
#define PROGB_TRIS TRISCbits.TRISC1
#define CDONE_TRIS TRISCbits.TRISC0
#define INITB_TRIS TRISCbits.TRISC5
char USB_Out_Buffer[CDC_DATA_OUT_EP_SIZE];
PTR_SPARTAN_3A_CONFIG_IN_PACKET PtrRespPacket = (PTR_SPARTAN_3A_CONFIG_IN_PACKET) USB_Out_Buffer;
void SPIOpen(unsigned char SyncMode, unsigned char BusMode, unsigned char SmpPhase);
void SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN Pin, unsigned char Direction);
void SPISetIOValue(SPARTAN_3A_CONFIG_IO_PIN Pin, unsigned char Value);
unsigned char SPIGetIOValue(SPARTAN_3A_CONFIG_IO_PIN Pin);
void ServicePacket(PTR_SPARTAN_3A_CONFIG_OUT_PACKET ptrPacket);
unsigned char USBSendPacket(unsigned char* data, unsigned char Size);
void USBSendStatus(SPARTAN_3A_CONFIG_STATUS Status, uint8_t SPINum, uint8_t ExecutedCmd);
void SPIOpen(unsigned char SyncMode, unsigned char BusMode, unsigned char SmpPhase)
{
//OpenSPI(SyncMode, BusMode, SmpPhase);
//Set SDO SPI Pin directions
ANSELHbits.ANS9 = 0;// RC7 as digital IO
ANSELHbits.ANS10 = 0;// RB4 as digital IO
TRISCbits.TRISC7 = 0;
TRISBbits.TRISB4 = 1;
TRISBbits.TRISB6 = 0;
//SSPSTAT settings
SSPSTATbits.SMP = 0; //Sample at middle
SSPSTATbits.CKE = 1;
// Run at Fosc/4
SSPADD = 0;
//SSPCON1 Setings
SSPCON1bits.CKP = 0;
SSPCON1bits.SSPM3 = 0;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM1 = 1;
SSPCON1bits.SSPM0 = 0;
//Disable SSP interrupt
PIE1bits.SSPIE = 0;
//Enable MSSP
SSPCON1bits.SSPEN = 1;
}
void SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN Pin, unsigned char Direction)
{
switch(Pin)
{
case SPARTAN_3A_CONFIG_IO_PIN_SI:
SI_TRIS = Direction;
break;
case SPARTAN_3A_CONFIG_IO_PIN_SO:
SO_TRIS = Direction;
break;
case SPARTAN_3A_CONFIG_IO_PIN_CS:
CS_TRIS = Direction;
break;
case SPARTAN_3A_CONFIG_IO_PIN_CLK:
CLK_TRIS = Direction;
break;
case SPARTAN_3A_CONFIG_IO_PIN_PROGB:
PROGB_TRIS = Direction;
break;
case SPARTAN_3A_CONFIG_IO_PIN_DONE:
CDONE_TRIS = Direction;
break;
case SPARTAN_3A_CONFIG_IO_PIN_INITB:
INITB_TRIS = Direction;
break;
}
}
void SPISetIOValue(SPARTAN_3A_CONFIG_IO_PIN Pin, unsigned char Value)
{
switch(Pin)
{
case SPARTAN_3A_CONFIG_IO_PIN_SI:
SI_LATCH = Value;
break;
case SPARTAN_3A_CONFIG_IO_PIN_SO:
SO_LATCH = Value;
break;
case SPARTAN_3A_CONFIG_IO_PIN_CS:
CS_LATCH = Value;
break;
case SPARTAN_3A_CONFIG_IO_PIN_CLK:
CLK_LATCH = Value;
break;
case SPARTAN_3A_CONFIG_IO_PIN_PROGB:
PROGB_LATCH = Value;
break;
case SPARTAN_3A_CONFIG_IO_PIN_DONE:
CDONE_LATCH = Value;
break;
case SPARTAN_3A_CONFIG_IO_PIN_INITB:
INITB_LATCH = Value;
break;
}
}
unsigned char SPIGetIOValue(SPARTAN_3A_CONFIG_IO_PIN Pin)
{
unsigned char tmpValue = 0xFF;
switch(Pin)
{
case SPARTAN_3A_CONFIG_IO_PIN_SI:
tmpValue = SI_PORT;
break;
case SPARTAN_3A_CONFIG_IO_PIN_SO:
tmpValue = SO_PORT;
break;
case SPARTAN_3A_CONFIG_IO_PIN_CS:
tmpValue = CS_PORT;
break;
case SPARTAN_3A_CONFIG_IO_PIN_CLK:
tmpValue = CLK_PORT;
break;
case SPARTAN_3A_CONFIG_IO_PIN_PROGB:
tmpValue = PROGB_PORT;
break;
case SPARTAN_3A_CONFIG_IO_PIN_DONE:
tmpValue = CDONE_PORT;
break;
case SPARTAN_3A_CONFIG_IO_PIN_INITB:
tmpValue = INITB_PORT;
break;
}
return tmpValue;
}
void
Delay1KTCYx(unsigned char unit)
{
do {
_delay(1000);
} while(--unit != 0);
}
void ServicePacket(PTR_SPARTAN_3A_CONFIG_OUT_PACKET ptrPacket)
{
static char buf[14];
//memset(&Packet, 0, sizeof(SPARTAN_3A_CONFIG_IN_PACKET));
if(ptrPacket->RawPacket.Byte0 != '~')
{
//Incorrect packet delimiter
return;
}
if (ptrPacket->SpiOpen.SpiNum != 1) {
return;
}
if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_OPEN)
{
SPIOpen(ptrPacket->SpiOpen.SyncMode, ptrPacket->SpiOpen.BusMode, ptrPacket->SpiOpen.SmpPhase);
USBSendStatus(STATUS_SUCCESS, ptrPacket->SpiOpen.SpiNum, ptrPacket->RawPacket.Byte1);
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_CLOSE)
{
CloseSPI();
USBSendStatus(STATUS_SUCCESS, ptrPacket->SPIClose.SpiNum, ptrPacket->RawPacket.Byte1);
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_GETSTRING)
{
PtrRespPacket->SPIBuffer.Tilda = '~';
PtrRespPacket->SPIBuffer.PacketType = SPARTAN_3A_CONFIG_IN_PACKET_BUFFER;
PtrRespPacket->SPIBuffer.SpiNum = ptrPacket->SPIGetString.SpiNum;
getsSPI(&PtrRespPacket->SPIBuffer.Data[0], ptrPacket->SPIGetString.Length);
USBSendPacket((unsigned char*)&PtrRespPacket->RawData[0], sizeof(SPARTAN_3A_CONFIG_IN_PACKET));
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_PUTSTRING)
{
putbufSPI(&ptrPacket->SPIPutString.Data[0], ptrPacket->SPIPutString.Length);
USBSendStatus(STATUS_SUCCESS, ptrPacket->SPIPutString.SpiNum, ptrPacket->RawPacket.Byte1);
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_GET_CHAR)
{
PtrRespPacket->SPIBuffer.Tilda = '~';
PtrRespPacket->SPIBuffer.PacketType = SPARTAN_3A_CONFIG_IN_PACKET_BUFFER;
PtrRespPacket->SPIBuffer.SpiNum = ptrPacket->SPIGetChar.SpiNum;
PtrRespPacket->SPIBuffer.Data[0] = ReadSPI();
USBSendPacket((unsigned char*)&PtrRespPacket->RawData[0], sizeof(SPARTAN_3A_CONFIG_IN_PACKET));
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_PUT_CHAR)
{
WriteSPI(ptrPacket->SPIPutChar.Data);
USBSendStatus(STATUS_SUCCESS, ptrPacket->SPIPutChar.SpiNum, ptrPacket->RawPacket.Byte1);
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_SET_IO_DIR)
{
SPISetIODirection(ptrPacket->SetIODir.Io, ptrPacket->SetIODir.Direction);
USBSendStatus(STATUS_SUCCESS, ptrPacket->SetIODir.SpiNum, ptrPacket->RawPacket.Byte1);
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_SET_IO_VALUE)
{
SPISetIOValue(ptrPacket->SetIOValue.Io, ptrPacket->SetIOValue.Value);
USBSendStatus(STATUS_SUCCESS, ptrPacket->SetIOValue.SpiNum, ptrPacket->RawPacket.Byte1);
}
else if(ptrPacket->RawPacket.Byte1 == SPARTAN_3A_CONFIG_OUT_PACKET_SPI_GET_IO_VALUE)
{
PtrRespPacket->SPIBuffer.Tilda = '~';
PtrRespPacket->SPIBuffer.PacketType = SPARTAN_3A_CONFIG_IN_PACKET_BUFFER;
PtrRespPacket->SPIBuffer.SpiNum = ptrPacket->GetIOValue.SpiNum;
PtrRespPacket->SPIBuffer.Data[0] = SPIGetIOValue(ptrPacket->GetIOValue.Io);
USBSendPacket((unsigned char*)&PtrRespPacket->RawData[0], sizeof(SPARTAN_3A_CONFIG_IN_PACKET));
}
}
void USBSendStatus(SPARTAN_3A_CONFIG_STATUS Status, uint8_t SPINum, uint8_t ExecutedCmd)
{
PtrRespPacket->Status.Tilda = '~';
PtrRespPacket->Status.PacketType = SPARTAN_3A_CONFIG_IN_PACKET_STATUS;
PtrRespPacket->Status.SpiNum = SPINum;
PtrRespPacket->Status.Status = (unsigned char)Status;
PtrRespPacket->Status.ExecutedCmd = ExecutedCmd;
USBSendPacket((unsigned char*)(char*)&PtrRespPacket->RawData[0], sizeof(SPARTAN_3A_CONFIG_IN_PACKET));
}
unsigned char USBSendPacket(unsigned char* data, unsigned char Size)
{
putUSBUSART(SPI_CDC_PORT, (char*)data, Size);
return 0;
}
void SpiFlashInit(void) {
//Set all pins except PROGB as inputs
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_SI, IO_DIRECTION_IN);
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_SO, IO_DIRECTION_IN);
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_CS, IO_DIRECTION_IN);
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_CLK, IO_DIRECTION_IN);
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_DONE, IO_DIRECTION_IN);
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_INITB, IO_DIRECTION_IN);
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_PROGB, IO_DIRECTION_OUT);
//Pull PROG_B Low for a few milliseconds to start configuration
SPISetIOValue(SPARTAN_3A_CONFIG_IO_PIN_PROGB, 0);
Delay1KTCYx(100);
SPISetIOValue(SPARTAN_3A_CONFIG_IO_PIN_PROGB, 1);
//Leave PROGB in HiZ state
SPISetIODirection(SPARTAN_3A_CONFIG_IO_PIN_PROGB, IO_DIRECTION_IN);
}
// Data arriving from USB. All messages are padded to 70 bytes.
static uint8_t usb_rx_buf[64+6] = {0};
static uint8_t usb_rx_avail = 0;
void SpiFlashTask(void) {
uint8_t bytes_read = getsUSBUSART(SPI_CDC_PORT, usb_rx_buf + usb_rx_avail, sizeof(usb_rx_buf) - usb_rx_avail);
if (bytes_read) {
usb_rx_avail += bytes_read;
}
if (usb_rx_avail == sizeof(usb_rx_buf)) {
ServicePacket((PTR_SPARTAN_3A_CONFIG_OUT_PACKET)usb_rx_buf);
usb_rx_avail = 0;
return;
}
}