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

bug/MessageFiltering #186

Merged
merged 11 commits into from
Nov 4, 2024
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
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
Loading