candev_stm32.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 OTA keys S.A.
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
31 #ifndef CANDEV_STM32_H
32 #define CANDEV_STM32_H
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #include "can/candev.h"
39 
41 #if defined(CAN3)
42 #define CANDEV_STM32_CHAN_NUMOF 3
43 #elif defined(CAN2)
44 #define CANDEV_STM32_CHAN_NUMOF 2
45 #elif defined(CAN1) || defined(CAN) || DOXYGEN
46 #define CANDEV_STM32_CHAN_NUMOF 1
47 #else
48 #error "CAN STM32: CPU not supported"
49 #endif
50 
55 #if defined(CPU_FAM_STM32F1)
56 #define ISR_CAN1_TX isr_usb_hp_can1_tx
57 #define ISR_CAN1_RX0 isr_usb_lp_can1_rx0
58 #define ISR_CAN1_RX1 isr_can1_rx1
59 #define ISR_CAN1_SCE isr_can1_sce
60 #else
61 #define ISR_CAN1_TX isr_can1_tx
62 #define ISR_CAN1_RX0 isr_can1_rx0
63 #define ISR_CAN1_RX1 isr_can1_rx1
64 #define ISR_CAN1_SCE isr_can1_sce
65 #define ISR_CAN2_TX isr_can2_tx
66 #define ISR_CAN2_RX0 isr_can2_rx0
67 #define ISR_CAN2_RX1 isr_can2_rx1
68 #define ISR_CAN2_SCE isr_can2_sce
69 #define ISR_CAN3_TX isr_can3_tx
70 #define ISR_CAN3_RX0 isr_can3_rx0
71 #define ISR_CAN3_RX1 isr_can3_rx1
72 #define ISR_CAN3_SCE isr_can3_sce
73 #endif
76 #if CANDEV_STM32_CHAN_NUMOF > 1 || DOXYGEN
78 #define CAN_STM32_NB_FILTER 28
79 #else
80 #define CAN_STM32_NB_FILTER 14
81 #endif
82 
83 #ifndef CANDEV_STM32_DEFAULT_BITRATE
85 #define CANDEV_STM32_DEFAULT_BITRATE 500000U
86 #endif
87 
88 #ifndef CANDEV_STM32_DEFAULT_SPT
90 #define CANDEV_STM32_DEFAULT_SPT 875
91 #endif
92 
94 typedef struct {
95  CAN_TypeDef *can;
96  uint32_t rcc_mask;
97  gpio_t rx_pin;
98  gpio_t tx_pin;
99 #ifndef CPU_FAM_STM32F1
101 #endif
103 #if CANDEV_STM32_CHAN_NUMOF > 1 || defined(DOXYGEN)
104  CAN_TypeDef *can_master;
105  uint32_t master_rcc_mask;
109  uint8_t first_filter;
112  uint8_t nb_filters;
113 #endif
114 #if defined(CPU_FAM_STM32F0)
115  uint8_t irqn;
116 #else
117  uint8_t tx_irqn;
118  uint8_t rx0_irqn;
119  uint8_t rx1_irqn;
120  uint8_t sce_irqn;
121 #endif
122  uint8_t ttcm : 1;
123  uint8_t abom : 1;
124  uint8_t awum : 1;
125  uint8_t nart : 1;
126  uint8_t rflm : 1;
127  uint8_t txfp : 1;
128  uint8_t lbkm : 1;
129  uint8_t silm : 1;
130 } can_conf_t;
132 #define HAVE_CAN_CONF_T
133 
135 #define CAN_STM32_TX_MAILBOXES 3
137 #define CAN_STM32_RX_MAILBOXES 2
138 
139 #ifndef CAN_STM32_RX_MAIL_FIFO
141 #define CAN_STM32_RX_MAIL_FIFO 12
142 #endif
143 
145 typedef struct can can_t;
147 #define HAVE_CAN_T
148 
150 typedef struct candev_stm32_rx_fifo {
152  int write_idx;
153  int read_idx;
154  int is_full;
156 
158 typedef struct candev_stm32_isr {
159  int isr_tx : 3;
160  int isr_rx : 2;
161  int isr_wkup : 1;
163 
165 struct can {
166  candev_t candev;
167  const can_conf_t *conf;
168  gpio_t rx_pin;
169  gpio_t tx_pin;
175 };
176 
177 #ifndef CPU_FAM_STM32F1
186 void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin,
187  gpio_af_t af);
188 #else
196 void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin);
197 #endif
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif /* CANDEV_STM32_H */
Definitions for low-level CAN driver interface.
#define CAN_STM32_TX_MAILBOXES
The number of transmit mailboxes.
Definition: candev_stm32.h:135
#define CAN_STM32_RX_MAIL_FIFO
This is the maximum number of frame the driver can receive simultaneously.
Definition: candev_stm32.h:141
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:166
struct candev_conf can_conf_t
Linux candev configuration.
ESP CAN device configuration.
Definition: can_esp.h:88
CAN_TypeDef * can
CAN device.
Definition: candev_stm32.h:95
uint8_t nb_filters
Number of filters to use.
Definition: candev_stm32.h:112
uint8_t nart
No automatic retransmission.
Definition: candev_stm32.h:125
uint8_t first_filter
First filter in the bank.
Definition: candev_stm32.h:109
uint8_t sce_irqn
SCE IRQ channel.
Definition: candev_stm32.h:120
uint8_t txfp
Transmit FIFO priority.
Definition: candev_stm32.h:127
uint8_t rx0_irqn
RX0 IRQ channel.
Definition: candev_stm32.h:118
uint32_t rcc_mask
RCC mask to enable clock.
Definition: candev_stm32.h:96
uint8_t rx1_irqn
RX1 IRQ channel.
Definition: candev_stm32.h:119
uint8_t abom
Automatic bus-off management.
Definition: candev_stm32.h:123
uint8_t tx_irqn
TX IRQ channel.
Definition: candev_stm32.h:117
uint8_t awum
Automatic wakeup mode.
Definition: candev_stm32.h:124
CAN_TypeDef * can_master
Master CAN device.
Definition: candev_stm32.h:104
gpio_af_t af
Alternate pin function to use.
Definition: candev_stm32.h:100
bool en_deep_sleep_wake_up
Enable deep-sleep wake-up interrupt.
Definition: candev_stm32.h:102
uint32_t master_rcc_mask
Master device RCC mask.
Definition: candev_stm32.h:105
uint8_t rflm
Receive FIFO locked mode.
Definition: candev_stm32.h:126
uint8_t ttcm
Time triggered communication mode.
Definition: candev_stm32.h:122
uint8_t lbkm
Loopback mode.
Definition: candev_stm32.h:128
Controller Area Network frame.
Definition: can.h:101
Low level device structure for ESP32 CAN (extension of candev_t)
Definition: can_esp.h:64
const struct can_frame * tx_mailbox[CAN_STM32_TX_MAILBOXES]
Tx mailboxes.
Definition: candev_stm32.h:172
candev_stm32_rx_fifo_t rx_fifo
Rx FIFOs.
Definition: candev_stm32.h:173
candev_stm32_isr_t isr_flags
ISR flags.
Definition: candev_stm32.h:174
gpio_t rx_pin
RX pin.
Definition: candev_stm32.h:168
candev_t candev
candev base structure
Definition: can_esp.h:65
gpio_t tx_pin
TX pin.
Definition: candev_stm32.h:169
const can_conf_t * conf
Configuration.
Definition: candev_stm32.h:167
gpio_af_t af
Alternate pin function to use.
Definition: candev_stm32.h:170
Internal interrupt flags.
Definition: candev_stm32.h:158
int isr_rx
Rx FIFO interrupt.
Definition: candev_stm32.h:160
int isr_tx
Tx mailboxes interrupt.
Definition: candev_stm32.h:159
int isr_wkup
Wake up interrupt.
Definition: candev_stm32.h:161
This structure holds anything related to the receive part.
Definition: candev_stm32.h:150
int write_idx
Write index in the receive FIFO.
Definition: candev_stm32.h:152
int read_idx
Read index in the receive FIFO.
Definition: candev_stm32.h:153
struct can_frame frame[CAN_STM32_RX_MAIL_FIFO]
Receive FIFO.
Definition: candev_stm32.h:151
int is_full
Flag set when the FIFO is full.
Definition: candev_stm32.h:154
Structure to hold driver state.
Definition: candev.h:77