altos/lpc: Add beep driver
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 void
21 ao_beep(uint8_t beep)
22 {
23         if (beep == 0) {
24                 lpc_ct32b1.tcr = ((0 << LPC_CT32B_TCR_CEN) |
25                                   (1 << LPC_CT32B_TCR_CRST));
26                 lpc_scb.sysahbclkctrl &= ~(1 << LPC_SCB_SYSAHBCLKCTRL_CT32B1);
27         } else {
28                 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_CT32B1);
29
30                 /* Set prescaler to match cc1111 clocks
31                  */
32                 lpc_ct32b1.pc = AO_LPC_CLKOUT / 750000 - 1;
33
34                 /* Write the desired data in the match registers */
35
36                 /* Reset after two time units */
37                 lpc_ct32b1.mr[0] = beep << 1;
38
39                 /* Flip output after one time unit */
40                 lpc_ct32b1.mr[1] = beep;
41
42                 /* Reset on match 0 */
43                 lpc_ct32b1.mcr = (1 << LPC_CT32B_MCR_MR0R);
44
45                 /* PWM on match 1 */
46                 lpc_ct32b1.pwmc = (1 << LPC_CT32B_PWMC_PWMEN1);
47                 
48                 /* timer mode */
49                 lpc_ct32b1.ctcr = 0;
50
51                 /* And turn the timer on */
52                 lpc_ct32b1.tcr = ((1 << LPC_CT32B_TCR_CEN) |
53                                   (1 << LPC_CT32B_TCR_CRST));
54         }
55 }
56
57 void
58 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant
59 {
60         ao_beep(beep);
61         ao_delay(ticks);
62         ao_beep(0);
63 }
64
65 void
66 ao_beep_init(void)
67 {
68         /* Our beeper is on c32b1_mat1
69          * which is on pin pio0_14
70          */
71
72         lpc_ioconf.pio0_14 = ((LPC_IOCONF_FUNC_PIO0_14_CT32B1_MAT1 << LPC_IOCONF_FUNC) |
73                               (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
74                               (0 << LPC_IOCONF_HYS) |
75                               (0 << LPC_IOCONF_INV) |
76                               (0 << LPC_IOCONF_OD));
77
78         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_CT32B1);
79
80         /* Disable the counter and reset the value */
81         lpc_ct32b1.tcr = ((0 << LPC_CT32B_TCR_CEN) |
82                           (1 << LPC_CT32B_TCR_CRST));
83 }