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 _cat(a,b) a##b
22 #define cat(a,b) _cat(a,b)
23 #define _cat4(a,b,c,d) a##b##c##d
24 #define cat4(a,b,c,d) _cat4(a,b,c,d)
25 #define _cat8(a,b,c,d,e,f,g,h) a##b##c##d##e##f##g##h
26 #define cat8(a,b,c,d,e,f,g,h) _cat8(a,b,c,d,e,f,g,h)
28 #define AO_TIMER_CLKCTRL cat(LPC_SCB_SYSAHBCLKCTRL_CT32B, BEEPER_TIMER)
29 #define AO_TIMER cat(lpc_ct32b, BEEPER_TIMER)
30 #define AO_TIMER_EMC cat(LPC_CT32B_EMR_EMC, BEEPER_OUTPUT)
31 #define AO_TIMER_PIO cat4(pio, BEEPER_PORT, _, BEEPER_PIN)
32 /* LPC_IOCONF_FUNC_PIO0_14_CT32B1_MAT1 */
33 #define AO_TIMER_FUNC cat8(LPC_IOCONF_FUNC_PIO, BEEPER_PORT, _, BEEPER_PIN, _CT32B, BEEPER_TIMER, _MAT, BEEPER_OUTPUT)
39 AO_TIMER.tcr = ((0 << LPC_CT32B_TCR_CEN) |
40 (1 << LPC_CT32B_TCR_CRST));
41 lpc_scb.sysahbclkctrl &= ~(1UL << AO_TIMER_CLKCTRL);
43 lpc_scb.sysahbclkctrl |= (1 << AO_TIMER_CLKCTRL);
45 /* Set prescaler to match cc1111 clocks
47 AO_TIMER.pr = AO_LPC_SYSCLK / 750000 - 1;
49 /* Write the desired data in the match registers */
51 /* Reset after two time units */
52 AO_TIMER.mr[0] = beep << 1;
54 /* PWM width is half of that */
55 AO_TIMER.mr[1] = beep;
57 /* Flip output 1 on PWM match */
58 AO_TIMER.emr = (LPC_CT32B_EMR_EMC_TOGGLE << LPC_CT32B_EMR_EMC1);
60 /* Reset on match 0 */
61 AO_TIMER.mcr = (1 << LPC_CT32B_MCR_MR0R);
64 AO_TIMER.pwmc = (1 << LPC_CT32B_PWMC_PWMEN1);
69 /* And turn the timer on */
70 AO_TIMER.tcr = ((1 << LPC_CT32B_TCR_CEN) |
71 (0 << LPC_CT32B_TCR_CRST));
76 ao_beep_for(uint8_t beep, AO_TICK_TYPE ticks)
86 lpc_ioconf.AO_TIMER_PIO = ((AO_TIMER_FUNC << LPC_IOCONF_FUNC) |
87 (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
88 (0 << LPC_IOCONF_HYS) |
89 (0 << LPC_IOCONF_INV) |
90 (1 << LPC_IOCONF_ADMODE) |
91 (0 << LPC_IOCONF_OD));