Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014-2017 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
123
124#include <stdint.h>
125#include <stddef.h>
126#include <limits.h>
127
128#include "periph_conf.h"
129#include "periph_cpu.h"
130
131#ifdef __cplusplus
132extern "C" {
133#endif
134
139#ifndef I2C_DEV
140#define I2C_DEV(x) (x)
141#endif
143
148#ifndef I2C_UNDEF
149#define I2C_UNDEF (UINT_FAST8_MAX)
150#endif
152
157#ifndef HAVE_I2C_T
158typedef uint_fast8_t i2c_t;
159#endif
161
165#define I2C_READ (0x0001)
166
175#define I2C_10BIT_MAGIC (0xF0u)
176
181#ifndef HAVE_I2C_SPEED_T
189#endif
191
196#ifndef HAVE_I2C_FLAGS_T
197typedef enum {
198 I2C_ADDR10 = 0x01,
199 I2C_REG16 = 0x02,
200 I2C_NOSTOP = 0x04,
201 I2C_NOSTART = 0x08,
203#endif
205
222void i2c_init(i2c_t dev);
223
236
258
259#if DOXYGEN /* functions to be implemented as `#define` in `periph_cpu.h` */
270gpio_t i2c_pin_sda(i2c_t dev);
271
282gpio_t i2c_pin_scl(i2c_t dev);
283#endif /* DOXYGEN */
284
297
304
329
330int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg,
331 void *data, uint8_t flags);
332
358int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg,
359 void *data, size_t len, uint8_t flags);
360
382
383int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags);
384
407
408int i2c_read_bytes(i2c_t dev, uint16_t addr,
409 void *data, size_t len, uint8_t flags);
410
429int i2c_write_byte(i2c_t dev, uint16_t addr, uint8_t data, uint8_t flags);
430
450int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
451 size_t len, uint8_t flags);
452
477int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg,
478 uint8_t data, uint8_t flags);
479
504int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg,
505 const void *data, size_t len, uint8_t flags);
506
507#ifdef __cplusplus
508}
509#endif
510
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_HIGH
high speed mode: ~3400 kbit/s
Definition periph_cpu.h:278
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition periph_cpu.h:275
int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg, uint8_t data, uint8_t flags)
Convenience function for writing one byte to a given register address.
int i2c_write_byte(i2c_t dev, uint16_t addr, uint8_t data, uint8_t flags)
Convenience function for writing a single byte onto the bus.
int i2c_read_bytes(i2c_t dev, uint16_t addr, void *data, size_t len, uint8_t flags)
Convenience function for reading bytes from a device.
void i2c_init(i2c_t dev)
Initialize the given I2C bus.
int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data, size_t len, uint8_t flags)
Convenience function for writing several bytes onto the bus.
void i2c_deinit_pins(i2c_t dev)
Change the pins of the given I2C bus back to plain GPIO functionality.
uint_fast8_t i2c_t
Default i2c_t type definition.
Definition i2c.h:158
int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg, void *data, size_t len, uint8_t flags)
Convenience function for reading several bytes from a given register address.
int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags)
Convenience function for reading one byte from a device.
void i2c_release(i2c_t dev)
Release the given I2C device to be used by others.
i2c_flags_t
I2C transfer flags.
Definition i2c.h:197
void i2c_init_pins(i2c_t dev)
Initialize the used I2C bus pins.
void i2c_acquire(i2c_t dev)
Get mutually exclusive access to the given I2C bus.
int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg, void *data, uint8_t flags)
Convenience function for reading one byte from a given register address.
int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg, const void *data, size_t len, uint8_t flags)
Convenience function for writing data to a given register address.
@ I2C_ADDR10
use 10-bit device addressing
Definition i2c.h:198
@ I2C_REG16
use 16-bit register addressing, big-endian
Definition i2c.h:199
@ I2C_NOSTOP
do not issue a STOP condition after transfer
Definition i2c.h:200
@ I2C_NOSTART
skip START sequence, ignores address field
Definition i2c.h:201
#define i2c_pin_sda(dev)
Macro for getting SDA pin.
#define i2c_pin_scl(dev)
Macro for getting SCL pin.