altos/stm32f4: Add -mfloat-abi=hard compile option
[fw/altos] / src / lpc / ao_timer_lpc.c
1 /*
2  * Copyright © 2013 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
21 #define AO_SYSTICK      (AO_LPC_SYSCLK / 2)
22
23 volatile AO_TICK_TYPE ao_tick_count;
24
25 AO_TICK_TYPE
26 ao_time(void)
27 {
28         return ao_tick_count;
29 }
30
31 uint64_t
32 ao_time_ns(void)
33 {
34         AO_TICK_TYPE    before, after;
35         uint32_t        cvr;
36
37         do {
38                 before = ao_tick_count;
39                 cvr = lpc_systick.cvr;
40                 after = ao_tick_count;
41         } while (before != after);
42
43         return (uint64_t) after * (1000000000ULL / AO_HERTZ) +
44                 (uint64_t) cvr * (1000000000ULL / AO_SYSTICK);
45 }
46
47 #if AO_DATA_ALL
48 volatile uint8_t        ao_data_interval = 1;
49 volatile uint8_t        ao_data_count;
50 #endif
51
52 void lpc_systick_isr(void)
53 {
54         if (lpc_systick.csr & (1 << LPC_SYSTICK_CSR_COUNTFLAG)) {
55                 ++ao_tick_count;
56 #if HAS_TASK_QUEUE
57                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
58                         ao_task_check_alarm((uint16_t) ao_tick_count);
59 #endif
60 #if AO_DATA_ALL
61                 if (++ao_data_count == ao_data_interval) {
62                         ao_data_count = 0;
63                         ao_adc_poll();
64 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
65                         ao_wakeup((void *) &ao_data_count);
66 #endif
67                 }
68 #endif
69         }
70 }
71
72 #if HAS_ADC
73 void
74 ao_timer_set_adc_interval(uint8_t interval)
75 {
76         ao_arch_critical(
77                 ao_data_interval = interval;
78                 ao_data_count = 0;
79                 );
80 }
81 #endif
82
83 #define SYSTICK_RELOAD ((AO_LPC_SYSCLK / 2) / 100 - 1)
84
85 /* Initialize our 100Hz clock */
86 void
87 ao_timer_init(void)
88 {
89         lpc_systick.rvr = SYSTICK_RELOAD;
90         lpc_systick.cvr = 0;
91         lpc_systick.csr = ((1 << LPC_SYSTICK_CSR_ENABLE) |
92                            (1 << LPC_SYSTICK_CSR_TICKINT) |
93                            (LPC_SYSTICK_CSR_CLKSOURCE_CPU_OVER_2 << LPC_SYSTICK_CSR_CLKSOURCE));
94 }
95
96 #define AO_LPC_M        ((AO_LPC_CLKOUT / AO_LPC_CLKIN) - 1)
97
98 #define AO_LPC_FCCO_MIN 156000000
99
100 static void
101 ao_clock_delay(void)
102 {
103         uint32_t        i;
104         for (i = 0; i < 200; i++)
105                 ao_arch_nop();
106 }
107
108 void
109 ao_clock_init(void)
110 {
111         uint8_t         p;
112         uint32_t        i;
113
114         /* Turn off all perhipherals except for GPIO configuration */
115         lpc_scb.sysahbclkctrl = ((1 << LPC_SCB_SYSAHBCLKCTRL_SYS) |
116                                  (1 << LPC_SCB_SYSAHBCLKCTRL_ROM) |
117                                  (1 << LPC_SCB_SYSAHBCLKCTRL_RAM0) |
118                                  (1 << LPC_SCB_SYSAHBCLKCTRL_FLASHARRAY) |
119                                  (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO) |
120                                  (1 << LPC_SCB_SYSAHBCLKCTRL_IOCON));
121
122         /* Enable the brown-out detection at the highest voltage to
123          * make sure the flash part remains happy
124          */
125
126         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_BOD_PD);
127         lpc_scb.bodctrl = ((LPC_SCB_BOD_BODRSTLEV_2_63 << LPC_SCB_BOD_BODRSTLEV) |
128                            (LPC_SCB_BOD_BODINTVAL_RESERVED << LPC_SCB_BOD_BODINTVAL) |
129                            (1 << LPC_SCB_BOD_BODRSTENA));
130
131         /* Turn the IRC clock back on */
132         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_IRC_PD);
133         ao_clock_delay();
134
135         /* Switch to the IRC clock */
136         lpc_scb.mainclksel = LPC_SCB_MAINCLKSEL_SEL_IRC << LPC_SCB_MAINCLKSEL_SEL;
137         lpc_scb.mainclkuen = (0 << LPC_SCB_MAINCLKUEN_ENA);
138         lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
139         while (!(lpc_scb.mainclkuen & (1 << LPC_SCB_MAINCLKUEN_ENA)))
140                 ;
141
142         /* Switch USB to the main clock */
143         lpc_scb.usbclksel = (LPC_SCB_USBCLKSEL_SEL_MAIN_CLOCK << LPC_SCB_USBCLKSEL_SEL);
144         lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
145         lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
146         while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
147                 ;
148
149         /* Find a PLL post divider ratio that gets the FCCO in range */
150         for (p = 0; p < 4; p++)
151                 if (AO_LPC_CLKOUT << (1 + p) >= AO_LPC_FCCO_MIN)
152                         break;
153
154         if (p == 4)
155                 ao_panic(AO_PANIC_CRASH);
156
157         /* Power down the PLL before touching the registers */
158         lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
159         ao_clock_delay();
160
161         /* Set PLL divider values */
162         lpc_scb.syspllctrl = ((AO_LPC_M << LPC_SCB_SYSPLLCTRL_MSEL) |
163                               (p << LPC_SCB_SYSPLLCTRL_PSEL));
164
165         /* Turn off the external crystal clock */
166         lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
167         ao_clock_delay();
168
169         /* Configure the crystal clock */
170         lpc_scb.sysoscctrl = ((0 << LPC_SCB_SYSOSCCTRL_BYPASS) |                           /* using a crystal */
171                               ((AO_LPC_CLKIN > 15000000) << LPC_SCB_SYSOSCCTRL_FREQRANGE));/* set range */
172
173         /* Turn on the external crystal clock */
174         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
175         ao_clock_delay();
176
177         /* Select crystal as PLL input */
178
179         lpc_scb.syspllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
180         lpc_scb.syspllclkuen = (1 << LPC_SCB_SYSPLLCLKUEN_ENA);
181         lpc_scb.syspllclkuen = (0 << LPC_SCB_SYSPLLCLKUEN_ENA);
182         lpc_scb.syspllclkuen = (1 << LPC_SCB_SYSPLLCLKUEN_ENA);
183         while (!(lpc_scb.syspllclkuen & (1 << LPC_SCB_SYSPLLCLKUEN_ENA)))
184                 ;
185
186         /* Turn on the PLL */
187         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
188
189         /* Wait for it to lock */
190
191         for (i = 0; i < 20000; i++)
192                 if (lpc_scb.syspllstat & (1 << LPC_SCB_SYSPLLSTAT_LOCK))
193                         break;
194         if (i == 20000)
195                 ao_panic(AO_PANIC_CRASH);
196
197         /* Switch to the PLL */
198         lpc_scb.mainclksel = LPC_SCB_MAINCLKSEL_SEL_PLL_OUTPUT << LPC_SCB_MAINCLKSEL_SEL;
199         lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
200         lpc_scb.mainclkuen = (0 << LPC_SCB_MAINCLKUEN_ENA);
201         lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
202         while (!(lpc_scb.mainclkuen & (1 << LPC_SCB_MAINCLKUEN_ENA)))
203                 ;
204
205         /* Set system clock divider */
206         lpc_scb.sysahbclkdiv = AO_LPC_CLKOUT / AO_LPC_SYSCLK;
207
208         /* Shut down perhipheral clocks (enabled as needed) */
209         lpc_scb.ssp0clkdiv = 0;
210         lpc_scb.uartclkdiv = 0;
211         lpc_scb.ssp1clkdiv = 0;
212         lpc_scb.usbclkdiv = 0;
213         lpc_scb.clkoutdiv = 0;
214
215         /* Switch USB PLL source to system osc so we can power down the IRC */
216         lpc_scb.usbpllclksel = (LPC_SCB_USBPLLCLKSEL_SEL_SYSOSC << LPC_SCB_USBPLLCLKSEL_SEL);
217         lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
218         lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
219         while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
220                 ;
221
222         /* Power down everything we don't need */
223         lpc_scb.pdruncfg = ((1 << LPC_SCB_PDRUNCFG_IRCOUT_PD) |
224                             (1 << LPC_SCB_PDRUNCFG_IRC_PD) |
225                             (0 << LPC_SCB_PDRUNCFG_BOD_PD) |
226                             (1 << LPC_SCB_PDRUNCFG_ADC_PD) |
227                             (1 << LPC_SCB_PDRUNCFG_WDTOSC_PD) |
228                             (1 << LPC_SCB_PDRUNCFG_USBPLL_PD) |
229                             (1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
230                             (1 << 11) |
231                             (7 << 13));
232 }