Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions CANopenNode_STM32/CO_driver_STM32.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ static CO_CANmodule_t* CANModule_local = NULL; /* Local instance of global CAN m

#ifdef CO_STM32_FDCAN_Driver
#ifndef FDCAN_BUFFER_INDEXES
#if defined (FDCAN_TX_BUFFER31)
#if defined(FDCAN_TX_BUFFER31)
#define FDCAN_BUFFER_INDEXES 0xFFFFFFFFU
#elif defined (FDCAN_TX_BUFFER2)
#elif defined(FDCAN_TX_BUFFER2)
#define FDCAN_BUFFER_INDEXES FDCAN_TX_BUFFER0 | FDCAN_TX_BUFFER1 | FDCAN_TX_BUFFER2
#else
#define FDCAN_BUFFER_INDEXES 0xFFFFFFFFU
Expand Down Expand Up @@ -517,9 +517,18 @@ prv_read_can_received_msg(CAN_HandleTypeDef* hcan, uint32_t fifo, uint32_t fifo_
uint8_t messageFound = 0;

#ifdef CO_STM32_FDCAN_Driver

/*
* Write received message to the temporary 64-bytes buffer.
* This is to ensure that the CAN nodes that do not comply with the newer CAN standards
* don't send wrong message with the wrong DLC value. This is a safety measure to avoid buffer overflow.
*
* Check the FDCAN implementation for STM32 in their respective reference manual.
*/
static FDCAN_RxHeaderTypeDef rx_hdr;
static uint8_t rx_data[64];
/* Read received message from FIFO */
if (HAL_FDCAN_GetRxMessage(hfdcan, fifo, &rx_hdr, rcvMsg.data) != HAL_OK) {
if (HAL_FDCAN_GetRxMessage(hfdcan, fifo, &rx_hdr, rx_data) != HAL_OK) {
return;
}
/* Setup identifier (with RTR) and length */
Expand Down Expand Up @@ -556,6 +565,9 @@ prv_read_can_received_msg(CAN_HandleTypeDef* hcan, uint32_t fifo, uint32_t fifo_
rcvMsg.dlc = 0;
break; /* Invalid length when more than 8 */
}
if (rcvMsg.dlc > 0) {
memcpy(rcvMsg.data, rx_data, rcvMsg.dlc);
}
rcvMsgIdent = rcvMsg.ident;
#else
static CAN_RxHeaderTypeDef rx_hdr;
Expand Down Expand Up @@ -653,7 +665,7 @@ HAL_FDCAN_TxBufferCompleteCallback(FDCAN_HandleTypeDef* hfdcan, uint32_t BufferI
CANModule_local->CANtxCount--;
CANModule_local->bufferInhibitFlag = buffer->syncFlag;
} else {
break; // if we could not send the message, break out of the loop (the tx buffers are full)
break; // if we could not send the message, break out of the loop (the tx buffers are full)
}
}
}
Expand Down Expand Up @@ -712,9 +724,9 @@ CO_CANinterrupt_TX(CO_CANmodule_t* CANmodule, uint32_t MailboxNumber) {
buffer->bufferFull = false;
CANmodule->CANtxCount--;
CANmodule->bufferInhibitFlag = buffer->syncFlag;
} else {
break; // if we could not send the message, break out of the loop (the tx buffers are full)
}
else
break; // if we could not send the message, break out of the loop (the tx buffers are full)
}
}
CO_UNLOCK_CAN_SEND(CANmodule);
Expand Down