Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
e-im committed May 16, 2024
1 parent 3f0b0ec commit 2542e9a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 56 deletions.
2 changes: 1 addition & 1 deletion libraries/core/inc/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define SIZEOF_ARRAY(arr) (sizeof((arr)) / sizeof((arr)[0]))
#define SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
// Casts void * to uint8_t *
#define VOID_PTR_UINT8(x) (_Generic((x), void * : (uint8_t *)(x), default : (x)))
#define VOID_PTR_UINT8(x) (_Generic((x), void *: (uint8_t *)(x), default: (x)))
#define SWAP_UINT16(x) (uint16_t)(((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8))

#define STRINGIFY_(x) #x
Expand Down
2 changes: 1 addition & 1 deletion libraries/ms-common/inc/delay.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ void delay_ms(uint32_t t);
void non_blocking_delay_ms(uint32_t t);

// Delay for a period in seconds.
#define delay_s(time) delay_ms((time)*1000)
#define delay_s(time) delay_ms((time) * 1000)
2 changes: 1 addition & 1 deletion libraries/ms-common/inc/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define FLASH_ADDR_TO_PAGE(addr) \
(((uintptr_t)(addr) - (uintptr_t)FLASH_BASE_ADDR) / FLASH_PAGE_BYTES)
#define FLASH_PAGE_TO_ADDR(page) ((uintptr_t)(page)*FLASH_PAGE_BYTES + (uintptr_t)FLASH_BASE_ADDR)
#define FLASH_PAGE_TO_ADDR(page) ((uintptr_t)(page) * FLASH_PAGE_BYTES + (uintptr_t)FLASH_BASE_ADDR)
#define FLASH_WRITE_BYTES FLASH_MCU_WRITE_BYTES
#define FLASH_PAGE_BYTES FLASH_MCU_PAGE_BYTES

Expand Down
7 changes: 5 additions & 2 deletions libraries/ms-drivers/inc/max17261_fuel_gauge.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ typedef struct {

uint32_t pack_design_cap_mah;
uint16_t cell_empty_voltage_v;
uint16_t charge_term_current_ma; // ref end-of-charge detection https://web.archive.org/web/20220121025712mp_/https://pdfserv.maximintegrated.com/en/an/user-guide-6597-max1726x-m5-ez-rev3-p4.pdf
uint16_t
charge_term_current_ma; // ref end-of-charge detection
// https://web.archive.org/web/20220121025712mp_/https://pdfserv.maximintegrated.com/en/an/user-guide-6597-max1726x-m5-ez-rev3-p4.pdf

uint16_t i_thresh_max_a;
int16_t i_thresh_min_a;
Expand Down Expand Up @@ -108,7 +110,8 @@ StatusCode max17261_temp(Max17261Storage *storage, uint16_t *temp_c);
* @param settings - populated settings struct
* @return STATUS_CODE_OK on success
*/
StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings, Max27261Params *params);
StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings,
Max27261Params *params);

StatusCode max17261_set_learned_params(Max17261Storage *storage, Max27261Params *params);
StatusCode max17261_get_learned_params(Max17261Storage *storage, Max27261Params *params);
68 changes: 38 additions & 30 deletions libraries/ms-drivers/src/max17261_fuel_gauge.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

#include <inttypes.h>

#include "log.h"
#include "delay.h"
#include "log.h"

// See Table 3 on pg.18 of the datasheet
#define PCT_LSB (1.0f / 256) // (%) LSBit is 1/256%
#define CAP_LSB (5.0f / storage->settings->sense_resistor_mohms) // (mAh) LSBit is 5 mili Volt hrs / Rsense (mAh)
#define TIM_LSB (5625U) // (ms) LSBit is 5625ms
#define CUR_LSB (1.5625f / storage->settings->sense_resistor_mohms) // (mA) LSBit is 1.5625uA / Rsense
#define VOLT_LSB (1.25f / 16) // (mV) LSBit is 1.25mV / 16
#define TEMP_LSB (1.0f / 256) // (C) LSBit is 1 / 256 C
#define PCT_LSB (1.0f / 256) // (%) LSBit is 1/256%
#define CAP_LSB \
(5.0f / storage->settings->sense_resistor_mohms) // (mAh) LSBit is 5 mili Volt hrs / Rsense (mAh)
#define TIM_LSB (5625U) // (ms) LSBit is 5625ms
#define CUR_LSB \
(1.5625f / storage->settings->sense_resistor_mohms) // (mA) LSBit is 1.5625uA / Rsense
#define VOLT_LSB (1.25f / 16) // (mV) LSBit is 1.25mV / 16
#define TEMP_LSB (1.0f / 256) // (C) LSBit is 1 / 256 C

static StatusCode max17261_get_reg(Max17261Storage *storage, Max17261Registers reg,
uint16_t *value) {
Expand Down Expand Up @@ -42,7 +44,7 @@ StatusCode max17261_state_of_charge(Max17261Storage *storage, uint16_t *soc_pct)
uint16_t reg = 0;
max17261_get_reg(storage, MAX17261_STATUS, &reg);
if (reg & (1 << 7)) {
max17261_set_reg(storage, MAX17261_STATUS, reg & (uint16_t)~(1 << 7));
max17261_set_reg(storage, MAX17261_STATUS, reg & (uint16_t) ~(1 << 7));
}

uint16_t status = 0;
Expand All @@ -65,7 +67,7 @@ StatusCode max17261_full_capacity(Max17261Storage *storage, uint32_t *full_cap_m
uint16_t full_cap_reg_val = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_FULL_CAP_REP, &full_cap_reg_val));
*full_cap_mAh = full_cap_reg_val * CAP_LSB;

uint16_t design = 0;
max17261_get_reg(storage, MAX17261_DESIGN_CAP, &design);
LOG_DEBUG("DES CAP: %d\n", design);
Expand Down Expand Up @@ -97,7 +99,7 @@ StatusCode max17261_current(Max17261Storage *storage, int16_t *current_ua) {
StatusCode max17261_voltage(Max17261Storage *storage, uint16_t *vcell_mv) {
uint16_t vcell_reg_val = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_VCELL, &vcell_reg_val));
*vcell_mv = (uint16_t)((float)(vcell_reg_val) * VOLT_LSB);
*vcell_mv = (uint16_t)((float)(vcell_reg_val)*VOLT_LSB);

return STATUS_CODE_OK;
}
Expand All @@ -121,25 +123,26 @@ StatusCode max17261_get_learned_params(Max17261Storage *storage, Max27261Params
StatusCode max17261_set_learned_params(Max17261Storage *storage, Max27261Params *params) {
status_ok_or_return(max17261_set_reg(storage, MAX17261_R_COMP0, params->rcomp0));
status_ok_or_return(max17261_set_reg(storage, MAX17261_TEMP_CO, params->tempco));
//status_ok_or_return(max17261_set_reg(storage, MAX17261_FULL_CAP_REP, params->fullcaprep));
// status_ok_or_return(max17261_set_reg(storage, MAX17261_FULL_CAP_REP, params->fullcaprep));
status_ok_or_return(max17261_set_reg(storage, MAX17261_CYCLES, params->cycles));
status_ok_or_return(max17261_set_reg(storage, MAX17261_FULL_CAP_NOM, params->fullcapnom));
return STATUS_CODE_OK;
}

StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings, Max27261Params *params) {
StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings,
Max27261Params *params) {
if (settings->i2c_port >= NUM_I2C_PORTS) {
return STATUS_CODE_INVALID_ARGS;
}

storage->settings = settings;

// ** Based on https://web.archive.org/web/20240330212616/https://pdfserv.maximintegrated.com/en/an/MAX1726x-Software-Implementation-user-guide.pdf
// ** Based on
// https://web.archive.org/web/20240330212616/https://pdfserv.maximintegrated.com/en/an/MAX1726x-Software-Implementation-user-guide.pdf
// Step 0 - check if already configured
uint16_t status = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_STATUS, &status));
if ((status & 0x0002) != 0) {

// Step 1 -- delay until FSTAT.DNR bit == 0
uint16_t fstat = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_FSTAT, &fstat));
Expand All @@ -153,20 +156,23 @@ StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings, M
// Step 2 -- disable hibernation
uint16_t hibcfg = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_HIB_CFG, &hibcfg));
status_ok_or_return(max17261_set_reg(storage, MAX17261_SOFT_WAKEUP, 0x90)); // wakup ic
status_ok_or_return(max17261_set_reg(storage, MAX17261_SOFT_WAKEUP, 0x90)); // wakup ic
status_ok_or_return(max17261_set_reg(storage, MAX17261_HIB_CFG, 0x0)); // disable hibernation
status_ok_or_return(max17261_set_reg(storage, MAX17261_SOFT_WAKEUP, 0x0)); // clear wakeup command

delay_ms(10); // delay that seems to make it work, TODO ????
status_ok_or_return(
max17261_set_reg(storage, MAX17261_SOFT_WAKEUP, 0x0)); // clear wakeup command

// Step 2.1 -- configure
status_ok_or_return(max17261_set_reg(storage, MAX17261_DESIGN_CAP, settings->pack_design_cap_mah / (uint32_t) CAP_LSB));
status_ok_or_return(max17261_set_reg(storage, MAX17261_I_CHG_TERM, settings->charge_term_current_ma / CUR_LSB));
status_ok_or_return(max17261_set_reg(storage, MAX17261_V_EMPTY, settings->cell_empty_voltage_v / VOLT_LSB));

status_ok_or_return(max17261_set_reg(storage, MAX17261_SOC_HOLD, 0x0)); // disable SOCHold, not relevant to us
delay_ms(10); // delay that seems to make it work, TODO ????

// Step 2.1 -- configure
status_ok_or_return(max17261_set_reg(storage, MAX17261_DESIGN_CAP,
settings->pack_design_cap_mah / (uint32_t)CAP_LSB));
status_ok_or_return(
max17261_set_reg(storage, MAX17261_I_CHG_TERM, settings->charge_term_current_ma / CUR_LSB));
status_ok_or_return(
max17261_set_reg(storage, MAX17261_V_EMPTY, settings->cell_empty_voltage_v / VOLT_LSB));

status_ok_or_return(
max17261_set_reg(storage, MAX17261_SOC_HOLD, 0x0)); // disable SOCHold, not relevant to us

uint16_t modelcfg = /*refresh*/ (1 << 15) | /*R100*/ (0 << 13) | /*RChg*/ (0 << 10) | (1 << 3);
status_ok_or_return(max17261_set_reg(storage, MAX17261_MODEL_I_CFG, modelcfg));
Expand All @@ -181,28 +187,30 @@ StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings, M
// Configure alerts
uint16_t config = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_CONFIG, &config));
config |= (1 << 2); // enable alerts
config |= (1 << 4); // thermal alerts
config |= (1 << 2); // enable alerts
config |= (1 << 4); // thermal alerts
// config |= (1 << 8); // external temp measurement TODO: hardware / uncomment
status_ok_or_return(max17261_set_reg(storage, MAX17261_CONFIG, config));

uint16_t current_th = (settings->i_thresh_max_a << 8) & (settings->i_thresh_min_a & 0x00FF);
status_ok_or_return(max17261_set_reg(storage, MAX17261_I_ALRT_TH, current_th));

status_ok_or_return(max17261_set_reg(storage, MAX17261_TEMP_ALRT_THRSH, (settings->temp_thresh_max_c < 8)));

status_ok_or_return(
max17261_set_reg(storage, MAX17261_TEMP_ALRT_THRSH, (settings->temp_thresh_max_c < 8)));

// Disable voltage alert (handled by AFE)
status_ok_or_return(max17261_set_reg(storage, MAX17261_VOLT_ALRT_THRSH, 0xFF00));
// Disable state of charge alerting
status_ok_or_return(max17261_set_reg(storage, MAX17261_SOC_ALRT_THRSH, 0xFF00));

// enable hibernation
status_ok_or_return(max17261_set_reg(storage, MAX17261_HIB_CFG, hibcfg));
status_ok_or_return(max17261_set_reg(storage, MAX17261_HIB_CFG, hibcfg));
}

// Step 3
status_ok_or_return(max17261_get_reg(storage, MAX17261_STATUS, &status));
status_ok_or_return(max17261_set_reg(storage, MAX17261_STATUS, status & (uint16_t)~(1 << 1))); // clear status POR bit
status_ok_or_return(max17261_set_reg(storage, MAX17261_STATUS,
status & (uint16_t) ~(1 << 1))); // clear status POR bit

if (params) {
status_ok_or_return(max17261_set_learned_params(storage, params));
Expand Down
8 changes: 4 additions & 4 deletions projects/bms_carrier/inc/current_sense.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

#define SENSE_RESISTOR_MOHM (0.5)

#define PACK_CAPACITY_MAH (160000) // 14V test module, TODO change to reflect pack
#define PACK_CAPACITY_MAH (160000) // 14V test module, TODO change to reflect pack

#define CELL_EMPTY_VOLTAGE_MV 2500 // LG M50T datasheet
#define CELL_EMPTY_VOLTAGE_MV 2500 // LG M50T datasheet

#define CHARGE_TERMINATION_CURRENT_MA 150 // just a guess, TODO verify
#define CHARGE_TERMINATION_CURRENT_MA 150 // just a guess, TODO verify

// Thresholds for ALRT Pin
#define CURRENT_SENSE_MAX_CURRENT_A (58.2f)
#define CURRENT_SENSE_MIN_CURRENT_A (27.0f) // Actually -27
#define CURRENT_SENSE_MIN_CURRENT_A (27.0f) // Actually -27
#define CURRENT_SENSE_MAX_TEMP_C (60U)
#define CURRENT_SENSE_MAX_VOLTAGE_V (150U)
#define ALRT_PIN_V_RES_MICRO_V (400)
Expand Down
14 changes: 7 additions & 7 deletions projects/bms_carrier/src/current_sense.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "current_sense.h"

#include <string.h>
#include <inttypes.h>
#include <string.h>

#include "bms.h"
#include "bms_carrier_setters.h"
#include "exported_enums.h"
#include "fault_bps.h"
#include "gpio_it.h"
#include "persist.h"
#include "interrupt.h"
#include "log.h"
#include "persist.h"
#include "tasks.h"

// Update stored learned params every 30 sec
Expand Down Expand Up @@ -72,9 +72,9 @@ StatusCode prv_fuel_gauge_read() {
// Commit params info periodically
if (xTaskGetTickCount() > s_last_params_update + pdMS_TO_TICKS(UPDATE_FLASH_PARAMS_PERIOD_MS)) {
LOG_DEBUG("Updating params\n");
s_last_params_update = xTaskGetTickCount();
max17261_get_learned_params(&s_fuel_guage_storage, &s_fuel_params);
persist_commit(&s_persist);
s_last_params_update = xTaskGetTickCount();
max17261_get_learned_params(&s_fuel_guage_storage, &s_fuel_params);
persist_commit(&s_persist);
}
return status;
}
Expand All @@ -90,7 +90,7 @@ TASK(current_sense, TASK_STACK_256) {
// fault_bps_set(BMS_FAULT_COMMS_LOSS_CURR_SENSE);
} else if (notification & 1 << KILLSWITCH_IT) {
fault_bps_set(BMS_FAULT_KILLSWITCH);
}
}
prv_fuel_gauge_read();
send_task_end();
}
Expand Down Expand Up @@ -150,7 +150,7 @@ StatusCode current_sense_init(BmsStorage *bms_storage, I2CSettings *i2c_settings

persist_init(&s_persist, CURRENT_SENSE_STORE_FLASH, &s_fuel_params, sizeof(s_fuel_params), false);
status_ok_or_return(max17261_init(&s_fuel_guage_storage, &s_fuel_gauge_settings, &s_fuel_params));

tasks_init_task(current_sense, TASK_PRIORITY(2), NULL);
return STATUS_CODE_OK;
}
20 changes: 10 additions & 10 deletions projects/bms_carrier/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ void pre_loop_init() {
fault_bps_init(&bms_storage.bps_storage);
current_sense_init(&bms_storage, &i2c_settings, FUEL_GAUGE_CYCLE_TIME_MS);
// cell_sense_init(&bms_storage.ltc_afe_storage);
//aux_sense_init(&bms_storage.aux_storage);
//init_bms_relays();
//bms_fan_init(&bms_storage);
// aux_sense_init(&bms_storage.aux_storage);
// init_bms_relays();
// bms_fan_init(&bms_storage);
}

void run_fast_cycle() {}

void run_medium_cycle() {
//run_can_rx_cycle();
//wait_tasks(1);
// run_can_rx_cycle();
// wait_tasks(1);

// cell_conversions();
// wait_tasks(1);
current_sense_run();
wait_tasks(1);

// cell_sense_run();
//aux_sense_run();
//bms_run_fan();
// aux_sense_run();
// bms_run_fan();

//run_can_tx_cycle();
//wait_tasks(1);
// run_can_tx_cycle();
// wait_tasks(1);
}

void run_slow_cycle() {
//cell_discharge(&bms_storage.ltc_afe_storage);
// cell_discharge(&bms_storage.ltc_afe_storage);

if (fault_bps_get()) {
LOG_DEBUG("FAULT_BITMASK: %d\n", fault_bps_get());
Expand Down

0 comments on commit 2542e9a

Please sign in to comment.