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 
114 #ifndef PERIPH_I2C_H
115 #define PERIPH_I2C_H
116 
117 #include <stdint.h>
118 #include <stddef.h>
119 #include <limits.h>
120 
121 #include "periph_conf.h"
122 #include "periph_cpu.h"
123 
124 #ifdef __cplusplus
125 extern "C" {
126 #endif
127 
132 #ifndef I2C_DEV
133 #define I2C_DEV(x) (x)
134 #endif
135 
141 #ifndef I2C_UNDEF
142 #define I2C_UNDEF (UINT_MAX)
143 #endif
144 
150 #ifndef HAVE_I2C_T
151 typedef unsigned int i2c_t;
152 #endif
153 
158 #define I2C_READ (0x0001)
159 
168 #define I2C_10BIT_MAGIC (0xF0u)
169 
174 #ifndef HAVE_I2C_SPEED_T
175 typedef enum {
181 } i2c_speed_t;
182 #endif
183 
189 #ifndef HAVE_I2C_FLAGS_T
190 typedef enum {
191  I2C_ADDR10 = 0x01,
192  I2C_REG16 = 0x02,
193  I2C_NOSTOP = 0x04,
194  I2C_NOSTART = 0x08,
195 } i2c_flags_t;
196 #endif
197 
210 void i2c_init(i2c_t dev);
211 
223 void i2c_init_pins(i2c_t dev);
224 
245 void i2c_deinit_pins(i2c_t dev);
246 
247 #if DOXYGEN /* functions to be implemented as `#define` in `periph_cpu.h` */
248 
258 gpio_t i2c_pin_sda(i2c_t dev);
259 
270 gpio_t i2c_pin_scl(i2c_t dev);
271 #endif /* DOXYGEN */
272 
283 int i2c_acquire(i2c_t dev);
284 
290 void i2c_release(i2c_t dev);
291 
317 int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg,
318  void *data, uint8_t flags);
319 
345 int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg,
346  void *data, size_t len, uint8_t flags);
347 
370 int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags);
371 
395 int i2c_read_bytes(i2c_t dev, uint16_t addr,
396  void *data, size_t len, uint8_t flags);
397 
416 int i2c_write_byte(i2c_t dev, uint16_t addr, uint8_t data, uint8_t flags);
417 
437 int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
438  size_t len, uint8_t flags);
439 
464 int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg,
465  uint8_t data, uint8_t flags);
466 
491 int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg,
492  const void *data, size_t len, uint8_t flags);
493 
494 
495 #ifdef __cplusplus
496 }
497 #endif
498 
499 #endif /* PERIPH_I2C_H */
500 
i2c_read_byte
int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags)
Convenience function for reading one byte from a device.
i2c_init_pins
void i2c_init_pins(i2c_t dev)
Initialize the used I2C bus pins.
I2C_SPEED_LOW
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: i2c.h:176
i2c_read_reg
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.
i2c_acquire
int i2c_acquire(i2c_t dev)
Get mutually exclusive access to the given I2C bus.
I2C_SPEED_NORMAL
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: i2c.h:177
I2C_ADDR10
@ I2C_ADDR10
use 10-bit device addressing
Definition: i2c.h:191
i2c_t
unsigned int i2c_t
Default i2c_t type definition.
Definition: i2c.h:151
i2c_read_bytes
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.
I2C_NOSTOP
@ I2C_NOSTOP
do not issue a STOP condition after transfer
Definition: i2c.h:193
i2c_speed_t
i2c_speed_t
Default mapping of I2C bus speed values.
Definition: i2c.h:175
i2c_write_regs
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_write_reg
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.
I2C_REG16
@ I2C_REG16
use 16-bit register addressing, big-endian
Definition: i2c.h:192
i2c_read_regs
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_release
void i2c_release(i2c_t dev)
Release the given I2C device to be used by others.
I2C_NOSTART
@ I2C_NOSTART
skip START sequence, ignores address field
Definition: i2c.h:194
I2C_SPEED_FAST_PLUS
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: i2c.h:179
i2c_pin_scl
gpio_t i2c_pin_scl(i2c_t dev)
Get the SCL pin of the given I2C bus.
I2C_SPEED_FAST
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: i2c.h:178
i2c_pin_sda
gpio_t i2c_pin_sda(i2c_t dev)
Get the SDA pin of the given I2C bus.
i2c_write_bytes
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.
i2c_write_byte
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.
I2C_SPEED_HIGH
@ I2C_SPEED_HIGH
high speed mode: ~3400 kbit/s
Definition: i2c.h:180
i2c_init
void i2c_init(i2c_t dev)
Initialize the given I2C bus.
i2c_flags_t
i2c_flags_t
I2C transfer flags.
Definition: i2c.h:190
i2c_deinit_pins
void i2c_deinit_pins(i2c_t dev)
Change the pins of the given I2C bus back to plain GPIO functionality.