|
32 | 32 |
|
33 | 33 | #include "serial_api_hal.h"
|
34 | 34 |
|
| 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 | + |
35 | 40 | int stdio_uart_inited = 0; // used in platform/mbed_board.c and platform/mbed_retarget.cpp
|
36 | 41 | serial_t stdio_uart;
|
37 | 42 |
|
@@ -367,29 +372,43 @@ void serial_baud(serial_t *obj, int baudrate)
|
367 | 372 | struct serial_s *obj_s = SERIAL_S(obj);
|
368 | 373 |
|
369 | 374 | obj_s->baudrate = baudrate;
|
| 375 | + |
370 | 376 | #if defined(LPUART1_BASE)
|
371 | 377 | /* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
|
372 | 378 | if (obj_s->uart == LPUART_1) {
|
373 |
| - /* If baudrate is lower than 9600 try to change to LSE */ |
374 | 379 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
| 380 | + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; |
| 381 | +#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE) & USE_LPUART_CLK_LSE) |
375 | 382 | if (baudrate <= 9600 && __HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
|
376 |
| - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; |
377 | 383 | PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
|
378 | 384 | 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 | + } |
383 | 388 | }
|
| 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); |
384 | 393 | if (init_uart(obj) == HAL_OK) {
|
385 | 394 | return;
|
386 | 395 | }
|
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 |
389 | 407 | PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
|
390 | 408 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
|
391 | 409 | }
|
392 | 410 | #endif /* LPUART1_BASE */
|
| 411 | + |
393 | 412 | if (init_uart(obj) != HAL_OK) {
|
394 | 413 | debug("Cannot initialize UART with baud rate %u\n", baudrate);
|
395 | 414 | }
|
|
0 commit comments