Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving the location of CAN init to main to prevent race conditions #123

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ extern osThreadId_t can_dispatch_handle;
extern const osThreadAttr_t can_dispatch_attributes;

int8_t queue_can_msg(can_msg_t msg);
can_t *init_can1(CAN_HandleTypeDef *hcan);

#endif
38 changes: 25 additions & 13 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <stdlib.h>
#include "fault.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "cerberus_conf.h"

#define CAN_MSG_QUEUE_SIZE 25 /* messages */
Expand All @@ -23,8 +25,6 @@
static osMessageQueueId_t can_inbound_queue;

/* Relevant Info for Initializing CAN 1 */
static can_t can1;

static uint16_t id_list[NUM_INBOUND_CAN_IDS] = {
//CANID_X,
0
Expand All @@ -48,6 +48,26 @@ static function_info_t can_callbacks[NUM_INBOUND_CAN_IDS] = {
//{ .id = 0x2410, .function = (*MC_update)(can_msg_t) }
};

void can1_callback(CAN_HandleTypeDef *hcan);

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

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

can1->hcan = hcan;
can1->callback = can1_callback;
can1->id_list = id_list;
can1->id_list_len = NUM_INBOUND_CAN_IDS;

assert(can_init(can1));

return can1;
}

static callback_t getFunction(uint8_t id)
{
//TODO: optimization of create algo to more efficiently find handler
Expand Down Expand Up @@ -96,16 +116,6 @@ void vRouteCanIncoming(void* pv_params)
osStatus_t status;
callback_t callback;
fault_data_t fault_data = { .id = CAN_ROUTING_FAULT, .severity = DEFCON2 };
can1.callback = can1_callback;
can1.id_list = id_list;
can1.id_list_len = NUM_INBOUND_CAN_IDS;

can1.hcan = (CAN_HandleTypeDef *)pv_params;

if (can_init(&can1)) {
fault_data.diag = "Failed to init CAN handler";
queue_fault(&fault_data);
}

can_inbound_queue = osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL);

Expand Down Expand Up @@ -148,10 +158,12 @@ void vCanDispatch(void* pv_params)

HAL_StatusTypeDef msg_status;

can_t *can1 = (can_t *)pv_params;

for(;;) {
/* Send CAN message */
if (osOK == osMessageQueueGet(can_outbound_queue, &msg_from_queue, NULL, 1)) {
msg_status = can_send_msg(&can1, &msg_from_queue);
msg_status = can_send_msg(can1, &msg_from_queue);
if (msg_status == HAL_ERROR) {
fault_data.diag = "Failed to send CAN message";
queue_fault(&fault_data);
Expand Down
5 changes: 3 additions & 2 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ int main(void)
/* Create Interfaces to Represent Relevant Hardware */
mpu_t *mpu = init_mpu(&hi2c1, &hadc1, &hadc2, &hadc3, GPIOC, GPIOB);
pdu_t *pdu = init_pdu(&hi2c2);
can_t *can1 = init_can1(&hcan1);

toggle_yled(mpu); // Toggle on LED2
HAL_Delay(500);
Expand Down Expand Up @@ -212,8 +213,8 @@ int main(void)

/* Hardware Messaging */
/* Note that CAN Router initializes CAN */
route_can_incoming_handle = osThreadNew(vRouteCanIncoming, &hcan1, &route_can_incoming_attributes);
can_dispatch_handle = osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes);
route_can_incoming_handle = osThreadNew(vRouteCanIncoming, NULL, &route_can_incoming_attributes);
can_dispatch_handle = osThreadNew(vCanDispatch, can1, &can_dispatch_attributes);
serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes);

/* Control Logic */
Expand Down