Skip to content

Commit

Permalink
bit fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Myers-Ty committed Jan 31, 2025
1 parent a406abc commit 85935c0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 66 deletions.
56 changes: 23 additions & 33 deletions Core/Inc/pdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,18 @@ int8_t write_brakelight(pdu_t *pdu, bool state);
int8_t write_fan_battbox(pdu_t *pdu, bool state);
int8_t write_rtds(pdu_t *pdu, bool state);

/* Function to Read the Status of Fuses from PDU */
typedef enum {
BATTBOX_FUSE_STAT = 0,
LV_BOARDS_FUSE_STAT,
RADFAN_FUSE_STAT,
BUCK_FUSE_STAT,
FANBATTBOX_FUSE_STAT,
PUMP_FUSE_STAT0,
DASHBOARD_FUSE_STAT,
BRKLIGHT_FUSE_STAT,
SD_TO_BRB_FUSE_STAT,
PUMP_FUSE_STAT1,
MAX_FUSES
} fuse_t;

typedef struct {
uint16_t f : 10; // 10 = fuse_t.values().length()
char BATTBOX_FUSE_STAT : 1;
char LV_BOARDS_FUSE_STAT : 1;
char RADFAN_FUSE_STAT : 1;
char BUCK_FUSE_STAT : 1;
char FANBATTBOX_FUSE_STAT : 1;
char PUMP_FUSE_STAT0 : 1;
char DASHBOARD_FUSE_STAT : 1;
char BRKLIGHT_FUSE_STAT : 1;
char SD_TO_BRB_FUSE_STAT : 1;
char PUMP_FUSE_STAT1 : 1;
char MAX_FUSES : 1;
} fuse_bitfield;

/**
Expand All @@ -72,24 +67,19 @@ int8_t read_fuses(pdu_t *pdu, fuse_bitfield *status);
*/
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 = 0, /* Cockpit BRB */
BMS_GOOD, /* Battery Management System (Shepherd) */
INERTIA_SW_GOOD, /* Inertia Switch */
SPARE_GPIO1,
IMD_GOOD, /* Insulation Monitoring Device */
BSPD_GOOD, /* Brake System Plausbility Device */
BOTS_GOOD, /* Brake Over Travel Switch */
HVD_INTLK_GOOD, /* HVD Interlock */
HVC_INTLK_GOOD, /* HV C Interlock*/
//SIDE_BRB_CLR, /* Side BRB */
//TSMS, /* Tractive System Main Switch */
MAX_SHUTDOWN_STAGES
} shutdown_stage_t;

typedef struct {
uint16_t s : 9; // 9 = shutdown_stage_t.values().length()
char CKPT_BRB_CLR : 1; /* Cockpit BRB */
char BMS_GOOD : 1; /* Battery Management System (Shepherd) */
char INERTIA_SW_GOOD : 1; /* Inertia Switch */
char SPARE_GPIO1;
char IMD_GOOD : 1; /* Insulation Monitoring Device */
char BSPD_GOOD : 1; /* Brake System Plausbility Device */
char BOTS_GOOD : 1; /* Brake Over Travel Switch */
char HVD_INTLK_GOOD : 1; /* HVD Interlock */
char HVC_INTLK_GOOD : 1; /* HV C Interlock*/
//char SIDE_BRB_CLR : 1; /* Side BRB */
//char TSMS : 1; /* Tractive System Main Switch */
char MAX_SHUTDOWN_STAGES : 1;
} shutdown_bitfield;

/**
Expand Down
21 changes: 14 additions & 7 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,25 @@ 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;
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)) {
fault_data.diag = "Failed to read fuses";
queue_fault(&fault_data);
}

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

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

// reverse the bit order
fuse_data.fuse_1 = reverse_bits(fuse_data.fuse_1);
Expand Down Expand Up @@ -348,23 +351,27 @@ void vShutdownMonitor(void *pv_params)
.len = 2,
.data = { 0 } };
pdu_t *pdu = (pdu_t *)pv_params;
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, &shutdowns)) {
fault_data.diag = "Failed to read shutdown buffer";
queue_fault(&fault_data);
}

memcpy(&shutdown_buf, &shutdowns, 2);

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

// reverse the bit order
shutdown_data.shut_1 = reverse_bits(shutdown_data.shut_1);
Expand Down
46 changes: 20 additions & 26 deletions Core/Src/pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,23 +295,17 @@ int8_t read_fuses(pdu_t *pdu, fuse_bitfield *status)
return error;
}

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;
status->BATTBOX_FUSE_STAT = (bank0_d >> PIN_BATTBOX_FUSE_STAT) & 1;
status->LV_BOARDS_FUSE_STAT = (bank0_d >> PIN_LV_BOARDS_FUSE_STAT) & 1;
status->RADFAN_FUSE_STAT = (bank0_d >> PIN_RADFAN_FUSE_STAT) & 1;
status->BUCK_FUSE_STAT = (bank1_d >> PIN_BUCK_FUSE_STAT) & 1;
status->FANBATTBOX_FUSE_STAT = (bank1_d >> PIN_FANBATTBOX_FUSE_STAT) &
1;
status->PUMP_FUSE_STAT0 = (bank1_d >> PIN_PUMP_FUSE_STAT0) & 1;
status->DASHBOARD_FUSE_STAT = (bank1_d >> PIN_DASHBOARD_FUSE_STAT) & 1;
status->BRKLIGHT_FUSE_STAT = (bank1_d >> PIN_BRKLIGHT_FUSE_STAT) & 1;
status->SD_TO_BRB_FUSE_STAT = (bank1_d >> PIN_SD_TO_BRB_FUSE_STAT) & 1;
status->PUMP_FUSE_STAT1 = (bank1_d >> PIN_PUMP_FUSE_STAT1) & 1;

osMutexRelease(pdu->mutex);
return 0;
Expand Down Expand Up @@ -365,15 +359,15 @@ int8_t read_shutdown(pdu_t *pdu, shutdown_bitfield *status)
return error;
}

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;
status->CKPT_BRB_CLR = (bank0_d >> PIN_CKPT_BRB_CLR) & 1;
status->BMS_GOOD = (bank0_d >> PIN_BMS_GOOD) & 1;
status->INERTIA_SW_GOOD = (bank0_d >> PIN_INERTIA_SW_GOOD) & 1;
status->SPARE_GPIO1 = (bank0_d >> PIN_SPARE_GPIO1) & 1;
status->IMD_GOOD = (bank0_d >> PIN_IMD_GOOD) & 1;
status->BSPD_GOOD = (bank0_d >> PIN_BSPD_GOOD) & 1;
status->BOTS_GOOD = (bank1_d >> PIN_BOTS_GOOD) & 1;
status->HVD_INTLK_GOOD = (bank1_d >> PIN_HVD_INTLK_GOOD) & 1;
status->HVC_INTLK_GOOD = (bank1_d >> PIN_HVC_INTLK_GOOD) & 1;

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

0 comments on commit 85935c0

Please sign in to comment.