/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : spi1_master.h
 * Description : spi1 master header file
 * Author(s)   : Mason
 * version     : V1.0
 * Modify date : 2022-08-04
 ***********************************************************************/
#ifndef __SPI1_H__
#define __SPI1_H__

#include  "um321xF.h"

//#define FULL_DUPLEX_MODE			//全双工模式
//#define SPI_RX_INT_MODE			//开启中断

#define WORK_MODE_0  				(0<<0)			//SPI1工作模式0:CPHOL = 0 CPHA = 0
#define WORK_MODE_1  				(1<<0)			//SPI1工作模式1:CPHOL = 0 CPHA = 1
#define WORK_MODE_2  				(2<<0)			//SPI1工作模式2:CPHOL = 1 CPHA = 0
#define WORK_MODE_3  				(3<<0)			//SPI1工作模式3:CPHOL = 1 CPHA = 1

#define SPI1_IRQ_ENABLE  			1
#define SPI1_IRQ_DISABLE  			0

#define SPI1_CS_ENABLE  			1
#define SPI1_CS_DISABLE  			0

#define SPI1_CS0                    0
#define	SPI1_CS1                    1


#define SPI_CLK_DIV_2           	0
#define SPI_CLK_DIV_4           	1
#define SPI_CLK_DIV_8           	2
#define SPI_CLK_DIV_16          	3
#define SPI_CLK_DIV_32          	4
#define SPI_CLK_DIV_64          	5
#define SPI_CLK_DIV_128         	6
#define SPI_CLK_DIV_256         	7


#define SPIIF_RXBF                  0
#define SPIIF_TXBE                 	1
#define SPIIF_IDLE                	2
#define SPIIF_TXCOL                 3
#define SPIIF_RXCOL                 4
#define SPIIF_SERR                 	5
#define SPIIF_MERR                 	6
#define SPIIF_TNF                 	7
#define SPIIF_RNF                 	8

#define SPIIE_RXBFIE                0
#define SPIIE_TXBEIE                1
#define SPIIE_IDLEIE                2
#define SPIIE_TXCOLIE               3
#define SPIIE_RXCOLIE               4
#define SPIIE_SERRIE                5
#define SPIIE_MERRIE                6
#define SPIIE_TNFIE                	7
#define SPIIE_RNFIE                 8


/************************************************************************
 * Function     : spi1_init
 * Description  : spi1_init
 * Input        : uint8_t work_mode: select spi1 work mode  0,1,2,3
 *                uint8_t spi1_clk_div：spi1 baud rate
 *                uint8_t spi1_cs：SPI1_CS0 or SPI1_CS1
 * Return       : none
 ************************************************************************/
void spi1_init(uint8_t work_mode,uint8_t spi1_clk_div,uint8_t spi1_cs); 

/************************************************************************
 * Function     : spi1_irq_init
 * Description  : spi1 irq init
 * Input        : uint8_t irq_enable: irq enable or disable
 *                uint32_t spi1_intr：spi1 interrupt type
 *                void (*pfunc)()：spi1 interrupt callback function
 * Return       : none
 ************************************************************************/
void spi1_irq_init(uint8_t irq_enable,uint32_t spi1_intr,void (*pfunc)());

/************************************************************************
 * Function     : spi1_cs_enable
 * Description  : spi1_cs_enable CS片选设置
 * Input        : uint8_t cs_enable：enable or disable
 *                uint8_t cs_select：spi1 cs0 or spi1 cs1
 * Return       : none
 ************************************************************************/
void spi1_cs_enable(uint8_t cs_enable, uint8_t cs_select);

/************************************************************************
 * Function     : spi1_irq_receive_byte
 * Description  : receive a byte in interrupt
 * Input        : none
 * Return       : spi rx buff
 ************************************************************************/
uint8_t spi1_irq_receive_byte(void);

/************************************************************************
 * Function     : spi1_master_full_duplex_tranfer
 * Description  : spi1_master_full_duplex_tranfer spi1全双工传输
 * Input        : uint8_t *send_data：tx data buff
 *                uint8_t *rec_buff：rx data buff
 *                uint32_t length：data length
 * Return       : 正常发送返回	：0
 *                异常发送返回 	：1
 ************************************************************************/
uint8_t spi1_master_full_duplex_tranfer(uint8_t *send_data, uint8_t *rec_buff, uint32_t length);

/************************************************************************
 * Function     : spi1_master_half_duplex_send_bytes
 * Description  : spi1_master_half_duplex_send_bytes
 * Input        : uint8_t *buff：tx data buff
 *                uint32_t length：data length
 * Return       : 正常发送返回	：0
 *                异常发送返回 	：1
 ************************************************************************/
uint8_t spi1_master_half_duplex_send_bytes(uint8_t *buff, uint32_t length);

/************************************************************************
 * Function     : spi1_master_half_duplex_receive_bytes
 * Description  : spi1_master_half_duplex_receive_bytes 	接收多个byte数据
 * Input        : uint8_t *buff：rx data buff
 *                uint32_t length：data length
 * Return       : 正常发送返回	：0
 *                异常发送返回 	：1
 ************************************************************************/
uint8_t spi1_master_half_duplex_receive_bytes(uint8_t *buff,uint32_t length);

#endif


