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

#include  "um321xF.h"

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

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

#define SPI0_IRQ_ENABLE  		    1
#define SPI0_IRQ_DISABLE  	        0

#define SPI0_CS_ENABLE  		    1
#define SPI0_CS_DISABLE  		    0

#define SPI0_CS0				    0
#define SPI0_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     : spi0_init
 * Description  : spi0_init
 * Input        : uint8_t work_mode: select spi0 work mode  0,1,2,3
 *                uint8_t spi0_clk_div：spi0 baud rate
 *                uint8_t spi0_cs：SPI0_CS0 or SPI0_CS1
 * Return       : none
 ************************************************************************/
void spi0_init(uint8_t work_mode,uint8_t spi0_clk_div,uint8_t spi0_cs);  

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

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

/************************************************************************
 * Function     : spi0_master_full_duplex_tranfer
 * Description  : spi0_master_full_duplex_tranfer spi0全双工传输
 * 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 spi0_master_full_duplex_tranfer(uint8_t *send_data, uint8_t *rec_buff, uint32_t length);

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

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

#endif
