Skip to content

Commit

Permalink
Implemented Suggested Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dnakhooda committed Oct 28, 2024
1 parent ff1ec7a commit 8db0864
Showing 1 changed file with 18 additions and 58 deletions.
76 changes: 18 additions & 58 deletions platforms/stm32f405/src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,7 @@
* implement in `stm32xxxx_it.c`, which STM32CubeMX generates
*/

HAL_StatusTypeDef can_init(can_t *can)
{
// Set Up Filters
uint8_t err = HAL_OK;
uint32_t filters_to_add[4];

for (uint8_t i = 0; i < can->id_list_len - (can->id_list_len % 4); i++) {
if ((i + 1) % 4 == 0)
err = can_add_filter(can,
filters_to_add[i-3],
filters_to_add[i-2],
filters_to_add[i-1],
filters_to_add[i]);

filters_to_add[i % 4] = can->id_list[i];
}

// Ugly Code
if (can->id_list_len % 4 == 3)
err = can_add_filter(can,
can->id_list[can->id_list_len-3],
can->id_list[can->id_list_len-2],
can->id_list[can->id_list_len-1],
can->id_list[can->id_list_len-1]);
else if (can->id_list_len % 4 == 2)
err = can_add_filter(can,
can->id_list[can->id_list_len-2],
can->id_list[can->id_list_len-1],
can->id_list[can->id_list_len-1],
can->id_list[can->id_list_len-1]);
else if (can->id_list_len % 4 == 1)
err = can_add_filter(can,
can->id_list[can->id_list_len-1],
can->id_list[can->id_list_len-1],
can->id_list[can->id_list_len-1],
can->id_list[can->id_list_len-1]);

if (err != HAL_OK)
return err;

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

Expand All @@ -61,32 +22,32 @@ HAL_StatusTypeDef can_init(can_t *can)
return err;
}

HAL_StatusTypeDef can_add_filter(can_t *can, uint32_t id1, uint32_t id2, uint32_t id3, uint32_t id4)
{
HAL_StatusTypeDef can_add_filter(can_t *can, uint32_t *id_list) {
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 = id1 << 5u;
filter.FilterMaskIdLow = id2 << 5u;
filter.FilterIdHigh = id3 << 5u;
filter.FilterMaskIdHigh = id4 << 5u;
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 = 0;

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

return HAL_CAN_Start(can->hcan);
filter.FilterBank = filterBank;

filterBank++;

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

HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg)
{
HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg) {
CAN_TxHeaderTypeDef tx_header;
tx_header.StdId = msg->id;
tx_header.ExtId = 0;
Expand All @@ -105,8 +66,7 @@ HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg)
return HAL_OK;
}

HAL_StatusTypeDef can_send_extended_msg(can_t *can, can_msg_t *msg)
{
HAL_StatusTypeDef can_send_extended_msg(can_t *can, can_msg_t *msg) {
CAN_TxHeaderTypeDef tx_header;
tx_header.StdId = 0;
tx_header.ExtId = msg->id;
Expand Down

0 comments on commit 8db0864

Please sign in to comment.