/*
 * soft_i2c_slave.h
 *
 *  Created on: Oct 16, 2024
 *      Author: true
 */

#ifndef USER_COMM_SOFT_I2C_SLAVE_H_
#define USER_COMM_SOFT_I2C_SLAVE_H_



#include <stdint.h>


typedef struct I2CSoftSlave {
    uint8_t addr;
    uint8_t addr_last_seen;
    uint16_t bit_time;
    void   (*start_cb)();               // call on any new read/write operation
    uint8_t (*rd_cb)();                 // returns data
    int8_t  (*wr_cb)(uint8_t data);     // return 0 to ACK, all other NACK
} I2CSoftSlave;


enum I2CSoftSlave_Error {
    // standard responses
    I2CSS_OK,
    I2CSS_STOP,
    I2CSS_NACK,
    I2CSS_ADDR_NO_MATCH,
    // errors
    I2CSS_START_SCL_NOT_LOW         = -1,
    I2CSS_ADDR_READ_ERROR           = -2,   // could be timeout, start or stop condition detected
    I2CSS_ADDR_READ_TIMEOUT         = -3,
    I2CSS_ADDR_READ_LASTBIT_TIMEOUT = -4,
    I2CSS_ACK_ERROR                 = -5,   // could be timeout, start or stop condition detected
    I2CSS_ACK_TIMEOUT               = -6,
    I2CSS_READ_ERROR                = -7,
    I2CSS_READ_TIMEOUT              = -8,
    I2CSS_WRITE_ERROR               = -9,
    I2CSS_WRITE_TIMEOUT             = -10,
    I2CSS_NO_READ_FUNCTION          = -32,
    I2CSS_NO_WRITE_FUNCTION         = -33,
    I2CSS_SCL_TIMEOUT               = -126,
    I2CSS_BUS_BUSY                  = -127,
    I2CSS_START_STOP                = -128
};



// call this function with SDA falling low interrupt.
void i2css_init();
int8_t i2css_start(struct I2CSoftSlave *slave);



#endif /* USER_COMM_SOFT_I2C_SLAVE_H_ */
