Skip to content

Commit

Permalink
Redoing because I misinterpreted function call
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Dec 14, 2023
1 parent 78dcc05 commit f2c5060
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
8 changes: 4 additions & 4 deletions platforms/stm32f405/include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_can.h"

/* function pointer type for the callback */
typedef void (*can_callback_t)(CAN_HandleTypeDef *hcan);

typedef struct{
CAN_HandleTypeDef *hcan;
uint16_t *id_list;
uint8_t id_list_len;

/* desired behavior varies by app - so implement this at app level */
void (*can_callback)(CAN_HandleTypeDef *hcan);
can_callback_t callback;
} can_t;

typedef struct{
Expand All @@ -23,9 +26,6 @@ typedef struct{
uint8_t len;
} can_msg_t;

/* function pointer type for the callback */
typedef void (*can_callback)(CAN_HandleTypeDef *hcan);

HAL_StatusTypeDef can_init(can_t *can, can_callback callback);
HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg);

Expand Down
49 changes: 47 additions & 2 deletions platforms/stm32f405/src/can.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
#include "can.h"
#include <string.h>

/* NOTE: STM32F405 will have MAX of 3 CAN buses */
#define MAX_CAN_BUS 3

can_t *can_struct_list[MAX_CAN_BUS] = {NULL, NULL, NULL};

static can_callback_t find_callback(CAN_HandleTypeDef *hcan)
{
for (uint8_t i = 0; i < MAX_CAN_BUS; i++) {
if (hcan == can_struct_list[i]->hcan)
return can_struct_list[i]->callback;
}
return NULL;
}

/* Add a CAN interfae to be searched for during the event of a callback */
static uint8_t add_interface(can_t *interface)
{
for (uint8_t i = 0; i < MAX_CAN_BUS; i++) {
/* Interface already added */
if (interface->hcan == can_struct_list[i]->hcan)
return -1;

/* If empty, add interface */
if (can_struct_list[i]->hcan == NULL) {
can_struct_list[i] = interface;
return 0;
}
}

/* No open slots, something is wrong */
return -2;
}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
/* Handle CAN reception event */
can_callback_t callback= find_callback(hcan);

if (callback != NULL)
{
callback(hcan);
}
}

HAL_StatusTypeDef can_init(can_t *can, can_callback callback)
{
Expand Down Expand Up @@ -41,8 +86,8 @@ HAL_StatusTypeDef can_init(can_t *can, can_callback callback)
return err;

/* Override the default callback for CAN_IT_RX_FIFO0_MSG_PENDING */
err = HAL_CAN_ActivateNotification(can->hcan, callback);
can->can_callback = callback;
err = HAL_CAN_ActivateNotification(can->hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
err = add_interface(can);

return err;
}
Expand Down

0 comments on commit f2c5060

Please sign in to comment.