#ifndef _GW3323_SPI_H
#define _GW3323_SPI_H
#include "include.h"

//--------------- SPI[0..1] Flag Definition ---------------//
#define SPI_FLAG_PENDING            ((uint32_t)0x00010000)

//SPI bus mode enumeration
typedef enum {
    SPI_3WIRE_MODE    = 0x0000,
    SPI_2WIRE_MODE    = 0x0004,
    SPI_2BIT_BID_MODE = 0x0008,
    SPI_4BIT_BID_MODE = 0x000c,
} SPI_BUS_MODE_TYPEDEF;

//SPI 时钟空闲状态选择
typedef enum {
    SPI_CLK_HIGH    = 0x0020,
    SPI_CLK_LOW     = 0x0000,
} SPI_CLK_BUS_MODE;

//SPI direction
typedef enum {
    SPI_DIR_TX    = 0x00,
    SPI_DIR_RX     = 0x01,
} SPI_DIR_TYPEDEF;

//SPI init structure
typedef struct{
    uint16_t baud;
    uint8_t spi_sm;//Slave mode select bit.0: master mode
    SPI_BUS_MODE_TYPEDEF bus_mode;
    SPI_CLK_BUS_MODE clk_mode;
    uint8_t spi_smps;//SPI output edge select bit.0: output data at the falling edge;
    uint8_t spi_oss;//SPI sample data is at the same clock edge with output data
} spi_init_typedef;

void spi_init(spi_typedef* spix, spi_init_typedef* spi_init_struct);
void spi_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
void spi_dir_sel(spi_typedef* spix, SPI_DIR_TYPEDEF direction);
void spi_it_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
void spi_lfsr_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
void spi_multi_bit_bus_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
void spi_hold_rx_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
void spi_hold_tx_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
void spi_soft_hold_cmd(spi_typedef* spix, FUNCTIONAL_STATE state);
FLAG_STATE spi_get_flag(spi_typedef* spix, uint32_t spi_flag);
void spi_clear_flag(spi_typedef* spix, uint32_t spi_flag);
void spi_set_dma_cnt(spi_typedef* spix, uint32_t cnt);
void spi_set_dma_addr(spi_typedef* spix, uint32_t addr);
uint8_t spi_receive_data(spi_typedef* spix);
void spi_send_data(spi_typedef* spix, uint8_t data);
#endif
