2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 #include <ao_fast_timer.h>
20 static void (*ao_fast_timer_callback[AO_FAST_TIMER_MAX])(void);
21 static uint8_t ao_fast_timer_count;
22 static uint8_t ao_fast_timer_users;
25 ao_fast_timer_enable(void)
27 stm_tim6.cr1 = ((0 << STM_TIM67_CR1_ARPE) |
28 (0 << STM_TIM67_CR1_OPM) |
29 (1 << STM_TIM67_CR1_URS) |
30 (0 << STM_TIM67_CR1_UDIS) |
31 (1 << STM_TIM67_CR1_CEN));
35 ao_fast_timer_disable(void)
37 stm_tim6.cr1 = ((0 << STM_TIM67_CR1_ARPE) |
38 (0 << STM_TIM67_CR1_OPM) |
39 (1 << STM_TIM67_CR1_URS) |
40 (0 << STM_TIM67_CR1_UDIS) |
41 (0 << STM_TIM67_CR1_CEN));
45 ao_fast_timer_on(void (*callback)(void))
47 ao_fast_timer_callback[ao_fast_timer_count] = callback;
48 if (!ao_fast_timer_count++)
49 ao_fast_timer_enable();
53 ao_fast_timer_off(void (*callback)(void))
57 for (n = 0; n < ao_fast_timer_count; n++)
58 if (ao_fast_timer_callback[n] == callback) {
59 for (; n < ao_fast_timer_count-1; n++) {
60 ao_fast_timer_callback[n] = ao_fast_timer_callback[n+1];
62 if (!--ao_fast_timer_count)
63 ao_fast_timer_disable();
68 void stm_tim6_isr(void)
71 if (stm_tim6.sr & (1 << STM_TIM67_SR_UIF)) {
74 for (i = 0; i < ao_fast_timer_count; i++)
75 (*ao_fast_timer_callback[i])();
80 * According to the STM clock-configuration, timers run
81 * twice as fast as the APB1 clock *if* the APB1 prescaler
85 #if AO_APB1_PRESCALER > 1
86 #define TIMER_23467_SCALER 2
88 #define TIMER_23467_SCALER 1
91 #ifndef FAST_TIMER_FREQ
92 #define FAST_TIMER_FREQ 10000
95 #define TIMER_FAST ((AO_PCLK1 * TIMER_23467_SCALER) / FAST_TIMER_FREQ)
98 ao_fast_timer_init(void)
100 if (!ao_fast_timer_users) {
101 stm_nvic_set_enable(STM_ISR_TIM6_POS);
102 stm_nvic_set_priority(STM_ISR_TIM6_POS, AO_STM_NVIC_CLOCK_PRIORITY);
104 /* Turn on timer 6 */
105 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);
107 stm_tim6.psc = TIMER_FAST;
111 /* Enable update interrupt */
112 stm_tim6.dier = (1 << STM_TIM67_DIER_UIE);
114 /* Poke timer to reload values */
115 stm_tim6.egr |= (1 << STM_TIM67_EGR_UG);
117 stm_tim6.cr2 = (STM_TIM67_CR2_MMS_RESET << STM_TIM67_CR2_MMS);
118 ao_fast_timer_disable();
120 if (ao_fast_timer_users == AO_FAST_TIMER_MAX)
121 ao_panic(AO_PANIC_FAST_TIMER);
122 ao_fast_timer_users++;