1 /* 2 * Copyright 2003, Thomas Kurschel. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _I2C_H 6 #define _I2C_H 7 8 9 #include <OS.h> 10 11 12 // timing for i2c bus 13 typedef struct i2c_timing { 14 // general timing as defined by standard 15 // (in microseconds for 100kHz/400kHz mode) 16 int buf; // bus free between start and stop (4.7/1.3) 17 int hd_sta; // hold time start condition (4.0/0.6) 18 int low; // low period of clock (4.7/1.3) 19 int high; // high period of clock (4.0/0.6) 20 int su_sta; // setup time of repeated start condition (4.7/0.6) 21 int hd_dat; // hold time data (5.0/- for CBUS, 0/0 for I2C) 22 int su_dat; // setup time data (0.250/0.100) 23 int r; // maximum raise time of clock and data signal (1.0/0.3) 24 int f; // maximum fall time of clock and data signal (0.3/0.3) 25 int su_sto; // setup time for stop condition (4.0/0.6) 26 27 // clock stretching limits, not part of i2c standard 28 int start_timeout; // max. delay of start condition 29 int byte_timeout; // max. delay of first bit of byte 30 int bit_timeout; // max. delay of one bit within a byte transmission 31 int ack_start_timeout; // max. delay of acknowledge start 32 33 // other timeouts, not part of i2c standard 34 int ack_timeout; // timeout of waiting for acknowledge 35 } i2c_timing; 36 37 38 struct i2c_bus; 39 40 // set signals on bus 41 typedef status_t (*i2c_set_signals)(void *cookie, int clock, int data); 42 // read signals from bus 43 typedef status_t (*i2c_get_signals)(void *cookie, int *clock, int *data); 44 // send/receive from bus 45 typedef status_t (*i2c_send_receive)(const struct i2c_bus *bus, uint32 slave_address, 46 const uint8 *writeBuffer, size_t writeLength, uint8 *readBuffer, 47 size_t readLength); 48 49 // i2c bus definition 50 typedef struct i2c_bus { 51 void *cookie; // user-defined cookie 52 // low-level 53 i2c_timing timing; 54 i2c_set_signals set_signals; // callback to set signals 55 i2c_get_signals get_signals; // callback to detect signals 56 // high-level 57 i2c_send_receive send_receive; 58 } i2c_bus; 59 60 61 #ifdef __cplusplus 62 extern "C" { 63 #endif 64 65 // send and receive data via i2c bus 66 status_t i2c_send_receive_callback(const i2c_bus *bus, uint32 slave_address, 67 const uint8 *writeBuffer, size_t writeLength, uint8 *readBuffer, 68 size_t readLength); 69 70 // fill <timing> with standard 100kHz bus timing 71 void i2c_get100k_timing(i2c_timing *timing); 72 73 // fill <timing> with standard 400kHz bus timing 74 // (as timing resolution is 1 microsecond, we cannot reach full speed!) 75 void i2c_get400k_timing(i2c_timing *timing); 76 77 #ifdef __cplusplus 78 } 79 #endif 80 81 #endif /* _I2C_H */ 82