Skip to content

Commit cefae22

Browse files
authored
Merge pull request #3492 from Nodraak/fix/3463/can_read_return_value
Fix #3463 CAN read() return value
2 parents ddcd3ad + 885b018 commit cefae22

File tree

7 files changed

+56
-14
lines changed

7 files changed

+56
-14
lines changed

targets/TARGET_STM/TARGET_STM32F0/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
235235

236236
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
237237

238+
// check FPM0 which holds the pending message count in FIFO 0
239+
// if no message is pending, return 0
240+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
241+
return 0;
242+
}
243+
238244
/* Get the Id */
239245
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
240246
if (!msg->format) {
@@ -261,10 +267,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
261267
/* Release the FIFO */
262268
if (handle == CAN_FIFO0) {
263269
/* Release FIFO0 */
264-
can->RF0R = CAN_RF0R_RFOM0;
270+
can->RF0R |= CAN_RF0R_RFOM0;
265271
} else { /* FIFONumber == CAN_FIFO1 */
266272
/* Release FIFO1 */
267-
can->RF1R = CAN_RF1R_RFOM1;
273+
can->RF1R |= CAN_RF1R_RFOM1;
268274
}
269275

270276
return 1;

targets/TARGET_STM/TARGET_STM32F1/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
238238

239239
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
240240

241+
// check FPM0 which holds the pending message count in FIFO 0
242+
// if no message is pending, return 0
243+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
244+
return 0;
245+
}
246+
241247
/* Get the Id */
242248
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
243249
if (!msg->format) {
@@ -264,10 +270,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
264270
/* Release the FIFO */
265271
if(handle == CAN_FIFO0) {
266272
/* Release FIFO0 */
267-
can->RF0R = CAN_RF0R_RFOM0;
273+
can->RF0R |= CAN_RF0R_RFOM0;
268274
} else { /* FIFONumber == CAN_FIFO1 */
269275
/* Release FIFO1 */
270-
can->RF1R = CAN_RF1R_RFOM1;
276+
can->RF1R |= CAN_RF1R_RFOM1;
271277
}
272278

273279
return 1;

targets/TARGET_STM/TARGET_STM32F2/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
248248

249249
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
250250

251+
// check FPM0 which holds the pending message count in FIFO 0
252+
// if no message is pending, return 0
253+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
254+
return 0;
255+
}
256+
251257
/* Get the Id */
252258
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
253259
if (!msg->format) {
@@ -274,10 +280,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
274280
/* Release the FIFO */
275281
if (handle == CAN_FIFO0) {
276282
/* Release FIFO0 */
277-
can->RF0R = CAN_RF0R_RFOM0;
283+
can->RF0R |= CAN_RF0R_RFOM0;
278284
} else { /* FIFONumber == CAN_FIFO1 */
279285
/* Release FIFO1 */
280-
can->RF1R = CAN_RF1R_RFOM1;
286+
can->RF1R |= CAN_RF1R_RFOM1;
281287
}
282288

283289
return 1;

targets/TARGET_STM/TARGET_STM32F3/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
238238

239239
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
240240

241+
// check FPM0 which holds the pending message count in FIFO 0
242+
// if no message is pending, return 0
243+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
244+
return 0;
245+
}
246+
241247
/* Get the Id */
242248
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
243249
if (!msg->format) {
@@ -264,10 +270,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
264270
/* Release the FIFO */
265271
if(handle == CAN_FIFO0) {
266272
/* Release FIFO0 */
267-
can->RF0R = CAN_RF0R_RFOM0;
273+
can->RF0R |= CAN_RF0R_RFOM0;
268274
} else { /* FIFONumber == CAN_FIFO1 */
269275
/* Release FIFO1 */
270-
can->RF1R = CAN_RF1R_RFOM1;
276+
can->RF1R |= CAN_RF1R_RFOM1;
271277
}
272278

273279
return 1;

targets/TARGET_STM/TARGET_STM32F4/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
251251

252252
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
253253

254+
// check FPM0 which holds the pending message count in FIFO 0
255+
// if no message is pending, return 0
256+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
257+
return 0;
258+
}
259+
254260
/* Get the Id */
255261
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
256262
if (!msg->format) {
@@ -277,10 +283,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
277283
/* Release the FIFO */
278284
if(handle == CAN_FIFO0) {
279285
/* Release FIFO0 */
280-
can->RF0R = CAN_RF0R_RFOM0;
286+
can->RF0R |= CAN_RF0R_RFOM0;
281287
} else { /* FIFONumber == CAN_FIFO1 */
282288
/* Release FIFO1 */
283-
can->RF1R = CAN_RF1R_RFOM1;
289+
can->RF1R |= CAN_RF1R_RFOM1;
284290
}
285291

286292
return 1;

targets/TARGET_STM/TARGET_STM32F7/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
251251

252252
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
253253

254+
// check FPM0 which holds the pending message count in FIFO 0
255+
// if no message is pending, return 0
256+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
257+
return 0;
258+
}
259+
254260
/* Get the Id */
255261
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
256262
if (!msg->format) {
@@ -277,10 +283,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
277283
/* Release the FIFO */
278284
if(handle == CAN_FIFO0) {
279285
/* Release FIFO0 */
280-
can->RF0R = CAN_RF0R_RFOM0;
286+
can->RF0R |= CAN_RF0R_RFOM0;
281287
} else { /* FIFONumber == CAN_FIFO1 */
282288
/* Release FIFO1 */
283-
can->RF1R = CAN_RF1R_RFOM1;
289+
can->RF1R |= CAN_RF1R_RFOM1;
284290
}
285291

286292
return 1;

targets/TARGET_STM/TARGET_STM32L4/can_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
238238

239239
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
240240

241+
// check FPM0 which holds the pending message count in FIFO 0
242+
// if no message is pending, return 0
243+
if ((can->RF0R & CAN_RF0R_FMP0) == 0) {
244+
return 0;
245+
}
246+
241247
/* Get the Id */
242248
msg->format = (CANFormat)((uint8_t)0x04 & can->sFIFOMailBox[handle].RIR);
243249
if (!msg->format) {
@@ -264,10 +270,10 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
264270
/* Release the FIFO */
265271
if(handle == CAN_FIFO0) {
266272
/* Release FIFO0 */
267-
can->RF0R = CAN_RF0R_RFOM0;
273+
can->RF0R |= CAN_RF0R_RFOM0;
268274
} else { /* FIFONumber == CAN_FIFO1 */
269275
/* Release FIFO1 */
270-
can->RF1R = CAN_RF1R_RFOM1;
276+
can->RF1R |= CAN_RF1R_RFOM1;
271277
}
272278

273279
return 1;

0 commit comments

Comments
 (0)