-
Notifications
You must be signed in to change notification settings - Fork 97
/
main.c
237 lines (193 loc) · 6.96 KB
/
main.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
/*
Copyright 2019 - 2021 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ltc6813.h"
#include "conf_general.h"
#include "usbcfg.h"
#include "pwr.h"
#include "comm_can.h"
#include "utils.h"
#include "comm_usb.h"
#include "bms_if.h"
#include "hdc1080.h"
#include "sht30.h"
#include "shtc3.h"
#include "bme280_if.h"
#include "confparser.h"
#include "commands.h"
#include "timeout.h"
#include "sleep.h"
#include "flash_helper.h"
#include "comm_uart.h"
#include "hw.h"
#include "selftest.h"
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
__attribute__((section(".ram4"))) volatile backup_data backup;
#ifndef VAR_INIT_CODE_HW_CONF
#define VAR_INIT_CODE_HW_CONF VAR_INIT_CODE
#endif
#define CAN_FRAME_MAX_PL_SIZE 8
int main(void) {
halInit();
chSysInit();
// Stop debug mode in case no power cycle has been done after upload. This
// saves power.
DBGMCU->CR = DBGMCU_CR_DBG_STOP;
// If there is no backup data in RAM try to load it from flash. This can
// happen if power was lost.
if (backup.ah_cnt_init_flag != VAR_INIT_CODE ||
backup.wh_cnt_init_flag != VAR_INIT_CODE ||
backup.ic_i_sens_v_ofs_init_flag != VAR_INIT_CODE ||
backup.controller_id_init_flag != VAR_INIT_CODE ||
backup.send_can_status_rate_hz_init_flag != VAR_INIT_CODE ||
backup.can_baud_rate_init_flag != VAR_INIT_CODE ||
backup.conf_flash_write_cnt_init_flag != VAR_INIT_CODE ||
backup.usb_cnt_init_flag != VAR_INIT_CODE ||
backup.hw_config_init_flag != VAR_INIT_CODE_HW_CONF ||
backup.ah_cnt_chg_total_init_flag != VAR_INIT_CODE ||
backup.wh_cnt_chg_total_init_flag != VAR_INIT_CODE ||
backup.ah_cnt_dis_total_init_flag != VAR_INIT_CODE ||
backup.wh_cnt_dis_total_init_flag != VAR_INIT_CODE) {
flash_helper_load_backup_data();
}
// Reset backup counters that haven't been set. Should work across firmware uploads.
if (backup.ah_cnt_init_flag != VAR_INIT_CODE) {
backup.ah_cnt = 0.0;
backup.ah_cnt_init_flag = VAR_INIT_CODE;
}
if (backup.wh_cnt_init_flag != VAR_INIT_CODE) {
backup.wh_cnt = 0.0;
backup.wh_cnt_init_flag = VAR_INIT_CODE;
}
if (backup.ic_i_sens_v_ofs_init_flag != VAR_INIT_CODE) {
backup.ic_i_sens_v_ofs = 0.0;
backup.ic_i_sens_v_ofs_init_flag = VAR_INIT_CODE;
}
if (backup.controller_id_init_flag != VAR_INIT_CODE) {
backup.controller_id = HW_DEFAULT_ID;
backup.controller_id_init_flag = VAR_INIT_CODE;
}
if (backup.send_can_status_rate_hz_init_flag != VAR_INIT_CODE) {
backup.send_can_status_rate_hz = CONF_SEND_CAN_STATUS_RATE_HZ;
backup.send_can_status_rate_hz_init_flag = VAR_INIT_CODE;
}
if (backup.can_baud_rate_init_flag != VAR_INIT_CODE) {
backup.can_baud_rate = CONF_CAN_BAUD_RATE;
backup.can_baud_rate_init_flag = VAR_INIT_CODE;
}
if (backup.conf_flash_write_cnt_init_flag != VAR_INIT_CODE) {
backup.conf_flash_write_cnt = 0;
backup.conf_flash_write_cnt_init_flag = VAR_INIT_CODE;
}
if (backup.usb_cnt_init_flag != VAR_INIT_CODE) {
backup.usb_cnt = 0;
backup.usb_cnt_init_flag = VAR_INIT_CODE;
}
if (backup.hw_config_init_flag != VAR_INIT_CODE_HW_CONF) {
memset((void*)backup.hw_config, 0, sizeof(backup.hw_config));
backup.hw_config_init_flag = VAR_INIT_CODE_HW_CONF;
}
if (backup.ah_cnt_chg_total_init_flag != VAR_INIT_CODE) {
backup.ah_cnt_chg_total = 0.0;
backup.ah_cnt_chg_total_init_flag = VAR_INIT_CODE;
}
if (backup.wh_cnt_chg_total_init_flag != VAR_INIT_CODE) {
backup.wh_cnt_chg_total = 0.0;
backup.wh_cnt_chg_total_init_flag = VAR_INIT_CODE;
}
if (backup.ah_cnt_dis_total_init_flag != VAR_INIT_CODE) {
backup.ah_cnt_dis_total = 0.0;
backup.ah_cnt_dis_total_init_flag = VAR_INIT_CODE;
}
if (backup.wh_cnt_dis_total_init_flag != VAR_INIT_CODE) {
backup.wh_cnt_dis_total = 0.0;
backup.wh_cnt_dis_total_init_flag = VAR_INIT_CODE;
}
if (backup.config_init_flag != MAIN_CONFIG_T_SIGNATURE) {
confparser_set_defaults_main_config_t((main_config_t*)(&backup.config));
backup.config_init_flag = MAIN_CONFIG_T_SIGNATURE;
backup.config.controller_id = backup.controller_id;
backup.config.send_can_status_rate_hz = backup.send_can_status_rate_hz;
backup.config.can_baud_rate = backup.can_baud_rate;
backup.config.battery_type = CONF_BATTERY_TYPE;
}
HW_INIT_HOOK();
conf_general_apply_hw_limits((main_config_t*)&backup.config);
palSetLineMode(LINE_LED_RED, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_LED_GREEN, PAL_MODE_OUTPUT_PUSHPULL);
LED_OFF(LINE_LED_RED);
LED_OFF(LINE_LED_GREEN);
// USB needs some time to detect if a cable is connected, so start it before powering the regulators
// to not waste too much power.
commands_init();
comm_usb_init();
// Only wait for USB every 4 boots
if (backup.usb_cnt >= 4) {
#if HW_TEST_USB_AT_BOOT
if (chVTGetSystemTimeX() < TIME_MS2I(580)) {
chThdSleepUntil(TIME_MS2I(600));
}
#endif
backup.usb_cnt = 0;
} else {
backup.usb_cnt++;
}
pwr_init();
ltc_init();
bms_if_init();
comm_can_init();
comm_can_set_baud(backup.config.can_baud_rate);
// Test wakeup every other boot
if (backup.usb_cnt % 2 == 0) {
HW_TEST_WAKE_UP();
}
#ifdef HW_UART_DEV
comm_uart_init();
#endif
sleep_init();
#ifdef HDC1080_SDA_GPIO
hdc1080_init(HDC1080_SDA_GPIO, HDC1080_SDA_PIN,
HDC1080_SCL_GPIO, HDC1080_SCL_PIN);
#endif
#ifdef SHT30_SDA_GPIO
sht30_init(SHT30_SDA_GPIO, SHT30_SDA_PIN,
SHT30_SCL_GPIO, SHT30_SCL_PIN);
#endif
#ifdef SHTC3_SDA_GPIO
shtc3_init(SHTC3_SDA_GPIO, SHTC3_SDA_PIN,
SHTC3_SCL_GPIO, SHTC3_SCL_PIN);
#endif
#ifdef BME280_SDA_GPIO
bme280_if_init(BME280_SDA_GPIO, BME280_SDA_PIN,
BME280_SCL_GPIO, BME280_SCL_PIN);
#endif
timeout_init();
selftest_init();
// Transmit a CAN boot-frame to notify other nodes on the bus about it.
comm_can_transmit_eid(
backup.config.controller_id | (CAN_PACKET_NOTIFY_BOOT << 8),
(uint8_t *)HW_NAME, (strlen(HW_NAME) <= CAN_FRAME_MAX_PL_SIZE) ?
strlen(HW_NAME) : CAN_FRAME_MAX_PL_SIZE);
for(;;) {
backup.controller_id = backup.config.controller_id;
backup.send_can_status_rate_hz = backup.config.send_can_status_rate_hz;
backup.can_baud_rate = backup.config.can_baud_rate;
chThdSleepMilliseconds(1);
}
return 0;
}