0% found this document useful (0 votes)
8 views15 pages

Gpio-1 001

The document is a source file for a GPIO driver specifically designed for the STM32F411CEU6 microcontroller. It includes functions for configuring and controlling GPIO pins and ports, with features such as pin setup, data manipulation, and support for alternate functions. The file also outlines dependencies and provides a detailed function for setting up GPIO pins with various configurations.

Uploaded by

Diaa Abossrie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Gpio-1 001

The document is a source file for a GPIO driver specifically designed for the STM32F411CEU6 microcontroller. It includes functions for configuring and controlling GPIO pins and ports, with features such as pin setup, data manipulation, and support for alternate functions. The file also outlines dependencies and provides a detailed function for setting up GPIO pins with various configurations.

Uploaded by

Diaa Abossrie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

/

***********************************************************************************
*******
* 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
*

*----------------------------------------------------------------------------------
---------------------------------------------- */

void GPIO_setupPin(uint8 port_num, uint8 pin_num, GPIO_PinModeType mode,


GPIO_PinOutputType output_type, GPIO_PinPuPdType pull_resistor, GPIO_PinSpeedType
speed, GPIO_AlternateFunctionType alt_func)
{
if((pin_num >= NUM_OF_PINS_PER_PORT) || (port_num >= NUM_OF_PORTS))
{
/* Do Nothing*/
}
else
{
switch(port_num)
{
case PORTA_ID:
if(mode == PIN_INPUT){
CLEAR_BIT(GPIOA_MODER,(2*pin_num));
CLEAR_BIT(GPIOA_MODER,(2*pin_num+1));
}
else if(mode == PIN_OUTPUT)
{
SET_BIT(GPIOA_MODER,(2*pin_num));
CLEAR_BIT(GPIOA_MODER,(2*pin_num+1));
}
else if(mode == PIN_ALTERNATE_FUNCTION)
{
CLEAR_BIT(GPIOA_MODER,(2*pin_num));
SET_BIT(GPIOA_MODER,(2*pin_num+1));

/* Configure Alternate Function */


if (pin_num < 8) {
GPIOA_AFRL &= ~(0xF << (4 * pin_num)); // Clear AF bits
GPIOA_AFRL |= (alt_func << (4 * pin_num)); // Set AF bits
} else {
GPIOA_AFRH &= ~(0xF << (4 * (pin_num - 8))); // Clear AF bits
GPIOA_AFRH |= (alt_func << (4 * (pin_num - 8))); // Set AF
bits
}

}else if(mode == PIN_ANALOG)


{
SET_BIT(GPIOA_MODER,(2*pin_num));
SET_BIT(GPIOA_MODER,(2*pin_num+1));
}

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

if((output_type == PIN_OUTPUT_PUSH_PULL) || (output_type ==


PIN_OUTPUT_DEFAULT)){
CLEAR_BIT(GPIOA_OTYPER, (pin_num));
}else{
SET_BIT(GPIOA_OTYPER, (pin_num));
}

/* 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 Alternate Function */


if (pin_num < 8) {
GPIOB_AFRL &= ~(0xF << (4 * pin_num)); // Clear AF bits
GPIOB_AFRL |= (alt_func << (4 * pin_num)); // Set AF bits
} else {
GPIOB_AFRH &= ~(0xF << (4 * (pin_num - 8))); // Clear AF bits
GPIOB_AFRH |= (alt_func << (4 * (pin_num - 8))); // Set AF
bits
}

}else if(mode == PIN_ANALOG)


{
SET_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));
}

if((output_type == PIN_OUTPUT_PUSH_PULL) || (output_type ==


PIN_OUTPUT_DEFAULT)){
CLEAR_BIT(GPIOB_OTYPER, (pin_num));
}else{
SET_BIT(GPIOB_OTYPER, (pin_num));
}

/* 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 Alternate Function */


if (pin_num < 8) {
GPIOC_AFRL &= ~(0xF << (4 * pin_num)); // Clear AF bits
GPIOC_AFRL |= (alt_func << (4 * pin_num)); // Set AF bits
} else {
GPIOC_AFRH &= ~(0xF << (4 * (pin_num - 8))); // Clear AF bits
GPIOC_AFRH |= (alt_func << (4 * (pin_num - 8))); // Set AF
bits
}

}else if(mode == PIN_ANALOG)


{
SET_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));
}

if((output_type == PIN_OUTPUT_PUSH_PULL) || (output_type ==


PIN_OUTPUT_DEFAULT)){
CLEAR_BIT(GPIOC_OTYPER, (pin_num));
}else{
SET_BIT(GPIOC_OTYPER, (pin_num));
}

/* 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 Alternate Function */


if (pin_num < 8) {
GPIOD_AFRL &= ~(0xF << (4 * pin_num)); // Clear AF bits
GPIOD_AFRL |= (alt_func << (4 * pin_num)); // Set AF bits
} else {
GPIOD_AFRH &= ~(0xF << (4 * (pin_num - 8))); // Clear AF bits
GPIOD_AFRH |= (alt_func << (4 * (pin_num - 8))); // Set AF
bits
}

}else if(mode == PIN_ANALOG)


{
SET_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));
}

if((output_type == PIN_OUTPUT_PUSH_PULL) || (output_type ==


PIN_OUTPUT_DEFAULT)){
CLEAR_BIT(GPIOD_OTYPER, (pin_num));
}else{
SET_BIT(GPIOD_OTYPER, (pin_num));
}

/* 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 Alternate Function */


if (pin_num < 8) {
GPIOE_AFRL &= ~(0xF << (4 * pin_num)); // Clear AF bits
GPIOE_AFRL |= (alt_func << (4 * pin_num)); // Set AF bits
} else {
GPIOE_AFRH &= ~(0xF << (4 * (pin_num - 8))); // Clear AF bits
GPIOE_AFRH |= (alt_func << (4 * (pin_num - 8))); // Set AF
bits
}

}else if(mode == PIN_ANALOG)


{
SET_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));
}

if((output_type == PIN_OUTPUT_PUSH_PULL) || (output_type ==


PIN_OUTPUT_DEFAULT)){
CLEAR_BIT(GPIOE_OTYPER, (pin_num));
}else{
SET_BIT(GPIOE_OTYPER, (pin_num));
}

/* 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 Alternate Function */


if (pin_num < 8) {
GPIOH_AFRL &= ~(0xF << (4 * pin_num)); // Clear AF bits
GPIOH_AFRL |= (alt_func << (4 * pin_num)); // Set AF bits
} else {
GPIOA_AFRH &= ~(0xF << (4 * (pin_num - 8))); // Clear AF bits
GPIOA_AFRH |= (alt_func << (4 * (pin_num - 8))); // Set AF
bits
}

}else if(mode == PIN_ANALOG)


{
SET_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));
}

if((output_type == PIN_OUTPUT_PUSH_PULL) || (output_type ==


PIN_OUTPUT_DEFAULT)){
CLEAR_BIT(GPIOH_OTYPER, (pin_num));
}else{
SET_BIT(GPIOH_OTYPER, (pin_num));
}

/* 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
*

*----------------------------------------------------------------------------------
---------------------------------------------- */

void GPIO_writePin(uint8 port_num, uint8 pin_num, uint8 value)


{
if((pin_num >= NUM_OF_PINS_PER_PORT) || (port_num >= NUM_OF_PORTS))
{
/* Do Nothing*/
}
else
{
switch(port_num)
{
case PORTA_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(GPIOA_ODR,pin_num);
}
else
{
CLEAR_BIT(GPIOA_ODR,pin_num);
}
break;
case PORTB_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(GPIOB_ODR,pin_num);
}
else
{
CLEAR_BIT(GPIOB_ODR,pin_num);
}
break;
case PORTC_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(GPIOC_ODR,pin_num);
}
else
{
CLEAR_BIT(GPIOC_ODR,pin_num);
}
break;
case PORTD_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(GPIOD_ODR,pin_num);
}
else
{
CLEAR_BIT(GPIOD_ODR,pin_num);
}
break;
case PORTE_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(GPIOE_ODR,pin_num);
}
else
{
CLEAR_BIT(GPIOE_ODR,pin_num);
}
break;
case PORTH_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(GPIOH_ODR,pin_num);
}
else
{
CLEAR_BIT(GPIOH_ODR,pin_num);
}
break;
}
}
}

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

*----------------------------------------------------------------------------------
---------------------------------------------- */

uint8 GPIO_readPin(uint8 port_num, uint8 pin_num)


{
if((pin_num >= NUM_OF_PINS_PER_PORT) || (port_num >= NUM_OF_PORTS))
{
/* Do Nothing*/
}
else
{
switch(port_num)
{
case PORTA_ID:
if(BIT_IS_SET(GPIOA_IDR,pin_num)){
return(LOGIC_HIGH);
}
else{
return(LOGIC_LOW);
}
break;
case PORTB_ID:
if(BIT_IS_SET(GPIOB_IDR,pin_num)){
return(LOGIC_HIGH);
}
else{
return(LOGIC_LOW);
}
break;
case PORTC_ID:
if(BIT_IS_SET(GPIOC_IDR,pin_num)){
return(LOGIC_HIGH);
}
else{
return(LOGIC_LOW);
}
break;
case PORTD_ID:
if(BIT_IS_SET(GPIOD_IDR,pin_num)){
return(LOGIC_HIGH);
}
else{
return(LOGIC_LOW);
}
break;
case PORTE_ID:
if(BIT_IS_SET(GPIOE_IDR,pin_num)){
return(LOGIC_HIGH);
}
else{
return(LOGIC_LOW);
}
break;
case PORTH_ID:
if(BIT_IS_SET(GPIOH_IDR,pin_num)){
return(LOGIC_HIGH);
}
else{
return(LOGIC_LOW);
}
break;
}
}
}

/
*----------------------------------------------------------------------------------
----------------------------------------------
* 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
*

*----------------------------------------------------------------------------------
---------------------------------------------- */

void GPIO_setupPort(uint8 port_num, GPIO_PortModeType mode, GPIO_PortOutputType


output_type, GPIO_PortPuPdType pull_resistor, GPIO_PortSpeedType speed){
if(port_num >= NUM_OF_PORTS)
{
/* Do Nothing*/
}
else
{
switch(port_num){
case PORTA_ID:
if((mode == PORT_INPUT) || (mode == PORT_OUTPUT)){
GPIOA_MODER = mode;
}
if((pull_resistor == PORT_NO_PULL) || (pull_resistor ==
PORT_PULL_UP) || (pull_resistor == PORT_PULL_DOWN)){
GPIOA_PUPDR = pull_resistor;
}
if((speed == PORT_SPEED_LOW) || (speed == PORT_SPEED_MEDIUM) ||
(speed == PORT_SPEED_HIGH) || (speed == PORT_SPEED_VERY_HIGH)){
GPIOA_OSPEEDR = speed;
}
if((output_type == PORT_OUTPUT_PUSH_PULL) || (output_type ==
PORT_OUTPUT_DEFAULT) || (output_type == PORT_OUTPUT_OPEN_DRAIN)){
GPIOA_OTYPER = output_type;
}
break;
case PORTB_ID:
if((mode == PORT_INPUT) || (mode == PORT_OUTPUT)){
GPIOB_MODER = mode;
}
if((pull_resistor == PORT_NO_PULL) || (pull_resistor ==
PORT_PULL_UP) || (pull_resistor == PORT_PULL_DOWN)){
GPIOB_PUPDR = pull_resistor;
}
if((speed == PORT_SPEED_LOW) || (speed == PORT_SPEED_MEDIUM) ||
(speed == PORT_SPEED_HIGH) || (speed == PORT_SPEED_VERY_HIGH)){
GPIOB_OSPEEDR = speed;
}
if((output_type == PORT_OUTPUT_PUSH_PULL) || (output_type ==
PORT_OUTPUT_DEFAULT) || (output_type == PORT_OUTPUT_OPEN_DRAIN)){
GPIOB_OTYPER = output_type;
}
break;
case PORTC_ID:
if((mode == PORT_INPUT) || (mode == PORT_OUTPUT)){
GPIOC_MODER = mode;
}
if((pull_resistor == PORT_NO_PULL) || (pull_resistor ==
PORT_PULL_UP) || (pull_resistor == PORT_PULL_DOWN)){
GPIOC_PUPDR = pull_resistor;
}
if((speed == PORT_SPEED_LOW) || (speed == PORT_SPEED_MEDIUM) ||
(speed == PORT_SPEED_HIGH) || (speed == PORT_SPEED_VERY_HIGH)){
GPIOC_OSPEEDR = speed;
}
if((output_type == PORT_OUTPUT_PUSH_PULL) || (output_type ==
PORT_OUTPUT_DEFAULT) || (output_type == PORT_OUTPUT_OPEN_DRAIN)){
GPIOC_OTYPER = output_type;
}
break;
case PORTD_ID:
if((mode == PORT_INPUT) || (mode == PORT_OUTPUT)){
GPIOD_MODER = mode;
}
if((pull_resistor == PORT_NO_PULL) || (pull_resistor ==
PORT_PULL_UP) || (pull_resistor == PORT_PULL_DOWN)){
GPIOD_PUPDR = pull_resistor;
}
if((speed == PORT_SPEED_LOW) || (speed == PORT_SPEED_MEDIUM) ||
(speed == PORT_SPEED_HIGH) || (speed == PORT_SPEED_VERY_HIGH)){
GPIOD_OSPEEDR = speed;
}
if((output_type == PORT_OUTPUT_PUSH_PULL) || (output_type ==
PORT_OUTPUT_DEFAULT) || (output_type == PORT_OUTPUT_OPEN_DRAIN)){
GPIOD_OTYPER = output_type;
}
break;
case PORTE_ID:
if((mode == PORT_INPUT) || (mode == PORT_OUTPUT)){
GPIOE_MODER = mode;
}
if((pull_resistor == PORT_NO_PULL) || (pull_resistor ==
PORT_PULL_UP) || (pull_resistor == PORT_PULL_DOWN)){
GPIOE_PUPDR = pull_resistor;
}
if((speed == PORT_SPEED_LOW) || (speed == PORT_SPEED_MEDIUM) ||
(speed == PORT_SPEED_HIGH) || (speed == PORT_SPEED_VERY_HIGH)){
GPIOE_OSPEEDR = speed;
}
if((output_type == PORT_OUTPUT_PUSH_PULL) || (output_type ==
PORT_OUTPUT_DEFAULT) || (output_type == PORT_OUTPUT_OPEN_DRAIN)){
GPIOE_OTYPER = output_type;
}
break;
case PORTH_ID:
if((mode == PORT_INPUT) || (mode == PORT_OUTPUT)){
GPIOH_MODER = mode;
}
if((pull_resistor == PORT_NO_PULL) || (pull_resistor ==
PORT_PULL_UP) || (pull_resistor == PORT_PULL_DOWN)){
GPIOH_PUPDR = pull_resistor;
}
if((speed == PORT_SPEED_LOW) || (speed == PORT_SPEED_MEDIUM) ||
(speed == PORT_SPEED_HIGH) || (speed == PORT_SPEED_VERY_HIGH)){
GPIOH_OSPEEDR = speed;
}
if((output_type == PORT_OUTPUT_PUSH_PULL) || (output_type ==
PORT_OUTPUT_DEFAULT) || (output_type == PORT_OUTPUT_OPEN_DRAIN)){
GPIOH_OTYPER = output_type;
}
break;
}
}
}

/
*----------------------------------------------------------------------------------
----------------------------------------------
* 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
*

*----------------------------------------------------------------------------------
---------------------------------------------- */

void GPIO_writePort(uint8 port_num, uint16 value){


if(port_num >= NUM_OF_PORTS)
{
/* Do Nothing*/
}
else
{
switch(port_num){
case PORTA_ID:
GPIOA_ODR = value;
break;
case PORTB_ID:
GPIOB_ODR = value;
break;
case PORTC_ID:
GPIOC_ODR = value;
break;
case PORTD_ID:
GPIOD_ODR = value;
break;
case PORTE_ID:
GPIOE_ODR = value;
break;
case PORTH_ID:
GPIOH_ODR = value;
break;
}
}
}

/
*----------------------------------------------------------------------------------
----------------------------------------------
* 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
*

*----------------------------------------------------------------------------------
---------------------------------------------- */

uint16 GPIO_readPort(uint8 port_num){


if(port_num >= NUM_OF_PORTS)
{
/* Do Nothing*/
}
else
{
switch(port_num){
case PORTA_ID:
return GPIOA_IDR;
break;
case PORTB_ID:
return GPIOB_IDR;
break;
case PORTC_ID:
return GPIOC_IDR;
break;
case PORTD_ID:
return GPIOD_IDR;
break;
case PORTE_ID:
return GPIOE_IDR;
break;
case PORTH_ID:
return GPIOH_IDR;
break;
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy