Skip to content

Commit

Permalink
Integrate bms_ic device into bms_context
Browse files Browse the repository at this point in the history
Previously, both the bms_ic and the bms_context had to be shared
between different files, even though the bms_ic dev logically
belongs to the bms_context.

This commit adds a const struct device *ic_dev to the bms_context,
solving the issue.
  • Loading branch information
martinjaeger committed Feb 4, 2024
1 parent 0af6e8c commit 8bbf2f4
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 145 deletions.
30 changes: 14 additions & 16 deletions app/src/bms_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

LOG_MODULE_REGISTER(bms, CONFIG_LOG_DEFAULT_LEVEL);

extern const struct device *bms_ic;

static float ocv_lfp[OCV_POINTS] = { 3.392F, 3.314F, 3.309F, 3.308F, 3.304F, 3.296F, 3.283F,
3.275F, 3.271F, 3.268F, 3.265F, 3.264F, 3.262F, 3.252F,
3.240F, 3.226F, 3.213F, 3.190F, 3.177F, 3.132F, 2.833F };
Expand Down Expand Up @@ -117,74 +115,74 @@ __weak void bms_state_machine(struct bms_context *bms)
switch (bms->state) {
case BMS_STATE_OFF:
if (bms_dis_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, true);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, true);
bms->state = BMS_STATE_DIS;
LOG_INF("OFF -> DIS (error flags: 0x%08x)", bms->ic_data.error_flags);
}
else if (bms_chg_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, true);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, true);
bms->state = BMS_STATE_CHG;
LOG_INF("OFF -> CHG (error flags: 0x%08x)", bms->ic_data.error_flags);
}
break;
case BMS_STATE_CHG:
if (!bms_chg_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, false);
/* DIS switch may be on on because of ideal diode control */
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, false);
bms->state = BMS_STATE_OFF;
LOG_INF("CHG -> OFF (error flags: 0x%08x)", bms->ic_data.error_flags);
}
else if (bms_dis_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, true);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, true);
bms->state = BMS_STATE_NORMAL;
LOG_INF("CHG -> NORMAL (error flags: 0x%08x)", bms->ic_data.error_flags);
}
#ifndef CONFIG_BMS_IC_BQ769X2 /* bq769x2 has built-in ideal diode control */
else {
/* ideal diode control for discharge MOSFET (with hysteresis) */
if (bms->ic_data.current > 0.5F) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, true);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, true);
}
else if (bms->ic_data.current < 0.1F) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, false);
}
}
#endif
break;
case BMS_STATE_DIS:
if (!bms_dis_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, false);
/* CHG_FET may be on because of ideal diode control */
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, false);
bms->state = BMS_STATE_OFF;
LOG_INF("DIS -> OFF (error flags: 0x%08x)", bms->ic_data.error_flags);
}
else if (bms_chg_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, true);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, true);
bms->state = BMS_STATE_NORMAL;
LOG_INF("DIS -> NORMAL (error flags: 0x%08x)", bms->ic_data.error_flags);
}
#ifndef CONFIG_BMS_IC_BQ769X2 /* bq769x2 has built-in ideal diode control */
else {
/* ideal diode control for charge MOSFET (with hysteresis) */
if (bms->ic_data.current < -0.5F) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, true);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, true);
}
else if (bms->ic_data.current > -0.1F) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, false);
}
}
#endif
break;
case BMS_STATE_NORMAL:
if (!bms_dis_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_DIS, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_DIS, false);
bms->state = BMS_STATE_CHG;
LOG_INF("NORMAL -> CHG (error flags: 0x%08x)", bms->ic_data.error_flags);
}
else if (!bms_chg_allowed(bms)) {
bms_ic_set_switches(bms_ic, BMS_SWITCH_CHG, false);
bms_ic_set_switches(bms->ic_dev, BMS_SWITCH_CHG, false);
bms->state = BMS_STATE_DIS;
LOG_INF("NORMAL -> DIS (error flags: 0x%08x)", bms->ic_data.error_flags);
}
Expand Down
9 changes: 4 additions & 5 deletions app/src/data_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <stdio.h>

extern const struct device *bms_ic;
extern struct bms_context bms;
extern float ocv_custom[OCV_POINTS];
extern float soc_custom[OCV_POINTS];
Expand Down Expand Up @@ -259,7 +258,7 @@ void data_objects_update_conf(enum thingset_callback_reason reason)
if (reason == THINGSET_CALLBACK_POST_WRITE) {
// ToDo: Validate new settings before applying them

bms_ic_configure(bms_ic, &bms.ic_conf, BMS_IC_CONF_ALL);
bms_ic_configure(bms.ic_dev, &bms.ic_conf, BMS_IC_CONF_ALL);

#ifdef CONFIG_THINGSET_STORAGE
thingset_storage_save_queued();
Expand All @@ -273,7 +272,7 @@ int32_t bat_preset(enum bms_cell_type type)

bms_init_config(&bms, type, new_capacity);

err = bms_ic_configure(bms_ic, &bms.ic_conf, BMS_IC_CONF_ALL);
err = bms_ic_configure(bms.ic_dev, &bms.ic_conf, BMS_IC_CONF_ALL);

#ifdef CONFIG_THINGSET_STORAGE
if (err > 0) {
Expand All @@ -296,7 +295,7 @@ int32_t bat_preset_lfp()

void print_registers()
{
bms_ic_debug_print_mem(bms_ic);
bms_ic_debug_print_mem(bms.ic_dev);
}

void reset_device()
Expand All @@ -306,5 +305,5 @@ void reset_device()

void shutdown()
{
bms_ic_set_mode(bms_ic, BMS_IC_MODE_OFF);
bms_ic_set_mode(bms.ic_dev, BMS_IC_MODE_OFF);
}
20 changes: 10 additions & 10 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

LOG_MODULE_REGISTER(bms_main, CONFIG_LOG_DEFAULT_LEVEL);

const struct device *bms_ic = DEVICE_DT_GET(DT_ALIAS(bms_ic));

struct bms_context bms;
struct bms_context bms = {
.ic_dev = DEVICE_DT_GET(DT_ALIAS(bms_ic)),
};

int main(void)
{
Expand All @@ -30,24 +30,24 @@ int main(void)
DT_PROP(DT_PATH(pcb), version_str));
LOG_INF("Firmware: %s", FIRMWARE_VERSION_ID);

if (!device_is_ready(bms_ic)) {
if (!device_is_ready(bms.ic_dev)) {
LOG_ERR("BMS IC not ready");
return -ENODEV;
}

bms_ic_assign_data(bms_ic, &bms.ic_data);
bms_ic_assign_data(bms.ic_dev, &bms.ic_data);

err = bms_ic_set_mode(bms_ic, BMS_IC_MODE_ACTIVE);
err = bms_ic_set_mode(bms.ic_dev, BMS_IC_MODE_ACTIVE);
if (err != 0) {
LOG_ERR("Failed to activate BMS IC: %d", err);
}

err = bms_ic_configure(bms_ic, &bms.ic_conf, BMS_IC_CONF_ALL);
err = bms_ic_configure(bms.ic_dev, &bms.ic_conf, BMS_IC_CONF_ALL);
if (err <= 0) {
LOG_ERR("Failed to configure BMS IC: %d", err);
}

err = bms_ic_read_data(bms_ic, BMS_IC_DATA_CELL_VOLTAGES);
err = bms_ic_read_data(bms.ic_dev, BMS_IC_DATA_CELL_VOLTAGES);
if (err != 0) {
LOG_ERR("Failed to read data from BMS IC: %d", err);
}
Expand All @@ -58,7 +58,7 @@ int main(void)

int64_t t_start = k_uptime_get();
while (true) {
err = bms_ic_read_data(bms_ic, BMS_IC_DATA_ALL);
err = bms_ic_read_data(bms.ic_dev, BMS_IC_DATA_ALL);
if (err != 0) {
LOG_ERR("Failed to read data from BMS IC: %d", err);
}
Expand All @@ -69,7 +69,7 @@ int main(void)

if (button_pressed_for_3s()) {
LOG_WRN("Button pressed for 3s: shutdown...");
bms_ic_set_mode(bms_ic, BMS_IC_MODE_OFF);
bms_ic_set_mode(bms.ic_dev, BMS_IC_MODE_OFF);
k_sleep(K_MSEC(10000));
}

Expand Down
3 changes: 3 additions & 0 deletions include/bms/bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ struct bms_context
/** Pointer to an array containing the State of Charge points for the OCV. */
float *soc_points;

/** BMS IC device pointer */
const struct device *ic_dev;

/** BMS IC configuration applied during start-up. */
struct bms_ic_conf ic_conf;

Expand Down
6 changes: 3 additions & 3 deletions tests/bms/src/state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include <stdio.h>
#include <time.h>

struct bms_context bms;

const struct device *bms_ic = DEVICE_DT_GET(DT_ALIAS(bms_ic));
struct bms_context bms = {
.ic_dev = DEVICE_DT_GET(DT_ALIAS(bms_ic)),
};

void init_conf()
{
Expand Down
Loading

0 comments on commit 8bbf2f4

Please sign in to comment.