Turn on the LED on STM32F4-Discovery

Sunday, August 17, 2014 10:00 PM

Prerequisites

- You have to see the previous tutorial of setting up the first project for IAR IDE Here.
- Download these two PDF documents : stm32f4discovery_sch.zip and DS8626
- Download the system_stm32f4xx.c source file and replace it in your project directory.
- You can download the project Here.
- If you want to see the video go to the bottom of this page.

I- Turn on the LED without using the discovery board drivers

Before coding we have to know in which PIN of the STM32F407VG Microcontroller the LEDs on the discovery board are connected. So we have to check the Discovery Board schematic (first PDF document) and go to the last sheet, number 6.


We notice that the LEDs are connected to the GPIO PORTD.
In this tutorial we will try to turn on the LED6 which is connected to the PIN 15

Clock configuration

Every peripheral on the STM32Fxx microcontroller is clocked by an internal clock.
So we have to identify in which internal clock the GPIO PORTD is connected. So we open the second PDF file and go to the page 18 in the Device overview.

As shown in the picture above the GPIO PORTD is clocked by AHB1 which the frequency can reach 168 MHz.
So we have to add in the workspace under the STM32F4xx_StdPeriph_Driver group the real-time clock control (RCC) source file the “stm32f4xx_rcc.c”
You can find this file under the driver folder in this link:
STM32F4-Discovery_FW_V1.1.0\Libraries\STM32F4xx_StdPeriph_Driver\src

Then be sure that the header file is also included inside the stm32f4xx_conf.h by removing the double slash '//'


Inside the stm32f4xx_rcc.c file we search for the RCC_AHB1PeriphClockCmd function which is responsible for enabling or disabling the AHB1 peripheral clock.
We copy this function under our main file, and we try to activate the GPIO PORTD

int main()
{
  //Enable the GPIOD Clock
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);  
  return 0;
}
 
            

 

GPIO configuration

We have to add in the IAR IDE under the STM32F4xx_StdPeriph_Driver group the GPIO source file the “stm32f4xx_gpio.c”
You can find this file under the driver folder in this link:
STM32F4-Discovery_FW_V1.1.0\Libraries\STM32F4xx_StdPeriph_Driver\src

Be sure that the header file is also activated inside the stm32f4xx_conf.h by removing the double slash ‘//’

Inside the stm32f4xx_gpio.c file we look for the GPIO_Init function which is responsible for initializing the GPIOx peripheral.

/**
  * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_InitStruct.
  * @param GPIOx: where x can be (A..I) to select the GPIO peripheral.
  * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that contains
  *                         the configuration information for the specified GPIO peripheral.
  * @retval None
  */
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
  .
  .
  . 
  . 
  . 
  . 
  .
  .
}
 
            

Before adding this function we have to create a new GPIO_InitTypeDef structure called GPIO_InitStruct inside the main file.

GPIO_InitTypeDef GPIO_InitStruct; 
   

The structure’s definition is inside the stm32f4xx_gpio.h

/** 
  * @brief   GPIO Init structure definition  
  */ 
typedef struct
{
  uint32_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.
                                       This parameter can be any value of @ref GPIO_pins_define */

  GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.
                                       This parameter can be a value of @ref GPIOMode_TypeDef */

  GPIOSpeed_TypeDef GPIO_Speed;   /*!< Specifies the speed for the selected pins.
                                       This parameter can be a value of @ref GPIOSpeed_TypeDef */

  GPIOOType_TypeDef GPIO_OType;   /*!< Specifies the operating output type for the selected pins.
                                       This parameter can be a value of @ref GPIOOType_TypeDef */

  GPIOPuPd_TypeDef GPIO_PuPd;     /*!< Specifies the operating Pull-up/Pull down for the selected pins.
                                       This parameter can be a value of @ref GPIOPuPd_TypeDef */
}GPIO_InitTypeDef;
       

This structure have 6 members. All of them are mandatory to configure correctly the GPIOx.
These Are the Following members:
GPIO_Pin:  pin 0/…./15/All, we choose the pin number 14.
GPIO_Mode: Input/Output/Alternate function/Analog, we choose the output.
GPIO_Speed: 2/25/50/100MHZ, we choose 50 MHZ.
GPIO_OType: Push-Pull/ Open-Drain, we choose push-pull.
GPIO_PuPd: Pull-up/Pull-down/No-Pull, we choose the pull-up.

#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

int main()
{
  ....
  // GPIOD Configuration
  GPIO_InitTypeDef GPIO_InitStruct;

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  
  GPIO_Init(GPIOD, &GPIO_InitStruct);
  
  
  return 0;
}      
	 

Then we put this structure as second parameter for the GPIO_Init function.

For more detail you have to watch my video

You have to add the ‘&’ before GPIO_InitStruct because as written in the GPIO_Init function, the second parameter is a pointer to a GPIO_InitTypeDef structure.

To turn on the LED we use the GPIO_SetBits from the “stm32f4xx_gpio.c”.
Here’s the code:

#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

int main()
{
  //Enable the GPIOD Clock
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
  
  // GPIOD Configuration
  GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  
  GPIO_Init(GPIOD, &GPIO_InitStruct);
  
  // GPIOD-PIN-15 ON
  GPIO_SetBits(GPIOD, GPIO_Pin_15);
  
  return 0;
}
 
              

Now you can compile your code and download it in your STM32F4xx microcontroller and the Blue LED will turn on.

You have to download the ST-LINK/V2 driver from ST website to be able to download the code inside your microcontroller

 

II- Turn on the LED using the discovery board drivers

The stm32f4_discovery.c source file simplify the work a lot. Instead of going to identify in which pin the LEDs are connected, the STM_EVAL_LEDInit function do this work for us.

The stm32f4_discovery.c contain a few functions for LEDs and Buttons inside the STM32F4-Discovery Board.

Here’s the code:

#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
int main()
{
  //Enable the GPIOD Clock
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
  
  // GPIOD Configuration
  STM_EVAL_LEDInit(LED6);
  
  // GPIOD-PIN-15 ON
  STM_EVAL_LEDOn(LED6);
  
  return 0;
}
       

This Tutorial still available for all STM32Fxx devices.
If you use the same STM32F407VG microcontroller with others boards than the Discovery, you must not add the stm32f4_discovery.c source file.

Video