Skip to content

Commit 45b4d41

Browse files
authored
Merge pull request #4329 from adustm/can_sync_error
Fix for #3863: STM Check can sync error
2 parents 9c6a068 + 6770678 commit 45b4d41

File tree

8 files changed

+39
-9
lines changed

8 files changed

+39
-9
lines changed

targets/TARGET_STM/TARGET_STM32F0/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32f0xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/TARGET_STM32F1/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32f1xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/TARGET_STM32F2/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32f2xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/TARGET_STM32F3/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32f3xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/TARGET_STM32F4/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32f4xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/TARGET_STM32F7/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32f7xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/TARGET_STM32L4/can_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20+
#include "stm32l4xx_hal.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {

targets/TARGET_STM/can_api.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
8585
error("Cannot initialize CAN");
8686
}
8787

88-
// Set initial CAN frequency to requested kb/s
89-
can_frequency(obj, hz);
88+
// Set initial CAN frequency to 100 kb/s
89+
if (can_frequency(obj, 100000) != 1) {
90+
error("Can frequency could not be set\n");
91+
}
9092

9193
uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14;
9294
can_filter(obj, 0, 0, CANStandard, filter_number);
@@ -195,19 +197,40 @@ int can_frequency(can_t *obj, int f)
195197
int pclk = HAL_RCC_GetPCLK1Freq();
196198
int btr = can_speed(pclk, (unsigned int)f, 1);
197199
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
200+
uint32_t tickstart = 0;
201+
int status = 1;
198202

199203
if (btr > 0) {
200204
can->MCR |= CAN_MCR_INRQ ;
205+
/* Get tick */
206+
tickstart = HAL_GetTick();
201207
while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
208+
if ((HAL_GetTick() - tickstart) > 2) {
209+
status = 0;
210+
break;
211+
}
202212
}
203-
can->BTR = btr;
204-
can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
205-
while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
213+
if (status != 0) {
214+
can->BTR = btr;
215+
can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
216+
/* Get tick */
217+
tickstart = HAL_GetTick();
218+
while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
219+
if ((HAL_GetTick() - tickstart) > 2) {
220+
status = 0;
221+
break;
222+
}
223+
}
224+
if (status == 0) {
225+
error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR & 0xFFFF0000) >> 16, (can->ESR & 0xFFFF), status);
226+
}
227+
} else {
228+
error("can init request timeout\n");
206229
}
207-
return 1;
208230
} else {
209-
return 0;
231+
status = 0;
210232
}
233+
return status;
211234
}
212235

213236
int can_write(can_t *obj, CAN_Message msg, int cc)
@@ -243,8 +266,8 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
243266
((uint32_t)msg.data[1] << 8) |
244267
((uint32_t)msg.data[0]));
245268
can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) |
246-
((uint32_t)msg.data[6] << 16) |
247-
((uint32_t)msg.data[5] << 8) |
269+
((uint32_t)msg.data[6] << 16) |
270+
((uint32_t)msg.data[5] << 8) |
248271
((uint32_t)msg.data[4]));
249272
/* Request transmission */
250273
can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;

0 commit comments

Comments
 (0)