Loading...
Searching...
No Matches
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
19
20#include <stdint.h>
21
22#include "cpu.h"
23#include "periph/cpu_gpio.h"
24
25#ifdef __cplusplus
26extern "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
46
47#ifndef DOXYGEN
52#define HAVE_I2C_SPEED_T
53typedef 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) || \
67 defined(CPU_FAM_STM32H7)
69#endif
72#endif /* ndef DOXYGEN */
73
77typedef struct {
78 I2C_TypeDef *dev;
79 i2c_speed_t speed;
80 gpio_t scl_pin;
81 gpio_t sda_pin;
82#ifndef CPU_FAM_STM32F1
85#endif
86 uint8_t bus;
87 uint32_t rcc_mask;
88#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
89 defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32G0) || \
90 defined(CPU_FAM_STM32G4) || defined(CPU_FAM_STM32L4) || \
91 defined(CPU_FAM_STM32L5) || defined(CPU_FAM_STM32WB) || \
92 defined(CPU_FAM_STM32U5) || defined(CPU_FAM_STM32WL) || \
93 defined(CPU_FAM_STM32C0) || defined(CPU_FAM_STM32H7)
94 uint32_t rcc_sw_mask;
95#endif
96#if defined(CPU_FAM_STM32F1) || defined(CPU_FAM_STM32F2) || \
97 defined(CPU_FAM_STM32F4) || defined(CPU_FAM_STM32L1) || \
98 defined(CPU_FAM_STM32MP1)
99 uint32_t clk;
100#endif
101 uint8_t irqn;
102} i2c_conf_t;
103
104#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
105 defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32L0) || \
106 defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32L5) || \
107 defined(CPU_FAM_STM32G0) || defined(CPU_FAM_STM32G4) || \
108 defined(CPU_FAM_STM32U5) || defined(CPU_FAM_STM32WB) || \
109 defined(CPU_FAM_STM32WL) || defined(CPU_FAM_STM32C0) || \
110 defined(CPU_FAM_STM32H7)
114typedef struct {
115 uint8_t presc;
116 uint8_t scll;
117 uint8_t sclh;
118 uint8_t sdadel;
119 uint8_t scldel;
120} i2c_timing_param_t;
121
136static const i2c_timing_param_t timing_params[] = {
137# if defined (CPU_FAM_STM32H7) /* HSI input clock 64MHz */
138 [ I2C_SPEED_NORMAL ] = {
139 .presc = 4,
140 .scll = 0x3F, /* t_SCLL = 5.0us */
141 .sclh = 0x33, /* t_SCLH = 4.0us */
142 .sdadel = 0x1, /* t_SDADEL = 500ns */
143 .scldel = 0xF, /* t_SCLDEL = 1250ns */
144 }, /* 0x4F10333F */
145 [ I2C_SPEED_FAST ] = {
146 .presc = 1,
147 .scll = 0x27, /* t_SCLL = 1250ns */
148 .sclh = 0x0F, /* t_SCLH = 500ns */
149 .sdadel = 0x1, /* t_SDADEL = 250ns */
150 .scldel = 0xC, /* t_SCLDEL = 500ns */
151 }, /* 0x1C100F27 */
152 [ I2C_SPEED_FAST_PLUS ] = {
153 .presc = 1,
154 .scll = 0x09, /* t_SCLL = 312.5ns */
155 .sclh = 0x05, /* t_SCLH = 187.5ns */
156 .sdadel = 0x1, /* t_SDADEL = 0ns */
157 .scldel = 0xC, /* t_SCLDEL = 187.5ns */
158 }, /* 0x1C100509 */
159# else
160 [ I2C_SPEED_NORMAL ] = {
161 .presc = 3,
162 .scll = 0x13, /* t_SCLL = 5.0us */
163 .sclh = 0xF, /* t_SCLH = 4.0us */
164 .sdadel = 0x2, /* t_SDADEL = 500ns */
165 .scldel = 0x4, /* t_SCLDEL = 1250ns */
166 },
167 [ I2C_SPEED_FAST ] = {
168 .presc = 1,
169 .scll = 0x9, /* t_SCLL = 1250ns */
170 .sclh = 0x3, /* t_SCLH = 500ns */
171 .sdadel = 0x2, /* t_SDADEL = 250ns */
172 .scldel = 0x3, /* t_SCLDEL = 500ns */
173 },
174 [ I2C_SPEED_FAST_PLUS ] = {
175 .presc = 0,
176 .scll = 0x4, /* t_SCLL = 312.5ns */
177 .sclh = 0x2, /* t_SCLH = 187.5ns */
178 .sdadel = 0x0, /* t_SDADEL = 0ns */
179 .scldel = 0x2, /* t_SCLDEL = 187.5ns */
180 }
181# endif
182};
183#endif /* CPU_FAM_STM32F0 || CPU_FAM_STM32F3 || CPU_FAM_STM32F7 ||
184 CPU_FAM_STM32L0 || CPU_FAM_STM32L4 || CPU_FAM_STM32L5 ||
185 CPU_FAM_STM32G0 || CPU_FAM_STM32G4 || CPU_FAM_STM32U5 ||
186 CPU_FAM_STM32WB || CPU_FAM_STM32WL || CPU_FAM_STM32C0 ||
187 CPU_FAM_STM32H7 */
188
189#if defined(CPU_FAM_STM32F0) || defined(CPU_FAM_STM32F3) || \
190 defined(CPU_FAM_STM32F7) || defined(CPU_FAM_STM32G0) || \
191 defined(CPU_FAM_STM32G4) || defined(CPU_FAM_STM32L0) || \
192 defined(CPU_FAM_STM32L4) || defined(CPU_FAM_STM32L5) || \
193 defined(CPU_FAM_STM32U5) || defined(CPU_FAM_STM32WB) || \
194 defined(CPU_FAM_STM32WL) || defined(CPU_FAM_STM32C0) || \
195 defined(CPU_FAM_STM32H7)
200#define PERIPH_I2C_MAX_BYTES_PER_FRAME (256U)
201#endif
202
203#ifdef __cplusplus
204}
205#endif
206
i2c_speed_t
Definition periph_cpu.h:272
@ 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
GPIO CPU definitions for the STM32 family.
gpio_af_t
Override alternative GPIO mode options.
Definition periph_cpu.h:162
I2C configuration structure.
Definition periph_cpu.h:295
gpio_af_t scl_af
scl pin alternate function value
Definition cpu_i2c.h:83
gpio_af_t sda_af
sda pin alternate function value
Definition cpu_i2c.h:84
IRQn_Type irqn
I2C event interrupt number.
Definition periph_cpu.h:459
uint8_t bus
APB bus.
Definition cpu_i2c.h:86
uint32_t rcc_mask
bit in clock enable register
Definition cpu_i2c.h:87