/***********************************************************************************************************
 * Copyright (c)  2021 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : spi.h
 * Description : spi driver header file
 * Author(s)   : yanhaihua
 * Version     : V1.0
 * Modify date : 2021-09-23
 ***********************************************************************************************************/
#ifndef __SPI_H__
#define __SPI_H__

#include "config.h"

#if defined(UM2080F32_USING_SPI0) || defined(UM2080F32_USING_SPI1)

#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

typedef enum
{
	SPI_MODE_0							= 0,																/* SPI0工作模式0:CPHOL = 0 CPHA = 0 */
	SPI_MODE_1							= 1,																/* SPI0工作模式1:CPHOL = 0 CPHA = 1 */
	SPI_MODE_2							= 2,																/* SPI0工作模式2:CPHOL = 1 CPHA = 0 */
	SPI_MODE_3							= 3,																/* SPI0工作模式3:CPHOL = 1 CPHA = 1 */
}em_spi_mode_t;

typedef enum
{
	SPI_IRQ_RXBFIE						= 0,																/* RX Buffer非空中断 */
	SPI_IRQ_TXBEIE						= 1,																/* TX Buffer空中断 */
	SPI_IRQ_IDLEIE						= 2,																/* SPI空闲标志中断使能 */
	SPI_IRQ_TXCOLIE						= 3,																/* 发送缓存溢出中断 */
	SPI_IRQ_RXCOLIE						= 4,																/* 接收缓存溢出中断 */
	SPI_IRQ_SERRIE						= 5,																/* Slave ERROR中断 */
	SPI_IRQ_MERRIE						= 6,																/* Master Error中断 */
	SPI_IRQ_TNFIE						= 7,																/* TX FIFO NOT FULL中断 */
	SPI_IRQ_RNFIE						= 8,																/* RX FIFO FULL中断 */
}em_spi_irq_t;

#endif

#ifdef UM2080F32_USING_SPI0

void spi0_master_init(em_spi_mode_t mode,uint8_t clk_div);  

void spi0_cs0_enable(void);

void spi0_cs0_disable(void);

void spi0_cs1_enable(void);

void spi0_cs1_disable(void);

uint8_t spi0_master_full_duplex_tranfer_byte(uint8_t data);

uint8_t spi0_master_full_duplex_tranfer_bytes(uint8_t *send_buff, uint8_t *recv_buff, uint32_t len);

uint8_t spi0_master_half_duplex_send_byte(uint8_t data);

uint8_t spi0_master_half_duplex_send_bytes(uint8_t *buff, uint32_t len);

uint8_t spi0_master_half_duplex_recv_byte(void);

uint8_t spi0_master_half_duplex_recv_bytes(uint8_t *buff,uint32_t len);

void spi0_slave_init(em_spi_mode_t work_mode);

uint8_t spi0_slave_full_duplex_tranfer_byte(uint8_t data);

uint8_t spi0_slave_full_duplex_tranfer_bytes(uint8_t *send_buff, uint8_t *recv_buff, uint32_t len);

uint8_t spi0_slave_half_duplex_send_byte(uint8_t data);

uint8_t spi0_slave_half_duplex_send_bytes(uint8_t *buff, uint32_t len);

uint8_t spi0_slave_half_duplex_recv_byte(void);

uint8_t spi0_slave_half_duplex_recv_bytes(uint8_t *data, uint32_t len);

void spi0_wait_idle(void);

void spi0_irq_init(em_enable_t irq_enable,em_spi_irq_t irq_type,void (*pfunc)());

#endif
#endif
