Unified all current peripheral implementation syntax with macros
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
#include <cassert>
|
||||
|
||||
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<int>(timer_key)];
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user