cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476G-Disco LSE Startup Problem

gokhannsahin
Associate II
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
1 ACCEPTED SOLUTION

Accepted Solutions
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.

View solution in original post

20 REPLIES 20
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
Up vote any posts that you find helpful, it shows what's working..
gokhannsahin
Associate II
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();

}

T J
Lead
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...

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
Up vote any posts that you find helpful, it shows what's working..
Posted on January 05, 2018 at 22:51

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

Posted on January 05, 2018 at 23:16

Your options here are to dig into the library code to determine where it fails, walk the chip registers through the LSE startup sequence, and to review what the external oscillator is doing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gokhannsahin
Associate II
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?

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 II
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