Gpio-1 001
Gpio-1 001
***********************************************************************************
*******
* File Name : GPIO.c
*
* Description : GPIO driver source file for STM32F411CEU6 microcontroller.
*
* Author : Diaa Abossrie
*
* Date : 12/07/2024
*
* Version : 1.1
*
* This file provides the implementation of functions for configuring and
controlling GPIO
* pins and ports on the STM32F411CEU6 microcontroller.
*
* Features:
* - Functions for pin setup, configuration, and mode selection.
* - Port-level configuration and data manipulation.
* - Read and write operations for GPIO pins and ports.
* - Support for alternate functions, pull-up/down resistors, and speed settings.
*
* Dependencies:
* - GPIO.h: Header file containing macros, enums, and function declarations.
* - common_macros.h: Helper macros for bit manipulation.
*
* Note:
* - Ensure the corresponding GPIO clocks are enabled before invoking these
functions.
* - This driver is designed specifically for the STM32F411CEU6 microcontroller.
***********************************************************************************
*******/
#include "GPIO.h"
#include "common_macros.h"
/
*----------------------------------------------------------------------------------
----------------------------------------------
* Function Name: GPIO_setupPin
*
* Description : Configures the GPIO pin for a specified port and pin number with
a given mode, pull-up/pull-down configuration,
* speed, and alternate function (if applicable).
*
* Parameters : port_num - The port number (e.g., PORTA_ID,
PORTB_ID)
* pin_num - The pin number (e.g., PIN0_ID, PIN1_ID,
PIN2_ID, ..., PIN15_ID)
* mode - Pin mode (e.g., PIN_INPUT, PIN_OUTPUT,
PIN_ALTERNATE_FUNCTION, PIN_ANALOG)
* output_type - Output type configuration (e.g.,
PIN_OUTPUT_PUSH_PULL, PIN_OUTPUT_DEFAULT, PIN_OUTPUT_OPEN_DRAIN)
* pull_resistor - Pull-up/pull-down configuration (e.g.,
PIN_PULL_UP, PIN_PULL_DOWN, PIN_NO_PULL)
* speed - Speed of the pin (e.g., PIN_SPEED_LOW,
PIN_SPEED_MEDIUM, PIN_SPEED_HIGH, PIN_SPEED_VERY_HIGH)
* alt_func - Alternate function for the pin (relevant only
for alternate function mode)
*
* Return : None
*
*----------------------------------------------------------------------------------
---------------------------------------------- */
/* Configure Pull-Up/Pull-Down */
if (pull_resistor == PIN_PULL_UP)
{
SET_BIT(GPIOA_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOA_PUPDR, (2 * pin_num + 1));
}
else if (pull_resistor == PIN_PULL_DOWN)
{
CLEAR_BIT(GPIOA_PUPDR, (2 * pin_num));
SET_BIT(GPIOA_PUPDR, (2 * pin_num + 1));
}
else /* No Pull-Up/Down */
{
CLEAR_BIT(GPIOA_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOA_PUPDR, (2 * pin_num + 1));
}
/* Configure Speed */
GPIOA_OSPEEDR &= ~(0x3 << (2 * pin_num)); // Clear speed bits
GPIOA_OSPEEDR |= (speed << (2 * pin_num)); // Set speed bits
break;
case PORTB_ID:
if(mode == PIN_INPUT){
CLEAR_BIT(GPIOB_MODER,(2*pin_num));
CLEAR_BIT(GPIOB_MODER,(2*pin_num+1));
}
else if(mode == PIN_OUTPUT)
{
SET_BIT(GPIOB_MODER,(2*pin_num));
CLEAR_BIT(GPIOB_MODER,(2*pin_num+1));
}
else if(mode == PIN_ALTERNATE_FUNCTION)
{
CLEAR_BIT(GPIOB_MODER,(2*pin_num));
SET_BIT(GPIOB_MODER,(2*pin_num+1));
/* Configure Pull-Up/Pull-Down */
if (pull_resistor == PIN_PULL_UP)
{
SET_BIT(GPIOB_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOB_PUPDR, (2 * pin_num + 1));
}
else if (pull_resistor == PIN_PULL_DOWN)
{
CLEAR_BIT(GPIOB_PUPDR, (2 * pin_num));
SET_BIT(GPIOB_PUPDR, (2 * pin_num + 1));
}
else /* No Pull-Up/Down */
{
CLEAR_BIT(GPIOB_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOB_PUPDR, (2 * pin_num + 1));
}
/* Configure Speed */
GPIOB_OSPEEDR &= ~(0x3 << (2 * pin_num)); // Clear speed bits
GPIOB_OSPEEDR |= (speed << (2 * pin_num)); // Set speed bits
break;
case PORTC_ID:
if(mode == PIN_INPUT){
CLEAR_BIT(GPIOC_MODER,(2*pin_num));
CLEAR_BIT(GPIOC_MODER,(2*pin_num+1));
}
else if(mode == PIN_OUTPUT)
{
SET_BIT(GPIOC_MODER,(2*pin_num));
CLEAR_BIT(GPIOC_MODER,(2*pin_num+1));
}
else if(mode == PIN_ALTERNATE_FUNCTION)
{
CLEAR_BIT(GPIOC_MODER,(2*pin_num));
SET_BIT(GPIOC_MODER,(2*pin_num+1));
/* Configure Pull-Up/Pull-Down */
if (pull_resistor == PIN_PULL_UP)
{
SET_BIT(GPIOC_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOC_PUPDR, (2 * pin_num + 1));
}
else if (pull_resistor == PIN_PULL_DOWN)
{
CLEAR_BIT(GPIOC_PUPDR, (2 * pin_num));
SET_BIT(GPIOC_PUPDR, (2 * pin_num + 1));
}
else /* No Pull-Up/Down */
{
CLEAR_BIT(GPIOC_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOC_PUPDR, (2 * pin_num + 1));
}
/* Configure Speed */
GPIOC_OSPEEDR &= ~(0x3 << (2 * pin_num)); // Clear speed bits
GPIOC_OSPEEDR |= (speed << (2 * pin_num)); // Set speed bits
break;
case PORTD_ID:
if(mode == PIN_INPUT){
CLEAR_BIT(GPIOD_MODER,(2*pin_num));
CLEAR_BIT(GPIOD_MODER,(2*pin_num+1));
}
else if(mode == PIN_OUTPUT)
{
SET_BIT(GPIOD_MODER,(2*pin_num));
CLEAR_BIT(GPIOD_MODER,(2*pin_num+1));
}
else if(mode == PIN_ALTERNATE_FUNCTION)
{
CLEAR_BIT(GPIOD_MODER,(2*pin_num));
SET_BIT(GPIOD_MODER,(2*pin_num+1));
/* Configure Pull-Up/Pull-Down */
if (pull_resistor == PIN_PULL_UP)
{
SET_BIT(GPIOD_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOD_PUPDR, (2 * pin_num + 1));
}
else if (pull_resistor == PIN_PULL_DOWN)
{
CLEAR_BIT(GPIOD_PUPDR, (2 * pin_num));
SET_BIT(GPIOD_PUPDR, (2 * pin_num + 1));
}
else /* No Pull-Up/Down */
{
CLEAR_BIT(GPIOD_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOD_PUPDR, (2 * pin_num + 1));
}
/* Configure Speed */
GPIOD_OSPEEDR &= ~(0x3 << (2 * pin_num)); // Clear speed bits
GPIOD_OSPEEDR |= (speed << (2 * pin_num)); // Set speed bits
break;
case PORTE_ID:
if(mode == PIN_INPUT){
CLEAR_BIT(GPIOE_MODER,(2*pin_num));
CLEAR_BIT(GPIOE_MODER,(2*pin_num+1));
}
else if(mode == PIN_OUTPUT)
{
SET_BIT(GPIOE_MODER,(2*pin_num));
CLEAR_BIT(GPIOE_MODER,(2*pin_num+1));
}
else if(mode == PIN_ALTERNATE_FUNCTION)
{
CLEAR_BIT(GPIOE_MODER,(2*pin_num));
SET_BIT(GPIOE_MODER,(2*pin_num+1));
/* Configure Pull-Up/Pull-Down */
if (pull_resistor == PIN_PULL_UP)
{
SET_BIT(GPIOE_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOE_PUPDR, (2 * pin_num + 1));
}
else if (pull_resistor == PIN_PULL_DOWN)
{
CLEAR_BIT(GPIOE_PUPDR, (2 * pin_num));
SET_BIT(GPIOE_PUPDR, (2 * pin_num + 1));
}
else /* No Pull-Up/Down */
{
CLEAR_BIT(GPIOE_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOE_PUPDR, (2 * pin_num + 1));
}
/* Configure Speed */
GPIOE_OSPEEDR &= ~(0x3 << (2 * pin_num)); // Clear speed bits
GPIOE_OSPEEDR |= (speed << (2 * pin_num)); // Set speed bits
break;
case PORTH_ID:
if(mode == PIN_INPUT){
CLEAR_BIT(GPIOH_MODER,(2*pin_num));
CLEAR_BIT(GPIOH_MODER,(2*pin_num+1));
}
else if(mode == PIN_OUTPUT)
{
SET_BIT(GPIOH_MODER,(2*pin_num));
CLEAR_BIT(GPIOH_MODER,(2*pin_num+1));
}
else if(mode == PIN_ALTERNATE_FUNCTION)
{
CLEAR_BIT(GPIOH_MODER,(2*pin_num));
SET_BIT(GPIOH_MODER,(2*pin_num+1));
/* Configure Pull-Up/Pull-Down */
if (pull_resistor == PIN_PULL_UP)
{
SET_BIT(GPIOH_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOH_PUPDR, (2 * pin_num + 1));
}
else if (pull_resistor == PIN_PULL_DOWN)
{
CLEAR_BIT(GPIOH_PUPDR, (2 * pin_num));
SET_BIT(GPIOH_PUPDR, (2 * pin_num + 1));
}
else /* No Pull-Up/Down */
{
CLEAR_BIT(GPIOH_PUPDR, (2 * pin_num));
CLEAR_BIT(GPIOH_PUPDR, (2 * pin_num + 1));
}
/* Configure Speed */
GPIOH_OSPEEDR &= ~(0x3 << (2 * pin_num)); // Clear speed bits
GPIOH_OSPEEDR |= (speed << (2 * pin_num)); // Set speed bits
break;
}
}
}
/
*----------------------------------------------------------------------------------
----------------------------------------------
* Function Name: GPIO_writePin
*
* Description : Writes a value to the specified GPIO pin.
*
* Parameters : port_num - The port number (e.g., PORTA_ID, PORTB_ID)
* pin_num - The pin number (e.g., PIN0_ID, PIN1_ID,
PIN2_ID, ..., PIN15_ID)
* value - Value to write to the pin (e.g., LOGIC_HIGH,
LOGIC_LOW)
*
* Return : None
*
*----------------------------------------------------------------------------------
---------------------------------------------- */
/
*----------------------------------------------------------------------------------
----------------------------------------------
* Function Name: GPIO_readPin
*
* Description : Reads the value of the specified GPIO pin.
*
* Parameters : port_num - The port number (e.g., PORTA_ID, PORTB_ID)
* pin_num - The pin number (e.g., PIN0_ID, PIN1_ID, PIN2_ID,
..., PIN15_ID)
*
* Return : The logic level of the pin (LOGIC_HIGH or LOGIC_LOW)
*
*----------------------------------------------------------------------------------
---------------------------------------------- */
/
*----------------------------------------------------------------------------------
----------------------------------------------
* Function Name: GPIO_setupPort
*
* Description : Configures the mode, pull-up/down resistors, speed, and output
type for a specified GPIO port.
*
* Parameters : port_num - The port number to configure (e.g., PORTA_ID,
PORTB_ID, PORTC_ID, etc.)
* mode - GPIO port mode (e.g., PORT_INPUT, PORT_OUTPUT)
* output_type - Output type configuration (e.g.,
PORT_OUTPUT_PUSH_PULL, PORT_OUTPUT_DEFAULT, PORT_OUTPUT_OPEN_DRAIN)
* pull_resistor - Pull-up/down resistor configuration (e.g.,
PORT_NO_PULL, PORT_PULL_UP, PORT_PULL_DOWN)
* speed - Port speed configuration (e.g., PORT_SPEED_LOW,
PORT_SPEED_MEDIUM, PORT_SPEED_HIGH, PORT_SPEED_VERY_HIGH)
*
* Return : None
*
*----------------------------------------------------------------------------------
---------------------------------------------- */
/
*----------------------------------------------------------------------------------
----------------------------------------------
* Function Name: GPIO_writePort
*
* Description : Writes a 16-bit value to the specified GPIO port's Output Data
Register (ODR).
*
* Parameters : port_num - The port number to write to (e.g., PORTA_ID, PORTB_ID,
PORTC_ID, etc.)
* value - The 16-bit value to write to the port's output data
register
*
* Return : None
*
*----------------------------------------------------------------------------------
---------------------------------------------- */
/
*----------------------------------------------------------------------------------
----------------------------------------------
* Function Name: GPIO_readPort
*
* Description : Reads a 16-bit value from the specified GPIO port's Input Data
Register (IDR).
*
* Parameters : port_num - The port number to read from (e.g., PORTA_ID,
PORTB_ID, PORTC_ID, etc.)
*
* Return : 16-bit value representing the current state of the GPIO port's
input data register
*
*----------------------------------------------------------------------------------
---------------------------------------------- */