Skip to content

Commit

Permalink
bitfield changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Myers-Ty committed Jan 29, 2025
1 parent c00889c commit a406abc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 72 deletions.
16 changes: 12 additions & 4 deletions Core/Inc/pdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int8_t write_rtds(pdu_t *pdu, bool state);

/* Function to Read the Status of Fuses from PDU */
typedef enum {
BATTBOX_FUSE_STAT,
BATTBOX_FUSE_STAT = 0,
LV_BOARDS_FUSE_STAT,
RADFAN_FUSE_STAT,
BUCK_FUSE_STAT,
Expand All @@ -50,14 +50,18 @@ typedef enum {
MAX_FUSES
} fuse_t;

typedef struct {
uint16_t f : 10; // 10 = fuse_t.values().length()
} fuse_bitfield;

/**
* @brief Read the status of the PDU fuses.
*
* @param pdu Pointer to struct representing the PDU
* @param status Buffer that fuse data will be written to
* @return int8_t Error code resulting from reading GPIO expander pins over I2C or mutex acquisition
*/
int8_t read_fuses(pdu_t *pdu, bool status[MAX_FUSES]);
int8_t read_fuses(pdu_t *pdu, fuse_bitfield *status);

/**
* @brief Read the state of the TSMS signal.
Expand All @@ -70,7 +74,7 @@ int8_t read_tsms_sense(pdu_t *pdu, bool *status);

/* Functions to Read Status of Various Stages of Shutdown Loop */
typedef enum {
CKPT_BRB_CLR, /* Cockpit BRB */
CKPT_BRB_CLR = 0, /* Cockpit BRB */
BMS_GOOD, /* Battery Management System (Shepherd) */
INERTIA_SW_GOOD, /* Inertia Switch */
SPARE_GPIO1,
Expand All @@ -84,14 +88,18 @@ typedef enum {
MAX_SHUTDOWN_STAGES
} shutdown_stage_t;

typedef struct {
uint16_t s : 9; // 9 = shutdown_stage_t.values().length()
} shutdown_bitfield;

/**
* @brief Read the status of the shutdown loop.
*
* @param pdu Pointer to struct representing the PDU
* @param status Buffer that fuse data will be written to
* @return int8_t Result of reading pins on the shutdown monitor GPIO expander of the PDU or result of mutex acquisition
*/
int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES]);
int8_t read_shutdown(pdu_t *pdu, shutdown_bitfield *status);

/**
* @brief Read the status of the shutdown loop.
Expand Down
38 changes: 11 additions & 27 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,22 @@ void read_fuse_data(void *arg)
fault_data_t fault_data = { .id = FUSE_MONITOR_FAULT,
.severity = DEFCON5 };
can_msg_t fuse_msg = { .id = CANID_FUSE, .len = 2, .data = { 0 } };
uint16_t fuse_buf;
bool fuses[MAX_FUSES] = { 0 };

fuse_bitfield fuses;
fuses.f = 0;

struct __attribute__((__packed__)) {
uint8_t fuse_1;
uint8_t fuse_2;
} fuse_data;

fuse_buf = 0;

if (read_fuses(pdu, fuses)) {
if (read_fuses(pdu, &fuses)) {
fault_data.diag = "Failed to read fuses";
queue_fault(&fault_data);
}

for (fuse_t fuse = 0; fuse < MAX_FUSES; fuse++) {
fuse_buf |=
fuses[fuse]
<< fuse; /* Sets the bit at position `fuse` to the state of the fuse */
}

fuse_data.fuse_1 = fuse_buf & 0xFF;
fuse_data.fuse_2 = (fuse_buf >> 8) & 0xFF;
fuse_data.fuse_1 = fuses.f & 0xFF;
fuse_data.fuse_2 = (fuses.f >> 8) & 0xFF;

// reverse the bit order
fuse_data.fuse_1 = reverse_bits(fuse_data.fuse_1);
Expand Down Expand Up @@ -355,32 +348,23 @@ void vShutdownMonitor(void *pv_params)
.len = 2,
.data = { 0 } };
pdu_t *pdu = (pdu_t *)pv_params;
bool shutdown_loop[MAX_SHUTDOWN_STAGES] = { 0 };
uint16_t shutdown_buf;
shutdown_bitfield shutdowns;
shutdowns.s = 0;

struct __attribute__((__packed__)) {
uint8_t shut_1;
uint8_t shut_2;
} shutdown_data;

for (;;) {
shutdown_buf = 0;

if (read_shutdown(pdu, shutdown_loop)) {
if (read_shutdown(pdu, &shutdowns)) {
fault_data.diag = "Failed to read shutdown buffer";
queue_fault(&fault_data);
}

for (shutdown_stage_t stage = 0; stage < MAX_SHUTDOWN_STAGES;
stage++) {
shutdown_buf |=
shutdown_loop[stage]
<< stage; /* Sets the bit at position `stage` to the state of the stage */
}

/* seperate each byte */
shutdown_data.shut_1 = shutdown_buf & 0xFF;
shutdown_data.shut_2 = (shutdown_buf >> 8) & 0xFF;
shutdown_data.shut_1 = shutdowns.s & 0xFF;
shutdown_data.shut_2 = (shutdowns.s >> 8) & 0xFF;

// reverse the bit order
shutdown_data.shut_1 = reverse_bits(shutdown_data.shut_1);
Expand Down
68 changes: 28 additions & 40 deletions Core/Src/pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,7 @@ void read_pump_sensors(pdu_t *pdu, uint32_t pump_sensors_buf[2])
sizeof(pdu->pump_sensors_dma_buf));
}

static void deconstruct_buf(uint8_t data, bool config[8])
{
for (uint8_t i = 0; i < 8; i++) {
config[i] = (data >> i) & 1;
}
}

int8_t read_fuses(pdu_t *pdu, bool status[MAX_FUSES])
int8_t read_fuses(pdu_t *pdu, fuse_bitfield *status)
{
if (!pdu)
return -1;
Expand All @@ -302,22 +295,23 @@ int8_t read_fuses(pdu_t *pdu, bool status[MAX_FUSES])
return error;
}

bool bank0[8];
deconstruct_buf(bank0_d, bank0);

bool bank1[8];
deconstruct_buf(bank1_d, bank1);

status[BATTBOX_FUSE_STAT] = bank0[PIN_BATTBOX_FUSE_STAT];
status[LV_BOARDS_FUSE_STAT] = bank0[PIN_LV_BOARDS_FUSE_STAT];
status[RADFAN_FUSE_STAT] = bank0[PIN_RADFAN_FUSE_STAT];
status[BUCK_FUSE_STAT] = bank1[PIN_BUCK_FUSE_STAT];
status[FANBATTBOX_FUSE_STAT] = bank1[PIN_FANBATTBOX_FUSE_STAT];
status[PUMP_FUSE_STAT0] = bank1[PIN_PUMP_FUSE_STAT0];
status[DASHBOARD_FUSE_STAT] = bank1[PIN_DASHBOARD_FUSE_STAT];
status[BRKLIGHT_FUSE_STAT] = bank1[PIN_BRKLIGHT_FUSE_STAT];
status[SD_TO_BRB_FUSE_STAT] = bank1[PIN_SD_TO_BRB_FUSE_STAT];
status[PUMP_FUSE_STAT1] = bank1[PIN_PUMP_FUSE_STAT1];
status->f |= ((bank0_d >> PIN_BATTBOX_FUSE_STAT) & 1)
<< BATTBOX_FUSE_STAT;
status->f |= ((bank0_d >> PIN_LV_BOARDS_FUSE_STAT) & 1)
<< LV_BOARDS_FUSE_STAT;
status->f |= ((bank0_d >> PIN_RADFAN_FUSE_STAT) & 1)
<< RADFAN_FUSE_STAT;
status->f |= ((bank1_d >> PIN_BUCK_FUSE_STAT) & 1) << BUCK_FUSE_STAT;
status->f |= ((bank1_d >> PIN_FANBATTBOX_FUSE_STAT) & 1)
<< FANBATTBOX_FUSE_STAT;
status->f |= ((bank1_d >> PIN_PUMP_FUSE_STAT0) & 1) << PUMP_FUSE_STAT0;
status->f |= ((bank1_d >> PIN_DASHBOARD_FUSE_STAT) & 1)
<< DASHBOARD_FUSE_STAT;
status->f |= ((bank1_d >> PIN_BRKLIGHT_FUSE_STAT) & 1)
<< BRKLIGHT_FUSE_STAT;
status->f |= ((bank1_d >> PIN_SD_TO_BRB_FUSE_STAT) & 1)
<< SD_TO_BRB_FUSE_STAT;
status->f |= ((bank1_d >> PIN_PUMP_FUSE_STAT1) & 1) << PUMP_FUSE_STAT1;

osMutexRelease(pdu->mutex);
return 0;
Expand Down Expand Up @@ -347,7 +341,7 @@ int8_t read_tsms_sense(pdu_t *pdu, bool *status)
return 0;
}

int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES])
int8_t read_shutdown(pdu_t *pdu, shutdown_bitfield *status)
{
if (!pdu)
return -1;
Expand All @@ -371,21 +365,15 @@ int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES])
return error;
}

bool bank0[8];
deconstruct_buf(bank0_d, bank0);

bool bank1[8];
deconstruct_buf(bank1_d, bank1);

status[CKPT_BRB_CLR] = bank0[PIN_CKPT_BRB_CLR];
status[BMS_GOOD] = bank0[PIN_BMS_GOOD];
status[INERTIA_SW_GOOD] = bank0[PIN_INERTIA_SW_GOOD];
status[SPARE_GPIO1] = bank0[PIN_SPARE_GPIO1];
status[IMD_GOOD] = bank0[PIN_IMD_GOOD];
status[BSPD_GOOD] = bank0[PIN_BSPD_GOOD];
status[BOTS_GOOD] = bank1[PIN_BOTS_GOOD];
status[HVD_INTLK_GOOD] = bank1[PIN_HVD_INTLK_GOOD];
status[HVC_INTLK_GOOD] = bank1[PIN_HVC_INTLK_GOOD];
status->s |= ((bank0_d >> PIN_CKPT_BRB_CLR) & 1) << CKPT_BRB_CLR;
status->s |= ((bank0_d >> PIN_BMS_GOOD) & 1) << BMS_GOOD;
status->s |= ((bank0_d >> PIN_INERTIA_SW_GOOD) & 1) << INERTIA_SW_GOOD;
status->s |= ((bank0_d >> PIN_SPARE_GPIO1) & 1) << SPARE_GPIO1;
status->s |= ((bank0_d >> PIN_IMD_GOOD) & 1) << IMD_GOOD;
status->s |= ((bank0_d >> PIN_BSPD_GOOD) & 1) << BSPD_GOOD;
status->s |= ((bank1_d >> PIN_BOTS_GOOD) & 1) << BOTS_GOOD;
status->s |= ((bank1_d >> PIN_HVD_INTLK_GOOD) & 1) << HVD_INTLK_GOOD;
status->s |= ((bank1_d >> PIN_HVC_INTLK_GOOD) & 1) << HVC_INTLK_GOOD;

osMutexRelease(pdu->mutex);
return 0;
Expand Down

0 comments on commit a406abc

Please sign in to comment.