altos/telelco-v3.0: Control LCD backlight with PWM
[fw/altos] / src / stm32f1 / ao_pwm_stm.c
1 /*
2  * Copyright © 2024 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_pwm.h"
21
22 static uint8_t  pwm_running;
23
24 static uint16_t pwm_value[NUM_PWM];
25
26 static void
27 ao_pwm_up(void)
28 {
29         if (pwm_running++ == 0) {
30                 struct stm_tim234       *tim = &AO_PWM_TIMER;
31
32                 tim->ccr1 = 0;
33                 tim->ccr2 = 0;
34                 tim->ccr3 = 0;
35                 tim->ccr4 = 0;
36                 tim->arr = PWM_MAX - 1; /* turn on the timer */
37                 tim->cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
38                             (0 << STM_TIM234_CR1_ARPE) |
39                             (STM_TIM234_CR1_CMS_EDGE << STM_TIM234_CR1_CMS) |
40                             (STM_TIM234_CR1_DIR_UP << STM_TIM234_CR1_DIR) |
41                             (0 << STM_TIM234_CR1_OPM) |
42                             (0 << STM_TIM234_CR1_URS) |
43                             (0 << STM_TIM234_CR1_UDIS) |
44                             (1 << STM_TIM234_CR1_CEN));
45
46                 /* Set the timer running */
47                 tim->egr = (1 << STM_TIM234_EGR_UG);
48         }
49 }
50
51 static void
52 ao_pwm_down(void)
53 {
54         if (--pwm_running == 0) {
55                 struct stm_tim234       *tim = &AO_PWM_TIMER;
56
57                 tim->arr = 0;
58                 tim->cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
59                             (0 << STM_TIM234_CR1_ARPE) |
60                             (STM_TIM234_CR1_CMS_EDGE << STM_TIM234_CR1_CMS) |
61                             (STM_TIM234_CR1_DIR_UP << STM_TIM234_CR1_DIR) |
62                             (0 << STM_TIM234_CR1_OPM) |
63                             (0 << STM_TIM234_CR1_URS) |
64                             (0 << STM_TIM234_CR1_UDIS) |
65                             (0 << STM_TIM234_CR1_CEN));
66
67                 /* Stop the timer */
68                 tim->egr = (1 << STM_TIM234_EGR_UG);
69         }
70 }
71
72 void
73 ao_pwm_set(uint8_t pwm, uint16_t value)
74 {
75         struct stm_tim234       *tim = &AO_PWM_TIMER;
76
77 #if PWM_MAX < UINT16_MAX
78         if (value > PWM_MAX)
79                 value = PWM_MAX;
80 #endif
81         if (value != 0) {
82                 if (pwm_value[pwm] == 0)
83                         ao_pwm_up();
84         }
85         switch (pwm) {
86         case 0:
87                 tim->ccr1 = value;
88                 break;
89         case 1:
90                 tim->ccr2 = value;
91                 break;
92         case 2:
93                 tim->ccr3 = value;
94                 break;
95         case 3:
96                 tim->ccr4 = value;
97                 break;
98         }
99         if (value == 0) {
100                 if (pwm_value[pwm] != 0)
101                         ao_pwm_down();
102         }
103         pwm_value[pwm] = value;
104 }
105
106 static void
107 ao_pwm_cmd(void)
108 {
109         uint8_t ch;
110         uint16_t val;
111
112         ch = (uint8_t) ao_cmd_decimal();
113         val = (uint16_t) ao_cmd_decimal();
114         if (ao_cmd_status != ao_cmd_success)
115                 return;
116
117         printf("Set channel %d to %d\n", ch, val);
118         ao_pwm_set(ch, val);
119 }
120
121 static const struct ao_cmds ao_pwm_cmds[] = {
122         { ao_pwm_cmd,   "P <ch> <val>\0Set PWM ch to val" },
123         { 0, NULL },
124 };
125
126 void
127 ao_pwm_init(void)
128 {
129         struct stm_tim234       *tim = &AO_PWM_TIMER;
130
131         stm_rcc.apb1enr |= (1 << AO_PWM_TIMER_ENABLE);
132
133         tim->cr1 = 0;
134         tim->psc = AO_PWM_TIMER_SCALE - 1;
135         tim->cnt = 0;
136         tim->ccer = ((1 << STM_TIM234_CCER_CC1E) |
137                      (0 << STM_TIM234_CCER_CC1P) |
138                      (1 << STM_TIM234_CCER_CC2E) |
139                      (0 << STM_TIM234_CCER_CC2P) |
140                      (1 << STM_TIM234_CCER_CC3E) |
141                      (0 << STM_TIM234_CCER_CC3P) |
142                      (1 << STM_TIM234_CCER_CC4E) |
143                      (0 << STM_TIM234_CCER_CC4P));
144
145         tim->ccmr1 = ((0 << STM_TIM234_CCMR1_OC2CE) |
146                       (STM_TIM234_CCMR1_OC2M_PWM_MODE_1 << STM_TIM234_CCMR1_OC2M) |
147                       (0 << STM_TIM234_CCMR1_OC2PE) |
148                       (0 << STM_TIM234_CCMR1_OC2FE) |
149                       (STM_TIM234_CCMR1_CC2S_OUTPUT << STM_TIM234_CCMR1_CC2S) |
150
151                       (0 << STM_TIM234_CCMR1_OC1CE) |
152                       (STM_TIM234_CCMR1_OC1M_PWM_MODE_1 << STM_TIM234_CCMR1_OC1M) |
153                       (0 << STM_TIM234_CCMR1_OC1PE) |
154                       (0 << STM_TIM234_CCMR1_OC1FE) |
155                       (STM_TIM234_CCMR1_CC1S_OUTPUT << STM_TIM234_CCMR1_CC1S));
156
157
158         tim->ccmr2 = ((0 << STM_TIM234_CCMR2_OC4CE) |
159                       (STM_TIM234_CCMR2_OC4M_PWM_MODE_1 << STM_TIM234_CCMR2_OC4M) |
160                       (0 << STM_TIM234_CCMR2_OC4PE) |
161                       (0 << STM_TIM234_CCMR2_OC4FE) |
162                       (STM_TIM234_CCMR2_CC4S_OUTPUT << STM_TIM234_CCMR2_CC4S) |
163
164                       (0 << STM_TIM234_CCMR2_OC3CE) |
165                       (STM_TIM234_CCMR2_OC3M_PWM_MODE_1 << STM_TIM234_CCMR2_OC3M) |
166                       (0 << STM_TIM234_CCMR2_OC3PE) |
167                       (0 << STM_TIM234_CCMR2_OC3FE) |
168                       (STM_TIM234_CCMR2_CC3S_OUTPUT << STM_TIM234_CCMR2_CC3S));
169         tim->egr = 0;
170
171         tim->sr = 0;
172         tim->dier = 0;
173         tim->smcr = 0;
174         tim->cr2 = ((0 << STM_TIM234_CR2_TI1S) |
175                     (STM_TIM234_CR2_MMS_RESET<< STM_TIM234_CR2_MMS) |
176                     (0 << STM_TIM234_CR2_CCDS));
177
178         stm_set_afio_mapr(AO_AFIO_PWM_REMAP,
179                           AO_AFIO_PWM_REMAP_VAL,
180                           AO_AFIO_PWM_REMAP_MASK);
181
182         ao_enable_port(AO_PWM_0_GPIO);
183
184         stm_gpio_conf(AO_PWM_0_GPIO, AO_PWM_0_PIN,
185                       STM_GPIO_CR_MODE_OUTPUT_2MHZ,
186                       STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
187 #if NUM_PWM > 1
188         stm_gpio_conf(AO_PWM_1_GPIO, AO_PWM_1_PIN,
189                       STM_GPIO_CR_MODE_OUTPUT_2MHZ,
190                       STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
191 #endif
192 #if NUM_PWM > 2
193         stm_gpio_conf(AO_PWM_2_GPIO, AO_PWM_2_PIN,
194                       STM_GPIO_CR_MODE_OUTPUT_2MHZ,
195                       STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
196 #endif
197 #if NUM_PWM > 3
198         stm_gpio_conf(AO_PWM_3_GPIO, AO_PWM_3_PIN,
199                       STM_GPIO_CR_MODE_OUTPUT_2MHZ,
200                       STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
201 #endif
202         ao_cmd_register(&ao_pwm_cmds[0]);
203 }