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.
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.
19 #ifndef _AO_ARCH_FUNCS_H_
20 #define _AO_ARCH_FUNCS_H_
22 #define AO_MODE_PULL_NONE 0
23 #define AO_MODE_PULL_UP 1
24 #define AO_MODE_PULL_DOWN 2
26 static inline void ao_enable_port(struct samd21_port *port)
29 samd21_pm.apbbmask |= (1UL << SAMD21_PM_APBBMASK_PORT);
32 static inline void ao_disable_port(struct samd21_port *port)
35 samd21_pm.apbbmask &= ~(1UL << SAMD21_PM_APBBMASK_PORT);
39 ao_gpio_set(struct samd21_port *port, uint8_t bit, uint8_t v)
42 port->outset = (1 << bit);
44 port->outclr = (1 << bit);
48 ao_gpio_get(struct samd21_port *port, uint8_t bit)
50 return (port->in >> bit) & 1;
54 ao_gpio_dir_set(struct samd21_port *port, uint8_t bit, bool output)
57 port->dirset = (1 << bit);
59 port->dirclr = (1 << bit);
63 ao_gpio_set_mode(struct samd21_port *port, uint8_t bit, uint32_t mode)
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);
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),
81 ao_enable_output(struct samd21_port *port, uint8_t pin, uint8_t v)
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));
96 ao_enable_input(struct samd21_port *port, uint8_t pin, uint32_t mode)
99 samd21_port_dir_set(port, pin, SAMD21_PORT_DIR_IN);
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));
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);
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),
121 ao_enable_cs(struct samd21_port *port, uint8_t pin)
123 ao_enable_output(port, pin, 1);
126 /* ao_spi_samd21.c */
128 #define AO_SPI_CPOL_BIT 6
129 #define AO_SPI_CPHA_BIT 7
131 #define AO_SPI_CONFIG_1 0x00
133 * PA08 SERCOM0.0 -> MOSI (DOPO 0)
134 * PA09 SERCOM0.1 -> SCLK (DOPO 0)
135 * PA10 SERCOM0.2 -> MISO (DIPO 2)
137 #define AO_SPI_0_CONFIG_PA08_PA09_PA10 AO_SPI_CONFIG_1
139 #define AO_SPI_CONFIG_2 0x08
141 * PA04 SERCOM0.0 -> MOSI (DOPO 0)
142 * PA05 SERCOM0.1 -> SCLK (DOPO 0)
143 * PA16 SERCOM0.2 -> MISO (DIPO 2)
145 #define AO_SPI_0_CONFIG_PA04_PA05_PA06 AO_SPI_CONFIG_2
147 #define AO_SPI_CONFIG_NONE 0x0c
149 #define AO_SPI_INDEX_MASK 0x07
150 #define AO_SPI_CONFIG_MASK 0x18
152 #define AO_SPI_INDEX(id) ((id) & AO_SPI_INDEX_MASK)
153 #define AO_SPI_CONFIG(id) ((id) & AO_SPI_CONFIG_MASK)
154 #define AO_SPI_PIN_CONFIG(id) ((id) & (AO_SPI_INDEX_MASK | AO_SPI_CONFIG_MASK))
155 #define AO_SPI_CPOL(id) ((uint32_t) (((id) >> AO_SPI_CPOL_BIT) & 1))
156 #define AO_SPI_CPHA(id) ((uint32_t) (((id) >> AO_SPI_CPHA_BIT) & 1))
159 * We're not going to do any fancy SPI pin remapping, just use the first
160 * three PAD pins, which means:
167 #define AO_SPI_0_PA08_PA09_PA10 (0 | AO_SPI_0_CONFIG_PA08_PA09_PA10)
168 #define AO_SPI_0_PA04_PA05_PA06 (0 | AO_SPI_0_CONFIG_PA04_PA05_PA06)
171 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
174 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
177 ao_spi_duplex(const void *out, void *in, uint16_t len, uint8_t spi_index);
180 ao_spi_get(uint8_t spi_index, uint32_t speed);
183 ao_spi_put(uint8_t spi_index);
188 #define ao_spi_get_mask(reg,mask,bus, speed) do { \
189 ao_spi_get(bus, speed); \
190 ao_spi_set_cs(reg,mask); \
193 #define ao_spi_put_mask(reg,mask,bus) do { \
194 ao_spi_clr_cs(reg,mask); \
199 ao_spi_get_bit(struct samd21_port *port, uint8_t bit, uint8_t bus, uint32_t speed)
201 ao_spi_get(bus, speed);
202 ao_gpio_set(port, bit, 0);
206 ao_spi_put_bit(struct samd21_port *port, uint8_t bit, uint8_t bus)
208 ao_gpio_set(port, bit, 1);
212 static inline uint8_t
213 ao_spi_speed(uint32_t hz)
215 int32_t baud = (int32_t) (AO_SYSCLK / (2 * hz)) - 1;
221 return (uint8_t) baud;
224 #define ao_spi_init_cs(port, mask) do { \
226 for (__bit__ = 0; __bit__ < 32; __bit__++) { \
227 if (mask & (1 << __bit__)) \
228 ao_enable_output(port, __bit__, 1); \
232 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
234 typedef uint32_t ao_arch_irq_t;
236 static inline uint32_t
237 ao_arch_irqsave(void) {
239 asm("mrs %0,primask" : "=&r" (primask));
240 ao_arch_block_interrupts();
245 ao_arch_irqrestore(uint32_t primask) {
246 asm("msr primask,%0" : : "r" (primask));
250 ao_arch_memory_barrier(void) {
251 asm volatile("" ::: "memory");
256 ao_arch_init_stack(struct ao_task *task, uint32_t *sp, void *start)
258 uint32_t a = (uint32_t) start;
261 /* Return address (goes into LR) */
264 /* Clear register values r0-r7 */
272 /* PRIMASK with interrupts enabled */
278 static inline void ao_arch_save_regs(void) {
279 /* Save general registers */
280 asm("push {r0-r7,lr}\n");
287 asm("mrs r0,primask");
291 static inline void ao_arch_save_stack(void) {
293 asm("mov %0,sp" : "=&r" (sp) );
294 ao_cur_task->sp32 = (sp);
295 if (sp < &ao_cur_task->stack32[0])
296 ao_panic (AO_PANIC_STACK);
299 static inline void ao_arch_restore_stack(void) {
301 asm("mov sp, %0" : : "r" (ao_cur_task->sp32) );
303 /* Restore PRIMASK */
305 asm("msr primask,r0");
309 asm("msr apsr_nczvq,r0");
311 /* Restore general registers */
312 asm("pop {r0-r7,pc}\n");
315 #ifndef HAS_SAMPLE_PROFILE
316 #define HAS_SAMPLE_PROFILE 0
319 #if !HAS_SAMPLE_PROFILE
320 #define HAS_ARCH_START_SCHEDULER 1
322 static inline void ao_arch_start_scheduler(void) {
326 asm("mrs %0,msp" : "=&r" (sp));
327 asm("msr psp,%0" : : "r" (sp));
328 asm("mrs %0,control" : "=&r" (control));
330 asm("msr control,%0" : : "r" (control));
335 #define ao_arch_isr_stack()
339 #define ao_arch_wait_interrupt() do { \
341 ao_arch_release_interrupts(); \
342 asm(".global ao_idle_loc\nao_idle_loc:"); \
343 ao_arch_block_interrupts(); \
346 #define ao_arch_critical(b) do { \
347 uint32_t __mask = ao_arch_irqsave(); \
348 do { b } while (0); \
349 ao_arch_irqrestore(__mask); \
352 /* ao_serial_samd21.c */
354 #if USE_SERIAL_0_FLOW && USE_SERIAL_0_SW_FLOW || USE_SERIAL_1_FLOW && USE_SERIAL_1_SW_FLOW
355 #define HAS_SERIAL_SW_FLOW 1
357 #define HAS_SERIAL_SW_FLOW 0
360 #if USE_SERIAL_1_FLOW && !USE_SERIAL_1_SW_FLOW
361 #define USE_SERIAL_1_HW_FLOW 1
364 #if USE_SERIAL_0_FLOW && !USE_SERIAL_0_SW_FLOW
365 #define USE_SERIAL_0_HW_FLOW 1
368 #if USE_SERIAL_0_HW_FLOW || USE_SERIAL_1_HW_FLOW
369 #define HAS_SERIAL_HW_FLOW 1
371 #define HAS_SERIAL_HW_FLOW 0
374 struct ao_samd21_usart {
375 struct ao_fifo rx_fifo;
376 struct ao_fifo tx_fifo;
377 struct samd21_sercom *reg;
380 #if HAS_SERIAL_SW_FLOW
381 /* RTS - 0 if we have FIFO space, 1 if not
382 * CTS - 0 if we can send, 0 if not
384 struct samd21_port *gpio_rts;
385 struct samd21_port *gpio_cts;
393 extern struct ao_samd21_usart ao_samd21_usart0;
397 ao_serial_init(void);
399 /* ao_usb_samd21.c */
403 ao_usb_out_hook(uint8_t *buffer, uint16_t count);
408 #endif /* _AO_ARCH_FUNCS_H_ */