-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPI_function.c
387 lines (315 loc) · 8.98 KB
/
SPI_function.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
384
385
386
387
#include "SPI_function.h"
#include "Bluetooth_Module.h"
//Source file of commands SPI for the EEPROM 25LC1024
#define READ 0b00000011 //3
#define WRITE 0b00000010 //2
#define WREN 0b00000110 //6
#define WRDI 0b00000100 //4
#define RDSR 0b00000101 //5
#define WRSR 0b00000001 //1
#define PE 0b01000010 //
#define SE 0b11011000 //
#define CE 0b11000111 //
#define RDID 0b10101011 //
#define DPD 0b10111001 //
/*Source code*/
/*Configuration function of the SPI bus on the PIC*/
void SetSPICONFIG(int chn,int isMaster,int taille_byte)
{
TRISE = 0x00;
SpiOpenFlags config = SPI_OPEN_SMP_END|SPI_CONFIG_MODE8|SPI_CON_ON;
switch (taille_byte)
{
case 1:
{
config=config|SPI_CONFIG_MODE8;
break;
}
case 2:
{
config=config|SPI_CONFIG_MODE16;
break;
}
case 4:
{
config=config|SPI_CONFIG_MODE32;
break;
}
if (isMaster==1)
{
config=config|SPI_CONFIG_MSTEN;
}
SpiChnOpen(chn,config,16);//Division of the frequency by 16
}
}
void SPI2Init(void)
{
TRISCbits.TRISC3 = 0;
// We indicate that bits 0 to 3 from the E port are outputs
TRISEbits.TRISE0 = 0;
TRISEbits.TRISE1 = 0;
TRISEbits.TRISE2 = 0;
//config SPI2
SPI2CONbits.ON = 0; // disable SPI port
SPI2CONbits.SIDL = 0; // Continue module operation in Idle mode
SPI2BUF = 0; // clear SPI buffer
SPI2CONbits.MODE16 = 0; //Mode 16 bits inactive
SPI2CONbits.MODE32 = 0; //Mode 32 bits inactive
SPI2CONbits.DISSDO = 0; // SDOx pin is controlled by the module
SPI2CONbits.MODE16 = 0; // set in 8-bit mode, clear in 16-bit mode
SPI2CONbits.SMP = 1; // Input data sampled at middle of data output time,dans notre cas end of time
SPI2CONbits.CKP = 0; // We indicate that IDLE is 0 and ACTIVE is 1
SPI2CONbits.CKE = 1; // The change of the data is made at the falling edge.
SPI2CONbits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode
SPI2CONbits.FRMEN = 0; // non-framed mode
SPI2BRG = 16; //It's the frequency divisor Fsck = Fpb / 2* (SPIxBRG+1)
SPI2CONbits.ON = 1; // enable SPI port, clear status
}
void SpiInitDevice(SpiChannel chn, int isMaster, int frmEn, int frmMaster)
{
TRISE = 0x00;
// 16 bits/char, input data sampled at end of data output time
SpiOpenFlags oFlags=SPI_OPEN_MODE8|SPI_OPEN_SMP_END|SPI_OPEN_ON;
if(isMaster)
{
oFlags|=SPI_OPEN_MSTEN; // Set as Master mode in this example
}
if(frmEn)
{
oFlags|=SPI_OPEN_FRMEN;
if(!frmMaster)
{
oFlags|=SPI_OPEN_FSP_IN;
}
}
// Open SPI module, use SPI channel 1, use flags set above, Divide Fpb by 4
SpiChnOpen(chn, oFlags, 8);
}
//Command function of the l'EEPROM
unsigned char writeSPI2( unsigned char data )
{
SPI2BUF = data; // write to buffer for TX
while(!SPI2STATbits.SPIRBF); // wait for transfer to complete
return SPI2BUF; // read the received value
}//writeSPI2
void WREN_cmd ()
{
WP = 1;
HOLD = 1;
CS = 0;
writeSPI2(WREN);
CS = 1;
}
void WRDI_cmd(void)
{
CS = 0;
writeSPI2(WRDI);
CS = 1;
}
unsigned char RDSR_cmd(void)
{
unsigned char reg;
CS = 0;
writeSPI2(RDSR);
reg = writeSPI2(0);
// printf("REGISTER : %d\n",reg);
CS=1;
return reg;
}
void WRSR_cmd (int BP1,int BP0)
{
char config = 0;
CS = 0;
if(BP0==1) config|0x01;
if(BP1==1) config|0x02;
writeSPI2(WRSR);
writeSPI2(config);
CS = 1;
}
void PE_cmd (unsigned char addr_tab[])
{
WREN_cmd();
CS = 0;
writeSPI2(PE);
writeSPI2(addr_tab[2]);
writeSPI2(addr_tab[1]);
writeSPI2(addr_tab[0]);
CS = 1;
}
void SE_cmd (unsigned char addr_tab[])
{
WREN_cmd();
CS = 0;
writeSPI2(SE);
writeSPI2(addr_tab[2]);
writeSPI2(addr_tab[1]);
writeSPI2(addr_tab[0]);
CS = 1;
}
void CE_cmd ()
{
WREN_cmd();
CS = 0;
writeSPI2(CE);
CS = 1;
}
void DPD_cmd()
{
CS = 0;
writeSPI2(DPD);
CS = 1;
}
unsigned char RDID_cmd (unsigned char addr_tab[])
{
unsigned char ID;
CS = 0;
writeSPI2(RDID);
writeSPI2(addr_tab[2]);
writeSPI2(addr_tab[1]);
writeSPI2(addr_tab[0]);
ID = writeSPI2(0);
CS = 1;
return ID;
}
void WRITE_cmd(unsigned char addr_tab[],unsigned char byte)
{
while(RDSR_cmd() & 0x1) ; // We check if the l'EEPROM is running
/*Writing permission*/
WREN_cmd();
CS = 0;
writeSPI2(WRITE);
/*adress sending*/
writeSPI2(addr_tab[2]);
writeSPI2(addr_tab[1]);
writeSPI2(addr_tab[0]);
/*byte sending*/
writeSPI2(byte);
CS = 1;
}
unsigned char READ_cmd (unsigned char addr_tab[])
{
int value;
while(RDSR_cmd() & 0x1); // We check if the l'EEPROM is running
CS = 0;
/*Command sending*/
writeSPI2(READ);
/*Address sending*/
writeSPI2(addr_tab[2]);
writeSPI2(addr_tab[1]);
writeSPI2(addr_tab[0]);
value = writeSPI2(0);
CS = 1;
return value;
}
void WRITE_cmd_32(unsigned char addr_tab[], int data) { // write a 32-bit value starting at an even address
// wait until any work in progress is completed
//while (RDSR_cmd() & 0x1); // check the WIP flag
WREN_cmd();
// perform a 32-bit write sequence (4 byte page write)
CS = 0; // select the Serial EEPROM
writeSPI2(WRITE); // write command
//Sending values on little-endian
writeSPI2(addr_tab[2]); // address LSB (word aligned)
writeSPI2(addr_tab[1]); // address LSB (word aligned)
writeSPI2(addr_tab[0]); // address MSB first
writeSPI2(data >> 24); // sending MSB
writeSPI2(data >> 16); // sending second byte
writeSPI2(data >> 8); // sending third byte
writeSPI2(data); // sending LSB
CS = 1;
}// writeSEE
unsigned int READ_cmd_32(unsigned char addr_tab[])
{
// read a 32-bit value starting at an even address
int i;
// wait until any work in progress is completed
//while (RDSR_cmd() & 0x1); // check WIP
// perform a 16-bit read sequence (two byte sequential read)
CS = 0; // select the Serial EEPROM
writeSPI2(READ); // read command
//Sending values on little-endian
writeSPI2(addr_tab[2]); // address LSB (word aligned)
writeSPI2(addr_tab[1]); // address LSB (word aligned)
writeSPI2(addr_tab[0]); // address MSB first
i = writeSPI2( 0); // send dummy, read msb
i = (i << 8) + writeSPI2(0); // send dummy, read lsb
i = (i << 8) + writeSPI2(0); // send dummy, read lsb
i = (i << 8) + writeSPI2(0); // send dummy, read lsb
CS = 1;
return ( i);
}// readSEE
void WRITE_cmd_n (unsigned char addr_tab[],unsigned char data[],unsigned int n)
{
char i=0;
while (RDSR_cmd() & 0x1){} // check the WIP flag
WREN_cmd();
// perform a 32-bit write sequence (4 byte page write)
CS = 0; // select the Serial EEPROM
writeSPI2(WRITE); // write command
//Sending values on little-endian
writeSPI2(addr_tab[2]); // address LSB (word aligned)
writeSPI2(addr_tab[1]); // address LSB (word aligned)
writeSPI2(addr_tab[0]); // address MSB first
for(i=0;i<n;i++)
{
writeSPI2(data[i]);
}
CS = 1;
}
void READ_cmd_n(unsigned char addr_tab[],unsigned char data[],unsigned int n)
{
// read a 32-bit value starting at an even address
char i;
// wait until any work in progress is completed
while (RDSR_cmd() & 0x1){} // check the WIP flag
// perform a 16-bit read sequence (two byte sequential read)
CS = 0; // select the Serial EEPROM
writeSPI2(READ); // read command
//Sending values on little-endian
writeSPI2(addr_tab[2]); // address LSB (word aligned)
writeSPI2(addr_tab[1]); // address LSB (word aligned)
writeSPI2(addr_tab[0]); // address MSB first
for(i=0;i<n;i++)
{
data[i]=writeSPI2(0);
}
CS = 1;
}// readSEE
int READ_string(unsigned char addr_tab[],unsigned char* data,unsigned int n)
{
// read a 32-bit value starting at an even address
int i;
// wait until any work in progress is completed
// while (RDSR_cmd() & 0x1); // check WIP
// perform a 16-bit read sequence (two byte sequential read)
CS = 0; // select the Serial EEPROM
writeSPI2(READ); // read command
//Sending values on little-endian
writeSPI2(addr_tab[2]); // address LSB (word aligned)
writeSPI2(addr_tab[1]); // address LSB (word aligned)
writeSPI2(addr_tab[0]); // address MSB first
for(i=0;i<n;i++)
{
data[i]=writeSPI2(0);
//Tis condition detect the end of a chain of character
if(data[i]=='\0')
{
break;
}
}
CS = 1;
return i;
}// readSEE
void WRITE_float (char addr_tab[],float value)
{
//Variables
union myfloat test;
WRITE_cmd_n(addr_tab,test.nb,sizeof(test.f));
}
float READ_float (char addr_tab[])
{
//Variables
union myfloat test;
READ_cmd_n(addr_tab,test.nb,sizeof(test.f));
return test.f;
}