#ifndef HARDWARE_I2C_H_
#define HARDWARE_I2C_H_

typedef struct
{
	volatile unsigned cfg;
	volatile unsigned ctrl;
	volatile unsigned st;
	volatile unsigned addr;
	volatile unsigned prsc;
	volatile unsigned msk;
	volatile unsigned txFifo;
	volatile unsigned rxFifo;
	volatile unsigned txWords;
	volatile unsigned rxThreshold;
} I2cRegs;

#define I2C_CFG_FILT_DEPTH(_x)  (((_x) & 0x1FU) << 1)
#define I2C_CFG_EN_OV           (1U << 0)

#define I2C_CTRL_RST_RX_PNTRS   (1U << 7)
#define I2C_CTRL_RST_TX_RDP     (1U << 6)
#define I2C_CTRL_RST_TX_PNTRS   (1U << 5)
#define I2C_CTRL_ADDR_MOD_10    (1U << 4)
#define I2C_CTRL_ADDR_MOD_7     (0U << 4)
#define I2C_CTRL_ACK            (1U << 3)
#define I2C_CTRL_STOP           (1U << 2)
#define I2C_CTRL_START          (1U << 1)
#define I2C_CTRL_EN             (1U << 0)

#define I2C_ST_TXF_END_EMPTY    (1U << 16)
#define I2C_ST_RX_TH_PASS       (1U << 15)
#define I2C_ST_MODE_MST         (1U << 14)
#define I2C_ST_BUS_CLEAR        (1U << 13)
#define I2C_ST_SLV_RX           (1U << 12)
#define I2C_ST_SLV_TX           (1U << 11)
#define I2C_ST_TXF_EMPTY        (1U << 10)
#define I2C_ST_TXF_FULL         (1U << 9)
#define I2C_ST_RXF_NOT_EMPTY    (1U << 8)
#define I2C_ST_RXF_OV           (1U << 7)
#define I2C_ST_RXF_FULL         (1U << 6)
#define I2C_ST_ARB_LOST         (1U << 5)
#define I2C_ST_BUS_ERR          (1U << 4)
#define I2C_ST_ACK_FAIL         (1U << 3)
#define I2C_ST_BTF              (1U << 2)
#define I2C_ST_STOP             (1U << 1)
#define I2C_ST_START            (1U << 0)

#define I2C_PRSC_TRISE(_x)      (((_X) & 0x1FU) << 26)
#define I2C_PRSC_SLV_HLD(_x)    (((_x) & 0x3FFU) << 16)
#define I2C_PRSC_FM             (1U << 15)
#define I2C_PRSC_SM             (0U << 15)
#define I2C_PRSC_DUTY_16_9      (1U << 14)
#define I2C_PRSC_DUTY_2         (0U << 14)
#define I2C_PRSC_PRSC(_x)       ((_x) & 0xFFFU)

#define I2C_MSK_TXF_END_EMPTY   I2C_ST_TXF_END_EMPTY
#define I2C_MSK_RX_TH_PASS      I2C_ST_RX_TH_PASS
#define I2C_MSK_MODE_MST        I2C_ST_MODE_MST
#define I2C_MSK_BUS_CLEAR       I2C_ST_BUS_CLEAR
#define I2C_MSK_SLV_RX          I2C_ST_SLV_RX
#define I2C_MSK_SLV_TX          I2C_ST_SLV_TX
#define I2C_MSK_TXF_EMPTY       I2C_ST_TXF_EMPTY
#define I2C_MSK_TXF_FULL        I2C_ST_TXF_FULL
#define I2C_MSK_RXF_NOT_EMPTY   I2C_ST_RXF_NOT_EMPTY
#define I2C_MSK_RXF_OV          I2C_ST_RXF_OV
#define I2C_MSK_RXF_FULL        I2C_ST_RXF_FULL
#define I2C_MSK_ARB_LOST        I2C_ST_ARB_LOST
#define I2C_MSK_BUS_ERR         I2C_ST_BUS_ERR
#define I2C_MSK_ACK_FAIL        I2C_ST_ACK_FAIL
#define I2C_MSK_BTF             I2C_ST_BTF
#define I2C_MSK_STOP            I2C_ST_STOP
#define I2C_MSK_START           I2C_ST_START

#endif /* HARDWARE_I2C_H_ */
