-
Notifications
You must be signed in to change notification settings - Fork 25
/
config.c
189 lines (164 loc) · 4.39 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
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
#include "config.h"
#include "ch.h"
#include "hal.h"
#include "eeprom.h"
#include "stm32f30x_conf.h"
#include <string.h>
#include "utils.h"
#include <stddef.h>
#include "current_monitor.h"
#define EEPROM_BASE 1000
// Global variables
uint16_t VirtAddVarTab[NB_OF_VAR];
static volatile Config config;
void config_init(void)
{
memset(VirtAddVarTab, 0, sizeof(VirtAddVarTab));
int ind = 0;
for (unsigned int i = 0; i < (sizeof(Config) / 2); i++) {
VirtAddVarTab[ind++] = EEPROM_BASE + i;
}
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
EE_Init();
config_read_all();
}
void config_load_default_configuration(void)
{
config.CANDeviceID = 0x01;
config.numCells = 12;
config.fullCellVoltage = 4.2;
config.emptyCellVoltage = 3.4;
config.packCapacity = 2500;
config.lowVoltageCutoff = 3.2;
config.lowVoltageWarning = 3.4;
config.highVoltageCutoff = 4.25;
config.highVoltageWarning = 4.1;
config.maxCurrentCutoff = 120.0;
config.maxContinuousCurrent = 100.0;
config.continuousCurrentCutoffTime = 30;
config.continuousCurrentCutoffWarning = 10;
config.maxChargeCurrent = 20.0;
config.chargeVoltage = 25.2;
config.chargeCurrent = 2.0;
config.turnOnDelay = 200;
config.shutdownDelay = 500;
config.chargeMode = FULL_CURRENT;
config.chargeCurrentGain_P = 0.1;
config.chargeCurrentGain_I = 1.0;
config.prechargeTimeout = 500;
config.balanceStartVoltage = 3.5;
config.balanceDifferenceThreshold = 0.01;
config.chargerDisconnectShutdown = true;
}
Config* config_get_configuration(void)
{
return &config;
}
bool config_write_all(void)
{
utils_sys_lock_cnt();
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)&config;
uint16_t var;
FLASH_ClearFlag(FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
for (unsigned int i = 0; i < (sizeof(Config) / 2); i++) {
var = (conf_addr[2 * i] << 8) & 0xFF00;
var |= conf_addr[2 * i + 1] & 0xFF;
if (EE_WriteVariable(EEPROM_BASE + i, var) != FLASH_COMPLETE) {
is_ok = false;
break;
}
}
utils_sys_unlock_cnt();
return is_ok;
}
bool config_write_field(uint16_t addr, uint8_t *data, uint8_t size)
{
if (addr == offsetof(Config, CANDeviceID))
{
}
else if (addr == offsetof(Config, numCells))
{
if (*data > 12)
{
*data = 12;
}
}
else if (addr == offsetof(Config, lowVoltageCutoff))
{
}
else if (addr == offsetof(Config, highVoltageCutoff))
{
if (*((float*)data) > 5.0)
{
*((float*)data) = 5.0;
}
else if (*((float*)data) < 0.0)
{
*((float*)data) = 0.0;
}
}
else if (addr == offsetof(Config, chargeVoltage))
{
if (*((float*)data) > config.numCells * config.highVoltageCutoff)
{
*((float*)data) = config.numCells * config.highVoltageCutoff;
}
else if (*((float*)data) < 0.0)
{
*((float*)data) = 0.0;
}
}
else if (addr == offsetof(Config, maxCurrentCutoff))
{
if (*((float*)data) > 150.0)
{
*((float*)data) = 150.0;
}
else if (*((float*)data) < 1.0)
{
*((float*)data) = 1.0;
}
current_monitor_set_overcurrent(*((float*)data));
}
else
{
/*return false;*/
}
memcpy((void*)&config + addr, (void*)data, size);
utils_sys_lock_cnt();
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)&config;
uint16_t var;
FLASH_ClearFlag(FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
for (unsigned int i = (addr % 2 == 0 ? addr : addr - 1); i < addr + size; i += 2) {
var = (conf_addr[i] << 8) & 0xFF00;
var |= conf_addr[i + 1] & 0xFF;
if (EE_WriteVariable(EEPROM_BASE + i / 2, var) != FLASH_COMPLETE) {
is_ok = false;
break;
}
}
utils_sys_unlock_cnt();
return is_ok;
}
void config_read_all(void)
{
bool is_ok = true;
uint8_t *conf_addr = (uint8_t*)&config;
uint16_t var;
for (unsigned int i = 0; i < (sizeof(Config) / 2); i++) {
if (EE_ReadVariable(EEPROM_BASE + i, &var) == 0) {
conf_addr[2 * i] = (var >> 8) & 0xFF;
conf_addr[2 * i + 1] = var & 0xFF;
} else {
is_ok = false;
break;
}
}
if (!is_ok) {
config_load_default_configuration();
config_write_all();
}
}