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 
40 #if defined(CPU_LINE_STM32F413xx) || defined(CPU_LINE_STM32F423xx)
41 #define CANDEV_STM32_CHAN_NUMOF 3
42 #elif defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || defined(CPU_FAM_STM32F4)
43 #define CANDEV_STM32_CHAN_NUMOF 2
44 #elif defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
45  defined(CPU_FAM_STM32L4) || DOXYGEN
46 
47 #define CANDEV_STM32_CHAN_NUMOF 1
48 #else
49 #error "CAN STM32: CPU not supported"
50 #endif
51 
56 #if defined(CPU_FAM_STM32F1)
57 #define ISR_CAN1_TX isr_usb_hp_can1_tx
58 #define ISR_CAN1_RX0 isr_usb_lp_can1_rx0
59 #define ISR_CAN1_RX1 isr_can1_rx1
60 #define ISR_CAN1_SCE isr_can1_sce
61 #else
62 #define ISR_CAN1_TX isr_can1_tx
63 #define ISR_CAN1_RX0 isr_can1_rx0
64 #define ISR_CAN1_RX1 isr_can1_rx1
65 #define ISR_CAN1_SCE isr_can1_sce
66 #define ISR_CAN2_TX isr_can2_tx
67 #define ISR_CAN2_RX0 isr_can2_rx0
68 #define ISR_CAN2_RX1 isr_can2_rx1
69 #define ISR_CAN2_SCE isr_can2_sce
70 #define ISR_CAN3_TX isr_can3_tx
71 #define ISR_CAN3_RX0 isr_can3_rx0
72 #define ISR_CAN3_RX1 isr_can3_rx1
73 #define ISR_CAN3_SCE isr_can3_sce
74 #endif
75 
77 #if CANDEV_STM32_CHAN_NUMOF > 1 || DOXYGEN
78 
79 #define CAN_STM32_NB_FILTER 28
80 #else
81 #define CAN_STM32_NB_FILTER 14
82 #endif
83 
84 #ifndef CANDEV_STM32_DEFAULT_BITRATE
85 
86 #define CANDEV_STM32_DEFAULT_BITRATE 500000U
87 #endif
88 
89 #ifndef CANDEV_STM32_DEFAULT_SPT
90 
91 #define CANDEV_STM32_DEFAULT_SPT 875
92 #endif
93 
95 typedef struct {
96  CAN_TypeDef *can;
97  uint32_t rcc_mask;
98  gpio_t rx_pin;
99  gpio_t tx_pin;
100 #ifndef CPU_FAM_STM32F1
102 #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
136 
137 #define CAN_STM32_RX_MAILBOXES 2
138 
139 
140 #ifndef CAN_STM32_RX_MAIL_FIFO
141 
142 #define CAN_STM32_RX_MAIL_FIFO 12
143 #endif
144 
146 typedef struct can can_t;
148 #define HAVE_CAN_T
149 
151 typedef struct candev_stm32_rx_fifo {
153  int write_idx;
154  int read_idx;
155  int is_full;
157 
159 typedef struct candev_stm32_isr {
160  int isr_tx : 3;
161  int isr_rx : 2;
162  int isr_wkup : 1;
164 
166 struct can {
167  candev_t candev;
168  const can_conf_t *conf;
169  gpio_t rx_pin;
170  gpio_t tx_pin;
176 };
177 
178 #ifndef CPU_FAM_STM32F1
179 
187 void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin,
188  gpio_af_t af);
189 #else
190 
197 void candev_stm32_set_pins(can_t *dev, gpio_t tx_pin, gpio_t rx_pin);
198 #endif
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #endif /* CANDEV_STM32_H */
candev_stm32_rx_fifo
This structure holds anything related to the receive part.
Definition: candev_stm32.h:151
CAN_STM32_RX_MAIL_FIFO
#define CAN_STM32_RX_MAIL_FIFO
This is the maximum number of frame the driver can receive simultaneously.
Definition: candev_stm32.h:142
can_conf_t::rcc_mask
uint32_t rcc_mask
RCC mask to enable clock.
Definition: candev_stm32.h:97
candev_stm32_rx_fifo::read_idx
int read_idx
Read index in the receive FIFO.
Definition: candev_stm32.h:154
can::isr_flags
candev_stm32_isr_t isr_flags
ISR flags.
Definition: candev_stm32.h:175
can_conf_t::can_master
CAN_TypeDef * can_master
Master CAN device.
Definition: candev_stm32.h:104
candev
Structure to hold driver state.
Definition: candev.h:78
can_conf_t::awum
uint8_t awum
Automatic wakeup mode.
Definition: candev_stm32.h:124
can_conf_t::first_filter
uint8_t first_filter
First filter in the bank.
Definition: candev_stm32.h:109
can_conf_t::tx_irqn
uint8_t tx_irqn
TX IRQ channel.
Definition: candev_stm32.h:117
candev_stm32_isr::isr_rx
int isr_rx
Rx FIFO interrupt.
Definition: candev_stm32.h:161
gpio_af_t
gpio_af_t
Available MUX values for configuring a pin's alternate function.
Definition: periph_cpu_common.h:85
can_conf_t::rx1_irqn
uint8_t rx1_irqn
RX1 IRQ channel.
Definition: candev_stm32.h:119
candev_stm32_isr_t
struct candev_stm32_isr candev_stm32_isr_t
Internal interrupt flags.
can::tx_mailbox
const struct can_frame * tx_mailbox[CAN_STM32_TX_MAILBOXES]
Tx mailboxes.
Definition: candev_stm32.h:173
candev_stm32_rx_fifo::write_idx
int write_idx
Write index in the receive FIFO.
Definition: candev_stm32.h:153
can_conf_t::can
CAN_TypeDef * can
CAN device.
Definition: candev_stm32.h:96
candev_stm32_isr::isr_wkup
int isr_wkup
Wake up interrupt.
Definition: candev_stm32.h:162
can_conf_t::rx0_irqn
uint8_t rx0_irqn
RX0 IRQ channel.
Definition: candev_stm32.h:118
can_conf_t::ttcm
uint8_t ttcm
Time triggered communication mode.
Definition: candev_stm32.h:122
can_conf_t::nart
uint8_t nart
No automatic retransmission.
Definition: candev_stm32.h:125
CAN_STM32_TX_MAILBOXES
#define CAN_STM32_TX_MAILBOXES
The number of transmit mailboxes.
Definition: candev_stm32.h:135
candev_stm32_isr::isr_tx
int isr_tx
Tx mailboxes interrupt.
Definition: candev_stm32.h:160
can_conf_t::txfp
uint8_t txfp
Transmit FIFO priority.
Definition: candev_stm32.h:127
can_conf_t::master_rcc_mask
uint32_t master_rcc_mask
Master device RCC mask.
Definition: candev_stm32.h:105
can::rx_fifo
candev_stm32_rx_fifo_t rx_fifo
Rx FIFOs.
Definition: candev_stm32.h:174
can_conf_t::sce_irqn
uint8_t sce_irqn
SCE IRQ channel.
Definition: candev_stm32.h:120
can_frame
Controller Area Network frame.
Definition: can.h:88
can_conf_t
ESP CAN device configuration.
Definition: can_esp.h:98
candev_stm32_rx_fifo::is_full
int is_full
Flag set when the FIFO is full.
Definition: candev_stm32.h:155
candev.h
Definitions for low-level CAN driver interface.
can_conf_t::nb_filters
uint8_t nb_filters
Number of filters to use.
Definition: candev_stm32.h:112
candev_stm32_isr
Internal interrupt flags.
Definition: candev_stm32.h:159
can::af
gpio_af_t af
Alternate pin function to use.
Definition: candev_stm32.h:171
can::tx_pin
gpio_t tx_pin
CAN transceiver TX pin.
Definition: can_esp.h:79
can_conf_t::silm
uint8_t silm
Silent mode.
Definition: candev_stm32.h:129
can_conf_t
struct candev_conf can_conf_t
Linux candev configuration.
candev_stm32_rx_fifo::frame
struct can_frame frame[CAN_STM32_RX_MAIL_FIFO]
Receive FIFO.
Definition: candev_stm32.h:152
can_conf_t::rflm
uint8_t rflm
Receive FIFO locked mode.
Definition: candev_stm32.h:126
can::rx_pin
gpio_t rx_pin
CAN transceiver RX pin.
Definition: can_esp.h:80
can_conf_t::af
gpio_af_t af
Alternate pin function to use.
Definition: candev_stm32.h:101
can
Low level device structure for ESP32 CAN (extension of candev_t)
Definition: can_esp.h:64
candev_stm32_rx_fifo_t
struct candev_stm32_rx_fifo candev_stm32_rx_fifo_t
This structure holds anything related to the receive part.
can::conf
const can_conf_t * conf
Configuration.
Definition: candev_stm32.h:168
can_conf_t::abom
uint8_t abom
Automatic bus-off management.
Definition: candev_stm32.h:123
can_conf_t::lbkm
uint8_t lbkm
Loopback mode.
Definition: candev_stm32.h:128
candev_stm32_set_pins
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.
can::candev
candev_t candev
candev base structure
Definition: can_esp.h:65