Midnight Sun Firmware
Loading...
Searching...
No Matches
gpio_datagram.h
1#pragma once
2
3/************************************************************************************************
4 * @file gpio_datagram.h
5 *
6 * @brief Header file defining the GpioDatagram class
7 *
8 * @date 2025-01-04
9 * @author Aryan Kashem
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <cstdint>
14#include <string>
15
16/* Inter-component Headers */
17
18/* Intra-component Headers */
19#include "command_code.h"
20
27namespace Datagram {
28
36class Gpio {
37 public:
38 static const constexpr uint8_t PINS_PER_PORT = 16U;
43 enum class Port {
44 GPIO_PORT_A = 0,
47 };
48
52 enum class State {
53 GPIO_STATE_LOW = 0,
55 };
56
60 enum class Mode {
61 GPIO_ANALOG = 0,
70 };
71
75 enum class AltFunction {
76 // No ALT function
77 GPIO_ALT_NONE = 0x00U,
78
79 // GPIO_ALT0 - System
80 GPIO_ALT0_SWDIO = 0x00U,
81 GPIO_ALT0_SWCLK = 0x00U,
82
83 // GPIO_ALT1 - TIM1/TIM2
84 GPIO_ALT1_TIM1 = 0x01U,
85 GPIO_ALT1_TIM2 = 0x01U,
86
87 // GPIO_ALT4 - I2C
88 GPIO_ALT4_I2C1 = 0x04U,
89 GPIO_ALT4_I2C2 = 0x04U,
90 GPIO_ALT4_I2C3 = 0x04U,
91
92 // GPIO_ALT5 - SPI
93 GPIO_ALT5_SPI1 = 0x05U,
94 GPIO_ALT5_SPI2 = 0x05U,
95
96 // GPIO_ALT6 - SPI3
97 GPIO_ALT6_SPI3 = 0x06U,
98
99 // GPIO_ALT7 - USART
100 GPIO_ALT7_USART1 = 0x07U,
101 GPIO_ALT7_USART2 = 0x07U,
102 GPIO_ALT7_USART3 = 0x07U,
103
104 // GPIO_ALT9 - CAN1
105 GPIO_ALT9_CAN1 = 0x09U,
106
107 // GPIO_ALT14 - Timers
108 GPIO_ALT14_TIM15 = 0x0EU,
109 GPIO_ALT14_TIM16 = 0x0EU,
110 };
111
113 static constexpr size_t GPIO_MAX_BUFFER_SIZE = PINS_PER_PORT * static_cast<uint8_t>(Port::NUM_GPIO_PORTS);
114
118 struct Payload {
120 uint8_t gpioPin;
121 uint8_t bufferLength;
123 };
124
129 explicit Gpio(Payload &data);
130
134 Gpio() = default;
135
141 std::string serialize(const CommandCode &commandCode) const;
142
147 void deserialize(std::string &gpioDatagramPayload);
148
153 void setGpioPort(const Port &gpioPort);
154
159 void setGpioPin(const uint8_t &gpioPin);
160
166 void setBuffer(const uint8_t *data, uint8_t length);
167
171 void clearBuffer();
172
177 Port getGpioPort() const;
178
183 uint8_t getGpioPin() const;
184
189 uint8_t getBufferLength() const;
190
195 const uint8_t *getBuffer() const;
196
197 private:
199};
200
201} // namespace Datagram
202
Definition: gpio_datagram.h:36
const uint8_t * getBuffer() const
Gets the Gpio buffer.
Definition: gpio_datagram.cc:87
void clearBuffer()
Clear the Gpio data buffer.
Definition: gpio_datagram.cc:71
void setBuffer(const uint8_t *data, uint8_t length)
Sets data in the Gpio buffer.
Definition: gpio_datagram.cc:66
Mode
Gpio Mode definition.
Definition: gpio_datagram.h:60
AltFunction
Gpio Alt function definition.
Definition: gpio_datagram.h:75
static const constexpr uint8_t PINS_PER_PORT
Definition: gpio_datagram.h:38
Port getGpioPort() const
Gets the target Gpio port.
Definition: gpio_datagram.cc:75
Port
Gpio Port definition.
Definition: gpio_datagram.h:43
std::string serialize(const CommandCode &commandCode) const
Serializes Gpio data with command code for transmission.
Definition: gpio_datagram.cc:22
static constexpr size_t GPIO_MAX_BUFFER_SIZE
Definition: gpio_datagram.h:113
uint8_t getBufferLength() const
Gets the Gpio buffer length.
Definition: gpio_datagram.cc:83
void deserialize(std::string &gpioDatagramPayload)
Deserializes Gpio data from payload string.
Definition: gpio_datagram.cc:37
void setGpioPort(const Port &gpioPort)
Sets the target Gpio port.
Definition: gpio_datagram.cc:58
uint8_t getGpioPin() const
Gets the target Gpio pin.
Definition: gpio_datagram.cc:79
Payload m_gpioDatagram
Definition: gpio_datagram.h:198
Gpio()=default
Default constructor for Gpio object.
void setGpioPin(const uint8_t &gpioPin)
Sets the target Gpio pin.
Definition: gpio_datagram.cc:62
CommandCode
Command Code Class.
Definition: command_code.h:28
@ 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
Gpio Datagram payload storage.
Definition: gpio_datagram.h:118
uint8_t bufferLength
Definition: gpio_datagram.h:121
Port gpioPort
Definition: gpio_datagram.h:119
uint8_t gpioPin
Definition: gpio_datagram.h:120
uint8_t buffer[GPIO_MAX_BUFFER_SIZE]
Definition: gpio_datagram.h:122
Definition: fsm.h:35