generated from zephyrproject-rtos/example-application
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.c
181 lines (154 loc) · 3.74 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
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/i2c.h>
#include <libopencm3/stm32/syscfg.h>
#include <libopencm3/stm32/usart.h>
#include "printf.h"
#include "uart.h"
#include "bq76920.h"
#define PORT_LED GPIOA
#define PIN_LED1 GPIO5
#define PIN_LED2 GPIO7
void uart_out(char *data);
void hard_fault(void);
void delay(int t);
uint8_t check_faults(uint8_t *fault_counter);
static void clock_setup(void)
{
rcc_clock_setup(&rcc_clock_config[RCC_CLOCK_CONFIG_HSI_16MHZ]);
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_SYSCFG);
rcc_periph_clock_enable(RCC_USART1);
rcc_periph_clock_enable(RCC_USART2);
rcc_periph_clock_enable(RCC_I2C1);
}
static void i2c_setup(void)
{
rcc_periph_reset_pulse(RST_I2C1);
gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO7);
gpio_set_af(GPIOB, GPIO_AF6, GPIO6 | GPIO7);
gpio_set_output_options(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_HIGH,
GPIO6 | GPIO7);
i2c_peripheral_disable(I2C1);
i2c_disable_stretching(I2C1);
i2c_enable_analog_filter(I2C1);
i2c_set_digital_filter(I2C1, 5);
i2c_set_speed(I2C1, i2c_speed_sm_100k, 16);
i2c_set_7bit_addr_mode(I2C1);
i2c_peripheral_enable(I2C1);
}
static void gpio_setup(void)
{
gpio_mode_setup(PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
PIN_LED1 | PIN_LED2);
// Shutdown buttons
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO0);
// allert
gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO8);
}
void delay(int t)
{
for (int i = 0; i < t; i++) {
__asm__("nop");
}
}
// Fast blink hard fault
void hard_fault(void)
{
while (1) {
gpio_toggle(PORT_LED, PIN_LED1);
delay(1e6);
};
}
uint8_t check_faults(uint8_t *fault_counter)
{
uint8_t ret = 0;
ret = bq76920_read_reg(SYS_STAT);
if (ret) {
gpio_set(PORT_LED, PIN_LED1);
delay(1e6);
if (*fault_counter < 10) {
bq76920_clear_faults();
bq76920_output_enable();
(*fault_counter)++;
} else {
// Shutdown
bq76920_shutdown();
}
}
return ret;
}
int main(void)
{
uint8_t ret = 0;
char buf[128];
uint8_t fault_counter = 0;
clock_setup();
gpio_setup();
gpio_set(PORT_LED, PIN_LED1);
delay(1e6);
gpio_clear(PORT_LED, PIN_LED1);
usart1_setup(115200);
usart2_setup(9600);
i2c_setup();
sprintf(buf, "Starting BMS\r\n");
uart1_out(buf);
// Set CC_CFG to 0x19 as per the datasheet
bq76920_write_reg(CC_CFG, 0x19);
ret = bq76920_read_reg(CC_CFG);
if (ret != 0x19) {
// Something is wrong with the chip
hard_fault();
}
bq76920_init();
bq76920_clear_faults();
bq76920_output_enable();
struct cells c = {};
while (1) {
bq76920_read_cells_v(&c);
uint8_t bal = bq76920_balance_cells(&c);
sprintf(buf,
"C0: %dmV C1: %dmV C2: %dmV C3: %dmV | Balance: %d\n\r",
c.c0, c.c1, c.c2, c.c3, bal);
uart1_out(buf);
ret = check_faults(&fault_counter);
if (ret) {
if (ret & SYS_STAT_OCD) {
sprintf(buf, "Fault: SYS_STAT_OCD\n\r");
uart1_out(buf);
}
if (ret & SYS_STAT_SCD) {
sprintf(buf, "Fault: SYS_STAT_SCD\n\r");
uart1_out(buf);
}
if (ret & SYS_STAT_OV) {
sprintf(buf, "Fault: SYS_STAT_OV\n\r");
uart1_out(buf);
}
if (ret & SYS_STAT_UV) {
sprintf(buf, "Fault: SYS_STAT_UV\n\r");
uart1_out(buf);
}
if (ret & SYS_STAT_OVRD_ALERT) {
sprintf(buf, "Fault: SYS_STAT_OVRD_ALERT\n\r");
uart1_out(buf);
}
if (ret & SYS_STAT_DEVICE_XREADY) {
sprintf(buf,
"Fault: SYS_STAT_DEVICE_XREADY\n\r");
uart1_out(buf);
}
sprintf(buf, "Fault Counter: %d\n\r", fault_counter);
uart1_out(buf);
}
if (!gpio_get(GPIOA, GPIO0) || uart_get_shutdown()) {
sprintf(buf, "Shutting down\n\r");
uart1_out(buf);
bq76920_shutdown();
}
gpio_toggle(PORT_LED, PIN_LED2);
delay(5e6);
}
return 0;
}