Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix STM32 CAN reset to not lose context #4932

Merged
merged 6 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
LMESTM committed Sep 5, 2017
commit 04ac65a4e6475255eccf26880f812515b420aed5
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F0/common_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct dac_s {
struct can_s {
CAN_HandleTypeDef CanHandle;
int index;
int hz;
};
#endif

Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F1/common_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct analogin_s {
struct can_s {
CAN_HandleTypeDef CanHandle;
int index;
int hz;
};
#endif

Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F2/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ struct pwmout_s {
struct can_s {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Laurent,
Could you add #if DEVICE_CAN (in case of future STM32F2 device that does not support CAN) ?

CAN_HandleTypeDef CanHandle;
int index;
int hz;
};

#define GPIO_IP_WITHOUT_BRR
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F3/common_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct analogin_s {
struct can_s {
CAN_HandleTypeDef CanHandle;
int index;
int hz;
};
#endif

Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F4/common_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ struct dac_s {
struct can_s {
CAN_HandleTypeDef CanHandle;
int index;
int hz;
};
#endif

Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F7/common_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct flash_s {
struct can_s {
CAN_HandleTypeDef CanHandle;
int index;
int hz;
};
#endif

Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32L4/common_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ struct dac_s {
struct can_s {
CAN_HandleTypeDef CanHandle;
int index;
int hz;
};
#endif

Expand Down
30 changes: 22 additions & 8 deletions targets/TARGET_STM/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
static uint32_t can_irq_ids[CAN_NUM] = {0};
static can_irq_handler irq_handler;

static void can_registers_init(can_t *obj)
{
if (HAL_CAN_Init(&obj->CanHandle) != HAL_OK) {
error("Cannot initialize CAN");
}

// Set initial CAN frequency to specified frequency
if (can_frequency(obj, obj->hz) != 1) {
error("Can frequency could not be set\n");
}
}

void can_init(can_t *obj, PinName rd, PinName td)
{
can_init_freq(obj, rd, td, 100000);
Expand Down Expand Up @@ -66,8 +78,8 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
pin_mode(td, PullUp);
}

/* Use default values for rist init */
obj->CanHandle.Instance = (CAN_TypeDef *)can;

obj->CanHandle.Init.TTCM = DISABLE;
obj->CanHandle.Init.ABOM = DISABLE;
obj->CanHandle.Init.AWUM = DISABLE;
Expand All @@ -80,19 +92,16 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
obj->CanHandle.Init.BS2 = CAN_BS2_8TQ;
obj->CanHandle.Init.Prescaler = 2;

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

// Set initial CAN frequency to specified frequency
if (can_frequency(obj, hz) != 1) {
error("Can frequency could not be set\n");
}
can_registers_init(obj);

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


void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
{
irq_handler = handler;
Expand Down Expand Up @@ -329,8 +338,13 @@ int can_read(can_t *obj, CAN_Message *msg, int handle)
void can_reset(can_t *obj)
{
CAN_TypeDef *can = obj->CanHandle.Instance;

/* Reset IP and delete errors */
can->MCR |= CAN_MCR_RESET;
can->ESR = 0x0;

/* restore registers state as saved in obj context */
can_registers_init(obj);
}

unsigned char can_rderror(can_t *obj)
Expand Down