altos/test: Adjust CRC error rate after FEC fix
[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 _cat2(a,b) a##b
22 #define cat2(a,b) _cat2(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 #ifndef AO_LPC_BEEP_TIMER
29 #define AO_LPC_BEEP_TIMER 1
30 #define AO_LPC_BEEP_CHANNEL 1
31 #define AO_LPC_BEEP_PORT        0
32 #define AO_LPC_BEEP_PIN         14
33 #endif
34
35 #define AO_LPC_CT_BEEP          cat2(lpc_ct32b, AO_LPC_BEEP_TIMER)
36 #define AO_LPC_CT_BEEP_CLKCTRL  cat2(LPC_SCB_SYSAHBCLKCTRL_CT32B, AO_LPC_BEEP_TIMER)
37 #define AO_LPC_CT_BEEP_EMR      cat2(LPC_CT32B_EMR_EMC, AO_LPC_BEEP_CHANNEL)
38 #define AO_LPC_CT_BEEP_MR       AO_LPC_BEEP_CHANNEL
39 #define AO_LPC_CT_BEEP_PWMC     cat2(LPC_CT32B_PWMC_PWMEN, AO_LPC_BEEP_CHANNEL)
40 #define AO_LPC_CT_BEEP_IOCONF   cat4(pio,AO_LPC_BEEP_PORT,_,AO_LPC_BEEP_PIN)
41 #define AO_LPC_CT_BEEP_FUNC     cat8(LPC_IOCONF_FUNC_PIO,AO_LPC_BEEP_PORT,_,AO_LPC_BEEP_PIN,_CT32B,AO_LPC_BEEP_TIMER,_MAT,AO_LPC_BEEP_CHANNEL)
42
43 void
44 ao_beep(uint8_t beep)
45 {
46         if (beep == 0) {
47                 AO_LPC_CT_BEEP.tcr = ((0 << LPC_CT32B_TCR_CEN) |
48                                   (1 << LPC_CT32B_TCR_CRST));
49                 lpc_scb.sysahbclkctrl &= ~(1UL << AO_LPC_CT_BEEP_CLKCTRL);
50         } else {
51                 lpc_scb.sysahbclkctrl |= (1UL << AO_LPC_CT_BEEP_CLKCTRL);
52
53                 /* Set prescaler to match cc1111 clocks
54                  */
55                 AO_LPC_CT_BEEP.pr = AO_LPC_SYSCLK / 750000 - 1;
56
57                 /* Write the desired data in the match registers */
58
59                 /* Reset after two time units */
60                 AO_LPC_CT_BEEP.mr[0] = beep << 1;
61
62                 /* PWM width is half of that */
63                 AO_LPC_CT_BEEP.mr[AO_LPC_CT_BEEP_MR] = beep;
64
65                 /* Flip output on PWM match */
66                 AO_LPC_CT_BEEP.emr = (LPC_CT32B_EMR_EMC_TOGGLE << AO_LPC_CT_BEEP_EMR);
67
68                 /* Reset on match 0 */
69                 AO_LPC_CT_BEEP.mcr = (1 << LPC_CT32B_MCR_MR0R);
70
71                 /* PWM on match */
72                 AO_LPC_CT_BEEP.pwmc = (1 << AO_LPC_CT_BEEP_PWMC);
73
74                 /* timer mode */
75                 AO_LPC_CT_BEEP.ctcr = 0;
76
77                 /* And turn the timer on */
78                 AO_LPC_CT_BEEP.tcr = ((1 << LPC_CT32B_TCR_CEN) |
79                                   (0 << LPC_CT32B_TCR_CRST));
80         }
81 }
82
83 void
84 ao_beep_for(uint8_t beep, AO_TICK_TYPE ticks) 
85 {
86         ao_beep(beep);
87         ao_delay(ticks);
88         ao_beep(0);
89 }
90
91 void
92 ao_beep_init(void)
93 {
94         /* Our beeper is on c32b1_mat1
95          * which is on pin pio0_14
96          */
97
98         lpc_ioconf.AO_LPC_CT_BEEP_IOCONF = ((AO_LPC_CT_BEEP_FUNC << LPC_IOCONF_FUNC) |
99                               (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
100                               (0 << LPC_IOCONF_HYS) |
101                               (0 << LPC_IOCONF_INV) |
102                               (1 << LPC_IOCONF_ADMODE) |
103                               (0 << LPC_IOCONF_OD));
104
105         lpc_scb.sysahbclkctrl |= (1 << AO_LPC_CT_BEEP_CLKCTRL);
106
107         /* Disable the counter and reset the value */
108         AO_LPC_CT_BEEP.tcr = ((0 << LPC_CT32B_TCR_CEN) |
109                           (1 << LPC_CT32B_TCR_CRST));
110 }