altos/stm: Make beeper driver support all possible tim234 configs
[fw/altos] / src / stm / 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
21 #if BEEPER_TIMER == 2
22 #define stm_beeper      stm_tim2
23 #define RCC_BEEPER      STM_RCC_APB1ENR_TIM2EN
24 #define BEEPER_AFR      STM_AFR_AF1
25 #elif BEEPER_TIMER == 3
26 #define stm_beeper      stm_tim3
27 #define RCC_BEEPER      STM_RCC_APB1ENR_TIM3EN
28 #define BEEPER_AFR      STM_AFR_AF2
29 #elif BEEPER_TIMER == 4
30 #define stm_beeper      stm_tim4
31 #define RCC_BEEPER      STM_RCC_APB1ENR_TIM4EN
32 #define BEEPER_AFR      STM_AFR_AF2
33 #else
34 #error BEEPER_TIMER must be 2, 3 or 4
35 #endif
36
37 void
38 ao_beep(uint8_t beep)
39 {
40         if (beep == 0) {
41                 stm_beeper.cr1 = 0;
42                 stm_rcc.apb1enr &= ~(1 << RCC_BEEPER);
43         } else {
44                 stm_rcc.apb1enr |= (1 << RCC_BEEPER);
45
46                 stm_beeper.cr2 = ((0 << STM_TIM234_CR2_TI1S) |
47                                 (STM_TIM234_CR2_MMS_RESET << STM_TIM234_CR2_MMS) |
48                                 (0 << STM_TIM234_CR2_CCDS));
49
50                 /* Set prescaler to match cc1111 clocks
51                  */
52                 stm_beeper.psc = AO_TIM23467_CLK / 750000;
53
54                 /* 1. Select the counter clock (internal, external, prescaler).
55                  *
56                  * Setting SMCR to zero means use the internal clock
57                  */
58
59                 stm_beeper.smcr = 0;
60
61                 /* 2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers. */
62                 stm_beeper.arr = beep;
63 #if BEEPER_CHANNEL == 1
64                 stm_beeper.ccr1 = beep;
65 #elif BEEPER_CHANNEL == 2
66                 stm_beeper.ccr2 = beep;
67 #elif BEEPER_CHANNEL == 3
68                 stm_beeper.ccr3 = beep;
69 #elif BEEPER_CHANNEL == 4
70                 stm_beeper.ccr4 = beep;
71 #else
72 #error invalid BEEPER_CHANNEL
73 #endif
74
75                 /* 3. Set the CCxIE and/or CCxDE bits if an interrupt and/or a
76                  * DMA request is to be generated.
77                  */
78                 /* don't want this */
79
80                 /* 4. Select the output mode. For example, you must write
81                  *  OCxM=011, OCxPE=0, CCxP=0 and CCxE=1 to toggle OCx output
82                  *  pin when CNT matches CCRx, CCRx preload is not used, OCx
83                  *  is enabled and active high.
84                  */
85
86 #define OC1M    (BEEPER_CHANNEL == 1 ? STM_TIM234_CCMR1_OC1M_TOGGLE : STM_TIM234_CCMR1_OC1M_FROZEN)
87 #define OC2M    (BEEPER_CHANNEL == 2 ? STM_TIM234_CCMR1_OC2M_TOGGLE : STM_TIM234_CCMR1_OC2M_FROZEN)
88 #define OC3M    (BEEPER_CHANNEL == 3 ? STM_TIM234_CCMR2_OC3M_TOGGLE : STM_TIM234_CCMR2_OC3M_FROZEN)
89 #define OC4M    (BEEPER_CHANNEL == 4 ? STM_TIM234_CCMR2_OC4M_TOGGLE : STM_TIM234_CCMR2_OC4M_FROZEN)
90
91 #define CCER(n) (BEEPER_CHANNEL == (n) ? 1 : 0)
92
93 #if BEEPER_CHANNEL == 1 || BEEPER_CHANNEL == 2
94                 stm_beeper.ccmr1 = ((0 << STM_TIM234_CCMR1_OC2CE) |
95                                     (OC2M << STM_TIM234_CCMR1_OC2M) |
96                                     (0 << STM_TIM234_CCMR1_OC2PE) |
97                                     (0 << STM_TIM234_CCMR1_OC2FE) |
98                                     (STM_TIM234_CCMR1_CC2S_OUTPUT << STM_TIM234_CCMR1_CC2S) |
99
100                                     (0 << STM_TIM234_CCMR1_OC1CE) |
101                                     (OC1M << STM_TIM234_CCMR1_OC1M) |
102                                     (0 << STM_TIM234_CCMR1_OC1PE) |
103                                     (0 << STM_TIM234_CCMR1_OC1FE) |
104                                     (STM_TIM234_CCMR1_CC1S_OUTPUT << STM_TIM234_CCMR1_CC1S));
105 #elif BEEPER_CHANNEL == 3 || BEEPER_CHANNEL == 4
106                 stm_beeper.ccmr2 = ((0 << STM_TIM234_CCMR2_OC4CE) |
107                                     (OC4M << STM_TIM234_CCMR2_OC4M) |
108                                     (0 << STM_TIM234_CCMR2_OC4PE) |
109                                     (0 << STM_TIM234_CCMR2_OC4FE) |
110                                     (STM_TIM234_CCMR2_CC4S_OUTPUT << STM_TIM234_CCMR2_CC4S) |
111
112                                     (0 << STM_TIM234_CCMR2_OC3CE) |
113                                     (OC3M << STM_TIM234_CCMR2_OC3M) |
114                                     (0 << STM_TIM234_CCMR2_OC3PE) |
115                                     (0 << STM_TIM234_CCMR2_OC3FE) |
116                                     (STM_TIM234_CCMR2_CC3S_OUTPUT << STM_TIM234_CCMR2_CC3S));
117 #else
118 #error invalid BEEPER_CHANNEL
119 #endif
120                 stm_beeper.ccer = ((0 << STM_TIM234_CCER_CC4NP) |
121                                    (0 << STM_TIM234_CCER_CC4P) |
122                                    (CCER(4) << STM_TIM234_CCER_CC4E) |
123                                    (0 << STM_TIM234_CCER_CC3NP) |
124                                    (0 << STM_TIM234_CCER_CC3P) |
125                                    (CCER(3) << STM_TIM234_CCER_CC3E) |
126                                    (0 << STM_TIM234_CCER_CC2NP) |
127                                    (0 << STM_TIM234_CCER_CC2P) |
128                                    (CCER(2) << STM_TIM234_CCER_CC2E) |
129                                    (0 << STM_TIM234_CCER_CC1NP) |
130                                    (0 << STM_TIM234_CCER_CC1P) |
131                                    (CCER(1) << STM_TIM234_CCER_CC1E));
132
133                 /* 5. Enable the counter by setting the CEN bit in the TIMx_CR1 register. */
134
135                 stm_beeper.cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
136                                 (0 << STM_TIM234_CR1_ARPE) |
137                                 (STM_TIM234_CR1_CMS_EDGE << STM_TIM234_CR1_CMS) |
138                                 (0 << STM_TIM234_CR1_DIR) |
139                                 (0 << STM_TIM234_CR1_OPM) |
140                                 (0 << STM_TIM234_CR1_URS) |
141                                 (0 << STM_TIM234_CR1_UDIS) |
142                                 (1 << STM_TIM234_CR1_CEN));
143
144                 /* Update the values */
145                 stm_beeper.egr = (1 << STM_TIM234_EGR_UG);
146         }
147 }
148
149 void
150 ao_beep_for(uint8_t beep, uint16_t ticks)
151 {
152         ao_beep(beep);
153         ao_delay(ticks);
154         ao_beep(0);
155 }
156
157 void
158 ao_beep_init(void)
159 {
160         ao_enable_port(BEEPER_PORT);
161         stm_afr_set(BEEPER_PORT, BEEPER_PIN, BEEPER_AFR);
162
163         /* Leave the timer off until requested */
164         stm_rcc.apb1enr &= ~(1 << RCC_BEEPER);
165 }