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 #include "periph/dev_enums.h"
133 
138 #ifndef I2C_DEV
139 #define I2C_DEV(x) (x)
140 #endif
141 
147 #ifndef I2C_UNDEF
148 #define I2C_UNDEF (UINT_MAX)
149 #endif
150 
156 #ifndef HAVE_I2C_T
157 typedef unsigned int i2c_t;
158 #endif
159 
164 #define I2C_READ (0x0001)
165 
174 #define I2C_10BIT_MAGIC (0xF0u)
175 
180 #ifndef HAVE_I2C_SPEED_T
181 typedef enum {
187 } i2c_speed_t;
188 #endif
189 
195 #ifndef HAVE_I2C_FLAGS_T
196 typedef enum {
197  I2C_ADDR10 = 0x01,
198  I2C_REG16 = 0x02,
199  I2C_NOSTOP = 0x04,
200  I2C_NOSTART = 0x08,
201 } i2c_flags_t;
202 #endif
203 
216 void i2c_init(i2c_t dev);
217 
228 int i2c_acquire(i2c_t dev);
229 
237 int i2c_release(i2c_t dev);
238 
262 int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg,
263  void *data, uint8_t flags);
264 
288 int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg,
289  void *data, size_t len, uint8_t flags);
290 
311 int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags);
312 
334 int i2c_read_bytes(i2c_t dev, uint16_t addr,
335  void *data, size_t len, uint8_t flags);
336 
353 int i2c_write_byte(i2c_t dev, uint16_t addr, uint8_t data, uint8_t flags);
354 
372 int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
373  size_t len, uint8_t flags);
374 
397 int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg,
398  uint8_t data, uint8_t flags);
399 
422 int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg,
423  const void *data, size_t len, uint8_t flags);
424 
425 
426 #ifdef __cplusplus
427 }
428 #endif
429 
430 #endif /* PERIPH_I2C_H */
431 
void i2c_init(i2c_t dev)
Initialize the given I2C bus.
fast mode: ~400 kbit/s
Definition: i2c.h:184
high speed mode: ~3400 kbit/s
Definition: i2c.h:186
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_release(i2c_t dev)
Release the given I2C device to be used by others.
int i2c_acquire(i2c_t dev)
Get mutually exclusive access to the given I2C 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.
i2c_speed_t
Default mapping of I2C bus speed values.
Definition: i2c.h:181
unsigned int i2c_t
Default i2c_t type definition.
Definition: i2c.h:157
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.
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_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.
Device enumerations for backward compatibility with existing peripheral driver implementations.
use 10-bit device addressing
Definition: i2c.h:197
skip START sequence, ignores address field
Definition: i2c.h:200
use 16-bit register addressing
Definition: i2c.h:198
low speed mode: ~10 kbit/s
Definition: i2c.h:182
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.
normal mode: ~100 kbit/s
Definition: i2c.h:183
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_flags_t
I2C transfer flags.
Definition: i2c.h:196
int i2c_read_byte(i2c_t dev, uint16_t addr, void *data, uint8_t flags)
Convenience function for reading one byte from a device.
fast plus mode: ~1000 kbit/s
Definition: i2c.h:185
do not issue a STOP condition after transfer
Definition: i2c.h:199