/
Rev3B_SPI.c
383 lines (363 loc) · 9.66 KB
/
Rev3B_SPI.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* Flash memory functions.
* These functions are used to interface with the flash memory unit on the device.
* It
*/
#include "p24FJ16GA002.h"
#include "REV3B_SPI.h"
/**
* Initialize Flash Memory
*/
/*
void FLASH_MEM_Init (void)
{
//MISO
MISO_TRIS = 1; //Set pin as input
//Assign function of SDI1R (in peripheral input register 20) to pin RP5
RPINR20bits.SDI1R = 5;
//MOSI
MOSI_TRIS = 0; //Set pin as output
//Assign pin RP6 (in peripheral output register 3) to function as SPI1TX ("7")
RPOR3bits.RP6R = SPI_TX_FUNCTION;
//SPICLK
SPI_CLK_TRIS = 0; //Set pin as output
//Assign pin RP4 (in peripheral output register 2) to function as SPI1CLK ("8")
RPOR2bits.RP4R = SPI_CLK_FUNCTION;
IFS0bits.SPI1IF = 0; // Clear the interrup flag
SPI1CON1bits.DISSCK = 0; // 0= Internal SPIx clock is enabled
SPI1CON1bits.DISSDO = 0; // 0= SDOx pin is controlled by the module
SPI1CON1bits.MODE16 = 0; // 0= set 8-bit mode
SPI1CON1bits.SMP = 0; // 0= Input data sampled at middle of data output time (not sure what this does)
SPI1CON1bits.SSEN = 0;
SPI1CON1bits.CKP = 0; // 0= Idle state for clock is a low level; active state is a high level
SPI1CON1bits.CKE = 0; // 0= Serial output data changes on transition from Idle clock state to active clock state
SPI1CON1bits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode
SPI1CON1bits.PPRE = 0b11; // 11= Primary Prescalar =1:1
SPI1CON1bits.SPRE = 0b111;// 111= Secondary Prescalar=1:1
SPI1CON2 = 0; // 0= non-framed mode
SPI1STATbits.SPIROV = 0; // 0= Clear the Receive Overflow flag bit
SPI1STATbits.SISEL = 1; // 1= Interrupt when data is available in receive buffer
SPI1STATbits.SPIEN = 1; //Enables SPI module and configures SCKx,SDOx,SDIx and ~SSx as serial port pins
}
*/
/**
*
*/
/*
void UNBLOCK_MEMORY(void)
{
unsigned int temp;
///Write to Status Register and Unblock all of the memory
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0x50; //Write to Status Register Enable Command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
SPI1BUF = 0b10101010; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
Nop();
Nop();
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0x01; //Write to Status Register command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0x00; //Status Register Bits (Disable Block Protection)
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
}
*/
/**
* Memory ID? get JEDEC_ID of Memory chip.
*/
/*
void JEDEC_ID (void)
{
unsigned int temp;
////JEDEC_ID
//IFS0bits.SPI1IF = 0;
FLASH_DISABLE_BIT = 0; // Enable Flash Chip
SPI1BUF = 0b10011111; //JEDEC-ID command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0b10101010; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
//printf("%x\n",temp);
SPI1BUF = 0b10101010; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
//printf("%x\n",temp);
SPI1BUF = 0b10101010; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF; //Read the data from the chip
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("%x\n",temp);
}
*/
/*
* Get Manuf_ID of memory chip.
*/
/*
unsigned int MANUF_ID(void)
{
//printf("In");
unsigned int temp;
////READ_ID
//IFS0bits.SPI1IF = 0;
FLASH_DISABLE_BIT = 0; // Enable Flash Chip
SPI1BUF = 0x90; //READ-ID command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0x00; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0x00; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0x00; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0b10101010; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("%x\n",temp);
return temp;
}
*/
/**
* Read the status register of the flash chip.
*/
/*
void READ_STATUS_REG(void)
{
unsigned int temp;
///Read Status Register
FLASH_DISABLE_BIT = 0; // Enable Flash Chip
SPI1BUF = 0x05; //Read Status Register command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0xAA; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("%x\n",temp);
}
*/
/**
* Erase 4 kilobytes starting
*/
/*
void _4KBYTE_ERASE(char address1,char address2,char address3)
{
unsigned int temp;
///Erase 4KBytes
WRITE_ENABLE();
//Erase Sector
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0x20; //Erase the next 4KBytes
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address1; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address2; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address3; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("# 4KByte Sector Erased\n");
for(temp =0;temp<5000;temp++);
}
*/
/**
* Erase 32 kilobytes at the given address bits
*/
/*
void _32KBYTE_ERASE(char address1,char address2,char address3)
{
unsigned int temp;
///Erase 32KBytes
WRITE_ENABLE();
//Erase Sector
FLASH_DISABLE_BIT = 0; sss //Enable Flash Chip
SPI1BUF = 0x52; //Erase the next 4KBytes
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address1; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address2; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address3; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("# 32KByte Sector Erased\n");
for(temp =0;temp<10000;temp++);
}
*/
/*
* Erase 64 kilobytes at the
*/
/*
void _64KBYTE_ERASE(char address1,char address2,char address3)
{
unsigned int temp;
///Erase 64KBytes
WRITE_ENABLE();
//Erase Sector
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0xD8; //Erase the next 4KBytes
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address1; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address2; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address3; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("# 64KByte Sector Erased\n");
for(temp =0;temp<10000;temp++);
}
*/
/**
* Clear the data on the flash chip
*/
/*
void CHIP_ERASE(void)
{
unsigned int temp;
///Chip Erase
WRITE_ENABLE();
for(temp =0;temp<100;temp++);
//Erase Chip
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0x60; //Chip Erase Command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1;
for(temp =0;temp<20000;temp++);
}
*/
/**
* Read a byte from memory.
*/
/*
unsigned int READ_BYTE(char address1,char address2,char address3)
{
unsigned int temp;
///Read Byte
FLASH_DISABLE_BIT = 0; // Enable Flash Chip
SPI1BUF = 0x03; //Read Byte command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address1; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address2; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address3; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0xAA; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("%x\n",temp);
return temp;
}
*/
/**
*
*/
/*
unsigned int READ_BYTE_AUTO(char state)
{
unsigned int temp;
if(state == 0)
{
FLASH_DISABLE_BIT = 0; // Enable Flash Chip
SPI1BUF = 0x03; //Read Byte command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = 0; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
}
if(state == 1)
{
SPI1BUF = 0xAA; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
return temp;
}
return 0;
}
*/
/**
* Enable the chip to write to be able to write to memory.
*/
/*
void WRITE_ENABLE(void)
{
unsigned int temp;
//Write Enable
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0x06; //Write Enable Command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
SPI1BUF = 0b10101010; //Dummy Byte
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
Nop();
Nop();
}
*/
/**
* Write the given data to the given address
*/
/*
void WRITE_BYTE(char address1,char address2,char address3, char data)
{
unsigned int temp;
//Write Byte
WRITE_ENABLE();
//Program Byte
FLASH_DISABLE_BIT = 0; //Enable Flash Chip
SPI1BUF = 0x02; //Program Byte command
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address1; //Address Byte 1
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address2; //Address Byte 2
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = address3; //Address Byte 3
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
SPI1BUF = data; //Byte being written
while(SPI1STATbits.SPIRBF == 0);
temp = SPI1BUF;
FLASH_DISABLE_BIT = 1; // Disable Flash Chip
//printf("ByteWritten\n")
}
*/