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