@@ -163,12 +163,23 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
163
163
obj -> CanHandle .Init .DataSyncJumpWidth = 0x1 ; // Not used - only in FDCAN
164
164
obj -> CanHandle .Init .DataTimeSeg1 = 0x1 ; // Not used - only in FDCAN
165
165
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 */
167
168
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
168
181
#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
172
183
obj -> CanHandle .Init .RxFifo0ElmtsNbr = 8 ;
173
184
obj -> CanHandle .Init .RxFifo0ElmtSize = FDCAN_DATA_BYTES_8 ;
174
185
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
180
191
obj -> CanHandle .Init .TxFifoQueueElmtsNbr = 3 ;
181
192
#endif
182
193
obj -> CanHandle .Init .TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION ;
183
- #ifndef TARGET_STM32G4
194
+ #ifdef TARGET_STM32H7
184
195
obj -> CanHandle .Init .TxElmtSize = FDCAN_DATA_BYTES_8 ;
185
196
#endif
186
197
can_internal_init (obj );
@@ -329,20 +340,18 @@ int can_frequency(can_t *obj, int f)
329
340
*/
330
341
int can_filter (can_t * obj , uint32_t id , uint32_t mask , CANFormat format , int32_t handle )
331
342
{
332
- UNUSED (handle ); // Not supported yet (seems to be a used in read function?)
333
-
334
343
FDCAN_FilterTypeDef sFilterConfig = {0 };
335
344
336
345
if (format == CANStandard ) {
337
346
sFilterConfig .IdType = FDCAN_STANDARD_ID ;
338
- sFilterConfig .FilterIndex = 0 ;
347
+ sFilterConfig .FilterIndex = handle ;
339
348
sFilterConfig .FilterType = FDCAN_FILTER_MASK ;
340
349
sFilterConfig .FilterConfig = FDCAN_FILTER_TO_RXFIFO0 ;
341
350
sFilterConfig .FilterID1 = id ;
342
351
sFilterConfig .FilterID2 = mask ;
343
352
} else if (format == CANExtended ) {
344
353
sFilterConfig .IdType = FDCAN_EXTENDED_ID ;
345
- sFilterConfig .FilterIndex = 0 ;
354
+ sFilterConfig .FilterIndex = handle ;
346
355
sFilterConfig .FilterType = FDCAN_FILTER_MASK ;
347
356
sFilterConfig .FilterConfig = FDCAN_FILTER_TO_RXFIFO0 ;
348
357
sFilterConfig .FilterID1 = id ;
@@ -391,7 +400,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
391
400
392
401
int can_read (can_t * obj , CAN_Message * msg , int handle )
393
402
{
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.
395
404
396
405
if (HAL_FDCAN_GetRxFifoFillLevel (& obj -> CanHandle , FDCAN_RX_FIFO0 ) == 0 ) {
397
406
return 0 ; // No message arrived
@@ -655,6 +664,8 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
655
664
#include <string.h>
656
665
#include <inttypes.h>
657
666
667
+ #define DEFAULT_RXFIFO 0 // default rx fifo for can by hardware is FIFO0
668
+
658
669
static uint32_t can_irq_ids [CAN_NUM ] = {0 };
659
670
static can_irq_handler irq_handler ;
660
671
@@ -965,8 +976,8 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
965
976
966
977
int can_read (can_t * obj , CAN_Message * msg , int handle )
967
978
{
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 ;
970
981
CAN_TypeDef * can = obj -> CanHandle .Instance ;
971
982
972
983
// 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)
976
987
}
977
988
978
989
/* 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 );
980
991
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 );
982
993
} else {
983
- msg -> id = (uint32_t )0x1FFFFFFF & (can -> sFIFOMailBox [handle ].RIR >> 3 );
994
+ msg -> id = (uint32_t )0x1FFFFFFF & (can -> sFIFOMailBox [rxfifo_default ].RIR >> 3 );
984
995
}
985
996
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 );
987
998
/* Get the DLC */
988
- msg -> len = (uint8_t )0x0F & can -> sFIFOMailBox [handle ].RDTR ;
999
+ msg -> len = (uint8_t )0x0F & can -> sFIFOMailBox [rxfifo_default ].RDTR ;
989
1000
/* 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);
991
1002
/* 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 );
1000
1011
1001
1012
/* 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 ;
1009
1014
1010
1015
return 1 ;
1011
1016
}
@@ -1138,6 +1143,8 @@ int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t
1138
1143
if (HAL_CAN_ConfigFilter (& obj -> CanHandle , & sFilterConfig ) == HAL_OK ) {
1139
1144
success = 1 ;
1140
1145
}
1146
+ } else if (format == CANAny ) {
1147
+ success = 0 ; // filter for CANAny is not supported by STM32, return a failure
1141
1148
}
1142
1149
1143
1150
return success ;
0 commit comments