altos: ADS124S0X driver compiles now
[fw/altos] / src / stm / ao_sample_profile_timer.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_sample_profile.h>
21
22 struct stm_exception {
23         uint32_t        r0;
24         uint32_t        r1;
25         uint32_t        r2;
26         uint32_t        r3;
27         uint32_t        r12;
28         uint32_t        lr;
29         uint32_t        pc;
30         uint32_t        psr;
31 };
32
33 void
34 stm_tim10_isr(void)
35 {
36         struct stm_exception    *ex;
37
38         asm("mov %0,sp" : "=&r" (ex));
39
40         stm_tim10.sr = 0;
41         ao_sample_profile_point(ex->pc, stm_tim11.cnt, (ex->psr & 0xff) != 0);
42 }
43
44 uint16_t
45 ao_sample_profile_timer_start(void)
46 {
47         /* Reset counts */
48         stm_tim11.cnt = 0;
49         stm_tim10.cnt = 0;
50
51         /* Turn on timer 11 */
52         stm_tim11.cr1 = ((0 << STM_TIM1011_CR1_CKD) |
53                          (0 << STM_TIM1011_CR1_ARPE) |
54                          (1 << STM_TIM1011_CR1_URS) |
55                          (0 << STM_TIM1011_CR1_UDIS) |
56                          (1 << STM_TIM1011_CR1_CEN));
57
58         /* Turn on timer 10 */
59         stm_tim10.cr1 = ((0 << STM_TIM1011_CR1_CKD) |
60                          (0 << STM_TIM1011_CR1_ARPE) |
61                          (1 << STM_TIM1011_CR1_URS) |
62                          (0 << STM_TIM1011_CR1_UDIS) |
63                          (1 << STM_TIM1011_CR1_CEN));
64         return stm_tim11.cnt;
65 }
66
67 void
68 ao_sample_profile_timer_stop(void)
69 {
70         stm_tim10.cr1 = 0;
71         stm_tim11.cr1 = 0;
72 }
73
74 #if AO_APB2_PRESCALER > 1
75 #define TIMER_91011_SCALER 2
76 #else
77 #define TIMER_91011_SCALER 1
78 #endif
79
80 #define TIMER_10kHz     ((AO_PCLK2 * TIMER_91011_SCALER) / 10000)
81 #define TIMER_1kHz      ((AO_PCLK2 * TIMER_91011_SCALER) / 1000)
82
83 void
84 ao_sample_profile_timer_init(void)
85 {
86         /* Turn on power for timer 10 and 11 */
87         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_TIM10EN) | (1 << STM_RCC_APB2ENR_TIM11EN);
88
89         /* Timer 10 is the 1kHz interrupt */
90         stm_tim10.cr1 = 0;
91         stm_tim10.psc = TIMER_10kHz;
92         stm_tim10.arr = 9;
93         stm_tim10.cnt = 0;
94
95         /* Enable timer 10 update interrupt */
96         stm_tim10.dier = (1 << STM_TIM1011_DIER_UIE);
97
98         /* Poke timer to reload values */
99         stm_tim10.egr |= (1 << STM_TIM1011_EGR_UG);
100
101         /* Timer 11 is the 1kHz counter */
102         stm_tim11.cr1 = 0;
103         stm_tim11.psc = TIMER_1kHz;
104         stm_tim11.arr = 0xffff;
105         stm_tim11.cnt = 0;
106
107         /* Disable interrupts for timer 11 */
108         stm_tim11.dier = 0;
109
110         /* Poke timer to reload values */
111         stm_tim11.egr |= (1 << STM_TIM1011_EGR_UG);
112
113         stm_tim10.sr = 0;
114         stm_nvic_set_enable(STM_ISR_TIM10_POS);
115         stm_nvic_set_priority(STM_ISR_TIM10_POS, AO_STM_NVIC_HIGH_PRIORITY);
116 }