i2c.h 1.3 KB

123456789101112131415161718192021222324252627
  1. /* Bit-banged open-drain I2C master: SCL=PD3, SDA=PD2 (external pull-ups required) */
  2. #ifndef I2C_H
  3. #define I2C_H
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. enum { I2C_OK = 0, I2C_NACK_ADDR = 1, I2C_NACK_DATA = 2, I2C_BUS_ERR = 3 };
  7. void i2c_init(void);
  8. void i2c_set_delay(uint8_t d); /* 0 = fastest, each step adds ~0.375 us per SCL period */
  9. uint8_t i2c_lines(uint8_t pullup); /* diag: bit0=SCL, bit1=SDA level; pullup=1 enables weak internal pull-ups */
  10. uint8_t i2c_recover(void); /* clock out stuck slave, then STOP */
  11. /* wlen>0: write phase; rlen>0: read phase (repeated START if both);
  12. * wlen==0 && rlen==0: address probe. addr is the 7-bit address. */
  13. uint8_t i2c_xfer(uint8_t addr, const uint8_t *w, uint8_t wlen, uint8_t *r, uint8_t rlen);
  14. /* Split-phase access. i2c_start(): START (or repeated START) + address byte
  15. * (addr_rw = addr << 1 | R/W). A non-repeated start clears the error flag and
  16. * runs bus recovery if a line is stuck. Returns I2C_OK / I2C_NACK_ADDR / I2C_BUS_ERR. */
  17. uint8_t i2c_start(uint8_t addr_rw, bool repeated);
  18. uint8_t i2c_write(uint8_t b); /* I2C_OK / I2C_NACK_DATA / I2C_BUS_ERR */
  19. uint8_t i2c_read(bool ack); /* ack=false on the last byte */
  20. bool i2c_bus_error(void); /* clock-stretch timeout since last non-repeated start */
  21. void i2c_stop(void);
  22. #endif