candev_stm32.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 OTA keys S.A.
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include "can/candev.h"
35 
37 #if defined(CAN3)
38 #define CANDEV_STM32_CHAN_NUMOF 3
39 #elif defined(CAN2)
40 #define CANDEV_STM32_CHAN_NUMOF 2
41 #elif defined(CAN1) || defined(CAN) || DOXYGEN
42 #define CANDEV_STM32_CHAN_NUMOF 1
43 #else
44 #error "CAN STM32: CPU not supported"
45 #endif
46 
51 #if defined(CPU_FAM_STM32F1)
52 #define ISR_CAN1_TX isr_usb_hp_can1_tx
53 #define ISR_CAN1_RX0 isr_usb_lp_can1_rx0
54 #define ISR_CAN1_RX1 isr_can1_rx1
55 #define ISR_CAN1_SCE isr_can1_sce
56 #else
57 #define ISR_CAN1_TX isr_can1_tx
58 #define ISR_CAN1_RX0 isr_can1_rx0
59 #define ISR_CAN1_RX1 isr_can1_rx1
60 #define ISR_CAN1_SCE isr_can1_sce
61 #define ISR_CAN2_TX isr_can2_tx
62 #define ISR_CAN2_RX0 isr_can2_rx0
63 #define ISR_CAN2_RX1 isr_can2_rx1
64 #define ISR_CAN2_SCE isr_can2_sce
65 #define ISR_CAN3_TX isr_can3_tx
66 #define ISR_CAN3_RX0 isr_can3_rx0
67 #define ISR_CAN3_RX1 isr_can3_rx1
68 #define ISR_CAN3_SCE isr_can3_sce
69 #endif
72 #if CANDEV_STM32_CHAN_NUMOF > 1 || DOXYGEN
74 #define CAN_STM32_NB_FILTER 28
75 #else
76 #define CAN_STM32_NB_FILTER 14
77 #endif
78 
79 #ifndef CANDEV_STM32_DEFAULT_BITRATE
81 #define CANDEV_STM32_DEFAULT_BITRATE 500000U
82 #endif
83 
84 #ifndef CANDEV_STM32_DEFAULT_SPT
86 #define CANDEV_STM32_DEFAULT_SPT 875
87 #endif
88 
90 typedef struct {
91  CAN_TypeDef *can;
92  uint32_t rcc_mask;
93  gpio_t rx_pin;
94  gpio_t tx_pin;
95 #ifndef CPU_FAM_STM32F1
97 #endif
99 #if CANDEV_STM32_CHAN_NUMOF > 1 || defined(DOXYGEN)
100  CAN_TypeDef *can_master;
101  uint32_t master_rcc_mask;
105  uint8_t first_filter;
108  uint8_t nb_filters;
109 #endif
110 #if defined(CPU_FAM_STM32F0)
111  uint8_t irqn;
112 #else
113  uint8_t tx_irqn;
114  uint8_t rx0_irqn;
115  uint8_t rx1_irqn;
116  uint8_t sce_irqn;
117 #endif
118  uint8_t ttcm : 1;
119  uint8_t abom : 1;
120  uint8_t awum : 1;
121  uint8_t nart : 1;
122  uint8_t rflm : 1;
123  uint8_t txfp : 1;
124  uint8_t lbkm : 1;
125  uint8_t silm : 1;
126 } can_conf_t;
128 #define HAVE_CAN_CONF_T
129 
131 #define CAN_STM32_TX_MAILBOXES 3
133 #define CAN_STM32_RX_MAILBOXES 2
134 
135 #ifndef CAN_STM32_RX_MAIL_FIFO
137 #define CAN_STM32_RX_MAIL_FIFO 12
138 #endif
139 
141 typedef struct can can_t;
143 #define HAVE_CAN_T
144 
146 typedef struct candev_stm32_rx_fifo {
148  int write_idx;
149  int read_idx;
150  int is_full;
152 
154 typedef struct candev_stm32_isr {
155  int isr_tx : 3;
156  int isr_rx : 2;
157  int isr_wkup : 1;
159 
161 struct can {
162  candev_t candev;
163  const can_conf_t *conf;
164  gpio_t rx_pin;
165  gpio_t tx_pin;
171 };
172 
173 #ifndef CPU_FAM_STM32F1
182 void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin,
183  gpio_af_t af);
184 #else
192 void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin);
193 #endif
194 
195 #ifdef __cplusplus
196 }
197 #endif
Definitions for low-level CAN driver interface.
#define CAN_STM32_TX_MAILBOXES
The number of transmit mailboxes.
Definition: candev_stm32.h:131
#define CAN_STM32_RX_MAIL_FIFO
This is the maximum number of frame the driver can receive simultaneously.
Definition: candev_stm32.h:137
void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin, gpio_af_t af)
Set the pins of an stm32 CAN device.
struct candev_stm32_rx_fifo candev_stm32_rx_fifo_t
This structure holds anything related to the receive part.
struct candev_stm32_isr candev_stm32_isr_t
Internal interrupt flags.
gpio_af_t
Override alternative GPIO mode options.
Definition: periph_cpu.h:162
struct candev_conf can_conf_t
Linux candev configuration.
ESP CAN device configuration.
Definition: can_esp.h:84
CAN_TypeDef * can
CAN device.
Definition: candev_stm32.h:91
uint8_t nb_filters
Number of filters to use.
Definition: candev_stm32.h:108
uint8_t nart
No automatic retransmission.
Definition: candev_stm32.h:121
uint8_t first_filter
First filter in the bank.
Definition: candev_stm32.h:105
uint8_t sce_irqn
SCE IRQ channel.
Definition: candev_stm32.h:116
uint8_t txfp
Transmit FIFO priority.
Definition: candev_stm32.h:123
uint8_t rx0_irqn
RX0 IRQ channel.
Definition: candev_stm32.h:114
uint32_t rcc_mask
RCC mask to enable clock.
Definition: candev_stm32.h:92
uint8_t rx1_irqn
RX1 IRQ channel.
Definition: candev_stm32.h:115
uint8_t abom
Automatic bus-off management.
Definition: candev_stm32.h:119
uint8_t tx_irqn
TX IRQ channel.
Definition: candev_stm32.h:113
uint8_t awum
Automatic wakeup mode.
Definition: candev_stm32.h:120
CAN_TypeDef * can_master
Master CAN device.
Definition: candev_stm32.h:100
gpio_af_t af
Alternate pin function to use.
Definition: candev_stm32.h:96
bool en_deep_sleep_wake_up
Enable deep-sleep wake-up interrupt.
Definition: candev_stm32.h:98
uint32_t master_rcc_mask
Master device RCC mask.
Definition: candev_stm32.h:101
uint8_t rflm
Receive FIFO locked mode.
Definition: candev_stm32.h:122
uint8_t ttcm
Time triggered communication mode.
Definition: candev_stm32.h:118
uint8_t lbkm
Loopback mode.
Definition: candev_stm32.h:124
Controller Area Network frame.
Definition: can.h:100
Low level device structure for ESP32 CAN (extension of candev_t)
Definition: can_esp.h:60
const struct can_frame * tx_mailbox[CAN_STM32_TX_MAILBOXES]
Tx mailboxes.
Definition: candev_stm32.h:168
candev_stm32_rx_fifo_t rx_fifo
Rx FIFOs.
Definition: candev_stm32.h:169
candev_stm32_isr_t isr_flags
ISR flags.
Definition: candev_stm32.h:170
gpio_t rx_pin
RX pin.
Definition: candev_stm32.h:164
candev_t candev
candev base structure
Definition: can_esp.h:61
gpio_t tx_pin
TX pin.
Definition: candev_stm32.h:165
const can_conf_t * conf
Configuration.
Definition: candev_stm32.h:163
gpio_af_t af
Alternate pin function to use.
Definition: candev_stm32.h:166
Internal interrupt flags.
Definition: candev_stm32.h:154
int isr_rx
Rx FIFO interrupt.
Definition: candev_stm32.h:156
int isr_tx
Tx mailboxes interrupt.
Definition: candev_stm32.h:155
int isr_wkup
Wake up interrupt.
Definition: candev_stm32.h:157
This structure holds anything related to the receive part.
Definition: candev_stm32.h:146
int write_idx
Write index in the receive FIFO.
Definition: candev_stm32.h:148
int read_idx
Read index in the receive FIFO.
Definition: candev_stm32.h:149
struct can_frame frame[CAN_STM32_RX_MAIL_FIFO]
Receive FIFO.
Definition: candev_stm32.h:147
int is_full
Flag set when the FIFO is full.
Definition: candev_stm32.h:150
Structure to hold driver state.
Definition: candev.h:76