2 * Copyright © 2018 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.
23 _ao_usart_tx_start(struct ao_stm_usart *usart)
25 if (!ao_fifo_empty(usart->tx_fifo)) {
26 #if HAS_SERIAL_SW_FLOW
27 if (usart->gpio_cts && ao_gpio_get(usart->gpio_cts, usart->pin_cts) == 1) {
28 ao_exti_enable(usart->gpio_cts, usart->pin_cts);
32 if (usart->reg->sr & (1 << STM_USART_SR_TXE))
34 usart->tx_running = 1;
35 usart->reg->cr1 |= (1 << STM_USART_CR1_TXEIE) | (1 << STM_USART_CR1_TCIE);
36 ao_fifo_remove(usart->tx_fifo, usart->reg->dr);
37 ao_wakeup(&usart->tx_fifo);
44 #if HAS_SERIAL_SW_FLOW
46 _ao_usart_cts(struct ao_stm_usart *usart)
48 if (_ao_usart_tx_start(usart))
49 ao_exti_disable(usart->gpio_cts, usart->pin_cts);
54 _ao_usart_rx(struct ao_stm_usart *usart, int is_stdin)
56 if (usart->reg->sr & (1 << STM_USART_SR_RXNE)) {
57 if (!ao_fifo_full(usart->rx_fifo)) {
58 ao_fifo_insert(usart->rx_fifo, usart->reg->dr);
59 ao_wakeup(&usart->rx_fifo);
61 ao_wakeup(&ao_stdin_ready);
62 #if HAS_SERIAL_SW_FLOW
63 /* If the fifo is nearly full, turn off RTS and wait
64 * for it to drain a bunch
66 if (usart->gpio_rts && ao_fifo_mostly(usart->rx_fifo)) {
67 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 1);
72 usart->reg->cr1 &= ~(1 << STM_USART_CR1_RXNEIE);
78 ao_usart_isr(struct ao_stm_usart *usart, int is_stdin)
80 _ao_usart_rx(usart, is_stdin);
82 if (!_ao_usart_tx_start(usart))
83 usart->reg->cr1 &= ~(1<< STM_USART_CR1_TXEIE);
85 if (usart->reg->sr & (1 << STM_USART_SR_TC)) {
86 usart->tx_running = 0;
87 usart->reg->cr1 &= ~(1 << STM_USART_CR1_TCIE);
88 if (usart->draining) {
90 ao_wakeup(&usart->tx_fifo);
96 _ao_usart_pollchar(struct ao_stm_usart *usart)
100 if (ao_fifo_empty(usart->rx_fifo))
104 ao_fifo_remove(usart->rx_fifo,u);
105 if ((usart->reg->cr1 & (1 << STM_USART_CR1_RXNEIE)) == 0) {
106 if (ao_fifo_barely(usart->rx_fifo))
107 usart->reg->cr1 |= (1 << STM_USART_CR1_RXNEIE);
109 #if HAS_SERIAL_SW_FLOW
110 /* If we've cleared RTS, check if there's space now and turn it back on */
111 if (usart->gpio_rts && usart->rts == 0 && ao_fifo_barely(usart->rx_fifo)) {
112 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 0);
122 ao_usart_getchar(struct ao_stm_usart *usart)
125 ao_arch_block_interrupts();
126 while ((c = _ao_usart_pollchar(usart)) == AO_READ_AGAIN)
127 ao_sleep(&usart->rx_fifo);
128 ao_arch_release_interrupts();
132 static inline uint8_t
133 _ao_usart_sleep_for(struct ao_stm_usart *usart, uint16_t timeout)
135 return ao_sleep_for(&usart->rx_fifo, timeout);
139 ao_usart_putchar(struct ao_stm_usart *usart, char c)
141 ao_arch_block_interrupts();
142 while (ao_fifo_full(usart->tx_fifo))
143 ao_sleep(&usart->tx_fifo);
144 ao_fifo_insert(usart->tx_fifo, c);
145 _ao_usart_tx_start(usart);
146 ao_arch_release_interrupts();
150 ao_usart_drain(struct ao_stm_usart *usart)
152 ao_arch_block_interrupts();
153 while (!ao_fifo_empty(usart->tx_fifo) || usart->tx_running) {
155 ao_sleep(&usart->tx_fifo);
157 ao_arch_release_interrupts();
160 static const struct {
162 } ao_usart_speeds[] = {
163 [AO_SERIAL_SPEED_4800] = {
166 [AO_SERIAL_SPEED_9600] = {
169 [AO_SERIAL_SPEED_19200] = {
172 [AO_SERIAL_SPEED_57600] = {
175 [AO_SERIAL_SPEED_115200] = {
181 ao_usart_set_speed(struct ao_stm_usart *usart, uint8_t speed)
183 if (speed > AO_SERIAL_SPEED_115200)
185 usart->reg->brr = ao_usart_speeds[speed].brr;
189 ao_usart_init(struct ao_stm_usart *usart, int hw_flow)
191 usart->reg->cr1 = ((0 << STM_USART_CR1_OVER8) |
192 (1 << STM_USART_CR1_UE) |
193 (0 << STM_USART_CR1_M) |
194 (0 << STM_USART_CR1_WAKE) |
195 (0 << STM_USART_CR1_PCE) |
196 (0 << STM_USART_CR1_PS) |
197 (0 << STM_USART_CR1_PEIE) |
198 (0 << STM_USART_CR1_TXEIE) |
199 (0 << STM_USART_CR1_TCIE) |
200 (1 << STM_USART_CR1_RXNEIE) |
201 (0 << STM_USART_CR1_IDLEIE) |
202 (1 << STM_USART_CR1_TE) |
203 (1 << STM_USART_CR1_RE) |
204 (0 << STM_USART_CR1_RWU) |
205 (0 << STM_USART_CR1_SBK));
207 usart->reg->cr2 = ((0 << STM_USART_CR2_LINEN) |
208 (STM_USART_CR2_STOP_1 << STM_USART_CR2_STOP) |
209 (0 << STM_USART_CR2_CLKEN) |
210 (0 << STM_USART_CR2_CPOL) |
211 (0 << STM_USART_CR2_CPHA) |
212 (0 << STM_USART_CR2_LBCL) |
213 (0 << STM_USART_CR2_LBDIE) |
214 (0 << STM_USART_CR2_LBDL) |
215 (0 << STM_USART_CR2_ADD));
217 usart->reg->cr3 = ((0 << STM_USART_CR3_ONEBITE) |
218 (0 << STM_USART_CR3_CTSIE) |
219 (0 << STM_USART_CR3_CTSE) |
220 (0 << STM_USART_CR3_RTSE) |
221 (0 << STM_USART_CR3_DMAT) |
222 (0 << STM_USART_CR3_DMAR) |
223 (0 << STM_USART_CR3_SCEN) |
224 (0 << STM_USART_CR3_NACK) |
225 (0 << STM_USART_CR3_HDSEL) |
226 (0 << STM_USART_CR3_IRLP) |
227 (0 << STM_USART_CR3_IREN) |
228 (0 << STM_USART_CR3_EIE));
231 usart->reg->cr3 |= ((1 << STM_USART_CR3_CTSE) |
232 (1 << STM_USART_CR3_RTSE));
234 /* Pick a 9600 baud rate */
235 ao_usart_set_speed(usart, AO_SERIAL_SPEED_9600);
238 #if HAS_SERIAL_HW_FLOW
240 ao_usart_set_flow(struct ao_stm_usart *usart)
247 struct ao_stm_usart ao_stm_usart1;
249 void stm_usart1_isr(void) { ao_usart_isr(&ao_stm_usart1, USE_SERIAL_1_STDIN); }
252 ao_serial1_getchar(void)
254 return ao_usart_getchar(&ao_stm_usart1);
258 ao_serial1_putchar(char c)
260 ao_usart_putchar(&ao_stm_usart1, c);
264 _ao_serial1_pollchar(void)
266 return _ao_usart_pollchar(&ao_stm_usart1);
270 _ao_serial1_sleep_for(uint16_t timeout)
272 return _ao_usart_sleep_for(&ao_stm_usart1, timeout);
276 ao_serial1_drain(void)
278 ao_usart_drain(&ao_stm_usart1);
282 ao_serial1_set_speed(uint8_t speed)
284 ao_usart_drain(&ao_stm_usart1);
285 ao_usart_set_speed(&ao_stm_usart1, speed);
287 #endif /* HAS_SERIAL_1 */
291 struct ao_stm_usart ao_stm_usart2;
293 void stm_usart2_isr(void) { ao_usart_isr(&ao_stm_usart2, USE_SERIAL_2_STDIN); }
296 ao_serial2_getchar(void)
298 return ao_usart_getchar(&ao_stm_usart2);
302 ao_serial2_putchar(char c)
304 ao_usart_putchar(&ao_stm_usart2, c);
308 _ao_serial2_pollchar(void)
310 return _ao_usart_pollchar(&ao_stm_usart2);
314 _ao_serial2_sleep_for(uint16_t timeout)
316 return _ao_usart_sleep_for(&ao_stm_usart2, timeout);
320 ao_serial2_drain(void)
322 ao_usart_drain(&ao_stm_usart2);
326 ao_serial2_set_speed(uint8_t speed)
328 ao_usart_drain(&ao_stm_usart2);
329 ao_usart_set_speed(&ao_stm_usart2, speed);
332 #if HAS_SERIAL_SW_FLOW
336 _ao_usart_cts(&ao_stm_usart2);
340 #endif /* HAS_SERIAL_2 */
344 struct ao_stm_usart ao_stm_usart3;
346 void stm_usart3_isr(void) { ao_usart_isr(&ao_stm_usart3, USE_SERIAL_3_STDIN); }
349 ao_serial3_getchar(void)
351 return ao_usart_getchar(&ao_stm_usart3);
355 ao_serial3_putchar(char c)
357 ao_usart_putchar(&ao_stm_usart3, c);
361 _ao_serial3_pollchar(void)
363 return _ao_usart_pollchar(&ao_stm_usart3);
367 _ao_serial3_sleep_for(uint16_t timeout)
369 return _ao_usart_sleep_for(&ao_stm_usart3, timeout);
373 ao_serial3_set_speed(uint8_t speed)
375 ao_usart_drain(&ao_stm_usart3);
376 ao_usart_set_speed(&ao_stm_usart3, speed);
380 ao_serial3_drain(void)
382 ao_usart_drain(&ao_stm_usart3);
384 #endif /* HAS_SERIAL_3 */
386 #if HAS_SERIAL_SW_FLOW
388 ao_serial_set_sw_rts_cts(struct ao_stm_usart *usart,
390 struct stm_gpio *port_rts,
392 struct stm_gpio *port_cts,
395 /* Pull RTS low to note that there's space in the FIFO
397 ao_enable_output(port_rts, pin_rts, 0);
398 usart->gpio_rts = port_rts;
399 usart->pin_rts = pin_rts;
402 ao_exti_setup(port_cts, pin_cts, AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_MED, isr);
403 usart->gpio_cts = port_cts;
404 usart->pin_cts = pin_cts;
418 #if SERIAL_1_PA9_PA10
419 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
421 stm_afr_set(&stm_gpioa, 9, STM_AFR_AF7);
422 stm_afr_set(&stm_gpioa, 10, STM_AFR_AF7);
425 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
427 stm_afr_set(&stm_gpiob, 6, STM_AFR_AF7);
428 stm_afr_set(&stm_gpiob, 7, STM_AFR_AF7);
430 #error "No SERIAL_1 port configuration specified"
434 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_USART1EN);
436 ao_stm_usart1.reg = &stm_usart1;
437 ao_usart_init(&ao_stm_usart1, 0);
439 stm_nvic_set_enable(STM_ISR_USART1_POS);
440 stm_nvic_set_priority(STM_ISR_USART1_POS, AO_STM_NVIC_MED_PRIORITY);
441 #if USE_SERIAL_1_STDIN && !DELAY_SERIAL_1_STDIN
442 ao_add_stdio(_ao_serial1_pollchar,
456 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
458 stm_afr_set(&stm_gpioa, 2, STM_AFR_AF7);
459 stm_afr_set(&stm_gpioa, 3, STM_AFR_AF7);
460 # if USE_SERIAL_2_FLOW
461 # if USE_SERIAL_2_SW_FLOW
462 ao_serial_set_sw_rts_cts(&ao_stm_usart2,
469 stm_afr_set(&stm_gpioa, 0, STM_AFR_AF7);
470 stm_afr_set(&stm_gpioa, 1, STM_AFR_AF7);
475 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
477 stm_afr_set(&stm_gpiod, 5, STM_AFR_AF7);
478 stm_afr_set(&stm_gpiod, 6, STM_AFR_AF7);
479 #if USE_SERIAL_2_FLOW
480 #error "Don't know how to set flowcontrol for serial 2 on PD"
483 #error "No SERIAL_2 port configuration specified"
487 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART2EN);
489 ao_stm_usart2.reg = &stm_usart2;
490 ao_usart_init(&ao_stm_usart2, USE_SERIAL_2_FLOW && !USE_SERIAL_2_SW_FLOW);
492 stm_nvic_set_enable(STM_ISR_USART2_POS);
493 stm_nvic_set_priority(STM_ISR_USART2_POS, AO_STM_NVIC_MED_PRIORITY);
494 #if USE_SERIAL_2_STDIN && !DELAY_SERIAL_2_STDIN
495 ao_add_stdio(_ao_serial2_pollchar,
508 #if SERIAL_3_PB10_PB11
509 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
511 stm_afr_set(&stm_gpiob, 10, STM_AFR_AF7);
512 stm_afr_set(&stm_gpiob, 11, STM_AFR_AF7);
514 #if SERIAL_3_PC10_PC11
515 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN);
517 stm_afr_set(&stm_gpioc, 10, STM_AFR_AF7);
518 stm_afr_set(&stm_gpioc, 11, STM_AFR_AF7);
521 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIODEN);
523 stm_afr_set(&stm_gpiod, 8, STM_AFR_AF7);
524 stm_afr_set(&stm_gpiod, 9, STM_AFR_AF7);
526 #error "No SERIAL_3 port configuration specified"
531 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART3EN);
533 ao_stm_usart3.reg = &stm_usart3;
534 ao_usart_init(&ao_stm_usart3, 0);
536 stm_nvic_set_enable(STM_ISR_USART3_POS);
537 stm_nvic_set_priority(STM_ISR_USART3_POS, AO_STM_NVIC_MED_PRIORITY);
538 #if USE_SERIAL_3_STDIN && !DELAY_SERIAL_3_STDIN
539 ao_add_stdio(_ao_serial3_pollchar,