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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 volatile __data AO_TICK_TYPE ao_tick_count;
29 volatile __data uint8_t ao_data_interval = 1;
30 volatile __data uint8_t ao_data_count;
33 void lpc_systick_isr(void)
35 if (lpc_systick.csr & (1 << LPC_SYSTICK_CSR_COUNTFLAG)) {
38 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
39 ao_task_check_alarm((uint16_t) ao_tick_count);
42 if (++ao_data_count == ao_data_interval) {
45 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
46 ao_wakeup((void *) &ao_data_count);
55 ao_timer_set_adc_interval(uint8_t interval)
58 ao_data_interval = interval;
64 #define SYSTICK_RELOAD ((AO_LPC_CLKOUT / 2) / 100 - 1)
66 /* Initialize our 100Hz clock */
70 lpc_systick.rvr = SYSTICK_RELOAD;
72 lpc_systick.csr = ((1 << LPC_SYSTICK_CSR_ENABLE) |
73 (1 << LPC_SYSTICK_CSR_TICKINT) |
74 (LPC_SYSTICK_CSR_CLKSOURCE_CPU_OVER_2 << LPC_SYSTICK_CSR_CLKSOURCE));
77 #define AO_LPC_M ((AO_LPC_CLKOUT / AO_LPC_CLKIN) - 1)
79 #define AO_LPC_FCCO_MIN 156000000
87 /* Turn off all perhipherals except for GPIO configuration */
88 lpc_scb.sysahbclkctrl = ((1 << LPC_SCB_SYSAHBCLKCTRL_SYS) |
89 (1 << LPC_SCB_SYSAHBCLKCTRL_ROM) |
90 (1 << LPC_SCB_SYSAHBCLKCTRL_RAM0) |
91 (1 << LPC_SCB_SYSAHBCLKCTRL_FLASHARRAY) |
92 (1 << LPC_SCB_SYSAHBCLKCTRL_GPIO) |
93 (1 << LPC_SCB_SYSAHBCLKCTRL_IOCON));
95 /* Turn the IRC clock back on */
96 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_IRC_PD);
98 /* Switch to the IRC clock */
99 lpc_scb.mainclksel = LPC_SCB_MAINCLKSEL_SEL_IRC << LPC_SCB_MAINCLKSEL_SEL;
100 lpc_scb.mainclkuen = (0 << LPC_SCB_MAINCLKUEN_ENA);
101 lpc_scb.mainclkuen = (1 << LPC_SCB_MAINCLKUEN_ENA);
103 /* Find a PLL post divider ratio that gets the FCCO in range */
104 for (p = 0; p < 4; p++)
105 if (AO_LPC_CLKOUT << (1 + p) >= AO_LPC_FCCO_MIN)
109 ao_panic(AO_PANIC_CRASH);
111 /* Power down the PLL before touching the registers */
112 lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
114 /* Set PLL divider values */
115 lpc_scb.syspllctrl = ((AO_LPC_M << LPC_SCB_SYSPLLCTRL_MSEL) |
116 (p << LPC_SCB_SYSPLLCTRL_PSEL));
119 /* Turn off the external crystal clock */
120 lpc_scb.pdruncfg |= (1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
122 /* Configure the crystal clock */
123 lpc_scb.sysoscctrl = ((0 << LPC_SCB_SYSOSCCTRL_BYPASS) | /* using a crystal */
124 ((AO_LPC_CLKIN > 15000000) << LPC_SCB_SYSOSCCTRL_FREQRANGE));/* set range */
126 /* Turn on the external crystal clock */
127 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSOSC_PD);
129 /* Select crystal as PLL input */
131 lpc_scb.syspllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
132 lpc_scb.syspllclkuen = 0;
133 lpc_scb.syspllclkuen = (1 << LPC_SCB_SYSPLLCLKUEN_ENA);
135 /* Turn on the PLL */
136 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_SYSPLL_PD);
138 /* Wait for it to lock */
140 for (i = 0; i < 20000; i++)
141 if (lpc_scb.syspllstat & (1 << LPC_SCB_SYSPLLSTAT_LOCK))
144 ao_panic(AO_PANIC_CRASH);
146 /* Switch to the PLL */
147 lpc_scb.mainclksel = LPC_SCB_MAINCLKSEL_SEL_PLL_OUTPUT << LPC_SCB_MAINCLKSEL_SEL;
148 lpc_scb.mainclkuen = 0 << LPC_SCB_MAINCLKUEN_ENA;
149 lpc_scb.mainclkuen = 1 << LPC_SCB_MAINCLKUEN_ENA;
151 /* Set system clock divider */
152 lpc_scb.sysahbclkdiv = AO_LPC_CLKOUT / AO_LPC_SYSCLK;
154 /* Set USB clock source */
155 lpc_scb.usbclksel = (LPC_SCB_USBCLKSEL_SEL_MAIN_CLOCK << LPC_SCB_USBCLKSEL_SEL);
156 lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
157 lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
159 /* Shut down perhipheral clocks (enabled as needed) */
160 lpc_scb.ssp0clkdiv = 0;
161 lpc_scb.uartclkdiv = 0;
162 lpc_scb.ssp1clkdiv = 0;
163 lpc_scb.usbclkdiv = 0;
164 lpc_scb.clkoutdiv = 0;