/*
 * soft_i2c.h
 */

#ifndef USER_COMM_SOFT_I2C_MASTER_H_
#define USER_COMM_SOFT_I2C_MASTER_H_

#include <CH59x_common.h>
#include <stdint.h>



#define SDA_PIN         4
#define SDA_BIT         (1 << (SDA_PIN % 8))
#define SDA_DIR_REG     R8_PA_DIR_0
#define SDA_DRV_REG     R8_PA_PD_DRV_0
#define SDA_CLR_REG     R8_PA_CLR_0
#define SDA_GET_REG     R8_PA_PIN_0

#define SCL_PIN         12
#define SCL_BIT         (1 << (SCL_PIN % 8))
#define SCL_DRV_REG     R8_PB_PD_DRV_1
#define SCL_DIR_REG     R8_PB_DIR_1
#define SCL_CLR_REG     R8_PB_CLR_1
#define SCL_GET_REG     R8_PB_PIN_1


#define SDA_IN_HI()     { SDA_DRV_REG &= ~SDA_BIT; SDA_DIR_REG &= ~SDA_BIT; }
#define SDA_OUTLO()     { SDA_DIR_REG |=  SDA_BIT; SDA_DRV_REG |=  SDA_BIT; }
#define SDA_SET_LO()    SDA_CLR_REG = SDA_BIT
#define SDA_GET()       ( SDA_GET_REG & SDA_BIT )

#define SCL_IN_HI()     { SCL_DRV_REG &= ~SCL_BIT; SCL_DIR_REG &= ~SCL_BIT; }
#define SCL_OUTLO()     { SCL_DIR_REG |=  SCL_BIT; SCL_DRV_REG |=  SCL_BIT; }
#define SCL_SET_LO()    SCL_CLR_REG = SCL_BIT
#define SCL_GET()       ( SCL_GET_REG & SCL_BIT )



void i2cm_init();

void i2cm_start();
void i2cm_restart();
void i2cm_stop();

uint8_t i2cm_rd(uint8_t ack);
uint8_t i2cm_wr(uint8_t dat);
uint8_t i2cm_addr(uint8_t addr, uint8_t reading_bit);

void i2cm_rdbuf(uint8_t *dat, uint8_t len);
void i2cm_wrbuf(const uint8_t *dat, uint8_t len);



#endif /* USER_COMM_SOFT_I2C_MASTER_H_ */
