/************************************************************************************************/
/**
* @file               i2c_bsp.c
* @author             MCU Ecosystem Development Team
* @brief              I2C BSP驱动函数，使用GPIO模拟I2C主模式，实现I2C主模式通信协议。
*
*
**************************************************************************************************
* @attention
* Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
*
**************************************************************************************************
*/

/*------------------------------------------includes--------------------------------------------*/
#include "i2c_bsp.h"

/*------------------------------------------variables-------------------------------------------*/
bsp_i2c_master_sim_t i2c_master_instance = {0};

/*------------------------------------------functions-------------------------------------------*/
/**
* @brief  I2C发送数据
* @param  dev_addr 从机地址
* @param  send_data 待发送数据
* @param  send_data_len 待发送数据长度
* @retval 无
*/
void bsp_i2c_master_send_start(uint8_t dev_addr, uint8_t *send_data, uint32_t send_data_len)
{
    /* 参数初始化 */
    i2c_master_instance.i2c_slave_address = dev_addr;
    i2c_master_instance.i2c_master_buffer = send_data;
    i2c_master_instance.i2c_master_data_len = send_data_len;
    i2c_master_instance.i2c_master_data = *send_data;
    i2c_master_instance.i2c_read_write_flag = 0;
    i2c_master_instance.i2c_master_data_cnt = 0;
    i2c_master_instance.i2c_scl_edge_cnt = 0;
    i2c_master_instance.i2c_status = I2C_START;
    
    /* I2C_SIM_TIM使能 */
    I2C_SIM_TIM_ENABLE();
}

/**
* @brief  I2C接收数据
* @param  dev_addr 从机地址
* @param  receive_data 接收数据
* @param  send_data_len 待接收数据长度
* @retval 无
*/
void bsp_i2c_master_receive_start(uint8_t dev_addr, uint8_t *receive_data, uint32_t receive_data_len)
{
    /* 参数初始化 */
    i2c_master_instance.i2c_slave_address = dev_addr;
    i2c_master_instance.i2c_master_buffer = receive_data;
    i2c_master_instance.i2c_master_data_len = receive_data_len;
    i2c_master_instance.i2c_master_data = 0;
    i2c_master_instance.i2c_read_write_flag = 1;
    i2c_master_instance.i2c_master_data_cnt = 0;
    i2c_master_instance.i2c_scl_edge_cnt = 0;
    i2c_master_instance.i2c_status = I2C_START;
    
    /* I2C_SIM_TIM使能 */
    I2C_SIM_TIM_ENABLE();
}

/**
* @brief  获取I2C状态
* @retval bsp_i2c_state_t  I2C状态
*             @arg I2C_IDLE
*             @arg I2C_START
*             @arg I2C_ADDRESS
*             @arg I2C_ADDRESS_ACK
*             @arg I2C_SEND_DATA
*             @arg I2C_RECEIVE_DATA
*             @arg I2C_RECEIVE_ACK
*             @arg I2C_SEND_ACK
*             @arg I2C_STOP
*/
bsp_i2c_state_t bsp_i2c_master_get_state(void)
{
    return i2c_master_instance.i2c_status;
}
