#ifndef __HAL_NCA9555_H__
#define __HAL_NCA9555_H__

 
#include "hc32_ll_gpio.h"
#include "hc32_ll_fcg.h"
#include "hc32_ll_interrupts.h"
#include "hc32_ll_tmr0.h"
#include "ring_buf.h"
#include "hc32_ll_aos.h"
 
 
 
/***************************************************IIC 驱动部门**********************************************************/
#define IIC_SCL_GPIO_PORT  gpioPortB
#define IIC_SCL_GPIO_PIN    1

#define IIC_SDA_GPIO_PORT   gpioPortB
#define IIC_SDA_GPIO_PIN    2

#define IIC_SCL_SET_GPIO_OUTPUT_STATUS(status)      if(status == 1)   GPIO_PinOutSet(IIC_SCL_GPIO_PORT, IIC_SCL_GPIO_PIN);\
                                                    else if(status == 0)  GPIO_PinOutClear(IIC_SCL_GPIO_PORT, IIC_SCL_GPIO_PIN);

#define IIC_SDA_SET_GPIO_OUTPUT_STATUS(status)     if(status == 1)   GPIO_PinOutSet(IIC_SDA_GPIO_PORT, IIC_SDA_GPIO_PIN);\
                                                    else if(status == 0)  GPIO_PinOutClear(IIC_SDA_GPIO_PORT, IIC_SDA_GPIO_PIN);

#define IIC_SDA_GET_GPIO_INPUT_STATUS   GPIO_PinInGet(IIC_SDA_GPIO_PORT, IIC_SDA_GPIO_PIN)


void IIC_gpio_init(void);
void IIC_start(void);
void IIC_stop(void);
uint8_t IIC_wait_ack(void);
void IIC_ack(void);
void IIC_nack(void);
void IIC_send_byte(uint8_t txd);
void IIC_send_byte(uint8_t txd);
uint8_t IIC_read_byte(unsigned char ack);

void iic3_init(void);
void nca9555_set_output_mode(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num);
void nca9555_set_gpio_output_status(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num, uint8_t status);

void nca9555_set_input_mode(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num);
uint8_t nca9555_get_gpio_status(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num);
/***********************************************************IIC 驱动 END**********************************************************************/

/************************************************************NCA9555 驱动***********************************************************/
#define SUCCESS 0
#define ERROR   1

#define SLAVE_ADDR0   0x40
#define SLAVE_ADDR1   0x42
#define SLAVE_ADDR2   0x44

#define HOST_WRITE_COMMAND 0x00
#define HOST_READ_COMMAND  0x01


/*输入相关*/
#define INPUT_PORT_REGISTER0                    0x00   /* 输入端口寄存器0，负责IO00-IO07 */
#define INPUT_PORT_REGISTER1                    0x01   /* 输入端口寄存器1，负责IO10-IO17 */
//#define POLARITY_INVERSION_PORT_REGISTER0       0x04   /* 输入端口寄存器0，负责IO00-IO07 */
//#define POLARITY_INVERSION_PORT_REGISTER1       0x05   /* 输入端口寄存器1，负责IO10-IO17 */

/*输出相关*/
#define OUTPUT_PORT_REGISTER0                   0x02   /* 输入端口寄存器0，负责IO00-IO07 */
#define OUTPUT_PORT_REGISTER1                   0x03   /* 输入端口寄存器1，负责IO10-IO17 */

#define CONFIG_PORT_REGISTER0                   0x06   /* 输入端口寄存器0，负责IO00-IO07 */
#define CONFIG_PORT_REGISTER1                   0x07   /* 输入端口寄存器1，负责IO10-IO17 */



#define GPIO_PORT0 0
#define GPIO_PORT1 1

#define GPIO_Pin00 0x01
#define GPIO_Pin01 0x02
#define GPIO_Pin02 0x04
#define GPIO_Pin03 0x08
#define GPIO_Pin04 0x10
#define GPIO_Pin05 0x20
#define GPIO_Pin06 0x40
#define GPIO_Pin07 0x80


#define HIGH 1
#define LOW  0


#define NCA9555_WAIT_IS_RETURN_SUCCESS(flag, tips)    if(flag != SUCCESS)\
                                                        { \
                                                            printf("%s", tips); \
                                                            return ERROR;\
                                                        }

																												
void nca9555_init(void);
void nca9555_read_wtite_test(void);
void nca9555_set_output_mode(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num);
void nca9555_set_gpio_output_status(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num, uint8_t status);
void nca9555_set_input_mode(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num);
uint8_t nca9555_get_gpio_status(uint8_t slave_num, uint8_t gpio_port,  uint8_t gpio_num);
																												
void nca9555_task(void *argv);
/************************************************************NCA9555 驱动  END***********************************************************/
 
#endif