stm32/uart: Rework uart_get_baudrate so it doesn't need a UART handle.

This commit is contained in:
Damien George 2018-12-09 22:51:25 +11:00
parent 524e13b006
commit 9690757cca
1 changed files with 47 additions and 25 deletions

View File

@ -411,34 +411,56 @@ uint32_t uart_get_baudrate(pyb_uart_obj_t *self) {
#if defined(STM32F0) #if defined(STM32F0)
uart_clk = HAL_RCC_GetPCLK1Freq(); uart_clk = HAL_RCC_GetPCLK1Freq();
#elif defined(STM32F7) || defined(STM32H7) #elif defined(STM32F7)
UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED; switch ((RCC->DCKCFGR2 >> ((self->uart_id - 1) * 2)) & 3) {
UART_GETCLOCKSOURCE(&self->uart, clocksource); case 0:
switch (clocksource) { if (self->uart_id == 1 || self->uart_id == 6) {
#if defined(STM32H7) uart_clk = HAL_RCC_GetPCLK2Freq();
case UART_CLOCKSOURCE_D2PCLK1: uart_clk = HAL_RCC_GetPCLK1Freq(); break; } else {
case UART_CLOCKSOURCE_D3PCLK1: uart_clk = HAL_RCC_GetPCLK1Freq(); break; uart_clk = HAL_RCC_GetPCLK1Freq();
case UART_CLOCKSOURCE_D2PCLK2: uart_clk = HAL_RCC_GetPCLK2Freq(); break; }
#else break;
case UART_CLOCKSOURCE_PCLK1: uart_clk = HAL_RCC_GetPCLK1Freq(); break; case 1:
case UART_CLOCKSOURCE_PCLK2: uart_clk = HAL_RCC_GetPCLK2Freq(); break; uart_clk = HAL_RCC_GetSysClockFreq();
case UART_CLOCKSOURCE_SYSCLK: uart_clk = HAL_RCC_GetSysClockFreq(); break; break;
#endif case 2:
#if defined(STM32H7) uart_clk = HSI_VALUE;
case UART_CLOCKSOURCE_CSI: uart_clk = CSI_VALUE; break; break;
#endif case 3:
case UART_CLOCKSOURCE_HSI: uart_clk = HSI_VALUE; break; uart_clk = LSE_VALUE;
case UART_CLOCKSOURCE_LSE: uart_clk = LSE_VALUE; break; break;
#if defined(STM32H7) }
case UART_CLOCKSOURCE_PLL2: #elif defined(STM32H7)
case UART_CLOCKSOURCE_PLL3: uint32_t csel;
#endif if (self->uart_id == 1 || self->uart_id == 6) {
case UART_CLOCKSOURCE_UNDEFINED: break; csel = RCC->D2CCIP2R >> 3;
} else {
csel = RCC->D2CCIP2R;
}
switch (csel & 3) {
case 0:
if (self->uart_id == 1 || self->uart_id == 6) {
uart_clk = HAL_RCC_GetPCLK2Freq();
} else {
uart_clk = HAL_RCC_GetPCLK1Freq();
}
break;
case 3:
uart_clk = HSI_VALUE;
break;
case 4:
uart_clk = CSI_VALUE;
break;
case 5:
uart_clk = LSE_VALUE;
break;
default:
break;
} }
#else #else
if (self->uart.Instance == USART1 if (self->uart_id == 1
#if defined(USART6) #if defined(USART6)
|| self->uart.Instance == USART6 || self->uart_id == 6
#endif #endif
) { ) {
uart_clk = HAL_RCC_GetPCLK2Freq(); uart_clk = HAL_RCC_GetPCLK2Freq();