implmemtation review

pull/22/head
midilab 2 years ago
parent 3a1e52589e
commit 841a6ee68d
  1. 2
      src/platforms/avr.h
  2. 38
      src/platforms/stm32.h
  3. 4
      src/uClock.cpp

@ -3,7 +3,7 @@
#define ATOMIC(X) noInterrupts(); X; interrupts(); #define ATOMIC(X) noInterrupts(); X; interrupts();
// want a different avr clock support? // want a different avr clock support?
// TODO: we should do this using macro guards for avrs different clocks // TODO: we should do this using macro guards for avrs different clocks freqeuncy setup at compile time
#define AVR_CLOCK_FREQ 16000000 #define AVR_CLOCK_FREQ 16000000
void initTimer(uint32_t init_clock) void initTimer(uint32_t init_clock)

@ -1,16 +1,13 @@
#include <Arduino.h> #include <Arduino.h>
static TIM_HandleTypeDef s_TimerInstance = {0}; static TIM_HandleTypeDef _uclockTimer = {0};
typedef void (*timer_callback_t)(void);
timer_callback_t timer_callback = NULL;
#define ATOMIC(X) noInterrupts(); X; interrupts(); #define ATOMIC(X) noInterrupts(); X; interrupts();
// forward declaration of ISR // forward declaration of ISR
void uclockISR(); void uclockISR();
void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t microseconds, timer_callback_t callback) void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t us_interval)
{ {
// Enable timer clock // Enable timer clock
if (tim == TIM2) __HAL_RCC_TIM2_CLK_ENABLE(); if (tim == TIM2) __HAL_RCC_TIM2_CLK_ENABLE();
@ -21,36 +18,33 @@ void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t microseconds, timer_callba
uint32_t prescaler = (SystemCoreClock / 1000000UL) - 1; uint32_t prescaler = (SystemCoreClock / 1000000UL) - 1;
// Calculate the period value // Calculate the period value
uint32_t period = (microseconds * 2UL) - 1UL; uint32_t period = (us_interval * 2UL) - 1UL;
// Set up the timer instance // Set up the timer instance
s_TimerInstance.Instance = tim; _uclockTimer.Instance = tim;
s_TimerInstance.Init.Prescaler = prescaler; _uclockTimer.Init.Prescaler = prescaler;
s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP; _uclockTimer.Init.CounterMode = TIM_COUNTERMODE_UP;
s_TimerInstance.Init.Period = period; _uclockTimer.Init.Period = period;
s_TimerInstance.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; _uclockTimer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
// Configure the timer instance // Configure the timer instance
HAL_TIM_Base_Init(&s_TimerInstance); HAL_TIM_Base_Init(&_uclockTimer);
HAL_TIM_Base_Start_IT(&s_TimerInstance); HAL_TIM_Base_Start_IT(&_uclockTimer);
// Save the callback function
timer_callback = callback;
} }
void TIM2_IRQHandler() void TIM2_IRQHandler()
{ {
// Call the callback function // Call the uClock ISR handler
timer_callback(); uclockISR();
// Clear the interrupt flag // Clear the interrupt flag
__HAL_TIM_CLEAR_FLAG(&s_TimerInstance, TIM_FLAG_UPDATE); __HAL_TIM_CLEAR_FLAG(&_uclockTimer, TIM_FLAG_UPDATE);
} }
void initTimer(uint32_t us_interval) void initTimer(uint32_t us_interval)
{ {
// Set up the timer to call the callback function every us_interval microseconds // Set up the timer to call the callback function every us_interval microseconds
timer_attachInterrupt(TIM2, us_interval, uclockISR); timer_attachInterrupt(TIM2, us_interval);
} }
void setTimer(uint32_t us_interval) void setTimer(uint32_t us_interval)
@ -59,7 +53,7 @@ void setTimer(uint32_t us_interval)
uint32_t period = (us_interval * 2UL) - 1UL; uint32_t period = (us_interval * 2UL) - 1UL;
// Update the timer instance with the new period value // Update the timer instance with the new period value
s_TimerInstance.Init.Period = period; _uclockTimer.Init.Period = period;
HAL_TIM_Base_Init(&s_TimerInstance); HAL_TIM_Base_Init(&_uclockTimer);
} }

@ -34,7 +34,7 @@
#include "platforms/avr.h" #include "platforms/avr.h"
#endif #endif
// //
// Teensyduino port // Teensyduino ARMs port
// //
#if defined(TEENSYDUINO) #if defined(TEENSYDUINO)
#include "platforms/teensy.h" #include "platforms/teensy.h"
@ -52,7 +52,7 @@
#include "platforms/esp32.h" #include "platforms/esp32.h"
#endif #endif
// //
// STM32XX family? // STM32XX family
// //
#if defined(ARDUINO_ARCH_STM32) #if defined(ARDUINO_ARCH_STM32)
#include "platforms/stm32.h" #include "platforms/stm32.h"

Loading…
Cancel
Save