cpu_i2c.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
3  * SPDX-FileCopyrightText: 2017 OTA keys S.A.
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
20 #include <stdint.h>
21 
22 #include "cpu.h"
23 #include "periph/cpu_gpio.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
34 #define PERIPH_I2C_NEED_READ_REG
36 #define PERIPH_I2C_NEED_WRITE_REG
38 #define PERIPH_I2C_NEED_READ_REGS
39 #if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
40  defined(CPU_FAM_STM32L1) || defined(CPU_FAM_STM32F4) || \
41  defined(CPU_FAM_STM32MP1)
43 #define PERIPH_I2C_NEED_WRITE_REGS
44 #endif
47 #ifndef DOXYGEN
52 #define HAVE_I2C_SPEED_T
53 typedef enum {
54 #if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
55  defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L1) || \
56  defined(CPU_FAM_STM32MP1)
58 #endif
61 #if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
62  defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32L0) || \
63  defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32WB) || \
64  defined(CPU_FAM_STM32G4) || defined(CPU_FAM_STM32G0) || \
65  defined(CPU_FAM_STM32L5) || defined(CPU_FAM_STM32U5) || \
66  defined(CPU_FAM_STM32WL) || defined(CPU_FAM_STM32C0)
68 #endif
69 } i2c_speed_t;
71 #endif /* ndef DOXYGEN */
72 
76 typedef struct {
77  I2C_TypeDef *dev;
78  i2c_speed_t speed;
79  gpio_t scl_pin;
80  gpio_t sda_pin;
81 #ifndef CPU_FAM_STM32F1
84 #endif
85  uint8_t bus;
86  uint32_t rcc_mask;
87 #if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
88  defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32G0) || \
89  defined(CPU_FAM_STM32G4) || defined(CPU_FAM_STM32L4) || \
90  defined(CPU_FAM_STM32L5) || defined(CPU_FAM_STM32WB) || \
91  defined(CPU_FAM_STM32U5) || defined(CPU_FAM_STM32WL) || \
92  defined(CPU_FAM_STM32C0)
93  uint32_t rcc_sw_mask;
94 #endif
95 #if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
96  defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L1) || \
97  defined(CPU_FAM_STM32MP1)
98  uint32_t clk;
99 #endif
100  uint8_t irqn;
101 } i2c_conf_t;
102 
103 #if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
104  defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32L0) || \
105  defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32L5) || \
106  defined(CPU_FAM_STM32G0) || defined(CPU_FAM_STM32G4) || \
107  defined(CPU_FAM_STM32U5) || defined(CPU_FAM_STM32WB) || \
108  defined(CPU_FAM_STM32WL) || defined(CPU_FAM_STM32C0)
112 typedef struct {
113  uint8_t presc;
114  uint8_t scll;
115  uint8_t sclh;
116  uint8_t sdadel;
117  uint8_t scldel;
118 } i2c_timing_param_t;
119 
133 static const i2c_timing_param_t timing_params[] = {
134  [ I2C_SPEED_NORMAL ] = {
135  .presc = 3,
136  .scll = 0x13, /* t_SCLL = 5.0us */
137  .sclh = 0xF, /* t_SCLH = 4.0us */
138  .sdadel = 0x2, /* t_SDADEL = 500ns */
139  .scldel = 0x4, /* t_SCLDEL = 1250ns */
140  },
141  [ I2C_SPEED_FAST ] = {
142  .presc = 1,
143  .scll = 0x9, /* t_SCLL = 1250ns */
144  .sclh = 0x3, /* t_SCLH = 500ns */
145  .sdadel = 0x2, /* t_SDADEL = 250ns */
146  .scldel = 0x3, /* t_SCLDEL = 500ns */
147  },
148  [ I2C_SPEED_FAST_PLUS ] = {
149  .presc = 0,
150  .scll = 0x4, /* t_SCLL = 312.5ns */
151  .sclh = 0x2, /* t_SCLH = 187.5ns */
152  .sdadel = 0x0, /* t_SDADEL = 0ns */
153  .scldel = 0x2, /* t_SCLDEL = 187.5ns */
154  }
155 };
156 #endif /* CPU_FAM_STM32F0 || CPU_FAM_STM32F3 || CPU_FAM_STM32F7 ||
157  CPU_FAM_STM32L0 || CPU_FAM_STM32L4 || CPU_FAM_STM32L5 ||
158  CPU_FAM_STM32G0 || CPU_FAM_STM32G4 || CPU_FAM_STM32U5 ||
159  CPU_FAM_STM32WB || CPU_FAM_STM32WL || CPU_FAM_STM32C0 */
160 
161 #if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
162  defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32G0) || \
163  defined(CPU_FAM_STM32G4) || defined(CPU_FAM_STM32L0) || \
164  defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32L5) || \
165  defined(CPU_FAM_STM32U5) || defined(CPU_FAM_STM32WB) || \
166  defined(CPU_FAM_STM32WL) || defined(CPU_FAM_STM32C0)
171 #define PERIPH_I2C_MAX_BYTES_PER_FRAME (256U)
172 #endif
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
i2c_speed_t
Definition: periph_cpu.h:272
GPIO CPU definitions for the STM32 family.
gpio_af_t
Override alternative GPIO mode options.
Definition: periph_cpu.h:162
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: periph_cpu.h:274
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: periph_cpu.h:276
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: periph_cpu.h:273
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:275
I2C configuration structure.
Definition: periph_cpu.h:295
gpio_af_t scl_af
scl pin alternate function value
Definition: cpu_i2c.h:82
gpio_af_t sda_af
sda pin alternate function value
Definition: cpu_i2c.h:83
uint8_t bus
APB bus.
Definition: cpu_i2c.h:85
uint32_t rcc_mask
bit in clock enable register
Definition: cpu_i2c.h:86
uint8_t irqn
I2C event interrupt number.
Definition: cpu_i2c.h:100