Loading...
Searching...
No Matches
gpiopin.c
1/***********************************************************************************
2 * @file gpiopin.c *
3 * @author Matt Ricci *
4 * @addtogroup GPIO_Pin GPIO Pin *
5 * @brief Implements functions for GPIO peripheral pin interface. *
6 * Includes methods to initialise, set, reset and update the config *
7 * of an individual GPIO pin. *
8 ***********************************************************************************/
9
10#include "gpiopin.h"
11#include "stddef.h"
12
13static void _GPIOpin_init(GPIO_TypeDef *, GPIO_Pin, GPIO_Config *);
14
15/* =============================================================================== */
29 // Early return error struct if port is NULL
30 if (port == NULL)
31 return (GPIOpin_t){.port = NULL};
32
33 // Create GPIO struct from parameters and initialise methods
34 GPIOpin_t gpio;
35 gpio.port = port;
36 gpio.pin = pin;
37 gpio.set = GPIOpin_set;
38 gpio.reset = GPIOpin_reset;
41
42 // Update config and enable peripheral
43 GPIOpin_updateConfig(&gpio, config);
44
45 // Return the new GPIO struct
46 return gpio;
47}
48
49// ALLOW FORMATTING
50#ifndef DOXYGEN_PRIVATE
51
52/* =============================================================================== */
64static void _GPIOpin_init(GPIO_TypeDef *port, GPIO_Pin pin, GPIO_Config *config) {
65
66 // Get index of supplied port by subtracting GPIOA address and dividing by size
67 int portIndex = ((uint32_t)port - GPIOA_BASE) / GPIO_PERIPHERAL_SIZE;
68
69 port->MODER &= ~(0b11 << (2 * pin)); // Clear MODER bits for pin
70 port->MODER |= (config->mode << (2 * pin)); // Shift in mode bits from config
71
72 port->OTYPER &= ~(0b01 << pin); // Clear OTYPE bits for pin
73 port->OTYPER |= (config->type << pin); // Shift in type bits from config
74
75 port->OSPEEDR &= ~(0b11 << (2 * pin)); // Clear OSPEEDR bits for pin
76 port->OSPEEDR |= (config->speed << (2 * pin)); // Shift in speed bits from config
77
78 port->PUPDR &= ~(0b11 << (2 * pin)); // Clear PUPDR bits for pin
79 port->PUPDR |= (config->pupd << (2 * pin)); // Shift in pupd bits from config
80
81 volatile uint32_t *afr = (pin <= 7) ? &port->AFR[0] : &port->AFR[1]; // Select AFRL (pin<=7) or AFRH (pin>7)
82 *afr &= ~(0b1111 << (4 * (pin & 7))); // Clear AFR bits for pin
83 *afr |= (config->afr << (4 * (pin & 7))); // Shift in afr bits from config
84}
85
86#endif
87
88/* =============================================================================== */
99 gpio->port->ODR |= (0b01 << gpio->pin);
100}
101
102/* =============================================================================== */
113 gpio->port->ODR &= ~(0b01 << gpio->pin);
114}
115
116/* =============================================================================== */
127 gpio->port->ODR ^= (0b01 << gpio->pin);
128}
129
130/* =============================================================================== */
143 // Initialise config with default values if passed NULL.
144 if (config == NULL) {
145 config = &GPIO_CONFIG_DEFAULT;
146 }
147
148 // Update peripheral with new config
149 gpio->config = *config;
150
151 // Initialise GPIO registers and enable peripheral
152 _GPIOpin_init(gpio->port, gpio->pin, config);
153}
GPIO_Speed speed
Pin output speed | (default GPIO_SPEED_HIGH)
Definition gpiopin.h:141
GPIO_PUPD pupd
Pin pull-up/pull-down | (default GPIO_PUPD_NONE)
Definition gpiopin.h:142
void(* updateConfig)(struct GPIOpin *, GPIO_Config *)
Definition gpiopin.h:158
GPIO_Mode mode
Pin I/O direction | (default GPIO_MODE_OUTPUT)
Definition gpiopin.h:139
GPIO_AF afr
Pin alternate function | (default GPIO_AF0)
Definition gpiopin.h:143
GPIO_TypeDef * port
GPIO port in which the pin is located.
Definition gpiopin.h:152
GPIO_Config config
Configuration parameters for the pin.
Definition gpiopin.h:154
void(* toggle)(struct GPIOpin *)
Definition gpiopin.h:157
GPIO_Type type
Pin output type | (default GPIO_TYPE_PUSHPULL)
Definition gpiopin.h:140
GPIO_Pin pin
Actual location of the pin within GPIO port.
Definition gpiopin.h:153
void(* set)(struct GPIOpin *)
Definition gpiopin.h:155
void(* reset)(struct GPIOpin *)
Definition gpiopin.h:156
void GPIOpin_toggle(GPIOpin_t *)
Toggle the selected GPIO pin.
Definition gpiopin.c:126
void GPIOpin_updateConfig(GPIOpin_t *, GPIO_Config *)
Update GPIO pin configuration.
Definition gpiopin.c:142
GPIO_Pin
GPIO pin enum.
Definition gpiopin.h:49
GPIOpin_t GPIOpin_init(GPIO_TypeDef *port, GPIO_Pin pin, GPIO_Config *config)
Initialiser for a GPIO peripheral pin interface.
Definition gpiopin.c:28
void GPIOpin_set(GPIOpin_t *)
Set the selected GPIO pin.
Definition gpiopin.c:98
void GPIOpin_reset(GPIOpin_t *)
Clear the selected GPIO pin.
Definition gpiopin.c:112
Struct definition for GPIO configuration.
Definition gpiopin.h:138
Struct definition for a GPIO pin.
Definition gpiopin.h:151
__IO uint32_t MODER
__IO uint32_t OSPEEDR
__IO uint32_t OTYPER
__IO uint32_t AFR[2]
__IO uint32_t PUPDR
__IO uint32_t ODR
General Purpose I/O.