periph_conf.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Eistec AB
3  * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
20 #include "periph_cpu.h"
21 
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
31 /* The crystal on the Mulle is designed for 12.5 pF load capacitance. According
32  * to the data sheet, the K60 will have a 5 pF parasitic capacitance on the
33  * XTAL32/EXTAL32 connection. The board traces might give some minor parasitic
34  * capacitance as well. */
35 /* Use the equation
36  * CL = (C1 * C2) / (C1 + C2) + Cstray
37  * with C1 == C2:
38  * C1 = 2 * (CL - Cstray)
39  */
40 /* enable 14pF load capacitor which will yield a crystal load capacitance of 12 pF */
41 #define RTC_LOAD_CAP_BITS (RTC_CR_SC8P_MASK | RTC_CR_SC4P_MASK | RTC_CR_SC2P_MASK)
42 
43 static const clock_config_t clock_config = {
44  /*
45  * This configuration results in the system running from the FLL output with
46  * the following clock frequencies:
47  * Core: 48 MHz
48  * Bus: 48 MHz
49  * Flex: 24 MHz
50  * Flash: 24 MHz
51  */
52  /* The board has a 16 MHz crystal, though it is not used in this configuration */
53  /* This configuration uses the RTC crystal to provide the base clock, it
54  * should have better accuracy than the internal slow clock, and lower power
55  * consumption than using the 16 MHz crystal and the OSC0 module */
56  .clkdiv1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) |
57  SIM_CLKDIV1_OUTDIV3(1) | SIM_CLKDIV1_OUTDIV4(1),
58  .rtc_clc = RTC_LOAD_CAP_BITS,
59  .osc32ksel = SIM_SOPT1_OSC32KSEL(2),
60  .clock_flags =
61  /* no OSC0_EN, the RTC module provides the clock input signal for the FLL */
62  KINETIS_CLOCK_RTCOSC_EN |
63  KINETIS_CLOCK_USE_FAST_IRC |
64  0,
65  .default_mode = KINETIS_MCG_MODE_FEE,
66  .erc_range = KINETIS_MCG_ERC_RANGE_LOW, /* Input clock is 32768 Hz */
67  /* 16 pF capacitors yield ca 10 pF load capacitance as required by the
68  * onboard xtal, not used when OSC0 is disabled */
69  .osc_clc = OSC_CR_SC16P_MASK,
70  .oscsel = MCG_C7_OSCSEL(1), /* Use RTC for external clock */
71  .fcrdiv = MCG_SC_FCRDIV(0), /* Fast IRC divide by 1 => 4 MHz */
72  .fll_frdiv = MCG_C1_FRDIV(0b000), /* Divide by 1 => FLL input 32768 Hz */
73  .fll_factor_fei = KINETIS_MCG_FLL_FACTOR_1464, /* FLL freq = 48 MHz */
74  .fll_factor_fee = KINETIS_MCG_FLL_FACTOR_1464, /* FLL freq = 48 MHz */
75  /* PLL is unavailable when using a 32768 Hz source clock, so the
76  * configuration below can only be used if the above config is modified to
77  * use the 16 MHz crystal instead of the RTC. */
78  .pll_prdiv = MCG_C5_PRDIV0(0b00111), /* Divide by 8 */
79  .pll_vdiv = MCG_C6_VDIV0(0b01100), /* Multiply by 36 => PLL freq = 72 MHz */
80 };
81 #define CLOCK_CORECLOCK (48000000ul)
82 #define CLOCK_BUSCLOCK (CLOCK_CORECLOCK / 1)
89 #define PIT_NUMOF (2U)
90 #define PIT_CONFIG { \
91  { \
92  .prescaler_ch = 0, \
93  .count_ch = 1, \
94  }, \
95  { \
96  .prescaler_ch = 2, \
97  .count_ch = 3, \
98  }, \
99  }
100 #define LPTMR_NUMOF (1U)
101 #define LPTMR_CONFIG { \
102  { \
103  .dev = LPTMR0, \
104  .irqn = LPTMR0_IRQn, \
105  .src = 2, \
106  .base_freq = 32768u, \
107  } \
108  }
109 #define TIMER_NUMOF ((PIT_NUMOF) + (LPTMR_NUMOF))
110 
111 #define PIT_BASECLOCK (CLOCK_BUSCLOCK)
112 #define PIT_ISR_0 isr_pit1
113 #define PIT_ISR_1 isr_pit3
114 #define LPTMR_ISR_0 isr_lptmr0
115 
122 static const uart_conf_t uart_config[] = {
123  {
124  .dev = UART0,
125  .freq = CLOCK_CORECLOCK,
126  .pin_rx = GPIO_PIN(PORT_A, 15),
127  .pin_tx = GPIO_PIN(PORT_A, 14),
128  .pcr_rx = PORT_PCR_MUX(3),
129  .pcr_tx = PORT_PCR_MUX(3),
130  .irqn = UART0_RX_TX_IRQn,
131  .scgc_addr = &SIM->SCGC4,
132  .scgc_bit = SIM_SCGC4_UART0_SHIFT,
133  .mode = UART_MODE_8N1,
134  .type = KINETIS_UART,
135  },
136  {
137  .dev = UART1,
138  .freq = CLOCK_CORECLOCK,
139  .pin_rx = GPIO_PIN(PORT_C, 3),
140  .pin_tx = GPIO_PIN(PORT_C, 4),
141  .pcr_rx = PORT_PCR_MUX(3),
142  .pcr_tx = PORT_PCR_MUX(3),
143  .irqn = UART1_RX_TX_IRQn,
144  .scgc_addr = &SIM->SCGC4,
145  .scgc_bit = SIM_SCGC4_UART1_SHIFT,
146  .mode = UART_MODE_8N1,
147  .type = KINETIS_UART,
148  },
149 };
150 
151 #define UART_0_ISR (isr_uart0_rx_tx)
152 #define UART_1_ISR (isr_uart1_rx_tx)
153 
154 #define UART_NUMOF ARRAY_SIZE(uart_config)
161 static const adc_conf_t adc_config[] = {
162  /* internal: temperature sensor */
163  /* The temperature sensor has a very high output impedance, it must not be
164  * sampled using hardware averaging, or the sampled values will be garbage */
165  [ 0] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 26, .avg = ADC_AVG_NONE },
166  /* internal: band gap */
167  [ 1] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 27, .avg = ADC_AVG_MAX },
168  /* internal: V_REFSH */
169  [ 2] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 29, .avg = ADC_AVG_MAX },
170  /* internal: V_REFSL */
171  [ 3] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 30, .avg = ADC_AVG_MAX },
172  /* internal: DAC0 module output level */
173  [ 4] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 23, .avg = ADC_AVG_MAX },
174  /* internal: VREF module output level */
175  [ 5] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 18, .avg = ADC_AVG_MAX },
176  /* on board connection to Mulle Vbat/2 on PGA1_DP pin */
177  [ 6] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 0, .avg = ADC_AVG_MAX },
178  /* on board connection to Mulle Vchr/2 on PGA1_DM pin */
179  [ 7] = { .dev = ADC1, .pin = GPIO_UNDEF, .chan = 19, .avg = ADC_AVG_MAX },
180  /* expansion port PGA0_DP pin */
181  [ 8] = { .dev = ADC0, .pin = GPIO_UNDEF, .chan = 0, .avg = ADC_AVG_MAX },
182  /* expansion port PGA0_DM pin */
183  [ 9] = { .dev = ADC0, .pin = GPIO_UNDEF, .chan = 19, .avg = ADC_AVG_MAX },
184  /* expansion port PTA17 */
185  [10] = { .dev = ADC1, .pin = GPIO_PIN(PORT_A, 17), .chan = 17, .avg = ADC_AVG_MAX },
186  /* expansion port PTB0 */
187  [11] = { .dev = ADC1, .pin = GPIO_PIN(PORT_B, 0), .chan = 8, .avg = ADC_AVG_MAX },
188  /* expansion port PTC0 */
189  [12] = { .dev = ADC0, .pin = GPIO_PIN(PORT_C, 0), .chan = 14, .avg = ADC_AVG_MAX },
190  /* expansion port PTC8 */
191  [13] = { .dev = ADC1, .pin = GPIO_PIN(PORT_C, 8), .chan = 4, .avg = ADC_AVG_MAX },
192  /* expansion port PTC9 */
193  [14] = { .dev = ADC1, .pin = GPIO_PIN(PORT_C, 9), .chan = 5, .avg = ADC_AVG_MAX },
194  /* expansion port PTC10 */
195  [15] = { .dev = ADC1, .pin = GPIO_PIN(PORT_C, 10), .chan = 6, .avg = ADC_AVG_MAX },
196  /* expansion port PTC11 */
197  [16] = { .dev = ADC1, .pin = GPIO_PIN(PORT_C, 11), .chan = 7, .avg = ADC_AVG_MAX },
198 };
199 
200 #define ADC_NUMOF ARRAY_SIZE(adc_config)
201 /*
202  * K60D ADC reference settings:
203  * 0: VREFH/VREFL external pin pair
204  * 1: VREF_OUT internal 1.2 V reference (VREF module must be enabled)
205  * 2-3: reserved
206  */
207 #define ADC_REF_SETTING 0
214 static const dac_conf_t dac_config[] = {
215  {
216  .dev = DAC0,
217  .scgc_addr = &SIM->SCGC2,
218  .scgc_bit = SIM_SCGC2_DAC0_SHIFT
219  }
220 };
221 
222 #define DAC_NUMOF ARRAY_SIZE(dac_config)
229 static const pwm_conf_t pwm_config[] = {
230  {
231  .ftm = FTM0,
232  .chan = {
233  { .pin = GPIO_PIN(PORT_C, 1), .af = 4, .ftm_chan = 0 },
234  { .pin = GPIO_PIN(PORT_C, 2), .af = 4, .ftm_chan = 1 },
235  { .pin = GPIO_UNDEF, .af = 0, .ftm_chan = 0 },
236  { .pin = GPIO_UNDEF, .af = 0, .ftm_chan = 0 }
237  },
238  .chan_numof = 2,
239  .ftm_num = 0
240  },
241  {
242  .ftm = FTM1,
243  .chan = {
244  { .pin = GPIO_PIN(PORT_A, 12), .af = 3, .ftm_chan = 0 },
245  { .pin = GPIO_PIN(PORT_A, 13), .af = 3, .ftm_chan = 1 },
246  { .pin = GPIO_UNDEF, .af = 0, .ftm_chan = 0 },
247  { .pin = GPIO_UNDEF, .af = 0, .ftm_chan = 0 }
248  },
249  .chan_numof = 2,
250  .ftm_num = 1
251  }
252 };
253 
254 #define PWM_NUMOF ARRAY_SIZE(pwm_config)
267 static const uint32_t spi_clk_config[] = {
268  (
269  SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | /* -> 93728Hz */
270  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(8) |
271  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(8) |
272  SPI_CTAR_PDT(0) | SPI_CTAR_DT(8)
273  ),
274  (
275  SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | /* -> 374912Hz */
276  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(6) |
277  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(6) |
278  SPI_CTAR_PDT(0) | SPI_CTAR_DT(6)
279  ),
280  (
281  SPI_CTAR_PBR(1) | SPI_CTAR_BR(4) | /* -> 999765Hz */
282  SPI_CTAR_PCSSCK(1) | SPI_CTAR_CSSCK(3) |
283  SPI_CTAR_PASC(1) | SPI_CTAR_ASC(3) |
284  SPI_CTAR_PDT(1) | SPI_CTAR_DT(3)
285  ),
286  (
287  SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | /* -> 4798873Hz */
288  SPI_CTAR_PCSSCK(2) | SPI_CTAR_CSSCK(0) |
289  SPI_CTAR_PASC(2) | SPI_CTAR_ASC(0) |
290  SPI_CTAR_PDT(2) | SPI_CTAR_DT(0)
291  ),
292  (
293  SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | /* -> 7998122Hz */
294  SPI_CTAR_PCSSCK(1) | SPI_CTAR_CSSCK(0) |
295  SPI_CTAR_PASC(1) | SPI_CTAR_ASC(0) |
296  SPI_CTAR_PDT(1) | SPI_CTAR_DT(0)
297  )
298 };
299 
300 static const spi_conf_t spi_config[] = {
301  {
302  .dev = SPI0,
303  .pin_miso = GPIO_PIN(PORT_D, 3),
304  .pin_mosi = GPIO_PIN(PORT_D, 2),
305  .pin_clk = GPIO_PIN(PORT_D, 1),
306  .pin_cs = {
307  GPIO_PIN(PORT_D, 0),
308  GPIO_PIN(PORT_D, 4),
309  GPIO_PIN(PORT_D, 5),
310  GPIO_PIN(PORT_D, 6),
311  SPI_CS_UNDEF,
312  },
313  .pcr = GPIO_AF_2,
314  .simmask = SIM_SCGC6_SPI0_MASK
315  },
316  {
317  .dev = SPI1,
318  .pin_miso = GPIO_PIN(PORT_E, 3),
319  .pin_mosi = GPIO_PIN(PORT_E, 1),
320  .pin_clk = GPIO_PIN(PORT_E, 2),
321  .pin_cs = {
322  GPIO_PIN(PORT_E, 4),
323  SPI_CS_UNDEF,
324  SPI_CS_UNDEF,
325  SPI_CS_UNDEF,
326  SPI_CS_UNDEF,
327  },
328  .pcr = GPIO_AF_2,
329  .simmask = SIM_SCGC6_SPI1_MASK
330  }
331 };
332 
333 #define SPI_NUMOF ARRAY_SIZE(spi_config)
340 static const i2c_conf_t i2c_config[] = {
341  {
342  .i2c = I2C0,
343  .scl_pin = GPIO_PIN(PORT_B, 2),
344  .sda_pin = GPIO_PIN(PORT_B, 1),
345  .freq = CLOCK_BUSCLOCK,
346  .speed = I2C_SPEED_FAST,
347  .irqn = I2C0_IRQn,
348  .scl_pcr = (PORT_PCR_MUX(2) | PORT_PCR_ODE_MASK),
349  .sda_pcr = (PORT_PCR_MUX(2) | PORT_PCR_ODE_MASK),
350  },
351 };
352 #define I2C_NUMOF ARRAY_SIZE(i2c_config)
353 #define I2C_0_ISR (isr_i2c0)
354 #define I2C_1_ISR (isr_i2c1)
357 #ifdef __cplusplus
358 }
359 #endif
360 
@ PORT_B
port B
Definition: periph_cpu.h:44
@ PORT_C
port C
Definition: periph_cpu.h:45
@ PORT_E
port E
Definition: periph_cpu.h:47
@ PORT_A
port A
Definition: periph_cpu.h:43
@ PORT_D
port D
Definition: periph_cpu.h:46
#define GPIO_PIN(x, y)
Define a CPU specific GPIO pin generator macro.
Definition: periph_cpu.h:42
#define GPIO_UNDEF
Definition of a fitting UNDEF value.
static const uart_conf_t uart_config[]
UART configuration.
Definition: periph_conf.h:35
static const spi_conf_t spi_config[]
SPI configuration.
Definition: periph_conf.h:93
static const i2c_conf_t i2c_config[]
I2C configuration.
Definition: periph_conf.h:65
static const adc_conf_t adc_config[]
ADC configuration.
Definition: periph_conf.h:247
static const pwm_conf_t pwm_config[]
Actual PWM configuration.
Definition: periph_conf.h:218
static const dac_conf_t dac_config[]
DAC configuration.
Definition: periph_conf.h:249
static const spi_clk_conf_t spi_clk_config[]
Pre-calculated clock divider values based on a CLOCK_CORECLOCK (32MHz)
Definition: periph_cpu.h:278
#define SPI_CS_UNDEF
Define value for unused CS line.
Definition: periph_cpu.h:362
#define UART0
UART0 register bank.
#define UART1
UART1 register bank.
#define CLOCK_BUSCLOCK
Bus clock frequency, used by several hardware modules in Kinetis CPUs.
Definition: mcg.h:146
#define CLOCK_CORECLOCK
System core clock in Hz.
Definition: periph_conf.h:34
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:275
#define ADC_AVG_NONE
Disable hardware averaging.
Definition: periph_cpu.h:369
@ KINETIS_UART
Kinetis UART module type.
Definition: periph_cpu.h:537
@ UART_MODE_8N1
8 data bits, no parity, 1 stop bit
Definition: periph_cpu.h:293
#define ADC_AVG_MAX
Maximum hardware averaging (32 samples)
Definition: periph_cpu.h:373
ADC device configuration.
Definition: periph_cpu.h:374
ADC_TypeDef * dev
ADC device used.
Definition: periph_cpu.h:375
DAC line configuration data.
Definition: periph_cpu.h:300
I2C configuration structure.
Definition: periph_cpu.h:295
I2C_Type * i2c
Pointer to hardware module registers.
Definition: periph_cpu.h:458
gpio_t pin
GPIO pin mapped to this channel.
Definition: periph_cpu.h:469
PWM device configuration.
pwm_chan_t chan[TIMER_CHANNEL_NUMOF]
channel mapping set to {GPIO_UNDEF, 0} if not used
Definition: periph_cpu.h:482
SPI device configuration.
Definition: periph_cpu.h:333
SPI_t * dev
pointer to the used SPI device
Definition: periph_cpu.h:334
UART device configuration.
Definition: periph_cpu.h:214
USART_t * dev
pointer to the used UART device
Definition: periph_cpu.h:215