#include "spi_helper.h"

#if 0 
// static I2C_HandleTypeDef *pADS1115_I2C = &hi2c1;

// osEventFlagsId_t spihelperEventHandle;
// const osEventFlagsAttr_t spihelperEvent_attributes = {
//   .name = "spihelperEvent"
// };

SPIHelper_TypeDef * SPIHelper_Init( )
{
    SPIHelper_TypeDef *Handle = (SPIHelper_TypeDef *)malloc(sizeof(SPIHelper_TypeDef));
    if (Handle == NULL)
    {
        return NULL;
    }
	// Handle->delaytime_trans_byte = 0;
    return Handle;
}
void SPIHelper_Set_hspi( SPIHelper_TypeDef *spihelper,  SPI_HandleTypeDef * hspi  )
{
	spihelper->hspi = hspi;
}
void SPIHelper_Set_Mode( SPIHelper_TypeDef *spihelper,  SPI_HELPER_MODE_TypeDef mode  )
{
	spihelper->mode	= mode;
}
void SPIHelper_Set_Trans_Type( SPIHelper_TypeDef *spihelper,  SPI_HELPER_TRANS_TypeDef trans_type  )
{
	spihelper->trans_type	= trans_type;
}
void SPIHelper_Set_Rcv_Type( SPIHelper_TypeDef *spihelper,  SPI_HELPER_RCV_TypeDef rcv_type  )
{
	spihelper->rcv_type	= rcv_type;
}

int SPIHelper_Set_callback_func_obj( SPIHelper_TypeDef *spihelper,   void * obj,  it_callback callback )
{
	spihelper->obj	= obj;
	spihelper->callback	= callback;
	return 0;
}

void SPIHelper_Set_rcv_buf( SPIHelper_TypeDef *spihelper, uint8_t *buf, uint32_t buf_size )
{
	spihelper->receive_buf	= buf;
	spihelper->receive_size	= buf_size;
	spihelper->receive_data_con	= 0;

}
void SPIHelper_Set_Trans_Buf( SPIHelper_TypeDef *spihelper, uint8_t *buf, uint32_t buf_size )
{
	spihelper->trans_buf	= buf;
	spihelper->trans_size	= buf_size;
	spihelper->trans_data_con	= 0;
}

int SPIHelper_Transmit( SPIHelper_TypeDef *spihelper,  uint8_t * buf, uint16_t size)
{
	// 考虑主从机模式
	int st = -1;
	// switch ( spihelper->mode == SPI_HELPER_MODE_MASTER )
	// {
	// case SPI_HELPER_MODE_MASTER:
	// 	if ( spihelper->trans_type == SPI_HELPER_TRANS_POLLING )
	// 	{
	// 		st = HAL_I2C_Master_Transmit( spihelper->hi2c, spihelper->i2c_write_address
	// 					, buf, size, 0xFFFF );
	// 	}
	// 	if ( spihelper->trans_type == SPI_HELPER_TRANS_IT )
	// 	{
	// 		// st = HAL_I2C_Master_Transmit_IT( spihelper->hi2c, spihelper->i2c_write_address
	// 		// 			, buf, size  );	
	// 		st = HAL_I2C_Master_Seq_Transmit_IT( spihelper->hi2c, spihelper->i2c_write_address
	// 					, buf, size, I2C_FIRST_FRAME );			
	// 	}
	// 	if ( spihelper->trans_type == SPI_HELPER_TRANS_DMA )
	// 	{
	// 		// st = HAL_I2C_Master_Transmit_DMA( spihelper->hi2c, spihelper->i2c_write_address
	// 		// 			, buf, size  );			
	// 		st = HAL_I2C_Master_Seq_Transmit_DMA( spihelper->hi2c, spihelper->i2c_write_address
	// 					, buf, size, I2C_FIRST_FRAME );		
 
	// 	}
	// 	break;
	// case SPI_HELPER_MODE_SLAVE:
	// 	break;	
	// default:
	// 	break;
	// }
	return st;
}
int SPIHelper_Begin_Rcv(SPIHelper_TypeDef *spihelper,  uint8_t * buf, uint16_t size)
{
	int st = -1;
	// switch ( spihelper->mode == SPI_HELPER_MODE_MASTER )
	// {
	// case SPI_HELPER_MODE_MASTER:
	// 	if ( spihelper->rcv_type == SPI_HELPER_TRANS_POLLING )
	// 	{
	// 		st = HAL_I2C_Master_Receive( spihelper->hi2c, spihelper->i2c_write_address
	// 					, buf, size, 0xFFFF );
	// 	}
	// 	if ( spihelper->rcv_type == SPI_HELPER_TRANS_IT )
	// 	{
	// 		// st = HAL_I2C_Master_Transmit_IT( spihelper->hi2c, spihelper->i2c_write_address
	// 		// 			, buf, size  );	
	// 		st = HAL_I2C_Master_Seq_Receive_IT( spihelper->hi2c, spihelper->i2c_write_address
	// 					, buf, size, I2C_FIRST_FRAME );			
	// 	}
	// 	if ( spihelper->rcv_type == SPI_HELPER_TRANS_DMA )
	// 	{
	// 		// st = HAL_I2C_Master_Transmit_DMA( spihelper->hi2c, spihelper->i2c_write_address
	// 		// 			, buf, size  );			
	// 		st = HAL_I2C_Master_Seq_Receive_DMA( spihelper->hi2c, spihelper->i2c_write_address
	// 					, buf, size, I2C_FIRST_FRAME );		
	// 	}
	// 	break;
	// case SPI_HELPER_MODE_SLAVE:
	// 	break;	
	// default:
	// 	break;
	// }
	return st;
}

int SPIHelper_Flags_Set(SPIHelper_TypeDef *spihelper,  SPI_HELPER_Event_TypeDef evt_type )
{
	return osEventFlagsSet(spihelper, evt_type);
}

// int SPIHelper_Flags_Clear(SPIHelper_TypeDef *spihelper,  SPI_HELPER_Event_TypeDef evt_type )
// {
// 	return osEventFlagsClear(spihelper, evt_type);
// }

// int SPIHelper_Flags_Wait(SPIHelper_TypeDef *spihelper,  uint32_t delay )
// {
// 	return 	osEventFlagsWait ( 
//                 spihelperEventHandle
//                 , SPI_HELPER_Event_READY|SPI_HELPER_Event_INIT|SPI_HELPER_Event_TRANS_ONLY|SPI_HELPER_Event_DATA_RCV
//                 , 0
//                 , delay );
// }

#endif