periph_conf.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017 Eistec AB
3  * SPDX-FileCopyrightText: 2021-2023 Hugues Larrive
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 static const clock_config_t clock_config = {
32  /*
33  * This configuration results in the system running from the FLL output with
34  * the following clock frequencies:
35  * Core: 60 MHz
36  * Bus: 30 MHz
37  * Flex: 20 MHz
38  * Flash: 20 MHz
39  */
40  .clkdiv1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) |
41  SIM_CLKDIV1_OUTDIV3(2) | SIM_CLKDIV1_OUTDIV4(2),
42  .rtc_clc = 0, /* External load caps on the FRDM-K22F board */
43  .osc32ksel = SIM_SOPT1_OSC32KSEL(2),
44  .clock_flags =
45  KINETIS_CLOCK_OSC0_EN |
46  KINETIS_CLOCK_RTCOSC_EN |
47  KINETIS_CLOCK_USE_FAST_IRC |
48  0,
49  .default_mode = KINETIS_MCG_MODE_FEE,
50  /* The crystal connected to OSC0 is 8 MHz */
51  .erc_range = KINETIS_MCG_ERC_RANGE_HIGH,
52  .osc_clc = 0, /* External load caps on the FRDM-K22F board */
53  .oscsel = MCG_C7_OSCSEL(0), /* Use OSC0 for external clock */
54  .fcrdiv = MCG_SC_FCRDIV(0), /* Fast IRC divide by 1 => 4 MHz */
55  .fll_frdiv = MCG_C1_FRDIV(0b011), /* Divide by 256 */
56  .fll_factor_fei = KINETIS_MCG_FLL_FACTOR_1464, /* FLL freq = 48 MHz */
57  .fll_factor_fee = KINETIS_MCG_FLL_FACTOR_1920, /* FLL freq = 60 MHz */
58  .pll_prdiv = MCG_C5_PRDIV0(0b00011), /* Divide by 4 */
59  .pll_vdiv = MCG_C6_VDIV0(0b00110), /* Multiply by 30 => PLL freq = 60 MHz */
60 };
61 #define CLOCK_CORECLOCK (60000000ul)
62 #define CLOCK_BUSCLOCK (CLOCK_CORECLOCK / 2)
69 #define PIT_NUMOF (2U)
70 #define PIT_CONFIG { \
71  { \
72  .prescaler_ch = 0, \
73  .count_ch = 1, \
74  }, \
75  { \
76  .prescaler_ch = 2, \
77  .count_ch = 3, \
78  }, \
79 }
80 #define LPTMR_NUMOF (1U)
81 #define LPTMR_CONFIG { \
82  { \
83  .dev = LPTMR0, \
84  .irqn = LPTMR0_IRQn, \
85  .src = 2, \
86  .base_freq = 32768u, \
87  }, \
88 }
89 #define TIMER_NUMOF ((PIT_NUMOF) + (LPTMR_NUMOF))
90 
91 #define PIT_BASECLOCK (CLOCK_BUSCLOCK)
92 #define PIT_ISR_0 isr_pit1
93 #define PIT_ISR_1 isr_pit3
94 #define LPTMR_ISR_0 isr_lptmr0
101 static const uart_conf_t uart_config[] = {
102  {
103  .dev = UART1,
104  .freq = CLOCK_CORECLOCK,
105  .pin_rx = GPIO_PIN(PORT_E, 1),
106  .pin_tx = GPIO_PIN(PORT_E, 0),
107  .pcr_rx = PORT_PCR_MUX(3),
108  .pcr_tx = PORT_PCR_MUX(3),
109  .irqn = UART1_RX_TX_IRQn,
110  .scgc_addr = &SIM->SCGC4,
111  .scgc_bit = SIM_SCGC4_UART1_SHIFT,
112  .mode = UART_MODE_8N1,
113  .type = KINETIS_UART,
114  },
115  {
116  .dev = UART0,
117  .freq = CLOCK_CORECLOCK,
118  .pin_rx = GPIO_PIN(PORT_D, 6),
119  .pin_tx = GPIO_PIN(PORT_D, 7),
120  .pcr_rx = PORT_PCR_MUX(3),
121  .pcr_tx = PORT_PCR_MUX(3),
122  .irqn = UART0_RX_TX_IRQn,
123  .scgc_addr = &SIM->SCGC4,
124  .scgc_bit = SIM_SCGC4_UART0_SHIFT,
125  .mode = UART_MODE_8N1,
126  .type = KINETIS_UART,
127  },
128 };
129 
130 #define UART_0_ISR (isr_uart1_rx_tx)
131 #define UART_1_ISR (isr_uart0_rx_tx)
132 
133 #define UART_NUMOF ARRAY_SIZE(uart_config)
140 static const adc_conf_t adc_config[] = {
141  /* dev, pin, channel */
142  [0] = { /* ADC0_DP0 */
143  .dev = ADC0, .pin = GPIO_UNDEF,
144  .chan = 0, .avg = ADC_AVG_MAX
145  },
146  [1] = { /* ADC0_DM0 */
147  .dev = ADC0, .pin = GPIO_UNDEF,
148  .chan = 19, .avg = ADC_AVG_MAX
149  },
150  [2] = { /* ADC1_DP0 */
151  .dev = ADC1, .pin = GPIO_UNDEF,
152  .chan = 0, .avg = ADC_AVG_MAX
153  },
154  [3] = { /* ADC1_DM0 */
155  .dev = ADC1, .pin = GPIO_UNDEF,
156  .chan = 19, .avg = ADC_AVG_MAX
157  },
158  [4] = { /* PTB0 (Arduino A0) */
159  .dev = ADC0, .pin = GPIO_PIN(PORT_B, 0),
160  .chan = 8, .avg = ADC_AVG_MAX
161  },
162  [5] = { /* PTB1 (Arduino A1) */
163  .dev = ADC0, .pin = GPIO_PIN(PORT_B, 1),
164  .chan = 9, .avg = ADC_AVG_MAX
165  },
166  [6] = { /* PTC1 (Arduino A2) */
167  .dev = ADC0, .pin = GPIO_PIN(PORT_C, 1),
168  .chan = 15, .avg = ADC_AVG_MAX
169  },
170  [7] = { /* PTC2 (Arduino A3) */
171  .dev = ADC0, .pin = GPIO_PIN(PORT_C, 2),
172  .chan = 4, .avg = ADC_AVG_MAX
173  },
174  /* internal: temperature sensor */
175  /* The temperature sensor has a very high output impedance, it must
176  * not be sampled using hardware averaging, or the sampled values
177  * will be garbage */
178  [8] = {
179  .dev = ADC0, .pin = GPIO_UNDEF,
180  .chan = 26, .avg = ADC_AVG_NONE
181  },
182  /* internal: band gap */
183  /* Note: the band gap buffer uses a bit of current and is turned off
184  * by default, set PMC->REGSC |= PMC_REGSC_BGBE_MASK before reading
185  * or the input will be floating */
186  [9] = {
187  .dev = ADC0, .pin = GPIO_UNDEF,
188  .chan = 27, .avg = ADC_AVG_MAX
189  },
190 };
191 
192 #define ADC_NUMOF ARRAY_SIZE(adc_config)
193 /*
194  * K22F ADC reference settings:
195  * 0: VREFH/VREFL external pin pair
196  * 1: VREF_OUT internal 1.2 V reference (VREF module must be enabled)
197  * 2-3: reserved
198  */
199 #define ADC_REF_SETTING 0
206 static const pwm_conf_t pwm_config[] = {
207  {
208  .ftm = FTM0,
209  .chan = {
210  { .pin = GPIO_PIN(PORT_A, 1), .af = 3, .ftm_chan = 6 },
211  { .pin = GPIO_PIN(PORT_A, 2), .af = 3, .ftm_chan = 7 },
212  { .pin = GPIO_PIN(PORT_D, 5), .af = 4, .ftm_chan = 5 },
213  },
214  .chan_numof = 3,
215  .ftm_num = 0
216  }
217 };
218 
219 #define PWM_NUMOF ARRAY_SIZE(pwm_config)
232 static const uint32_t spi_clk_config[] = {
233  (
234  SPI_CTAR_PBR(0) | SPI_CTAR_BR(8) | /* -> 93750Hz */
235  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(8) |
236  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(8) |
237  SPI_CTAR_PDT(0) | SPI_CTAR_DT(8)
238  ),
239  (
240  SPI_CTAR_PBR(0) | SPI_CTAR_BR(6) | /* -> 375000Hz */
241  SPI_CTAR_PCSSCK(0) | SPI_CTAR_CSSCK(6) |
242  SPI_CTAR_PASC(0) | SPI_CTAR_ASC(6) |
243  SPI_CTAR_PDT(0) | SPI_CTAR_DT(6)
244  ),
245  (
246  SPI_CTAR_PBR(1) | SPI_CTAR_BR(4) | /* -> 1000000Hz */
247  SPI_CTAR_PCSSCK(1) | SPI_CTAR_CSSCK(3) |
248  SPI_CTAR_PASC(1) | SPI_CTAR_ASC(3) |
249  SPI_CTAR_PDT(1) | SPI_CTAR_DT(3)
250  ),
251  (
252  SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | /* -> 4800000Hz */
253  SPI_CTAR_PCSSCK(2) | SPI_CTAR_CSSCK(0) |
254  SPI_CTAR_PASC(2) | SPI_CTAR_ASC(0) |
255  SPI_CTAR_PDT(2) | SPI_CTAR_DT(0)
256  ),
257  (
258  SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | /* -> 8000000Hz */
259  SPI_CTAR_PCSSCK(1) | SPI_CTAR_CSSCK(0) |
260  SPI_CTAR_PASC(1) | SPI_CTAR_ASC(0) |
261  SPI_CTAR_PDT(1) | SPI_CTAR_DT(0)
262  )
263 };
264 
265 static const spi_conf_t spi_config[] = {
266  {
267  .dev = SPI0,
268  .pin_miso = GPIO_PIN(PORT_D, 3),
269  .pin_mosi = GPIO_PIN(PORT_D, 2),
270  .pin_clk = GPIO_PIN(PORT_D, 1),
271  .pin_cs = {
272  GPIO_PIN(PORT_C, 4),
273  GPIO_PIN(PORT_D, 4),
274  SPI_CS_UNDEF,
275  SPI_CS_UNDEF,
277  },
278  .pcr = GPIO_AF_2,
279  .simmask = SIM_SCGC6_SPI0_MASK
280  }
281 };
282 
283 #define SPI_NUMOF ARRAY_SIZE(spi_config)
290 static const i2c_conf_t i2c_config[] = {
291  {
292  .i2c = I2C0,
293  .scl_pin = GPIO_PIN(PORT_B, 2),
294  .sda_pin = GPIO_PIN(PORT_B, 3),
295  .freq = CLOCK_BUSCLOCK,
296  .speed = I2C_SPEED_FAST,
297  .irqn = I2C0_IRQn,
298  .scl_pcr = (PORT_PCR_MUX(2) | PORT_PCR_ODE_MASK),
299  .sda_pcr = (PORT_PCR_MUX(2) | PORT_PCR_ODE_MASK),
300  },
301 };
302 #define I2C_NUMOF ARRAY_SIZE(i2c_config)
303 #define I2C_0_ISR (isr_i2c0)
304 #define I2C_1_ISR (isr_i2c1)
307 #ifdef __cplusplus
308 }
309 #endif
310 
@ 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 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
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