AnsweredAssumed Answered

STM32L051C6T6 USART buard rate not correct when using MSI

Question asked by blesener.daniel on Aug 14, 2015
Latest reply on Aug 15, 2015 by Clive One
I have USART1/2 working fine when using HSI. When using MSI clock (code below) I do not see the correct output frequency on the scope.

I am setting the MSI to 2.097MHz. I measured this as 2.31MHz from MCO. 
I am setting USART1 to 115200. The BRR reg states 17 which should give me 2130000/18 = 118K. 

When measuring the usart signal on a scope I see about 101K.Very confusing. Let me know if you see any errors or have any pointers. Thanks.

  HAL_RCC_DeInit();
    
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;


  __PWR_CLK_ENABLE();


  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);


  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  //RCC_OscInitStruct.MSICalibrationValue = ?;//This is automatically loaded with calibration value from factor at 25C - change if temp changes
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);


  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  //APB2 must be at least as fast as APB1
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);


  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);


  //HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/100);


  //HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  
  SystemCoreClockUpdate();
  
  __CRC_CLK_ENABLE();
}

//////////////////////////////////////USART///////////////////////////////////////////
    __USART1_CLK_ENABLE();                          // Enable USART 1 clock


    husart1.Instance                                 = USART1;
    husart1.Init.BaudRate                            = 115200;
    husart1.Init.WordLength                          = UART_WORDLENGTH_8B;
    husart1.Init.StopBits                            = UART_STOPBITS_1;
    husart1.Init.Parity                              = UART_PARITY_NONE;
    husart1.Init.Mode                                = UART_MODE_TX_RX;
    husart1.Init.HwFlowCtl                           = UART_HWCONTROL_NONE;
    husart1.Init.OverSampling                        = UART_OVERSAMPLING_16;
    husart1.AdvancedInit.AdvFeatureInit              = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT;
    husart1.AdvancedInit.OverrunDisable              = UART_ADVFEATURE_OVERRUN_DISABLE;
    husart1.AdvancedInit.DMADisableonRxError         = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
    
    HAL_UART_Init(&husart1);                        // Initializes the USART1 according to the specified parameters above and creates the associated handle.


    //husart1.Instance->BRR = 0xB6;


     /* Enable the USART1 Receive interrupt: this interrupt is generated when the USART1 receive data register is not empty */
    __HAL_UART_ENABLE_IT(&husart1, UART_IT_RXNE);


    __HAL_UART_DISABLE_IT(&husart1, UART_IT_TC);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_WUF);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_CM);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_CTS);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_LBD);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_TXE);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_IDLE);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_PE);
    __HAL_UART_DISABLE_IT(&husart1, UART_IT_ERR);

Outcomes