Midnight Sun Firmware
Loading...
Searching...
No Matches
gpio.h
1#pragma once
2
3/************************************************************************************************
4 * @file gpio.h
5 *
6 * @brief GPIO Library Header file
7 *
8 * @date 2024-11-02
9 * @author Midnight Sun Team #24 - MSXVI
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <stdint.h>
14
15/* Inter-component Headers */
16
17/* Intra-component Headers */
18#include "status.h"
19
26#ifndef GPIO_PINS_PER_PORT
28#define GPIO_PINS_PER_PORT 16U
29#endif
30
31#ifndef GPIO_ADDRESS_OFFSET
33#define GPIO_ADDRESS_OFFSET 0x400U
34#endif
35
37typedef enum {
41} GpioState;
42
44typedef enum {
54} GpioMode;
55
57typedef enum {
58 // No ALT function
59 GPIO_ALT_NONE = 0x00U,
61 // GPIO_ALT0 - System
65 // GPIO_ALT1 - TIM1/TIM2
69 // GPIO_ALT4 - I2C
74 // GPIO_ALT5 - SPI
78 // GPIO_ALT6 - SPI3
81 // GPIO_ALT7 - USART
86 // GPIO_ALT9 - CAN1
89 // GPIO_ALT14 - Timers
93
97typedef enum {
105} GpioPort;
106
107#define GPIO_TOTAL_PINS ((GPIO_PINS_PER_PORT) * (NUM_GPIO_PORTS))
108
112typedef struct GpioAddress {
114 uint8_t pin;
116
124
134StatusCode gpio_init_pin(const GpioAddress *address, const GpioMode pin_mode, GpioState init_state);
135
145StatusCode gpio_init_pin_af(const GpioAddress *address, const GpioMode pin_mode, GpioAlternateFunctions alt_func);
146
154StatusCode gpio_set_state(const GpioAddress *address, GpioState state);
155
163
170GpioState gpio_get_state(const GpioAddress *address);
171
StatusCode gpio_init(void)
Initializes GPIO globally by disabling JTAG and enabling all GPIO clocks.
Definition: gpio.c:29
GpioMode
Available modes for the GPIO pins.
Definition: gpio.h:44
GpioState
High or low GPIO state.
Definition: gpio.h:37
StatusCode gpio_init_pin(const GpioAddress *address, const GpioMode pin_mode, GpioState init_state)
Initializes a GPIO pin by address.
Definition: gpio.c:56
StatusCode gpio_toggle_state(const GpioAddress *address)
Toggles the GPIO.
Definition: gpio.c:170
GpioPort
Available GPIO ports.
Definition: gpio.h:97
StatusCode gpio_init_pin_af(const GpioAddress *address, const GpioMode pin_mode, GpioAlternateFunctions alt_func)
Initializes an alternate function for a GPIO pin by address.
Definition: gpio.c:92
GpioAlternateFunctions
Alternate function mapping. Read STM32L433 datasheet page 75.
Definition: gpio.h:57
StatusCode gpio_set_state(const GpioAddress *address, GpioState state)
Sets the GPIO pin to a valid state.
Definition: gpio.c:154
GpioState gpio_get_state(const GpioAddress *address)
Gets the GPIO state.
Definition: gpio.c:182
@ GPIO_INPUT_PULL_UP
Definition: gpio.h:48
@ GPIO_OUTPUT_OPEN_DRAIN
Definition: gpio.h:49
@ GPIO_ALTFN_OPEN_DRAIN
Definition: gpio.h:51
@ GPIO_OUTPUT_PUSH_PULL
Definition: gpio.h:50
@ GPIO_ALTFN_PUSH_PULL
Definition: gpio.h:52
@ NUM_GPIO_MODES
Definition: gpio.h:53
@ GPIO_INPUT_FLOATING
Definition: gpio.h:46
@ GPIO_INPUT_PULL_DOWN
Definition: gpio.h:47
@ GPIO_ANALOG
Definition: gpio.h:45
@ GPIO_STATE_HIGH
Definition: gpio.h:39
@ NUM_GPIO_STATES
Definition: gpio.h:40
@ GPIO_STATE_LOW
Definition: gpio.h:38
@ GPIO_PORT_H
Definition: gpio.h:103
@ GPIO_PORT_D
Definition: gpio.h:101
@ GPIO_PORT_A
Definition: gpio.h:98
@ GPIO_PORT_E
Definition: gpio.h:102
@ GPIO_PORT_C
Definition: gpio.h:100
@ NUM_GPIO_PORTS
Definition: gpio.h:104
@ GPIO_PORT_B
Definition: gpio.h:99
@ GPIO_ALT1_TIM1
Definition: gpio.h:66
@ GPIO_ALT1_TIM2
Definition: gpio.h:67
@ GPIO_ALT0_SWDIO
Definition: gpio.h:62
@ GPIO_ALT7_USART3
Definition: gpio.h:84
@ GPIO_ALT5_SPI1
Definition: gpio.h:75
@ GPIO_ALT4_I2C1
Definition: gpio.h:70
@ GPIO_ALT_NONE
Definition: gpio.h:59
@ GPIO_ALT0_SWCLK
Definition: gpio.h:63
@ GPIO_ALT9_CAN1
Definition: gpio.h:87
@ GPIO_ALT4_I2C2
Definition: gpio.h:71
@ GPIO_ALT14_TIM16
Definition: gpio.h:91
@ GPIO_ALT5_SPI2
Definition: gpio.h:76
@ GPIO_ALT6_SPI3
Definition: gpio.h:79
@ GPIO_ALT14_TIM15
Definition: gpio.h:90
@ GPIO_ALT4_I2C3
Definition: gpio.h:72
@ GPIO_ALT7_USART1
Definition: gpio.h:82
@ GPIO_ALT7_USART2
Definition: gpio.h:83
StatusCode
StatusCodes for various errors.
Definition: status.h:27
Port and pin data.
Definition: gpio.h:112
GpioPort port
Definition: gpio.h:113
uint8_t pin
Definition: gpio.h:114