STM32 Microcontroller- GPIO Driver Development(Part-2)

 



GPIO Driver Development

                In the previous blog, we discussed how to develop the drive file for controller specific. Now, let us start developing GPIO driver file. This driver file will be used by the user application, it may want to initialize or use a different GPIO ports, or the user application may change the PIN number, it can change the GPIO modes, speed, output, type etc. So, that's why driver file should give a configuration structure to the user application, So the user application will initialize or fill that structure and then it will pass the structure to the driver APIs, and the driver APIs will decode the structure, and it will take appropriate action like initializing the peripheral registers. So, now there is a need to create configuration structure.

GPIO Handle and Configuration structures 

Configurable items for User Application:
  • GPIO Port Number
  • GPIO Pin Number
  • GPIO Mode
  • GPIO Speed
  • GPIO Output type
  • GPIO Pullup-Pulldown
  • GPIO Alternate function mode
Include this structure in the GPIO.h header file

typedef struct
{
     uint8_t GPIO_PinNumber;
     uint8_t GPIO_PinMode;
     uint8_t GPIO_PinSpeed;
     uint8_t GPIO_PinPuPdControl;
     uint8_t GPIO_PinOPType;
     uint8_t GPIO_PinAltFunMode;
}GPIO_PinConfig_t;

typedef struct
{
       /*To hold the base address of the GPIO port to which the pin belongs to*/
      GPIO_RegDef_t      *pGPIOx=((GPIO_RegDef_t *) GPIOA_BASEADDR);

     / *This holds the pin configuration settings*/
      GPIO_PinConfig_t    GPIO_PinConfig;
}GPIO_Handle_t;

In the first structure, we need to include the configuration details of the GPIO. While in the second structure, pointer is used to hold the base address of GPIO port, and to hold the pin configuration settings.

GPIO Driver API Requirements:

  • GPIO Initialization
  • Enable/ Disable GPIO Port Clock
  • Read from a GPIO Pin
  • Write to GPIO Pin
  • Configure alternate functionality
  • Interrupt Handling
Include this declaration in the GPIO.h header file

void GPIO_Init(GPIO_Handle_t *pGPIOHandle);
void GPIO_DeInit(GPIO_RegDef_t *pGPIOx);


Define User Configurable Macros

Include this macros in the GPIO.h header file

To select Pin Mode

#define     GPIO_MODE_IN                          0             //Configure as Input
#define     GPIO_MODE_OUT                      1            //Configure as Output
#define     GPIO_MODE_ALTFN                  2            //Alternate Function
#define     GPIO_MODE_ANALOG              3           //Analog Pin
#define     GPIO_MODE_IT_FT                    4           //Interrupt pin Falling edge
#define     GPIO_MODE_IT_RT                    5          //Raising edge
#define     GPIO_MODE_IT_RFT                  6         //Rasing edge Falling edge Trigger

To select Pin Number

#define     GPIO_PIN_NO_0                          0
#define     GPIO_PIN_NO_1                          1
#define     GPIO_PIN_NO_2                          2
#define     GPIO_PIN_NO_3                          3
#define     GPIO_PIN_NO_4                          4
#define     GPIO_PIN_NO_5                          5
#define     GPIO_PIN_NO_6                          6
#define     GPIO_PIN_NO_7                          7
#define     GPIO_PIN_NO_8                          8
#define     GPIO_PIN_NO_9                          9
#define     GPIO_PIN_NO_10                       10
#define     GPIO_PIN_NO_11                       11
#define     GPIO_PIN_NO_12                       12
#define     GPIO_PIN_NO_13                       13
#define     GPIO_PIN_NO_14                       14
#define     GPIO_PIN_NO_15                       15

To Select Speed

#define    GPIO_SPEED_LOW                      0
#define    GPIO_SPEED_LOW                      1
#define    GPIO_SPEED_LOW                      2
#define    GPIO_SPEED_LOW                      3

To Select Pin Pull Up and Pull Down configuration Macros

#define   GPIO_NO_PUPD                            0
#define   GPIO_PIN_PU                                1
#define   GPIO_PIN_PD                                2

In the coming blog, we can write definition for GPIO_Init.

Comments

  1. Very much informative.... Understanding made much more simpler. Keep up the good work.

    ReplyDelete
  2. Good informative easy to understand 👍

    ReplyDelete

Post a Comment

Popular posts from this blog

The Hidden You

What's Important in Life

STM32 Microcontroller- GPIO structure