Refactored UART frontent retrieval system

This commit is contained in:
Ea-r-th
2025-09-10 01:43:11 -07:00
parent 2f8ba8d9ee
commit 8f3bd7ebd8
7 changed files with 70 additions and 74 deletions

View File

@@ -369,16 +369,16 @@ typedef struct
typedef struct
{
__IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */
__IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */
__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */
__IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */
__IO uint32_t BSRR; /*!< GPIO port bit set/reset register, Address offset: 0x1A */
__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /*!< GPIO alternate function low register, Address offset: 0x20-0x24 */
__IO uint32_t BRR; /*!< GPIO bit reset register, Address offset: 0x28 */
__IO uint32_t MODER; /*!< SHAL_GPIO port mode register, Address offset: 0x00 */
__IO uint32_t OTYPER; /*!< SHAL_GPIO port output type register, Address offset: 0x04 */
__IO uint32_t OSPEEDR; /*!< SHAL_GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /*!< SHAL_GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint32_t IDR; /*!< SHAL_GPIO port input data register, Address offset: 0x10 */
__IO uint32_t ODR; /*!< SHAL_GPIO port output data register, Address offset: 0x14 */
__IO uint32_t BSRR; /*!< SHAL_GPIO port bit set/reset register, Address offset: 0x1A */
__IO uint32_t LCKR; /*!< SHAL_GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /*!< SHAL_GPIO alternate function low register, Address offset: 0x20-0x24 */
__IO uint32_t BRR; /*!< SHAL_GPIO bit reset register, Address offset: 0x28 */
} GPIO_TypeDef;
/**
@@ -6490,7 +6490,7 @@ typedef struct
/******************************************************************************/
/* */
/* General Purpose IOs (GPIO) */
/* General Purpose IOs (SHAL_GPIO) */
/* */
/******************************************************************************/
/******************* Bit definition for GPIO_MODER register *****************/
@@ -10933,7 +10933,7 @@ typedef struct
((INSTANCE) == DMA1_Channel6) || \
((INSTANCE) == DMA1_Channel7))
/****************************** GPIO Instances ********************************/
/****************************** SHAL_GPIO Instances ********************************/
#define IS_GPIO_ALL_INSTANCE(INSTANCE) (((INSTANCE) == GPIOA) || \
((INSTANCE) == GPIOB) || \
((INSTANCE) == GPIOC) || \
@@ -10941,14 +10941,14 @@ typedef struct
((INSTANCE) == GPIOE) || \
((INSTANCE) == GPIOF))
/**************************** GPIO Alternate Function Instances ***************/
/**************************** SHAL_GPIO Alternate Function Instances ***************/
#define IS_GPIO_AF_INSTANCE(INSTANCE) (((INSTANCE) == GPIOA) || \
((INSTANCE) == GPIOB) || \
((INSTANCE) == GPIOC) || \
((INSTANCE) == GPIOD) || \
((INSTANCE) == GPIOE))
/****************************** GPIO Lock Instances ***************************/
/****************************** SHAL_GPIO Lock Instances ***************************/
#define IS_GPIO_LOCK_INSTANCE(INSTANCE) (((INSTANCE) == GPIOA) || \
((INSTANCE) == GPIOB))
@@ -11192,11 +11192,11 @@ typedef struct
/****************************** TSC Instances *********************************/
#define IS_TSC_ALL_INSTANCE(INSTANCE) ((INSTANCE) == TSC)
/*********************** UART Instances : IRDA mode ***************************/
/*********************** SHAL_UART Instances : IRDA mode ***************************/
#define IS_IRDA_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2))
/********************* UART Instances : Smard card mode ***********************/
/********************* SHAL_UART Instances : Smard card mode ***********************/
#define IS_SMARTCARD_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2))
@@ -11210,35 +11210,35 @@ typedef struct
#define IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2))
/******************** UART Instances : Asynchronous mode **********************/
/******************** SHAL_UART Instances : Asynchronous mode **********************/
#define IS_UART_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2) || \
((INSTANCE) == USART3) || \
((INSTANCE) == USART4))
/******************** UART Instances : Half-Duplex mode **********************/
/******************** SHAL_UART Instances : Half-Duplex mode **********************/
#define IS_UART_HALFDUPLEX_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2) || \
((INSTANCE) == USART3) || \
((INSTANCE) == USART4))
/****************** UART Instances : Hardware Flow control ********************/
/****************** SHAL_UART Instances : Hardware Flow control ********************/
#define IS_UART_HWFLOW_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2) || \
((INSTANCE) == USART3) || \
((INSTANCE) == USART4))
/****************** UART Instances : LIN mode ********************/
/****************** SHAL_UART Instances : LIN mode ********************/
#define IS_UART_LIN_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2))
/****************** UART Instances : wakeup from stop mode ********************/
/****************** SHAL_UART Instances : wakeup from stop mode ********************/
#define IS_UART_WAKEUP_FROMSTOP_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2))
/* Old macro definition maintained for legacy purpose */
#define IS_UART_WAKEUP_INSTANCE IS_UART_WAKEUP_FROMSTOP_INSTANCE
/****************** UART Instances : Driver enable detection ********************/
/****************** SHAL_UART Instances : Driver enable detection ********************/
#define IS_UART_DRIVER_ENABLE_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
((INSTANCE) == USART2) || \
((INSTANCE) == USART3) || \

View File

@@ -20,7 +20,7 @@
X(C0) X(C1) X(C2) X(C3) X(C4) X(C5) X(C6) X(C7) X(C8) X(C9) X(C10) X(C11) X(C12) X(C13) X(C14) X(C15)
//Build enum map of available GPIO pins
//Build enum map of available SHAL_GPIO pins
enum class GPIO_Key : uint8_t {
#define X(key) key,
AVAILABLE_GPIO

View File

@@ -15,8 +15,8 @@
//Abstraction of GPIO registers
class GPIO{
//Abstraction of SHAL_GPIO registers
class SHAL_GPIO{
public:
@@ -43,8 +43,8 @@ private:
friend class GPIOManager;
explicit GPIO(GPIO_Key key);
GPIO();
explicit SHAL_GPIO(GPIO_Key key);
SHAL_GPIO();
GPIO_Key m_GPIO_KEY = GPIO_Key::INVALID;
@@ -54,7 +54,7 @@ private:
//Init GPIO for normal use
//Init SHAL_GPIO for normal use
#define PIN_TO_KEY(name) GPIO_Key::name
#define PIN(name) GPIOManager::get(PIN_TO_KEY(name))
@@ -62,19 +62,19 @@ private:
#define GPIO_A
//Manages instances of GPIO objects
//Manages instances of SHAL_GPIO objects
class GPIOManager{
public:
static GPIO& get(GPIO_Key);
static SHAL_GPIO& get(GPIO_Key);
GPIOManager() = delete;
private:
inline static GPIO m_gpios[AVAILABLE_PORTS][PINS_PER_PORT] = {{}};
inline static SHAL_GPIO m_gpios[AVAILABLE_PORTS][PINS_PER_PORT] = {{}};
};

View File

@@ -2,7 +2,7 @@
******************************************************************************
* @file SHAL_TIM.h
* @author Luca Lizaranzu
* @brief Relating to UART and USART object abstractions
* @brief Relating to SHAL_UART and USART object abstractions
******************************************************************************
*/
@@ -11,11 +11,13 @@
#include "SHAL_UART_REG.h"
class UART{
class SHAL_UART{
friend class UARTManager;
public:
void init(UART_Pair pair);
//begins Tx and Usart TODO either modify this function or add a new one that supports Rx
void begin(uint32_t baudRate) volatile;
@@ -27,30 +29,29 @@ public:
private:
UART() = default; //Initializer for array
SHAL_UART() = default; //Initializer for array
//Creates a UART based on a pair of two valid U(S)ART pins
explicit UART(UART_Pair pair);
//Creates a SHAL_UART based on a pair of two valid U(S)ART pins
UART_Pair m_UARTPair = UART_Pair::INVALID;
};
#define getUART(uart_pair) UARTManager::get(uart_pair)
#define UART(num) UARTManager::get(num)
class UARTManager{
public:
static UART& get(UART_Pair pair);
static SHAL_UART& get(uint8_t uart);
UARTManager() = delete;
private:
inline static UART m_UARTs[NUM_USART_LINES] = {};
inline static SHAL_UART m_UARTs[NUM_USART_LINES] = {};
};

View File

@@ -7,11 +7,11 @@
GPIO::GPIO() : m_GPIO_KEY(GPIO_Key::INVALID){
SHAL_GPIO::SHAL_GPIO() : m_GPIO_KEY(GPIO_Key::INVALID){
//Do not initialize anything
}
GPIO::GPIO(GPIO_Key key) : m_GPIO_KEY(key) {
SHAL_GPIO::SHAL_GPIO(GPIO_Key key) : m_GPIO_KEY(key) {
volatile unsigned long* gpioEnable = getGPIORCCEnable(key).reg;
unsigned long gpioOffset = getGPIORCCEnable(key).offset;
@@ -19,41 +19,41 @@ GPIO::GPIO(GPIO_Key key) : m_GPIO_KEY(key) {
*gpioEnable |= (1 << gpioOffset); //Set enable flag
}
void GPIO::setLow() {
void SHAL_GPIO::setLow() {
auto gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->ODR &= ~(1 << gpioPeripheral.global_offset);
}
void GPIO::setHigh() {
void SHAL_GPIO::setHigh() {
auto gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->ODR |= (1 << gpioPeripheral.global_offset);
}
void GPIO::toggle() volatile {
void SHAL_GPIO::toggle() volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->ODR ^= (1 << gpioPeripheral.global_offset);
}
void GPIO::setPinType(PinType type) volatile {
void SHAL_GPIO::setPinType(PinType type) volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->OTYPER &= ~(1 << gpioPeripheral.global_offset);
gpioPeripheral.reg->OTYPER |= (static_cast<uint8_t>(type) << gpioPeripheral.global_offset);
}
void GPIO::setOutputSpeed(OutputSpeed speed) volatile {
void SHAL_GPIO::setOutputSpeed(OutputSpeed speed) volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->OSPEEDR |= (static_cast<uint8_t>(speed) << (2 * gpioPeripheral.global_offset));
}
void GPIO::setInternalResistor(InternalResistorType type) volatile {
void SHAL_GPIO::setInternalResistor(InternalResistorType type) volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->PUPDR &= ~(0x03 << (2 * gpioPeripheral.global_offset));
gpioPeripheral.reg->PUPDR |= (static_cast<uint8_t>(type) << (2 * gpioPeripheral.global_offset));
}
void GPIO::setAlternateFunction(GPIO_Alternate_Function AF) volatile {
void SHAL_GPIO::setAlternateFunction(GPIO_Alternate_Function AF) volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
int afrIndex = gpioPeripheral.global_offset < 8 ? 0 : 1; //Get index of AFR
@@ -62,13 +62,13 @@ void GPIO::setAlternateFunction(GPIO_Alternate_Function AF) volatile {
gpioPeripheral.reg->AFR[afrIndex] |= (static_cast<int>(AF) << (gpioPeripheral.global_offset * 4));
}
void GPIO::setPinMode(PinMode mode) volatile {
void SHAL_GPIO::setPinMode(PinMode mode) volatile {
SHAL_GPIO_Peripheral gpioPeripheral = getGPIORegister(m_GPIO_KEY);
gpioPeripheral.reg->MODER &= ~(0x03 << (2 * gpioPeripheral.global_offset)); //Clear any previous mode
gpioPeripheral.reg->MODER |= (static_cast<uint8_t>(mode) << (2 * gpioPeripheral.global_offset)); //Set mode based on pinmode bit structure
}
void GPIO::useAsExternalInterrupt(TriggerMode mode, EXTICallback callback) {
void SHAL_GPIO::useAsExternalInterrupt(TriggerMode mode, EXTICallback callback) {
uint32_t gpioPin = getGPIORegister(m_GPIO_KEY).global_offset; //Use existing structs to get offset
@@ -108,13 +108,13 @@ void GPIO::useAsExternalInterrupt(TriggerMode mode, EXTICallback callback) {
}
GPIO& GPIOManager::get(GPIO_Key key) {
SHAL_GPIO& GPIOManager::get(GPIO_Key key) {
unsigned int gpioPort = getGPIOPortNumber(key);
unsigned long gpioPin = getGPIORegister(key).global_offset; //Use existing structs to get offset
if (m_gpios[gpioPort][gpioPin].m_GPIO_KEY == GPIO_Key::INVALID){
m_gpios[gpioPort][gpioPin] = GPIO(key);
m_gpios[gpioPort][gpioPin] = SHAL_GPIO(key);
}
return m_gpios[gpioPort][gpioPin];

View File

@@ -2,7 +2,7 @@
******************************************************************************
* @file SHAL_TIM.h
* @author Luca Lizaranzu
* @brief Related to USART and UART abstractions
* @brief Related to USART and SHAL_UART abstractions
******************************************************************************
*/
@@ -10,10 +10,13 @@
#include "SHAL_UART.h"
#include "SHAL_GPIO.h"
UART::UART(const UART_Pair pair) : m_UARTPair(pair){
void SHAL_UART::init(const UART_Pair pair){
m_UARTPair = pair;
SHAL_UART_Pair uart_pair = getUARTPair(pair); //Get the UART_PAIR information to be initialized
//Get the GPIO pins for this UART setup
//Get the SHAL_GPIO pins for this SHAL_UART setup
GPIO_Key Tx_Key = uart_pair.TxKey; //Tx pin
GPIO_Key Rx_Key = uart_pair.RxKey; //Rx pin
@@ -23,12 +26,12 @@ UART::UART(const UART_Pair pair) : m_UARTPair(pair){
GET_GPIO(Tx_Key).setAlternateFunction(uart_pair.TxAlternateFunctionMask);
GET_GPIO(Rx_Key).setAlternateFunction(uart_pair.RxAlternateFunctionMask);
SHAL_UART_ENABLE_REG pairUARTEnable = getUARTEnableReg(pair); //Register and mask to enable the UART channel
SHAL_UART_ENABLE_REG pairUARTEnable = getUARTEnableReg(pair); //Register and mask to enable the SHAL_UART channel
*pairUARTEnable.reg |= pairUARTEnable.mask; //Enable UART line
*pairUARTEnable.reg |= pairUARTEnable.mask; //Enable SHAL_UART line
}
void UART::begin(uint32_t baudRate) volatile {
void SHAL_UART::begin(uint32_t baudRate) volatile {
USART_TypeDef* usart = getUARTPair(m_UARTPair).USARTReg;
@@ -44,11 +47,11 @@ void UART::begin(uint32_t baudRate) volatile {
}
void UART::sendString(const char *s) volatile {
void SHAL_UART::sendString(const char *s) volatile {
while (*s) sendChar(*s++); //Send chars while we haven't reached end of s
}
void UART::sendChar(char c) volatile {
void SHAL_UART::sendChar(char c) volatile {
USART_TypeDef* usart = getUARTPair(m_UARTPair).USARTReg;
@@ -59,11 +62,6 @@ void UART::sendChar(char c) volatile {
UART& UARTManager::get(UART_Pair pair) {
//Reassign if pair doesn't match
if(m_UARTs[getUARTChannel(pair)].m_UARTPair != pair) {
m_UARTs[getUARTChannel(pair)] = UART(pair);
}
return m_UARTs[getUARTChannel(pair)];
SHAL_UART& UARTManager::get(uint8_t uart) {
return m_UARTs[uart];
}

View File

@@ -2,13 +2,9 @@
#include "stm32f0xx.h"
volatile GPIO* blueLED = nullptr;
volatile GPIO* greenLED = nullptr;
volatile UART* uart2;
void c3Interrupt(){
PIN(A5).toggle();
uart2->sendString("test");
UART(2).sendString("New test");
}
void tim2Handler(){
@@ -17,9 +13,10 @@ void tim2Handler(){
int main() {
uart2 = &getUART(UART_Pair::Tx2A2_Rx2A3);
uart2->begin(115200);
UART(2).init(UART_Pair::Tx2A2_Rx2A3);
UART(2).begin(115200);
PIN(C3).useAsExternalInterrupt(TriggerMode::RISING_EDGE,c3Interrupt);