Skip to content

STM32L0/4 Enable use of LPUART in stop mode #5941

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

Merged
merged 2 commits into from
Feb 12, 2018
Merged
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
54 changes: 32 additions & 22 deletions targets/TARGET_STM/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,32 +369,30 @@ void serial_baud(serial_t *obj, int baudrate)
struct serial_s *obj_s = SERIAL_S(obj);

obj_s->baudrate = baudrate;
Copy link
Contributor

@bcostm bcostm Jan 31, 2018

Choose a reason for hiding this comment

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

Not related to this PR, but maybe put back the previous baudrate if the init_uart function has returned a FAIL ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could but it still doesn't really notify you that the operation has failed. You would have to check wat the actual baudrate is after te operation to verify it succeeded.

if (init_uart(obj) != HAL_OK) {

#if defined(LPUART1_BASE)
/* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
if (obj_s->uart == LPUART_1) {
/* Try to change LPUART clock source */
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if (baudrate == 9600) {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
if (init_uart(obj) == HAL_OK){
return;
}
}
else {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
if (init_uart(obj) == HAL_OK){
return;
}
}
if (obj_s->uart == LPUART_1) {
/* If baudrate is lower than 9600 try to change to LSE */
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if (baudrate <= 9600 && __HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
} else {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
}
if (init_uart(obj) == HAL_OK) {
return;
}
/* Change LPUART clock source and try again */
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
}
#endif /* LPUART1_BASE */

if (init_uart(obj) != HAL_OK) {
debug("Cannot initialize UART with baud rate %u\n", baudrate);
}
}
Expand Down Expand Up @@ -527,6 +525,18 @@ HAL_StatusTypeDef init_uart(serial_t *obj)
huart->Init.Mode = UART_MODE_TX_RX;
}

#if defined(LPUART1_BASE)
if (huart->Instance == LPUART1) {
if (obj_s->baudrate <= 9600) {
HAL_UARTEx_EnableClockStopMode(huart);
HAL_UARTEx_EnableStopMode(huart);
} else {
HAL_UARTEx_DisableClockStopMode(huart);
HAL_UARTEx_DisableStopMode(huart);
}
}
#endif

return HAL_UART_Init(huart);
}

Expand Down