#ifndef I2C_H #define I2C_H #include "common.h" #include "atomic.h" #include "io.h" static inline uint32_t i2c_wait(void) { waitfor(I2C_IRQ); } static inline void i2c_send_byte(uint8_t byte, uint8_t ctl) { i2c_wait(); I2C_WDATA = (byte << 8) | I2C_NAK | ctl; } static inline uint32_t i2c_data_status(void) { i2c_wait(); return I2C_RDATA; } static inline bool i2c_acked(void) { return !(i2c_data_status() & I2C_NAK); } static inline uint8_t i2c_recv_byte(uint8_t ctl) { uint32_t rdata; i2c_wait(); I2C_WDATA = (~0xff) | ctl; return i2c_data_status() >> 8; } static inline void i2c_set_speed(unsigned int khz) { I2C_DIVISOR = ((CPU_HZ/4000)-1)/khz; } extern volatile bool _i2c_locked; static bool i2c_lock(void) { return !atomic_xchg(_i2c_locked, true); } static void i2c_unlock(void) { _i2c_locked = false; } #define I2C_ERR_BUSY (-1) #define I2C_ERR_CONFLICT (-2) #define I2C_ERR_NAK (-3) #define I2C_ERR_IO (-4) #define I2C_ERR_STUCK (-5) #define I2C_ERR_NODEV (-6) extern int i2c_send(uint8_t addr, const void *data, size_t len); extern int i2c_recv(uint8_t addr, void *data, size_t len); extern int i2c_send_recv(uint8_t addr, const void *odata, size_t olen, void *idata, size_t ilen); #endif /* I2C_H */