# GPIO and clock configuration of STM32F427VG

I am very new in STM32 projects and it is my first time writing a program with STM32. I used to program with AVR and now I should progress to STM32. I have a board with STM32F427VG on it without any external XTAL. I wrote the following code to make a simple LED blink:

#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
int main(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_InitTypeDef GPIO_InitDef; //Where GPIO_InitDef is variable to work with struct
GPIO_InitDef.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitDef);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
int i;

while(1)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
for (i = 0; i < 500000; i++);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
for (i = 0; i < 500000; i++);

}
}


I use Embitz to compile my code. In order to configure the internal clock I use the following configuration:

Then I press run and then generate. Then I copy the generated system_stm32f4xx file and replace with the default system_stm32f4xx file in the project.

Still, I can't see any change in the LED.

I know I am very new in STM32 projects, so most likely this question is very simple for anybody. Thanks if anybody helps me to find the problem.

• I don't have enough rep to comment here but are you sure the compiler is not optimising your 'delay loops' out of existence with the result that the LED is actually flashing faster than you can see ? – Ian Cook Sep 20 '16 at 17:41
• Optimization is probably the issue, this should be coded with sleep calls to be processor speed independent anyway. – Dean Franks Nov 3 '17 at 21:23
• I don't see in the code where you initialize the system clock, using Systeminit call or similar, then you need to handle the Systick handler else you can wait till Xmas and nothing will happen. :) – Abel Tom Mar 14 '18 at 6:05

You need to do more than replace the stm32f4xx file. See http://clockspeeds.blogspot.com/2013/01/stm32f4-discovery-clock-frequency.html. In particular, you have to do some work on the startup_stm32f4xx.c file.

From that site:

> STEP 3: In stm32f4xx.h file ensure HSE (external clock) value is 8 MHz
>
>
> #if !defined  (HSE_VALUE)   #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
> #endif /* HSE_VALUE */
>
>
> STEP 4: In startup_stm32f4xx.c file change comment on SystemInit()
> function
>
>
> /*----------Function
> prototypes-----------------------------------------------*/ extern int
> main(void);           /*!< The entry point for the application.    */
> //extern void SystemInit(void);    /*!< Setup the microcontroller
> system(CMSIS) */ void Default_Reset_Handler(void);   /*!< Default
> reset handler                */ static void Default_Handler(void);
> /*!< Default exception handler            */
>
> to
>
> /*----------Function
> prototypes-----------------------------------------------*/ extern int
> main(void);           /*!< The entry point for the application.    */
> extern void SystemInit(void);    /*!< Setup the microcontroller
> system(CMSIS) */ void Default_Reset_Handler(void);   /*!< Default
> reset handler                */ static void Default_Handler(void);
> /*!< Default exception handler            */
>
> STEP 5: On the same file down under there will be a function
>
> void Default_Reset_Handler(void)
>
> after assembly code towards the end add a line of code calling
> SystemInit() BEFORE calling main() ie, change to
>
> SystemInit(); main();
>
> Thats it! now in main function we can add
>
> RCC_HSEConfig(RCC_HSE_ON); while(!RCC_WaitForHSEStartUp()) { }
`
• where is "startup_stm32f4xx.c" I cant find it on embitz. I know this file was in Keil but I can't find it on embitz – Amir Sep 20 '16 at 19:05
• I don't know embitz – Scott Seidman Sep 20 '16 at 19:08

You must use CubeMX software it was designed for hardware code generation to STM32 mcu family. You can easily set system clock, gpio, spi, ethernet etc. with its GUI.

http://www.st.com/en/development-tools/stm32cubemx.html