Spread the love

 

____________________________________________________________________________________________________

In this tutorial we will try to explain the working of GPIO(Gernal Purpose Input Output) registers of Stm32f4 using CMSIS library provided by ST.

LED on STM32F4 are connected on pins of PORTD pins (PD12, PD13, PD14 and PD15). So we first need to enable the clock of PORTD. For that we need to call the following function.

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

After Enabling the clock of PORTD ,configure the register of GPIO by using CMSIS Library structure. So we need to configure following 5 options.

  1. GPIO_Pin:- Select the pin which are used
  2. GPIO_Mode:- Select the mode in which want to operate see below
  3. GPIO_OType:- Select the type of output
  4. GPIO_PuPd:- Select pull resistor
  5. GPIO_Speed:- Select pin speed

Every setting have options

  1. GPIO_Pin:-
                               we can select GPIO pins as follows:-
// declare a variable of structure GPIO_InitTypeDef
GPIO_InitTypeDef  GPIO_Ini_Structure;

// First option if use 1 pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

// Second option if use 2 or more pins
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;

// Third option if want to use all pins of same port
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;

1. GPIO_Mode:-Type of mode of operation
GPIO_Mode_IN: Set pin as input
GPIO_Mode_OUT: Set pin as output
GPIO_Mode_AF: Set pin as alternating function ( like when used as SPI, USART, etc)
GPIO_Mode_AN: Set pin as analog (ADC or DAC)
2. GPIO_OType:- Type of Mode of output pins
GPIO_OType_PP: Output type is push-pull
GPIO_OType_OD: Output type is open drain
3. GPIO_PuPd:-  Select type of Pull-up/Pull-down
GPIO_PuPd_UP: Enable pull up resistor
GPIO_PuPd_DOWN: Enable pull down resistor
GPIO_PuPd_NOPULL: Disable pull resistor
4. GPIO_Speed:- Select GPIO speed
GPIO_Speed_100MHz
GPIO_Speed_50MHz
GPIO_Speed_25MHz
GPIO_Speed_2MHz

After That call the GPIO initialize function GPIO_Init()  present in stm32f4xx_gpio.h header file.

/**
* @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);

We need to initialize the PD12, PD13, PD14 and PD15 pins as output mode .

    GPIO_InitTypeDef  GPIO_Ini_Structure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 
    GPIO_Ini_Structure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
    GPIO_Ini_Structure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Ini_Structure.GPIO_OType = GPIO_OType_PP;
    GPIO_Ini_Structure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Ini_Structure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(PORTD, &GPIO_Ini_Structure);

To toggle the LED present on stm32f4 discovery call the below function.

FM_Led_Toggle(LED_NAME led_name);

A sample example of blinking the led as follows

/***************************************************************
* File     : fm_stm32f4_led.h
* website  : www.firmcodes.com
* email_id : support@firmcodes.com
* IDE used : The files are Compiled in coocox ide and
	     tested on stm32f4 discovery board.
***************************************************************/

#include "stm32f4xx.h"
#include "fm_stm32f4_led.h"
#include "fm_stm32f4_delay.h"

void main()
{
    // initialize the system frequency
    SystemInit();
    // Delay initialize
    delay_init();
    // all LED initialize
    FM_Led_Init();
    while(1)
    {
       // Toggle all LED
       FM_Led_Toggle(LED_GREEN);
       FM_Led_Toggle(LED_ORANGE);
       FM_Led_Toggle(LED_RED);
       FM_Led_Toggle(LED_BLUE);
       // Delay for 1sec
       delay_ms(1000);
    }
}

Dependency  to compile the program as follows:-

  1. stm32f4xx.h
  2. stm32f4xx_gpio.h
  3. stm32f4xx_rcc.h
  4. fm_stm32f4_led.h
  5. fm_stm32f4_delay.h

Led and delay library is given as follows:

   LED Library                                                                                               Delay Library

download (1)download (1)

 

  1. fm_stm32f4_led.h function
/***************************************************************
* File     : fm_stm32f4_led.h
* website  : www.firmcodes.com
* email_id : support@firmcodes.com
* IDE used : The files are Compiled in coocox ide and 
			 tested on stm32f4 discovery board.
***************************************************************/

#ifndef __FM_STM32F4_LED_H
#define __FM_STM32F4_LED_H

/***************************************************************
// Header Files Includes
***************************************************************/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"

/***************************************************************
* List of All LED
***************************************************************/
typedef enum 
{
  LED_GREEN = 0,  // Green Led on STM32F4-Discovery
  LED_ORANGE = 1, // Orange Led on STM32F4-Discovery
  LED_RED = 2,    // Red Led on STM32F4-Discovery
  LED_BLUE = 3    // Blue Led on STM32F4-Discovery
}LED_NAME;

#define  TOTAL_LED   4 // Anzahl von LED_NAME_t


/***************************************************************
* Status einer LED
***************************************************************/
typedef enum {
  LED_OFF = 0,  // LED AUS
  LED_ON        // LED EIN
}LED_STATUS;


/***************************************************************
* Strcture of  LED
***************************************************************/
typedef struct {
  LED_NAME LED_NAME;    // Name
  GPIO_TypeDef* LED_PORT; // Port
  const uint16_t LED_PIN; // Pin
  const uint32_t LED_CLK; // Clock
  LED_STATUS LED_INIT;  // status
}LED_struct;


/***************************************************************
* Gloable Function
***************************************************************/
void FM_Led_Init(void);
void FM_Led_Off(LED_NAME led_name);
void FM_Led_On(LED_NAME led_name);
void FM_Led_Toggle(LED_NAME led_name);


/***************************************************************/
#endif // __FM_STM32F4_LED_H

      3. fm_stm32f4_led.c function

/***************************************************************
* File     : fm_stm32f4_led.c
* website  : www.firmcodes.com
* email_id : support@firmcodes.com
* IDE used : The files are Compiled in coocox ide and 
			 tested on stm32f4 discovery board.
***************************************************************/
#include "fm_stm32f4_led.h"




/***************************************************************
* All LED on Stm32f4 discovery board
***************************************************************/
LED_struct LED[] = {
  // Name    ,PORT     , PIN       , CLOCK            , Init
  {LED_GREEN ,GPIOD,GPIO_Pin_12,RCC_AHB1Periph_GPIOD,LED_OFF},   // PD12=Green LED on Discovery-Board
  {LED_ORANGE,GPIOD,GPIO_Pin_13,RCC_AHB1Periph_GPIOD,LED_OFF},   // PD13=Orange LED on Discovery-Board
  {LED_RED   ,GPIOD,GPIO_Pin_14,RCC_AHB1Periph_GPIOD,LED_OFF},   // PD14=Red LED on Discovery-Board
  {LED_BLUE  ,GPIOD,GPIO_Pin_15,RCC_AHB1Periph_GPIOD,LED_OFF},   // PD15=Blue LED on Discovery-Board
};

/***************************************************************
* Initialize all LED
***************************************************************/
void FM_Led_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  LED_NAME led_name;
  
  for(led_name=0;led_name<TOTAL_LED;led_name++) {
    // Clock Enable
    RCC_AHB1PeriphClockCmd(LED[led_name].LED_CLK, ENABLE);

    // Config as digital output
    GPIO_InitStructure.GPIO_Pin = LED[led_name].LED_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LED[led_name].LED_PORT, &GPIO_InitStructure);

  }
}


/***************************************************************
* LED off
***************************************************************/
void FM_Led_Off(LED_NAME led_name)
{
  LED[led_name].LED_PORT->BSRRH = LED[led_name].LED_PIN;
}

/***************************************************************
* LED on
***************************************************************/
void FM_Led_On(LED_NAME led_name)
{
  LED[led_name].LED_PORT->BSRRL = LED[led_name].LED_PIN;
} 

/***************************************************************
* LED toggle
***************************************************************/
void FM_Led_Toggle(LED_NAME led_name)
{
	
  LED[led_name].LED_PORT->ODR ^= LED[led_name].LED_PIN;

}

     3. fm_stm32f4_delay.h function

/***************************************************************
* File     : fm_stm32f4_delay.h
* website  : www.firmcodes.com
* email_id : support@firmcodes.com
* IDE used : The files are Compiled in coocox ide and 
			 tested on stm32f4 discovery board.
***************************************************************/

#ifndef __FM_STM32F4_DELAY_H
#define __FM_STM32F4_DELAY_H 

/***************************************************************
// Header Files Includes
***************************************************************/
#include "stm32f4xx.h"			   
#include "misc.h"



/***************************************************************
* Gloable Function
***************************************************************/	 
void FM_delay_init(void);
void delay_ms(u16 nms);
void delay_us(u32 nus);

/***************************************************************/
#endif // __FM_STM32F4_DELAY_H

4. fm_stm32f4_delay.c function

#include "fm_stm32f4_delay.h"

static u8  fac_us=0;  // micro second count
static u16 fac_ms=0;  // mili second count

/***************************************************************
* delay Initilize 
***************************************************************/
void delay_init()	 
{
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	//HSE  HCLK/8
	fac_us=SystemCoreClock/8000000;							//SYSCLK/8  
	fac_ms=(u16)fac_us*1000;   
}								    

/***************************************************************
* delay in micro second
***************************************************************/		    								   
void delay_us(u32 micro_sec)
{		
	u32 temp;	    	 
	SysTick->LOAD=micro_sec*fac_us; //Load	  		 
	SysTick->VAL=0x00;        		//Clear
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;        
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16)));   
	SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;       
	SysTick->VAL =0X00;       
}

/***************************************************************
* Delay in mili second
***************************************************************/
void delay_ms(u16 mili_sec)
{	 		  	  
	u32 temp;		   
	SysTick->LOAD=(u32)mili_sec*fac_ms;
	SysTick->VAL =0x00;           
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;         
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16)));
	SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;       
	SysTick->VAL =0X00;       
} 

Complete project compiled in coocox is given below

download (1)

  1. Led blinking on stm32f4 Discovery Board