-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.c
385 lines (349 loc) · 9.11 KB
/
eeprom.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
#include "eeprom.h"
#include "mcc_generated_files/memory.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "shell.h"
#include "mcc_lora_config.h"
uint8_t c8[16];
uint8_t mui[16];
uint32_t EEPROM_types;
extern uint32_t uid;
extern _par_t _pars[];
extern uint8_t b[128];
extern char val_buf[BUF_LEN];
void Sync_EEPROM(void)
{
_par_t* __pars=_pars;
int i=1;
if(DATAEE_ReadByte(0))
{
while(__pars->type)
{
if(__pars->type==PAR_UI8)
{
__pars->u.ui8par=DATAEE_ReadByte(i);
i++;
}
else if(__pars->type==PAR_UI32 || __pars->type==PAR_I32 )
{
for(uint8_t j=0;j<4;j++) ((uint8_t*)(&(__pars->u.ui32par)))[j]=DATAEE_ReadByte(i+3-j);
i+=4;
}
else if(__pars->type==PAR_KEY128)
{
for(uint8_t j=0;j<16;j++) __pars->u.key[j]=DATAEE_ReadByte(i+j);
i+=16;
}
__pars++;
}
get_uid(&uid);
}
else
{
while(__pars->type)
{
if(__pars->type==PAR_UI8)
{
DATAEE_WriteByte(i,__pars->u.ui8par);
i++;
}
else if(__pars->type==PAR_UI32 || __pars->type==PAR_I32 )
{
for(int j=0;j<4;j++) DATAEE_WriteByte(i+3-j,((uint8_t*)(&(__pars->u.ui32par)))[j]);
i+=4;
}
else if(__pars->type==PAR_KEY128)
{
for(uint8_t j=0;j<16;j++)
{
// send_chars(ui8tox(__pars->u.key[j],b));
DATAEE_WriteByte(i+j,__pars->u.key[j]);
}
// send_chars("\r\n");
i+=16;
}
__pars++;
}
set_s("UID",&uid);
set_uid(uid);
DATAEE_WriteByte(0x0000, 1);
}
make_deveui();
EEPROM_types=get_EEPROM_types();
if(EEPROM_types==0xFFFFFFFF)
{
EEPROM_types=0;
put_EEPROM_types(EEPROM_types);
}
}
uint32_t get_EEPROM_types()
{
uint8_t t[4];
uint16_t dev_start=EUI_EEPROM_START-4;
for(uint8_t j=0;j<4;j++) t[j]=DATAEE_ReadByte(dev_start+j);
return *((uint32_t*)t);
}
void put_EEPROM_types(uint32_t t)
{
uint16_t dev_start=EUI_EEPROM_START-4;
for(uint8_t j=0;j<4;j++) DATAEE_WriteByte(dev_start+j,((uint8_t*)(&t))[j]);
}
uint8_t get_EEPROM_type(uint8_t n)
{
return (uint8_t)((EEPROM_types>>n)&0x00000001);
}
void set_EEPROM_type(uint8_t n)
{
EEPROM_types|=(0x00000001)<<n;
put_EEPROM_types(EEPROM_types);
}
void clear_EEPROM_type(uint8_t n)
{
EEPROM_types&=~((0x00000001)<<n);
put_EEPROM_types(EEPROM_types);
}
void erase_EEPROM_Data(void)
{
uint16_t dev_start=EUI_EEPROM_START-sizeof(EEPROM_types)-3;
uint16_t size=MAX_EEPROM_RECORDS*sizeof(EEPROM_Data_t)+sizeof(EEPROM_types)+3;
for(uint16_t j=0;j<size;j++) DATAEE_WriteByte(dev_start+j,0xFF);
}
void erase_ALL_EEPROM(void)
{
for(uint16_t j=0;j<0x3FF;j++) DATAEE_WriteByte(j,0xFF);
}
void clear_uid(void)
{
NVMCON1bits.REG = 1;
TBLPTRU=0x20;
TBLPTRH=0x00;
TBLPTRL=0x00;
NVMCON1bits.WREN = 1;
NVMCON1bits.FREE = 1;
INTERRUPT_GlobalInterruptDisable();
NVMCON2 = 0x55;
NVMCON2 = 0xAA;
NVMCON1bits.WR = 1; // Start program
INTERRUPT_GlobalInterruptEnable();
NVMCON1bits.WREN = 0; // Disable writes to memory
}
uint32_t read_uid(void)
{
uint32_t res;
NVMCON1bits.REG=1;
TBLPTRU=0x20;
TBLPTRH=0x00;
TBLPTRL=0x00;
for(uint8_t j=0;j<16;j++)
{
asm("TBLRDPOSTINC");
c8[j]=TABLAT;
}
/* send_chars("c=");
for(uint8_t j=0;j<16;j++)
{
send_chars(" ");
send_chars(ui8tox(c8[j],b));
}
send_chars("\r\n");*/
for(uint8_t j=0;j<4;j++) ((uint8_t*)(&res))[j]=c8[j];
return res;
}
void write_uid(void)
{
NVMCON1bits.REG = 1;
TBLPTRU=0x20;
TBLPTRH=0x00;
TBLPTRL=0x00;
for(uint8_t j=0;j<16;j++)
{
TABLAT=c8[j];
asm("TBLWTPOSTINC");
}
NVMCON1bits.REG = 1;
NVMCON1bits.WREN = 1;
NVMCON1bits.FREE = 0;
INTERRUPT_GlobalInterruptDisable();
NVMCON2 = 0x55;
NVMCON2 = 0xAA;
NVMCON1bits.WR = 1; // Start program
INTERRUPT_GlobalInterruptEnable();
NVMCON1bits.WREN = 0; // Disable writes to memory
}
void set_uid(uint32_t uid)
{
read_uid();
clear_uid();
for(uint8_t j=0;j<4;j++) c8[j]=((uint8_t*)(&uid))[j];
write_uid();
}
void get_uid(uint32_t* uid)
{
read_uid();
for(uint8_t j=0;j<4;j++) ((uint8_t*)uid)[j]=c8[j];
}
void get_mui(uint8_t* mui)
{
NVMCON1bits.REG=1;
TBLPTRU=0x3F;
TBLPTRH=0x00;
for(int8_t n=0;n<16;n++)
{
TBLPTRL=n;
mui[n]=0;
INTERRUPT_GlobalInterruptDisable();
asm("TBLRD");
mui[n]=TABLAT;
INTERRUPT_GlobalInterruptEnable();
}
}
uint32_t get_did(void)
{
uint32_t did0=0;
uint8_t r;
NVMCON1bits.REG=1;
TBLPTRU=0x3F;
TBLPTRH=0xFF;
for(uint8_t i=0;i<4;i++)
{
TBLPTRL=0xFC + i;
INTERRUPT_GlobalInterruptDisable();
asm("TBLRD");
r=TABLAT;
did0|=((uint32_t)r)<<(8*i);
INTERRUPT_GlobalInterruptEnable();
}
return did0;
}
void make_deveui(void)
{
get_mui(mui);
printVar("mui=",PAR_KEY128,mui,true,true);
GenericEui_t deveui,joineui;
for(uint8_t j=0;j<8;j++)
{
ui8tox(mui[j+2],b);
val_buf[2*j]=b[2];
val_buf[2*j+1]=b[3];
}
val_buf[16]=0;
send_chars("val_buf=");
send_chars(val_buf);
send_chars("\r\n");
set_par("DEV0EUI",val_buf);
set_s("DEV0EUI",&deveui);
printVar("DeviceEui=",PAR_EUI64,&deveui,true,true);
set_par("JOIN0EUI",val_buf);
set_s("JOIN0EUI",&joineui);
printVar("JoinEui=",PAR_EUI64,&joineui,true,true);
}
uint8_t get_Eui(uint8_t n,GenericEui_t* deveui)
{
uint16_t dev_start=EUI_EEPROM_START+n*sizeof(EEPROM_Data_t);
uint8_t not_zero=0, not_ff=0;
for(uint8_t j=0;j<sizeof(GenericEui_t);j++)
{
deveui->buffer[j]=DATAEE_ReadByte(dev_start+j);
if(deveui->buffer[j]) not_zero=1;
if(deveui->buffer[j]!=0xFF) not_ff=1;
};
return (not_zero¬_ff);
}
uint8_t put_Eui(uint8_t n,GenericEui_t* deveui)
{
uint16_t dev_start=EUI_EEPROM_START+n*sizeof(EEPROM_Data_t);
for(uint8_t j=0;j<sizeof(GenericEui_t);j++)
{
DATAEE_WriteByte(dev_start+j,deveui->buffer[j]);
};
return 0;
}
void put_DevNonce(uint8_t n, uint16_t devnonce)
{
uint16_t dev_start=EUI_EEPROM_START+n*sizeof(EEPROM_Data_t)+sizeof(GenericEui_t);
DATAEE_WriteByte(dev_start,(uint8_t)(devnonce&0x00FF));
DATAEE_WriteByte(dev_start+1,(uint8_t)((devnonce&0xFF00)>>8));
}
uint16_t get_DevNonce(uint8_t n)
{
uint16_t dev_start=EUI_EEPROM_START+n*sizeof(EEPROM_Data_t)+sizeof(GenericEui_t);
return ( (uint16_t)DATAEE_ReadByte(dev_start) | ((uint16_t)(DATAEE_ReadByte(dev_start+1)))<<8);
}
void put_JoinNonce(uint8_t* joinnonce)
{
uint16_t dev_start=EUI_EEPROM_START-sizeof(EEPROM_types)-3;
for(uint8_t j=0;j<3;j++) DATAEE_WriteByte(dev_start+j,joinnonce[j]);
}
void get_JoinNonce(uint8_t* joinnonce)
{
uint16_t dev_start=EUI_EEPROM_START-sizeof(EEPROM_types)-3;
for(uint8_t j=0;j<3;j++) joinnonce[j]=DATAEE_ReadByte(dev_start+j);
}
void getinc_JoinNonce(uint8_t* joinnonce)
{
uint16_t dev_start=EUI_EEPROM_START-sizeof(EEPROM_types)-3;
for(uint8_t j=0;j<3;j++) joinnonce[j]=DATAEE_ReadByte(dev_start+j);
joinnonce[0]++;
if(!joinnonce[0]) joinnonce[1]++;
if(!joinnonce[1]) joinnonce[2]++;
for(uint8_t j=0;j<3;j++) DATAEE_WriteByte(dev_start+j,joinnonce[j]);
}
uint16_t get_TSLR2()
{
uint8_t t0,t1;
NVMCON1bits.REG=1;
TBLPTRU=0x3F;
TBLPTRH=0x00;
TBLPTRL=0x26;
INTERRUPT_GlobalInterruptDisable();
asm("TBLRDPOSTINC");
t0=TABLAT;
asm("TBLRD");
t1=TABLAT;
INTERRUPT_GlobalInterruptEnable();
return t1*256+t0;
}
uint16_t get_TSHR2()
{
uint8_t t0,t1;
NVMCON1bits.REG=1;
TBLPTRU=0x3F;
TBLPTRH=0x00;
TBLPTRL=0x2C;
INTERRUPT_GlobalInterruptDisable();
asm("TBLRDPOSTINC");
t0=TABLAT;
asm("TBLRD");
t1=TABLAT;
INTERRUPT_GlobalInterruptEnable();
return t1*256+t0;
}
uint16_t get_FVRA2X()
{
uint8_t t0,t1;
NVMCON1bits.REG=1;
TBLPTRU=0x3F;
TBLPTRH=0x00;
TBLPTRL=0x32;
INTERRUPT_GlobalInterruptDisable();
asm("TBLRDPOSTINC");
t0=TABLAT;
asm("TBLRD");
t1=TABLAT;
INTERRUPT_GlobalInterruptEnable();
return t1*256+t0;
}
uint16_t get_FVRA1X()
{
uint8_t t0,t1;
NVMCON1bits.REG=1;
TBLPTRU=0x3F;
TBLPTRH=0x00;
TBLPTRL=0x30;
INTERRUPT_GlobalInterruptDisable();
asm("TBLRDPOSTINC");
t0=TABLAT;
asm("TBLRD");
t1=TABLAT;
INTERRUPT_GlobalInterruptEnable();
return t1*256+t0;
}