Compare commits

..

No commits in common. "2ab77d75d62298e39b9ee5d29ad065d69d2d4406" and "887f92167d58e551abac18f4f899c74bddc13d46" have entirely different histories.

3 changed files with 21 additions and 54 deletions

View File

@ -24,8 +24,6 @@ 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.:
@ -35,7 +33,6 @@ 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,51 +100,48 @@ HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle) {
return HAL_FDCAN_Start(hcan);
}
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;
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;
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 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);
return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data);
}
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
@ -220,26 +217,6 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle,
ftcan_msg_received_cb(header.Identifier, datalen, data);
}
HAL_StatusTypeDef ftcan_get_tx_queue_length(uint32_t *length) {
if (length == NULL) {
return HAL_ERROR;
}
*length = HAL_FDCAN_GetTxFifoFreeLevel(hcan);
return HAL_OK;
}
#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,
@ -294,4 +271,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,13 +41,6 @@ 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);