cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32L4] Hard fault during OSC32_OUT init as GPO

Syn_BE
Associate III

I want to use the OSC32_OUT Pin (PC15) of the STM32L432KC as general purpose output. Moreover i connected a FET with a pullup to this pin and want to control them. I do not use any LSE oscillator functionality. The datasheet states that upon reset this pin already can be used as GPIO if not configured as LSE and or controlled by the Backup domain.

If i use the generated CUBE IDE init code, i always face a Hard Fault in the HAL_GPIO_Init routine.

The fault anaylzer shows the follwing messages:

  • Bus, memory management or usage fault (FORCED)
  • Attempt to execute undefined instruction (UNDEFINSTR)
  • SP: 0x2000FEC8
  • LR: 0x40010400
  • PC: 0xA0000000 (this seems to be wrong)
  • R0: 0x48000800 (GPIOC)

Attached the init code:

#define STANDBY_Pin          GPIO_PIN_15
#define STANDBY_GPIO_Port    GPIOC
 
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  
   /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(STANDBY_GPIO_Port, STANDBY_Pin, GPIO_PIN_RESET);
  
  /*Configure GPIO pin : STANDBY_Pin (PC15) */
  GPIO_InitStruct.Pin = STANDBY_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(STANDBY_GPIO_Port, &GPIO_InitStruct);
}

If i step through the HAL_GPIO_Init function i do not got any hard faults.

Did i miss something?

5 REPLIES 5
Uwe Bonnes
Principal III

Unexpected crashes are often caused by to few wait states or otherwise screwed clock setup.

Syn_BE
Associate III

Thank you for your answer, i suspected also this sources of error.

But i get the hard fault only if i try to init PC15. If i comment it out all other pins inits and works just fine. Also i do not face any faults during further execution.

Btw. PC14 inits also just fine, i use it as input.

I checked also the supply voltage, it is stable at 3.314V.

Here is my current SystemClock_Config, i checked it again and can not see any errors:

  • I run the whole device from internal clock generators (crystal less operation)
  • Wait States set to 4
  • Core Clock (SYSCLK) is set to 80 MHz
  • HCLK set to to 80 MHz
  • PCLK1 set to 80 MHz
  • PCLK2 set to 80 MHz
  • PLL1SAI1R set to 40 MHz
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;                                            
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 4;
  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_USART2|RCC_PERIPHCLK_SAI1
                              |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_USB;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  PeriphClkInit.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLLSAI1;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSI;
  PeriphClkInit.PLLSAI1.PLLSAI1M = 4;
  PeriphClkInit.PLLSAI1.PLLSAI1N = 20;
  PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_SAI1CLK|RCC_PLLSAI1_ADC1CLK;
  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();
  }
}

Syn_BE
Associate III

I have a workaround. But I'm probably fighting the symptom rather than the cause.

I setup the device core clock to 20 MHz first (AHB div 4), make the Pin initializations, and apply then the 80 MHz configuration.

Try to set the given pin as outputting High (1) *before* setting it as output.

JW

Timvosch
Associate

This is an old thread, but it appears on google searches.

Long story short, PC14 and PC15 can sink barely any current. Sink more and it causes glitches!

I've been battling a similar issue where toggling PC14 causes severe PC corruption and often a fault. The datasheet mentions once that PC14 and PC15 are connected to the internal power switch. This means that they can draw substantially less current. In other words, draw too much current from one of these pins and the CPU can glitch out causing faults, skipped instructions or corrupted registers.