microHAL
An abstraction layer for your future F4xx projects
Loading...
Searching...
No Matches
bxcan.h
Go to the documentation of this file.
1
12#ifndef BXCAN_H
13#define BXCAN_H
14
15/* -- Includes -- */
16#include <stdint.h>
17#include "stm32f4xx.h"
18#include "defines.h"
19
20/* -- Structs -- */
24struct __attribute__((packed)) bxCANMailboxRegs {
25 REG32 IR;
26 REG32 TR;
27 REG32 LR;
28 REG32 HR;
29};
30
31_Static_assert((sizeof(struct bxCANMailboxRegs)) == (sizeof(uint32_t) * 4U),
32 "bxCAN Mailbox register struct size mismatch. Is it aligned?");
33
37struct __attribute__((packed)) bxCANFilterRegs {
38 REG32 FR1;
39 REG32 FR2;
40};
41
42_Static_assert((sizeof(struct bxCANFilterRegs)) == (sizeof(uint32_t) * 2U),
43 "bxCAN Filter register struct size mismatch. Is it aligned?");
44
48struct __attribute__((packed)) bxCANRegs {
49 REG32 MCR;
50 REG32 MSR;
51 REG32 TSR;
52 REG32 RFR[2];
53 REG32 IER;
54 REG32 ESR;
55 REG32 BTR;
56 REG32 _reserved1[88];
57 struct bxCANMailboxRegs TxMailbox[3];
58 struct bxCANMailboxRegs FIFOMailbox[2];
59 REG32 _reserved2[12];
60 REG32 FMR;
61 REG32 FM1R;
62 REG32 _reserved3;
63 REG32 FS1R;
64 REG32 _reserved4;
65 REG32 FFA1R;
66 REG32 _reserved5;
67 REG32 FA1R;
68 REG32 _reserved6[8];
69 struct bxCANFilterRegs FilterBank[28];
70};
71
72_Static_assert((sizeof(struct bxCANRegs)) == (sizeof(uint32_t) * 200U),
73 "bxCAN register struct size mismatch. Is it aligned?");
74
75#define CAN(NUM) (struct bxCANRegs *)(CAN1_BASE + (0x400UL * (uint8_t)NUM))
76
77struct __attribute__((packed)) bxCANISR {
78 _Bool TME : 1;
79 _Bool FMP0 : 1;
80 _Bool FF0 : 1;
81 _Bool FOV0 : 1;
82 _Bool FMP1 : 1;
83 _Bool FF1 : 1;
84 _Bool FOV1 : 1;
85 _Bool EWG : 1;
86 _Bool EPV : 1;
87 _Bool BOF : 1;
88 _Bool LEC : 1;
89 _Bool ERR : 1;
90 _Bool WKU : 1;
91 _Bool SLK : 1;
92};
93
94_Static_assert((sizeof(struct bxCANISR)) == (sizeof(uint8_t) * 2U),
95 "bxCAN ISR struct size mismatch. Is it aligned?");
96
100struct __attribute__((packed)) bxCANBitrateConfig {
101 uint8_t TimeSeg1 : 4;
102 uint8_t TimeSeg2 : 3;
103 uint16_t BaudPrescaler : 10;
104};
105
106_Static_assert((sizeof(struct bxCANBitrateConfig)) == (sizeof(uint8_t) * 3U),
107 "bxCAN Bitrate config struct size mismatch. Is it aligned?");
108
112struct __attribute__((packed)) bxCANFilterConfig {
113 uint16_t FilterID[2];
114 uint16_t FilterIDMask[2];
115 _Bool ListMode : 1;
116 _Bool SingleScale : 1;
117 _Bool FIFO : 1;
118 _Bool Activate : 1;
119};
120
121_Static_assert((sizeof(struct bxCANFilterConfig)) == (sizeof(uint8_t) * 9U),
122 "bxCAN Filter config struct size mismatch. Is it aligned?");
123
124struct __attribute__((packed)) bxCANAutomationConfig {
125 _Bool AutoBusOff : 1;
126 _Bool AutoWakeUp : 1;
127 _Bool AutoReTx : 1;
128};
129
133struct __attribute__((packed)) bxCANFrame {
134 /* --- Header --- */
135 volatile uint32_t ID;
136 volatile _Bool IDE;
137 volatile _Bool RTR;
138 volatile uint8_t DLC;
139 /* --- Time --- */
140 volatile uint16_t TIME;
141 volatile _Bool TGT;
142 /* --- Data --- */
143 volatile uint8_t DATA[8];
144 // CRC handled by HW
145 // ACK handled by HW
146};
147
148_Static_assert((sizeof(struct bxCANFrame)) == (sizeof(uint8_t) * 18U),
149 "bxCAN Frame struct size mismatch. Is it aligned?");
150
154struct __attribute__((packed)) bxCANErrorInfo {
155 uint8_t RxErrors;
156 uint8_t TxErrors;
157 uint8_t LastCode;
158 _Bool BusOff;
159 _Bool Passive;
160 _Bool Warning;
161};
162
163_Static_assert((sizeof(struct bxCANErrorInfo)) == (sizeof(uint8_t) * 6U),
164 "bxCAN Error info struct size mismatch. Is it aligned?");
165
166/* -- Enums -- */
170typedef enum bxcan_peripheral {
171#ifdef CAN1_BASE
172 BXCAN_PERIPH_1 = 0x0,
173#endif
174#ifdef CAN2_BASE
175 BXCAN_PERIPH_2 = 0x1,
176#endif
177 BXCAN_PERIPH_LEN
179
183typedef enum bxcan_mode {
184 BXCAN_MODE_INIT = 0x0,
185 BXCAN_MODE_NORM,
186 BXCAN_MODE_LPOW,
188
192typedef enum bxcan_test_mode {
193 BXCAN_TEST_MODE_NONE = 0x0,
194 BXCAN_TEST_MODE_LOOP,
195 BXCAN_TEST_MODE_SLNT,
196 BXCAN_TEST_MODE_BOTH
198
203 BXCAN_FIFO_PRIORITY_ID = 0x0,
204 BXCAN_FIFO_PRIORITY_RQ,
206
219 const bxcan_mode_t mode);
220
233 const bxcan_test_mode_t mode);
234
243 const _Bool state);
244
256 const struct bxCANISR config);
257
271 const struct bxCANBitrateConfig config);
272
286 const struct bxCANAutomationConfig config);
287
299void bxcan_configure_fifo(const bxcan_peripheral_t can, const _Bool lock,
300 const bxcan_fifo_priority_t priority);
301
317 const uint8_t filters);
318
334void bxcan_configure_filter(const bxcan_peripheral_t can, const uint8_t filter,
335 const struct bxCANFilterConfig config);
336
349void bxcan_tx_frame(const bxcan_peripheral_t can, struct bxCANFrame *frame);
350
365void bxcan_rx_frame_fetch(const bxcan_peripheral_t can, const _Bool FIFO,
366 struct bxCANMailboxRegs *buffer);
367
379void bxcan_rx_frame_process(const struct bxCANMailboxRegs buffer,
380 struct bxCANFrame *frame);
381
391
392#endif
enum bxcan_mode bxcan_mode_t
Available bxCAN operation modes.
void bxcan_set_filter_start(const bxcan_peripheral_t can, const uint8_t filters)
Sets the desired bxCAN filter start bank.
Definition bxcan.c:174
void bxcan_configure_filter(const bxcan_peripheral_t can, const uint8_t filter, const struct bxCANFilterConfig config)
Configures the bxCAN filter according to config.
Definition bxcan.c:194
bxcan_fifo_priority
Available bxCAN FIFO priorities.
Definition bxcan.h:202
void bxcan_configure_bitrate(const bxcan_peripheral_t can, const struct bxCANBitrateConfig config)
Configures the bxCAN bitrate according to config.
Definition bxcan.c:121
void bxcan_configure_fifo(const bxcan_peripheral_t can, const _Bool lock, const bxcan_fifo_priority_t priority)
Configures the bxCAN FIFO according to config.
Definition bxcan.c:158
enum bxcan_fifo_priority bxcan_fifo_priority_t
Available bxCAN FIFO priorities.
void bxcan_set_test_mode(const bxcan_peripheral_t can, const bxcan_test_mode_t mode)
Configures bxCAN test mode features.
Definition bxcan.c:54
bxcan_test_mode
Available bxCAN test modes.
Definition bxcan.h:192
bxcan_mode
Available bxCAN operation modes.
Definition bxcan.h:183
const struct bxCANErrorInfo bxcan_get_error_info(const bxcan_peripheral_t can)
Fetches all CAN errors.
Definition bxcan.c:342
void bxcan_tx_frame(const bxcan_peripheral_t can, struct bxCANFrame *frame)
Transmits the specified CAN frame.
Definition bxcan.c:264
bxcan_peripheral
Available bxCAN operation modes.
Definition bxcan.h:170
void bxcan_set_current_mode(const bxcan_peripheral_t can, const bxcan_mode_t mode)
Sets the bxCAN to the specified mode.
Definition bxcan.c:24
void bxcan_configure_automation(const bxcan_peripheral_t can, const struct bxCANAutomationConfig config)
Configures the bxCAN automation according to config.
Definition bxcan.c:140
void bxcan_set_time_triggered_mode(const bxcan_peripheral_t can, const _Bool state)
Sets bxCAN time triggered communication.
Definition bxcan.c:79
void bxcan_rx_frame_fetch(const bxcan_peripheral_t can, const _Bool FIFO, struct bxCANMailboxRegs *buffer)
Fetches received CAN frame data.
Definition bxcan.c:305
enum bxcan_test_mode bxcan_test_mode_t
Available bxCAN test modes.
enum bxcan_peripheral bxcan_peripheral_t
Available bxCAN operation modes.
void bxcan_rx_frame_process(const struct bxCANMailboxRegs buffer, struct bxCANFrame *frame)
Extracts received CAN frame data.
Definition bxcan.c:323
void bxcan_set_interrupts(const bxcan_peripheral_t can, const struct bxCANISR config)
Enables the specified bxCAN interrupts.
Definition bxcan.c:95
Defines used commonly in most files.
CMSIS device header stubs.
Definition bxcan.h:124
Contains bxCAN baudrate configuration.
Definition bxcan.h:100
uint8_t TimeSeg2
Definition bxcan.h:102
uint8_t TimeSeg1
Definition bxcan.h:101
uint16_t BaudPrescaler
Definition bxcan.h:103
Contains bxCAN error info.
Definition bxcan.h:154
Contains bxCAN filter configuration.
Definition bxcan.h:112
Contains bxCAN Filter registers.
Definition bxcan.h:37
Contains bxCAN frame structure.
Definition bxcan.h:133
Definition bxcan.h:77
_Bool TME
Definition bxcan.h:78
_Bool FOV0
Definition bxcan.h:81
_Bool FMP1
Definition bxcan.h:82
_Bool SLK
Definition bxcan.h:91
_Bool LEC
Definition bxcan.h:88
_Bool BOF
Definition bxcan.h:87
_Bool FOV1
Definition bxcan.h:84
_Bool EPV
Definition bxcan.h:86
_Bool FF0
Definition bxcan.h:80
_Bool FF1
Definition bxcan.h:83
_Bool EWG
Definition bxcan.h:85
_Bool FMP0
Definition bxcan.h:79
_Bool WKU
Definition bxcan.h:90
_Bool ERR
Definition bxcan.h:89
Contains bxCAN Mailbox registers.
Definition bxcan.h:24
REG32 LR
Definition bxcan.h:27
REG32 IR
Definition bxcan.h:25
REG32 TR
Definition bxcan.h:26
REG32 HR
Definition bxcan.h:28
Contains bxCAN registers.
Definition bxcan.h:48