/***********************************************************************
 * Copyright (c)  2017 - 2020, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : spi0.h
 * Description : spi0 header file
 * Author(s)   : will
 * version     : V1.0
 * Modify date : 2019-11-21
 ***********************************************************************/
#ifndef __SPI0_H__
#define __SPI0_H__

#include  "um321xA.h"

//#define full_duplex 				//全双工模式

//#define irq_mode_enable			//开启中断

#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

void spi0_init(uint8_t work_mode,uint8_t spi0_clk_div,uint8_t spi0_cs);  

void spi0_irq_init(uint8_t irq_enable,uint32_t spi0_intr,void (*pfunc)());

void spi0_cs_enable(uint8_t cs_enable, uint8_t cs_select);

uint8_t spi0_master_full_duplex_tranfer(uint8_t *send_data, uint8_t *rec_buff, uint32_t length);

uint8_t spi0_master_half_duplex_send_bytes(uint8_t *buff, uint32_t length);
	
uint8_t spi0_master_half_duplex_receive_bytes(uint8_t *buff,uint32_t length);

uint8_t spi0_irq_receive_byte(void);

uint8_t spi0_irq_send_bytes(uint32_t length);

static void spi0_wait_idle(void);


uint8_t spi0_command(uint32_t write_length, uint8_t *write_buf,
					 uint32_t read_length, uint8_t *read_buf,
					 uint8_t cs);


#endif


