Skip to content

Commit

Permalink
restore order
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed Sep 5, 2024
1 parent 2042879 commit 2583617
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 156 deletions.
30 changes: 20 additions & 10 deletions Core/Src/bms.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

osMessageQueueId_t bms_monitor_queue;

bms_t* bms;
bms_t *bms;

void bms_fault_callback();

osThreadId_t bms_monitor_handle;
const osThreadAttr_t bms_monitor_attributes
= { .name = "BMSCANMonitor", .stack_size = 64 * 8, .priority = (osPriority_t)osPriorityHigh2 };
const osThreadAttr_t bms_monitor_attributes = {
.name = "BMSCANMonitor",
.stack_size = 64 * 8,
.priority = (osPriority_t)osPriorityHigh2
};

void bms_fault_callback()
{
fault_data_t fault_data

= { .id = BMS_CAN_MONITOR_FAULT, .severity = DEFCON1 }; /*TO-DO: update severity*/
= { .id = BMS_CAN_MONITOR_FAULT,
.severity = DEFCON1 }; /*TO-DO: update severity*/
fault_data.diag = "Failing To Receive CAN Messages from Shepherd";
osTimerStart(bms->bms_monitor_timer, BMS_CAN_MONITOR_DELAY);
queue_fault(&fault_data);
Expand All @@ -36,22 +40,28 @@ void bms_init()
assert(bms);

/*TO-DO: specify timer attributes*/
bms->bms_monitor_timer = osTimerNew(&bms_fault_callback, osTimerOnce, NULL, NULL);
bms->bms_monitor_timer =
osTimerNew(&bms_fault_callback, osTimerOnce, NULL, NULL);

bms_monitor_queue = osMessageQueueNew(CAN_QUEUE_SIZE, sizeof(can_msg_t), NULL);
bms_monitor_queue =
osMessageQueueNew(CAN_QUEUE_SIZE, sizeof(can_msg_t), NULL);
assert(bms_monitor_queue);
}

void vBMSCANMonitor(void* pv_params)
void vBMSCANMonitor(void *pv_params)
{
can_msg_t msg_from_queue;

osTimerStart(bms->bms_monitor_timer, BMS_CAN_MONITOR_DELAY);
for (;;) {
if (osOK == osMessageQueueGet(bms_monitor_queue, &msg_from_queue, NULL, osWaitForever)) {
if (osOK == osMessageQueueGet(bms_monitor_queue,
&msg_from_queue, NULL,
osWaitForever)) {
/*TO-DO: fix duration (ticks)*/
osTimerStart(bms->bms_monitor_timer, BMS_CAN_MONITOR_DELAY);
bms->dcl = (uint16_t)((msg_from_queue.data[1] << 8) & msg_from_queue.data[0]);
osTimerStart(bms->bms_monitor_timer,
BMS_CAN_MONITOR_DELAY);
bms->dcl = (uint16_t)((msg_from_queue.data[1] << 8) &
msg_from_queue.data[0]);
// serial_print("BMS DCL %d", bms->dcl);
}
}
Expand Down
38 changes: 22 additions & 16 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,52 @@
#define CAN_MSG_QUEUE_SIZE 50 /* messages */
static osMessageQueueId_t can_outbound_queue;

can_t* can1;
can_t *can1;

/* Relevant Info for Initializing CAN 1 */
static uint32_t id_list[] = { DTI_CANID_ERPM, DTI_CANID_CURRENTS, DTI_CANID_TEMPS_FAULT,
DTI_CANID_ID_IQ, DTI_CANID_SIGNALS, BMS_DCL_MSG };
static uint32_t id_list[] = { DTI_CANID_ERPM, DTI_CANID_CURRENTS,
DTI_CANID_TEMPS_FAULT, DTI_CANID_ID_IQ,
DTI_CANID_SIGNALS, BMS_DCL_MSG };

void init_can1(CAN_HandleTypeDef* hcan)
void init_can1(CAN_HandleTypeDef *hcan)
{
assert(hcan);

/* Create PDU struct */
can1 = malloc(sizeof(can_t));
assert(can1);

can1->hcan = hcan;
can1->id_list = id_list;
can1->hcan = hcan;
can1->id_list = id_list;
can1->id_list_len = sizeof(id_list) / sizeof(uint32_t);

assert(!can_init(can1));

can_outbound_queue = osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL);
can_outbound_queue =
osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL);
}

/* Callback to be called when we get a CAN message */
void can1_callback(CAN_HandleTypeDef* hcan)
void can1_callback(CAN_HandleTypeDef *hcan)
{
fault_data_t fault_data = {
.id = CAN_ROUTING_FAULT,
.id = CAN_ROUTING_FAULT,
.severity = DEFCON2,
};

CAN_RxHeaderTypeDef rx_header;
can_msg_t new_msg;

/* Read in CAN message */
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, new_msg.data) != HAL_OK) {
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header,
new_msg.data) != HAL_OK) {
fault_data.diag = "Failed to read CAN Msg";
queue_fault(&fault_data);
return;
}

new_msg.len = rx_header.DLC;
new_msg.id = rx_header.StdId;
new_msg.id = rx_header.StdId;

// TODO: Switch to hash map
switch (new_msg.id) {
Expand All @@ -90,21 +93,24 @@ void can1_callback(CAN_HandleTypeDef* hcan)

osThreadId_t can_dispatch_handle;
const osThreadAttr_t can_dispatch_attributes = {
.name = "CanDispatch",
.name = "CanDispatch",
.stack_size = 128 * 8,
.priority = (osPriority_t)osPriorityRealtime5,
.priority = (osPriority_t)osPriorityRealtime5,
};

void vCanDispatch(void* pv_params)
void vCanDispatch(void *pv_params)
{
fault_data_t fault_data = { .id = CAN_DISPATCH_FAULT, .severity = DEFCON1 };
fault_data_t fault_data = { .id = CAN_DISPATCH_FAULT,
.severity = DEFCON1 };

can_msg_t msg_from_queue;
HAL_StatusTypeDef msg_status;

for (;;) {
/* Send CAN message */
if (osOK == osMessageQueueGet(can_outbound_queue, &msg_from_queue, NULL, osWaitForever)) {
if (osOK == osMessageQueueGet(can_outbound_queue,
&msg_from_queue, NULL,
osWaitForever)) {
msg_status = can_send_msg(can1, &msg_from_queue);
if (msg_status == HAL_ERROR) {
fault_data.diag = "Failed to send CAN message";
Expand Down
22 changes: 13 additions & 9 deletions Core/Src/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ osMessageQueueId_t fault_handle_queue;

osThreadId_t fault_handle;
const osThreadAttr_t fault_handle_attributes = {
.name = "FaultHandler",
.name = "FaultHandler",
.stack_size = 52 * 8,
.priority = (osPriority_t)osPriorityRealtime1,
.priority = (osPriority_t)osPriorityRealtime1,
};

int queue_fault(fault_data_t* fault_data)
int queue_fault(fault_data_t *fault_data)
{
if (!fault_handle_queue)
return -1;
Expand All @@ -28,30 +28,34 @@ int queue_fault(fault_data_t* fault_data)
return 0;
}

void vFaultHandler(void* pv_params)
void vFaultHandler(void *pv_params)
{
fault_data_t fault_data;
osStatus_t status;
const state_req_t fault_request = { .id = FUNCTIONAL, .state.functional = FAULTED };
fault_handle_queue = osMessageQueueNew(FAULT_HANDLE_QUEUE_SIZE, sizeof(fault_data_t), NULL);
const state_req_t fault_request = { .id = FUNCTIONAL,
.state.functional = FAULTED };
fault_handle_queue = osMessageQueueNew(FAULT_HANDLE_QUEUE_SIZE,
sizeof(fault_data_t), NULL);

for (;;) {
/* Wait until a message is in the queue, send messages when they are in the queue */
status = osMessageQueueGet(fault_handle_queue, &fault_data, NULL, osWaitForever);
status = osMessageQueueGet(fault_handle_queue, &fault_data,
NULL, osWaitForever);
if (status == osOK) {
uint32_t fault_id = (uint32_t)fault_data.id;
endian_swap(&fault_id, 4);
uint8_t defcon = (uint8_t)fault_data.severity;

can_msg_t msg;
msg.id = CANID_FAULT_MSG;
msg.id = CANID_FAULT_MSG;
msg.len = 8;
uint8_t msg_data[8];
memcpy(msg_data, &fault_id, 4);
memcpy(msg_data + 4, &defcon, 1);
memcpy(msg.data, msg_data, msg.len);
queue_can_msg(msg);
printf("\r\nFault Handler! Diagnostic Info:\t%s\r\n\r\n", fault_data.diag);
printf("\r\nFault Handler! Diagnostic Info:\t%s\r\n\r\n",
fault_data.diag);
switch (fault_data.severity) {
case DEFCON1: /* Highest(1st) Priority */
queue_state_transition(fault_request);
Expand Down
Loading

0 comments on commit 2583617

Please sign in to comment.