Skip to content

Commit 9be8541

Browse files
committed
STM32: add lpuart_clock_source config
Keep same clock configuration as done before this PR (LSE and PCLK1). Use a JSON file to change it.
1 parent 2be3c13 commit 9be8541

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

targets/TARGET_STM/serial_api.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232

3333
#include "serial_api_hal.h"
3434

35+
// Possible choices of the LPUART_CLOCK_SOURCE configuration set in json file
36+
#define USE_LPUART_CLK_LSE 0x01
37+
#define USE_LPUART_CLK_PCLK1 0x02
38+
#define USE_LPUART_CLK_HSI 0x04
39+
3540
int stdio_uart_inited = 0; // used in platform/mbed_board.c and platform/mbed_retarget.cpp
3641
serial_t stdio_uart;
3742

@@ -367,29 +372,43 @@ void serial_baud(serial_t *obj, int baudrate)
367372
struct serial_s *obj_s = SERIAL_S(obj);
368373

369374
obj_s->baudrate = baudrate;
375+
370376
#if defined(LPUART1_BASE)
371377
/* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
372378
if (obj_s->uart == LPUART_1) {
373-
/* If baudrate is lower than 9600 try to change to LSE */
374379
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
380+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
381+
#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_LSE)
375382
if (baudrate <= 9600 && __HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
376-
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
377383
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
378384
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
379-
} else {
380-
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
381-
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
382-
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
385+
if (init_uart(obj) == HAL_OK) {
386+
return;
387+
}
383388
}
389+
#endif
390+
#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_PCLK1)
391+
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
392+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
384393
if (init_uart(obj) == HAL_OK) {
385394
return;
386395
}
387-
/* Change LPUART clock source and try again */
388-
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
396+
#endif
397+
#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_HSI)
398+
if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
399+
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
400+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
401+
if (init_uart(obj) == HAL_OK) {
402+
return;
403+
}
404+
}
405+
#endif
406+
// Last chance using SYSCLK
389407
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
390408
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
391409
}
392410
#endif /* LPUART1_BASE */
411+
393412
if (init_uart(obj) != HAL_OK) {
394413
debug("Cannot initialize UART with baud rate %u\n", baudrate);
395414
}

targets/targets.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@
730730
"help": "Define if a Low Speed External xtal (LSE) is available on the board (0 = No, 1 = Yes). If Yes, the LSE will be used to clock the RTC, LPUART, ... otherwise the Low Speed Internal clock (LSI) will be used",
731731
"value": "1"
732732
},
733+
"lpuart_clock_source": {
734+
"help": "Define the LPUART clock source. Mask values: USE_LPUART_CLK_LSE, USE_LPUART_CLK_PCLK1, USE_LPUART_CLK_HSI",
735+
"value": "USE_LPUART_CLK_LSE|USE_LPUART_CLK_PCLK1"
736+
},
733737
"stdio_uart_tx": {
734738
"help": "default TX STDIO pins is defined in PinNames.h file, but it can be overridden"
735739
},

0 commit comments

Comments
 (0)