Skip to content

Commit de7e326

Browse files
authored
Merge pull request #14677 from MubeenHCLite/corrections_in_STM_canapi
Changes required in STM CAN read API
2 parents 7523945 + 6f436db commit de7e326

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

targets/TARGET_STM/can_api.c

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,23 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
163163
obj->CanHandle.Init.DataSyncJumpWidth = 0x1; // Not used - only in FDCAN
164164
obj->CanHandle.Init.DataTimeSeg1 = 0x1; // Not used - only in FDCAN
165165
obj->CanHandle.Init.DataTimeSeg2 = 0x1; // Not used - only in FDCAN
166-
#ifndef TARGET_STM32G4
166+
#ifdef TARGET_STM32H7
167+
/* Message RAM offset is only supported in STM32H7 platforms of supported FDCAN platforms */
167168
obj->CanHandle.Init.MessageRAMOffset = 0;
169+
170+
/* The number of Standard and Extended ID filters are initialized to the maximum possile extent
171+
* for STM32H7 platforms
172+
*/
173+
obj->CanHandle.Init.StdFiltersNbr = 128; // to be aligned with the handle parameter in can_filter
174+
obj->CanHandle.Init.ExtFiltersNbr = 128; // to be aligned with the handle parameter in can_filter
175+
#else
176+
/* The number of Standard and Extended ID filters are initialized to the maximum possile extent
177+
* for STM32G0x1, STM32G4 and STM32L5 platforms
178+
*/
179+
obj->CanHandle.Init.StdFiltersNbr = 28; // to be aligned with the handle parameter in can_filter
180+
obj->CanHandle.Init.ExtFiltersNbr = 8; // to be aligned with the handle parameter in can_filter
168181
#endif
169-
obj->CanHandle.Init.StdFiltersNbr = 1; // to be aligned with the handle parameter in can_filter
170-
obj->CanHandle.Init.ExtFiltersNbr = 1; // to be aligned with the handle parameter in can_filter
171-
#ifndef TARGET_STM32G4
182+
#ifdef TARGET_STM32H7
172183
obj->CanHandle.Init.RxFifo0ElmtsNbr = 8;
173184
obj->CanHandle.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
174185
obj->CanHandle.Init.RxFifo1ElmtsNbr = 0;
@@ -180,7 +191,7 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
180191
obj->CanHandle.Init.TxFifoQueueElmtsNbr = 3;
181192
#endif
182193
obj->CanHandle.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
183-
#ifndef TARGET_STM32G4
194+
#ifdef TARGET_STM32H7
184195
obj->CanHandle.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
185196
#endif
186197
can_internal_init(obj);
@@ -329,20 +340,18 @@ int can_frequency(can_t *obj, int f)
329340
*/
330341
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle)
331342
{
332-
UNUSED(handle); // Not supported yet (seems to be a used in read function?)
333-
334343
FDCAN_FilterTypeDef sFilterConfig = {0};
335344

336345
if (format == CANStandard) {
337346
sFilterConfig.IdType = FDCAN_STANDARD_ID;
338-
sFilterConfig.FilterIndex = 0;
347+
sFilterConfig.FilterIndex = handle;
339348
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
340349
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
341350
sFilterConfig.FilterID1 = id;
342351
sFilterConfig.FilterID2 = mask;
343352
} else if (format == CANExtended) {
344353
sFilterConfig.IdType = FDCAN_EXTENDED_ID;
345-
sFilterConfig.FilterIndex = 0;
354+
sFilterConfig.FilterIndex = handle;
346355
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
347356
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
348357
sFilterConfig.FilterID1 = id;
@@ -391,7 +400,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
391400

392401
int can_read(can_t *obj, CAN_Message *msg, int handle)
393402
{
394-
UNUSED(handle); // Not supported yet (seems to be a handle to a filter configuration?)
403+
UNUSED(handle); // Not supported, RXFIFO0 is set default by can_filter and cannot be changed.
395404

396405
if (HAL_FDCAN_GetRxFifoFillLevel(&obj->CanHandle, FDCAN_RX_FIFO0) == 0) {
397406
return 0; // No message arrived
@@ -655,6 +664,8 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
655664
#include <string.h>
656665
#include <inttypes.h>
657666

667+
#define DEFAULT_RXFIFO 0 // default rx fifo for can by hardware is FIFO0
668+
658669
static uint32_t can_irq_ids[CAN_NUM] = {0};
659670
static can_irq_handler irq_handler;
660671

@@ -965,8 +976,8 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
965976

966977
int can_read(can_t *obj, CAN_Message *msg, int handle)
967978
{
968-
//handle is the FIFO number
969-
979+
//FIFO selection cannot be controlled by software for STM32, default FIFO is 0, hence handle is not used
980+
int rxfifo_default = DEFAULT_RXFIFO;
970981
CAN_TypeDef *can = obj->CanHandle.Instance;
971982

972983
// check FPM0 which holds the pending message count in FIFO 0
@@ -976,36 +987,30 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
976987
}
977988

978989
/* Get the Id */
979-
msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR) >> 2);
990+
msg->format = (CANFormat)(((uint8_t)0x04 & can->sFIFOMailBox[rxfifo_default].RIR) >> 2);
980991
if (!msg->format) {
981-
msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[handle].RIR >> 21);
992+
msg->id = (uint32_t)0x000007FF & (can->sFIFOMailBox[rxfifo_default].RIR >> 21);
982993
} else {
983-
msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[handle].RIR >> 3);
994+
msg->id = (uint32_t)0x1FFFFFFF & (can->sFIFOMailBox[rxfifo_default].RIR >> 3);
984995
}
985996

986-
msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[handle].RIR) >> 1);
997+
msg->type = (CANType)(((uint8_t)0x02 & can->sFIFOMailBox[rxfifo_default].RIR) >> 1);
987998
/* Get the DLC */
988-
msg->len = (uint8_t)0x0F & can->sFIFOMailBox[handle].RDTR;
999+
msg->len = (uint8_t)0x0F & can->sFIFOMailBox[rxfifo_default].RDTR;
9891000
/* Get the FMI */
990-
// msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDTR >> 8);
1001+
// msg->FMI = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDTR >> 8);
9911002
/* Get the data field */
992-
msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDLR;
993-
msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 8);
994-
msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 16);
995-
msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDLR >> 24);
996-
msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[handle].RDHR;
997-
msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 8);
998-
msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 16);
999-
msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[handle].RDHR >> 24);
1003+
msg->data[0] = (uint8_t)0xFF & can->sFIFOMailBox[rxfifo_default].RDLR;
1004+
msg->data[1] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDLR >> 8);
1005+
msg->data[2] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDLR >> 16);
1006+
msg->data[3] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDLR >> 24);
1007+
msg->data[4] = (uint8_t)0xFF & can->sFIFOMailBox[rxfifo_default].RDHR;
1008+
msg->data[5] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDHR >> 8);
1009+
msg->data[6] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDHR >> 16);
1010+
msg->data[7] = (uint8_t)0xFF & (can->sFIFOMailBox[rxfifo_default].RDHR >> 24);
10001011

10011012
/* Release the FIFO */
1002-
if (handle == CAN_FIFO0) {
1003-
/* Release FIFO0 */
1004-
can->RF0R |= CAN_RF0R_RFOM0;
1005-
} else { /* FIFONumber == CAN_FIFO1 */
1006-
/* Release FIFO1 */
1007-
can->RF1R |= CAN_RF1R_RFOM1;
1008-
}
1013+
can->RF0R |= CAN_RF0R_RFOM0;
10091014

10101015
return 1;
10111016
}
@@ -1138,6 +1143,8 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t
11381143
if (HAL_CAN_ConfigFilter(&obj->CanHandle, &sFilterConfig) == HAL_OK) {
11391144
success = 1;
11401145
}
1146+
} else if (format == CANAny) {
1147+
success = 0; // filter for CANAny is not supported by STM32, return a failure
11411148
}
11421149

11431150
return success;

0 commit comments

Comments
 (0)