diff --git a/SHAL/Include/Peripheral/I2C/Reg/SHAL_I2C_REG_F072xB.h b/SHAL/Include/Peripheral/I2C/Reg/SHAL_I2C_REG_F072xB.h index 3a70ed7..c3431a2 100644 --- a/SHAL/Include/Peripheral/I2C/Reg/SHAL_I2C_REG_F072xB.h +++ b/SHAL/Include/Peripheral/I2C/Reg/SHAL_I2C_REG_F072xB.h @@ -12,6 +12,8 @@ #define NUM_I2C_BUSES 2 +#define SHAL_I2C1 I2C(1) +#define SHAL_I2C2 I2C(2) enum class I2C_Pair : uint8_t{ //I2C_1 diff --git a/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_F072xB.h b/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_F072xB.h index 8b373ca..f4bf7cd 100644 --- a/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_F072xB.h +++ b/SHAL/Include/Peripheral/Timer/Reg/SHAL_TIM_REG_F072xB.h @@ -29,6 +29,17 @@ enum class Timer_Key : uint8_t { //For STM32F072 S_TIM_INVALID }; +#define SHAL_TIM1 TimerManager::get(Timer_Key::S_TIM1) +#define SHAL_TIM2 TimerManager::get(Timer_Key::S_TIM2) +#define SHAL_TIM3 TimerManager::get(Timer_Key::S_TIM3) +#define SHAL_TIM6 TimerManager::get(Timer_Key::S_TIM6) +#define SHAL_TIM7 TimerManager::get(Timer_Key::S_TIM7) +#define SHAL_TIM14 TimerManager::get(Timer_Key::S_TIM14) +#define SHAL_TIM15 TimerManager::get(Timer_Key::S_TIM15) +#define SHAL_TIM16 TimerManager::get(Timer_Key::S_TIM16) +#define SHAL_TIM17 TimerManager::get(Timer_Key::S_TIM17) + + //Get TIMER_KEY peripheral struct including bus register, enable mask, TIMER_KEY mask constexpr TIM_RCC_Enable getTimerRCC(Timer_Key t) { diff --git a/SHAL/Include/Peripheral/Timer/SHAL_TIM.h b/SHAL/Include/Peripheral/Timer/SHAL_TIM.h index 484b75e..8c431d0 100644 --- a/SHAL/Include/Peripheral/Timer/SHAL_TIM.h +++ b/SHAL/Include/Peripheral/Timer/SHAL_TIM.h @@ -18,6 +18,11 @@ class Timer { friend class TimerManager; public: + /// + /// \param prescaler The amount of times the base clock has to cycle before the timer adds one to the count + /// \param autoReload The number of timer counts before the count is reset and IRQ is called + void init(uint32_t prescaler, uint32_t autoReload); + //Starts the counter void start(); @@ -49,12 +54,16 @@ private: #define getTimer(timer_key) TimerManager::get(timer_key) +#define TIM(num) TimerManager::getTimerFromIndex(num) //Manages all timers so user does not have to personally initialize class TimerManager{ public: static Timer& get(Timer_Key); + + static Timer& getTimerFromIndex(uint8_t index){return timers[index];} + TimerManager() = delete; private: diff --git a/SHAL/Include/Peripheral/UART/Reg/SHAL_UART_REG_F072xB.h b/SHAL/Include/Peripheral/UART/Reg/SHAL_UART_REG_F072xB.h index 63dd06e..975bff5 100644 --- a/SHAL/Include/Peripheral/UART/Reg/SHAL_UART_REG_F072xB.h +++ b/SHAL/Include/Peripheral/UART/Reg/SHAL_UART_REG_F072xB.h @@ -12,6 +12,11 @@ #define NUM_USART_LINES 4 +#define SHAL_UART1 UART(1) +#define SHAL_UART2 UART(2) +#define SHAL_UART3 UART(3) +#define SHAL_UART4 UART(4) + //Valid usart Tx and Rx pairings for STM32F072 enum class UART_Pair : uint8_t{ //UART1 diff --git a/SHAL/Src/Peripheral/Timer/SHAL_TIM.cpp b/SHAL/Src/Peripheral/Timer/SHAL_TIM.cpp index fa0601d..24b39b2 100644 --- a/SHAL/Src/Peripheral/Timer/SHAL_TIM.cpp +++ b/SHAL/Src/Peripheral/Timer/SHAL_TIM.cpp @@ -6,8 +6,7 @@ #include Timer::Timer(Timer_Key t) : TIMER_KEY(t){ - TIM_RCC_Enable rcc = getTimerRCC(TIMER_KEY); - *rcc.busEnableReg |= (1 << rcc.offset); + } Timer::Timer() : TIMER_KEY(Timer_Key::S_TIM_INVALID){ @@ -37,6 +36,14 @@ void Timer::enableInterrupt() { NVIC_EnableIRQ(getIRQn(TIMER_KEY)); } +void Timer::init(uint32_t prescaler, uint32_t autoReload) { + TIM_RCC_Enable rcc = getTimerRCC(TIMER_KEY); + *rcc.busEnableReg |= (1 << rcc.offset); + + setPrescaler(prescaler); + setARR(autoReload); +} + Timer &TimerManager::get(Timer_Key timer_key) { @@ -52,3 +59,5 @@ Timer &TimerManager::get(Timer_Key timer_key) { return timers[static_cast(timer_key)]; } + + diff --git a/SHAL/Src/main.cpp b/SHAL/Src/main.cpp index 898161a..0b50bcc 100644 --- a/SHAL/Src/main.cpp +++ b/SHAL/Src/main.cpp @@ -13,23 +13,20 @@ void tim2Handler(){ int main() { + //Setup UART2 (used by nucleo devices for USB comms) + SHAL_UART2.init(UART_Pair::Tx2A2_Rx2A3); + SHAL_UART2.begin(115200); - UART(2).init(UART_Pair::Tx2A2_Rx2A3); - - UART(2).begin(115200); - + //Use pin C3 to trigger a function on external interrupt PIN(C3).useAsExternalInterrupt(TriggerMode::RISING_EDGE,c3Interrupt); - Timer timer2 = getTimer(Timer_Key::S_TIM2); + SHAL_TIM2.init(8000-1,1500-1); + SHAL_TIM2.setCallbackFunc(tim2Handler); + SHAL_TIM2.start(); PIN(A4).setPinMode(PinMode::OUTPUT_MODE); PIN(A5).setPinMode(PinMode::OUTPUT_MODE); - timer2.setPrescaler(8000 - 1); - timer2.setARR(1500 - 1); - timer2.setCallbackFunc(tim2Handler); - timer2.start(); - while (true) { __WFI(); }