Skip to content

Commit b47199f

Browse files
fd can updates
1 parent 06e8138 commit b47199f

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

platforms/stm32h563/include/fdcan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ HAL_StatusTypeDef can_init(can_t *can, FDCAN_HandleTypeDef *hcan);
2828
HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg);
2929
HAL_StatusTypeDef can_add_filter_standard(can_t *can, uint16_t can_ids[2]);
3030
HAL_StatusTypeDef can_add_filter_extended(can_t *can, uint32_t can_ids[2]);
31+
bool can_is_bus_off(can_t *can);
32+
HAL_StatusTypeDef can_recover_bus_off(can_t *can);
3133

3234
// clang-format on
3335
#endif // FDCAN_H

platforms/stm32h563/src/fdcan.c

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ HAL_StatusTypeDef can_init(can_t *can, FDCAN_HandleTypeDef *hcan)
1717
HAL_StatusTypeDef status = HAL_FDCAN_ConfigInterruptLines(can->hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, FDCAN_INTERRUPT_LINE0);
1818
if (status != HAL_OK)
1919
{
20-
printf("[fdcan.c/can_init()] ERROR: Failed to run HAL_FDCAN_ConfigInterruptLines() (Status: %d).\n", status);
20+
printf("[fdcan.c/can_init()] ERROR: Failed to run HAL_FDCAN_ConfigInterruptLines() FDCAN_IT_RX_FIFO0_NEW_MESSAGE (Status: %d).\n", status);
2121
return status;
2222
}
23-
24-
/* Activate interrupt notifications */
25-
status = HAL_FDCAN_ActivateNotification(can->hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
26-
if (status != HAL_OK)
27-
{
28-
printf("[fdcan.c/can_init()] ERROR: Failed to run HAL_FDCAN_ActivateNotification() (Status: %d).\n", status);
29-
return status;
23+
status = HAL_FDCAN_ActivateNotification(can->hcan, FDCAN_IT_BUS_OFF, FDCAN_INTERRUPT_LINE1);
24+
if(status != HAL_OK) {
25+
printf("[fdcan.c/can_init()] ERROR: Failed to run HAL_FDCAN_ConfigInterruptLines() for FDCAN_IT_BUS_OFF (Status: %d).\n", status);
3026
}
27+
// status = HAL_FDCAN_ActivateNotification(can->hcan, FDCAN_IT_BUS_OFF, 0);
28+
// if(status != HAL_OK) {
29+
// printf("[fdcan.c/can_init()] ERROR: Failed to run HAL_FDCAN_ConfigInterruptLines() for FDCAN_IT_BUS_OFF (Status: %d).\n", status);
30+
// }
31+
// this is the direct code from the st guy
3132

3233
/* Set up the global filter to reject all messages by default. You must explicitly add messages to your filters in the app layer to receive them. */
3334
status = HAL_FDCAN_ConfigGlobalFilter(hcan, FDCAN_REJECT, FDCAN_REJECT, FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE);
@@ -101,6 +102,36 @@ HAL_StatusTypeDef can_add_filter_extended(can_t *can, uint32_t can_ids[2])
101102
return status;
102103
}
103104

105+
/* Returns true if the CAN node is in bus-off state. */
106+
bool can_is_bus_off(can_t *can) {
107+
FDCAN_ProtocolStatusTypeDef status;
108+
HAL_FDCAN_GetProtocolStatus(can->hcan, &status);
109+
return (bool)status.BusOff;
110+
}
111+
112+
/* Recovers from bus-off by stopping and restarting the FDCAN peripheral.
113+
Filters survive Stop/Start but notifications do not, so RX FIFO interrupt is re-enabled. */
114+
HAL_StatusTypeDef can_recover_bus_off(can_t *can) {
115+
HAL_StatusTypeDef status = HAL_FDCAN_Stop(can->hcan);
116+
if (status != HAL_OK) {
117+
printf("[fdcan.c/can_recover_bus_off()] ERROR: HAL_FDCAN_Stop() failed (Status: %d).\n", status);
118+
return status;
119+
}
120+
121+
status = HAL_FDCAN_Start(can->hcan);
122+
if (status != HAL_OK) {
123+
printf("[fdcan.c/can_recover_bus_off()] ERROR: HAL_FDCAN_Start() failed (Status: %d).\n", status);
124+
return status;
125+
}
126+
127+
status = HAL_FDCAN_ActivateNotification(can->hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
128+
if (status != HAL_OK) {
129+
printf("[fdcan.c/can_recover_bus_off()] ERROR: HAL_FDCAN_ActivateNotification() failed (Status: %d).\n", status);
130+
}
131+
132+
return status;
133+
}
134+
104135
/* Sends a CAN message. */
105136
HAL_StatusTypeDef can_send_msg(can_t *can, can_msg_t *msg)
106137
{

0 commit comments

Comments
 (0)