/*
 * i2c0.h
 */

#ifndef I2C0_H_
#define I2C0_H_

#include "stdbool.h"
#include "stdint.h"

bool i2c_mcs_is_error_write_op(const uint32_t mcs);
bool i2c_mcs_is_error_read_op(const uint32_t mcs);

// init i2c0
// bFast = false -> 100kHz
// bFast = true -> 400kHz
void i2c0_init(bool bFast);

// try to unstick a bus
int i2c0_stuck_bus_release();

// read as a single transaction
// return 0 on error
int i2c0_read_buf(uint8_t i2c_addr, uint8_t* out_data, int length);

// write as single transaction
// return 0 on error
int i2c0_write_buf(uint8_t i2c_addr, uint8_t* data, int length);

// write, repeated start, read
// write_length > 0
// read_length may be 0
// return 0 on error
int i2c0_write_then_read(uint8_t i2c_addr, uint8_t* write_data, int write_length, uint8_t* read_data, int read_length);

// write as single transaction, for scatter-gather io
// send write1_data then write2_data
// write1_length > 0
// write2_length may be 0
// return 0 on error
int i2c0_write_then_write(uint8_t i2c_addr, uint8_t* write1_data, int write1_length, uint8_t* write2_data, int write2_length);

// write u8, repeated start, read u8
// return 0 on error
int i2c0_read_reg8(uint8_t i2c_addr, uint8_t reg_addr, uint8_t* out_data);
// write u8, repeated start, read u16
// return 0 on error
int i2c0_read_reg16(uint8_t i2c_addr, uint8_t reg_addr, uint16_t* out_data);

// write u8, write u8 as single transaction
// return 0 on error
int i2c0_write_reg8(uint8_t i2c_addr, uint8_t reg_addr, uint8_t data);

// write u8, write u16 as single transaction
// return 0 on error
int i2c0_write_reg16(uint8_t i2c_addr, uint8_t reg_addr, uint16_t data);

// scan bus, print found devices to console
// always returns 1
int i2c0_busScan(void);

// calls i2c0_busScan
void i2c0_probe(void);

#endif /* I2C0_H_ */
