/**
******************************************************************************************************************************************************************************************
*@file                       ws18xxx_communication_driver.h
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#ifndef  __WS18XXX_INTERFACE_DRIVER_H__
#define  __WS18XXX_INTERFACE_DRIVER_H__
#include "cpu_gpio.h"

/********IIC ADDR**************************/
#define DEVICE_WRITE_ADDR 0x58 
#define DEVICE_READ_ADDR  0x59

//========URAT Config================================================================================
#define NFC_UART	ws_uart0
#define  Baud_Rate  9600

/* COMMUNICATION define */
#define INTERFACE_SPI      1
#define INTERFACE_I2C      0
#define INTERFACE_UART     0 

/* COMMUNICATION select */
#define INTERFACE  INTERFACE_SPI  

/******* SPI *******/
#if(INTERFACE_SPI)
#define WS_MOSI        GPIOA_Pin1 
#define WS_MOSI_HIGH   gpio_set(WS_MOSI)    
#define WS_MOSI_LOW    gpio_reset(WS_MOSI)
											 
#define WS_MISO        gpio_get_input(GPIOA_Pin0)
											 
#define WS_SCK         GPIOA_Pin2
#define WS_SCK_HIGH    gpio_set(WS_SCK)                     
#define WS_SCK_LOW     gpio_reset(WS_SCK)
                       
#define WS_NSS         GPIOA_Pin3 
#define WS_NSS_HIGH    gpio_set(WS_NSS)                     
#define WS_NSS_LOW     gpio_reset(WS_NSS)
#endif					

/*******IIC*********/
#if(INTERFACE_I2C)
#define IIC_SCL        GPIOA_Pin0 
#define IIC_SCL_H      gpio_set(IIC_SCL)                     
#define IIC_SCL_L      gpio_reset(IIC_SCL)
											 
#define IIC_SDA        GPIOA_Pin3 
#define IIC_SDA_H      gpio_set(IIC_SDA)                     
#define IIC_SDA_L      gpio_reset(IIC_SDA)
#define IIC_SDA_READ() gpio_get_input(IIC_SDA)
#define IIC_SDA_OUT()	 gpio_init(IIC_SDA,GPIO_Mode_Output)
#define IIC_SDA_IN()	 gpio_init(IIC_SDA,GPIO_Mode_Input)			
#endif

/********URAT************/
#if(INTERFACE_UART)
#define NFC_UART_TX       GPIOA_Pin0 
#define NFC_UART_RX       GPIOA_Pin3 
#endif

#define NFC_RST        GPIOB_Pin4 
#define NFC_RST_HIGH   gpio_set(NFC_RST)                     
#define NFC_RST_LOW    gpio_reset(NFC_RST)

#define WS_IRQ         GPIOA_Pin4

#define LED2           GPIOD_Pin0
#define LED2_ON        gpio_set(LED2)                     
#define LED2_OFF       gpio_reset(LED2)
									     
#define LED3           GPIOD_Pin1
#define LED3_ON        gpio_set(LED3)                     
#define LED3_OFF       gpio_reset(LED3)

#define BEEP           GPIOC_Pin0
#define BEEP_ON        gpio_set(BEEP)                     
#define BEEP_OFF       gpio_reset(BEEP)

void SPIWriteByte(unsigned char SPIData);
unsigned char SPIReadByte(void);

void I2C_Start(void);
void I2C_Stop(void);
void I2C_SendAck(uint8_t bAck);
uint8_t I2C_WaitAck(void);
void IIC_WriteByte(uint8_t dat);
uint8_t IIC_ReadByte(void);
uint8_t IIC_Write_Byte(uint8_t WriteAddr,uint8_t WriteData);
uint8_t IIC_Write_2Byte(uint8_t WriteAddr, uint8_t WriteData1,uint8_t WriteData2);
uint8_t IIC_ReadMutiBytes(uint8_t writeAddr,uint8_t *buffer,uint8_t length);
uint8_t IIC_ReadOneByte(uint8_t writeAddr);

unsigned char ReadRawRC(unsigned char Address);
void WriteRawRC(unsigned char Address, unsigned char value);
void SetBitMask(unsigned char reg,unsigned char mask);
void ClearBitMask(unsigned char reg,unsigned char mask);  

uint8_t Uart_ReadByte(uint8_t *buf,uint8_t len);
void Uart_WriteByte(uint8_t Byte);

#endif

