This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.c
118 lines (85 loc) · 2.98 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
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
/*
* Code for reading/writing parameters in EEPROM, their default values in
* program memory. Based on Polaric Tracker code.
*/
#define __CONFIG_C__ /* IMPORTANT */
#include "ch.h"
#include "chsys.h"
#include "defines.h"
#include "config.h"
#include "string.h"
#include "hal.h"
#define BYTEPTR(x) (uint8_t*)(uint32_t)(x)
#define WORDPTR(x) (uint16_t*)(uint32_t)(x)
#define PTR(x) (void*)(uint32_t)(x)
void reset_param(uint16_t ee_addr)
{
while (!eeprom_is_ready())
t_yield();
eeprom_write_byte(WORDPTR(ee_addr), 0xff);
}
/************************************************************************
* Write config parameter into EEPROM.
************************************************************************/
void set_param(uint16_t ee_addr, void* ram_addr, const uint8_t size)
{
register uint8_t byte, checksum = 0x0f;
register void* addr = ram_addr;
while (!eeprom_is_ready())
t_yield();
for (int i=0; i<size; i++)
{
byte = *((uint8_t*) addr++);
checksum ^= byte;
}
eeprom_write_block(ram_addr, PTR(ee_addr), size);
eeprom_write_byte(WORDPTR(ee_addr+size), checksum);
}
/************************************************************************
* Get config parameter from EEPROM.
* If checksum does not match, use default value from program memory
* instead and return 1. Value is copied into ram_addr.
************************************************************************/
int get_param(uint16_t ee_addr, void* ram_addr, const uint8_t size, const void* default_val)
{
register uint8_t checksum = 0x0f, s_checksum;
register void* dest = ram_addr;
while (!eeprom_is_ready())
t_yield();
eeprom_read_block(ram_addr, PTR(ee_addr), size);
for (int i=0;i<size; i++)
checksum ^= *((uint8_t*) dest++);
s_checksum = eeprom_read_byte(PTR(ee_addr+size));
if (s_checksum != checksum) {
memcpy(ram_addr, default_val, size);
return 1;
}
else
return 0;
}
/************************************************************************
* Write single byte config parameter into EEPROM.
************************************************************************/
void set_byte_param(uint16_t ee_addr, uint8_t byte)
{
while (!eeprom_is_ready())
t_yield();
eeprom_write_byte(WORDPTR(ee_addr), byte);
eeprom_write_byte(PTR(ee_addr+1), (0x0f ^ byte));
}
/************************************************************************
* Get (and return) single byte config parameter from EEPROM.
* If checksum does not match, use default value from flash memory
* instead.
************************************************************************/
uint8_t get_byte_param(uint16_t ee_addr, const void* default_val)
{
while (!eeprom_is_ready())
t_yield();
register uint8_t b1 = eeprom_read_byte(WORDPTR(ee_addr));
register uint8_t b2 = eeprom_read_byte(WORDPTR(ee_addr+1));
if ((0x0f ^ b1) == b2)
return b1;
else
return *((uint8_t*) default_val);
}