#ifndef __BSP_SPI_H
#define __BSP_SPI_H

#include "stm32f4xx_hal.h"
#include "main.h"
// #include "sys.h"

typedef enum 
{
  SPI_Trans_Polling       	= 0x00U,
  SPI_Trans_IT   	= 0x01U,
  SPI_Trans_DMA     = 0x02U,
} SPI_Transmode_TypeDef;
 
typedef enum 
{
  SPI_WORKMODE_RX_TX       	= 0x00U,
  SPI_WORKMODE_TX   	= 0x01U,
  SPI_WORKMODE_RX   	= 0x02U,
} SPI_WORKMODE_RX_TX_TypeDef;
 

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

typedef struct 
{
	SPI_HandleTypeDef *hspi;
 
	volatile SPI_WORKMODE_RX_TX_TypeDef rx_tx;

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

	// Record_Trans_Rcv_TypeDef *trans_record;
	// Record_Trans_Rcv_TypeDef *rcv_record;

	uint8_t *trans_buf;
	uint16_t trans_size;
	uint8_t *rcv_buf;
	uint16_t rcv_size;

	volatile uint8_t transferring;
	uint32_t timebase;

	// enable_func_gpio enable_trans_gpio;
	// enable_func_gpio enable_trans_cplt_gpio;

	void * obj;
	callback_fun callback;

 
}SPI_HELPER_TypeDef;

extern SPI_HELPER_TypeDef *spihelper;

SPI_HELPER_TypeDef * SPI_HELPER_Init( );
void SPI_HELPER_Set_Huart(SPI_HELPER_TypeDef * spihelper, SPI_HELPER_TypeDef *hspi);
 

int SPI_HELPER_Setup_Trans_mode( SPI_HELPER_TypeDef * spihelper, uint8_t trans_mode );
int SPI_HELPER_Setup_Rcv_mode( SPI_HELPER_TypeDef * spihelper, uint8_t rcv_mode );

int SPI_HELPER_Set_Rcv_Buf( SPI_HELPER_TypeDef * spihelper, uint8_t * buf, uint16_t size);
int SPI_HELPER_Set_Trans_Buf( SPI_HELPER_TypeDef * spihelper, uint8_t * buf, uint16_t size);

int SPI_HELPER_Setup_WokMode_RX_TX( SPI_HELPER_TypeDef * spihelper, uint8_t rx_tx );

int SPI_HELPER_Set_Callback( SPI_HELPER_TypeDef * spihelper, void *obj, callback_fun func);

int SPI_HELPER_Trans(SPI_HELPER_TypeDef * spihelper, uint8_t *buf, uint16_t size);
int SPI_HELPER_Trans_TxCplt_Callback( SPI_HELPER_TypeDef * spihelper );

int SPI_HELPER_Start_Rcv(SPI_HELPER_TypeDef * spihelper, uint8_t *buf, uint16_t size);
int SPI_HELPER_RCV_Cplt_Callback( SPI_HELPER_TypeDef * spihelper );

int SPI_HELPER_Start_Trans_Rcv(SPI_HELPER_TypeDef * spihelper, uint8_t *txbuf ,uint8_t *rxbuf, uint16_t size);
int SPI_HELPER_Trans_RCV_Cplt_Callback( SPI_HELPER_TypeDef * spihelper );

void SPI_HELPER_Test(void) ;
void SPI_HELPER_TransmitReceive(uint8_t* txData, uint8_t* rxData, uint16_t size) ;

#endif

/*
void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi);
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi);
*/