PER Firmware
Loading...
Searching...
No Matches
can_common.h
Go to the documentation of this file.
1#ifndef CAN_COMMON_H
2#define CAN_COMMON_H
3
12#include <stdint.h>
13
15#include "common/phal/can.h"
16
17typedef struct {
18 uint32_t tx_of; // queue overflow
19 uint32_t tx_fail; // timed out
20 uint32_t rx_overrun; // fifo overrun
22
23// FreeRTOS
24#define CAN_TX_BACKPRESSURE_MS (2) // Wait up to 2ms if FDCAN TX FIFO is full before dropping message
25#define CAN_TX_QUEUE_LENGTH (64) // Length of software queue for each CAN peripheral
26#define CAN_RX_QUEUE_LENGTH (64) // Length of software queue for received messages
27
28#define CAN_TX_MAILBOX_CNT (3)
29#define CAN_TX_TIMEOUT_MS (15)
30#define CAN_TX_BLOCK_TIMEOUT (30 * 16000)
31
32#define CAN_MAILBOX_HIGH_PRIO 0
33#define CAN_MAILBOX_MED_PRIO 1
34#define CAN_MAILBOX_LOW_PRIO 2
35
36/* Standard CAN flag and mask definitions */
37#define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
38#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
39#define CAN_ERR_FLAG 0x20000000U /* error message frame */
40
41/* valid bits in CAN ID for frame formats */
42#define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
43#define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
44#define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
45
46#ifndef CAN1_IDX
47#define CAN1_IDX 0
48#endif
49#ifndef CAN2_IDX
50#define CAN2_IDX 1
51#endif
52#ifndef CAN3_IDX
53#define CAN3_IDX 2
54#endif
55
56#if defined(STM32G474xx)
57// G4 uses FDCAN peripheral
58#define GET_PERIPH_IDX(bus) ((bus == FDCAN1) ? CAN1_IDX : ((bus == FDCAN2) ? CAN2_IDX : CAN3_IDX))
59#else
60// F4/F7/L4 use bxCAN peripheral
61#define GET_PERIPH_IDX(bus) ((bus == CAN1) ? CAN1_IDX : CAN2_IDX)
62#endif
63
64#if defined(STM32G474xx)
65#define NUM_CAN_PERIPHERALS_MAX 3
66#else
67#define NUM_CAN_PERIPHERALS_MAX 2
68#endif
69
70typedef struct {
71 uint32_t rx_of; // queue overflow
72 can_peripheral_stats_t can_peripheral_stats[NUM_CAN_PERIPHERALS_MAX];
74
75extern can_stats_t can_stats;
76extern volatile uint32_t last_can_rx_time_ms;
77
78
79#if defined(STM32F407xx)
80// bxCAN uses 3 mailboxes per peripheral
81extern QueueHandle_t q_tx_can[][CAN_TX_MAILBOX_CNT];
82#elif defined(STM32G474xx)
83// G4/FDCAN uses a single TX queue per peripheral (no mailboxes)
84extern QueueHandle_t q_tx_can[];
85#else
86#error "Unsupported architecture"
87#endif
88extern QueueHandle_t q_rx_can;
89
90void CAN_enqueue_tx(CanMsgTypeDef_t *msg);
91
92#include "common/can_library/generated/can_router.h"
93
94void CAN_tx_update();
95void CAN_rx_update();
96bool CAN_library_init();
97
98#if defined(STM32G474xx)
99// G4/FDCAN IRQ handling is done in fdcan.c via PHAL_FDCAN_rxCallback
100#else
101void CAN_handle_irq(CAN_TypeDef *bus, uint8_t fifo);
102#endif
103
104#endif // CAN_COMMON_H
Wrapper macros for FreeRTOS constructs (tasks, queues, semaphores) to simplify static memory allocati...
Definition can.h:31
Definition can_common.h:17
Definition can_common.h:70