periph_conf.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Benjamin Valentin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
21 #include <stdint.h>
22 
23 #include "cpu.h"
24 #include "periph_cpu.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
61 #define CLOCK_USE_PLL (1)
62 
63 #if CLOCK_USE_PLL
64 /* edit these values to adjust the PLL output frequency */
65 #define CLOCK_PLL_MUL (47U) /* must be >= 31 & <= 95 */
66 #define CLOCK_PLL_DIV (1U) /* adjust to your needs */
67 #define CLOCK_CORECLOCK (((CLOCK_PLL_MUL + 1) * 1000000U) / CLOCK_PLL_DIV)
68 #else
69 /* edit this value to your needs */
70 #define CLOCK_DIV (1U)
71 /* generate the actual core clock frequency */
72 #define CLOCK_CORECLOCK (8000000 / CLOCK_DIV)
73 #endif
80 static const tc32_conf_t timer_config[] = {
81  { /* Timer 0 - System Clock */
82  .dev = TC3,
83  .irq = TC3_IRQn,
84  .pm_mask = PM_APBCMASK_TC3,
85  .gclk_ctrl = GCLK_CLKCTRL_ID_TCC2_TC3,
86 #if CLOCK_USE_PLL || CLOCK_USE_XOSC32_DFLL
87  .gclk_src = SAM0_GCLK_1MHZ,
88 #else
89  .gclk_src = SAM0_GCLK_MAIN,
90 #endif
91  .flags = TC_CTRLA_MODE_COUNT16,
92  },
93  { /* Timer 1 */
94  .dev = TC4,
95  .irq = TC4_IRQn,
96  .pm_mask = PM_APBCMASK_TC4 | PM_APBCMASK_TC5,
97  .gclk_ctrl = GCLK_CLKCTRL_ID_TC4_TC5,
98 #if CLOCK_USE_PLL || CLOCK_USE_XOSC32_DFLL
99  .gclk_src = SAM0_GCLK_1MHZ,
100 #else
101  .gclk_src = SAM0_GCLK_MAIN,
102 #endif
103  .flags = TC_CTRLA_MODE_COUNT32,
104  }
105 };
106 
107 #define TIMER_0_MAX_VALUE 0xffff
108 
109 /* interrupt function name mapping */
110 #define TIMER_0_ISR isr_tc3
111 #define TIMER_1_ISR isr_tc4
112 
113 #define TIMER_NUMOF ARRAY_SIZE(timer_config)
120 static const uart_conf_t uart_config[] = {
121  {
122  .dev = &SERCOM2->USART,
123  .rx_pin = GPIO_PIN(PA, 9), /* D5 */
124  .tx_pin = GPIO_PIN(PA, 8), /* D4 */
125  .mux = GPIO_MUX_D,
126  .rx_pad = UART_PAD_RX_1,
127  .tx_pad = UART_PAD_TX_0,
128  .flags = UART_FLAG_NONE,
129  .gclk_src = SAM0_GCLK_MAIN,
130  },
131  {
132  .dev = &SERCOM0->USART,
133  .rx_pin = GPIO_PIN(PA, 5), /* D1 */
134  .tx_pin = GPIO_PIN(PA, 4), /* D0 */
135  .mux = GPIO_MUX_D,
136  .rx_pad = UART_PAD_RX_1,
137  .tx_pad = UART_PAD_TX_0,
138  .flags = UART_FLAG_NONE,
139  .gclk_src = SAM0_GCLK_MAIN,
140  }
141 };
142 
143 /* interrupt function name mapping */
144 #define UART_0_ISR isr_sercom2
145 #define UART_1_ISR isr_sercom0
146 
147 #define UART_NUMOF ARRAY_SIZE(uart_config)
154 #define PWM_0_EN 1
155 #define PWM_1_EN 1
156 
157 #if PWM_0_EN
158 /* PWM0 channels */
159 static const pwm_conf_chan_t pwm_chan0_config[] = {
160  /* GPIO pin, MUX value, TCC channel */
161  { GPIO_PIN(PA, 4), GPIO_MUX_E, 0 },
162  { GPIO_PIN(PA, 5), GPIO_MUX_E, 1 },
163  { GPIO_PIN(PA, 19), GPIO_MUX_F, 3 },
164  { GPIO_PIN(PA, 22), GPIO_MUX_F, 4 },
165  { GPIO_PIN(PA, 23), GPIO_MUX_F, 5 },
166 };
167 #endif
168 #if PWM_1_EN
169 /* PWM1 channels */
170 static const pwm_conf_chan_t pwm_chan1_config[] = {
171  /* GPIO pin, MUX value, TCC channel */
172  { GPIO_PIN(PA, 6), GPIO_MUX_E, 0 },
173  { GPIO_PIN(PA, 7), GPIO_MUX_E, 1 },
174 };
175 #endif
176 
177 /* PWM device configuration */
178 static const pwm_conf_t pwm_config[] = {
179 #if PWM_0_EN
180  {TCC_CONFIG(TCC0), pwm_chan0_config, ARRAY_SIZE(pwm_chan0_config), SAM0_GCLK_MAIN},
181 #endif
182 #if PWM_1_EN
183  {TCC_CONFIG(TCC1), pwm_chan1_config, ARRAY_SIZE(pwm_chan1_config), SAM0_GCLK_MAIN},
184 #endif
185 };
186 
187 /* number of devices that are actually defined */
188 #define PWM_NUMOF ARRAY_SIZE(pwm_config)
195 static const spi_conf_t spi_config[] = {
196  { /* Flash */
197  .dev = &SERCOM1->SPI,
198  .miso_pin = GPIO_PIN(PA, 18),
199  .mosi_pin = GPIO_PIN(PA, 16),
200  .clk_pin = GPIO_PIN(PA, 17),
201  .miso_mux = GPIO_MUX_C,
202  .mosi_mux = GPIO_MUX_C,
203  .clk_mux = GPIO_MUX_C,
204  .miso_pad = SPI_PAD_MISO_2,
205  .mosi_pad = SPI_PAD_MOSI_0_SCK_1,
206  .gclk_src = SAM0_GCLK_MAIN,
207 #ifdef MODULE_PERIPH_DMA
208  .tx_trigger = SERCOM1_DMAC_ID_TX,
209  .rx_trigger = SERCOM1_DMAC_ID_RX,
210 #endif
211  },
212  { /* D0 … D2 (user pins) */
213  .dev = &SERCOM0->SPI,
214  .miso_pin = GPIO_PIN(PA, 6), /* D2 */
215  .mosi_pin = GPIO_PIN(PA, 4), /* D0 */
216  .clk_pin = GPIO_PIN(PA, 5), /* D1 */
217  .miso_mux = GPIO_MUX_D,
218  .mosi_mux = GPIO_MUX_D,
219  .clk_mux = GPIO_MUX_D,
220  .miso_pad = SPI_PAD_MISO_2,
221  .mosi_pad = SPI_PAD_MOSI_0_SCK_1,
222  .gclk_src = SAM0_GCLK_MAIN,
223 #ifdef MODULE_PERIPH_DMA
224  .tx_trigger = SERCOM0_DMAC_ID_TX,
225  .rx_trigger = SERCOM0_DMAC_ID_RX,
226 #endif
227  },
228 };
229 
230 #define SPI_NUMOF ARRAY_SIZE(spi_config)
237 static const i2c_conf_t i2c_config[] = {
238  {
239  .dev = &(SERCOM2->I2CM),
240  .speed = I2C_SPEED_NORMAL,
241  .scl_pin = GPIO_PIN(PA, 9), /* D5 */
242  .sda_pin = GPIO_PIN(PA, 8), /* D4 */
243  .mux = GPIO_MUX_D,
244  .gclk_src = SAM0_GCLK_MAIN,
245  .flags = I2C_FLAG_NONE
246  }
247 };
248 #define I2C_NUMOF ARRAY_SIZE(i2c_config)
255 #ifndef RTT_FREQUENCY
256 #define RTT_FREQUENCY (32768U) /* in Hz. For changes see `rtt.c` */
257 #endif
264 #define ADC_PRESCALER ADC_CTRLB_PRESCALER_DIV512
265 
266 #define ADC_NEG_INPUT ADC_INPUTCTRL_MUXNEG_GND
267 #define ADC_GAIN_FACTOR_DEFAULT ADC_INPUTCTRL_GAIN_1X
268 #define ADC_REF_DEFAULT ADC_REFCTRL_REFSEL_INT1V
269 
270 static const adc_conf_chan_t adc_channels[] = {
271  /* port, pin, muxpos */
272  { .inputctrl = ADC_INPUTCTRL_MUXPOS_PA04 },
273  { .inputctrl = ADC_INPUTCTRL_MUXPOS_PA05 },
274  { .inputctrl = ADC_INPUTCTRL_MUXPOS_PA06 },
275  { .inputctrl = ADC_INPUTCTRL_MUXPOS_PA07 },
276  { .inputctrl = ADC_INPUTCTRL_MUXPOS_PA08 },
277  { .inputctrl = ADC_INPUTCTRL_MUXPOS_PA09 },
278 };
279 
280 #define ADC_NUMOF ARRAY_SIZE(adc_channels)
287 static const sam0_common_usb_config_t sam_usbdev_config[] = {
288  {
289  .dm = GPIO_PIN(PA, 24),
290  .dp = GPIO_PIN(PA, 25),
291  .d_mux = GPIO_MUX_G,
292  .device = &USB->DEVICE,
293  .gclk_src = SAM0_GCLK_MAIN,
294  }
295 };
298 #ifdef __cplusplus
299 }
300 #endif
301 
#define GPIO_PIN(x, y)
Define a CPU specific GPIO pin generator macro.
Definition: periph_cpu.h:45
static const uart_conf_t uart_config[]
UART configuration.
Definition: periph_conf.h:38
static const spi_conf_t spi_config[]
SPI configuration.
Definition: periph_conf.h:96
static const i2c_conf_t i2c_config[]
I2C configuration.
Definition: periph_conf.h:68
static const timer_conf_t timer_config[]
All timers on board.
Definition: periph_conf.h:39
static const pwm_conf_t pwm_config[]
Actual PWM configuration.
Definition: periph_conf.h:221
#define ARRAY_SIZE(a)
Calculate the number of elements in a static array.
Definition: container.h:82
static const gpio_t adc_channels[]
Static array with declared ADC channels.
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: periph_cpu.h:277
@ UART_PAD_RX_1
select pad 1
@ PA
port A
@ I2C_FLAG_NONE
No flags set.
@ SPI_PAD_MISO_2
use pad 2 for MISO line
@ UART_FLAG_NONE
No flags set.
@ UART_PAD_TX_0
select pad 0
#define TCC_CONFIG(tim)
Static initializer for TCC timer configuration.
@ GPIO_MUX_E
select peripheral function E
@ GPIO_MUX_D
select peripheral function D
@ GPIO_MUX_G
select peripheral function G
@ GPIO_MUX_C
select peripheral function C
@ GPIO_MUX_F
select peripheral function F
@ SPI_PAD_MOSI_0_SCK_1
use pad 0 for MOSI, pad 1 for SCK
#define ADC_INPUTCTRL_MUXPOS_PA05
Alias for PIN5.
Definition: periph_cpu.h:123
@ SAM0_GCLK_1MHZ
1 MHz clock for xTimer
Definition: periph_cpu.h:75
#define ADC_INPUTCTRL_MUXPOS_PA08
Alias for PIN16.
Definition: periph_cpu.h:134
#define ADC_INPUTCTRL_MUXPOS_PA09
Alias for PIN17.
Definition: periph_cpu.h:135
#define ADC_INPUTCTRL_MUXPOS_PA07
Alias for PIN7.
Definition: periph_cpu.h:125
#define ADC_INPUTCTRL_MUXPOS_PA04
Alias for PIN4.
Definition: periph_cpu.h:122
#define ADC_INPUTCTRL_MUXPOS_PA06
Alias for PIN6.
Definition: periph_cpu.h:124
#define SAM0_GCLK_MAIN
120 MHz main clock
Definition: periph_cpu.h:73
ADC Channel Configuration.
I2C configuration structure.
Definition: periph_cpu.h:298
TWI_t * dev
Pointer to hardware module registers.
Definition: periph_cpu.h:299
PWM channel configuration data structure.
PWM device configuration.
USB peripheral parameters.
SPI device configuration.
Definition: periph_cpu.h:336
SPI_t * dev
pointer to the used SPI device
Definition: periph_cpu.h:337
Timer device configuration.
TC0_t * dev
Pointer to the used as Timer device.
Definition: periph_cpu.h:264
UART device configuration.
Definition: periph_cpu.h:217
USART_t * dev
pointer to the used UART device
Definition: periph_cpu.h:218