2 * Copyright © 2020 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.
19 _ao_usart_tx_start(struct ao_stm_usart *usart)
21 if (!ao_fifo_empty(usart->tx_fifo)) {
22 #if HAS_SERIAL_SW_FLOW
23 if (usart->gpio_cts && ao_gpio_get(usart->gpio_cts, usart->pin_cts) == 1) {
24 ao_exti_enable(usart->gpio_cts, usart->pin_cts);
28 if (usart->reg->isr & (1 << STM_USART_ISR_TXE))
30 usart->tx_running = 1;
31 usart->reg->cr1 |= (1 << STM_USART_CR1_TXEIE) | (1 << STM_USART_CR1_TCIE);
32 ao_fifo_remove(usart->tx_fifo, usart->reg->tdr);
33 ao_wakeup(&usart->tx_fifo);
40 #if HAS_SERIAL_SW_FLOW
42 _ao_usart_cts(struct ao_stm_usart *usart)
44 if (_ao_usart_tx_start(usart))
45 ao_exti_disable(usart->gpio_cts, usart->pin_cts);
50 _ao_usart_rx(struct ao_stm_usart *usart, int is_stdin)
52 if (usart->reg->isr & (1 << STM_USART_ISR_RXNE)) {
53 usart->reg->icr = (1 << STM_USART_ICR_ORECF);
54 if (!ao_fifo_full(usart->rx_fifo)) {
55 ao_fifo_insert(usart->rx_fifo, usart->reg->rdr);
56 ao_wakeup(&usart->rx_fifo);
58 ao_wakeup(&ao_stdin_ready);
59 #if HAS_SERIAL_SW_FLOW
60 /* If the fifo is nearly full, turn off RTS and wait
61 * for it to drain a bunch
63 if (usart->gpio_rts && ao_fifo_mostly(usart->rx_fifo)) {
64 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 1);
69 usart->reg->cr1 &= ~(1UL << STM_USART_CR1_RXNEIE);
75 ao_usart_isr(struct ao_stm_usart *usart, int is_stdin)
77 _ao_usart_rx(usart, is_stdin);
79 if (!_ao_usart_tx_start(usart))
80 usart->reg->cr1 &= ~(1UL << STM_USART_CR1_TXEIE);
82 if (usart->reg->isr & (1 << STM_USART_ISR_TC)) {
83 usart->tx_running = 0;
84 usart->reg->cr1 &= ~(1UL << STM_USART_CR1_TCIE);
85 if (usart->draining) {
87 ao_wakeup(&usart->tx_fifo);
93 _ao_usart_pollchar(struct ao_stm_usart *usart)
97 if (ao_fifo_empty(usart->rx_fifo))
101 ao_fifo_remove(usart->rx_fifo,u);
102 if ((usart->reg->cr1 & (1 << STM_USART_CR1_RXNEIE)) == 0) {
103 if (ao_fifo_barely(usart->rx_fifo))
104 usart->reg->cr1 |= (1 << STM_USART_CR1_RXNEIE);
106 #if HAS_SERIAL_SW_FLOW
107 /* If we've cleared RTS, check if there's space now and turn it back on */
108 if (usart->gpio_rts && usart->rts == 0 && ao_fifo_barely(usart->rx_fifo)) {
109 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 0);
119 ao_usart_getchar(struct ao_stm_usart *usart)
122 ao_arch_block_interrupts();
123 while ((c = _ao_usart_pollchar(usart)) == AO_READ_AGAIN)
124 ao_sleep(&usart->rx_fifo);
125 ao_arch_release_interrupts();
130 static inline uint8_t
131 _ao_usart_sleep_for(struct ao_stm_usart *usart, uint16_t timeout)
133 return ao_sleep_for(&usart->rx_fifo, timeout);
138 ao_usart_putchar(struct ao_stm_usart *usart, char c)
140 ao_arch_block_interrupts();
141 while (ao_fifo_full(usart->tx_fifo))
142 ao_sleep(&usart->tx_fifo);
143 ao_fifo_insert(usart->tx_fifo, c);
144 _ao_usart_tx_start(usart);
145 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();
161 const uint32_t ao_usart_speeds[] = {
162 [AO_SERIAL_SPEED_4800] = 4800,
163 [AO_SERIAL_SPEED_9600] = 9600,
164 [AO_SERIAL_SPEED_19200] = 19200,
165 [AO_SERIAL_SPEED_57600] = 57600,
166 [AO_SERIAL_SPEED_115200] = 115200,
170 ao_usart_set_speed(struct ao_stm_usart *usart, uint8_t speed)
172 if (speed > AO_SERIAL_SPEED_115200)
174 usart->reg->brr = AO_PCLK2 / ao_usart_speeds[speed];
178 ao_usart_init(struct ao_stm_usart *usart, int hw_flow)
180 usart->reg->cr1 = ((0 << STM_USART_CR1_M1) |
181 (0 << STM_USART_CR1_EOBIE) |
182 (0 << STM_USART_CR1_RTOIE) |
183 (0 << STM_USART_CR1_DEAT) |
184 (0 << STM_USART_CR1_DEDT) |
185 (0 << STM_USART_CR1_OVER8) |
186 (0 << STM_USART_CR1_CMIE) |
187 (0 << STM_USART_CR1_MME) |
188 (0 << STM_USART_CR1_M0) |
189 (0 << STM_USART_CR1_WAKE) |
190 (0 << STM_USART_CR1_PCE) |
191 (0 << STM_USART_CR1_PS) |
192 (0 << STM_USART_CR1_PEIE) |
193 (0 << STM_USART_CR1_TXEIE) |
194 (0 << STM_USART_CR1_TCIE) |
195 (1 << STM_USART_CR1_RXNEIE) |
196 (0 << STM_USART_CR1_IDLEIE) |
197 (1 << STM_USART_CR1_TE) |
198 (1 << STM_USART_CR1_RE) |
199 (0 << STM_USART_CR1_UESM) |
200 (0 << STM_USART_CR1_UE));
202 usart->reg->cr2 = ((0 << STM_USART_CR2_ADD) |
203 (0 << STM_USART_CR2_RTOEN) |
204 (0 << STM_USART_CR2_ABRMOD) |
205 (0 << STM_USART_CR2_ABREN) |
206 (0 << STM_USART_CR2_MSBFIRST) |
207 (0 << STM_USART_CR2_DATAINV) |
208 (0 << STM_USART_CR2_TXINV) |
209 (0 << STM_USART_CR2_RXINV) |
210 (0 << STM_USART_CR2_SWAP) |
211 (0 << STM_USART_CR2_LINEN) |
212 (0 << STM_USART_CR2_STOP) |
213 (0 << STM_USART_CR2_CLKEN) |
214 (0 << STM_USART_CR2_CPOL) |
215 (0 << STM_USART_CR2_CHPA) |
216 (0 << STM_USART_CR2_LBCL) |
217 (0 << STM_USART_CR2_LBDIE) |
218 (0 << STM_USART_CR2_LBDL) |
219 (0 << STM_USART_CR2_ADDM7));
221 uint32_t cr3 = ((0 << STM_USART_CR3_WUFIE) |
222 (0 << STM_USART_CR3_WUS) |
223 (0 << STM_USART_CR3_SCARCNT) |
224 (0 << STM_USART_CR3_DEP) |
225 (0 << STM_USART_CR3_DEM) |
226 (0 << STM_USART_CR3_DDRE) |
227 (0 << STM_USART_CR3_OVRDIS) |
228 (0 << STM_USART_CR3_ONEBIT) |
229 (0 << STM_USART_CR3_CTIIE) |
230 (0 << STM_USART_CR3_CTSE) |
231 (0 << STM_USART_CR3_RTSE) |
232 (0 << STM_USART_CR3_DMAT) |
233 (0 << STM_USART_CR3_DMAR) |
234 (0 << STM_USART_CR3_SCEN) |
235 (0 << STM_USART_CR3_NACK) |
236 (0 << STM_USART_CR3_HDSEL) |
237 (0 << STM_USART_CR3_IRLP) |
238 (0 << STM_USART_CR3_IREN) |
239 (0 << STM_USART_CR3_EIE));
242 cr3 |= ((1 << STM_USART_CR3_CTSE) |
243 (1 << STM_USART_CR3_RTSE));
245 usart->reg->cr3 = cr3;
247 /* Pick a 9600 baud rate */
248 ao_usart_set_speed(usart, AO_SERIAL_SPEED_9600);
250 /* Enable the usart */
251 usart->reg->cr1 |= (1 << STM_USART_CR1_UE);
256 struct ao_stm_usart ao_stm_usart1;
258 void stm_usart1_isr(void) { ao_usart_isr(&ao_stm_usart1, USE_SERIAL_1_STDIN); }
261 ao_serial1_getchar(void)
263 return ao_usart_getchar(&ao_stm_usart1);
267 ao_serial1_putchar(char c)
269 ao_usart_putchar(&ao_stm_usart1, c);
273 _ao_serial1_pollchar(void)
275 return _ao_usart_pollchar(&ao_stm_usart1);
280 _ao_serial1_sleep_for(uint16_t timeout)
282 return _ao_usart_sleep_for(&ao_stm_usart1, timeout);
288 ao_serial1_drain(void)
290 ao_usart_drain(&ao_stm_usart1);
294 ao_serial1_set_speed(uint8_t speed)
296 ao_usart_drain(&ao_stm_usart1);
297 ao_usart_set_speed(&ao_stm_usart1, speed);
300 #endif /* HAS_SERIAL_1 */
304 struct ao_stm_usart ao_stm_usart2;
306 void stm_usart2_isr(void) { ao_usart_isr(&ao_stm_usart2, USE_SERIAL_2_STDIN); }
309 ao_serial2_getchar(void)
311 return ao_usart_getchar(&ao_stm_usart2);
315 ao_serial2_putchar(char c)
317 ao_usart_putchar(&ao_stm_usart2, c);
321 _ao_serial2_pollchar(void)
323 return _ao_usart_pollchar(&ao_stm_usart2);
328 _ao_serial2_sleep_for(uint16_t timeout)
330 return _ao_usart_sleep_for(&ao_stm_usart2, timeout);
334 ao_serial2_drain(void)
336 ao_usart_drain(&ao_stm_usart2);
340 ao_serial2_set_speed(uint8_t speed)
342 ao_usart_drain(&ao_stm_usart2);
343 ao_usart_set_speed(&ao_stm_usart2, speed);
347 #if USE_SERIAL_2_FLOW && USE_SERIAL_2_SW_FLOW
351 _ao_usart_cts(&ao_stm_usart2);
355 #endif /* HAS_SERIAL_2 */
357 #if HAS_SERIAL_SW_FLOW
359 ao_serial_set_sw_rts_cts(struct ao_stm_usart *usart,
361 struct stm_gpio *port_rts,
363 struct stm_gpio *port_cts,
366 /* Pull RTS low to note that there's space in the FIFO
368 ao_enable_output(port_rts, pin_rts, 0);
369 usart->gpio_rts = port_rts;
370 usart->pin_rts = pin_rts;
373 ao_exti_setup(port_cts, pin_cts, AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_MED, isr);
374 usart->gpio_cts = port_cts;
375 usart->pin_cts = pin_cts;
381 ao_serial_shutdown(void)
383 # if SERIAL_2_PA2_PA3
384 stm_moder_set(&stm_gpioa, 2, STM_MODER_INPUT);
385 stm_moder_set(&stm_gpioa, 3, STM_MODER_INPUT);
386 # elif SERIAL_2_PA9_PA10
387 stm_moder_set(&stm_gpioa, 9, STM_MODER_INPUT);
388 stm_moder_set(&stm_gpioa, 10, STM_MODER_INPUT);
389 # elif SERIAL_2_PA14_PA15
390 stm_moder_set(&stm_gpioa, 14, STM_MODER_INPUT);
391 stm_moder_set(&stm_gpioa, 15, STM_MODER_INPUT);
392 # elif SERIAL_2_PB6_PB7
393 stm_moder_set(&stm_gpiob, 6, STM_MODER_INPUT);
394 stm_moder_set(&stm_gpiob, 7, STM_MODER_INPUT);
397 stm_rcc.apb2enr &= ~(1 << STM_RCC_APB2ENR_USART1EN);
400 stm_nvic_set_disable(STM_ISR_USART2_POS);
401 stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_USART2EN);
421 # if SERIAL_2_PA2_PA3
422 ao_enable_port(&stm_gpioa);
423 stm_afr_set(&stm_gpioa, 2, STM_AFR_AF4);
424 stm_afr_set(&stm_gpioa, 3, STM_AFR_AF4);
425 # elif SERIAL_2_PA9_PA10
426 ao_enable_port(&stm_gpioa);
427 stm_afr_set(&stm_gpioa, 9, STM_AFR_AF4);
428 stm_afr_set(&stm_gpioa, 10, STM_AFR_AF4);
429 # elif SERIAL_2_PA14_PA15
430 ao_enable_port(&stm_gpioa);
431 stm_afr_set(&stm_gpioa, 14, STM_AFR_AF4);
432 stm_afr_set(&stm_gpioa, 15, STM_AFR_AF4);
433 # elif SERIAL_2_PB6_PB7
434 ao_enable_port(&stm_gpiob);
435 stm_afr_set(&stm_gpiob, 6, STM_AFR_AF0);
436 stm_afr_set(&stm_gpiob, 7, STM_AFR_AF0);
438 # error "No SERIAL_2 port configuration specified"
441 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USART2EN);
443 ao_stm_usart2.reg = &stm_usart2;
444 ao_usart_init(&ao_stm_usart2, USE_SERIAL_2_FLOW && !USE_SERIAL_2_SW_FLOW);
446 stm_nvic_set_enable(STM_ISR_USART2_POS);
447 stm_nvic_set_priority(STM_ISR_USART2_POS, 4);
448 # if USE_SERIAL_2_STDIN && !DELAY_SERIAL_2_STDIN
449 ao_add_stdio(_ao_serial2_pollchar,