Skip to content

[STM32F3]: fix for uart baudrate. #2099

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
9 changes: 7 additions & 2 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F3/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "pinmap.h"
#include <string.h>
#include "PeripheralPins.h"
#include "mbed_error.h"

#define UART_NUM (5)

Expand Down Expand Up @@ -69,8 +70,12 @@ static void init_uart(serial_t *obj)
// Disable the reception overrun detection
UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT;
UartHandle.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;

HAL_UART_Init(&UartHandle);
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
Copy link
Contributor

Choose a reason for hiding this comment

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

misaligned comment. Did you consider doing this as part of the HAL init mbed_sdk_init(), I assume this might be required for more than just UART.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am fixing this locally, will merge it manually

Copy link
Contributor

Choose a reason for hiding this comment

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

There are tabs, please use spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On Arm , or uARM :SystemInit is called after boot (SystemInit write SystemCoreClock and call HAL_Init), then __main is called, this will perform zero initialized of bss , recopy of initialized variable (this will change the value of SystemCoreClock), then _initio calls init_serial (which calls init_uart, init_uart works properly if SystemCoreClock is correctly sets)
Later before calling main , mbed_sdk_init is called, this will update again SystemCoreClock and do again HAL_Init) which will allow a correct usage of UART or other peripheral with HAL.

On GCC_ARM, as zero initialized of bss , recopy of initialized variable is done early after reset and before SystemInit, the value of SystemCoreClock remains un-changed after SystemInit, so with GCC_ARM no need to add this fix in init_uart or in mbed_sdk_init.

On IAR , SystemInit is called after boot (SystemInit write SystemCoreClock and call HAL_Init), then __iar_program_start, this calls __iar_data_init3 which does zero initialized of bss, recopy of initialized data (this will change the value of SystemCoreClock), then mbed_sdk_init is called (This will put the value of systemCoreClock to the correct value) and the init_uart is called at the 1st printf . On IAR the fix in init_uart is not needed.

=> The Init is different according to compilers , this leads to un-confortable fix on ARM and IAR, due the late zero initiliazed, or recopy of initialized data for ARM, IAR compiler.

/* and before HAL Init. SystemCoreClock init required here */
SystemCoreClockUpdate();
if (HAL_UART_Init(&UartHandle) != HAL_OK) {
error("Cannot initialize UART\n");
}
}

void serial_init(serial_t *obj, PinName tx, PinName rx)
Expand Down