Skip to content

Commit

Permalink
bug/MessageFiltering (#186)
Browse files Browse the repository at this point in the history
* Possibly Working Filters?

* Fixed A Couple Bugs

* Even More Bug Fixes

* Implemented Suggested Changes

* Fix Header File

* Change Passing In Array

* Fixed Error

* Fix Formatting

* Remove IQR Handler Call

* Remove Can List Attributes
  • Loading branch information
dnakhooda authored Nov 4, 2024
1 parent 20415db commit a5c81fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 54 deletions.
3 changes: 1 addition & 2 deletions platforms/stm32f405/include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

typedef struct {
CAN_HandleTypeDef *hcan;
const uint32_t *id_list;
uint8_t id_list_len;
} can_t;

typedef struct {
Expand All @@ -26,6 +24,7 @@ typedef struct {
} can_msg_t;

HAL_StatusTypeDef can_init(can_t *can);
HAL_StatusTypeDef can_add_filter(can_t *can, uint32_t *id_list);
HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg);
HAL_StatusTypeDef can_send_extended_msg(can_t *can, can_msg_t *msg);

Expand Down
80 changes: 28 additions & 52 deletions platforms/stm32f405/src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,9 @@

HAL_StatusTypeDef can_init(can_t *can)
{
/* set up filter */
uint16_t high_id = can->id_list[0];
uint16_t low_id = can->id_list[0];

for (uint8_t i = 0; i < can->id_list_len; i++) {
if (can->id_list[i] > high_id)
high_id = can->id_list[i];
if (can->id_list[i] < low_id)
low_id = can->id_list[i];
}

// uint32_t full_id = ((uint32_t)high_id << 16) | low_id;

CAN_FilterTypeDef sFilterConfig;

sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;

// sFilterConfig.FilterBank = 0; /* Filter bank number
// (0 to 27 for most STM32 series) */ sFilterConfig.FilterMode =
// CAN_FILTERMODE_IDLIST; /* Identifier list mode */
// sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; /* 32-bit identifier
// list */

// sFilterConfig.FilterIdHigh = (full_id & 0xFFFF0000U) >> 5;
// sFilterConfig.FilterIdLow = (full_id & 0xFFFFU) << 5;

// sFilterConfig.FilterMaskIdHigh = 0xFFFF << 5; /* Set to all ones for
// ID range */ sFilterConfig.FilterMaskIdLow = 0xFFFF; /* Set to
// all ones for ID range */

// sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; /* FIFO to assign the
// filter to */ sFilterConfig.FilterActivation = ENABLE; /* Enable
// the filter */

uint8_t err = 0;
err = HAL_CAN_ConfigFilter(can->hcan, &sFilterConfig);
if (err != HAL_OK)
return err;

/* set up interrupt & activate CAN */
HAL_CAN_IRQHandler(can->hcan);

err = HAL_CAN_ActivateNotification(can->hcan,
CAN_IT_RX_FIFO0_MSG_PENDING);
int8_t err = HAL_CAN_ActivateNotification(can->hcan,
CAN_IT_RX_FIFO0_MSG_PENDING);
if (err != HAL_OK)
return err;
err = HAL_CAN_Start(can->hcan);
Expand All @@ -71,6 +21,32 @@ HAL_StatusTypeDef can_init(can_t *can)
return err;
}

HAL_StatusTypeDef can_add_filter(can_t *can, uint32_t id_list[4])
{
static int filterBank = 0;

if (filterBank > 7)
return HAL_ERROR;

CAN_FilterTypeDef filter;

filter.FilterActivation = ENABLE;
filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
filter.FilterScale = CAN_FILTERSCALE_16BIT;
filter.FilterMode = CAN_FILTERMODE_IDLIST;

filter.FilterIdLow = id_list[0] << 5u;
filter.FilterMaskIdLow = id_list[1] << 5u;
filter.FilterIdHigh = id_list[2] << 5u;
filter.FilterMaskIdHigh = id_list[3] << 5u;

filter.FilterBank = filterBank;

filterBank++;

return HAL_CAN_ConfigFilter(can->hcan, &filter);
}

HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg)
{
CAN_TxHeaderTypeDef tx_header;
Expand Down

0 comments on commit a5c81fb

Please sign in to comment.