altos: Add program flash function
[fw/altos] / src / stm / ao_profile.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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_profile.h>
20
21 static void ao_profile_test(void)
22 {
23         uint8_t i;
24         uint32_t        ticks[20];
25
26         for (i = 0; i < 20; i++) {
27                 ticks[i] = ao_profile_tick();
28                 ao_delay(0);
29         }
30         for (i = 0; i < 19; i++)
31                 printf ("%d\n", ticks[i+1] - ticks[i]);
32 }
33
34 static const struct ao_cmds ao_profile_cmds[] = {
35         { ao_profile_test,      "P\0Test profile counter" },
36         { 0, NULL }
37 };
38
39 void ao_profile_init(void)
40 {
41         /* Turn on timer 2 and 4 */
42         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM2EN) |
43                 (1 << STM_RCC_APB1ENR_TIM4EN);
44
45         /* disable timers */
46         stm_tim4.cr1 = 0;
47         stm_tim2.cr1 = 0;
48
49         /* tim4 is master */
50
51
52         stm_tim4.cr2 = ((0 << STM_TIM234_CR2_TI1S) |
53                         (STM_TIM234_CR2_MMS_UPDATE << STM_TIM234_CR2_MMS) |
54                         (0 << STM_TIM234_CR2_CCDS));
55
56         stm_tim4.smcr = ((0 << STM_TIM234_SMCR_ETP) |
57                          (0 << STM_TIM234_SMCR_ECE) |
58                          (STM_TIM234_SMCR_ETPS_OFF << STM_TIM234_SMCR_ETPS) |
59                          (STM_TIM234_SMCR_ETF_NONE << STM_TIM234_SMCR_ETF) |
60                          (0 << STM_TIM234_SMCR_MSM) |
61                          (STM_TIM234_SMCR_TS_ITR3 << STM_TIM234_SMCR_TS) |
62                          (0 << STM_TIM234_SMCR_OCCS) |
63                          (STM_TIM234_SMCR_SMS_DISABLE << STM_TIM234_SMCR_SMS));
64
65         stm_tim4.dier = 0;
66         stm_tim4.sr = 0;
67
68         stm_tim4.psc = 31;
69         stm_tim4.cnt = 0;
70         stm_tim4.arr = 0xffff;
71
72         /* tim2 is slaved to tim4 */
73
74         stm_tim2.cr2 = ((0 << STM_TIM234_CR2_TI1S) |
75                         (STM_TIM234_CR2_MMS_ENABLE << STM_TIM234_CR2_MMS) |
76                         (0 << STM_TIM234_CR2_CCDS));
77         stm_tim2.smcr = ((0 << STM_TIM234_SMCR_ETP) |
78                          (0 << STM_TIM234_SMCR_ECE) |
79                          (STM_TIM234_SMCR_ETPS_OFF << STM_TIM234_SMCR_ETPS) |
80                          (STM_TIM234_SMCR_ETF_NONE << STM_TIM234_SMCR_ETF) |
81                          (0 << STM_TIM234_SMCR_MSM) |
82                          (STM_TIM234_SMCR_TS_ITR3 << STM_TIM234_SMCR_TS) |
83                          (0 << STM_TIM234_SMCR_OCCS) |
84                          (STM_TIM234_SMCR_SMS_EXTERNAL_CLOCK << STM_TIM234_SMCR_SMS));
85         stm_tim2.dier = 0;
86         stm_tim2.sr = 0;
87         stm_tim2.psc = 0;
88         stm_tim2.cnt = 0;
89         stm_tim2.arr = 0xffff;
90
91         /* Start your timers */
92
93         stm_tim2.cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
94                         (0 << STM_TIM234_CR1_ARPE) |
95                         (STM_TIM234_CR1_CMS_EDGE | STM_TIM234_CR1_CMS) |
96                         (STM_TIM234_CR1_DIR_UP << STM_TIM234_CR1_DIR) |
97                         (0 << STM_TIM234_CR1_OPM) |
98                         (0 << STM_TIM234_CR1_URS) |
99                         (0 << STM_TIM234_CR1_UDIS) |
100                         (1 << STM_TIM234_CR1_CEN));
101
102         stm_tim4.cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
103                         (0 << STM_TIM234_CR1_ARPE) |
104                         (STM_TIM234_CR1_CMS_EDGE | STM_TIM234_CR1_CMS) |
105                         (STM_TIM234_CR1_DIR_UP << STM_TIM234_CR1_DIR) |
106                         (0 << STM_TIM234_CR1_OPM) |
107                         (1 << STM_TIM234_CR1_URS) |
108                         (0 << STM_TIM234_CR1_UDIS) |
109                         (1 << STM_TIM234_CR1_CEN));
110
111         ao_cmd_register(&ao_profile_cmds[0]);
112 }