diff --git a/.github/workflows/format-check.yml b/.github/workflows/format-check.yml new file mode 100644 index 0000000..b370b61 --- /dev/null +++ b/.github/workflows/format-check.yml @@ -0,0 +1,19 @@ +name: Code Style Check + +on: [push] + +jobs: + formatting-check: + name: Check format of C + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Run clang-format style check for C/C++ sources + uses: Northeastern-Electric-Racing/clang-format-action@main + with: + clang-format-version: '18' + # exclude vl6180x + # use the clang-format from embedded base + format-filepath: "./clang-format" diff --git a/.gitignore b/.gitignore index 835cd1f..825d20a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .vscode/ __pycache__/ -*.egg-info/ \ No newline at end of file +*.egg-info/ + +*.nix \ No newline at end of file diff --git a/general/.clang-format-ignore b/general/.clang-format-ignore new file mode 100644 index 0000000..4830892 --- /dev/null +++ b/general/.clang-format-ignore @@ -0,0 +1 @@ +./*/vl6180x* \ No newline at end of file diff --git a/general/src/LTC4015.c b/general/src/LTC4015.c index 6d8ea49..662f98d 100644 --- a/general/src/LTC4015.c +++ b/general/src/LTC4015.c @@ -62,7 +62,7 @@ HAL_StatusTypeDef LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger return (QCOUNT, highAlert) // Need away to tell its being flagged, but not - // really sure what to return + // really sure what to return } else if (LIMIT_ALERTS | 0x2000 == 0x2000) { LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS ^ 0x2000); LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger diff --git a/general/src/ltc68041.c b/general/src/ltc68041.c index cad8d4d..66eee4a 100644 --- a/general/src/ltc68041.c +++ b/general/src/ltc68041.c @@ -193,7 +193,7 @@ void LTC6804_adcv(ltc_config *config) // 3 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake. This command can be removed. + // awake. This command can be removed. // 4 HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); @@ -243,7 +243,7 @@ void LTC6804_adax(ltc_config *config) wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake. This command can be removed. + // awake. This command can be removed. HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); HAL_SPI_Transmit(config->spi, cmd, 4, HAL_MAX_DELAY); HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_SET); @@ -346,7 +346,7 @@ LTC6804_rdcv(ltc_config *config, cell_data[data_counter] + (cell_data[data_counter + 1] << 8); // Each cell code is received as - // two bytes and is combined to + // two bytes and is combined to // create the parsed cell voltage code cell_codes[current_ic][current_cell + @@ -363,7 +363,7 @@ LTC6804_rdcv(ltc_config *config, (cell_data[data_counter] << 8) + cell_data[data_counter + 1]; // The received PEC for the current_ic - // is transmitted as the 7th and 8th + // is transmitted as the 7th and 8th // after the 6 cell voltage data bytes data_pec = pec15_calc( BYT_IN_REG, @@ -371,13 +371,13 @@ LTC6804_rdcv(ltc_config *config, if (received_pec != data_pec) { pec_error = -1; // The pec_error variable is simply set negative if - // any PEC errors + // any PEC errors // are detected in the serial data } data_counter = data_counter + 2; // Because the transmitted PEC code is - // 2 bytes long the data_counter + // 2 bytes long the data_counter // must be incremented by 2 bytes to point to the next ICs cell voltage // data } @@ -403,7 +403,7 @@ LTC6804_rdcv(ltc_config *config, cell_data[data_counter] + (cell_data[data_counter + 1] << 8); // Each cell code is received as - // two bytes and is combined to + // two bytes and is combined to // create the parsed cell voltage code cell_codes[current_ic] @@ -420,7 +420,7 @@ LTC6804_rdcv(ltc_config *config, (cell_data[data_counter] << 8) + cell_data[data_counter + 1]; // The received PEC for the current_ic is - // transmitted as the 7th and 8th + // transmitted as the 7th and 8th // after the 6 cell voltage data bytes data_pec = pec15_calc(BYT_IN_REG, @@ -428,13 +428,13 @@ LTC6804_rdcv(ltc_config *config, if (received_pec != data_pec) { pec_error = -1; // The pec_error variable is simply set negative if any - // PEC errors + // PEC errors // are detected in the serial data } data_counter = data_counter + 2; // Because the transmitted PEC code is 2 - // bytes long the data_counter + // bytes long the data_counter // must be incremented by 2 bytes to point to the next ICs cell voltage // data } @@ -536,7 +536,7 @@ void LTC6804_rdcv_reg( // 3 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake. This command can be removed. + // awake. This command can be removed. // 4 HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); @@ -624,7 +624,7 @@ int8_t LTC6804_rdaux( LTC6804_rdaux_reg( config, gpio_reg, total_ic, data); // Reads the raw auxiliary register data into - // the data[] array + // the data[] array for (uint8_t current_ic = 0; current_ic < total_ic; @@ -644,7 +644,7 @@ int8_t LTC6804_rdaux( data[data_counter] + (data[data_counter + 1] << 8); // Each gpio codes is received as - // two bytes and is combined to + // two bytes and is combined to // create the parsed gpio voltage code aux_codes[current_ic] @@ -662,7 +662,7 @@ int8_t LTC6804_rdaux( (data[data_counter] << 8) + data[data_counter + 1]; // The received PEC for the current_ic is - // transmitted as the 7th and 8th + // transmitted as the 7th and 8th // after the 6 gpio voltage data bytes data_pec = pec15_calc( BYT_IN_REG, @@ -670,7 +670,7 @@ int8_t LTC6804_rdaux( if (received_pec == data_pec) { pec_error = 0; // The pec_error variable is simply set negative if - // any PEC errors + // any PEC errors // are detected in the received serial data } else if (received_pec != data_pec) { if (retries == @@ -690,7 +690,7 @@ int8_t LTC6804_rdaux( data_counter = data_counter + 2; // Because the transmitted PEC code - // is 2 bytes long the data_counter + // is 2 bytes long the data_counter // must be incremented by 2 bytes to point to the next ICs gpio // voltage data } @@ -716,7 +716,7 @@ int8_t LTC6804_rdaux( (data[data_counter] + (data[data_counter + 1] << 8)); // Each gpio codes is received as - // two bytes and is combined to + // two bytes and is combined to // create the parsed gpio voltage code aux_codes[current_ic] [current_gpio + @@ -732,7 +732,7 @@ int8_t LTC6804_rdaux( (data[data_counter] << 8) + data[data_counter + 1]; // The received PEC for the current_ic is - // transmitted as the 7th and 8th + // transmitted as the 7th and 8th // after the 6 gpio voltage data bytes data_pec = pec15_calc( BYT_IN_REG, @@ -740,7 +740,7 @@ int8_t LTC6804_rdaux( if (received_pec != data_pec) { pec_error = -1; // The pec_error variable is simply set negative if - // any PEC errors + // any PEC errors // are detected in the received serial data } else { pec_error = 0; @@ -749,7 +749,7 @@ int8_t LTC6804_rdaux( data_counter = data_counter + 2; // Because the transmitted PEC code is - // 2 bytes long the data_counter + // 2 bytes long the data_counter // must be incremented by 2 bytes to point to the next ICs gpio voltage // data } @@ -840,7 +840,7 @@ void LTC6804_rdaux_reg( // 3 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake, this command can be removed. + // awake, this command can be removed. // 4 HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); HAL_SPI_Transmit(config->spi, cmd, 4, HAL_MAX_DELAY); @@ -889,7 +889,7 @@ void LTC6804_clrcell(ltc_config *config) // 3 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake. This command can be removed. + // awake. This command can be removed. // 4 @@ -944,7 +944,7 @@ void LTC6804_clraux(ltc_config *config) // 3 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake.This command can be removed. + // awake.This command can be removed. // 4 HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); HAL_SPI_Transmit(config->spi, cmd, 4, HAL_MAX_DELAY); @@ -1034,7 +1034,7 @@ void LTC6804_wrcfg( cmd[cmd_index] = data_config[current_ic - 1] [current_byte]; // adding the config data - // to the array to be sent + // to the array to be sent cmd_index = cmd_index + 1; } // 3 @@ -1042,7 +1042,7 @@ void LTC6804_wrcfg( BYTES_IN_REG, &data_config[current_ic - 1] [0]); // calculating the PEC for each ICs - // configuration register data + // configuration register data cmd[cmd_index] = (uint8_t)(cfg_pec >> 8); cmd[cmd_index + 1] = (uint8_t)cfg_pec; cmd_index = cmd_index + 2; @@ -1051,7 +1051,7 @@ void LTC6804_wrcfg( // 4 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake.This command can be removed. + // awake.This command can be removed. // 5 HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); @@ -1134,7 +1134,7 @@ LTC6804_rdcfg(ltc_config *config, // 2 wakeup_idle( config); // This will guarantee that the LTC6804 isoSPI port is - // awake. This command can be removed. + // awake. This command can be removed. // 3 HAL_GPIO_WritePin(config->gpio, config->cs_pin, GPIO_PIN_RESET); HAL_SPI_Transmit(config->spi, cmd, 4, HAL_MAX_DELAY); @@ -1237,7 +1237,7 @@ pec15_calc(uint8_t len, // Number of bytes that will be used to calculate a PEC } return (remainder * 2); // The CRC15 has a 0 in the LSB so the remainder must - // be multiplied by 2 + // be multiplied by 2 } void write_68(ltc_config *config, @@ -1277,7 +1277,7 @@ void write_68(ltc_config *config, BYTES_IN_REG, &data[(current_ic - 1) * 6]); // Calculating the PEC for each ICs - // configuration register data + // configuration register data cmd[cmd_index] = (uint8_t)(data_pec >> 8); cmd[cmd_index + 1] = (uint8_t)data_pec; cmd_index = cmd_index + 2; diff --git a/platforms/stm32f405/include/can.h b/platforms/stm32f405/include/can.h index c318366..69fb2ea 100644 --- a/platforms/stm32f405/include/can.h +++ b/platforms/stm32f405/include/can.h @@ -14,15 +14,15 @@ */ typedef struct { - CAN_HandleTypeDef *hcan; - const uint32_t *id_list; - uint8_t id_list_len; + CAN_HandleTypeDef *hcan; + const uint32_t *id_list; + uint8_t id_list_len; } can_t; typedef struct { - uint32_t id; - uint8_t data[8]; - uint8_t len; + uint32_t id; + uint8_t data[8]; + uint8_t len; } can_msg_t; HAL_StatusTypeDef can_init(can_t *can); diff --git a/platforms/stm32f405/src/can.c b/platforms/stm32f405/src/can.c index b3f2024..cd82bb1 100644 --- a/platforms/stm32f405/src/can.c +++ b/platforms/stm32f405/src/can.c @@ -7,102 +7,106 @@ * implement in `stm32xxxx_it.c`, which STM32CubeMX generates */ -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); - if (err != HAL_OK) - return err; - err = HAL_CAN_Start(can->hcan); - if (err != HAL_OK) - return err; - - return err; +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); + if (err != HAL_OK) + return err; + err = HAL_CAN_Start(can->hcan); + if (err != HAL_OK) + return err; + + return err; } -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; - tx_header.IDE = CAN_ID_STD; - tx_header.RTR = CAN_RTR_DATA; - tx_header.DLC = msg->len; - tx_header.TransmitGlobalTime = DISABLE; +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; + tx_header.IDE = CAN_ID_STD; + tx_header.RTR = CAN_RTR_DATA; + tx_header.DLC = msg->len; + tx_header.TransmitGlobalTime = DISABLE; - uint32_t tx_mailbox; - if (HAL_CAN_GetTxMailboxesFreeLevel(can->hcan) == 0) - return HAL_BUSY; + uint32_t tx_mailbox; + if (HAL_CAN_GetTxMailboxesFreeLevel(can->hcan) == 0) + return HAL_BUSY; - if (HAL_CAN_AddTxMessage(can->hcan, &tx_header, msg->data, &tx_mailbox)) - return HAL_ERROR; + if (HAL_CAN_AddTxMessage(can->hcan, &tx_header, msg->data, &tx_mailbox)) + return HAL_ERROR; - return HAL_OK; + return HAL_OK; } -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; - tx_header.IDE = CAN_ID_EXT; - tx_header.RTR = CAN_RTR_DATA; - tx_header.DLC = msg->len; - tx_header.TransmitGlobalTime = DISABLE; +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; + tx_header.IDE = CAN_ID_EXT; + tx_header.RTR = CAN_RTR_DATA; + tx_header.DLC = msg->len; + tx_header.TransmitGlobalTime = DISABLE; - uint32_t tx_mailbox; - if (HAL_CAN_GetTxMailboxesFreeLevel(can->hcan) == 0) - return HAL_BUSY; + uint32_t tx_mailbox; + if (HAL_CAN_GetTxMailboxesFreeLevel(can->hcan) == 0) + return HAL_BUSY; - if (HAL_CAN_AddTxMessage(can->hcan, &tx_header, msg->data, &tx_mailbox)) - return HAL_ERROR; + if (HAL_CAN_AddTxMessage(can->hcan, &tx_header, msg->data, &tx_mailbox)) + return HAL_ERROR; - return HAL_OK; + return HAL_OK; } diff --git a/platforms/stm32g431/include/fdcan.h b/platforms/stm32g431/include/fdcan.h index 974451e..6d4a699 100644 --- a/platforms/stm32g431/include/fdcan.h +++ b/platforms/stm32g431/include/fdcan.h @@ -11,18 +11,18 @@ typedef void (*can_callback_t)(FDCAN_HandleTypeDef *hcan); typedef struct { - FDCAN_HandleTypeDef *hcan; - const uint16_t *id_list; - uint8_t id_list_len; + FDCAN_HandleTypeDef *hcan; + const uint16_t *id_list; + uint8_t id_list_len; - /* desired behavior varies by app - so implement this at app level */ - can_callback_t callback; + /* desired behavior varies by app - so implement this at app level */ + can_callback_t callback; } can_t; typedef struct { - uint32_t id; - uint8_t data[8]; - uint8_t len; + uint32_t id; + uint8_t data[8]; + uint8_t len; } can_msg_t; HAL_StatusTypeDef can_init(can_t *can); diff --git a/platforms/stm32g431/src/fdcan.c b/platforms/stm32g431/src/fdcan.c index 17c8a19..bbc936c 100644 --- a/platforms/stm32g431/src/fdcan.c +++ b/platforms/stm32g431/src/fdcan.c @@ -5,100 +5,105 @@ /* NOTE: STM32G431 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(FDCAN_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; +can_t *can_struct_list[MAX_CAN_BUS] = { NULL, NULL, NULL }; + +static can_callback_t find_callback(FDCAN_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; +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; } /* Run callback function when there a new message is received */ -void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hcan, uint32_t RxFifo0ITs) { - /* Handle CAN reception event */ - can_callback_t callback = find_callback(hcan); - - if (callback != NULL) { - callback(hcan); - } +void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hcan, uint32_t RxFifo0ITs) +{ + /* Handle CAN reception event */ + can_callback_t callback = find_callback(hcan); + + if (callback != NULL) { + callback(hcan); + } } -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; - - FDCAN_FilterTypeDef sFilterConfig; - - sFilterConfig.IdType = FDCAN_STANDARD_ID; - sFilterConfig.FilterIndex = 0; - sFilterConfig.FilterType = FDCAN_FILTER_RANGE; - sFilterConfig.FilterConfig = FDCAN_FILTER_DISABLE; - sFilterConfig.FilterID1 = 0x0; - sFilterConfig.FilterID2 = 0x7FF; - - uint8_t err = 0; - err = HAL_FDCAN_ConfigFilter(can->hcan, &sFilterConfig); - if (err != HAL_OK) - return err; - - /* set up interrupt & activate CAN */ - err = HAL_FDCAN_Start(can->hcan); - if (err != HAL_OK) - return err; - - /* Override the default callback for FDCAN_IT_LIST_RX_FIFO0 */ - // err = HAL_FDCAN_ActivateNotification(can->hcan, FDCAN_IT_LIST_RX_FIFO0); - err = add_interface(can); - - return err; +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; + + FDCAN_FilterTypeDef sFilterConfig; + + sFilterConfig.IdType = FDCAN_STANDARD_ID; + sFilterConfig.FilterIndex = 0; + sFilterConfig.FilterType = FDCAN_FILTER_RANGE; + sFilterConfig.FilterConfig = FDCAN_FILTER_DISABLE; + sFilterConfig.FilterID1 = 0x0; + sFilterConfig.FilterID2 = 0x7FF; + + uint8_t err = 0; + err = HAL_FDCAN_ConfigFilter(can->hcan, &sFilterConfig); + if (err != HAL_OK) + return err; + + /* set up interrupt & activate CAN */ + err = HAL_FDCAN_Start(can->hcan); + if (err != HAL_OK) + return err; + + /* Override the default callback for FDCAN_IT_LIST_RX_FIFO0 */ + // err = HAL_FDCAN_ActivateNotification(can->hcan, FDCAN_IT_LIST_RX_FIFO0); + err = add_interface(can); + + return err; } -HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg) { - FDCAN_TxHeaderTypeDef tx_header; - tx_header.Identifier = msg->id; - tx_header.IdType = FDCAN_STANDARD_ID; - tx_header.TxFrameType = FDCAN_DATA_FRAME; - tx_header.DataLength = msg->len; - tx_header.ErrorStateIndicator = FDCAN_ESI_ACTIVE; - tx_header.BitRateSwitch = FDCAN_BRS_OFF; - tx_header.FDFormat = FDCAN_CLASSIC_CAN; - - uint32_t tx_mailbox; - if (HAL_FDCAN_GetTxFifoFreeLevel(can->hcan) == 0) - return HAL_BUSY; - - if (HAL_FDCAN_AddMessageToTxFifoQ(can->hcan, &tx_header, &msg->data)) - return HAL_ERROR; - - return HAL_OK; +HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg) +{ + FDCAN_TxHeaderTypeDef tx_header; + tx_header.Identifier = msg->id; + tx_header.IdType = FDCAN_STANDARD_ID; + tx_header.TxFrameType = FDCAN_DATA_FRAME; + tx_header.DataLength = msg->len; + tx_header.ErrorStateIndicator = FDCAN_ESI_ACTIVE; + tx_header.BitRateSwitch = FDCAN_BRS_OFF; + tx_header.FDFormat = FDCAN_CLASSIC_CAN; + + uint32_t tx_mailbox; + if (HAL_FDCAN_GetTxFifoFreeLevel(can->hcan) == 0) + return HAL_BUSY; + + if (HAL_FDCAN_AddMessageToTxFifoQ(can->hcan, &tx_header, &msg->data)) + return HAL_ERROR; + + return HAL_OK; } \ No newline at end of file