Compare commits

...

4 Commits

Author SHA1 Message Date
aa0a1d0d05
expose FIFO pressure level in header 2025-06-19 03:34:10 +02:00
2ab77d75d6
fix buffer index calculation
they take a bit field as an argument, and then immediately use a clz instruction to convert it back to an index for some reason
2025-06-18 16:18:45 +02:00
1b79e5cbea
add include guards, update readme 2025-06-18 16:12:46 +02:00
7de8bbdc3b
add queue length and buffer transmit functions 2025-06-18 15:51:10 +02:00
3 changed files with 52 additions and 21 deletions

View File

@ -24,6 +24,8 @@ When using the FDCAN peripheral (H7 series), you also need to define
`FTCAN_NUM_FILTERS` (and set it to the value of "Std Filters Nbr" you configured
in your `.ioc`).
If you want to use the dedicated buffers of the FDCAN, you must additionally define `FTCAN_NUM_TX_BUFFERS` (set to value of "Tx Buffers Nbr" in CubeMX).
If you use
[VSCode with the stm-32-for-vscode extension](https://podio.com/fasttubede/modulubergreifend/apps/tech-tutorials/items/57),
you can add these definitions in the `STM32-for-VSCode-config.yaml` file, e.g.:
@ -33,6 +35,7 @@ you can add these definitions in the `STM32-for-VSCode-config.yaml` file, e.g.:
cDefinitions:
- STM32H7
- FTCAN_NUM_FILTERS=32
- FTCAN_NUM_TX_BUFFERS=10 # optional, only if using dedicated tx buffers
```
## Usage

View File

@ -100,48 +100,51 @@ HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle) {
return HAL_FDCAN_Start(hcan);
}
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
size_t datalen) {
static FDCAN_TxHeaderTypeDef header;
header.Identifier = id;
header.IdType = FDCAN_STANDARD_ID;
header.TxFrameType = FDCAN_DATA_FRAME;
static FDCAN_TxHeaderTypeDef * fdcan_initialize_tx_header(FDCAN_TxHeaderTypeDef * header, uint16_t id, size_t datalen) {
header->Identifier = id;
header->IdType = FDCAN_STANDARD_ID;
header->TxFrameType = FDCAN_DATA_FRAME;
switch (datalen) {
case 0:
header.DataLength = FDCAN_DLC_BYTES_0;
header->DataLength = FDCAN_DLC_BYTES_0;
break;
case 1:
header.DataLength = FDCAN_DLC_BYTES_1;
header->DataLength = FDCAN_DLC_BYTES_1;
break;
case 2:
header.DataLength = FDCAN_DLC_BYTES_2;
header->DataLength = FDCAN_DLC_BYTES_2;
break;
case 3:
header.DataLength = FDCAN_DLC_BYTES_3;
header->DataLength = FDCAN_DLC_BYTES_3;
break;
case 4:
header.DataLength = FDCAN_DLC_BYTES_4;
header->DataLength = FDCAN_DLC_BYTES_4;
break;
case 5:
header.DataLength = FDCAN_DLC_BYTES_5;
header->DataLength = FDCAN_DLC_BYTES_5;
break;
case 6:
header.DataLength = FDCAN_DLC_BYTES_6;
header->DataLength = FDCAN_DLC_BYTES_6;
break;
case 7:
header.DataLength = FDCAN_DLC_BYTES_7;
header->DataLength = FDCAN_DLC_BYTES_7;
break;
case 8:
default:
header.DataLength = FDCAN_DLC_BYTES_8;
header->DataLength = FDCAN_DLC_BYTES_8;
break;
}
header.ErrorStateIndicator = FDCAN_ESI_PASSIVE;
header.BitRateSwitch = FDCAN_BRS_OFF;
header.FDFormat = FDCAN_CLASSIC_CAN;
header.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
header->ErrorStateIndicator = FDCAN_ESI_PASSIVE;
header->BitRateSwitch = FDCAN_BRS_OFF;
header->FDFormat = FDCAN_CLASSIC_CAN;
header->TxEventFifoControl = FDCAN_NO_TX_EVENTS;
return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data);
return header;
}
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data, size_t datalen) {
static FDCAN_TxHeaderTypeDef header;
return HAL_FDCAN_AddMessageToTxFifoQ(hcan, fdcan_initialize_tx_header(&header, id, datalen), data);
}
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
@ -217,6 +220,22 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle,
ftcan_msg_received_cb(header.Identifier, datalen, data);
}
uint32_t ftcan_get_tx_queue_length() {
return HAL_FDCAN_GetTxFifoFreeLevel(hcan);
}
#if FTCAN_NUM_TX_BUFFERS
static_assert(FDCAN_TX_BUFFER11 == 1 << 11 && FDCAN_TX_BUFFER21 == 1 << 21, "Your HAL version has a different definition for the transmit buffer indices. Please update your HAL version or change the code below.");
HAL_StatusTypeDef ftcan_transmit_buffer(uint16_t id, const uint8_t *data, size_t datalen, uint32_t buffer_index) {
static FDCAN_TxHeaderTypeDef header;
if (buffer_index >= FTCAN_NUM_TX_BUFFERS) {
return HAL_ERROR;
}
return HAL_FDCAN_AddMessageToTxBuffer(hcan, fdcan_initialize_tx_header(&header, id, datalen), data, 1 << buffer_index);
}
#endif // FTCAN_NUM_TX_BUFFERS
#endif
__weak void ftcan_msg_received_cb(uint16_t id, size_t datalen,
@ -271,4 +290,4 @@ uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes) {
uint8_t *ftcan_marshal_signed(uint8_t *data, int64_t val, size_t num_bytes) {
return ftcan_marshal_unsigned(data, val, num_bytes);
}
}

View File

@ -41,11 +41,20 @@ HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle);
#error "Unknown CAN peripheral"
#endif
#if defined(FTCAN_IS_FDCAN) && defined(FTCAN_NUM_TX_BUFFERS)
static_assert(FTCAN_NUM_TX_BUFFERS > 0,
"If you want to use the dedicated transmit buffers, you must define FTCAN_NUM_TX_BUFFERS as configured in CubeMX (at least 1).");
HAL_StatusTypeDef ftcan_transmit_buffer(uint16_t id, const uint8_t *data, size_t datalen, uint32_t buffer_index);
#endif // FTCAN_IS_FDCAN && FTCAN_NUM_TX_BUFFERS
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
size_t datalen);
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask);
uint32_t ftcan_get_tx_queue_length();
/**
* Define this function to be notified of incoming CAN messages
*/