altos/stm32l0: Remove some unused code in ao_timer.c
[fw/altos] / src / stm32l0 / ao_timer.c
1 /*
2  * Copyright © 2020 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_task.h>
21 #if HAS_FAKE_FLIGHT
22 #include <ao_fake_flight.h>
23 #endif
24
25 #ifndef HAS_TICK
26 #define HAS_TICK 1
27 #endif
28
29 #if HAS_TICK
30 volatile AO_TICK_TYPE ao_tick_count;
31
32 AO_TICK_TYPE
33 ao_time(void)
34 {
35         return ao_tick_count;
36 }
37
38 #if 0
39 uint64_t
40 ao_time_ns(void)
41 {
42         AO_TICK_TYPE    before, after;
43         uint32_t        cvr;
44
45         do {
46                 before = ao_tick_count;
47                 cvr = stm_systick.cvr;
48                 after = ao_tick_count;
49         } while (before != after);
50
51         return (uint64_t) after * (1000000000ULL / AO_HERTZ) +
52                 (uint64_t) cvr * (1000000000ULL / AO_SYSTICK);
53 }
54 #endif
55
56 #if AO_DATA_ALL
57 volatile uint8_t        ao_data_interval = 1;
58 volatile uint8_t        ao_data_count;
59 #endif
60
61 void stm_systick_isr(void)
62 {
63         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
64                 ++ao_tick_count;
65 #if HAS_TASK_QUEUE
66                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
67                         ao_task_check_alarm((uint16_t) ao_tick_count);
68 #endif
69 #if AO_DATA_ALL
70                 if (++ao_data_count == ao_data_interval) {
71                         ao_data_count = 0;
72 #if HAS_ADC
73 #if HAS_FAKE_FLIGHT
74                         if (ao_fake_flight_active)
75                                 ao_fake_flight_poll();
76                         else
77 #endif
78                                 ao_adc_poll();
79 #endif
80 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
81                         ao_wakeup((void *) &ao_data_count);
82 #endif
83                 }
84 #endif
85 #ifdef AO_TIMER_HOOK
86                 AO_TIMER_HOOK;
87 #endif
88         }
89 }
90
91 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
92
93 void
94 ao_timer_init(void)
95 {
96         stm_systick.csr = 0;
97         stm_systick.rvr = SYSTICK_RELOAD;
98         stm_systick.cvr = 0;
99         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
100                            (1 << STM_SYSTICK_CSR_TICKINT) |
101                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
102 }
103
104 #endif