Skip to main content
gokhannsahin
Associate III
January 5, 2018
Solved

STM32L476G-Disco LSE Startup Problem

  • January 5, 2018
  • 20 replies
  • 4007 views
Posted on January 05, 2018 at 14:44

Hi everyone,

I want to use RTC module and LSE crystal which is default on board 32768KHz. I generate the code via STM32Cube because I couldn't find any example of RTC that used via LSE. After generated, the code doesn't pass the LSE ready check. I guess the reason for that issue isn't hardware because the code is running on discovery board. I have tried firmware 1.10 and 1.9 libraries for stm32l4 but the result is same. Where am I doing wrong?

#stm32l476-discovery-board #lse-startup
This topic has been closed for replies.
Best answer by Bogdan Golab
Posted on January 08, 2018 at 10:38

It could be a hw problem but your probe (osciloscope) may affect the circuit and prevents the LSE from starting anyway (during the time when the scope is connected to the circuit). I guess they use pretty low capacitors (4.7-10pF).

I want to say that LSE may does not work but connecting scope may also prevent it from starting so it i snot a prove that LSE is broken.

I can test it on my board but later after work.

20 replies

Tesla DeLorean
Guru
January 5, 2018
Posted on January 05, 2018 at 18:02

It is not working, so you'll need to keep your mind open. I would suggest checking to see if you can actually see the oscillator working, or if it just takes too long for LSERDY to signal. Check the low power domain is enabled. Check the signal by routing LSE to MCO (Pin PA8)

Dig harder for examples

STM32Cube_FW_L4_V1.10.0\Projects\STM32L432KC-Nucleo\Examples\RTC\RTC_Alarm\Src\stm32l4xx_hal_msp.c

/**

* @brief RTC MSP Initialization

* This function configures the hardware resources used in this example

* @param hrtc: RTC handle pointer

*

* @note Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select

* the RTC clock source; in this case the Backup domain will be reset in

* order to modify the RTC Clock source, as consequence RTC registers (including

* the backup registers) and RCC_BDCR register are set to their reset values.

*

* @retval None

*/

void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

/*##-1- Enables the PWR Clock and Enables access to the backup domain ###################################*/

/* To change the source clock of the RTC feature (LSE, LSI), You have to:

- Enable the power clock using __HAL_RCC_PWR_CLK_ENABLE()

- Enable write access using HAL_PWR_EnableBkUpAccess() function before to

configure the RTC clock source (to be done once after reset).

- Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and

__HAL_RCC_BACKUPRESET_RELEASE().

- Configure the needed RTC clock source */

__HAL_RCC_PWR_CLK_ENABLE();

HAL_PWR_EnableBkUpAccess();

/*##-2- Configure LSE/LSI as RTC clock source ###############################*/

#ifdef RTC_CLOCK_SOURCE_LSE

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.LSIState = RCC_LSI_OFF;

if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;

PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

{

Error_Handler();

}

#elif defined (RTC_CLOCK_SOURCE_LSI)

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

RCC_OscInitStruct.LSIState = RCC_LSI_ON;

RCC_OscInitStruct.LSEState = RCC_LSE_OFF;

if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;

PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

{

Error_Handler();

}

#else

#error Please select the RTC Clock source inside the main.h file

#endif /*RTC_CLOCK_SOURCE_LSE*/

/*##-2- Enable RTC peripheral Clocks #######################################*/

/* Enable RTC Clock */

__HAL_RCC_RTC_ENABLE();

/*##-4- Configure the NVIC for RTC Alarm ###################################*/

HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x0F, 0);

HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);

}
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
gokhannsahin
Associate III
January 5, 2018
Posted on January 05, 2018 at 22:18

The code can't pass SystemClockConfig() at the begining, so I mean the following code.

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.LSIState = RCC_LSI_OFF;

if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

Tesla DeLorean
Guru
January 5, 2018
Posted on January 05, 2018 at 22:24

I'd recommend clearing the structure, data on the stack may interfere with desired operation.

RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
gokhannsahin
Associate III
January 5, 2018
Posted on January 05, 2018 at 22:51

I just tried that it still can't pass the systemclockconfig()..

T J
Senior III
January 5, 2018
Posted on January 05, 2018 at 23:14

Did you try to single step ?

best starting point is to turn off data and code cache, and single step...

gokhannsahin
Associate III
January 6, 2018
Posted on January 06, 2018 at 18:59

I guess that I tried all possibilities in order to solve this problem, but I can't. The code can't pass SystemClockConfig() function after Hal_init() called also it's waiting 5 seconds is default on cubemx for lse to be ready. However, it can't and finally errorhandler() is called. Can it be a bug?

RomainR.
ST Employee
January 6, 2018
Posted on January 06, 2018 at 19:06

Hi Sahin.gokhan

Can you share your code (main.c) where SystemClockConfig() is defined ?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
RomainR.
ST Employee
January 6, 2018
Posted on January 06, 2018 at 19:23

I think something is wrong and incomplete into

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;

SystemClockConfig() should configure: 

RCC_OscInitStruc for main SYSCLK by using HSE, MSI or HSI with or without PLL.

And configure LSE or LSI. Not both.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Bogdan Golab
Lead
January 6, 2018
Posted on January 06, 2018 at 19:27

We believe that you enabled LSE in RCC section of the CubeMX and LSE is selected for RTC in the clock tab. If yes, please share the code.

gokhannsahin
Associate III
January 6, 2018
Posted on January 06, 2018 at 19:51

This is systemconfig function has been generated via cubemx and the errorhandler is called after run the following line.

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK).

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_PeriphCLKInitTypeDef PeriphClkInit;

/**Configure LSE Drive Capability

*/

__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_OscInitStruct.LSEState = RCC_LSE_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(__FILE__, __LINE__);

}

/**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_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(__FILE__, __LINE__);

}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;

PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Enables the Clock Security System

*/

HAL_RCCEx_EnableLSECSS();

/**Configure the main internal regulator output voltage

*/

if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/**Enable MSI Auto calibration

*/

HAL_RCCEx_EnableMSIPLLMode();

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

________________

Attachments :

main.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyDF&d=%2Fa%2F0X0000000b4i%2FMtRzAnXPHzei3qia.BrLia5s_jPFIS0QX6GEAuTZPcw&asPdf=false
RomainR.
ST Employee
January 6, 2018
Posted on January 06, 2018 at 20:41

I've tested your 

SystemClock_Config() on NucleoL476RG (with LSE 32768 mounted) and function is OK.

No Hard fault for me.

Check LSE on board. Maybe is a hardware issue.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Bogdan Golab
Lead
January 6, 2018
Posted on January 06, 2018 at 20:53

Very similar issue:

https://os.mbed.com/questions/67987/STM32L476-LSE-startup-issues/

 
gokhannsahin
Associate III
January 8, 2018
Posted on January 08, 2018 at 10:24

‌ thank you for your suggestion, although tried on your link, the problem isn't still solved.

The kitwas bought yesterday and only that code was run on it. Furthermore, can't see any oscillation that is similar to 32768Khz in pins of crystal.

________________

Attachments :

issue.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hy8f&d=%2Fa%2F0X0000000b4f%2F5INmfvPM3D62WutIn2kj8OxYJLVWjLoUsRBAaGdh334&asPdf=false
Bogdan Golab
Bogdan GolabBest answer
Lead
January 8, 2018
Posted on January 08, 2018 at 10:38

It could be a hw problem but your probe (osciloscope) may affect the circuit and prevents the LSE from starting anyway (during the time when the scope is connected to the circuit). I guess they use pretty low capacitors (4.7-10pF).

I want to say that LSE may does not work but connecting scope may also prevent it from starting so it i snot a prove that LSE is broken.

I can test it on my board but later after work.

Bogdan Golab
Lead
January 8, 2018
Posted on January 08, 2018 at 18:36

Just generated simple CubeMX project for the STM32L476-DISCO board:

 

There are enabled LSE for RTC clocking, RTC and the time is displayed on the LCD.

The code is awful but seems to work without any issue.

Please note: the project is prepared to run in SRAM without flashing the board so if you want to use it without any modification start in in debugger.

If you want to flash it change the Target IROM settings to reflect real flash addresses.

It's for KEIL IDE.

gokhannsahin
Associate III
January 9, 2018
Posted on January 09, 2018 at 12:47

Golab.Piotr

‌ thank you, I break the crystal without knowing it when trying the 32768KHz in order to scope. I have tried to scope the only pin that is routed out of RTC (512Hz) not pins of crystal, finally, it run.Thank you again.:)

Bogdan Golab
Lead
January 9, 2018
Posted on January 09, 2018 at 12:50

Luckily you can use other sources of the clock for RTC...