Skip to content

Commit

Permalink
Bringup BMSB Analog IO calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLafleur committed Mar 1, 2025
1 parent f5a8ebe commit 1b120e9
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 269 deletions.
15 changes: 15 additions & 0 deletions components/bms_boss/include/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// System Includes
#include "FloatTypes.h"
#include "Types.h"
#include "LIB_nvm.h"

// Firmware Includes
#include "HW_adc.h"
Expand Down Expand Up @@ -44,9 +45,23 @@ typedef struct
bool feedback_sfty_imd :1;
} IO_S;

#define IO_CALIBRATIONVALUES_VERSION_0 0U
typedef struct
{
float32_t current_sense_differential;
float32_t spare[4];
} LIB_NVM_STORAGE(IO_calibrationValues_V0_S);

/******************************************************************************
* E X T E R N S
******************************************************************************/

extern IO_S IO;
extern IO_calibrationValues_V0_S IO_calibrationValues;
extern const IO_calibrationValues_V0_S IO_calibrationValues_default;

/******************************************************************************
* P R I V A T E F U N C T I O N P R O T O T Y P E S
******************************************************************************/

void IO_stopCalibration_currentSense(void);
3 changes: 2 additions & 1 deletion components/bms_boss/include/LIB_nvm_componentSpecific.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

typedef enum
{
NVM_ENTRYID_COUNT = 0U,
NVM_ENTRYID_IO_CALIBRATION = 0U,
NVM_ENTRYID_COUNT,
} lib_nvm_entryId_E;
9 changes: 9 additions & 0 deletions components/bms_boss/include/Sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "HW_gpio.h"

#include "FloatTypes.h"
#include "LIB_utility.h"

/******************************************************************************
* T Y P E D E F S
Expand All @@ -32,11 +33,17 @@ typedef enum {
SYS_CONTACTORS_HVP_CLOSED,
} SYS_Contactors_E;

typedef enum {
SYS_LOCKOUTREASON_ADC_CALIBRATING = 0U,
SYS_LOCKOUTREASON_COUNT,
} SYS_LockoutReason_E;


typedef struct
{
SYS_state_E state;
SYS_Contactors_E contacts;
FLAG_create(lockout_reason, SYS_LOCKOUTREASON_COUNT);
} SYS_S;

extern SYS_S SYS;
Expand All @@ -55,3 +62,5 @@ bool SYS_SFT_checkBrusaChargerTimeout(void);
bool SYS_SFT_checkElconChargerTimeout(void);
void SYS_stopCharging(void);
void SYS_continueCharging(void);
void SYS_setLockoutState(SYS_LockoutReason_E reason, bool enable);
bool SYS_getLockoutState(SYS_LockoutReason_E reason);
118 changes: 1 addition & 117 deletions components/bms_boss/include/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
#include "Types.h"
#include "stddef.h"
#include "string.h"
#include "LIB_utility.h"

/******************************************************************************
* M A C R O S
******************************************************************************/

// Count number of items in array
#define COUNTOF(x) ((uint16_t)(sizeof(x) / sizeof(x[0]))) // return size of an array as uint16
// Count leading zeroes
static inline uint16_t u32CountLeadingZeroes(uint32_t x)
{
Expand Down Expand Up @@ -78,125 +77,10 @@ static inline uint16_t u32CountLeadingZeroes(uint32_t x)
} \
} while (zero())


// FLAGS
// This should get moved elsewhere at some point
#define FLAG_bits_each 16
#define WORDS_FROM_COUNT(count) (uint16_t)((count + (FLAG_bits_each - 1)) / FLAG_bits_each)
#define FLAG_GET_WORD(name, flag) (name[(uint16_t)flag / FLAG_bits_each])
#define FLAG_GET_MASK(flag) (1U << ((uint16_t)flag % FLAG_bits_each))

#define FLAG_create(name, size) uint16_t(name)[WORDS_FROM_COUNT(size)]
#define FLAG_set(name, pos) FLAG_GET_WORD(name, pos) |= (uint16_t)FLAG_GET_MASK(pos)
#define FLAG_clear(name, pos) FLAG_GET_WORD(name, pos) &= (uint16_t)~FLAG_GET_MASK(pos)
#define FLAG_get(name, pos) ((bool)((FLAG_GET_WORD(name, pos) & FLAG_GET_MASK(pos)) == FLAG_GET_MASK(pos)))
#define FLAG_assign(name, pos, value) \
do { \
if (value) \
{ \
FLAG_set(name, pos); \
} \
else \
{ \
FLAG_clear(name, pos); \
} \
} while (zero())


/******************************************************************************
* P U B L I C F U N C T I O N S
******************************************************************************/

/*
* FLAG_setAll
* @brief Sets all of the flags in the given flag word
* @param name Flag word name
* @param count Number of flags in the given flag
*/
static inline void FLAG_setAll(uint16_t* name, uint16_t count)
{
uint16_t numWords = WORDS_FROM_COUNT(count);
uint16_t extraBits = count % FLAG_bits_each;

if (numWords > 1U)
{
memset(name, 0xFF, (uint16_t)(numWords * FLAG_bits_each));
}
if (extraBits > 0U)
{
name[numWords] |= (0xFF >> (FLAG_bits_each - extraBits));
}
}


/*
* FLAG_clearAll
* @brief clears all of the bits in the given flag
* @param name Flag word name
* @param count Number of bits in the given flag
*/
static inline void FLAG_clearAll(uint16_t* name, uint16_t count)
{
memset(name, 0x00, (uint16_t)(WORDS_FROM_COUNT(count) * FLAG_bits_each));
}


/*
* FLAG_any
* @brief check if any of the flags in the flag word is set
* @param name Flag word name
* @param count Number of flags in the given flag
*/
static inline bool FLAG_any(uint16_t* name, uint16_t count)
{
for (uint16_t word = 0U; word < WORDS_FROM_COUNT(count); word++)
{
if ((FLAG_GET_WORD(name, word) & 0xFF) != 0U)
{
return true;
}
}
return false;
}


/*
* FLAG_all
* @brief checks if all flags in the flag word are set
* @param name Flag word name
* @param count Number of bits in the given flag word
*/
static inline bool FLAG_all(uint16_t* name, uint16_t count)
{
for (uint16_t word = 0U; word < WORDS_FROM_COUNT(count); word++)
{
if ((FLAG_GET_WORD(name, word) & 0xFF) != 0xFF)
{
return false;
}
}
return true;
}


/*
* FLAG_none
* @brief checks if none of the flags in the flag word are set
* @param name Flag word name
* @param count Number of bits in the given flag word
*/
static inline bool FLAG_none(uint16_t* name, uint16_t count)
{
for (uint16_t word = 0U; word < WORDS_FROM_COUNT(count); word++)
{
if ((FLAG_GET_WORD(name, word) & 0xFF) != 0U)
{
return false;
}
}
return true;
}

static inline uint8_t reverse_byte(uint8_t x)
{
static const uint8_t table[] = {
Expand Down
13 changes: 12 additions & 1 deletion components/bms_boss/src/IO.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ static io_S io;
*/
IO_S IO;

IO_calibrationValues_V0_S IO_calibrationValues;

const IO_calibrationValues_V0_S IO_calibrationValues_default = {
.current_sense_differential = 0.0f,
};

/******************************************************************************
* P R I V A T E F U N C T I O N P R O T O T Y P E S
Expand Down Expand Up @@ -121,7 +126,7 @@ static void IO10Hz_PRD(void)
else if (io.adcState == ADC_STATE_RUNNING)
{
IO_unpackADCBuffer();
IO.current = (io.adcData[ADC_CURRENT_P].value - io.adcData[ADC_CURRENT_N].value) / ADC_MAX_VAL * VREF;
IO.current = ((io.adcData[ADC_CURRENT_P].value - io.adcData[ADC_CURRENT_N].value) / (float32_t)ADC_MAX_VAL) * VREF - IO_calibrationValues.current_sense_differential;
IO.mcu_temp = (io.adcData[ADC_MCU_TEMP].value / ADC_MAX_VAL) * VREF;
}

Expand All @@ -139,6 +144,12 @@ const ModuleDesc_S IO_desc = {
.periodic10Hz_CLK = &IO10Hz_PRD,
};

void IO_stopCalibration_currentSense(void)
{
IO_calibrationValues.current_sense_differential = IO.current;
lib_nvm_requestWrite(NVM_ENTRYID_IO_CALIBRATION);
}

/******************************************************************************
* P R I V A T E F U N C T I O N S
******************************************************************************/
Expand Down
12 changes: 11 additions & 1 deletion components/bms_boss/src/LIB_nvm_componentSpecific.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@
******************************************************************************/

#include "LIB_nvm.h"
#include "IO.h"

/******************************************************************************
* P U B L I C V A R S
******************************************************************************/

const lib_nvm_entry_S lib_nvm_entries[NVM_ENTRYID_COUNT];
const lib_nvm_entry_S lib_nvm_entries[NVM_ENTRYID_COUNT] = {
[NVM_ENTRYID_IO_CALIBRATION] = {
.entryId = NVM_ENTRYID_IO_CALIBRATION,
.version = IO_CALIBRATIONVALUES_VERSION_0,
.entrySize = sizeof(IO_calibrationValues_V0_S),
.entryDefault_Ptr = &IO_calibrationValues_default,
.entryRam_Ptr = &IO_calibrationValues,
.minTimeBetweenWritesMs = 60000U,
},
};
43 changes: 29 additions & 14 deletions components/bms_boss/src/Sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,25 @@ void SYS_SFT_openContactors(void)

void SYS_SFT_cycleContacts(void)
{
if (SYS.contacts == SYS_CONTACTORS_OPEN)
if (FLAG_any((uint16_t*)&SYS.lockout_reason, SYS_LOCKOUTREASON_COUNT) == false)
{
HW_GPIO_writePin(HW_GPIO_PCHG, true);
SYS.contacts = SYS_CONTACTORS_PRECHARGE;
}
else if (SYS.contacts == SYS_CONTACTORS_PRECHARGE)
{
HW_GPIO_writePin(HW_GPIO_AIR, true);
SYS.contacts = SYS_CONTACTORS_CLOSED;
}
else if (SYS.contacts == SYS_CONTACTORS_CLOSED)
{
HW_GPIO_writePin(HW_GPIO_PCHG, false);
SYS.contacts = SYS_CONTACTORS_HVP_CLOSED;
if (SYS.contacts == SYS_CONTACTORS_OPEN)
{
HW_GPIO_writePin(HW_GPIO_PCHG, true);
SYS.contacts = SYS_CONTACTORS_PRECHARGE;
}
else if (SYS.contacts == SYS_CONTACTORS_PRECHARGE)
{
HW_GPIO_writePin(HW_GPIO_AIR, true);
SYS.contacts = SYS_CONTACTORS_CLOSED;
}
else if (SYS.contacts == SYS_CONTACTORS_CLOSED)
{
HW_GPIO_writePin(HW_GPIO_PCHG, false);
SYS.contacts = SYS_CONTACTORS_HVP_CLOSED;
}
}

}

bool SYS_SFT_checkMCTimeout(void)
Expand All @@ -156,4 +160,15 @@ void SYS_continueCharging(void)
bool SYS_SFT_checkElconChargerTimeout(void)
{
return (CANRX_validate(PRIVBMS, ELCON_criticalData) != CANRX_MESSAGE_VALID);
}
}

void SYS_setLockoutState(SYS_LockoutReason_E reason, bool enable)
{
SYS_SFT_openContactors();
FLAG_assign(SYS.lockout_reason, reason, enable);
}

bool SYS_getLockoutState(SYS_LockoutReason_E reason)
{
return FLAG_get(SYS.lockout_reason, reason);
}
38 changes: 38 additions & 0 deletions components/bms_boss/src/UDS.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

// module include
#include "UDS.h"
#include "Sys.h"
#include "IO.h"

// other includes
#include "CAN/CanTypes.h"
Expand Down Expand Up @@ -67,6 +69,39 @@ extern void isotp_user_debug(const char* message, ...);
* P R I V A T E F U N C T I O N S
******************************************************************************/

static void routine_0x00f0(udsRoutineControlType_E routineControlType, uint8_t *payload, uint8_t payloadLengthBytes)
{
UNUSED(payloadLengthBytes);
UNUSED(payload);
switch (routineControlType)
{
case UDS_ROUTINE_CONTROL_START:
{
SYS_setLockoutState(SYS_LOCKOUTREASON_ADC_CALIBRATING, true);
}
break;

case UDS_ROUTINE_CONTROL_GET_RESULT:
{
const bool completed = true;
IO_stopCalibration_currentSense();
SYS_setLockoutState(SYS_LOCKOUTREASON_ADC_CALIBRATING, false);
uds_sendPositiveResponse(UDS_SID_ROUTINE_CONTROL,
UDS_ROUTINE_CONTROL_GET_RESULT,
(uint8_t*)&completed,
sizeof(completed));
}
break;

case UDS_ROUTINE_CONTROL_STOP:
case UDS_ROUTINE_CONTROL_NONE:
default:
uds_sendNegativeResponse(UDS_SID_ROUTINE_CONTROL, UDS_NRC_SUB_FUNCTION_NOT_SUPPORTED);
break;
}

}

static void routine_0xf0f0(udsRoutineControlType_E routineControlType, uint8_t *payload, uint8_t payloadLengthBytes)
{
UNUSED(payloadLengthBytes);
Expand Down Expand Up @@ -218,6 +253,9 @@ void uds_cb_routineControl(udsRoutineControlType_E routineControlType, uint8_t *

switch (routineId.u16)
{
case 0x00f0:
routine_0x00f0(routineControlType, payload, payloadLengthBytes);;
break;
case 0xf0f0:
routine_0xf0f0(routineControlType, payload, payloadLengthBytes);;
break;
Expand Down
Loading

0 comments on commit 1b120e9

Please sign in to comment.