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