Skip to content

Commit a969cc1

Browse files
committed
Fix: also transmit a status message when sending the CAN frame failed.
1 parent 72304de commit a969cc1

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

include/peripherals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ enum Opcodes_RTC {
6868

6969
enum Opcodes_CAN {
7070
CAN_TX_FRAME = 0x01,
71+
CAN_STATUS = 0x40,
7172
CAN_FILTER = 0x50,
7273
};
7374

src/can.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636
#define CFG_HW_RCC_SEMID 3
3737
#undef DUAL_CORE
3838

39+
#define X8H7_CAN_STS_FLG_RX_OVR 0x01 // Receive Buffer Overflow
40+
#define X8H7_CAN_STS_FLG_TX_BO 0x02 // Bus-Off
41+
#define X8H7_CAN_STS_FLG_TX_EP 0x04 // Transmit Error-Passive
42+
#define X8H7_CAN_STS_FLG_RX_EP 0x08 // Receive Error-Passive
43+
#define X8H7_CAN_STS_FLG_TX_WAR 0x10 // Transmit Error Warning
44+
#define X8H7_CAN_STS_FLG_RX_WAR 0x20 // Receive Error Warning
45+
#define X8H7_CAN_STS_FLG_EWARN 0x40 // Error Warning
46+
#define X8H7_CAN_STS_FLG_TX_OVR 0x80 // Transmit Buffer Overflow
47+
48+
#define X8H7_CAN_STS_INT_TX 0x01
49+
#define X8H7_CAN_STS_INT_RX 0x02
50+
#define X8H7_CAN_STS_INT_ERR 0x04
51+
3952
/**************************************************************************************
4053
* GLOBAL VARIABLES
4154
**************************************************************************************/
@@ -729,16 +742,23 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
729742
TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;
730743
TxHeader.MessageMarker = 0;
731744

732-
if (HAL_FDCAN_AddMessageToTxFifoQ(&obj->CanHandle, &TxHeader, msg.data) != HAL_OK) {
733-
// Note for debug: you can get the error code calling HAL_FDCAN_GetError(&obj->CanHandle)
734-
printf("error: %ld\n", HAL_FDCAN_GetError(&obj->CanHandle));
735-
return 0;
736-
}
745+
if (HAL_FDCAN_AddMessageToTxFifoQ(&obj->CanHandle, &TxHeader, msg.data) != HAL_OK)
746+
{
747+
uint32_t const err_code = HAL_FDCAN_GetError(&obj->CanHandle);
748+
printf("error: %ld\n", err_code);
737749

738-
uint8_t _msg[2] = {0x01, 0};
739-
enqueue_packet(obj == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, 0x40, sizeof(_msg), _msg);
750+
uint8_t msg[2] = {X8H7_CAN_STS_INT_ERR, 0};
751+
if (err_code == HAL_FDCAN_ERROR_FIFO_FULL) msg[1] = X8H7_CAN_STS_FLG_TX_OVR;
740752

741-
return 1;
753+
enqueue_packet(obj == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
754+
return 0;
755+
}
756+
else
757+
{
758+
uint8_t msg[2] = {X8H7_CAN_STS_INT_TX, 0};
759+
enqueue_packet(obj == &fdcan_1 ? PERIPH_FDCAN1 : PERIPH_FDCAN2, CAN_STATUS, sizeof(msg), msg);
760+
return 1;
761+
}
742762
}
743763

744764
int can_read(can_t *obj, CAN_Message *msg, int handle)

0 commit comments

Comments
 (0)