forked from igkov/bcomp11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
69 lines (65 loc) · 1.78 KB
/
config.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
#include <string.h>
#include "eeprom.h"
#include "config.h"
#if !defined( WIN32 )
#include <lpc11xx.h>
#else
#define __NOP()
#endif
extern unsigned char crc8(unsigned char *block, int len);
int config_read(int param, uint8_t *value, int len) {
config_unit_t unit;
uint32_t addr = 2*param*sizeof(config_unit_t);
if (param >= CONFIG_MAX_PARAM ||
param < 0)
return 1;
// 1îå ÷òåíèå:
ee_read(addr, (uint8_t*)&unit, sizeof(config_unit_t));
if (unit.flag == 0xDE &&
unit.len >= len &&
unit.crc8 == crc8(unit.data, len)) {
memcpy(value, unit.data, len);
return 0;
}
// Íå ïðî÷èòàíî. 2îå ÷òåíèå:
ee_read(addr+sizeof(config_unit_t), (uint8_t*)&unit, sizeof(config_unit_t));
if (unit.flag == 0xEA &&
unit.len >= len &&
unit.crc8 == crc8(unit.data, len)) {
memcpy(value, unit.data, len);
return 0;
}
return 2;
}
int config_save(int param, uint8_t *value, int len) {
config_unit_t unit;
uint32_t addr = 2*param*sizeof(config_unit_t);
int n;
if (param >= CONFIG_MAX_PARAM ||
param < 0)
return 1;
if (len > CONFIG_MAX_SIZE)
return 2;
unit.crc8 = crc8(value, len);
unit.len = len;
memcpy(unit.data, value, len);
// 1àÿ çàïèñü:
unit.flag = 0xDE;
ee_write(addr, (uint8_t*)&unit, sizeof(config_unit_t));
// NOTE: äàííàÿ çàäåðæêà àêòóàëüíà äëÿ i2c-ïàìÿòè,
// ò.ê. ìû èìååì îøèáêè ñ íåäîçàïèñüþ. Ïðè÷åì â ñèëó àðõèòåêòóðû
// êîíôèãà íå ïðîÿâëÿåìûå ÿâíî.
for (n=0; n<30000; n++) {
__NOP();
}
// 2àÿ çàïèñü:
unit.flag = 0xEA;
ee_write(addr+sizeof(config_unit_t), (uint8_t*)&unit, sizeof(config_unit_t));
// NOTE: äàííàÿ çàäåðæêà àêòóàëüíà äëÿ i2c-ïàìÿòè,
// ò.ê. ìû èìååì îøèáêè ñ íåäîçàïèñüþ. Ïðè÷åì â ñèëó àðõèòåêòóðû
// êîíôèãà íå ïðîÿâëÿåìûå ÿâíî.
for (n=0; n<30000; n++) {
__NOP();
}
return 0;
}