cancel
Showing results for 
Search instead for 
Did you mean: 

Could anyone help me build my STM32F1 HAL project?

421669077
Associate
Posted on September 15, 2015 at 07:15

I have a minimum STM32F103ZET6 system board with. I have downloaded STM32CubeF1(Version 1.2.0) package. I build a simple project, which just blink a led, and I compile it with no error/warning. The main.c source code is shown below

#include''stm32f1xx_hal.h''

#include''stm32f1xx_hal_gpio.h''

#include''stm32f1xx_hal_rcc.h''

#include''stm32f1xx_hal_rcc_ex.h''

#include''stdint.h''

void m_delay(volatile uint32_t m_time){

  while(m_time > 0){

    m_time--;

  }

}

void SystemClock_Config(){

  RCC_ClkInitTypeDef Clk_InitStructure = {0};

  RCC_OscInitTypeDef Osc_InitStructure = {0};

  Osc_InitStructure.OscillatorType  = RCC_OSCILLATORTYPE_HSE; 

  Osc_InitStructure.HSEState        = RCC_HSE_ON; 

  Osc_InitStructure.HSEPredivValue    = RCC_HSE_PREDIV_DIV1; 

  Osc_InitStructure.PLL.PLLState    = RCC_PLL_ON; */

  Osc_InitStructure.PLL.PLLSource   = RCC_PLLSOURCE_HSE; 

  Osc_InitStructure.PLL.PLLMUL      = RCC_PLL_MUL9; 

  if (HAL_RCC_OscConfig(&Osc_InitStructure)!= HAL_OK)

  {

    /* Initialization Error */

    while(1); 

  }

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 

     clocks dividers */

  Clk_InitStructure.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  Clk_InitStructure.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  Clk_InitStructure.AHBCLKDivider = RCC_SYSCLK_DIV1;

  Clk_InitStructure.APB2CLKDivider = RCC_HCLK_DIV1;

  Clk_InitStructure.APB1CLKDivider = RCC_HCLK_DIV2;  

  if (HAL_RCC_ClockConfig(&Clk_InitStructure, FLASH_LATENCY_2)!= HAL_OK)

  {

    /* Initialization Error */

    while(1); 

  }

}

int main(){

  HAL_Init();

  SystemClock_Config();

  __HAL_RCC_GPIOG_CLK_ENABLE();

  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.Pin = GPIO_PIN_14;

  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStructure.Pull = GPIO_PULLUP;

  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;

  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);

  while(1){

    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, 1);

    HAL_Delay(500);

    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, 0);

    HAL_Delay(500);

  }

  return 0;

}

But it didn't work well. The pin output HIGH and didn't change its value. It looks like the program block at  ''HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, 1)''. Even I change the HAL_Delay() to the function I write m_delay(), it also can be blocked.  So what wrong with my code?
3 REPLIES 3
Amel NASRI
ST Employee
Posted on September 15, 2015 at 12:26

Hi yuzhou.huang,

I suggest you:

1- Debug your code as the problem may be caused by clock configuration code for example, not GPIO related function

2- Have a look to the example STM32Cube_FW_F1_V1.2.0\Projects\STM32F103RB-Nucleo\Examples\GPIO\GPIO_IOToggle implementing similar functionality as what you want.

3- Use the STM32CubeMX tool to generate initialization project to which you may add your own code.

-Mayla-

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.

Posted on September 15, 2015 at 15:04

If the external clocks, or internal PLL, don't start correctly you're going to fall into one of the while(1) tar pits. Add code to identify where it's stuck, output via a GPIO/LED or USART.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
keaven
Associate II
Posted on September 16, 2015 at 17:27

Hello,

HAL_Delay(500); requires SysTick_Handler() did you implement it? Otherwise the tick counter doesn't increment and you are stall in HAL_Delay().

void SysTick_Handler(void)
{
HAL_IncTick();
}