2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 #define AO_SYSTICK (AO_LPC_SYSCLK / 2)
23 volatile AO_TICK_TYPE ao_tick_count;
34 AO_TICK_TYPE before, after;
38 before = ao_tick_count;
39 cvr = lpc_systick.cvr;
40 after = ao_tick_count;
41 } while (before != after);
43 return (uint64_t) after * (1000000000ULL / AO_HERTZ) +
44 (uint64_t) cvr * (1000000000ULL / AO_SYSTICK);
48 volatile uint8_t ao_data_interval = 1;
49 volatile uint8_t ao_data_count;
52 void lpc_systick_isr(void)
54 if (lpc_systick.csr & (1 << LPC_SYSTICK_CSR_COUNTFLAG)) {
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);
61 if (++ao_data_count == ao_data_interval) {
64 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
65 ao_wakeup((void *) &ao_data_count);
74 ao_timer_set_adc_interval(uint8_t interval)
77 ao_data_interval = interval;
83 #define SYSTICK_RELOAD ((AO_LPC_SYSCLK / 2) / 100 - 1)
85 /* Initialize our 100Hz clock */
89 lpc_systick.rvr = SYSTICK_RELOAD;
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));
96 #define AO_LPC_M ((AO_LPC_CLKOUT / AO_LPC_CLKIN) - 1)
98 #define AO_LPC_FCCO_MIN 156000000
104 for (i = 0; i < 200; i++)
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));
122 /* Enable the brown-out detection at the highest voltage to
123 * make sure the flash part remains happy
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));
131 /* Turn the IRC clock back on */
132 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_IRC_PD);
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)))
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)))
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)
155 ao_panic(AO_PANIC_CRASH);
157 /* Power down the PLL before touching the registers */
158 lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
161 /* Set PLL divider values */
162 lpc_scb.syspllctrl = ((AO_LPC_M << LPC_SCB_SYSPLLCTRL_MSEL) |
163 (p << LPC_SCB_SYSPLLCTRL_PSEL));
165 /* Turn off the external crystal clock */
166 lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
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 */
173 /* Turn on the external crystal clock */
174 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
177 /* Select crystal as PLL input */
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)))
186 /* Turn on the PLL */
187 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
189 /* Wait for it to lock */
191 for (i = 0; i < 20000; i++)
192 if (lpc_scb.syspllstat & (1 << LPC_SCB_SYSPLLSTAT_LOCK))
195 ao_panic(AO_PANIC_CRASH);
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)))
205 /* Set system clock divider */
206 lpc_scb.sysahbclkdiv = AO_LPC_CLKOUT / AO_LPC_SYSCLK;
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;
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)))
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) |