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
26 extern "C"
27 {
28 #endif
29 
34 static 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)
83 
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
108 
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 
132 static 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
152 
158 static 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)
197 
203 static 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)
213 
219 #define HAVE_PWM_MODE_T
220 typedef 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)
224 } pwm_mode_t;
225 
229 #define PWM_CHAN_MAX (4U)
230 typedef 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;
240 
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)
265 
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  GPIO_UNDEF,
319  GPIO_UNDEF,
320  GPIO_UNDEF,
321  GPIO_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)
329 
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)
349 
355 #define KINETIS_TRNG TRNG
356 
358 #ifdef __cplusplus
359 }
360 #endif
361 
362 #endif /* PERIPH_CONF_H */
363 
PORT_C
@ PORT_C
port C
Definition: periph_cpu.h:38
CLOCK_CORECLOCK
#define CLOCK_CORECLOCK
GCLK reference speed.
Definition: periph_conf.h:32
spi_clk_config
static const spi_clk_conf_t spi_clk_config[]
Pre-calculated clock divider values based on a CLOCK_CORECLOCK (32MHz)
Definition: periph_cpu.h:260
pwm_mode_t
pwm_mode_t
Definition: periph_conf.h:220
adc_conf_t::dev
ADC_TypeDef * dev
ADC device used.
Definition: periph_cpu.h:75
pwm_conf_t::ftm_chan
uint8_t ftm_chan
the actual FTM channel used
Definition: periph_conf.h:235
UART_MODE_8N1
@ UART_MODE_8N1
8 data bits, no parity, 1 stop bit
Definition: periph_cpu.h:286
GPIO_IN_PU
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
i2c_conf_t::i2c
I2C_Type * i2c
Pointer to hardware module registers.
Definition: periph_cpu.h:451
pwm_conf_t
PWM device configuration.
Definition: periph_cpu_common.h:153
PWM_LEFT
@ PWM_LEFT
left aligned
Definition: periph_conf.h:221
KINETIS_LPUART
@ KINETIS_LPUART
Kinetis Low-power UART (LPUART) module type.
Definition: periph_cpu.h:508
GPIO_UNDEF
#define GPIO_UNDEF
Definition of a fitting UNDEF value.
Definition: periph_cpu_common.h:52
uart_conf_t
UART device configuration.
Definition: periph_cpu.h:166
pwm_conf_t::af
uint8_t af
alternate function mapping
Definition: periph_conf.h:234
dac_conf_t
DAC line configuration data.
Definition: periph_cpu.h:518
ADC_AVG_NONE
#define ADC_AVG_NONE
Disable hardware averaging.
Definition: periph_cpu.h:362
PWM_RIGHT
@ PWM_RIGHT
right aligned
Definition: periph_conf.h:222
uart_conf_t::dev
cc2538_uart_t * dev
pointer to the used UART device
Definition: periph_cpu.h:167
pwm_conf_t::tpm_num
uint8_t tpm_num
FTM number used.
Definition: periph_conf.h:238
adc_conf_t
ADC device configuration.
Definition: periph_cpu.h:74
pwm_conf_t::tpm
TPM_Type * tpm
used TPM
Definition: periph_conf.h:231
GPIO_PIN
#define GPIO_PIN(x, y)
Define a CPU specific GPIO pin generator macro.
Definition: periph_cpu.h:35
i2c_conf_t
I2C configuration options.
Definition: periph_cpu.h:128
I2C_SPEED_FAST
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: i2c.h:178
PORT_B
@ PORT_B
port B
Definition: periph_cpu.h:37
spi_conf_t::dev
SPI_Type * dev
SPI device to use.
Definition: periph_cpu.h:465
spi_conf_t
SPI configuration structure type.
Definition: periph_cpu.h:273
PWM_CENTER
@ PWM_CENTER
center aligned
Definition: periph_conf.h:223
ADC_AVG_MAX
#define ADC_AVG_MAX
Maximum hardware averaging (32 samples)
Definition: periph_cpu.h:366
PWM_CHAN_MAX
#define PWM_CHAN_MAX
PWM configuration structure.
Definition: periph_conf.h:229