Skip to content

Commit 67b90a1

Browse files
authored
Merge pull request #5 from arduino/fix-can-tx
Fix: only transmit CAN frame on valid opcode AND always send a status message upon attempted CAN transmit
2 parents 5d45504 + 6456bbe commit 67b90a1

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.su
33
*.o
44
STM32H747AII6*
5+
.idea/

include/peripherals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ enum Opcodes_RTC {
6767
};
6868

6969
enum Opcodes_CAN {
70+
CAN_TX_FRAME = 0x01,
71+
CAN_STATUS = 0x40,
7072
CAN_FILTER = 0x50,
7173
};
7274

src/can.c

Lines changed: 34 additions & 10 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
**************************************************************************************/
@@ -264,7 +277,7 @@ void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
264277
uint32_t* info = (uint32_t*)data;
265278
CANFormat format = info[1] < 0x800 ? CANStandard : CANExtended;
266279
canFilter(PERIPH_FDCAN1, info[1], info[2], format, info[0]);
267-
} else {
280+
} else if (opcode == CAN_TX_FRAME) {
268281
CAN_Message msg;
269282
msg.type = CANData;
270283
msg.format = CANStandard;
@@ -281,6 +294,8 @@ void fdcan1_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
281294
if (ret == 0) {
282295
canReset(PERIPH_FDCAN1);
283296
}
297+
} else {
298+
dbg_printf("fdcan1_handler: error invalid opcode (:%d)\n", opcode);
284299
}
285300
}
286301

@@ -291,7 +306,7 @@ void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
291306
uint32_t* info = (uint32_t*)data;
292307
CANFormat format = info[1] < 0x800 ? CANStandard : CANExtended;
293308
canFilter(PERIPH_FDCAN2, info[1], info[2], format, info[0]);
294-
} else {
309+
} else if (opcode == CAN_TX_FRAME) {
295310
CAN_Message msg;
296311
msg.type = CANData;
297312
msg.format = CANStandard;
@@ -308,6 +323,8 @@ void fdcan2_handler(uint8_t opcode, uint8_t *data, uint16_t size) {
308323
if (ret == 0) {
309324
canReset(PERIPH_FDCAN2);
310325
}
326+
} else {
327+
dbg_printf("fdcan2_handler: error invalid opcode (:%d)\n", opcode);
311328
}
312329
}
313330

@@ -725,16 +742,23 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
725742
TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;
726743
TxHeader.MessageMarker = 0;
727744

728-
if (HAL_FDCAN_AddMessageToTxFifoQ(&obj->CanHandle, &TxHeader, msg.data) != HAL_OK) {
729-
// Note for debug: you can get the error code calling HAL_FDCAN_GetError(&obj->CanHandle)
730-
printf("error: %ld\n", HAL_FDCAN_GetError(&obj->CanHandle));
731-
return 0;
732-
}
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);
733749

734-
uint8_t _msg[2] = {0x01, 0};
735-
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;
736752

737-
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+
}
738762
}
739763

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

0 commit comments

Comments
 (0)