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.
26 * Samd21 definitions and code fragments for AltOS
29 #define AO_PORT_TYPE uint32_t
31 #define AO_LED_TYPE AO_PORT_TYPE
33 #define ao_arch_naked_declare __attribute__((naked))
34 #define ao_arch_naked_define
36 #define ao_arch_reboot() \
37 (samd21_scb.aircr = ((SAMD21_SCB_AIRCR_VECTKEY_KEY << SAMD21_SCB_AIRCR_VECTKEY) | \
38 (1 << SAMD21_SCB_AIRCR_SYSRESETREQ)))
40 #define ao_arch_nop() asm("nop")
41 #define ao_arch_interrupt(n) /* nothing */
42 #define ao_arch_block_interrupts() asm("cpsid i")
43 #define ao_arch_release_interrupts() asm("cpsie i")
46 #define AO_ROMCONFIG_SYMBOL __attribute__((section(".init.1"))) const
47 #define AO_USBCONFIG_SYMBOL __attribute__((section(".init.2"))) const
52 * For the samd21, we want to use the DFLL48 clock
55 /* GCLK 0 is always the system clock source */
57 #define AO_GCLK_SYSCLK 0
59 /* If there's a 32kHz xtal, use that for the 32kHz oscillator */
61 # ifndef AO_GCLK_XOSC32K
62 # define AO_GCLK_XOSC32K 1
66 /* If there's a high-freq xtal, use that */
69 # define AO_GCLK_XOSC 1
72 # ifndef AO_XOSC_GCLK_DIV
73 # define AO_XOSC_GCLK_DIV 1
76 # define AO_FDPLL96M ((AO_XOSC_FREQ / AO_XOSC_DIV * AO_XOSC_MUL) / AO_XOSC_GCLK_DIV)
78 /* By default, use the xosc for the system, but allow the dfll48m to
79 * drive USB if desired.
82 # ifndef AO_GCLK_FDPLL96M
83 # define AO_SYSCLK AO_FDPLL96M
84 # define AO_GCLK_FDPLL96M AO_GCLK_SYSCLK
85 # define AO_GCLK_DFLL48M 2
91 # ifndef AO_GCLK_DFLL48M
92 # define AO_SYSCLK AO_DFLL48M
93 # define AO_GCLK_DFLL48M AO_GCLK_SYSCLK
99 # define AO_GCLK_USB AO_GCLK_DFLL48M
101 # define AO_GCLK_USB AO_GCLK_SYSCLK
108 * For the samd21, we want to use the DFLL48 clock
111 /* GCLK 0 is always the system clock source */
113 #define AO_GCLK_SYSCLK 0
115 /* If there's a 32kHz xtal, use that for the 32kHz oscillator */
117 # ifndef AO_GCLK_XOSC32K
118 # define AO_GCLK_XOSC32K 1
122 /* If there's a high-freq xtal, use that */
124 # ifndef AO_GCLK_XOSC
125 # define AO_GCLK_XOSC 1
128 # ifndef AO_XOSC_GCLK_DIV
129 # define AO_XOSC_GCLK_DIV 1
132 # define AO_FDPLL96M ((AO_XOSC_FREQ / AO_XOSC_DIV * AO_XOSC_MUL) / AO_XOSC_GCLK_DIV)
134 /* By default, use the xosc for the system, but allow the dfll48m to
135 * drive USB if desired.
138 # ifndef AO_GCLK_FDPLL96M
139 # define AO_SYSCLK AO_FDPLL96M
140 # define AO_GCLK_FDPLL96M AO_GCLK_SYSCLK
141 # define AO_GCLK_DFLL48M 2
147 # ifndef AO_GCLK_DFLL48M
148 # define AO_SYSCLK AO_DFLL48M
149 # define AO_GCLK_DFLL48M AO_GCLK_SYSCLK
155 # define AO_GCLK_USB AO_GCLK_DFLL48M
157 # define AO_GCLK_USB AO_GCLK_SYSCLK
161 #define AO_HCLK (AO_SYSCLK / AO_AHB_PRESCALER)
163 #define AO_HCLK (AO_SYSCLK / AO_AHB_PRESCALER)
164 #define AO_PCLK (AO_HCLK / AO_APB_PRESCALER)
165 #define AO_SYSTICK (AO_HCLK)
166 #define AO_PANIC_DELAY_SCALE (AO_SYSCLK / 12000000)
168 #define AO_SAMD21_NVIC_HIGH_PRIORITY (0 << 6)
169 #define AO_SAMD21_NVIC_CLOCK_PRIORITY (1 << 6)
170 #define AO_SAMD21_NVIC_MED_PRIORITY (2 << 6)
171 #define AO_SAMD21_NVIC_LOW_PRIORITY (3 << 6)
173 /* ADC maximum reported value */
174 #define AO_ADC_MAX 4095
176 #define AO_SAMD21_NVIC_HIGH_PRIORITY (0 << 6)
177 #define AO_SAMD21_NVIC_CLOCK_PRIORITY (1 << 6)
178 #define AO_SAMD21_NVIC_MED_PRIORITY (2 << 6)
179 #define AO_SAMD21_NVIC_LOW_PRIORITY (3 << 6)
181 /* ADC maximum reported value */
182 #define AO_ADC_MAX 4095
184 /* This has to be 65536 so that TCC and TC match; TC isn't configurable */
185 #define AO_TCC_PERIOD 65536
186 #define SNEK_PWM_MAX (AO_TCC_PERIOD-1)
188 #define AO_TICK_TYPE uint32_t
189 #define AO_TICK_SIGNED int32_t
192 ao_usb_waiting(void);
194 #define AO_CMD_LEN 128
195 #define AO_STACK_SIZE 2048
197 #ifndef HAS_BOOT_LOADER
198 #define HAS_BOOT_LOADER 1
202 #define AO_BOOT_APPLICATION_BASE ((uint32_t *) 0x00001000)
203 #ifndef AO_BOOT_APPLICATION_BOUND
204 #define AO_BOOT_APPLICATION_BOUND ((uint32_t *) (0x00000000 + samd21_flash_size()))
206 #define AO_BOOT_LOADER_BASE ((uint32_t *) 0x00000000)
209 #endif /* _AO_ARCH_H_ */