samd21: Get serial driver building
[fw/altos] / src / samd21 / ao_serial_samd21.c
1 /*
2  * Copyright © 2019 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #include <ao.h>
16
17 static int
18 _ao_usart_tx_start(struct ao_samd21_usart *usart)
19 {
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);
24                         return 0;
25                 }
26 #endif
27                 if (usart->reg->intflag & (1 << SAMD21_SERCOM_INTFLAG_DRE))
28                 {
29                         usart->tx_running = 1;
30                         usart->reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_DRE) | (1 << SAMD21_SERCOM_INTFLAG_TXC);
31                         ao_fifo_remove(usart->tx_fifo, usart->reg->data);
32                         ao_wakeup(&usart->tx_fifo);
33                         return 1;
34                 }
35         }
36         return 0;
37 }
38
39 static void
40 _ao_usart_rx(struct ao_samd21_usart *usart, int is_stdin)
41 {
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);
46                         if (is_stdin)
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
51                          */
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);
54                                 usart->rts = 0;
55                         }
56 #endif
57                 } else {
58                         usart->reg->intenclr = (1 << SAMD21_SERCOM_INTFLAG_RXC);
59                 }
60         }
61 }
62
63 static void
64 ao_usart_isr(struct ao_samd21_usart *usart, int is_stdin)
65 {
66         _ao_usart_rx(usart, is_stdin);
67
68         if (!_ao_usart_tx_start(usart))
69                 usart->reg->intenclr = (1 << SAMD21_SERCOM_INTFLAG_DRE);
70
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) {
75                         usart->draining = 0;
76                         ao_wakeup(&usart->tx_fifo);
77                 }
78         }
79 }
80
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,
87 };
88
89 static void
90 ao_usart_set_speed(struct ao_samd21_usart *usart, uint8_t speed)
91 {
92         uint64_t        top = (uint64_t) ao_usart_speeds[speed] << (4 + 16);
93         uint16_t        baud = (uint16_t) (65536 - (top + AO_SYSCLK/2) / AO_SYSCLK);
94
95         usart->reg->baud = baud;
96 }
97
98 static void
99 ao_usart_init(struct ao_samd21_usart *usart, bool hw_flow, uint8_t id)
100 {
101         struct samd21_sercom *reg = usart->reg;
102
103         (void) hw_flow;
104
105         /* Send a clock along */
106         samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_SERCOM0_CORE + id);
107
108         samd21_nvic_set_enable(SAMD21_NVIC_ISR_SERCOM0_POS + id);
109         samd21_nvic_set_priority(SAMD21_NVIC_ISR_SERCOM0_POS + id, 4);
110
111         /* enable */
112         samd21_pm.apbcmask |= (1 << (SAMD21_PM_APBCMASK_SERCOM0 + id));
113
114         /* Reset */
115         reg->ctrla = (1 << SAMD21_SERCOM_CTRLA_SWRST);
116
117         while ((reg->ctrla & (1 << SAMD21_SERCOM_CTRLA_SWRST)) ||
118                (reg->syncbusy & (1 << SAMD21_SERCOM_SYNCBUSY_SWRST)))
119                 ;
120
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));
130
131         ao_usart_set_speed(usart, AO_SERIAL_SPEED_9600);
132
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 */
147
148         /* Enable receive interrupt */
149         reg->intenset = (1 << SAMD21_SERCOM_INTFLAG_RXC);
150 }
151
152 static int
153 _ao_usart_pollchar(struct ao_samd21_usart *usart)
154 {
155         int     c;
156
157         if (ao_fifo_empty(usart->rx_fifo))
158                 c = AO_READ_AGAIN;
159         else {
160                 uint8_t u;
161                 ao_fifo_remove(usart->rx_fifo, u);
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);
165                 }
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);
170                         usart->rts = 1;
171                 }
172 #endif
173                 c = u;
174         }
175         return c;
176 }
177
178 static char
179 ao_usart_getchar(struct ao_samd21_usart *usart)
180 {
181         int c;
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();
186         return (char) c;
187 }
188
189 static void
190 ao_usart_putchar(struct ao_samd21_usart *usart, char c)
191 {
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();
198 }
199
200 static void
201 ao_usart_drain(struct ao_samd21_usart *usart)
202 {
203         ao_arch_block_interrupts();
204         while (!ao_fifo_empty(usart->tx_fifo) || usart->tx_running) {
205                 usart->draining = 1;
206                 ao_sleep(&usart->tx_fifo);
207         }
208         ao_arch_release_interrupts();
209 }
210
211 #if HAS_SERIAL_0
212
213 struct ao_samd21_usart ao_samd21_usart0;
214
215 void samd21_sercom0_isr(void) { ao_usart_isr(&ao_samd21_usart0, USE_SERIAL_0_STDIN); }
216
217 char
218 ao_serial0_getchar(void)
219 {
220         return ao_usart_getchar(&ao_samd21_usart0);
221 }
222
223 void
224 ao_serial0_putchar(char c)
225 {
226         ao_usart_putchar(&ao_samd21_usart0, c);
227 }
228
229 int
230 _ao_serial0_pollchar(void)
231 {
232         return _ao_usart_pollchar(&ao_samd21_usart0);
233 }
234
235 void
236 ao_serial0_drain(void)
237 {
238         ao_usart_drain(&ao_samd21_usart0);
239 }
240
241 void
242 ao_serial0_set_speed(uint8_t speed)
243 {
244         ao_usart_drain(&ao_samd21_usart0);
245         ao_usart_set_speed(&ao_samd21_usart0, speed);
246 }
247 #endif  /* HAS_SERIAL_0 */
248
249 #if HAS_SERIAL_1
250
251 struct ao_samd21_usart ao_samd21_usart1;
252
253 void samd21_sercom1_isr(void) { ao_usart_isr(&ao_samd21_usart1, USE_SERIAL_1_STDIN); }
254
255 char
256 ao_serial1_getchar(void)
257 {
258         return ao_usart_getchar(&ao_samd21_usart1);
259 }
260
261 void
262 ao_serial1_putchar(char c)
263 {
264         ao_usart_putchar(&ao_samd21_usart1, c);
265 }
266
267 int
268 _ao_serial1_pollchar(void)
269 {
270         return _ao_usart_pollchar(&ao_samd21_usart1);
271 }
272
273 void
274 ao_serial1_drain(void)
275 {
276         ao_usart_drain(&ao_samd21_usart1);
277 }
278
279 void
280 ao_serial1_set_speed(uint8_t speed)
281 {
282         ao_usart_drain(&ao_samd21_usart1);
283         ao_usart_set_speed(&ao_samd21_usart1, speed);
284 }
285 #endif  /* HAS_SERIAL_1 */
286
287 void
288 ao_serial_init(void)
289 {
290 #if HAS_SERIAL_0
291
292 #if SERIAL_0_PA10_PA11
293         /* Pin settings */
294         ao_enable_port(&samd21_port_a);
295         samd21_port_pmux_set(&samd21_port_a, 10, SAMD21_PORT_PMUX_FUNC_C);
296         samd21_port_pmux_set(&samd21_port_a, 11, SAMD21_PORT_PMUX_FUNC_C);
297 #else
298 #error "No SERIAL_0 port configuration specified"
299 #endif
300
301         ao_samd21_usart0.reg = &samd21_sercom0;
302         ao_usart_init(&ao_samd21_usart0, 0, 0);
303
304 #if USE_SERIAL_0_STDIN
305         ao_add_stdio(_ao_serial0_pollchar,
306                      ao_serial0_putchar,
307                      NULL);
308 #endif
309 #endif
310 #if HAS_SERIAL_1
311
312 #if SERIAL_1_PA00_PA01
313         /* Pin settings */
314         ao_enable_port(&samd21_port_a);
315         samd21_port_pmux_set(&samd21_port_a, 0, SAMD21_PORT_PMUX_FUNC_D);
316         samd21_port_pmux_set(&samd21_port_a, 1, SAMD21_PORT_PMUX_FUNC_D);
317 #else
318 #error "No SERIAL_1 port configuration specified"
319 #endif
320
321         ao_samd21_usart1.reg = &samd21_sercom1;
322         ao_usart_init(&ao_samd21_usart1, 0, 0);
323
324 #if USE_SERIAL_1_STDIN
325         ao_add_stdio(_ao_serial1_pollchar,
326                      ao_serial1_putchar,
327                      NULL);
328 #endif
329 #endif
330 }