89193333f5c32a138d2bd593ced54c2c04a1db1f
[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                 ao_task_check_alarm();
66 #if AO_DATA_ALL
67                 if (++ao_data_count == ao_data_interval) {
68                         ao_data_count = 0;
69 #if HAS_ADC
70 #if HAS_FAKE_FLIGHT
71                         if (ao_fake_flight_active)
72                                 ao_fake_flight_poll();
73                         else
74 #endif
75                                 ao_adc_poll();
76 #endif
77 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
78                         ao_wakeup((void *) &ao_data_count);
79 #endif
80                 }
81 #endif
82 #ifdef AO_TIMER_HOOK
83                 AO_TIMER_HOOK;
84 #endif
85         }
86 }
87
88 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
89
90 void
91 ao_timer_init(void)
92 {
93         stm_systick.csr = 0;
94         stm_systick.rvr = SYSTICK_RELOAD;
95         stm_systick.cvr = 0;
96         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
97                            (1 << STM_SYSTICK_CSR_TICKINT) |
98                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
99 }
100
101 void
102 ao_timer_stop(void)
103 {
104         stm_systick.csr = 0;
105 }
106
107 #endif