diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-15 19:52:56 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-15 19:52:56 +0100 |
commit | 4d7f4eb6a924f4df458eda2e624f429d67ef2248 (patch) | |
tree | 84b51ea3c49ab375e0bdfaa0c4c29885c610e7d5 /stmhal/timer.c | |
parent | e95da5b784b290c81cf670b7094d4ea8ea754bc5 (diff) | |
download | micropython-4d7f4eb6a924f4df458eda2e624f429d67ef2248.tar.gz micropython-4d7f4eb6a924f4df458eda2e624f429d67ef2248.zip |
stmhal: Add ADC function to read data at a given frequency.
Reads ADC values into a bytearray (or similar) at a fixed rate. Needs a
better name and improved API. Also fix up DAC dma function (which also
needs a better name and API).
Diffstat (limited to 'stmhal/timer.c')
-rw-r--r-- | stmhal/timer.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/stmhal/timer.c b/stmhal/timer.c index 1e77f0fea3..5ea605039d 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -27,6 +27,7 @@ TIM_HandleTypeDef TIM3_Handle; TIM_HandleTypeDef TIM5_Handle; +TIM_HandleTypeDef TIM6_Handle; // TIM3 is set-up for the USB CDC interface void timer_tim3_init(void) { @@ -57,6 +58,7 @@ void timer_tim3_deinit(void) { */ // TIM5 is set-up for the servo controller +// This function inits but does not start the timer void timer_tim5_init(void) { // TIM5 clock enable __TIM5_CLK_ENABLE(); @@ -74,6 +76,31 @@ void timer_tim5_init(void) { HAL_TIM_PWM_Init(&TIM5_Handle); } +// Init TIM6 with a counter-overflow at the given frequency (given in Hz) +// TIM6 is used by the DAC and ADC for auto sampling at a given frequency +// This function inits but does not start the timer +void timer_tim6_init(uint freq) { + // TIM6 clock enable + __TIM6_CLK_ENABLE(); + + // Timer runs at SystemCoreClock / 2 + // Compute the prescaler value so TIM6 triggers at freq-Hz + uint32_t period = (SystemCoreClock / 2) / freq; + uint32_t prescaler = 1; + while (period > 0xffff) { + period >>= 1; + prescaler <<= 1; + } + + // Time base clock configuration + TIM6_Handle.Instance = TIM6; + TIM6_Handle.Init.Period = period - 1; + TIM6_Handle.Init.Prescaler = prescaler - 1; + TIM6_Handle.Init.ClockDivision = 0; // unused for TIM6 + TIM6_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM6 + HAL_TIM_Base_Init(&TIM6_Handle); +} + // Interrupt dispatch void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim == &TIM3_Handle) { |