cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMx generates unfunctionnal code with lib 1.5

JulienD
Senior
Posted on April 24, 2016 at 18:02

Hi,

Here's some code generated with cubeMx:

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI
| RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
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;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV8;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

STM32Cube FW_L1 V1.5.0 lib changed some behaviour in HAL_RCCEx_PeriphCLKConfig function. It may return HAL_ERROR if HSE state is on. This makes the above code return an error and not set the RTC. I don't know exactly where's the bug but very basically I commented out the ''return HAL_ERROR'' and it does the job (maybe for my own use...). I suppose this is a bug either in the lib either in cubeMx that does not call in the good order... But maybe I did it wrong. In this case what ? Regards Julien #stm32l1-lib-cue-1.5
5 REPLIES 5
Walid FTITI_O
Senior II
Posted on April 25, 2016 at 11:17

Hi JulienD,

Please , describe the issue behavour that you have seen (In which case you get Hal_error ?  have you tried to debug in see in what level your hal-error is ? )

Also, may you share the .ioc file of your project.

-Hannibal-

JulienD
Senior
Posted on April 28, 2016 at 10:15

Hi Hannibal.

Here's the simplified ioc file.

The hal_error occurs around the line 160 in file stm32l1xx_hal_rcc_ex.c.

/* Check HSE State */ 
if (((PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL) == RCC_CSR_RTCSEL_HSE) && HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)) { 
/* To update HSE divider, first switch-OFF HSE clock oscillator*/ 
return HAL_ERROR; 
} 
}

here's the code inserted in the main file :

/* USER CODE BEGIN WHILE */ 
__HAL_RTC_WAKEUPTIMER_ENABLE(&hrtc); 
while (1) { 
if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hrtc, RTC_FLAG_WUTF)) { 
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF); 
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15); 
} 
/* USER CODE END WHILE */

With this code and ioc file, the D15 pin should toggle every 250 ms but it does not.

It works if I comment the ''return HAL_ERROR'' line.

Also, I need to power cycle the microcontroler if you want to be sure of the configuration.

Julien

________________

Attachments :

Diamond.ioc : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1IJ&d=%2Fa%2F0X0000000bl6%2FTj9IACa7NRCKGb8ihNj4Q7j6v99dY8LLz_7vUTUOEDc&asPdf=false
JulienD
Senior
Posted on October 25, 2016 at 10:53

Hello Hannibal

Any update on this issue?

Regards

Julien

Walid FTITI_O
Senior II
Posted on October 25, 2016 at 13:29

Hi JulienD, 

It is not a software related issue. It seems you have a problem in the used HSE in your hardware. 

In page 78 of the STM32L151xE datasheet follow this note:

''For CL1 and CL2, it is recommended to use high-quality external ceramic capacitors in the 5 pF to 25 pF range (typ.), designed for high-frequency applications, and selected to match the requirements of the crystal or resonator (see Figure 16). CL1 and CL2 are usually the same size. The crystal manufacturer typically specifies a load capacitance which is the series combination of CL1 and CL2. PCB and MCU pin capacitance must be included (10 pF can be used as a rough estimate of the combined pin and board capacitance) when sizing CL1 and CL2. Refer to the application note AN2867 “Oscillator design guide for ST microcontrollers�

-Hannibal-

JulienD
Senior
Posted on October 25, 2016 at 14:36

Well, the board is running smooth on HSE for a long time ago.

Since the first message, I had the opportunity to go deeper with the clock system. Here are my complete investigations: The ioc file is composed of a sysclock clocked by PLL which is clocked by HSE and an RTC clocked by HSE too thru a /8 prescaler. The SystemClock_Config() function generated by cubeMX does the following things: - configure HSE and HSI on, PLL clocked by HSE by calling HAL_RCC_OscConfig() - set the sysclock to use HSE by calling HAL_RCC_ClockConfig() - configure RTC clock source by calling HAL_RCCEx_PeriphCLKConfig() version 1.5 added a check before setting the RTC clock source / prescaler. It's a good idea because ( documentation 6.3.1: This prescaler cannot be modified if HSE is enabled (HSEON = 1))

/* Check if user wants to change HSE RTC prescaler whereas HSE is enabled */
temp_reg = (RCC->CR & RCC_CR_RTCPRE);
if ((temp_reg != (PeriphClkInit->RTCClockSelection & RCC_CR_RTCPRE))
#if defined (LCD)
|| (temp_reg != (PeriphClkInit->LCDClockSelection & RCC_CR_RTCPRE))
#endif /* LCD */
) {
/* Check HSE State */
if (((PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL) == RCC_CSR_RTCSEL_HSE) && HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)) {
/* To update HSE divider, first switch-OFF HSE clock oscillator*/
return HAL_ERROR;
}
}

This function checks if the desired prescaler value is different from the current one which is the case because we need 8 and the current value is 0 (after power cycle). (documentation 6.3.1 These bits are reset by power on reset, they keep their value after system reset.) It checks if HSE is ready. HSE is ready because it has been setup by the previous call to HAL_RCC_OscConfig(), which itself checks if HSE is ready. Because HSE is ready, the HAL_RCCEx_PeriphCLKConfig() returns HAL_ERROR So with this ioc file, the generated code will always return an error after power cycle. IMHO, with 1.5 lib, the HAL_RCCEx_PeriphCLKConfig() should be called before HAL_RCC_OscConfig() in SystemClock_Config(). Regards Julien