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
11#include <stdint.h>
12
14#include "common/phal/can.h"
15
16#if defined(STM32G474xx)
17typedef enum : uint8_t {
18 CAN_PERIPHERAL1 = 0,
19 CAN_PERIPHERAL2 = 1,
20 CAN_PERIPHERAL3 = 2,
21 CAN_NUM_PERIPHERALS = 3,
22 CAN_PERIPHERAL_INVALID = 0xFF
23} CAN_peripheral_t;
24
25static inline CAN_peripheral_t BUS_TO_PERIPHERAL(FDCAN_GlobalTypeDef *bus) {
26 if (bus == FDCAN1) return CAN_PERIPHERAL1;
27 else if (bus == FDCAN2) return CAN_PERIPHERAL2;
28 else if (bus == FDCAN3) return CAN_PERIPHERAL3;
29 else return CAN_PERIPHERAL_INVALID;
30}
31#else
32typedef enum : uint8_t {
33 CAN_PERIPHERAL1 = 0,
34 CAN_PERIPHERAL2 = 1,
35 CAN_NUM_PERIPHERALS = 2,
36 CAN_PERIPHERAL_INVALID = 0xFF
37} CAN_peripheral_t;
38
39static inline CAN_peripheral_t BUS_TO_PERIPHERAL(CAN_TypeDef *bus) {
40 if (bus == CAN1) return CAN_PERIPHERAL1;
41 else if (bus == CAN2) return CAN_PERIPHERAL2;
42 else return CAN_PERIPHERAL_INVALID;
43}
44#endif
45
46typedef struct {
47 uint32_t rx_overflow; // software queue overflow
48 uint32_t tx_overflow; // software queue overflow
49 uint32_t tx_enqueue_count;
50 uint32_t tx_task_wake_count;
51 uint32_t tx_sent_count;
52 uint32_t tx_callback_count;
53 // todo: track hardware stats
54 // todo: per-peripheral stats
56
57extern volatile can_stats_t can_stats;
58extern volatile uint32_t last_can_rx_time_ms;
59// todo: last tx time
60
61extern QueueHandle_t can_rx_queue;
62extern QueueHandle_t can_tx_queues[CAN_NUM_PERIPHERALS];
63
64void CAN_enqueue_tx(CanMsgTypeDef_t *msg);
65void CAN_tx_update(void);
66void CAN_rx_update(void);
67bool CAN_init(void);
68void CAN_enable_IRQs(void);
69void CAN_rx_init(void);
70void CAN_tx_init(void);
71
72#define DEFINE_CAN_TASKS() \
73 DEFINE_TASK(CAN_rx_update, 0, osPriorityHigh, STACK_2048); \
74 DEFINE_TASK(CAN_tx_update, 0, osPriorityHigh, STACK_1024);
75
76#define START_CAN_TASKS() \
77 START_TASK(CAN_rx_update); \
78 START_TASK(CAN_tx_update); \
79 CAN_enable_IRQs();
80
81#define CAN_TX_QUEUE_LENGTH (32) // Length of software queue for each CAN peripheral
82#define CAN_RX_QUEUE_LENGTH (32) // Length of software queue for received messages
83
84#define NVIC_RX_IRQ_PRIO (6)
85#define NVIC_TX_IRQ_PRIO (7)
86static_assert(
87 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY < NVIC_RX_IRQ_PRIO,
88 "Do not set an IRQ priority higher (numerically lower) than FreeRTOS to prevent corruption of kernel data structures"
89);
90static_assert(
91 NVIC_RX_IRQ_PRIO < NVIC_TX_IRQ_PRIO,
92 "RX priority should be higher (numerically lower) than TX"
93);
94
95#endif // CAN_COMMON_H
Wrapper macros for FreeRTOS constructs (tasks, queues, semaphores) to simplify static memory allocati...
Definition can.h:37
Definition can_common.h:46