#ifndef __BSP_I2C_H
#define __BSP_I2C_H

#include "stm32f4xx.h"


#ifdef __cplusplus
	extern "C" {
#endif

#include <stdlib.h>
#include "main.h"
#include "stm32f4xx.h"
#include "i2c.h"

typedef enum 
{
  I2C_Trans_Polling       	= 0x00U,
  I2C_Trans_IT   	= 0x01U,
  I2C_Trans_DMA     = 0x02U,
} I2C_Transmode_TypeDef;
 

/* -----------------------    variables     ---------------------------------*/
// extern UART_HandleTypeDef huart3;
// UART_HandleTypeDef *pi2chelper = &huart3;


typedef  int ( *i2c_callback_fun)  (void *obj, void *buf, uint16_t size);

typedef struct 
{
	I2C_HandleTypeDef *hi2c;

    volatile uint8_t flag_busy;
    volatile uint8_t flag_send_or_rcv;

    uint8_t dev_addr;
    uint16_t mem_addr_size;  // I2C_MEMADD_SIZE_8BIT  I2C_MEMADD_SIZE_16BIT
    uint16_t write_delay_ms;
    // uint8_t mem_addr[2];
    uint16_t mem_addr ;
 
	// trans buf size, mem 方法不含地址
    uint32_t size;
    uint8_t *buf;


	I2C_Transmode_TypeDef trans_mode; /* 0 :polling, 1: IT  2: DMA*/
	I2C_Transmode_TypeDef rcv_mode;

	// volatile uint8_t transferring;
	// uint32_t timebase;

	// enable_func_gpio enable_trans_gpio;
	// enable_func_gpio enable_trans_cplt_gpio;

	void * obj;
	i2c_callback_fun callback;

	//  这个是供外部调用的； 中断是统一管理的， 中断会找到这里的接收回调，然后返回回去
 
}I2C_HELPER_TypeDef;

// extern I2C_HELPER_TypeDef *i2chelper;

I2C_HELPER_TypeDef * I2C_HELPER_Init( );
void I2C_HELPER_Set_Hi2c(I2C_HELPER_TypeDef * i2chelper, I2C_HandleTypeDef *hi2c);
void I2C_HELPER_Set_dev_addr(I2C_HELPER_TypeDef * i2chelper, uint8_t dev_addr);
void I2C_HELPER_Set_mem_addr_size(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr_size);
void I2C_HELPER_Set_mem_addr(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr);


int I2C_HELPER_Setup_Trans_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t trans_mode );
int I2C_HELPER_Setup_Rcv_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t rcv_mode );

 

int I2C_HELPER_Set_Callback( I2C_HELPER_TypeDef * i2chelper, void *obj, i2c_callback_fun func);

//Trans
int I2C_HELPER_Trans(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size);
// int __I2C_HELPER_Trans(I2C_HELPER_TypeDef * i2chelper , uint8_t *buf, uint16_t size);
int I2C_HELPER_Trans_TxCplt_Callback( I2C_HELPER_TypeDef * i2chelper );

/* 接收方式 也有三种类型 */
int I2C_HELPER_Start_Rcv(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size);
int I2C_HELPER_RCV_Cplt_Callback( I2C_HELPER_TypeDef * i2chelper );
int I2C_HELPER_RCV_IDLE_Callback( I2C_HELPER_TypeDef * i2chelper, uint16_t size );

int I2C_HELPER_read_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size);
int I2C_HELPER_write_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size);

int I2C_HELPER_read_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
int I2C_HELPER_write_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);

// struct i2c_buff
// {
//     volatile uint8_t flag_busy;
//     volatile uint8_t flag_send_or_rcve;
//     uint8_t memory_addr_size;
//     uint8_t dev_addr;
//     I2C_TypeDef *i2c;
//     DMA_Stream_TypeDef *dma_rx_stream;
//     DMA_Stream_TypeDef *dma_tx_stream;
//     uint32_t size;
//     uint8_t *buf;
//     uint8_t mem_addr[2];
// };
// int i2c_init(struct i2c_buff *i2c_buf,I2C_TypeDef *i2c,DMA_Stream_TypeDef *dma_rx_stream,DMA_Stream_TypeDef *dma_tx_stream);
// int i2c_read_memory(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
// int i2c_write_memory(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
// void i2c_dma_tx_it_tc_callback(struct i2c_buff *i2c_buf);
// void i2c_dma_rx_it_tc_callback(struct i2c_buff *i2c_buf);
// int i2c_clear_state(struct i2c_buff *i2c_buf);
// int i2c_get_state(struct i2c_buff *i2c_buf);
// void i2c_it_af_callback(struct i2c_buff *i2c_buf);
// void i2c_it_btf_callback(struct i2c_buff *i2c_buf);
// void i2c_it_addr_callback(struct i2c_buff *i2c_buf);
// void i2c_it_sb_callback(struct i2c_buff *i2c_buf);
// int i2c_read_bytes(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint8_t *buf,uint32_t size);
// int i2c_write_bytes(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint8_t *buf,uint32_t size);
// void i2c_stop(struct i2c_buff *i2c_buf);


// // int I2C_HELPER_Set_Interface_Type(I2C_HELPER_TypeDef * i2chelper, uint8_t interface_type);

// int I2C_HELPER_Setup_Trans_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t trans_mode );
// int I2C_HELPER_Setup_Rcv_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t rcv_mode );

// int I2C_HELPER_Set_trans_GPIO( I2C_HELPER_TypeDef * i2chelper, enable_func_gpio trans_gpio_func );
// int I2C_HELPER_Set_trans_cplt_GPIO( I2C_HELPER_TypeDef * i2chelper, enable_func_gpio trans_cplt_gpio_func );
 
// int I2C_HELPER_Set_enable_idle( I2C_HELPER_TypeDef * i2chelper, uint8_t enable_idle);

// int I2C_HELPER_Set_Rcv_Buf( I2C_HELPER_TypeDef * i2chelper, uint8_t * buf, uint16_t size);
// int I2C_HELPER_Set_Trans_Buf( I2C_HELPER_TypeDef * i2chelper, uint8_t * buf, uint16_t size);



#endif

/*

HAL 原型函数
    // st = HAL_I2C_Mem_Write(ads1115_i2c_helper->hi2c, ADS1115_WRITE_ADDRESS, 
    //             my_ads1115.reg_data[0],ads1115_i2c_helper->mem_addr_size ,  reg_data,2,0xFFFF);
    // st = HAL_I2C_Mem_Read(ads1115_i2c_helper->hi2c, ADS1115_WRITE_ADDRESS, 
    //         0x00,ads1115_i2c_helper->mem_addr_size ,  rx_data,2,0xFFFF);


485 
enable rcv
HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 0); 
enable send 
HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 1); 

3160 485   --高    (232 - 低)
enable rcv
        HAL_GPIO_WritePin(i2chelper->dplx_gpio, i2chelper->dplx_gpio ,  1);
        HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 0); 
enable send 
        HAL_GPIO_WritePin(i2chelper->dplx_gpio, i2chelper->dplx_gpio ,  0);
        HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 1); 


i2chelper = I2C_HELPER_Init();

I2C_HELPER_Set_Huart(  i2chelper, phuart),
I2C_HELPER_Setup_Transmod(  i2chelper, 2 );
 I2C_HELPER_Set_enable_snd_func(   i2chelper, NULL );
 I2C_HELPER_Set_enable_rcv_func(   i2chelper, NULL);
 I2C_HELPER_Set_enable_idle(   i2chelper,  enable_idle);
 I2C_HELPER_Set_enable_rcv_func(   i2chelper,  obj, (callback_fun) func);


// #if MAX3160_ENABLE
// #define     DUPLEX_HALF  	1
// #define     DUPLEX_FULL  	0
// #define    SEL_485 			1
// #define    SEL_232 			0

// #define    ENABLE_485_Trans      1
// #define    DISABLE_485_Trans     0
// # endif

// #if MAX485_ENABLE
// #define    ENABLE_485_Trans      1
// #define    DISABLE_485_Trans     0
// # endif


*/