diff --git a/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h b/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h index 849bade..6fb73b2 100644 --- a/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h +++ b/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h @@ -7875,7 +7875,7 @@ typedef struct #define RCC_AHBENR_GPIOEEN RCC_AHBENR_GPIOEEN_Msk /*!< GPIOE clock enable */ #define RCC_AHBENR_GPIOFEN_Pos (22U) #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_Msk (0x1UL << RCC_AHBENR_TSCEN_Pos) /*!< 0x01000000 */ #define RCC_AHBENR_TSCEN RCC_AHBENR_TSCEN_Msk /*!< TS controller clock enable */ diff --git a/SHAL/Include/Peripheral/GPIO/SHAL_GPIO.h b/SHAL/Include/Peripheral/GPIO/SHAL_GPIO.h index 29d0a62..eb5130f 100644 --- a/SHAL/Include/Peripheral/GPIO/SHAL_GPIO.h +++ b/SHAL/Include/Peripheral/GPIO/SHAL_GPIO.h @@ -18,19 +18,7 @@ enum class PinMode { INVALID }; -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; - } -} - +unsigned long getPinMode(PinMode mode); //Abstraction of GPIO registers class GPIO{ @@ -65,12 +53,12 @@ class GPIOManager{ public: - static GPIO& get(GPIO_Key, PinMode pinMode); + static GPIO* get(GPIO_Key, PinMode pinMode); GPIOManager() = delete; private: -inline static GPIO m_gpios[AVAILABLE_PORTS][PINS_PER_PORT] = {}; +inline static GPIO* m_gpios[AVAILABLE_PORTS][PINS_PER_PORT] = {{nullptr}}; }; diff --git a/SHAL/Src/Peripheral/GPIO/SHAL_GPIO.cpp b/SHAL/Src/Peripheral/GPIO/SHAL_GPIO.cpp index c5c992a..9d1c3b6 100644 --- a/SHAL/Src/Peripheral/GPIO/SHAL_GPIO.cpp +++ b/SHAL/Src/Peripheral/GPIO/SHAL_GPIO.cpp @@ -4,6 +4,27 @@ #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(){ //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; 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() { @@ -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 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){ - m_gpios[gpioPort][gpioPin] = GPIO(key,pinMode); + *m_gpios[gpioPort][gpioPin] = GPIO(key,pinMode); } return m_gpios[gpioPort][gpioPin]; diff --git a/SHAL/Src/main.cpp b/SHAL/Src/main.cpp index 43a2e66..1ab354f 100644 --- a/SHAL/Src/main.cpp +++ b/SHAL/Src/main.cpp @@ -1,23 +1,28 @@ #include "SHAL.h" #include "stm32f0xx.h" +GPIO* blueLED; +GPIO* greenLED; + extern "C" void EXTI0_1_IRQHandler(void) { if (EXTI->PR & (1 << 0)) { //Check pending flag EXTI->PR |= (1 << 0); //Clear it by writing 1 - GPIOA->ODR ^= (1 << 5); + blueLED->toggle(); } } void tim2Handler(){ - GPIOA->ODR ^= (1 << 4); + greenLED->toggle(); } int main() { - RCC->AHBENR |= RCC_AHBENR_GPIOAEN; RCC->AHBENR |= RCC_AHBENR_GPIOBEN; 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.setARR(1500 - 1); timer2.setCallbackFunc(tim2Handler); @@ -26,12 +31,6 @@ int main() { 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 |= (0x0 << (0 * 2));