00409f4a85957e16e53825f4e8420e85a168c535
[fw/altos] / src / stm / ao_serial_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; 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 struct ao_stm_usart {
21         struct ao_fifo          rx_fifo;
22         struct ao_fifo          tx_fifo;
23         struct stm_usart        *reg;
24         uint8_t                 tx_started;
25 };
26
27 void
28 ao_debug_out(char c)
29 {
30         if (c == '\n')
31                 ao_debug_out('\r');
32         while (!(stm_usart1.sr & (1 << STM_USART_SR_TXE)));
33         stm_usart1.dr = c;
34 }
35
36 static void
37 _ao_usart_tx_start(struct ao_stm_usart *usart)
38 {
39         if (!ao_fifo_empty(usart->tx_fifo) && !usart->tx_started)
40         {
41                 usart->tx_started = 1;
42                 ao_fifo_remove(usart->tx_fifo, usart->reg->dr);
43         }
44 }
45
46 static void
47 ao_usart_isr(struct ao_stm_usart *usart, int stdin)
48 {
49         uint32_t        sr;
50
51         sr = usart->reg->sr;
52         usart->reg->sr = 0;
53
54         if (sr & (1 << STM_USART_SR_RXNE)) {
55                 char c = usart->reg->dr;
56                 if (!ao_fifo_full(usart->rx_fifo))
57                         ao_fifo_insert(usart->rx_fifo, c);
58                 ao_wakeup(&usart->rx_fifo);
59                 if (stdin)
60                         ao_wakeup(&ao_stdin_ready);
61         }
62         if (sr & (1 << STM_USART_SR_TC)) {
63                 usart->tx_started = 0;
64                 _ao_usart_tx_start(usart);
65                 ao_wakeup(&usart->tx_fifo);
66         }
67 }
68
69 char
70 ao_usart_getchar(struct ao_stm_usart *usart)
71 {
72         char c;
73         ao_arch_block_interrupts();
74         while (ao_fifo_empty(usart->rx_fifo))
75                 ao_sleep(&usart->rx_fifo);
76         ao_fifo_remove(usart->rx_fifo, c);
77         ao_arch_release_interrupts();
78         return c;
79 }
80
81 char
82 ao_usart_pollchar(struct ao_stm_usart *usart)
83 {
84         char    c;
85         
86         ao_arch_block_interrupts();
87         if (ao_fifo_empty(usart->rx_fifo))
88                 c = AO_READ_AGAIN;
89         else
90                 ao_fifo_remove(usart->rx_fifo,c);
91         ao_arch_release_interrupts();
92         return c;
93 }
94
95 void
96 ao_usart_putchar(struct ao_stm_usart *usart, char c)
97 {
98         ao_arch_block_interrupts();
99         while (ao_fifo_full(usart->tx_fifo))
100                 ao_sleep(&usart->tx_fifo);
101         ao_fifo_insert(usart->tx_fifo, c);
102         _ao_usart_tx_start(usart);
103         ao_arch_release_interrupts();
104 }
105
106 void
107 ao_usart_drain(struct ao_stm_usart *usart)
108 {
109         ao_arch_block_interrupts();
110         while (!ao_fifo_empty(usart->tx_fifo))
111                 ao_sleep(&usart->tx_fifo);
112         ao_arch_release_interrupts();
113 }
114
115 static const struct {
116         uint32_t brr;
117 } ao_usart_speeds[] = {
118         [AO_SERIAL_SPEED_4800] = {
119                 AO_PCLK1 / 4800
120         },
121         [AO_SERIAL_SPEED_9600] = {
122                 AO_PCLK1 / 9600
123         },
124         [AO_SERIAL_SPEED_19200] = {
125                 AO_PCLK1 / 19200
126         },
127         [AO_SERIAL_SPEED_57600] = {
128                 AO_PCLK1 / 57600
129         },
130 };
131
132 void
133 ao_usart_set_speed(struct ao_stm_usart *usart, uint8_t speed)
134 {
135         if (speed > AO_SERIAL_SPEED_57600)
136                 return;
137         usart->reg->brr = ao_usart_speeds[speed].brr;
138 }
139
140 void
141 ao_usart_init(struct ao_stm_usart *usart)
142 {
143         usart->reg->cr1 = ((0 << STM_USART_CR1_OVER8) |
144                           (1 << STM_USART_CR1_UE) |
145                           (0 << STM_USART_CR1_M) |
146                           (0 << STM_USART_CR1_WAKE) |
147                           (0 << STM_USART_CR1_PCE) |
148                           (0 << STM_USART_CR1_PS) |
149                           (0 << STM_USART_CR1_PEIE) |
150                           (0 << STM_USART_CR1_TXEIE) |
151                           (1 << STM_USART_CR1_TCIE) |
152                           (1 << STM_USART_CR1_RXNEIE) |
153                           (0 << STM_USART_CR1_IDLEIE) |
154                           (1 << STM_USART_CR1_TE) |
155                           (1 << STM_USART_CR1_RE) |
156                           (0 << STM_USART_CR1_RWU) |
157                           (0 << STM_USART_CR1_SBK));
158
159         usart->reg->cr2 = ((0 << STM_USART_CR2_LINEN) |
160                           (STM_USART_CR2_STOP_1 << STM_USART_CR2_STOP) |
161                           (0 << STM_USART_CR2_CLKEN) |
162                           (0 << STM_USART_CR2_CPOL) |
163                           (0 << STM_USART_CR2_CPHA) |
164                           (0 << STM_USART_CR2_LBCL) |
165                           (0 << STM_USART_CR2_LBDIE) |
166                           (0 << STM_USART_CR2_LBDL) |
167                           (0 << STM_USART_CR2_ADD));
168
169         usart->reg->cr3 = ((0 << STM_USART_CR3_ONEBITE) |
170                           (0 << STM_USART_CR3_CTSIE) |
171                           (0 << STM_USART_CR3_CTSE) |
172                           (0 << STM_USART_CR3_RTSE) |
173                           (0 << STM_USART_CR3_DMAT) |
174                           (0 << STM_USART_CR3_DMAR) |
175                           (0 << STM_USART_CR3_SCEN) |
176                           (0 << STM_USART_CR3_NACK) |
177                           (0 << STM_USART_CR3_HDSEL) |
178                           (0 << STM_USART_CR3_IRLP) |
179                           (0 << STM_USART_CR3_IREN) |
180                           (0 << STM_USART_CR3_EIE));
181
182         /* Pick a 9600 baud rate */
183         ao_usart_set_speed(usart, AO_SERIAL_SPEED_9600);
184 }
185
186 #if HAS_SERIAL_1
187
188 struct ao_stm_usart ao_stm_usart1;
189
190 void stm_usart1_isr(void) { ao_usart_isr(&ao_stm_usart1, USE_SERIAL_1_STDIN); }
191
192 char
193 ao_serial1_getchar(void)
194 {
195         return ao_usart_getchar(&ao_stm_usart1);
196 }
197
198 void
199 ao_serial1_putchar(char c)
200 {
201         ao_usart_putchar(&ao_stm_usart1, c);
202 }
203
204 char
205 ao_serial1_pollchar(void)
206 {
207         return ao_usart_pollchar(&ao_stm_usart1);
208 }
209
210 void
211 ao_serial1_set_speed(uint8_t speed)
212 {
213         ao_usart_set_speed(&ao_stm_usart1, speed);
214 }
215 #endif  /* HAS_SERIAL_1 */
216
217 #if HAS_SERIAL_2
218
219 struct ao_stm_usart ao_stm_usart2;
220
221 void stm_usart2_isr(void) { ao_usart_isr(&ao_stm_usart2, USE_SERIAL_2_STDIN); }
222
223 char
224 ao_serial2_getchar(void)
225 {
226         return ao_usart_getchar(&ao_stm_usart2);
227 }
228
229 void
230 ao_serial2_putchar(char c)
231 {
232         ao_usart_putchar(&ao_stm_usart2, c);
233 }
234
235 char
236 ao_serial2_pollchar(void)
237 {
238         return ao_usart_pollchar(&ao_stm_usart2);
239 }
240
241 void
242 ao_serial2_set_speed(uint8_t speed)
243 {
244         ao_usart_set_speed(&ao_stm_usart2, speed);
245 }
246 #endif  /* HAS_SERIAL_2 */
247
248 #if HAS_SERIAL_3
249
250 struct ao_stm_usart ao_stm_usart3;
251
252 void stm_usart3_isr(void) { ao_usart_isr(&ao_stm_usart3, USE_SERIAL_2_STDIN); }
253
254 char
255 ao_serial3_getchar(void)
256 {
257         return ao_usart_getchar(&ao_stm_usart3);
258 }
259
260 void
261 ao_serial3_putchar(char c)
262 {
263         ao_usart_putchar(&ao_stm_usart3, c);
264 }
265
266 char
267 ao_serial3_pollchar(void)
268 {
269         return ao_usart_pollchar(&ao_stm_usart3);
270 }
271
272 void
273 ao_serial3_set_speed(uint8_t speed)
274 {
275         ao_usart_set_speed(&ao_stm_usart3, speed);
276 }
277 #endif  /* HAS_SERIAL_3 */
278
279 void
280 ao_serial_init(void)
281 {
282 #if HAS_SERIAL_1
283         /*
284          *      TX      RX
285          *      PA9     PA10
286          *      PB6     PB7     *
287          */
288
289 #if SERIAL_1_PA9_PA10
290         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
291
292         stm_afr_set(&stm_gpioa, 9, STM_AFR_AF7);
293         stm_afr_set(&stm_gpioa, 10, STM_AFR_AF7);
294 #else
295 #if SERIAL_1_PB6_PB7
296         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
297
298         stm_afr_set(&stm_gpiob, 6, STM_AFR_AF7);
299         stm_afr_set(&stm_gpiob, 7, STM_AFR_AF7);
300 #else
301 #error "No SERIAL_1 port configuration specified"
302 #endif
303 #endif
304         /* Enable USART */
305         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_USART1EN);
306
307         ao_stm_usart1.reg = &stm_usart1;
308         ao_usart_init(&ao_stm_usart1);
309
310         stm_nvic_set_enable(STM_ISR_USART1_POS);
311         stm_nvic_set_priority(STM_ISR_USART1_POS, 4);
312 #if USE_SERIAL_1_STDIN
313         ao_add_stdio(ao_serial1_pollchar,
314                      ao_serial1_putchar,
315                      NULL);
316 #endif
317 #endif
318
319 #if HAS_SERIAL_2
320         /*
321          *      TX      RX
322          *      PA2     PA3
323          *      PD5     PD6
324          */
325
326 #if SERIAL_2_PA2_PA3
327         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
328
329         stm_afr_set(&stm_gpioa, 2, STM_AFR_AF7);
330         stm_afr_set(&stm_gpioa, 3, STM_AFR_AF7);
331 #else
332 #if SERIAL_2_PD5_PD6
333         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
334
335         stm_afr_set(&stm_gpiod, 5, STM_AFR_AF7);
336         stm_afr_set(&stm_gpiod, 6, STM_AFR_AF7);
337 #else
338 #error "No SERIAL_2 port configuration specified"
339 #endif  
340 #endif
341         /* Enable USART */
342         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART2EN);
343
344         ao_stm_usart2.reg = &stm_usart2;
345         ao_usart_init(&ao_stm_usart2);
346
347         stm_nvic_set_enable(STM_ISR_USART2_POS);
348         stm_nvic_set_priority(STM_ISR_USART2_POS, 4);
349 #if USE_SERIAL_2_STDIN
350         ao_add_stdio(ao_serial2_pollchar,
351                      ao_serial2_putchar,
352                      NULL);
353 #endif
354 #endif
355
356 #if HAS_SERIAL_3
357         /*
358          *      TX      RX
359          *      PB10    PB11
360          *      PC10    PC11
361          *      PD8     PD9
362          */
363 #if SERIAL_3_PB10_PB11
364         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
365
366         stm_afr_set(&stm_gpiob, 10, STM_AFR_AF7);
367         stm_afr_set(&stm_gpiob, 11, STM_AFR_AF7);
368 #else
369 #if SERIAL_3_PC10_PC11
370         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN);
371
372         stm_afr_set(&stm_gpioc, 10, STM_AFR_AF7);
373         stm_afr_set(&stm_gpioc, 11, STM_AFR_AF7);
374 #else
375 #if SERIAL_3_PD8_PD9
376         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
377
378         stm_afr_set(&stm_gpiod, 8, STM_AFR_AF7);
379         stm_afr_set(&stm_gpiod, 9, STM_AFR_AF7);
380 #else
381 #error "No SERIAL_3 port configuration specified"
382 #endif
383 #endif
384 #endif
385         /* Enable USART */
386         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART3EN);
387
388         ao_stm_usart3.reg = &stm_usart3;
389         ao_usart_init(&ao_stm_usart3);
390
391         stm_nvic_set_enable(STM_ISR_USART3_POS);
392         stm_nvic_set_priority(STM_ISR_USART3_POS, 4);
393 #if USE_SERIAL_3_STDIN
394         ao_add_stdio(ao_serial3_pollchar,
395                      ao_serial3_putchar,
396                      NULL);
397 #endif
398 #endif
399 }
400
401