3b77afc0d8b397d3b71e873c539c93df0fcf657f
[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 uint64_t
39 ao_time_ns(void)
40 {
41         AO_TICK_TYPE    before, after;
42         uint32_t        cvr;
43
44         do {
45                 before = ao_tick_count;
46                 cvr = stm_systick.cvr;
47                 after = ao_tick_count;
48         } while (before != after);
49
50         return (uint64_t) after * (1000000000ULL / AO_HERTZ) +
51                 (uint64_t) cvr * (1000000000ULL / AO_SYSTICK);
52 }
53
54 #if AO_DATA_ALL
55 volatile uint8_t        ao_data_interval = 1;
56 volatile uint8_t        ao_data_count;
57 #endif
58
59 void stm_systick_isr(void)
60 {
61         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
62                 ++ao_tick_count;
63 #if HAS_TASK_QUEUE
64                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
65                         ao_task_check_alarm((uint16_t) ao_tick_count);
66 #endif
67 #if AO_DATA_ALL
68                 if (++ao_data_count == ao_data_interval) {
69                         ao_data_count = 0;
70 #if HAS_ADC
71 #if HAS_FAKE_FLIGHT
72                         if (ao_fake_flight_active)
73                                 ao_fake_flight_poll();
74                         else
75 #endif
76                                 ao_adc_poll();
77 #endif
78 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
79                         ao_wakeup((void *) &ao_data_count);
80 #endif
81                 }
82 #endif
83 #ifdef AO_TIMER_HOOK
84                 AO_TIMER_HOOK;
85 #endif
86         }
87 }
88
89 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
90
91 void
92 ao_timer_init(void)
93 {
94         stm_systick.csr = 0;
95         stm_systick.rvr = SYSTICK_RELOAD;
96         stm_systick.cvr = 0;
97         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
98                            (1 << STM_SYSTICK_CSR_TICKINT) |
99                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
100 }
101
102 #endif