2 * Copyright © 2019 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.
18 _ao_usart_tx_start(struct ao_samd21_usart *usart)
20 if (!ao_fifo_empty(&usart->tx_fifo)) {
21 #if HAS_SERIAL_SW_FLOW
22 if (usart->gpio_cts && ao_gpio_get(usart->gpio_cts, usart->pin_cts, foo) == 1) {
23 ao_exti_enable(usart->gpio_cts, usart->pin_cts);
27 if (usart->reg->intflag & (1 << SAMD21_SERCOM_INTFLAG_DRE))
29 usart->tx_running = 1;
30 usart->reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_DRE) | (1 << SAMD21_SERCOM_INTFLAG_TXC);
31 usart->reg->data = ao_fifo_remove(&usart->tx_fifo);
32 ao_wakeup(&usart->tx_fifo);
40 _ao_usart_rx(struct ao_samd21_usart *usart, int is_stdin)
42 if (usart->reg->intflag & (1 << SAMD21_SERCOM_INTFLAG_RXC)) {
43 if (!ao_fifo_full(&usart->rx_fifo)) {
44 ao_fifo_insert(&usart->rx_fifo, usart->reg->data);
45 ao_wakeup(&usart->rx_fifo);
47 ao_wakeup(&ao_stdin_ready);
48 #if HAS_SERIAL_SW_FLOW
49 /* If the fifo is nearly full, turn off RTS and wait
50 * for it to drain a bunch
52 if (usart->gpio_rts && ao_fifo_mostly(usart->rx_fifo)) {
53 ao_gpio_set(usart->gpio_rts, usart->pin_rts, usart->pin_rts, 1);
58 usart->reg->intenclr = (1 << SAMD21_SERCOM_INTFLAG_RXC);
64 ao_usart_isr(struct ao_samd21_usart *usart, int is_stdin)
66 _ao_usart_rx(usart, is_stdin);
68 if (!_ao_usart_tx_start(usart))
69 usart->reg->intenclr = (1 << SAMD21_SERCOM_INTFLAG_DRE);
71 if (usart->reg->intflag & (1 << SAMD21_SERCOM_INTFLAG_TXC)) {
72 usart->tx_running = 0;
73 usart->reg->intenclr = (1 << SAMD21_SERCOM_INTFLAG_TXC);
74 if (usart->draining) {
76 ao_wakeup(&usart->tx_fifo);
81 static const uint32_t ao_usart_speeds[] = {
82 [AO_SERIAL_SPEED_4800] = 4800,
83 [AO_SERIAL_SPEED_9600] = 9600,
84 [AO_SERIAL_SPEED_19200] = 19200,
85 [AO_SERIAL_SPEED_57600] = 57600,
86 [AO_SERIAL_SPEED_115200] = 115200,
90 ao_usart_set_speed(struct ao_samd21_usart *usart, uint8_t speed)
92 uint64_t top = (uint64_t) ao_usart_speeds[speed] << (4 + 16);
93 uint16_t baud = 65536 - (top + AO_SYSCLK/2) / AO_SYSCLK;
95 usart->reg->baud = baud;
99 ao_usart_init(struct ao_samd21_usart *usart, int hw_flow, int id)
101 struct samd21_sercom *reg = usart->reg;
105 /* Send a clock along */
106 samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_SERCOM0_CORE + id);
108 samd21_nvic_set_enable(SAMD21_NVIC_ISR_SERCOM0_POS + id);
109 samd21_nvic_set_priority(SAMD21_NVIC_ISR_SERCOM0_POS + id, 4);
112 samd21_pm.apbcmask |= (1 << (SAMD21_PM_APBCMASK_SERCOM0 + id));
115 reg->ctrla = (1 << SAMD21_SERCOM_CTRLA_SWRST);
117 while ((reg->ctrla & (1 << SAMD21_SERCOM_CTRLA_SWRST)) ||
118 (reg->syncbusy & (1 << SAMD21_SERCOM_SYNCBUSY_SWRST)))
121 reg->ctrlb = ((0 << SAMD21_SERCOM_CTRLB_CHSIZE) |
122 (0 << SAMD21_SERCOM_CTRLB_SBMODE) |
123 (0 << SAMD21_SERCOM_CTRLB_COLDEN) |
124 (0 << SAMD21_SERCOM_CTRLB_SFDE) |
125 (0 << SAMD21_SERCOM_CTRLB_ENC) |
126 (0 << SAMD21_SERCOM_CTRLB_PMODE) |
127 (1 << SAMD21_SERCOM_CTRLB_TXEN) |
128 (1 << SAMD21_SERCOM_CTRLB_RXEN) |
129 (3 << SAMD21_SERCOM_CTRLB_FIFOCLR));
131 ao_usart_set_speed(usart, AO_SERIAL_SPEED_9600);
133 /* finish setup and enable the hardware */
134 reg->ctrla = ((0 << SAMD21_SERCOM_CTRLA_SWRST) |
135 (1 << SAMD21_SERCOM_CTRLA_ENABLE) |
136 (1 << SAMD21_SERCOM_CTRLA_MODE) |
137 (1 << SAMD21_SERCOM_CTRLA_RUNSTDBY) |
138 (0 << SAMD21_SERCOM_CTRLA_IBON) |
139 (0 << SAMD21_SERCOM_CTRLA_SAMPR) |
140 (1 << SAMD21_SERCOM_CTRLA_TXPO) | /* pad[2] */
141 (3 << SAMD21_SERCOM_CTRLA_RXPO) | /* pad[3] */
142 (0 << SAMD21_SERCOM_CTRLA_SAMPA) |
143 (0 << SAMD21_SERCOM_CTRLA_FORM) | /* no parity */
144 (0 << SAMD21_SERCOM_CTRLA_CMODE) | /* async */
145 (0 << SAMD21_SERCOM_CTRLA_CPOL) |
146 (1 << SAMD21_SERCOM_CTRLA_DORD)); /* LSB first */
148 /* Enable receive interrupt */
149 reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_RXC);
153 _ao_usart_pollchar(struct ao_samd21_usart *usart)
157 if (ao_fifo_empty(&usart->rx_fifo))
161 u = ao_fifo_remove(&usart->rx_fifo);
162 if ((usart->reg->intenset & (1 << SAMD21_SERCOM_INTFLAG_RXC)) == 0) {
163 if (ao_fifo_barely(&usart->rx_fifo))
164 usart->reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_RXC);
166 #if HAS_SERIAL_SW_FLOW
167 /* If we've cleared RTS, check if there's space now and turn it back on */
168 if (usart->gpio_rts && usart->rts == 0 && ao_fifo_barely(usart->rx_fifo)) {
169 ao_gpio_set(usart->gpio_rts, usart->pin_rts, foo, 0);
179 ao_usart_getchar(struct ao_samd21_usart *usart)
182 ao_arch_block_interrupts();
183 while ((c = _ao_usart_pollchar(usart)) == AO_READ_AGAIN)
184 ao_sleep(&usart->rx_fifo);
185 ao_arch_release_interrupts();
190 ao_usart_putchar(struct ao_samd21_usart *usart, char c)
192 ao_arch_block_interrupts();
193 while (ao_fifo_full(&usart->tx_fifo))
194 ao_sleep(&usart->tx_fifo);
195 ao_fifo_insert(&usart->tx_fifo, c);
196 _ao_usart_tx_start(usart);
197 ao_arch_release_interrupts();
201 ao_usart_drain(struct ao_samd21_usart *usart)
203 ao_arch_block_interrupts();
204 while (!ao_fifo_empty(&usart->tx_fifo) || usart->tx_running) {
206 ao_sleep(&usart->tx_fifo);
208 ao_arch_release_interrupts();
213 struct ao_samd21_usart ao_samd21_usart0;
215 void samd21_sercom0_isr(void) { ao_usart_isr(&ao_samd21_usart0, USE_SERIAL_0_STDIN); }
218 ao_serial0_getchar(void)
220 return ao_usart_getchar(&ao_samd21_usart0);
224 ao_serial0_putchar(char c)
226 ao_usart_putchar(&ao_samd21_usart0, c);
230 _ao_serial0_pollchar(void)
232 return _ao_usart_pollchar(&ao_samd21_usart0);
236 ao_serial0_drain(void)
238 ao_usart_drain(&ao_samd21_usart0);
242 ao_serial0_set_speed(uint8_t speed)
244 ao_usart_drain(&ao_samd21_usart0);
245 ao_usart_set_speed(&ao_samd21_usart0, speed);
247 #endif /* HAS_SERIAL_0 */
254 #if SERIAL_0_PA10_PA11
256 ao_enable_port(&samd21_port_a);
257 samd21_port_pmux_set(&samd21_port_a, 10, SAMD21_PORT_PMUX_FUNC_C);
258 samd21_port_pmux_set(&samd21_port_a, 11, SAMD21_PORT_PMUX_FUNC_C);
260 #error "No SERIAL_0 port configuration specified"
263 ao_samd21_usart0.reg = &samd21_sercom0;
264 ao_usart_init(&ao_samd21_usart0, 0, 0);
266 #if USE_SERIAL_0_STDIN
267 ao_add_stdio(_ao_serial0_pollchar,