123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- #include "hal_nca9555.h"
-
-
-
- #define READ_SDA1 GPIO_ReadInputPins(GPIO_PORT_C,GPIO_PIN_10)
- #define IIC_SDA1(n) (n?GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_10):GPIO_ResetPins(GPIO_PORT_C, GPIO_PIN_10))
- #define IIC_SCL1(n) (n?GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_11):GPIO_ResetPins(GPIO_PORT_C, GPIO_PIN_11))
- static void delay_us(uint16_t time)
- {
- uint16_t i;
- for(i=time;i>0;i--)
- {
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();
- }
- }
- static void iic1_init(void)
- {
- stc_gpio_init_t pstcGpioInit1;
- GPIO_StructInit(&pstcGpioInit1);
-
- pstcGpioInit1.u16PinDir = PIN_DIR_OUT;
- pstcGpioInit1.u16PinState = PIN_STAT_RST;
- GPIO_Init(GPIO_PORT_C, GPIO_PIN_10,&pstcGpioInit1);
- GPIO_Init(GPIO_PORT_C, GPIO_PIN_11,&pstcGpioInit1);
-
- GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_10);
- GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_11);
- }
-
- static void SDA_IN1(void)
- {
- stc_gpio_init_t pstcGpioInit3;//iic的引脚
- pstcGpioInit3.u16PinDir = PIN_DIR_IN;
- GPIO_Init(GPIO_PORT_C, GPIO_PIN_10,&pstcGpioInit3);
- }
- static void SDA_OUT1(void)
- {
- stc_gpio_init_t pstcGpioInit3;
- pstcGpioInit3.u16PinDir = PIN_DIR_OUT;
- pstcGpioInit3.u16PinOutputType=PIN_OUT_TYPE_NMOS;
- GPIO_Init(GPIO_PORT_C, GPIO_PIN_10,&pstcGpioInit3);
- }
-
-
- static void iic_start(void)
- {
- SDA_OUT1();
-
- IIC_SDA1(1);
- IIC_SCL1(1);
- delay_us(5);
-
- IIC_SDA1(0);
- delay_us(5);
- IIC_SCL1(0);
- }
- static void iic_stop(void)
- {
- SDA_OUT1();
-
- IIC_SCL1(0);
- IIC_SDA1(0);
- delay_us(5);
-
- IIC_SCL1(1);
- IIC_SDA1(1);
- delay_us(5);
- }
- static void iic_ack(void)
- {
- SDA_OUT1();
-
- IIC_SCL1(0);
- IIC_SDA1(0);
- delay_us(5);
-
- IIC_SCL1(1);
- delay_us(5);
- IIC_SCL1(0);
- }
- static void iic_nack(void)
- {
- SDA_OUT1();
-
- IIC_SCL1(0);
- IIC_SDA1(1);
- delay_us(5);
-
- IIC_SCL1(1);
- delay_us(5);
- IIC_SCL1(0);
- }
-
- static uint8_t iic_wait_ack(void)
- {
- uint16_t t =200;
- SDA_OUT1();
-
- IIC_SDA1(1);delay_us(1);
- IIC_SCL1(1);delay_us(1);
- SDA_IN1();
- while(READ_SDA1)
- {
- t--;
-
- if(t==0)
- {
- iic_stop();
- return 1;
- }
- delay_us(1);
- }
- IIC_SCL1(0);
-
- return 0;
- }
- static void iic_send_byte(uint8_t byte)
- {
- uint8_t bit_cnt;
- SDA_OUT1();
- IIC_SCL1(0);
-
- for(bit_cnt=0;bit_cnt<8;bit_cnt++)
- {
- if(byte&0x80)
- IIC_SDA1(1);
- else
- IIC_SDA1(0);
-
- byte<<=1;
- delay_us(5);
- IIC_SCL1(1);
- delay_us(5);
- IIC_SCL1(0);
- }
- }
-
- static uint8_t iic_recive_byte(unsigned char ack)
- {
- uint8_t retc = 0;
- uint8_t bit_cnt;
- SDA_IN1();
-
- for(bit_cnt=0;bit_cnt<8;bit_cnt++)
- {
- IIC_SCL1(0);
- delay_us(5);
- IIC_SCL1(1);
-
- retc=retc<<1;
- if(READ_SDA1) retc++;
- delay_us(5);
- }
-
- return(retc);
- }
-
- void nca9555_init(void)
- {
- iic1_init();
- }
- uint8_t nca9555_write_byte(uint8_t addr, uint8_t command, uint8_t write_register_data)
- {
- uint8_t ret = 1;
- iic_start();
- iic_send_byte(addr);
- ret = iic_wait_ack();
- iic_send_byte(command);
- ret = iic_wait_ack();
- iic_send_byte(write_register_data);
- ret = iic_wait_ack();
- iic_stop();
- return SUCCESS;
- }
- /*
- * nca9555读取寄存器值
- *
- * addr 读取地址
- * read_register_data 要读取的寄存器
- * read_data 读取数据存放地址
- *
- *
- * 返回值:读取成功返回SUCCESS 失败返回ERROR
- *
- * */
- uint8_t nca9555_read_byte(uint8_t slave_num, uint8_t addr, uint8_t read_register_data, uint8_t *read_data)
- {
- uint8_t ret = 0;
- iic_start();
- iic_send_byte(slave_num);
- ret = iic_wait_ack();
- iic_send_byte(read_register_data);
- ret = iic_wait_ack();
- iic_start(); /* 开始接收数据 */
- iic_send_byte(addr);
- ret = iic_wait_ack();
- *read_data = iic_recive_byte(0);
- iic_stop();
- return SUCCESS;
- }
- /*
- * 设置指定GPIO的模式
- *
- * slave_num 需要操作的从机设备
- * gpio_port gpio端口 端口0/1
- * gpio_num 哪一个GPIO
- *
- * 返回值:void
- * */
- void nca9555_set_output_mode(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num)
- {
- uint8_t register_original_data = 0;
- if(gpio_port > 1 || gpio_num > 0x80) return;
- if(gpio_port == 0)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER0, ®ister_original_data);
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER0, register_original_data & (~gpio_num));
- }
- else if(gpio_port == 1)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER1, ®ister_original_data);
- nca9555_write_byte( slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER1, register_original_data & (~gpio_num));
- }
- }
- /*
- * 设置GPIO输出状态
- *
- * slave_num 需要操作的从机设备
- * gpio_port gpio端口 端口0/1
- * gpio_num 哪一个GPIO
- * status 输出状态
- *
- * 返回值:void
- **/
- void nca9555_set_gpio_output_status(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num, uint8_t status)
- {
- uint8_t register_original_data = 0;
- if(gpio_port > 1 || gpio_num > 0x80) return;
- if(gpio_port == 0)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, OUTPUT_PORT_REGISTER0, ®ister_original_data);
- if(status == 1)
- {
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER0, register_original_data | gpio_num);
- }
- else
- {
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER0, register_original_data & (~gpio_num));
- }
- }
- else if(gpio_port == 1)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, OUTPUT_PORT_REGISTER1, ®ister_original_data);
- if(status == 1)
- {
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER1, register_original_data | gpio_num);
- }
- else
- {
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER1, register_original_data & (~gpio_num));
- }
- }
- }
- /*
- * 设置GPIO为输入模式
- *
- * slave_num 需要操作的从机设备
- * gpio_port gpio端口 端口0/1
- * gpio_num 哪一个GPIO
- *
- * 返回值:void
- **/
- void nca9555_set_input_mode(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num)
- {
- uint8_t register_original_data = 0;
- if(gpio_port > 1 || gpio_num > 0x80) return;
- if(gpio_port == 0)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER0, ®ister_original_data);
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER0, register_original_data & gpio_num);
- }
- else if(gpio_port == 1)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER1, ®ister_original_data);
- nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER1, register_original_data & gpio_num);
- }
- }
- /*
- * 获取GPIO状态
- *
- * slave_num 需要操作的从机设备
- * gpio_port gpio端口 端口0/1
- * gpio_num 哪一个GPIO
- *
- * 返回值:GPIO状态
- **/
- uint8_t nca9555_get_gpio_status(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num)
- {
- uint8_t register_original_data = 0;
- uint8_t gpio_status = 0;
- if(gpio_port > 1 || gpio_num > 0x80)
- {
- return 2;
- }
- if(gpio_port == 0)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, INPUT_PORT_REGISTER0, ®ister_original_data);
- }
- else if(gpio_port == 1)
- {
- nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, INPUT_PORT_REGISTER1, ®ister_original_data);
- }
- switch(gpio_num)
- {
- case 0x01: gpio_status = register_original_data & gpio_num;break;
- case 0x02: gpio_status = (register_original_data & gpio_num) >> 1;break;
- case 0x04: gpio_status = (register_original_data & gpio_num) >> 2;break;
- case 0x08: gpio_status = (register_original_data & gpio_num) >> 3;break;
- case 0x10: gpio_status = (register_original_data & gpio_num) >> 4;break;
- case 0x20: gpio_status = (register_original_data & gpio_num) >> 5;break;
- case 0x40: gpio_status = (register_original_data & gpio_num) >> 6;break;
- case 0x80: gpio_status = (register_original_data & gpio_num) >> 7;break;
- default: break;
- }
- return gpio_status;
- }
- #include "FreeRTOS.h"
- #include "FreeRTOSConfig.h"
- #include "task.h"
- #include "hal_sc7a20.h"
- int32_t hc32_flash_write(uint32_t write_addr,uint8_t *buffer,uint32_t number);
- void nca9555_task(void *argv)
- {
- nca9555_set_output_mode(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03);
- uint8_t tmp[256] = "asdasd00";
- while(1)
- {
- //hc32_flash_write(0X22000, tmp, 232);
- //nca9555_set_gpio_output_status(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03,HIGH);
- vTaskDelay(500);
- //nca9555_set_gpio_output_status(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03,LOW);
- vTaskDelay(18000);
- //hc32_flash_write(0X22000, tmp, 256);
- }
- }
|