47656551c00305a17d4fe15a30325452d67329e0
[fw/altos] / src / samd21 / ao_arch_funcs.h
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  * 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.
17  */
18
19 #ifndef _AO_ARCH_FUNCS_H_
20 #define _AO_ARCH_FUNCS_H_
21
22 #define AO_MODE_PULL_NONE       0
23 #define AO_MODE_PULL_UP         1
24 #define AO_MODE_PULL_DOWN       2
25
26 static inline void ao_enable_port(struct samd21_port *port)
27 {
28         (void) port;
29         samd21_pm.apbbmask |= (1UL << SAMD21_PM_APBBMASK_PORT);
30 }
31
32 static inline void ao_disable_port(struct samd21_port *port)
33 {
34         (void) port;
35         samd21_pm.apbbmask &= ~(1UL << SAMD21_PM_APBBMASK_PORT);
36 }
37
38 static inline void
39 ao_gpio_set(struct samd21_port *port, uint8_t bit, uint8_t v)
40 {
41         if (v)
42                 port->outset = (1 << bit);
43         else
44                 port->outclr = (1 << bit);
45 }
46
47 static inline uint8_t
48 ao_gpio_get(struct samd21_port *port, uint8_t bit)
49 {
50         return (port->in >> bit) & 1;
51 }
52
53 static inline void
54 ao_gpio_dir_set(struct samd21_port *port, uint8_t bit, bool output)
55 {
56         if (output)
57                 port->dirset = (1 << bit);
58         else
59                 port->dirclr = (1 << bit);
60 }
61
62 static inline void
63 ao_gpio_set_mode(struct samd21_port *port, uint8_t bit, uint32_t mode)
64 {
65         uint8_t pincfg = 0;
66
67         if (mode != AO_MODE_PULL_NONE) {
68                 pincfg |= (1 << SAMD21_PORT_PINCFG_PULLEN);
69                 ao_gpio_set(port, bit, mode == AO_MODE_PULL_UP);
70         }
71
72         samd21_port_pincfg_set(port, bit,
73                                (0 << SAMD21_PORT_PINCFG_DRVSTR) |
74                                (1 << SAMD21_PORT_PINCFG_PULLEN) |
75                                (0 << SAMD21_PORT_PINCFG_INEN) |
76                                (0 << SAMD21_PORT_PINCFG_PMUXEN),
77                                pincfg);
78 }
79
80 static inline void
81 ao_enable_output(struct samd21_port *port, uint8_t pin, uint8_t v)
82 {
83         ao_enable_port(port);
84         ao_gpio_set(port, pin, v);
85         samd21_port_dir_set(port, pin, SAMD21_PORT_DIR_OUT);
86         samd21_port_pincfg_set(port, pin,
87                                (1 << SAMD21_PORT_PINCFG_DRVSTR) |
88                                (1 << SAMD21_PORT_PINCFG_PULLEN) |
89                                (1 << SAMD21_PORT_PINCFG_INEN),
90                                (0 << SAMD21_PORT_PINCFG_DRVSTR) |
91                                (0 << SAMD21_PORT_PINCFG_PULLEN) |
92                                (0 << SAMD21_PORT_PINCFG_INEN));
93 }
94
95 static inline void
96 ao_enable_input(struct samd21_port *port, uint8_t pin, uint32_t mode)
97 {
98         ao_enable_port(port);
99         samd21_port_dir_set(port, pin, SAMD21_PORT_DIR_IN);
100         uint8_t pincfg;
101
102         pincfg = ((0 << SAMD21_PORT_PINCFG_DRVSTR) |
103                   (0 << SAMD21_PORT_PINCFG_PULLEN) |
104                   (1 << SAMD21_PORT_PINCFG_INEN) |
105                   (0 << SAMD21_PORT_PINCFG_PMUXEN));
106
107         if (mode != AO_MODE_PULL_NONE) {
108                 pincfg |= (1 << SAMD21_PORT_PINCFG_PULLEN);
109                 ao_gpio_set(port, pin, mode == AO_MODE_PULL_UP);
110         }
111
112         samd21_port_pincfg_set(port, pin,
113                                (1 << SAMD21_PORT_PINCFG_DRVSTR) |
114                                (1 << SAMD21_PORT_PINCFG_PULLEN) |
115                                (1 << SAMD21_PORT_PINCFG_INEN) |
116                                (1 << SAMD21_PORT_PINCFG_PMUXEN),
117                                pincfg);
118 }
119
120 static inline void
121 ao_enable_cs(struct samd21_port *port, uint8_t pin)
122 {
123         ao_enable_output(port, pin, 1);
124 }
125
126 /* ao_spi_samd21.c */
127
128 #define AO_SPI_INDEX_BIT        0
129 #define AO_SPI_INDEX_MASK       0x07
130
131 #define AO_SPI_CONFIG_BIT       4
132 #define AO_SPI_CONFIG_MASK      (3 << AO_SPI_CONFIG_BIT)
133
134 #define AO_SPI_CPOL_BIT         6
135 #define AO_SPI_CPHA_BIT         7
136
137 #define AO_SPI_DOPO_BIT         8
138 #define  AO_SPI_DOPO_MOSI_0_SCLK_1      (0 << AO_SPI_DOPO_BIT)
139 #define  AO_SPI_DOPO_MOSI_2_SCLK_3      (1 << AO_SPI_DOPO_BIT)
140 #define  AO_SPI_DOPO_MOSI_3_SCLK_1      (2 << AO_SPI_DOPO_BIT)
141 #define  AO_SPI_DOPO_MOSI_0_SCLK_3      (3 << AO_SPI_DOPO_BIT)
142 #define  AO_SPI_DOPO_MASK               (3 << AO_SPI_DOPO_BIT)
143
144 #define AO_SPI_DIPO_BIT         10
145 #define  AO_SPI_DIPO_MISO_0             (0 << AO_SPI_DIPO_BIT)
146 #define  AO_SPI_DIPO_MISO_1             (1 << AO_SPI_DIPO_BIT)
147 #define  AO_SPI_DIPO_MISO_2             (2 << AO_SPI_DIPO_BIT)
148 #define  AO_SPI_DIPO_MISO_3             (3 << AO_SPI_DIPO_BIT)
149 #define  AO_SPI_DIPO_MASK               (3 << AO_SPI_DIPO_MASK)
150
151 #define AO_SPI_CONFIG_0         (0 << AO_SPI_CONFIG_BIT)
152 #define AO_SPI_CONFIG_1         (1 << AO_SPI_CONFIG_BIT)
153 #define AO_SPI_CONFIG_2         (2 << AO_SPI_CONFIG_BIT)
154 #define AO_SPI_CONFIG_3         (3 << AO_SPI_CONFIG_BIT)
155
156 /*
157  * PA08 SERCOM0.0 -> MOSI       (DOPO 0)
158  * PA09 SERCOM0.1 -> SCLK       (DOPO 0)
159  * PA10 SERCOM0.2 -> MISO       (DIPO 2)
160  */
161 #define AO_SPI_0_CONFIG_PA08_PA09_PA10  (AO_SPI_CONFIG_0 | \
162                                          AO_SPI_DOPO_MOSI_0_SCLK_1 | \
163                                          AO_SPI_DIPO_MISO_2)
164
165 /*
166  * PA04 SERCOM0.0 -> MOSI       (DOPO 0)
167  * PA05 SERCOM0.1 -> SCLK       (DOPO 0)
168  * PA16 SERCOM0.2 -> MISO       (DIPO 2)
169  */
170 #define AO_SPI_0_CONFIG_PA04_PA05_PA06  (AO_SPI_CONFIG_1 |              \
171                                          AO_SPI_DOPO_MOSI_0_SCLK_1 |    \
172                                          AO_SPI_DIPO_MISO_2)
173
174 /*
175  * PB10 SERCOM4.2 -> MOSI       (DOPO 1)
176  * PB11 SERCOM4.3 -> SCLK       (DOPO 1)
177  * PA12 SERCOM4.0 -> MISO       (DIPO 0)
178  */
179 #define AO_SPI_4_CONFIG_PB10_PB11_PA12  (AO_SPI_CONFIG_0 |              \
180                                          AO_SPI_DOPO_MOSI_2_SCLK_3 |    \
181                                          AO_SPI_DIPO_MISO_0)
182
183 /*
184  * PB22 SERCOM5.2 -> MOSI       (DOPO 1)
185  * PB23 SERCOM5.3 -> SCLK       (DOPO 1)
186  * PB03 SERCOM5.1 -> MISO       (DIPO 1)
187  */
188 #define AO_SPI_5_CONFIG_PB22_PB23_PB03  (AO_SPI_CONFIG_3 |              \
189                                          AO_SPI_DOPO_MOSI_2_SCLK_3 |    \
190                                          AO_SPI_DIPO_MISO_1)
191
192 #define AO_SPI_INDEX(id)        ((uint8_t) ((id) & AO_SPI_INDEX_MASK))
193 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
194 #define AO_SPI_PIN_CONFIG(id)   ((id) & (AO_SPI_INDEX_MASK | AO_SPI_CONFIG_MASK))
195 #define AO_SPI_CPOL(id)         ((uint32_t) (((id) >> AO_SPI_CPOL_BIT) & 1))
196 #define AO_SPI_CPHA(id)         ((uint32_t) (((id) >> AO_SPI_CPHA_BIT) & 1))
197 #define AO_SPI_DOPO(id)         ((uint32_t) (((id) >> AO_SPI_DOPO_BIT) & 3))
198 #define AO_SPI_DIPO(id)         ((uint32_t) (((id) >> AO_SPI_DIPO_BIT) & 3))
199
200 /*
201  * We're not going to do any fancy SPI pin remapping, just use the first
202  * three PAD pins, which means:
203  *
204  * MOSI: PAD.0
205  * SCK:  PAD.1
206  * MISO: PAD.2
207  */
208
209 #define AO_SPI_0_PA08_PA09_PA10 (0 | AO_SPI_0_CONFIG_PA08_PA09_PA10)
210 #define AO_SPI_0_PA04_PA05_PA06 (0 | AO_SPI_0_CONFIG_PA04_PA05_PA06)
211
212 #define AO_SPI_4_PB10_PB11_PA12 (4 | AO_SPI_4_CONFIG_PB10_PB11_PA12)
213
214 #define AO_SPI_5_PB22_PB23_PB03 (5 | AO_SPI_5_CONFIG_PB22_PB23_PB03)
215
216 void
217 ao_spi_send(const void *block, uint16_t len, uint16_t spi_index);
218
219 void
220 ao_spi_recv(void *block, uint16_t len, uint16_t spi_index);
221
222 void
223 ao_spi_duplex(const void *out, void *in, uint16_t len, uint16_t spi_index);
224
225 void
226 ao_spi_get(uint16_t spi_index, uint32_t speed);
227
228 void
229 ao_spi_put(uint16_t spi_index);
230
231 void
232 ao_spi_init(void);
233
234 #define ao_spi_set_cs(reg,mask) do {            \
235                 reg->outclr = mask;             \
236         } while(0)
237
238 #define ao_spi_clr_cs(reg,mask) do {            \
239                 reg->outset = mask;             \
240         } while(0)
241
242 #define ao_spi_get_mask(reg,mask,spi_index, speed) do {         \
243                 ao_spi_get(spi_index, speed);                           \
244                 ao_spi_set_cs(reg,mask);                        \
245         } while (0)
246
247 #define ao_spi_put_mask(reg,mask,spi_index) do {        \
248                 ao_spi_clr_cs(reg,mask);        \
249                 ao_spi_put(spi_index);          \
250         } while (0)
251
252 static inline void
253 ao_spi_get_bit(struct samd21_port *port, uint8_t bit, uint16_t spi_index, uint32_t speed)
254 {
255         ao_spi_get(spi_index, speed);
256         ao_gpio_set(port, bit, 0);
257 }
258
259 static inline void
260 ao_spi_put_bit(struct samd21_port *port, uint8_t bit, uint16_t spi_index)
261 {
262         ao_gpio_set(port, bit, 1);
263         ao_spi_put(spi_index);
264 }
265
266 static inline uint8_t
267 ao_spi_speed(uint32_t hz)
268 {
269         int32_t baud = (int32_t) (AO_SYSCLK / (2 * hz)) - 1;
270
271         if (baud < 0)
272                 baud = 0;
273         if (baud > 255)
274                 baud = 255;
275         return (uint8_t) baud;
276 }
277
278 #define ao_spi_init_cs(port, mask) do {                                 \
279                 uint8_t __bit__;                                        \
280                 for (__bit__ = 0; __bit__ < 32; __bit__++) {            \
281                         if (mask & (1 << __bit__))                      \
282                                 ao_enable_output(port, __bit__, 1); \
283                 }                                                       \
284         } while (0)
285
286 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
287
288 typedef uint32_t        ao_arch_irq_t;
289
290 static inline uint32_t
291 ao_arch_irqsave(void) {
292         uint32_t        primask;
293         asm("mrs %0,primask" : "=&r" (primask));
294         ao_arch_block_interrupts();
295         return primask;
296 }
297
298 static inline void
299 ao_arch_irqrestore(uint32_t primask) {
300         asm("msr primask,%0" : : "r" (primask));
301 }
302
303 static inline void
304 ao_arch_memory_barrier(void) {
305         asm volatile("" ::: "memory");
306 }
307
308 #if HAS_TASK
309 static inline void
310 ao_arch_init_stack(struct ao_task *task, uint32_t *sp, void *start)
311 {
312         uint32_t        a = (uint32_t) start;
313         int             i;
314
315         /* Return address (goes into LR) */
316         ARM_PUSH32(sp, a);
317
318         /* Clear register values r0-r7 */
319         i = 8;
320         while (i--)
321                 ARM_PUSH32(sp, 0);
322
323         /* APSR */
324         ARM_PUSH32(sp, 0);
325
326         /* PRIMASK with interrupts enabled */
327         ARM_PUSH32(sp, 0);
328
329         task->sp32 = sp;
330 }
331
332 static inline void ao_arch_save_regs(void) {
333         /* Save general registers */
334         asm("push {r0-r7,lr}\n");
335
336         /* Save APSR */
337         asm("mrs r0,apsr");
338         asm("push {r0}");
339
340         /* Save PRIMASK */
341         asm("mrs r0,primask");
342         asm("push {r0}");
343 }
344
345 static inline void ao_arch_save_stack(void) {
346         uint32_t        *sp;
347         asm("mov %0,sp" : "=&r" (sp) );
348         ao_cur_task->sp32 = (sp);
349         if (sp < &ao_cur_task->stack32[0])
350                 ao_panic (AO_PANIC_STACK);
351 }
352
353 static inline void ao_arch_restore_stack(void) {
354         /* Switch stacks */
355         asm("mov sp, %0" : : "r" (ao_cur_task->sp32) );
356
357         /* Restore PRIMASK */
358         asm("pop {r0}");
359         asm("msr primask,r0");
360
361         /* Restore APSR */
362         asm("pop {r0}");
363         asm("msr apsr_nczvq,r0");
364
365         /* Restore general registers */
366         asm("pop {r0-r7,pc}\n");
367 }
368
369 #ifndef HAS_SAMPLE_PROFILE
370 #define HAS_SAMPLE_PROFILE 0
371 #endif
372
373 #if !HAS_SAMPLE_PROFILE
374 #define HAS_ARCH_START_SCHEDULER        1
375
376 static inline void ao_arch_start_scheduler(void) {
377         uint32_t        sp;
378         uint32_t        control;
379
380         asm("mrs %0,msp" : "=&r" (sp));
381         asm("msr psp,%0" : : "r" (sp));
382         asm("mrs %0,control" : "=&r" (control));
383         control |= (1 << 1);
384         asm("msr control,%0" : : "r" (control));
385         asm("isb");
386 }
387 #endif
388
389 #define ao_arch_isr_stack()
390
391 #endif
392
393 #define ao_arch_wait_interrupt() do {                           \
394                 asm("\twfi\n");                                 \
395                 ao_arch_release_interrupts();                   \
396                 asm(".global ao_idle_loc\nao_idle_loc:");       \
397                 ao_arch_block_interrupts();                     \
398         } while (0)
399
400 #define ao_arch_critical(b) do {                        \
401                 uint32_t __mask = ao_arch_irqsave();    \
402                 do { b } while (0);                     \
403                 ao_arch_irqrestore(__mask);             \
404         } while (0)
405
406 /* ao_serial_samd21.c */
407
408 #if USE_SERIAL_0_FLOW && USE_SERIAL_0_SW_FLOW || USE_SERIAL_1_FLOW && USE_SERIAL_1_SW_FLOW
409 #define HAS_SERIAL_SW_FLOW      1
410 #else
411 #define HAS_SERIAL_SW_FLOW      0
412 #endif
413
414 #if USE_SERIAL_1_FLOW && !USE_SERIAL_1_SW_FLOW
415 #define USE_SERIAL_1_HW_FLOW    1
416 #endif
417
418 #if USE_SERIAL_0_FLOW && !USE_SERIAL_0_SW_FLOW
419 #define USE_SERIAL_0_HW_FLOW    1
420 #endif
421
422 #if USE_SERIAL_0_HW_FLOW || USE_SERIAL_1_HW_FLOW
423 #define HAS_SERIAL_HW_FLOW      1
424 #else
425 #define HAS_SERIAL_HW_FLOW      0
426 #endif
427
428 struct ao_samd21_usart {
429         struct ao_fifo          rx_fifo;
430         struct ao_fifo          tx_fifo;
431         struct samd21_sercom    *reg;
432         uint8_t                 tx_running;
433         uint8_t                 draining;
434 #if HAS_SERIAL_SW_FLOW
435         /* RTS - 0 if we have FIFO space, 1 if not
436          * CTS - 0 if we can send, 0 if not
437          */
438         struct samd21_port      *gpio_rts;
439         struct samd21_port      *gpio_cts;
440         uint8_t                 pin_rts;
441         uint8_t                 pin_cts;
442         uint8_t                 rts;
443 #endif
444 };
445
446 #if HAS_USART_0
447 extern struct ao_samd21_usart   ao_samd21_usart0;
448 #endif
449
450 void
451 ao_serial_init(void);
452
453 /* ao_usb_samd21.c */
454
455 #if AO_USB_OUT_HOOK
456 void
457 ao_usb_out_hook(uint8_t *buffer, uint16_t count);
458 #endif
459
460 void start(void);
461
462 #endif /* _AO_ARCH_FUNCS_H_ */