lpc: Make beeper timer configurable
[fw/altos] / src / lpc / ao_beep_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 _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)
27
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)
34
35 void
36 ao_beep(uint8_t beep)
37 {
38         if (beep == 0) {
39                 AO_TIMER.tcr = ((0 << LPC_CT32B_TCR_CEN) |
40                                   (1 << LPC_CT32B_TCR_CRST));
41                 lpc_scb.sysahbclkctrl &= ~(1UL << AO_TIMER_CLKCTRL);
42         } else {
43                 lpc_scb.sysahbclkctrl |= (1 << AO_TIMER_CLKCTRL);
44
45                 /* Set prescaler to match cc1111 clocks
46                  */
47                 AO_TIMER.pr = AO_LPC_SYSCLK / 750000 - 1;
48
49                 /* Write the desired data in the match registers */
50
51                 /* Reset after two time units */
52                 AO_TIMER.mr[0] = beep << 1;
53
54                 /* PWM width is half of that */
55                 AO_TIMER.mr[1] = beep;
56
57                 /* Flip output 1 on PWM match */
58                 AO_TIMER.emr = (LPC_CT32B_EMR_EMC_TOGGLE << LPC_CT32B_EMR_EMC1);
59
60                 /* Reset on match 0 */
61                 AO_TIMER.mcr = (1 << LPC_CT32B_MCR_MR0R);
62
63                 /* PWM on match 1 */
64                 AO_TIMER.pwmc = (1 << LPC_CT32B_PWMC_PWMEN1);
65
66                 /* timer mode */
67                 AO_TIMER.ctcr = 0;
68
69                 /* And turn the timer on */
70                 AO_TIMER.tcr = ((1 << LPC_CT32B_TCR_CEN) |
71                                   (0 << LPC_CT32B_TCR_CRST));
72         }
73 }
74
75 void
76 ao_beep_for(uint8_t beep, AO_TICK_TYPE ticks)
77 {
78         ao_beep(beep);
79         ao_delay(ticks);
80         ao_beep(0);
81 }
82
83 void
84 ao_beep_init(void)
85 {
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));
92 }