altos/stm32f1: Set beeper pin to 0 while off
[fw/altos] / src / stm32f1 / ao_beep_stm.c
1 /*
2  * Copyright © 2012 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 #include "ao_beep.h"
21
22 #if BEEPER_TIMER == 2
23 #define stm_beeper      stm_tim2
24 #define RCC_BEEPER      STM_RCC_APB1ENR_TIM2EN
25 #elif BEEPER_TIMER == 3
26 #define stm_beeper      stm_tim3
27 #define RCC_BEEPER      STM_RCC_APB1ENR_TIM3EN
28 #elif BEEPER_TIMER == 4
29 #define stm_beeper      stm_tim4
30 #define RCC_BEEPER      STM_RCC_APB1ENR_TIM4EN
31 #else
32 #error BEEPER_TIMER must be 2, 3 or 4
33 #endif
34
35 void
36 ao_beep(uint8_t beep)
37 {
38         if (beep == 0) {
39                 stm_beeper.cr1 = 0;
40                 stm_rcc.apb1enr &= ~(1UL << RCC_BEEPER);
41                 stm_gpio_conf(BEEPER_PORT, BEEPER_PIN,
42                               STM_GPIO_CR_MODE_OUTPUT_2MHZ,
43                               STM_GPIO_CR_CNF_OUTPUT_PUSH_PULL);
44         } else {
45                 stm_gpio_conf(BEEPER_PORT, BEEPER_PIN,
46                               STM_GPIO_CR_MODE_OUTPUT_2MHZ,
47                               STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
48                 stm_rcc.apb1enr |= (1UL << RCC_BEEPER);
49
50                 stm_beeper.cr2 = ((0 << STM_TIM234_CR2_TI1S) |
51                                 (STM_TIM234_CR2_MMS_RESET << STM_TIM234_CR2_MMS) |
52                                 (0 << STM_TIM234_CR2_CCDS));
53
54                 /* Set prescaler to match cc1111 clocks
55                  */
56                 stm_beeper.psc = AO_TIM23467_CLK / 750000;
57
58                 /* 1. Select the counter clock (internal, external, prescaler).
59                  *
60                  * Setting SMCR to zero means use the internal clock
61                  */
62
63                 stm_beeper.smcr = 0;
64
65                 /* 2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers. */
66                 stm_beeper.arr = beep;
67 #if BEEPER_CHANNEL == 1
68                 stm_beeper.ccr1 = beep;
69 #elif BEEPER_CHANNEL == 2
70                 stm_beeper.ccr2 = beep;
71 #elif BEEPER_CHANNEL == 3
72                 stm_beeper.ccr3 = beep;
73 #elif BEEPER_CHANNEL == 4
74                 stm_beeper.ccr4 = beep;
75 #else
76 #error invalid BEEPER_CHANNEL
77 #endif
78
79                 /* 3. Set the CCxIE and/or CCxDE bits if an interrupt and/or a
80                  * DMA request is to be generated.
81                  */
82                 /* don't want this */
83
84                 /* 4. Select the output mode. For example, you must write
85                  *  OCxM=011, OCxPE=0, CCxP=0 and CCxE=1 to toggle OCx output
86                  *  pin when CNT matches CCRx, CCRx preload is not used, OCx
87                  *  is enabled and active high.
88                  */
89
90 #define OC1M    (BEEPER_CHANNEL == 1 ? STM_TIM234_CCMR1_OC1M_TOGGLE : STM_TIM234_CCMR1_OC1M_FROZEN)
91 #define OC2M    (BEEPER_CHANNEL == 2 ? STM_TIM234_CCMR1_OC2M_TOGGLE : STM_TIM234_CCMR1_OC2M_FROZEN)
92 #define OC3M    (BEEPER_CHANNEL == 3 ? STM_TIM234_CCMR2_OC3M_TOGGLE : STM_TIM234_CCMR2_OC3M_FROZEN)
93 #define OC4M    (BEEPER_CHANNEL == 4 ? STM_TIM234_CCMR2_OC4M_TOGGLE : STM_TIM234_CCMR2_OC4M_FROZEN)
94
95 #define CCER(n) (BEEPER_CHANNEL == (n) ? 1 : 0)
96
97 #if BEEPER_CHANNEL == 1 || BEEPER_CHANNEL == 2
98                 stm_beeper.ccmr1 = ((0 << STM_TIM234_CCMR1_OC2CE) |
99                                     (OC2M << STM_TIM234_CCMR1_OC2M) |
100                                     (0 << STM_TIM234_CCMR1_OC2PE) |
101                                     (0 << STM_TIM234_CCMR1_OC2FE) |
102                                     (STM_TIM234_CCMR1_CC2S_OUTPUT << STM_TIM234_CCMR1_CC2S) |
103
104                                     (0 << STM_TIM234_CCMR1_OC1CE) |
105                                     (OC1M << STM_TIM234_CCMR1_OC1M) |
106                                     (0 << STM_TIM234_CCMR1_OC1PE) |
107                                     (0 << STM_TIM234_CCMR1_OC1FE) |
108                                     (STM_TIM234_CCMR1_CC1S_OUTPUT << STM_TIM234_CCMR1_CC1S));
109 #elif BEEPER_CHANNEL == 3 || BEEPER_CHANNEL == 4
110                 stm_beeper.ccmr2 = ((0 << STM_TIM234_CCMR2_OC4CE) |
111                                     (OC4M << STM_TIM234_CCMR2_OC4M) |
112                                     (0 << STM_TIM234_CCMR2_OC4PE) |
113                                     (0 << STM_TIM234_CCMR2_OC4FE) |
114                                     (STM_TIM234_CCMR2_CC4S_OUTPUT << STM_TIM234_CCMR2_CC4S) |
115
116                                     (0 << STM_TIM234_CCMR2_OC3CE) |
117                                     (OC3M << STM_TIM234_CCMR2_OC3M) |
118                                     (0 << STM_TIM234_CCMR2_OC3PE) |
119                                     (0 << STM_TIM234_CCMR2_OC3FE) |
120                                     (STM_TIM234_CCMR2_CC3S_OUTPUT << STM_TIM234_CCMR2_CC3S));
121 #else
122 #error invalid BEEPER_CHANNEL
123 #endif
124                 stm_beeper.ccer = ((0 << STM_TIM234_CCER_CC4NP) |
125                                    (0 << STM_TIM234_CCER_CC4P) |
126                                    (CCER(4) << STM_TIM234_CCER_CC4E) |
127                                    (0 << STM_TIM234_CCER_CC3NP) |
128                                    (0 << STM_TIM234_CCER_CC3P) |
129                                    (CCER(3) << STM_TIM234_CCER_CC3E) |
130                                    (0 << STM_TIM234_CCER_CC2NP) |
131                                    (0 << STM_TIM234_CCER_CC2P) |
132                                    (CCER(2) << STM_TIM234_CCER_CC2E) |
133                                    (0 << STM_TIM234_CCER_CC1NP) |
134                                    (0 << STM_TIM234_CCER_CC1P) |
135                                    (CCER(1) << STM_TIM234_CCER_CC1E));
136
137                 /* 5. Enable the counter by setting the CEN bit in the TIMx_CR1 register. */
138
139                 stm_beeper.cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
140                                 (0 << STM_TIM234_CR1_ARPE) |
141                                 (STM_TIM234_CR1_CMS_EDGE << STM_TIM234_CR1_CMS) |
142                                 (0 << STM_TIM234_CR1_DIR) |
143                                 (0 << STM_TIM234_CR1_OPM) |
144                                 (0 << STM_TIM234_CR1_URS) |
145                                 (0 << STM_TIM234_CR1_UDIS) |
146                                 (1 << STM_TIM234_CR1_CEN));
147
148                 /* Update the values */
149                 stm_beeper.egr = (1 << STM_TIM234_EGR_UG);
150         }
151 }
152
153 void
154 ao_beep_for(uint8_t beep, AO_TICK_TYPE ticks)
155 {
156         ao_beep(beep);
157         ao_delay(ticks);
158         ao_beep(0);
159 }
160
161 void
162 ao_beep_init(void)
163 {
164 #if BEEPER_TIMER == 2
165         if (BEEPER_PORT == &stm_gpioa) {
166                 switch (BEEPER_PIN) {
167                 case 0:
168                 case 1:
169                 case 2:
170                 case 3:
171                         stm_set_afio_mapr(STM_AFIO_MAPR_TIM2_REMAP,
172                                           STM_AFIO_MAPR_TIM2_REMAP_PA0_PA1_PA2_PA3,
173                                           STM_AFIO_MAPR_TIM2_REMAP_MASK);
174                         break;
175                 default:
176                         ao_panic(AO_PANIC_CRASH);
177                         break;
178                 }
179         }
180 #elif BEEPER_TIMER == 3
181         if (BEEPER_PORT == &stm_gpioa) {
182                 switch (BEEPER_PIN) {
183                 case 6:
184                 case 7:
185                         stm_set_afio_mapr(STM_AFIO_MAPR_TIM3_REMAP,
186                                           STM_AFIO_MAPR_TIM3_REMAP_PA6_PA7_PB0_PB1,
187                                           STM_AFIO_MAPR_TIM3_REMAP_MASK);
188                         break;
189                 default:
190                         ao_panic(AO_PANIC_CRASH);
191                         break;
192                 }
193         } else if (BEEPER_PORT == &stm_gpiob) {
194                 switch (BEEPER_PIN) {
195                 case 4:
196                 case 5:
197                 case 0:
198                 case 1:
199                         stm_set_afio_mapr(STM_AFIO_MAPR_TIM3_REMAP,
200                                           STM_AFIO_MAPR_TIM3_REMAP_PB4_PB5_PB0_PB1,
201                                           STM_AFIO_MAPR_TIM3_REMAP_MASK);
202                         break;
203                 default:
204                         ao_panic(AO_PANIC_CRASH);
205                         break;
206                 }
207         } else if (BEEPER_PORT == &stm_gpioc) {
208                 switch (BEEPER_PIN) {
209                 case 6:
210                 case 7:
211                 case 8:
212                 case 9:
213                         stm_set_afio_mapr(STM_AFIO_MAPR_TIM3_REMAP,
214                                           STM_AFIO_MAPR_TIM3_REMAP_PC6_PC7_PC8_PC9,
215                                           STM_AFIO_MAPR_TIM3_REMAP_MASK);
216                         break;
217                 default:
218                         ao_panic(AO_PANIC_CRASH);
219                         break;
220                 }
221         }
222 #elif BEEPER_TIMER == 4
223         ao_panic(AO_PANIC_CRASH);
224 #endif
225         ao_enable_output(BEEPER_PORT, BEEPER_PIN, 0);
226
227         /* Leave the timer off until requested */
228         stm_rcc.apb1enr &= ~(1UL << RCC_BEEPER);
229 }