Skip to content

Commit

Permalink
Hw tweak, added bme280 driver, sleep improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vedderb committed Jan 25, 2023
1 parent 36cedd7 commit fe2a7db
Show file tree
Hide file tree
Showing 33 changed files with 4,300 additions and 333 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ endif
# Stack size to be allocated to the Cortex-M process stack. This stack is
# the stack used by the main() thread.
ifeq ($(USE_PROCESS_STACKSIZE),)
USE_PROCESS_STACKSIZE = 0x400
USE_PROCESS_STACKSIZE = 2048
endif

# Stack size to the allocated to the Cortex-M main/exceptions stack. This
Expand Down
30 changes: 26 additions & 4 deletions bms_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
#include "hdc1080.h"
#include "sht30.h"
#include "shtc3.h"
#include "bme280_if.h"
#include "comm_can.h"
#include "timeout.h"
#include "sleep.h"
#include "terminal.h"
#include "flash_helper.h"
#include "commands.h"

#include <math.h>

Expand Down Expand Up @@ -82,13 +84,13 @@ static THD_FUNCTION(charge_thd, p) {

int no_charge_cnt = 0;

for (;;) {
#ifdef ADC_CH_CURRENT
float chg_current = m_i_in_filter;
float chg_current = m_i_in_filter;
#else
float chg_current = m_i_in_filter_ic;
float chg_current = m_i_in_filter_ic;
#endif

for (;;) {
if (m_is_charging && HW_TEMP_CELLS_MAX() >= backup.config.t_charge_max &&
backup.config.t_charge_mon_en) {
bms_if_fault_report(FAULT_CODE_CHARGE_OVERTEMP);
Expand Down Expand Up @@ -126,7 +128,7 @@ static THD_FUNCTION(charge_thd, p) {

chThdSleepMilliseconds(10);

if (chg_current > -0.5 && m_is_charging && !HW_CHARGER_DETECTED()) {
if (fabsf(chg_current) < backup.config.min_charge_current && m_is_charging && !HW_CHARGER_DETECTED()) {
no_charge_cnt++;

if (no_charge_cnt > 100) {
Expand Down Expand Up @@ -575,6 +577,8 @@ float bms_if_get_humsens_hum_pcb(void) {
return sht30_get_hum();
#elif defined(SHTC3_SDA_GPIO)
return shtc3_get_hum();
#elif defined(BME280_SDA_GPIO)
return bme280_if_get_hum();
#else
return 0.0;
#endif
Expand All @@ -587,17 +591,35 @@ float bms_if_get_humsens_temp_pcb(void) {
return sht30_get_temp();
#elif defined(SHTC3_SDA_GPIO)
return shtc3_get_temp();
#elif defined(BME280_SDA_GPIO)
return bme280_if_get_temp();
#else
return 0.0;
#endif
}

float bms_if_get_humsens_pres_pcb(void) {
#if defined(BME280_SDA_GPIO)
return bme280_if_get_pres();
#else
return 0.0;
#endif
}

float bms_if_get_humsens_hum_ext(void) {
#if defined(BME280_SDA_GPIO)
return bme280_if_get_hum();
#else
return sht30_get_hum();
#endif
}

float bms_if_get_humsens_temp_ext(void) {
#if defined(BME280_SDA_GPIO)
return bme280_if_get_temp();
#else
return sht30_get_temp();
#endif
}

float bms_if_get_soc(void) {
Expand Down
1 change: 1 addition & 0 deletions bms_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void bms_if_force_balance(bool bal_en);
void bms_if_zero_current_offset(void);
float bms_if_get_humsens_hum_pcb(void);
float bms_if_get_humsens_temp_pcb(void);
float bms_if_get_humsens_pres_pcb(void);
float bms_if_get_humsens_hum_ext(void);
float bms_if_get_humsens_temp_ext(void);
float bms_if_get_soc(void);
Expand Down
125 changes: 125 additions & 0 deletions comm_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ static can_status_msg_4 stat_msgs_4[CAN_STATUS_MSGS_TO_STORE];
static can_status_msg_5 stat_msgs_5[CAN_STATUS_MSGS_TO_STORE];
static bms_soc_soh_temp_stat bms_stat_msgs[CAN_BMS_STATUS_MSGS_TO_STORE];
static bms_soc_soh_temp_stat bms_stat_v_cell_min;
static io_board_adc_values io_board_adc_1_4[CAN_STATUS_MSGS_TO_STORE];
static io_board_adc_values io_board_adc_5_8[CAN_STATUS_MSGS_TO_STORE];
static io_board_digial_inputs io_board_digital_in[CAN_STATUS_MSGS_TO_STORE];
static psw_status psw_stat[CAN_STATUS_MSGS_TO_STORE];

static mutex_t can_mtx;
Expand Down Expand Up @@ -93,6 +96,10 @@ void comm_can_init(void) {
stat_msgs_4[i].id = -1;
stat_msgs_5[i].id = -1;

io_board_adc_1_4[i].id = -1;
io_board_adc_5_8[i].id = -1;
io_board_digital_in[i].id = -1;

psw_stat[i].id = -1;
}

Expand Down Expand Up @@ -344,6 +351,72 @@ bms_soc_soh_temp_stat *comm_can_get_bms_stat_v_cell_min(void) {
return &bms_stat_v_cell_min;
}

io_board_adc_values *comm_can_get_io_board_adc_1_4_index(int index) {
if (index < CAN_STATUS_MSGS_TO_STORE && io_board_adc_1_4[index].id >= 0) {
return &io_board_adc_1_4[index];
} else {
return 0;
}
}

io_board_adc_values *comm_can_get_io_board_adc_1_4_id(int id) {
if (id == 255 && io_board_adc_1_4[0].id >= 0) {
return &io_board_adc_1_4[0];
}

for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
if (io_board_adc_1_4[i].id == id) {
return &io_board_adc_1_4[i];
}
}

return 0;
}

io_board_adc_values *comm_can_get_io_board_adc_5_8_index(int index) {
if (index < CAN_STATUS_MSGS_TO_STORE && io_board_adc_5_8[index].id >= 0) {
return &io_board_adc_5_8[index];
} else {
return 0;
}
}

io_board_adc_values *comm_can_get_io_board_adc_5_8_id(int id) {
if (id == 255 && io_board_adc_5_8[0].id >= 0) {
return &io_board_adc_5_8[0];
}

for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
if (io_board_adc_5_8[i].id == id) {
return &io_board_adc_5_8[i];
}
}

return 0;
}

io_board_digial_inputs *comm_can_get_io_board_digital_in_index(int index) {
if (index < CAN_STATUS_MSGS_TO_STORE) {
return &io_board_digital_in[index];
} else {
return 0;
}
}

io_board_digial_inputs *comm_can_get_io_board_digital_in_id(int id) {
if (id == 255 && io_board_digital_in[0].id >= 0) {
return &io_board_digital_in[0];
}

for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
if (io_board_digital_in[i].id == id) {
return &io_board_digital_in[i];
}
}

return 0;
}

void comm_can_io_board_set_output_digital(int id, int channel, bool on) {
int32_t send_index = 0;
uint8_t buffer[8];
Expand Down Expand Up @@ -838,6 +911,58 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
}
break;

case CAN_PACKET_IO_BOARD_ADC_1_TO_4:
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
io_board_adc_values *msg = &io_board_adc_1_4[i];
if (msg->id == id || msg->id == -1) {
ind = 0;
msg->id = id;
msg->rx_time = chVTGetSystemTimeX();
ind = 0;
int j = 0;
while (ind < len) {
msg->adc_voltages[j++] = buffer_get_float16(data8, 1e2, &ind);
}
break;
}
}
break;

case CAN_PACKET_IO_BOARD_ADC_5_TO_8:
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
io_board_adc_values *msg = &io_board_adc_5_8[i];
if (msg->id == id || msg->id == -1) {
ind = 0;
msg->id = id;
msg->rx_time = chVTGetSystemTimeX();
ind = 0;
int j = 0;
while (ind < len) {
msg->adc_voltages[j++] = buffer_get_float16(data8, 1e2, &ind);
}
break;
}
}
break;

case CAN_PACKET_IO_BOARD_DIGITAL_IN:
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
io_board_digial_inputs *msg = &io_board_digital_in[i];
if (msg->id == id || msg->id == -1) {
ind = 0;
msg->id = id;
msg->rx_time = chVTGetSystemTimeX();
msg->inputs = 0;
ind = 0;
while (ind < len) {
msg->inputs |= (uint64_t)data8[ind] << (ind * 8);
ind++;
}
break;
}
}
break;

case CAN_PACKET_BMS_SOC_SOH_TEMP_STAT: {
int32_t ind = 0;
bms_soc_soh_temp_stat msg;
Expand Down
6 changes: 6 additions & 0 deletions comm_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ bms_soc_soh_temp_stat *comm_can_get_bms_soc_soh_temp_stat_id(int id);
bms_soc_soh_temp_stat *comm_can_get_bms_stat_v_cell_min(void);

// IO Board
io_board_adc_values *comm_can_get_io_board_adc_1_4_index(int index);
io_board_adc_values *comm_can_get_io_board_adc_1_4_id(int id);
io_board_adc_values *comm_can_get_io_board_adc_5_8_index(int index);
io_board_adc_values *comm_can_get_io_board_adc_5_8_id(int id);
io_board_digial_inputs *comm_can_get_io_board_digital_in_index(int index);
io_board_digial_inputs *comm_can_get_io_board_digital_in_id(int id);
void comm_can_io_board_set_output_digital(int id, int channel, bool on);
void comm_can_io_board_set_output_pwm(int id, int channel, float duty);

Expand Down
14 changes: 10 additions & 4 deletions conf_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
#define HW_NAME_MAX_CHARS 16

#if !defined(HW_SOURCE) && !defined(HW_HEADER)
#define HW_HEADER "hw_12s7p_v1.h"
#define HW_SOURCE "hw_12s7p_v1.c"
//#define HW_HEADER "hw_12s7p_v1.h"
//#define HW_SOURCE "hw_12s7p_v1.c"

//#define HW_HEADER "hw_18s_light.h"
//#define HW_SOURCE "hw_18s_light.c"
Expand All @@ -53,8 +53,8 @@
//#define HW_HEADER "hw_rbat.h"
//#define HW_SOURCE "hw_rbat.c"

//#define HW_HEADER "hw_lb.h"
//#define HW_SOURCE "hw_lb.c"
#define HW_HEADER "hw_lb.h"
#define HW_SOURCE "hw_lb.c"
#endif

/*
Expand All @@ -81,6 +81,12 @@
#endif

#include "hw.h"

// Application-specific battery type.
#ifndef CONF_BATTERY_TYPE
#define CONF_BATTERY_TYPE 0xAA
#endif

#include "conf_default.h"

// Functions
Expand Down
10 changes: 5 additions & 5 deletions config/conf_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
#define CONF_EXT_PCH_R_BOTTOM 10000
#endif

// Min Charge Current
#ifndef CONF_MIN_CHARGE_CURRENT
#define CONF_MIN_CHARGE_CURRENT 0.5
#endif

// Max Charge Current
#ifndef CONF_MAX_CHARGE_CURRENT
#define CONF_MAX_CHARGE_CURRENT 16
Expand Down Expand Up @@ -158,11 +163,6 @@
#define CONF_T_CHARGE_MON_EN 1
#endif

// Application-specific battery type.
#ifndef CONF_BATTERY_TYPE
#define CONF_BATTERY_TYPE 0xAA
#endif

// CONF_DEFAULT_H_
#endif

4 changes: 4 additions & 0 deletions config/confparser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This file is autogenerated by VESC Tool

#include <string.h>
#include "buffer.h"
#include "conf_general.h"
#include "confparser.h"
Expand Down Expand Up @@ -33,6 +34,7 @@ int32_t confparser_serialize_main_config_t(uint8_t *buffer, const main_config_t
buffer_append_float32_auto(buffer, conf->ext_shunt_gain, &ind);
buffer_append_float32_auto(buffer, conf->ext_pch_r_top, &ind);
buffer_append_float32_auto(buffer, conf->ext_pch_r_bot, &ind);
buffer_append_float32_auto(buffer, conf->min_charge_current, &ind);
buffer_append_float32_auto(buffer, conf->max_charge_current, &ind);
buffer_append_int32(buffer, conf->sleep_timeout_reset_ms, &ind);
buffer_append_float32_auto(buffer, conf->soc_filter_const, &ind);
Expand Down Expand Up @@ -76,6 +78,7 @@ bool confparser_deserialize_main_config_t(const uint8_t *buffer, main_config_t *
conf->ext_shunt_gain = buffer_get_float32_auto(buffer, &ind);
conf->ext_pch_r_top = buffer_get_float32_auto(buffer, &ind);
conf->ext_pch_r_bot = buffer_get_float32_auto(buffer, &ind);
conf->min_charge_current = buffer_get_float32_auto(buffer, &ind);
conf->max_charge_current = buffer_get_float32_auto(buffer, &ind);
conf->sleep_timeout_reset_ms = buffer_get_int32(buffer, &ind);
conf->soc_filter_const = buffer_get_float32_auto(buffer, &ind);
Expand Down Expand Up @@ -112,6 +115,7 @@ void confparser_set_defaults_main_config_t(main_config_t *conf) {
conf->ext_shunt_gain = CONF_EXT_SHUNT_GAIN;
conf->ext_pch_r_top = CONF_EXT_PCH_R_TOP;
conf->ext_pch_r_bot = CONF_EXT_PCH_R_BOTTOM;
conf->min_charge_current = CONF_MIN_CHARGE_CURRENT;
conf->max_charge_current = CONF_MAX_CHARGE_CURRENT;
conf->sleep_timeout_reset_ms = CONF_SLEEP_TIMEOUT_MS;
conf->soc_filter_const = CONF_SOC_FILTER_CONST;
Expand Down
2 changes: 1 addition & 1 deletion config/confparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdbool.h>

// Constants
#define MAIN_CONFIG_T_SIGNATURE 1838987580
#define MAIN_CONFIG_T_SIGNATURE 2297766070

// Functions
int32_t confparser_serialize_main_config_t(uint8_t *buffer, const main_config_t *conf);
Expand Down
Loading

0 comments on commit fe2a7db

Please sign in to comment.