periph_conf.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017 Thomas Stilwell <stilwellt@openlabs.co>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
19 #include "periph_cpu.h"
20 
21 #ifdef __cplusplus
22 extern "C"
23 {
24 #endif
25 
30 static const clock_config_t clock_config = {
31  /*
32  * This configuration results in the system running with the internal clock
33  * with the following clock frequencies:
34  * Core: 48 MHz
35  * Bus: 24 MHz
36  * Flash: 24 MHz
37  */
38 
39  .clkdiv1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(1),
40  .rtc_clc = RTC_CR_SC16P_MASK | RTC_CR_SC4P_MASK | RTC_CR_SC2P_MASK,
41 
42  /* Use the 32 kHz oscillator as ERCLK32K. Note that the values here have a
43  * different mapping for the KW41Z than the values used in the Kinetis
44  * K series */
45  .osc32ksel = SIM_SOPT1_OSC32KSEL(0),
46 
47  /* enable clocks */
48  .clock_flags =
49  KINETIS_CLOCK_OSC0_EN | /* Enable RSIM oscillator */
50  KINETIS_CLOCK_RTCOSC_EN |
51  KINETIS_CLOCK_USE_FAST_IRC |
52  KINETIS_CLOCK_MCGIRCLK_EN | /* Used for LPUART clocking */
53  KINETIS_CLOCK_MCGIRCLK_STOP_EN |
54  0,
55 
56  /* Using FEI mode by default, the external crystal settings below are only
57  * used if mode is changed to an external mode (PEE, FBE, or FEE) */
58  .default_mode = KINETIS_MCG_MODE_FEI,
59 
60  /* The crystal connected to RSIM OSC is 32 MHz */
61  .erc_range = KINETIS_MCG_ERC_RANGE_VERY_HIGH,
62 
63  .osc_clc = 0, /* not used by kw41z */
64  .oscsel = MCG_C7_OSCSEL(0), /* Use RSIM for external clock */
65  .fcrdiv = MCG_SC_FCRDIV(0), /* Fast IRC divide by 1 => 4 MHz */
66 
67  .fll_frdiv = MCG_C1_FRDIV(0b101), /* Divide by 1024 */
68  .fll_factor_fei = KINETIS_MCG_FLL_FACTOR_1464, /* FEI FLL freq = 48 MHz */
69  .fll_factor_fee = KINETIS_MCG_FLL_FACTOR_1280, /* FEE FLL freq = 40 MHz */
70 };
71 /* Radio xtal frequency, either 32 MHz or 26 MHz */
72 #define CLOCK_RADIOXTAL (32000000ul)
73 /* CPU core clock, the MCG clock output frequency */
74 #define CLOCK_CORECLOCK (48000000ul)
75 #define CLOCK_BUSCLOCK (CLOCK_CORECLOCK / 2)
76 #define CLOCK_MCGFLLCLK (CLOCK_CORECLOCK)
77 #define CLOCK_OSCERCLK (CLOCK_RADIOXTAL)
78 #define CLOCK_MCGIRCLK (4000000ul)
85 #define PIT_NUMOF (1U)
86 #define PIT_CONFIG { \
87  { \
88  .prescaler_ch = 0, \
89  .count_ch = 1, \
90  }, \
91  }
92 #define LPTMR_NUMOF (1U)
93 #define LPTMR_CONFIG { \
94  { \
95  .dev = LPTMR0, \
96  .base_freq = 32768u, \
97  .src = 2, \
98  .irqn = LPTMR0_IRQn, \
99  }, \
100  }
101 #define TIMER_NUMOF ((PIT_NUMOF) + (LPTMR_NUMOF))
102 #define PIT_BASECLOCK (CLOCK_BUSCLOCK)
103 #define LPTMR_ISR_0 isr_lptmr0
110 #ifndef LPUART_0_SRC
111 #define LPUART_0_SRC 1
112 #endif
113 
114 #if (LPUART_0_SRC == 3)
115 /* Use MCGIRCLK (4 MHz internal reference - not available in KINETIS_PM_LLS) */
116 #define LPUART_0_CLOCK CLOCK_MCGIRCLK
117 #define UART_CLOCK_PM_BLOCKER KINETIS_PM_LLS
118 #define UART_MAX_UNCLOCKED_BAUDRATE 19200ul
119 #elif (LPUART_0_SRC == 2)
120 #define LPUART_0_CLOCK CLOCK_OSCERCLK
121 #elif (LPUART_0_SRC == 1)
122 /* Use CLOCK_MCGFLLCLK (48 MHz FLL output - not available in KINETIS_PM_STOP) */
123 #define LPUART_0_CLOCK CLOCK_MCGFLLCLK
124 #define UART_CLOCK_PM_BLOCKER KINETIS_PM_STOP
125 #define UART_MAX_UNCLOCKED_BAUDRATE 57600ul
126 #endif
127 
128 static const uart_conf_t uart_config[] = {
129  {
130  .dev = LPUART0,
131  .freq = LPUART_0_CLOCK,
132  .pin_rx = GPIO_PIN(PORT_C, 6),
133  .pin_tx = GPIO_PIN(PORT_C, 7),
134  .pcr_rx = PORT_PCR_MUX(4) | GPIO_IN_PU,
135  .pcr_tx = PORT_PCR_MUX(4),
136  .irqn = LPUART0_IRQn,
137  .scgc_addr = &SIM->SCGC5,
138  .scgc_bit = SIM_SCGC5_LPUART0_SHIFT,
139  .mode = UART_MODE_8N1,
140  .type = KINETIS_LPUART,
141 #ifdef MODULE_PERIPH_LLWU /* TODO remove ifdef after #11789 is merged */
142  .llwu_rx = LLWU_WAKEUP_PIN_PTC6,
143 #endif
144  },
145 };
146 #define UART_NUMOF ARRAY_SIZE(uart_config)
147 #define LPUART_0_ISR isr_lpuart0
154 static const adc_conf_t adc_config[] = {
155  /* ADC0_SE1 A0 */
156  [0] = { .dev = ADC0, .pin = GPIO_PIN(PORT_B, 1), .chan = 1, .avg = ADC_AVG_MAX },
157  /* ADC0_SE2 A1 */
158  [1] = { .dev = ADC0, .pin = GPIO_PIN(PORT_B, 2), .chan = 3, .avg = ADC_AVG_MAX },
159  /* ADC0_SE3 A2 */
160  [2] = { .dev = ADC0, .pin = GPIO_PIN(PORT_B, 3), .chan = 2, .avg = ADC_AVG_MAX },
161  /* ADC0_SE4 A3 */
162  [3] = { .dev = ADC0, .pin = GPIO_PIN(PORT_B, 18), .chan = 4, .avg = ADC_AVG_MAX },
163 
164  /* internal: temperature sensor */
165  /* The temperature sensor has a very high output impedance, it must not be
166  * sampled using hardware averaging, or the sampled values will be garbage */
167  [4] = { .dev = ADC0, .pin = GPIO_UNDEF, .chan = 26, .avg = ADC_AVG_NONE },
168  /* internal: band gap */
169  /* Note: the band gap buffer uses a bit of current and is turned off
170  * by default,
171  * Set PMC->REGSC |= PMC_REGSC_BGBE_MASK before reading or the input will
172  * be floating */
173  [5] = { .dev = ADC0, .pin = GPIO_UNDEF, .chan = 27, .avg = ADC_AVG_MAX },
174  /* internal: DCDC divided battery level */
175  [6] = { .dev = ADC0, .pin = GPIO_UNDEF, .chan = 23, .avg = ADC_AVG_MAX },
176 };
177 
178 #define ADC_NUMOF ARRAY_SIZE(adc_config)
179 /*
180  * KW41Z ADC reference settings:
181  * 0: VREFH external pin or VREF_OUT 1.2 V signal (if VREF module is enabled)
182  * 1: VDDA (analog supply input voltage)
183  * 2-3: reserved
184  */
185 #define ADC_REF_SETTING 1
186 #if ADC_REF_SETTING
187 #define ADC_REF_VOLTAGE (3.3f)
188 #else
189 #define ADC_REF_VOLTAGE (1.2f)
190 #endif
191 
192 #define ADC_TEMPERATURE_CHANNEL (4)
199 static const dac_conf_t dac_config[] = {
200  {
201  /* PTB18 | ADC0_SE4 | A3 */
202  .dev = DAC0,
203  .scgc_addr = &SIM->SCGC6,
204  .scgc_bit = SIM_SCGC6_DAC0_SHIFT,
205  },
206 };
207 
208 #define DAC_NUMOF ARRAY_SIZE(dac_config)
215 #define HAVE_PWM_MODE_T
216 typedef enum {
217  PWM_LEFT = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK),
218  PWM_RIGHT = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK),
219  PWM_CENTER = (TPM_CnSC_MSB_MASK)
221 
225 #define PWM_CHAN_MAX (4U)
226 typedef struct {
227  TPM_Type *tpm;
228  struct {
229  gpio_t pin;
230  uint8_t af;
231  uint8_t ftm_chan;
232  } chan[PWM_CHAN_MAX];
233  uint8_t chan_numof;
234  uint8_t tpm_num;
235 } pwm_conf_t;
242 static const pwm_conf_t pwm_config[] = {
243  {
244  .tpm = TPM0,
245  .chan = {
246  { .pin = GPIO_PIN(PORT_B, 0), .af = 5, .ftm_chan = 1 }
247  },
248  .chan_numof = 1,
249  .tpm_num = 0
250  },
251  {
252  .tpm = TPM1,
253  .chan = {
254  { .pin = GPIO_PIN(PORT_C, 4), .af = 5, .ftm_chan = 0 }
255  },
256  .chan_numof = 1,
257  .tpm_num = 1
258  }
259 };
260 
261 #define PWM_NUMOF ARRAY_SIZE(pwm_config)
274 static const uint32_t spi_clk_config[] = {
275  (
276  SPI_CTAR_PBR(2) | SPI_CTAR_BR(5) | /* -> 100000Hz */
277  SPI_CTAR_PCSSCK(2) | SPI_CTAR_CSSCK(4) |
278  SPI_CTAR_PASC(2) | SPI_CTAR_ASC(4) |
279  SPI_CTAR_PDT(2) | SPI_CTAR_DT(4)
280  ),
281  (
282  SPI_CTAR_PBR(2) | SPI_CTAR_BR(3) | /* -> 400000Hz */
283  SPI_CTAR_PCSSCK(2) | SPI_CTAR_CSSCK(2) |
284  SPI_CTAR_PASC(2) | SPI_CTAR_ASC(2) |
285  SPI_CTAR_PDT(2) | SPI_CTAR_DT(2)
286  ),
287  (
288  SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | /* -> 1000000Hz */
289  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(3) |
290  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(3) |
291  SPI_CTAR_PDT(0) | SPI_CTAR_DT(3)
292  ),
293  (
294  SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | /* -> 4000000Hz */
295  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(1) |
296  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(1) |
297  SPI_CTAR_PDT(0) | SPI_CTAR_DT(1)
298  ),
299  (
300  SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | /* -> 4000000Hz */
301  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(0) |
302  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(0) |
303  SPI_CTAR_PDT(0) | SPI_CTAR_DT(0)
304  )
305 };
306 
307 static const spi_conf_t spi_config[] = {
308  {
309  .dev = SPI0,
310  .pin_miso = GPIO_PIN(PORT_C, 18),
311  .pin_mosi = GPIO_PIN(PORT_C, 17),
312  .pin_clk = GPIO_PIN(PORT_C, 16),
313  .pin_cs = {
314  GPIO_PIN(PORT_C, 19),
315  SPI_CS_UNDEF,
316  SPI_CS_UNDEF,
317  SPI_CS_UNDEF,
318  SPI_CS_UNDEF,
319  },
320  .pcr = (gpio_pcr_t)(GPIO_AF_2 | GPIO_IN_PU),
321  .simmask = SIM_SCGC6_SPI0_MASK
322  },
323 };
324 
325 #define SPI_NUMOF ARRAY_SIZE(spi_config)
332 static const i2c_conf_t i2c_config[] = {
333  {
334  .i2c = I2C1,
335  .scl_pin = GPIO_PIN(PORT_C, 2),
336  .sda_pin = GPIO_PIN(PORT_C, 3),
337  .freq = CLOCK_CORECLOCK,
338  .speed = I2C_SPEED_FAST,
339  .irqn = I2C1_IRQn,
340  .scl_pcr = (PORT_PCR_MUX(3)),
341  .sda_pcr = (PORT_PCR_MUX(3)),
342  },
343 };
344 #define I2C_NUMOF ARRAY_SIZE(i2c_config)
345 #define I2C_0_ISR (isr_i2c1)
352 #define KINETIS_TRNG TRNG
355 #ifdef __cplusplus
356 }
357 #endif
358 
@ PORT_B
port B
Definition: periph_cpu.h:44
@ PORT_C
port C
Definition: periph_cpu.h:45
#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
pwm_mode_t
Definition: periph_conf.h:216
#define PWM_CHAN_MAX
PWM configuration structure.
Definition: periph_conf.h:225
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 CLOCK_CORECLOCK
System core clock in Hz.
Definition: periph_conf.h:34
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:275
@ PWM_CENTER
center aligned
Definition: periph_conf.h:219
@ PWM_LEFT
left aligned
Definition: periph_conf.h:217
@ PWM_RIGHT
right aligned
Definition: periph_conf.h:218
#define ADC_AVG_NONE
Disable hardware averaging.
Definition: periph_cpu.h:369
@ KINETIS_LPUART
Kinetis Low-power UART (LPUART) module type.
Definition: periph_cpu.h:538
@ 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
PWM device configuration.
uint8_t ftm_chan
the actual FTM channel used
Definition: periph_conf.h:231
TPM_Type * tpm
used TPM
Definition: periph_conf.h:227
uint8_t af
alternate function mapping
Definition: periph_conf.h:230
gpio_t pin
GPIO pin used, set to GPIO_UNDEF.
Definition: periph_conf.h:229
uint8_t tpm_num
FTM number used.
Definition: periph_conf.h:234
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