/*
* Copyright (c) 2023-2028, SpaceTouch Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date          Author      Notes
* 2023-07-18    zhixiongl       the first version
*/


#ifndef _HAL_I2C_H
#define _HAL_I2C_H

#ifdef __cplusplus
extern "C"
{
#endif


#include "platform.h"


//#define DBG_HAL_I2C

#if defined(DBG_HAL_I2C)
#define HAL_I2C_LOG(fmt, ...)      rt_kprintf(fmt, ##__VA_ARGS__)
#else
#define HAL_I2C_LOG(fmt, ...)
#endif


typedef enum
{
    I2C_0 = 0,
    I2C_1,
    I2C_NUM,
} i2c_port_t;


typedef enum
{
    I2C_READ = 0,
    I2C_WRITE,
} i2c_event_t;


#ifdef BSP_USING_I2C0_SLAVE
typedef struct
{
    uint8_t   busy;

    uint16_t  len;
    uint8_t   buf[I2C0_SLAVE_MSG_LEN];

    uint16_t cur_cnt;
} i2c0_slave_msg_t;
extern i2c0_slave_msg_t i2c0_slave_msg_tx;
extern i2c0_slave_msg_t i2c0_slave_msg_rx;
#endif

#ifdef BSP_USING_I2C1_SLAVE
typedef struct
{
    uint8_t   busy;

    uint16_t  len;
    uint8_t   buf[I2C1_SLAVE_MSG_LEN];

    uint16_t cur_cnt;
} i2c1_slave_msg_t;
extern i2c1_slave_msg_t i2c1_slave_msg_tx;
extern i2c1_slave_msg_t i2c1_slave_msg_rx;
#endif

#define HAL_I2C_WR                0x0000
#define HAL_I2C_RD               (1u << 0)
#define HAL_I2C_ADDR_10BIT       (1u << 2)
#define HAL_I2C_NO_START         (1u << 4)
#define HAL_I2C_IGNORE_NACK      (1u << 5)
#define HAL_I2C_NO_READ_ACK      (1u << 6)
#define HAL_I2C_NO_STOP          (1u << 7)

#define HAL_I2C_RESTART          (1u << 8)


#define HAL_I2C_EOK                          0               /**< There is no error */
#define HAL_I2C_ERROR                        1               /**< A generic error happens */


int32_t hal_i2c_master_xfer(i2c_port_t i2c_port, uint16_t addr, uint8_t *buf, uint16_t len, uint16_t flags);
int32_t hal_i2c_init(i2c_port_t i2c_port);
int32_t hal_i2c_slave_init(i2c_port_t i2c_port, uint8_t device_addr);
void iic_set_callback(void (* pcall_back)(uint8_t *buff, uint32_t length, i2c_event_t event));
#ifdef __cplusplus
}
#endif

#endif

