Problems with Serial ports (STM32Duino 2.4.0)

Post here first, or if you can't find a relevant section!
Post Reply
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Problems with Serial ports (STM32Duino 2.4.0)

Post by Bambo »

Hi, i'm trying to make a short program that prints to the serial monitor i have connected to an stm32 based board. The problem is that when i re-map the Serial (SerialLP1 is default) to use different pins, the serial monitor doesn't receive any information.

I have a DHT11 setup with its Rx connected to pin PA2 on the stm32, and Tx connected to PA3 on the stm32. Here is the code i'm using to print numbers but when i compile and upload it using STM ST-LINK and then manually restart the board, nothing is received by the computer's serial monitor.

Code: Select all

#include <HardwareSerial.h>
#include "stm32l4xx.h"
#include "stm32l4xx_hal.h"
#include "stm32l4xx_hal_conf.h"
#include "stm32_def.h"

void setup()
{
    Serial.setRx((uint32_t)PA3);
    Serial.setTx((uint32_t)PA2);
    Serial.begin(9600);
    Serial.println("Starting");
}

int counter = 0;
void loop()
{
    counter++;
    if (counter > 1000) {
        counter = 0;
        Serial.println(sin(counter));
    }
}
Here are the board.txt, build_opt.h, hal_conf_extra.h and Sysclock_Config.h

board.txt

Code: Select all

 #empty 
build_opt.h

Code: Select all

-DHAL_MODULE_ENABLED
-DHAL_SAI_MODULE_ENABLED
-DHAL_TIM_MODULE_ENABLED
-DHAL_ADC_MODULE_ENABLED
-DHAL_MMC_MODULE_ENABLED
-DHAL_SD_MODULE_ENABLED
-DHAL_GPIO_MODULE_ENABLED
-DHAL_EXTI_MODULE_ENABLED
-DHAL_I2C_MODULE_ENABLED
-DHAL_DMA_MODULE_ENABLED
-DHAL_RCC_MODULE_ENABLED
-DHAL_FLASH_MODULE_ENABLED
-DHAL_PWR_MODULE_ENABLED
-DHAL_CORTEX_MODULE_ENABLED
-DHAL_UART_MODULE_ENABLED
-DHAL_RTC_MODULE_ENABLED
-DENABLE_HWSERIAL2
hal_conf_extra.h

Code: Select all

#define HAL_SAI_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
#define USE_FULL_ASSERT
#define HAL_ADC_MODULE_ENABLED
#define HAL_MODULE_ENABLED
#define HAL_MMC_MODULE_ENABLED
#define HAL_SD_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
#define HAL_EXTI_MODULE_ENABLED
#define HAL_I2C_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_PWR_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
#define HAL_RTC_MODULE_ENABLED
Sysclock_Config.h

Code: Select all

#pragma once

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

    /* Configure LSE Drive Capability */
    HAL_PWR_EnableBkUpAccess();
    __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
    /* Initializes the CPU, AHB and APB busses clocks */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_MSI | RCC_OSCILLATORTYPE_HSI48;
    RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    RCC_OscInitStruct.MSIState = RCC_MSI_ON;
    RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
    RCC_OscInitStruct.MSICalibrationValue = 0;
    RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
    RCC_OscInitStruct.PLL.PLLM = 1;
    RCC_OscInitStruct.PLL.PLLN = 40;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
    RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
    RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }
    /* Initializes the CPU, AHB and APB busses 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_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
        Error_Handler();
    }

    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SDMMC1;
    PeriphClkInit.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_PLLSAI1;
    PeriphClkInit.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1;
    PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
    PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
    PeriphClkInit.PLLSAI1.PLLSAI1N = 16;
    PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV17;
    PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV4;
    PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
    PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK | RCC_PLLSAI1_ADC1CLK | RCC_PLLSAI1_48M2CLK;

    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
        Error_Handler();
    }

    /* Configure the main internal regulator output voltage */
    if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) {
        Error_Handler();
    }

    /* Enable MSI Auto calibration */
    HAL_RCCEx_EnableMSIPLLMode();
}
Thank you for reading, hopefully you can help me figure this out!
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by fpiSTM »

Which board ?
Why boards.txt is empty?
Why enable all HAL module using both build.opt and hal extra?
Why include all those headers which are already include by default?
Is USB enabled? Which options?
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by Bambo »

Which board?
I'm using a home made board with an STM32L452RE onboard which was working great with previous versions, but now doesn't want to work after upgrading to 2.4.0.
Why boards.txt is empty?
The local boards.txt is empty because i don't know what should be there.
Why enable all HAL module using both build.opt and hal extra?
I did this because I am not sure whether the build_opt.h or the hal_conf_extra.h is the right one to use.
Why include all those headers which are already include by default?
I have had errors with the compiler unable to find definitions so i sometimes have to manually provide them.


Here are the settings for the USB and more.
CmGFmuSo0A.png
CmGFmuSo0A.png (26.79 KiB) Viewed 1156 times
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by fpiSTM »

Looking at the Nucleo L452RE definition, by default Serial is mapped on SerialLP1 using PA2, PA3. Your code does not change this as you simply re-set the same pins so it will always use the LPUART1.

https://github.com/stm32duino/Arduino_C ... #L123-L134

So if it not work, pay attention to your system_cor_clock_config as the LPUART1 have some constraints about clocking.

Edit: Tested your clock config and it works as expected.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by fpiSTM »

I guess this topic is linked to this (your?) GitHub issue: https://github.com/stm32duino/Arduino_C ... ssues/1903
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by fpiSTM »

Finally, having this define:
-DENABLE_HWSERIAL2

Lead to have 2 instances on the same pins as PA2 and PA3 have both IP: LPUART1 and UART2.
To sum up, in this case:
Serial is mapped to SerialLP
Serial2 is defined (due to ENABLE_HWSERIAL2) and uses the same pins PA2/PA3 as it is initialized with the first pins found in the map.
So to force to use an other pins you have to define
PIN_SERIAL2_RX
PIN_SERIAL2_TX
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by Bambo »

fpiSTM wrote: Wed Feb 01, 2023 1:18 pm Looking at the Nucleo L452RE definition, by default Serial is mapped on SerialLP1 using PA2, PA3. Your code does not change this as you simply re-set the same pins so it will always use the LPUART1.

https://github.com/stm32duino/Arduino_C ... #L123-L134

So if it not work, pay attention to your system_cor_clock_config as the LPUART1 have some constraints about clocking.

Edit: Tested your clock config and it works as expected.
Thank you fpiSTM.
fpiSTM wrote: Wed Feb 01, 2023 1:27 pm I guess this topic is linked to this (your?) GitHub issue: https://github.com/stm32duino/Arduino_C ... ssues/1903
Yeah this issue is linked to that one.
fpiSTM wrote: Wed Feb 01, 2023 1:36 pm Finally, having this define:
-DENABLE_HWSERIAL2

Lead to have 2 instances on the same pins as PA2 and PA3 have both IP: LPUART1 and UART2.
To sum up, in this case:
Serial is mapped to SerialLP
Serial2 is defined (due to ENABLE_HWSERIAL2) and uses the same pins PA2/PA3 as it is initialized with the first pins found in the map.
So to force to use an other pins you have to define
PIN_SERIAL2_RX
PIN_SERIAL2_TX
I have removed -GENABLE_HWSERIAL2 from the build_opt.txt but unfortunatley there is still no serial data printed to the monitor running. I am querying the hardware developers to see if there are any issues with the new changes.

Edit:

I have added a few changes to my sysclock config function

Code: Select all

    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
    PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
But the lpuart1 is still not working properly.

Edit 2:

I am looking through the source code for the HardwareSerial class and can't seem to find any code which relates to the initialization sequences i have found here https://github.com/STMicroelectronics/S ... _msp.c#L49
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by Bambo »

Update: I managed to get the debugger working again and found that my Sysclock_Config() function was not being called on startup since it was being missed from compilation. Adding #include "Sysclock_Config.h" (which contains the function definition) the correct initialization code now runs.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Problems with Serial ports (STM32Duino 2.4.0)

Post by fpiSTM »

That sound logical that user header file not included by default. As it is a function I would rename it .c.
Post Reply

Return to “General discussion”