2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
27 while (!(stm_usart1.sr & (1 << STM_USART_SR_TXE)));
31 static volatile uint8_t tx;
34 _ao_usart_tx_start(struct ao_stm_usart *usart)
36 if (!ao_fifo_empty(usart->tx_fifo)) {
37 #if HAS_SERIAL_SW_FLOW
38 if (usart->gpio_cts && ao_gpio_get(usart->gpio_cts, usart->pin_cts) == 1) {
39 ao_exti_enable(usart->gpio_cts, usart->pin_cts);
43 if (usart->reg->sr & (1 << STM_USART_SR_TXE))
45 usart->tx_running = 1;
46 usart->reg->cr1 |= (1 << STM_USART_CR1_TXEIE) | (1 << STM_USART_CR1_TCIE);
47 ao_fifo_remove(usart->tx_fifo, tx);
49 ao_wakeup(&usart->tx_fifo);
56 #if HAS_SERIAL_SW_FLOW
58 _ao_usart_cts(struct ao_stm_usart *usart)
60 if (_ao_usart_tx_start(usart))
61 ao_exti_disable(usart->gpio_cts, usart->pin_cts);
66 _ao_usart_rx(struct ao_stm_usart *usart, int is_stdin)
68 if (usart->reg->sr & (1 << STM_USART_SR_RXNE)) {
69 if (!ao_fifo_full(usart->rx_fifo)) {
70 ao_fifo_insert(usart->rx_fifo, (char) usart->reg->dr);
71 ao_wakeup(&usart->rx_fifo);
73 ao_wakeup(&ao_stdin_ready);
74 #if HAS_SERIAL_SW_FLOW
75 /* If the fifo is nearly full, turn off RTS and wait
76 * for it to drain a bunch
78 if (usart->gpio_rts && ao_fifo_mostly(usart->rx_fifo)) {
79 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 1);
84 usart->reg->cr1 &= ~(1UL << STM_USART_CR1_RXNEIE);
90 ao_usart_isr(struct ao_stm_usart *usart, int is_stdin)
92 _ao_usart_rx(usart, is_stdin);
94 if (!_ao_usart_tx_start(usart))
95 usart->reg->cr1 &= ~(1UL << STM_USART_CR1_TXEIE);
97 if (usart->reg->sr & (1 << STM_USART_SR_TC)) {
98 usart->tx_running = 0;
99 usart->reg->cr1 &= ~(1UL << STM_USART_CR1_TCIE);
100 if (usart->draining) {
102 ao_wakeup(&usart->tx_fifo);
108 _ao_usart_pollchar(struct ao_stm_usart *usart)
112 if (ao_fifo_empty(usart->rx_fifo))
116 ao_fifo_remove(usart->rx_fifo,u);
117 if ((usart->reg->cr1 & (1 << STM_USART_CR1_RXNEIE)) == 0) {
118 if (ao_fifo_barely(usart->rx_fifo))
119 usart->reg->cr1 |= (1 << STM_USART_CR1_RXNEIE);
121 #if HAS_SERIAL_SW_FLOW
122 /* If we've cleared RTS, check if there's space now and turn it back on */
123 if (usart->gpio_rts && usart->rts == 0 && ao_fifo_barely(usart->rx_fifo)) {
124 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 0);
134 ao_usart_getchar(struct ao_stm_usart *usart)
137 ao_arch_block_interrupts();
138 while ((c = _ao_usart_pollchar(usart)) == AO_READ_AGAIN)
139 ao_sleep(&usart->rx_fifo);
140 ao_arch_release_interrupts();
144 static inline uint8_t
145 _ao_usart_sleep_for(struct ao_stm_usart *usart, AO_TICK_TYPE timeout)
147 return ao_sleep_for(&usart->rx_fifo, timeout);
151 ao_usart_putchar(struct ao_stm_usart *usart, char c)
153 ao_arch_block_interrupts();
154 while (ao_fifo_full(usart->tx_fifo))
155 ao_sleep(&usart->tx_fifo);
156 ao_fifo_insert(usart->tx_fifo, c);
157 _ao_usart_tx_start(usart);
158 ao_arch_release_interrupts();
162 ao_usart_drain(struct ao_stm_usart *usart)
164 ao_arch_block_interrupts();
165 while (!ao_fifo_empty(usart->tx_fifo) || usart->tx_running) {
167 ao_sleep(&usart->tx_fifo);
169 ao_arch_release_interrupts();
172 static const struct {
174 } ao_usart_speeds[] = {
175 [AO_SERIAL_SPEED_4800] = {
178 [AO_SERIAL_SPEED_9600] = {
181 [AO_SERIAL_SPEED_19200] = {
184 [AO_SERIAL_SPEED_57600] = {
187 [AO_SERIAL_SPEED_115200] = {
193 ao_usart_set_speed(struct ao_stm_usart *usart, uint8_t speed)
197 if (speed > AO_SERIAL_SPEED_115200)
200 if (usart == &ao_stm_usart1)
201 brr = AO_PCLK2 / ao_usart_speeds[speed].baud;
204 brr = AO_PCLK1 / ao_usart_speeds[speed].baud;
205 usart->reg->brr = brr;
209 ao_usart_init(struct ao_stm_usart *usart, int hw_flow, uint8_t speed)
211 usart->reg->cr1 = ((1 << STM_USART_CR1_UE) |
212 (0 << STM_USART_CR1_M) |
213 (0 << STM_USART_CR1_WAKE) |
214 (0 << STM_USART_CR1_PCE) |
215 (0 << STM_USART_CR1_PS) |
216 (0 << STM_USART_CR1_PEIE) |
217 (0 << STM_USART_CR1_TXEIE) |
218 (0 << STM_USART_CR1_TCIE) |
219 (1 << STM_USART_CR1_RXNEIE) |
220 (0 << STM_USART_CR1_IDLEIE) |
221 (1 << STM_USART_CR1_TE) |
222 (1 << STM_USART_CR1_RE) |
223 (0 << STM_USART_CR1_RWU) |
224 (0 << STM_USART_CR1_SBK));
226 usart->reg->cr2 = ((0 << STM_USART_CR2_LINEN) |
227 (STM_USART_CR2_STOP_1 << STM_USART_CR2_STOP) |
228 (0 << STM_USART_CR2_CLKEN) |
229 (0 << STM_USART_CR2_CPOL) |
230 (0 << STM_USART_CR2_CPHA) |
231 (0 << STM_USART_CR2_LBCL) |
232 (0 << STM_USART_CR2_LBDIE) |
233 (0 << STM_USART_CR2_LBDL) |
234 (0 << STM_USART_CR2_ADD));
236 usart->reg->cr3 = ((0 << STM_USART_CR3_CTSIE) |
237 (0 << STM_USART_CR3_CTSE) |
238 (0 << STM_USART_CR3_RTSE) |
239 (0 << STM_USART_CR3_DMAT) |
240 (0 << STM_USART_CR3_DMAR) |
241 (0 << STM_USART_CR3_SCEN) |
242 (0 << STM_USART_CR3_NACK) |
243 (0 << STM_USART_CR3_HDSEL) |
244 (0 << STM_USART_CR3_IRLP) |
245 (0 << STM_USART_CR3_IREN) |
246 (0 << STM_USART_CR3_EIE));
249 usart->reg->cr3 |= ((1 << STM_USART_CR3_CTSE) |
250 (1 << STM_USART_CR3_RTSE));
252 ao_usart_set_speed(usart, speed);
257 struct ao_stm_usart ao_stm_usart1;
259 void stm_usart1_isr(void) { ao_usart_isr(&ao_stm_usart1, USE_SERIAL_1_STDIN); }
262 ao_serial1_getchar(void)
264 return ao_usart_getchar(&ao_stm_usart1);
268 ao_serial1_putchar(char c)
270 ao_usart_putchar(&ao_stm_usart1, c);
274 _ao_serial1_pollchar(void)
276 return _ao_usart_pollchar(&ao_stm_usart1);
280 _ao_serial1_sleep_for(AO_TICK_TYPE timeout)
282 return _ao_usart_sleep_for(&ao_stm_usart1, timeout);
286 ao_serial1_drain(void)
288 ao_usart_drain(&ao_stm_usart1);
292 ao_serial1_set_speed(uint8_t speed)
294 ao_usart_drain(&ao_stm_usart1);
295 ao_usart_set_speed(&ao_stm_usart1, speed);
297 #endif /* HAS_SERIAL_1 */
301 struct ao_stm_usart ao_stm_usart2;
303 void stm_usart2_isr(void) { ao_usart_isr(&ao_stm_usart2, USE_SERIAL_2_STDIN); }
306 ao_serial2_getchar(void)
308 return ao_usart_getchar(&ao_stm_usart2);
312 ao_serial2_putchar(char c)
314 ao_usart_putchar(&ao_stm_usart2, c);
318 _ao_serial2_pollchar(void)
320 return _ao_usart_pollchar(&ao_stm_usart2);
324 _ao_serial2_sleep_for(AO_TICK_TYPE timeout)
326 return _ao_usart_sleep_for(&ao_stm_usart2, timeout);
330 ao_serial2_drain(void)
332 ao_usart_drain(&ao_stm_usart2);
336 ao_serial2_set_speed(uint8_t speed)
338 ao_usart_drain(&ao_stm_usart2);
339 ao_usart_set_speed(&ao_stm_usart2, speed);
342 #if HAS_SERIAL_SW_FLOW
346 _ao_usart_cts(&ao_stm_usart2);
350 #endif /* HAS_SERIAL_2 */
354 struct ao_stm_usart ao_stm_usart3;
356 void stm_usart3_isr(void) { ao_usart_isr(&ao_stm_usart3, USE_SERIAL_3_STDIN); }
359 ao_serial3_getchar(void)
361 return ao_usart_getchar(&ao_stm_usart3);
365 ao_serial3_putchar(char c)
367 ao_usart_putchar(&ao_stm_usart3, c);
371 _ao_serial3_pollchar(void)
373 return _ao_usart_pollchar(&ao_stm_usart3);
377 _ao_serial3_sleep_for(AO_TICK_TYPE timeout)
379 return _ao_usart_sleep_for(&ao_stm_usart3, timeout);
383 ao_serial3_set_speed(uint8_t speed)
385 ao_usart_drain(&ao_stm_usart3);
386 ao_usart_set_speed(&ao_stm_usart3, speed);
390 ao_serial3_drain(void)
392 ao_usart_drain(&ao_stm_usart3);
394 #endif /* HAS_SERIAL_3 */
396 #if HAS_SERIAL_SW_FLOW
398 ao_serial_set_sw_rts_cts(struct ao_stm_usart *usart,
400 struct stm_gpio *port_rts,
402 struct stm_gpio *port_cts,
405 /* Pull RTS low to note that there's space in the FIFO
407 ao_enable_output(port_rts, pin_rts, 0);
408 usart->gpio_rts = port_rts;
409 usart->pin_rts = pin_rts;
412 ao_exti_setup(port_cts, pin_cts, AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_MED, isr);
413 usart->gpio_cts = port_cts;
414 usart->pin_cts = pin_cts;
428 #if SERIAL_1_PA9_PA10
429 ao_enable_port(&stm_gpioa);
430 stm_gpio_conf(&stm_gpioa, 9,
431 STM_GPIO_CR_MODE_OUTPUT_2MHZ,
432 STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
434 stm_gpio_conf(&stm_gpioa, 10,
435 STM_GPIO_CR_MODE_INPUT,
436 STM_GPIO_CR_CNF_INPUT_FLOATING);
438 stm_set_afio_mapr(STM_AFIO_MAPR_USART1_REMAP,
439 STM_AFIO_MAPR_USART1_REMAP_PA9_PA10,
440 STM_AFIO_MAPR_USART1_REMAP_MASK);
443 ao_enable_port(&stm_gpiob);
444 stm_gpio_conf(&stm_gpiob, 6,
445 STM_GPIO_CR_MODE_OUTPUT_2MHZ,
446 STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
448 stm_gpio_conf(&stm_gpiob, 7,
449 STM_GPIO_CR_MODE_INPUT,
450 STM_GPIO_CR_CNF_INPUT_FLOATING);
452 stm_set_afio_mapr(STM_AFIO_MAPR_USART1_REMAP,
453 STM_AFIO_MAPR_USART1_REMAP_PB6_PB7,
454 STM_AFIO_MAPR_USART1_REMAP_MASK);
456 #error "No SERIAL_1 port configuration specified"
460 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_USART1EN);
462 ao_stm_usart1.reg = &stm_usart1;
463 ao_usart_init(&ao_stm_usart1, 0);
465 stm_nvic_set_enable(STM_ISR_USART1_POS);
466 stm_nvic_set_priority(STM_ISR_USART1_POS, AO_STM_NVIC_MED_PRIORITY);
467 #if USE_SERIAL_1_STDIN && !DELAY_SERIAL_1_STDIN
468 ao_add_stdio(_ao_serial1_pollchar,
482 ao_enable_port(&stm_gpioa);
483 stm_gpio_conf(&stm_gpioa, 2,
484 STM_GPIO_CR_MODE_OUTPUT_2MHZ,
485 STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
487 stm_gpio_conf(&stm_gpioa, 3,
488 STM_GPIO_CR_MODE_INPUT,
489 STM_GPIO_CR_CNF_INPUT_FLOATING);
491 #ifndef USE_SERIAL_2_FLOW
492 #define USE_SERIAL_2_FLOW 0
493 #define USE_SERIAL_2_SW_FLOW 0
496 # if USE_SERIAL_2_FLOW
497 # if USE_SERIAL_2_SW_FLOW
498 ao_serial_set_sw_rts_cts(&ao_stm_usart2,
505 stm_gpio_conf(&stm_gpioa, 0, /* CTS */
506 STM_GPIO_CR_MODE_INPUT,
507 STM_GPIO_CR_CNF_INPUT_FLOATING);
508 stm_gpio_conf(&stm_gpioa, 1, /* RTS */
509 STM_GPIO_CR_MODE_OUTPUT_2MHZ,
510 STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
514 stm_set_afio_mapr(STM_AFIO_MAPR_USART2_REMAP,
515 STM_AFIO_MAPR_USART2_REMAP_PA0_PA1_PA2_PA3_PA4,
516 STM_AFIO_MAPR_USART2_REMAP_MASK);
517 #elif SERIAL_2_PD5_PD6
518 ao_enable_port(&stm_gpiod);
519 stm_gpio_conf(&stm_gpiod, 5,
520 STM_GPIO_CR_MODE_OUTPUT_2MHZ,
521 STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
523 stm_gpio_conf(&stm_gpiod, 6,
524 STM_GPIO_CR_MODE_INPUT,
525 STM_GPIO_CR_CNF_INPUT_FLOATING);
527 # if USE_SERIAL_2_FLOW
528 # if USE_SERIAL_2_SW_FLOW
529 ao_serial_set_sw_rts_cts(&ao_stm_usart2,
536 stm_gpio_conf(&stm_gpiod, 3, /* CTS */
537 STM_GPIO_CR_MODE_INPUT,
538 STM_GPIO_CR_CNF_INPUT_FLOATING);
539 stm_gpio_conf(&stm_gpiod, 4, /* RTS */
540 STM_GPIO_CR_MODE_OUTPUT_2MHZ,
541 STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL);
545 stm_set_afio_mapr(STM_AFIO_MAPR_USART2_REMAP,
546 STM_AFIO_MAPR_USART2_REMAP_PD3_PD4_PD5_PD6_PD7,
547 STM_AFIO_MAPR_USART2_REMAP_MASK);
549 #error "No SERIAL_2 port configuration specified"
552 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART2EN);
554 ao_stm_usart2.reg = &stm_usart2;
555 ao_usart_init(&ao_stm_usart2, USE_SERIAL_2_FLOW && !USE_SERIAL_2_SW_FLOW, SERIAL_2_SPEED);
557 stm_nvic_set_enable(STM_ISR_USART2_POS);
558 stm_nvic_set_priority(STM_ISR_USART2_POS, AO_STM_NVIC_MED_PRIORITY);
559 #if USE_SERIAL_2_STDIN && !DELAY_SERIAL_2_STDIN
560 ao_add_stdio(_ao_serial2_pollchar,
573 #if SERIAL_3_PB10_PB11
574 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
576 stm_afr_set(&stm_gpiob, 10, STM_AFR_AF7);
577 stm_afr_set(&stm_gpiob, 11, STM_AFR_AF7);
579 #if SERIAL_3_PC10_PC11
580 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN);
582 stm_afr_set(&stm_gpioc, 10, STM_AFR_AF7);
583 stm_afr_set(&stm_gpioc, 11, STM_AFR_AF7);
586 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
588 stm_afr_set(&stm_gpiod, 8, STM_AFR_AF7);
589 stm_afr_set(&stm_gpiod, 9, STM_AFR_AF7);
591 #error "No SERIAL_3 port configuration specified"
596 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART3EN);
598 ao_stm_usart3.reg = &stm_usart3;
599 ao_usart_init(&ao_stm_usart3, 0);
601 stm_nvic_set_enable(STM_ISR_USART3_POS);
602 stm_nvic_set_priority(STM_ISR_USART3_POS, AO_STM_NVIC_MED_PRIORITY);
603 #if USE_SERIAL_3_STDIN && !DELAY_SERIAL_3_STDIN
604 ao_add_stdio(_ao_serial3_pollchar,