altos: Use explicit boot loader signal in ao_boot_reboot
[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.pr = AO_LPC_SYSCLK / 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                 /* PWM width is half of that */
40                 lpc_ct32b1.mr[1] = beep;
41
42                 /* Flip output 1 on PWM match */
43                 lpc_ct32b1.emr = (LPC_CT32B_EMR_EMC_TOGGLE << LPC_CT32B_EMR_EMC1);
44
45                 /* Reset on match 0 */
46                 lpc_ct32b1.mcr = (1 << LPC_CT32B_MCR_MR0R);
47
48                 /* PWM on match 1 */
49                 lpc_ct32b1.pwmc = (1 << LPC_CT32B_PWMC_PWMEN1);
50                 
51                 /* timer mode */
52                 lpc_ct32b1.ctcr = 0;
53
54                 /* And turn the timer on */
55                 lpc_ct32b1.tcr = ((1 << LPC_CT32B_TCR_CEN) |
56                                   (0 << LPC_CT32B_TCR_CRST));
57         }
58 }
59
60 void
61 ao_beep_for(uint8_t beep, uint16_t ticks) __reentrant
62 {
63         ao_beep(beep);
64         ao_delay(ticks);
65         ao_beep(0);
66 }
67
68 void
69 ao_beep_init(void)
70 {
71         /* Our beeper is on c32b1_mat1
72          * which is on pin pio0_14
73          */
74
75         lpc_ioconf.pio0_14 = ((LPC_IOCONF_FUNC_PIO0_14_CT32B1_MAT1 << LPC_IOCONF_FUNC) |
76                               (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
77                               (0 << LPC_IOCONF_HYS) |
78                               (0 << LPC_IOCONF_INV) |
79                               (1 << LPC_IOCONF_ADMODE) |
80                               (0 << LPC_IOCONF_OD));
81
82         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_CT32B1);
83
84         /* Disable the counter and reset the value */
85         lpc_ct32b1.tcr = ((0 << LPC_CT32B_TCR_CEN) |
86                           (1 << LPC_CT32B_TCR_CRST));
87 }