lpc: Attempt to make beeper generate correct tone
[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 #define AO_TIMER_PWM            cat(LPC_CT32B_PWMC_PWMEN, BEEPER_OUTPUT)
35
36 void
37 ao_beep(uint8_t beep)
38 {
39         if (beep == 0) {
40                 AO_TIMER.tcr = ((0 << LPC_CT32B_TCR_CEN) |
41                                   (1 << LPC_CT32B_TCR_CRST));
42                 lpc_scb.sysahbclkctrl &= ~(1UL << AO_TIMER_CLKCTRL);
43         } else {
44                 lpc_scb.sysahbclkctrl |= (1 << AO_TIMER_CLKCTRL);
45
46                 /* Set prescaler to match cc1111 clocks
47                  */
48                 AO_TIMER.pr = AO_LPC_SYSCLK / 750000 - 1;
49
50                 /* Write the desired data in the match registers */
51
52                 /* Reset after two time units */
53                 AO_TIMER.mr[0] = beep << 1;
54
55                 /* PWM width is half of that */
56                 AO_TIMER.mr[BEEPER_OUTPUT] = beep;
57
58                 /* Flip output on PWM match */
59                 AO_TIMER.emr = (LPC_CT32B_EMR_EMC_TOGGLE << AO_TIMER_EMC);
60
61                 /* Reset on match 0 */
62                 AO_TIMER.mcr = (1 << LPC_CT32B_MCR_MR0R);
63
64                 /* PWM on match */
65                 AO_TIMER.pwmc = (1 << AO_TIMER_PWM);
66
67                 /* timer mode */
68                 AO_TIMER.ctcr = 0;
69
70                 /* And turn the timer on */
71                 AO_TIMER.tcr = ((1 << LPC_CT32B_TCR_CEN) |
72                                   (0 << LPC_CT32B_TCR_CRST));
73         }
74 }
75
76 void
77 ao_beep_for(uint8_t beep, AO_TICK_TYPE ticks)
78 {
79         ao_beep(beep);
80         ao_delay(ticks);
81         ao_beep(0);
82 }
83
84 void
85 ao_beep_init(void)
86 {
87         lpc_ioconf.AO_TIMER_PIO = ((AO_TIMER_FUNC << LPC_IOCONF_FUNC) |
88                               (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
89                               (0 << LPC_IOCONF_HYS) |
90                               (0 << LPC_IOCONF_INV) |
91                               (1 << LPC_IOCONF_ADMODE) |
92                               (0 << LPC_IOCONF_OD));
93 }