/*
 * @Author       : stark1898y 1658608470@qq.com
 * @Date         : 2024-12-15 15:42:00
 * @LastEditors  : stark1898y 1658608470@qq.com
 * @LastEditTime : 2024-12-15 15:42:00
 * @FilePath     : \BLE_TYQ_CH592F - 副本\BSP\inc\bsp_i2c.h
 * @Description  :
 *
 * Copyright (c) 2024 by yzy, All Rights Reserved.
 */
#ifndef __BSP_I2C_H__
#define __BSP_I2C_H__

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#include "CONFIG.h"

#define GXHTC3C_ADDR        0x70
// #define GXHTC3C_7BIT_ADDR   0x38 // (GXHTC3C_ADDR >> 1)

// 供电电压VDD从0上升上电电压VPOR，芯片会进入空闲状态。然后应该通过发送命令让芯片进入休眠状态以降低芯片功耗
#define GXHTC3C_CMD_SLEEP      0xB098
// 当芯片处于休眠状态时，如果要进行其它的命令操作，需要发送唤醒命令
#define GXHTC3C_CMD_WAKEUP     0x3517

#define GXHTC3C_CMD_NORMAL_CLK_STRE_ON_TEMP_FRONT        0x7CA2
#define GXHTC3C_CMD_NORMAL_CLK_STRE_ON_HUMI_FRONT        0x5C24

#define GXHTC3C_CMD_NORMAL_CLK_STRE_OFF_TEMP_FRONT       0x7866
#define GXHTC3C_CMD_NORMAL_CLK_STRE_OFF_HUMI_FRONT       0x58E0

#define GXHTC3C_CMD_LOW_CLK_STRE_ON_TEMP_FRONT           0x6458
#define GXHTC3C_CMD_LOW_CLK_STRE_ON_HUMI_FRONT           0x44DE

#define GXHTC3C_CMD_LOW_CLK_STRE_OFF_TEMP_FRONT          0x609C
#define GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT          0x401A

#define GXHTC3C_CMD_SOFT_REST        0x805D

// 读传感器序列号
#define GXHTC3C_CMD_READ_ID          0xEFC8

#define I2C_BUFFER_LENGTH   32
#define I2C_READ      1
#define I2C_WRITE     0

typedef enum {
    I2C_READY,
    I2C_MRX,
    I2C_MTX,
    I2C_SRX,
    I2C_STX,
}i2c_state_t;

typedef enum {
    I2C_NO_MEM = 1,
    I2C_STATE,
    I2C_MT_NACK,
    I2C_ARB_LOST,
    I2C_BUS_ERROR,
    I2C_OVR,
    I2C_PECERR,
    I2C_TIMEOUT,
    I2C_SMBALERT,
}i2c_error_t;

/**
 * @brief   User callback function on I2C slave transmitting.
 *
 * @param data  Pointer to user data to transmit.
 *
 * @param len   Pointer to user data length.
 */
typedef void (*i2c_on_slave_transmit)(uint8_t *data, uint8_t *len);

/**
 * @brief   User callback function on I2C slave received.
 *
 * @param data  Pointer to current received data.
 *
 * @param len  Received data length.
 */
typedef void (*i2c_on_slave_receive)(uint8_t *data, uint8_t len);

struct i2c_slave_cb {
    i2c_on_slave_transmit on_transmit;
    i2c_on_slave_receive on_receive;
};

// /**
//  * @brief   I2C interrupt routine initialization.
//  *
//  * @param address I2C address.
//  */
// void i2c_app_init(uint8_t address);

/**
 * @brief   I2C slave user callback function regiester.
 *
 * @param cb    Pointer to user callback function.
 */
void i2c_slave_cb_register(struct i2c_slave_cb *cb);

/**
 * @brief   I2C master write data to slave.
 *
 * @param addr_7bit I2C slave 7bit address.
 * @param data      Pointer to the write data.
 * @param length    Write data length.
 * @param wait      Choose to wait for the write process to end or not.
 * @param send_stop Choose to send stop or not.
 * @return          0 If successful.
 */
int i2c_write_to(uint8_t addr_7bit, const uint8_t *data, uint8_t length,
        uint8_t wait, uint8_t send_stop);

/**
 * @brief   I2C master read data to slave
 *
 * @param addr_7bit I2C slave 7bit address.
 * @param data      Pointer to the read data to put in.
 * @param length    Read data length.
 * @param send_stop Choose to send stop or not
 * @param timeout   Read process timeout.
 * @return          Negative on error code otherwise indicates the actual read length.
 */
int i2c_read_from(uint8_t addr_7bit, uint8_t *data, uint8_t length,
        uint8_t send_stop, int timeout);

void GXHTC3C_Sleep(void);
void GXHTC3C_Wakeup(void);
void GXHTC3C_GetStart(void);
uint8_t GXHTC3C_GetTempHumi(float *humi, float *temp);

void BSP_I2C_DeInit(void);

void GXHTC3C_Init(void);


#endif // !__BSP_I2C_H__

