Page 1 of 1

STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 11:58 am
by meold
Hi, I'm trying to migrate from 1.9.0 to 2.4.0 arduino core and have an issue with usart.
Initialization:

Code: Select all

#define TX_PIN                                PA15
#define RX_PIN                                PB3
HardwareSerial hws(RX_PIN, TX_PIN);
const char data[] = {0xAA, 0x00, 0xFF};

void setup()
{
  Serial.begin(115200);
  hws.begin(19200, SERIAL_8N1);
}
Loop:

Code: Select all

void loop()
{
  delay(1000);
  hws.write(data, 3);
}
As a result I'm getting garbage:
Capture.JPG
Capture.JPG (70.65 KiB) Viewed 1890 times
All the possible actions like speed, mode, pinout and other already checked. In the variant all is correctly configured. Same code correctly works on 1.9.0 core.

It is strange, that received bytes count is not equals to bytes sent.

Re: STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 3:02 pm
by fpiSTM
There is no F411VE variant defined. So, I wonder which variant you used?
You should check the pins you used and if the default Serial is not mapped on the same one.

Re: STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 3:36 pm
by meold
Hi,

Yes, you are right - by default there is no variant for F411VE available, but it is actually exists in variant files via path "variants\STM32F4xx\F411V(C-E)T", so I've added it according to the tutorial.

I'm not sure how correctly check to which pins mapped default Serial, but anyway it used for USB CDC communication.
Anyway, here is my part for pins config related to uarts:

Code: Select all

#ifdef HAL_UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_TX[] = {
  {PA_2,  USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
  {PA_9,  USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
  {PA_11, USART6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF8_USART6)},
  {PA_15, USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
  {PB_6,  USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
  {PC_6,  USART6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF8_USART6)},
  {PD_5,  USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
  {NC,    NP,     0}
};
#endif

#ifdef HAL_UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_RX[] = {
  {PA_3,  USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
  {PA_10, USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
  {PA_12, USART6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF8_USART6)},
  {PB_3,  USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
  {PB_7,  USART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
  {PC_7,  USART6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF8_USART6)},
  {PD_6,  USART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
  {NC,    NP,     0}
};
#endif
And here is the clock config:

Code: Select all

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

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __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_HSE|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 6;
  RCC_OscInitStruct.PLL.PLLN = 96;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  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_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

Re: STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 4:24 pm
by fpiSTM
OK. That's fine if you declare it.
Pay attention for USB, it requires 48MHz input clock.

I saw that you used HSE. My guess is that the value of this HSE is not the default one defined by STM32 HAL:
https://github.com/stm32duino/Arduino_C ... h#L99-L101

which is 8MHz. So if your HSE has a different value you have to redefine it else the systemCoreClock frequency is not correct which could explain your issue with serial as it probably impact baudrate.

Re: STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 5:22 pm
by meold
Hi,

Yes, you are right - my crystal have 12MHz frequency and I missed this. :roll:

The fix is pretty simple - just by adding to the boards.txt for appropriate board/variant next config:

Code: Select all

xxx.build.extra_flags=-DHSE_VALUE=12000000
Thank you for your help :)

Re: STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 6:38 pm
by fpiSTM
Welcome. You can also change it using build_opt.h feature.
https://github.com/stm32duino/wiki/wiki ... uild_opt.h

Re: STM32F411VE HardwareSerial sends garbage

Posted: Wed Feb 22, 2023 6:41 pm
by fpiSTM
meold wrote: Wed Feb 22, 2023 5:22 pm
Yes, you are right - my crystal have 12MHz frequency and I missed this. :roll:
No worry. It is not easy. This is an advanced configuration. Sometimes I forgot this too.

Re: STM32F411VE HardwareSerial sends garbage

Posted: Tue Jul 25, 2023 2:33 am
by bevis0405
If you are using interrupts for the UART, make sure that the interrupt handlers are correctly implemented to prevent data loss or corruption.

Re: STM32F411VE HardwareSerial sends garbage

Posted: Thu Sep 07, 2023 3:59 am
by bekean
If your HSE is different, you must modify it else the systemCoreClock frequency is incorrect, which may be the cause of your serial communication problems since it likely affects baudrate.

Re: STM32F411VE HardwareSerial sends garbage

Posted: Sat Sep 09, 2023 3:03 am
by jesse99
bevis0405 wrote: Tue Jul 25, 2023 2:33 am If you are using interrupts for the UART, make sure that the interrupt handlers are correctly implemented to prevent data loss or corruption.
I am not using interrupts for the UART, this method is not worth trying.