13 Commits

Author SHA1 Message Date
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
887f92167d cleanup const cast
HAL_FDCAN_AddMessageToTxFifoQ definition is now correctly marked const in the HAL
2025-04-23 17:22:34 +02:00
4e3bb026f8 silence unused parameter warnings on weak fallback ftcan_msg_received_cb 2025-04-11 15:15:36 +02:00
0b91166780 change readme 2025-02-27 17:37:31 +01:00
5c90c7cda4 Recognize H723 2024-11-25 20:54:16 +01:00
6f6cbf1d1e add STM32F042x6 2024-11-02 17:31:54 +01:00
80068a98fa Describe CubeMX settings 2024-06-03 15:35:02 +02:00
de2c25828b Describe how to set compiler definitions in VSCode 2024-05-27 17:08:22 +02:00
8cca72d90d add more informative error message for FDCAN peripheral config 2024-04-25 18:47:17 +02:00
433a142732 Fix marshalling loop 2023-04-02 01:01:16 +02:00
f151e3a4fe Recognize most used FaSTTUBe chips
Closes #2
2023-04-01 18:06:34 +02:00
10 changed files with 133 additions and 33 deletions

View File

@ -18,11 +18,26 @@ is included:
- `STM32F3`
- `STM32H7`
- `STM32F0`
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.:
```yaml
# Compiler definitions. The -D prefix for the compiler will be automatically added.
cDefinitions:
- STM32H7
- FTCAN_NUM_FILTERS=32
- FTCAN_NUM_TX_BUFFERS=10 # optional, only if using dedicated tx buffers
```
## Usage
1. Include `can-halal.h`
@ -31,3 +46,36 @@ in your `.ioc`).
4. To transmit messages, call `ftcan_transmit()`
5. When a message is received, `ftcan_msg_received_cb()` is called. It has a
default empty implementation, which you can simply override.
## Enabling CAN in STM32CubeMX
This isn't specific to `can-halal`, but for completeness sake is included here.
### bxCAN (e.g. STM32F3xx)
1. Enable the CAN peripheral
![Connectivity -> CAN -> Activated](doc/bxcan-activate.png)
2. Setup the [bit timings](http://bittiming.can-wiki.info/).
**Note:** the baud rate depends on your system clock, so make sure that is
setup correctly first!
![Connectivity -> CAN -> Parameter Settings -> Bit Timings Parameters](doc/bxcan-bittimings.png)
3. Make sure the CAN_RX0 interrupt is enabled
![Connectivity -> CAN -> NVIC Settings -> CAN_RX0 interrupt](doc/bxcan-interrupt.png)
### FDCAN (e.g. STM32H7xx)
1. Enable the CAN peripheral
![Connectivity -> FDCAN(1)](doc/fdcan-activate.png)
2. Setup the frame format, nominal SJW, filters, and FIFOs. The numbers for
filters/FIFOs in the screenshot are examples.
**Note:** You need to tell `can-halal` about the number of filters by
defining `FTCAN_NUM_FILTERS` (see above).
![Connectivity -> FDCAN(1) -> Parameter Settings -> Basic Parameters](doc/fdcan-basic.png)
3. Setup the [bit timings](http://bittiming.can-wiki.info/). We only use CAN in
classic mode, not FD mode, so we only need to worry about the nominal bit
timings
**Note:** the baud rate depends on your system clock, so make sure that is
setup correctly first!
![Connectivity -> FDCAN(1) -> Parameter Settings -> Bit Timings Parameters](doc/fdcan-bittimings.png)
4. Make sure the interrupts are enabled
![Connectivity -> FDCAN(1) -> NVIC Settings](doc/fdcan-interrupt.png)

View File

@ -100,51 +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;
// HAL_FDCAN_AddMessageToTxFifoQ doesn't modify the data, but it's not marked
// as const for some reason.
uint8_t *data_nonconst = (uint8_t *)data;
return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data_nonconst);
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) {
@ -220,10 +220,34 @@ 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,
const uint8_t *data) {}
const uint8_t *data) {
(void)id;
(void)datalen;
(void)data;
}
uint64_t ftcan_unmarshal_unsigned(const uint8_t **data_ptr, size_t num_bytes) {
if (num_bytes > 8) {
@ -260,7 +284,7 @@ uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes) {
num_bytes = 8;
}
for (size_t i = num_bytes - 1; i >= 0; i++) {
for (int i = num_bytes - 1; i >= 0; i--) {
data[i] = val & 0xFF;
val >>= 8;
}
@ -270,4 +294,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

@ -1,15 +1,36 @@
#ifndef CAN_HALAL_H
#define CAN_HALAL_H
#if defined(STM32F302x8) || defined(STM32F302xB) || defined(STM32F302xC) || defined(STM32F3)
#include "stm32f3xx_hal.h"
#define FTCAN_IS_BXCAN
#define FTCAN_NUM_FILTERS 13
// Define family macros if none are defined and we recognize a chip macro
#if !defined(STM32F3) && !defined(STM32H7) && !defined(STM32F0)
#if defined(STM32F302x6) || defined(STM32F302x8) || defined(STM32F302xB) || \
defined(STM32F302xC)
#define STM32F3
#endif
#if defined(STM32H7A3xx) || defined(STM32H723xx)
#define STM32H7
#endif
#if defined(STM32F042x6)
#define STM32F0
#endif
#endif
#if defined(STM32F3)
#include "stm32f3xx_hal.h"
#define FTCAN_IS_BXCAN
#define FTCAN_NUM_FILTERS 13
#elif defined(STM32H7)
#include "stm32h7xx_hal.h"
#define FTCAN_IS_FDCAN
#include "stm32h7xx_hal.h"
#define FTCAN_IS_FDCAN
#ifndef FTCAN_NUM_FILTERS
#error "Please configure the number of filters in CubeMX, and then add a compiler define for FTCAN_NUM_FILTERS"
#endif
#elif defined(STM32F0)
#include "stm32f0xx_hal.h"
#define FTCAN_IS_BXCAN
#define FTCAN_NUM_FILTERS 13
#else
#error "Couldn't detect STM family"
#error "Couldn't detect STM family"
#endif
#if defined(FTCAN_IS_BXCAN)
@ -20,6 +41,13 @@ 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);

BIN
doc/bxcan-activate.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
doc/bxcan-bittimings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
doc/bxcan-interrupt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
doc/fdcan-activate.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
doc/fdcan-basic.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
doc/fdcan-bittimings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
doc/fdcan-interrupt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB