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