-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoes_in_main.c
55 lines (47 loc) · 1.73 KB
/
goes_in_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// The actual ISR, modify this to your needs
// Run HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) once to set up the ISR
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
// Receiving
CanardCANFrame rx_frame;
const uint64_t timestamp = HAL_GetTick() * 1000ULL;
const int16_t rx_res = canardSTM32Recieve(hcan, CAN_RX_FIFO0, &rx_frame);
if (rx_res < 0) {
printf("Receive error %d\n", rx_res);
}
else if (rx_res > 0) // Success - process the frame
{
canardHandleRxFrame(&canard, &rx_frame, timestamp);
}
}
// CAN Filter setup, modify this to your needs
// Run this once before calling HAL_CAN_Start()
void setupCANFilter(CAN_HandleTypeDef *hcan) {
CAN_FilterTypeDef filter;
filter.FilterBank = 0;
filter.FilterMode = CAN_FILTERMODE_IDMASK;
filter.FilterFIFOAssignment = CAN_RX_FIFO0;
filter.FilterIdHigh = 0;
filter.FilterIdLow = 0;
filter.FilterMaskIdHigh = 0;
filter.FilterMaskIdLow = 0;
filter.FilterScale = CAN_FILTERSCALE_32BIT;
filter.FilterActivation = ENABLE;
filter.SlaveStartFilterBank = 14;
HAL_CAN_ConfigFilter(hcan, &filter);
}
// Processes the canard Tx queue and attempts to transmit the messages
// Call this function very often to check if there are any Tx to process
// Calling it once every cycle of the while(1) loop is not a bad idea
void processCanardTxQueue(CAN_HandleTypeDef *hcan) {
// Transmitting
for (const CanardCANFrame *tx_frame ; (tx_frame = canardPeekTxQueue(&canard)) != NULL;) {
const int16_t tx_res = canardSTM32Transmit(hcan, tx_frame);
if (tx_res <= 0) {
printf("Transmit error %d\n", tx_res);
} else if (tx_res > 0) {
printf("Successfully transmitted message\n");
}
// Pop canardTxQueue either way
canardPopTxQueue(&canard);
}
}