Skip to content

Commit d266ee5

Browse files
drivers: can: Implement stm32fd driver
This driver is the SoC specific implementation of the Bosch M_CAN IP. Signed-off-by: Alexander Wachter <[email protected]>
1 parent a1a16b0 commit d266ee5

File tree

6 files changed

+357
-34
lines changed

6 files changed

+357
-34
lines changed

drivers/can/Kconfig.stm32fd

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# STM32 CAN configuration options
2-
if CAN_MCAN && SOC_SERIES_STM32G4X
2+
3+
# Copyright (c) 2020 Alexander Wachter
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
DT_COMPAT_STM32_FDCAN := st,stm32-fdcan
37

48
config CAN_STM32FD
5-
bool
6-
default y
9+
bool "STM32 FDCAN driver"
10+
default $(dt_compat_enabled,$(DT_COMPAT_STM32_FDCAN))
11+
select CAN_MCAN
12+
select USE_STM32_LL_RCC
13+
14+
if CAN_STM32FD
715

816
config CAN_MAX_STD_ID_FILTER
917
int "Maximum number of std ID filters"
@@ -21,15 +29,14 @@ config CAN_MAX_EXT_ID_FILTER
2129
Defines the maximum number of filters with extended ID (29-bit)
2230
that can be attached.
2331

24-
config CAN_CKDIV
25-
int "CKDIV register value"
26-
range 0 15
27-
default 0
32+
config CAN_STM32_CLOCK_DIVISOR
33+
int "CAN clock divisor"
34+
range 1 30
35+
default 1
2836
help
29-
This value is written to the CKDIV register.
30-
The APB clock is divided according to this value before it is feed to
31-
CAN core. Note that the the divider affects all CAN controllers.
32-
The values of the register are multiplied by two and zero corresponds
33-
to one. The value six, for example results in a clock divided by 12.
37+
The APB clock is divided by this value (stored in CKDIV register)
38+
before it is fed to the CAN core.
39+
Note that the the divisor affects all CAN controllers.
40+
Allowed values: 1 or 2 * n, where n <= 15.
3441

35-
endif #CAN_MCAN
42+
endif #CAN_STM32FD

drivers/can/can_mcan.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ void can_mcan_configure_timing(struct can_mcan_reg *can,
8686
timing->phase_seg2 > 0);
8787
__ASSERT_NO_MSG(timing->prescaler <= 0x200 &&
8888
timing->prescaler > 0);
89+
__ASSERT_NO_MSG(timing->sjw <= 0x80 && timing->sjw > 0);
90+
8991
can->nbtp = (((uint32_t)timing->phase_seg1 - 1UL) & 0xFF) <<
9092
CAN_MCAN_NBTP_NTSEG1_POS |
9193
(((uint32_t)timing->phase_seg2 - 1UL) & 0x7F) <<
@@ -105,6 +107,8 @@ void can_mcan_configure_timing(struct can_mcan_reg *can,
105107
timing_data->phase_seg2 > 0);
106108
__ASSERT_NO_MSG(timing_data->prescaler <= 20 &&
107109
timing_data->prescaler > 0);
110+
__ASSERT_NO_MSG(timing_data->sjw <= 0x80 &&
111+
timing_data->sjw > 0);
108112

109113
can->dbtp = (((uint32_t)timing_data->phase_seg1 - 1UL) & 0x1F) <<
110114
CAN_MCAN_DBTP_DTSEG1_POS |
@@ -247,12 +251,12 @@ int can_mcan_init(const struct device *dev, const struct can_mcan_config *cfg,
247251
can->txefc = ((uint32_t)msg_ram->tx_event_fifo & CAN_MCAN_TXEFC_EFSA_MSK) |
248252
(ARRAY_SIZE(msg_ram->tx_event_fifo) <<
249253
CAN_MCAN_TXEFC_EFS_POS);
250-
can->txbc = ((uint32_t)msg_ram->tx_fifo & CAN_MCAN_TXBC_TBSA) |
251-
(ARRAY_SIZE(msg_ram->tx_fifo) << CAN_MCAN_TXBC_TFQS_POS);
252-
if (sizeof(msg_ram->tx_fifo[0].data) <= 24) {
253-
can->txesc = (sizeof(msg_ram->tx_fifo[0].data) - 8) / 4;
254+
can->txbc = ((uint32_t)msg_ram->tx_buffer & CAN_MCAN_TXBC_TBSA) |
255+
(ARRAY_SIZE(msg_ram->tx_buffer) << CAN_MCAN_TXBC_TFQS_POS);
256+
if (sizeof(msg_ram->tx_buffer[0].data) <= 24) {
257+
can->txesc = (sizeof(msg_ram->tx_buffer[0].data) - 8) / 4;
254258
} else {
255-
can->txesc = (sizeof(msg_ram->tx_fifo[0].data) - 32) / 16 + 5;
259+
can->txesc = (sizeof(msg_ram->tx_buffer[0].data) - 32) / 16 + 5;
256260
}
257261

258262
if (sizeof(msg_ram->rx_fifo0[0].data) <= 24) {
@@ -612,7 +616,7 @@ int can_mcan_send(const struct can_mcan_config *cfg,
612616
{
613617
struct can_mcan_reg *can = cfg->can;
614618
size_t data_length = can_dlc_to_bytes(frame->dlc);
615-
struct can_mcan_tx_fifo_hdr tx_hdr = {
619+
struct can_mcan_tx_buffer_hdr tx_hdr = {
616620
.rtr = frame->rtr == CAN_REMOTEREQUEST,
617621
.xtd = frame->id_type == CAN_EXTENDED_IDENTIFIER,
618622
.esi = 0,
@@ -675,10 +679,10 @@ int can_mcan_send(const struct can_mcan_config *cfg,
675679
tx_hdr.ext_id = frame->id;
676680
}
677681

678-
msg_ram->tx_fifo[put_idx].hdr = tx_hdr;
682+
msg_ram->tx_buffer[put_idx].hdr = tx_hdr;
679683

680684
for (src = frame->data_32,
681-
dst = msg_ram->tx_fifo[put_idx].data_32,
685+
dst = msg_ram->tx_buffer[put_idx].data_32,
682686
end = dst + CAN_DIV_CEIL(data_length, sizeof(uint32_t));
683687
dst < end;
684688
src++, dst++) {

drivers/can/can_mcan.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct can_mcan_rx_fifo_hdr {
3030
union {
3131
struct {
3232
volatile uint32_t ext_id : 29; /* Extended Identifier */
33-
volatile uint32_t rtr : 1; /* Retmote Transmission Request*/
33+
volatile uint32_t rtr : 1; /* Remote Transmission Request*/
3434
volatile uint32_t xtd : 1; /* Extended identifier */
3535
volatile uint32_t esi : 1; /* Error state indicator */
3636
};
@@ -63,7 +63,7 @@ struct can_mcan_mm {
6363
volatile uint8_t cnt : 3;
6464
} __packed;
6565

66-
struct can_mcan_tx_fifo_hdr {
66+
struct can_mcan_tx_buffer_hdr {
6767
union {
6868
struct {
6969
volatile uint32_t ext_id : 29; /* Identifier */
@@ -86,8 +86,8 @@ struct can_mcan_tx_fifo_hdr {
8686
struct can_mcan_mm mm; /* Message marker */
8787
} __packed;
8888

89-
struct can_mcan_tx_fifo {
90-
struct can_mcan_tx_fifo_hdr hdr;
89+
struct can_mcan_tx_buffer {
90+
struct can_mcan_tx_buffer_hdr hdr;
9191
union {
9292
volatile uint8_t data[64];
9393
volatile uint32_t data_32[16];
@@ -152,7 +152,7 @@ struct can_mcan_msg_sram {
152152
volatile struct can_mcan_rx_fifo rx_fifo1[NUM_RX_FIFO1_ELEMENTS];
153153
volatile struct can_mcan_rx_fifo rx_buffer[NUM_RX_BUF_ELEMENTS];
154154
volatile struct can_mcan_tx_event_fifo tx_event_fifo[NUM_TX_BUF_ELEMENTS];
155-
volatile struct can_mcan_tx_fifo tx_fifo[NUM_TX_BUF_ELEMENTS];
155+
volatile struct can_mcan_tx_buffer tx_buffer[NUM_TX_BUF_ELEMENTS];
156156
} __packed;
157157

158158
struct can_mcan_data {

0 commit comments

Comments
 (0)