0
\$\begingroup\$

I am using STM32L152R8T6-A MCU. I use 3 UARTs in my project. UART buadrates are 9600 and 115200 bps.

Initially I set the clock source to HSI (16MHz) and everything worked fine. But then I decided to change the clock source to MSI (2.097 or 4.194 MHz). The program runs properly but it seems that UARTs miss some bytes, i.e. instead of getting 11 bytes, it gets the last 8 bytes.

I used the STM32CubeMX configurations and didn't change anything in initializations:

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

Has anyone any ideas?

\$\endgroup\$
8
  • \$\begingroup\$ What supply voltage and temperature the STM32 runs at, and do either voltage or temperature change when it is running? \$\endgroup\$
    – Justme
    Dec 27 '20 at 8:48
  • \$\begingroup\$ @Justme Everything is the same for HSI and MSI. Supply is regulated 3.3V and temperature is normal. \$\endgroup\$
    – Iman H
    Dec 27 '20 at 9:19
  • \$\begingroup\$ How are you reading from the port? \$\endgroup\$ Dec 27 '20 at 14:14
  • \$\begingroup\$ @ChrisStratton With a very simple polling mode. No interrupt is used. \$\endgroup\$
    – Iman H
    Dec 27 '20 at 14:31
  • \$\begingroup\$ What else are you doing? Show the code \$\endgroup\$ Dec 27 '20 at 14:52
0
\$\begingroup\$

It is most likely that the intenal oscillator has just too much tolerance. They are factory calibrated at 3.0V and 25 °C, and HSI should be within 1% anf MSI should be within 0.5%.

So if you have not calibrated them to be accurate in your operating conditions, they could be out of required tolerance for UART comms.

Basically, for anything that requires good frequency precision, like UART comms, an external crystal should be used.

There is some possibility that since slower clock causes missing bytes, that it might have simply related to how your code runs at slower clock and cannot receive all bytes.

\$\endgroup\$
1
  • \$\begingroup\$ That's really rather unlikely to be the issue \$\endgroup\$ Dec 27 '20 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.