#ifndef _DRV_SPI_H_
#define _DRV_SPI_H_

#ifdef __cplusplus
extern "C" {
#endif 

#include "kit_macro.h"

typedef enum
{
    kSpiDev_1,
    kSpiDev_2,
    kSpiDev_3,  
    kSpiDev_End,
}SpiDev;

//在CLOCK_PCLK2_FREQ 和 CLOCK_PCLK1_FREQ 基础上进行分频
typedef enum
{
    kSpiFreq_Div2 = 0,
    kSpiFreq_Div4,
    kSpiFreq_Div8,
    kSpiFreq_Div16,
    kSpiFreq_Div32,
    kSpiFreq_Div64,
    kSpiFreq_Div128,
    kSpiFreq_Div256,
    kSpiFreq_End,
}SpiFreq;


/********************************************************************
        CPOL    CPHA
MODE0    0       0
MODE1    0       1
MODE2    1       0
MODE3    1       1
CPOL: SPI空闲时的时钟信号电平(1:高电平, 0:低电平)
CPHA: SPI在时钟第几个边沿采样(1:第二个边沿开始, 0:第一个边沿开始)
**********************************************************************/
typedef enum
{
    kSpiMode_C0E0,
    kSpiMode_C0E1,
    kSpiMode_C1E0,
    kSpiMode_C1E1,
    kSpiMode_End,
}SpiMode;

typedef enum
{
    SpiFrame_MSBFirst,
    SpiFrame_LSBFirst,
    SpiFrame_End,
}SpiFrame;

#define SPI_CFG_CPHA_0              (0u)
#define SPI_CFG_CPHA_1              KIT_CREAT_BIT(0)
#define SPI_CFG_CPOL_0              (0u)
#define SPI_CFG_CPOL_1              KIT_CREAT_BIT(1)
#define SPI_CFG_SLAVER              (0u)
#define SPI_CFG_MASTER              (0x0104u)
#define SPI_CFG_DISABLE             (0u)
#define SPI_CFG_ENABLE              KIT_CREAT_BIT(6)
#define SPI_CFG_MSB                 (0u)
#define SPI_CFG_LSB                 KIT_CREAT_BIT(7)
#define SPI_CFG_CS_HARD             (0u)             //CS由外设控制
#define SPI_CFG_CS_SOFT             KIT_CREAT_BIT(9) //CS由软件控制
#define SPI_CFG_2L_RX_TX            (0u)             //双线收发
#define SPI_CFG_2L_RX               (0x0400u)        //双线只收
#define SPI_CFG_1L                  (0x8000u)        //单线制
#define SPI_CFG_FRAME_TI            (0x100000u)        //单线制

KitResult drv_spi_init(SpiDev dev, SpiFreq freq, SpiMode mode, SpiFrame frame_order, uint16_t sck_idx, uint16_t miso_idx, uint16_t mosi_idx);
KitResult drv_spi_init1(SpiDev dev, SpiFreq freq, uint32_t spi_config, uint16_t sck_idx, uint16_t miso_idx, uint16_t mosi_idx);
KitResult drv_spi_set_dma(SpiDev dev, bool is_rx_en, bool is_tx_en);
KitResult drv_spi_sync_send_receive(SpiDev dev, uint8_t *data);
KitResult drv_spi_series_sync_send_receive(SpiDev dev, uint8_t *tx_buf, uint16_t tx_len, uint8_t *rx_buf, uint16_t rx_len);

KitResult drv_spi_dma_sync_send(SpiDev dev, uint8_t *tx_buf,  uint16_t tx_len);
KitResult drv_spi_dma_sync_receive(SpiDev dev, uint8_t *rx_buf,  uint16_t rx_len);
#ifdef __cplusplus
}
#endif

#endif
