使用ioctl 的demo代码
#include <sys/ioctl.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "nca9555.h"
int i2c_init(void)
{
int fd = 0;
//打开IIC总线设备节点
fd = open("/dev/i2c-2", O_RDWR);
return fd;
}
void i2c_deinit(int fd)
{
close(fd);
}
/**
* \brief I2C读数据
*
* \param[in] fd:I2C设备文件描述符
* \param[in] dev_addr:I2C设备地址
* \param[in] reg_addr:寄存器地址
* \param[in] data:指向希望写入的数据地址
* \param[in] len:希望写入的字节个数
*
* \retval 成功返回0,失败返回-1
*
* \note 该函数适用于8位从机地址,且寄存器地址只有1个字节的情况
*/
int i2c_write(int fd, unsigned short dev_addr, unsigned char reg_addr, unsigned char* data, unsigned int len)
{
int ret = -1;
unsigned char buff[256] = { 0 };
buff[0] = reg_addr;
memcpy(&buff[1], data, len);
//写数据是1个msg
struct i2c_msg msg = {
.addr = dev_addr,
.flags = 0,
.len = len + 1,
.buf = buff,
};
struct i2c_rdwr_ioctl_data rdwr_msg = {
.msgs = &msg,
.nmsgs = 1,
};
ret = ioctl(fd, I2C_RDWR, &rdwr_msg);
return ret;
}
/**
* \brief I2C读数据
*
* \param[in] fd:I2C设备文件描述符
* \param[in] dev_addr:I2C设备地址
* \param[in] reg_addr:寄存器地址
* \param[out] data:存放读取到的数据
* \param[in] len:希望读取的字节个数
*
* \retval 成功返回0,失败返回-1
*
* \note 该函数适用于8位从机地址,且寄存器地址只有1个字节的情况
*/
int i2c_read(int fd, unsigned short dev_addr, unsigned char reg_addr, unsigned char* data, unsigned int len)
{
int ret = -1;
//读数据有2个msg
struct i2c_msg msg[2] = {
{
.addr = dev_addr, //设备地址
.flags = 0, //标志,为0表示写数据
.len = 1, //要写的数据的长度
.buf = ®_addr, //要写的数据的地址
},
{
.addr = dev_addr, //设备地址
.flags = I2C_M_RD, //标志,I2C_M_RD表示主机向主机读数据
.len = len, //要读取的数据的长度
.buf = data, //读取的数据存放的地址
},
};
struct i2c_rdwr_ioctl_data rdwr_msg = {
.msgs = msg,
.nmsgs = 2,
};
ret = ioctl(fd, I2C_RDWR, &rdwr_msg);
return ret;
}
void set_all_io_direction(int fd, IO_NUM directon)
{
uint8_t data[2] = {0};
memcpy(&data, (char*)&(directon.num), 2);
printf("set dir data[0]:%#x, data[1]:%#x \r\n", data[0], data[1]);
i2c_write(fd, NCA9555_ADDR, CONFIG_ADDR0, &data[0], 1);
i2c_write(fd, NCA9555_ADDR, CONFIG_ADDR1, &data[1], 1);
}
uint8_t read_io(int fd, uint8_t pin_num)
{
uint8_t data = 0;
uint8_t value = 0;
if (pin_num < 8)
{
i2c_read(fd, NCA9555_ADDR, INPUT_ADDR0, &data, 1);
value = data & (0x1 << pin_num);
value = value >> pin_num;
}
else
{
i2c_read(fd, NCA9555_ADDR, INPUT_ADDR1, &data, 1);
value = data & (0x1 << (pin_num - 8));
value = value >> (pin_num - 8);
}
return value;
}
uint8_t read_io_1_8(int fd)
{
uint8_t data = 0;
i2c_read(fd, NCA9555_ADDR, INPUT_ADDR0, &data, 1);
return data;
}
uint8_t read_io_9_16(int fd)
{
uint8_t data = 0;
i2c_read(fd, NCA9555_ADDR, INPUT_ADDR1, &data, 1);
return data;
}
void write_io(int fd, uint8_t pin_num, uint8_t pin_value)
{
uint8_t data = 0;
if (pin_num < 8)
{
i2c_read(fd, NCA9555_ADDR, INPUT_ADDR0, &data, 1);
if (pin_value == 1)
{
data |= (0x1 << pin_num);
}
else
{
data &=~ (0x1 << pin_num);
}
i2c_write(fd, NCA9555_ADDR, OUTPUT_ADDR0, &data, 1);
}
else
{
i2c_read(fd, NCA9555_ADDR, INPUT_ADDR1, &data, 1);
if (pin_value == 1)
{
data |= (0x1 << (pin_num - 8));
}
else
{
data &=~ (0x1 << (pin_num - 8));
}
i2c_write(fd, NCA9555_ADDR, OUTPUT_ADDR1, &data, 1);
}
}
写时序
读时序
实际测试结果: