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 
101 #include <stdint.h>
102 #include <stddef.h>
103 #include <limits.h>
104 
105 #include "periph_conf.h"
106 #include "periph_cpu.h"
107 
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111 
116 #ifndef I2C_DEV
117 #define I2C_DEV(x) (x)
118 #endif
125 #ifndef I2C_UNDEF
126 #define I2C_UNDEF (UINT_FAST8_MAX)
127 #endif
134 #ifndef HAVE_I2C_T
135 typedef uint_fast8_t i2c_t;
136 #endif
142 #define I2C_READ (0x0001)
143 
152 #define I2C_10BIT_MAGIC (0xF0u)
153 
158 #ifndef HAVE_I2C_SPEED_T
159 typedef enum {
165 } i2c_speed_t;
166 #endif
173 #ifndef HAVE_I2C_FLAGS_T
174 typedef enum {
175  I2C_ADDR10 = 0x01,
176  I2C_REG16 = 0x02,
177  I2C_NOSTOP = 0x04,
178  I2C_NOSTART = 0x08,
179 } i2c_flags_t;
180 #endif
199 void i2c_init(i2c_t dev);
200 
213 
235 
236 #if DOXYGEN /* functions to be implemented as `#define` in `periph_cpu.h` */
247 gpio_t i2c_pin_sda(i2c_t dev);
248 
259 gpio_t i2c_pin_scl(i2c_t dev);
260 #endif /* DOXYGEN */
261 
273 void i2c_acquire(i2c_t dev);
274 
280 void i2c_release(i2c_t dev);
281 
307 int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg,
308  void *data, uint8_t flags);
309 
335 int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg,
336  void *data, size_t len, uint8_t flags);
337 
360 int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags);
361 
385 int i2c_read_bytes(i2c_t dev, uint16_t addr,
386  void *data, size_t len, uint8_t flags);
387 
406 int i2c_write_byte(i2c_t dev, uint16_t addr, uint8_t data, uint8_t flags);
407 
427 int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
428  size_t len, uint8_t flags);
429 
454 int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg,
455  uint8_t data, uint8_t flags);
456 
481 int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg,
482  const void *data, size_t len, uint8_t flags);
483 
484 #ifdef __cplusplus
485 }
486 #endif
487 
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.
gpio_t i2c_pin_sda(i2c_t dev)
Get the SDA pin of the given I2C bus.
uint_fast8_t i2c_t
Default i2c_t type definition.
Definition: i2c.h:135
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.
i2c_speed_t
Default mapping of I2C bus speed values.
Definition: i2c.h:159
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:174
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.
gpio_t i2c_pin_scl(i2c_t dev)
Get the SCL pin of the given I2C bus.
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: i2c.h:161
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: i2c.h:163
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: i2c.h:160
@ I2C_SPEED_HIGH
high speed mode: ~3400 kbit/s
Definition: i2c.h:164
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: i2c.h:162
@ I2C_ADDR10
use 10-bit device addressing
Definition: i2c.h:175
@ I2C_REG16
use 16-bit register addressing, big-endian
Definition: i2c.h:176
@ I2C_NOSTOP
do not issue a STOP condition after transfer
Definition: i2c.h:177
@ I2C_NOSTART
skip START sequence, ignores address field
Definition: i2c.h:178