Skip to content

Commit 04ac65a

Browse files
committed
STM32: CAN: restore registers after can_reset
After reset the MCR register content needs to be restored so we're introducing the can_registers_init function to be called at the first init stage, but also after reset. We also store the can frequency to go through the initialisation phase again.
1 parent 8357f6f commit 04ac65a

File tree

8 files changed

+29
-8
lines changed

8 files changed

+29
-8
lines changed

targets/TARGET_STM/TARGET_STM32F0/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct dac_s {
132132
struct can_s {
133133
CAN_HandleTypeDef CanHandle;
134134
int index;
135+
int hz;
135136
};
136137
#endif
137138

targets/TARGET_STM/TARGET_STM32F1/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ struct analogin_s {
120120
struct can_s {
121121
CAN_HandleTypeDef CanHandle;
122122
int index;
123+
int hz;
123124
};
124125
#endif
125126

targets/TARGET_STM/TARGET_STM32F2/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct pwmout_s {
139139
struct can_s {
140140
CAN_HandleTypeDef CanHandle;
141141
int index;
142+
int hz;
142143
};
143144

144145
#define GPIO_IP_WITHOUT_BRR

targets/TARGET_STM/TARGET_STM32F3/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ struct analogin_s {
128128
struct can_s {
129129
CAN_HandleTypeDef CanHandle;
130130
int index;
131+
int hz;
131132
};
132133
#endif
133134

targets/TARGET_STM/TARGET_STM32F4/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct dac_s {
137137
struct can_s {
138138
CAN_HandleTypeDef CanHandle;
139139
int index;
140+
int hz;
140141
};
141142
#endif
142143

targets/TARGET_STM/TARGET_STM32F7/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct flash_s {
136136
struct can_s {
137137
CAN_HandleTypeDef CanHandle;
138138
int index;
139+
int hz;
139140
};
140141
#endif
141142

targets/TARGET_STM/TARGET_STM32L4/common_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ struct dac_s {
139139
struct can_s {
140140
CAN_HandleTypeDef CanHandle;
141141
int index;
142+
int hz;
142143
};
143144
#endif
144145

targets/TARGET_STM/can_api.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
static uint32_t can_irq_ids[CAN_NUM] = {0};
2929
static can_irq_handler irq_handler;
3030

31+
static void can_registers_init(can_t *obj)
32+
{
33+
if (HAL_CAN_Init(&obj->CanHandle) != HAL_OK) {
34+
error("Cannot initialize CAN");
35+
}
36+
37+
// Set initial CAN frequency to specified frequency
38+
if (can_frequency(obj, obj->hz) != 1) {
39+
error("Can frequency could not be set\n");
40+
}
41+
}
42+
3143
void can_init(can_t *obj, PinName rd, PinName td)
3244
{
3345
can_init_freq(obj, rd, td, 100000);
@@ -66,8 +78,8 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
6678
pin_mode(td, PullUp);
6779
}
6880

81+
/* Use default values for rist init */
6982
obj->CanHandle.Instance = (CAN_TypeDef *)can;
70-
7183
obj->CanHandle.Init.TTCM = DISABLE;
7284
obj->CanHandle.Init.ABOM = DISABLE;
7385
obj->CanHandle.Init.AWUM = DISABLE;
@@ -80,19 +92,16 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
8092
obj->CanHandle.Init.BS2 = CAN_BS2_8TQ;
8193
obj->CanHandle.Init.Prescaler = 2;
8294

83-
if (HAL_CAN_Init(&obj->CanHandle) != HAL_OK) {
84-
error("Cannot initialize CAN");
85-
}
95+
/* Store frequency to be restored in case of reset */
96+
obj->hz = hz;
8697

87-
// Set initial CAN frequency to specified frequency
88-
if (can_frequency(obj, hz) != 1) {
89-
error("Can frequency could not be set\n");
90-
}
98+
can_registers_init(obj);
9199

92100
uint32_t filter_number = (can == CAN_1) ? 0 : 14;
93101
can_filter(obj, 0, 0, CANStandard, filter_number);
94102
}
95103

104+
96105
void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
97106
{
98107
irq_handler = handler;
@@ -329,8 +338,13 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
329338
void can_reset(can_t *obj)
330339
{
331340
CAN_TypeDef *can = obj->CanHandle.Instance;
341+
342+
/* Reset IP and delete errors */
332343
can->MCR |= CAN_MCR_RESET;
333344
can->ESR = 0x0;
345+
346+
/* restore registers state as saved in obj context */
347+
can_registers_init(obj);
334348
}
335349

336350
unsigned char can_rderror(can_t *obj)

0 commit comments

Comments
 (0)