Changed object array to pointer array for GPIOs

This commit is contained in:
2025-09-01 02:29:03 -07:00
parent 0418e3d5c5
commit db75c06c90
4 changed files with 39 additions and 30 deletions

View File

@@ -7875,7 +7875,7 @@ typedef struct
#define RCC_AHBENR_GPIOEEN RCC_AHBENR_GPIOEEN_Msk /*!< GPIOE clock enable */ #define RCC_AHBENR_GPIOEEN RCC_AHBENR_GPIOEEN_Msk /*!< GPIOE clock enable */
#define RCC_AHBENR_GPIOFEN_Pos (22U) #define RCC_AHBENR_GPIOFEN_Pos (22U)
#define RCC_AHBENR_GPIOFEN_Msk (0x1UL << RCC_AHBENR_GPIOFEN_Pos) /*!< 0x00400000 */ #define RCC_AHBENR_GPIOFEN_Msk (0x1UL << RCC_AHBENR_GPIOFEN_Pos) /*!< 0x00400000 */
#define RCC_AHBENR_GPIOFEN RCC_AHBENR_GPIOFEN_Msk /*!< GPIOF clock enable */ #define RCC_AHBENR_GPIOFEN RCC_AHBENR_GPIOFEN_Msk /*!< GPIOF clock enable */RCC_AHBENR_GPIOAEN
#define RCC_AHBENR_TSCEN_Pos (24U) #define RCC_AHBENR_TSCEN_Pos (24U)
#define RCC_AHBENR_TSCEN_Msk (0x1UL << RCC_AHBENR_TSCEN_Pos) /*!< 0x01000000 */ #define RCC_AHBENR_TSCEN_Msk (0x1UL << RCC_AHBENR_TSCEN_Pos) /*!< 0x01000000 */
#define RCC_AHBENR_TSCEN RCC_AHBENR_TSCEN_Msk /*!< TS controller clock enable */ #define RCC_AHBENR_TSCEN RCC_AHBENR_TSCEN_Msk /*!< TS controller clock enable */

View File

@@ -18,19 +18,7 @@ enum class PinMode {
INVALID INVALID
}; };
unsigned long getPinMode(PinMode mode){ unsigned long getPinMode(PinMode mode);
switch(mode){
case PinMode::INPUT_MODE:
return 0b00;
case PinMode::OUTPUT_MODE:
return 0b01;
case PinMode::ALTERNATE_FUNCTION_MODE:
return 0b10;
case PinMode::ANALOG_MODE:
return 0b11;
}
}
//Abstraction of GPIO registers //Abstraction of GPIO registers
class GPIO{ class GPIO{
@@ -65,12 +53,12 @@ class GPIOManager{
public: public:
static GPIO& get(GPIO_Key, PinMode pinMode); static GPIO* get(GPIO_Key, PinMode pinMode);
GPIOManager() = delete; GPIOManager() = delete;
private: private:
inline static GPIO m_gpios[AVAILABLE_PORTS][PINS_PER_PORT] = {}; inline static GPIO* m_gpios[AVAILABLE_PORTS][PINS_PER_PORT] = {{nullptr}};
}; };

View File

@@ -4,6 +4,27 @@
#include "SHAL_GPIO.h" #include "SHAL_GPIO.h"
unsigned long getPinMode(PinMode mode){
switch(mode){
case PinMode::INPUT_MODE:
return 0b00;
case PinMode::OUTPUT_MODE:
return 0b01;
case PinMode::ALTERNATE_FUNCTION_MODE:
return 0b10;
case PinMode::ANALOG_MODE:
return 0b11;
case PinMode::INVALID:
assert(false);
return 0;
}
__builtin_unreachable();
}
GPIO::GPIO(){ GPIO::GPIO(){
//Do not initialize anything //Do not initialize anything
} }
@@ -15,9 +36,10 @@ GPIO::GPIO(GPIO_Key key, PinMode pinMode) : m_GPIO_KEY(key),
volatile unsigned long* gpioEnable = getGPIORCCEnable(key).reg; volatile unsigned long* gpioEnable = getGPIORCCEnable(key).reg;
unsigned long gpioOffset = getGPIORCCEnable(key).offset; unsigned long gpioOffset = getGPIORCCEnable(key).offset;
*gpioEnable |= (1 << gpioOffset); *gpioEnable |= (1 << gpioOffset); //Set enable flag
p_GPIORegister->MODER |= (getPinMode(pinMode) << (2 * m_offset)); p_GPIORegister->MODER &= ~(0b11 << (2 * m_offset)); //Clear any previous mode
p_GPIORegister->MODER |= (getPinMode(pinMode) << (2 * m_offset)); //Set mode based on pinmode bit structure
} }
void GPIO::setLow() { void GPIO::setLow() {
@@ -35,15 +57,15 @@ void GPIO::toggle() {
GPIO &GPIOManager::get(GPIO_Key key, PinMode pinMode) { GPIO* GPIOManager::get(GPIO_Key key, PinMode pinMode) {
unsigned int gpioPort = getGPIOPortNumber(key); unsigned int gpioPort = getGPIOPortNumber(key);
unsigned long gpioPin = getGPIORegister(key).global_offset; //Use existing structs to get offset unsigned long gpioPin = getGPIORegister(key).global_offset; //Use existing structs to get offset
GPIO curr = m_gpios[gpioPort][gpioPin]; GPIO curr = *m_gpios[gpioPort][gpioPin];
if(curr.m_GPIO_KEY == GPIO_Key::INVALID || curr.m_pinMode != pinMode){ if(curr.m_GPIO_KEY == GPIO_Key::INVALID || curr.m_pinMode != pinMode){
m_gpios[gpioPort][gpioPin] = GPIO(key,pinMode); *m_gpios[gpioPort][gpioPin] = GPIO(key,pinMode);
} }
return m_gpios[gpioPort][gpioPin]; return m_gpios[gpioPort][gpioPin];

View File

@@ -1,23 +1,28 @@
#include "SHAL.h" #include "SHAL.h"
#include "stm32f0xx.h" #include "stm32f0xx.h"
GPIO* blueLED;
GPIO* greenLED;
extern "C" void EXTI0_1_IRQHandler(void) { extern "C" void EXTI0_1_IRQHandler(void) {
if (EXTI->PR & (1 << 0)) { //Check pending flag if (EXTI->PR & (1 << 0)) { //Check pending flag
EXTI->PR |= (1 << 0); //Clear it by writing 1 EXTI->PR |= (1 << 0); //Clear it by writing 1
GPIOA->ODR ^= (1 << 5); blueLED->toggle();
} }
} }
void tim2Handler(){ void tim2Handler(){
GPIOA->ODR ^= (1 << 4); greenLED->toggle();
} }
int main() { int main() {
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
Timer timer2 = getTimer(Timer_Key::S_TIM2); Timer timer2 = getTimer(Timer_Key::S_TIM2);
blueLED = initGPIO(GPIO_Key::A4, PinMode::OUTPUT_MODE);
greenLED = initGPIO(GPIO_Key::A5, PinMode::OUTPUT_MODE);
timer2.setPrescaler(8000 - 1); timer2.setPrescaler(8000 - 1);
timer2.setARR(1500 - 1); timer2.setARR(1500 - 1);
timer2.setCallbackFunc(tim2Handler); timer2.setCallbackFunc(tim2Handler);
@@ -26,12 +31,6 @@ int main() {
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; //Enable SYSCFG clock (needed for EXTI) RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; //Enable SYSCFG clock (needed for EXTI)
GPIOA->MODER &= ~(0b11 << (4 * 2));
GPIOA->MODER |= (0b1 << (4 * 2));
GPIOA->MODER &= ~(0x3 << (5 * 2));
GPIOA->MODER |= (0x1 << (5 * 2));
GPIOB->MODER &= ~(0x3 << (0 * 2)); GPIOB->MODER &= ~(0x3 << (0 * 2));
GPIOB->MODER |= (0x0 << (0 * 2)); GPIOB->MODER |= (0x0 << (0 * 2));