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.
24 struct ao_fifo rx_fifo;
25 struct ao_fifo tx_fifo;
26 struct stm_usart *reg;
30 #if HAS_SERIAL_SW_FLOW
31 /* RTS - 0 if we have FIFO space, 1 if not
32 * CTS - 0 if we can send, 0 if not
34 struct stm_gpio *gpio_rts;
35 struct stm_gpio *gpio_cts;
43 _ao_usart_tx_start(struct ao_stm_usart *usart)
45 if (!ao_fifo_empty(usart->tx_fifo)) {
46 #if HAS_SERIAL_SW_FLOW
47 if (usart->gpio_cts && ao_gpio_get(usart->gpio_cts, usart->pin_cts) == 1) {
48 ao_exti_enable(usart->gpio_cts, usart->pin_cts);
52 if (usart->reg->sr & (1 << STM_USART_SR_TXE))
54 usart->tx_running = 1;
55 usart->reg->cr1 |= (1 << STM_USART_CR1_TXEIE) | (1 << STM_USART_CR1_TCIE);
56 ao_fifo_remove(usart->tx_fifo, usart->reg->dr);
57 ao_wakeup(&usart->tx_fifo);
64 #if HAS_SERIAL_SW_FLOW
66 _ao_usart_cts(struct ao_stm_usart *usart)
68 if (_ao_usart_tx_start(usart))
69 ao_exti_disable(usart->gpio_cts, usart->pin_cts);
74 _ao_usart_rx(struct ao_stm_usart *usart, int is_stdin)
76 if (usart->reg->sr & (1 << STM_USART_SR_RXNE)) {
77 if (!ao_fifo_full(usart->rx_fifo)) {
78 ao_fifo_insert(usart->rx_fifo, usart->reg->dr);
79 ao_wakeup(&usart->rx_fifo);
81 ao_wakeup(&ao_stdin_ready);
82 #if HAS_SERIAL_SW_FLOW
83 /* If the fifo is nearly full, turn off RTS and wait
84 * for it to drain a bunch
86 if (usart->gpio_rts && ao_fifo_mostly(usart->rx_fifo)) {
87 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 1);
92 usart->reg->cr1 &= ~(1 << STM_USART_CR1_RXNEIE);
98 ao_usart_isr(struct ao_stm_usart *usart, int is_stdin)
100 _ao_usart_rx(usart, is_stdin);
102 if (!_ao_usart_tx_start(usart))
103 usart->reg->cr1 &= ~(1<< STM_USART_CR1_TXEIE);
105 if (usart->reg->sr & (1 << STM_USART_SR_TC)) {
106 usart->tx_running = 0;
107 usart->reg->cr1 &= ~(1 << STM_USART_CR1_TCIE);
108 if (usart->draining) {
110 ao_wakeup(&usart->tx_fifo);
116 _ao_usart_pollchar(struct ao_stm_usart *usart)
120 if (ao_fifo_empty(usart->rx_fifo))
124 ao_fifo_remove(usart->rx_fifo,u);
125 if ((usart->reg->cr1 & (1 << STM_USART_CR1_RXNEIE)) == 0) {
126 if (ao_fifo_barely(usart->rx_fifo))
127 usart->reg->cr1 |= (1 << STM_USART_CR1_RXNEIE);
129 #if HAS_SERIAL_SW_FLOW
130 /* If we've cleared RTS, check if there's space now and turn it back on */
131 if (usart->gpio_rts && usart->rts == 0 && ao_fifo_barely(usart->rx_fifo)) {
132 ao_gpio_set(usart->gpio_rts, usart->pin_rts, 0);
142 ao_usart_getchar(struct ao_stm_usart *usart)
145 ao_arch_block_interrupts();
146 while ((c = _ao_usart_pollchar(usart)) == AO_READ_AGAIN)
147 ao_sleep(&usart->rx_fifo);
148 ao_arch_release_interrupts();
152 static inline uint8_t
153 _ao_usart_sleep_for(struct ao_stm_usart *usart, uint16_t timeout)
155 return ao_sleep_for(&usart->rx_fifo, timeout);
159 ao_usart_putchar(struct ao_stm_usart *usart, char c)
161 ao_arch_block_interrupts();
162 while (ao_fifo_full(usart->tx_fifo))
163 ao_sleep(&usart->tx_fifo);
164 ao_fifo_insert(usart->tx_fifo, c);
165 _ao_usart_tx_start(usart);
166 ao_arch_release_interrupts();
170 ao_usart_drain(struct ao_stm_usart *usart)
172 ao_arch_block_interrupts();
173 while (!ao_fifo_empty(usart->tx_fifo) || usart->tx_running) {
175 ao_sleep(&usart->tx_fifo);
177 ao_arch_release_interrupts();
181 ao_usart_set_speed(struct ao_stm_usart *usart, uint32_t speed)
185 usart->reg->brr = usart->clk / speed;
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_ONEBIT) |
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, 9600);
238 #if HAS_SERIAL_HW_FLOW
240 ao_usart_set_flow(struct ao_stm_usart *usart)
247 struct ao_stm_usart ao_stm_usart6;
249 void stm_usart6_isr(void) { ao_usart_isr(&ao_stm_usart6, USE_SERIAL_6_STDIN); }
252 ao_serial6_getchar(void)
254 return ao_usart_getchar(&ao_stm_usart6);
258 ao_serial6_putchar(char c)
260 ao_usart_putchar(&ao_stm_usart6, c);
264 _ao_serial6_pollchar(void)
266 return _ao_usart_pollchar(&ao_stm_usart6);
270 _ao_serial6_sleep_for(uint16_t timeout)
272 return _ao_usart_sleep_for(&ao_stm_usart6, timeout);
276 ao_serial6_set_speed(uint32_t speed)
278 ao_usart_drain(&ao_stm_usart6);
279 ao_usart_set_speed(&ao_stm_usart6, speed);
283 ao_serial6_drain(void)
285 ao_usart_drain(&ao_stm_usart6);
287 #endif /* HAS_SERIAL_6 */
289 #if HAS_SERIAL_SW_FLOW
291 ao_serial_set_sw_rts_cts(struct ao_stm_usart *usart,
293 struct stm_gpio *port_rts,
295 struct stm_gpio *port_cts,
298 /* Pull RTS low to note that there's space in the FIFO */
299 ao_enable_output(port_rts, pin_rts, 0);
300 usart->gpio_rts = port_rts;
301 usart->pin_rts = pin_rts;
304 ao_exti_setup(port_cts, pin_cts, AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_MED, isr);
305 usart->gpio_cts = port_cts;
306 usart->pin_cts = pin_cts;
314 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_USART6EN);
316 ao_stm_usart6.reg = &stm_usart6;
317 ao_stm_usart6.clk = AO_P2CLK;
319 ao_enable_port(SERIAL_6_RX_PORT);
320 ao_enable_port(SERIAL_6_TX_PORT);
322 stm_afr_set(SERIAL_6_RX_PORT, SERIAL_6_RX_PIN, STM_AFR_AF8);
323 stm_afr_set(SERIAL_6_TX_PORT, SERIAL_6_TX_PIN, STM_AFR_AF8);
325 stm_nvic_set_enable(STM_ISR_USART6_POS);
326 stm_nvic_set_priority(STM_ISR_USART6_POS, AO_STM_NVIC_MED_PRIORITY);
328 _ao_usart_init(&ao_stm_usart6, USE_SERIAL_6_FLOW && !USE_SERIAL_6_SW_FLOW);
330 # if USE_SERIAL_6_FLOW
331 # if USE_SERIAL_6_SW_FLOW
332 ao_serial_set_sw_rts_cts(&ao_stm_usart6,
339 stm_afr_set(SERIAL_6_PORT_RTS, SERIAL_6_PIN_RTS, STM_AFR_AF8);
340 stm_afr_set(SERIAL_6_PORT_CTS, SERIAL_6_PIN_CTS, STM_AFR_AF8);
344 #if USE_SERIAL_6_STDIN && !DELAY_SERIAL_6_STDIN
345 ao_add_stdio(_ao_serial6_pollchar,