Linux/安卓系统使用CH347转接I2C功能有三种应用方式:
1. 使用CH34X_MPHSI_Master总线驱动为系统扩展原生I2C Master,此方式无需进行单独的应用层编程;
2. 使用CH341PAR_LINUX字符设备驱动,此方式需要配合使用厂商提供的库文件,编程访问设备功能;
3. 使用CH341PAR_ANDROID免驱APP,基于安卓原生USB Host API进行二次开发,此方式无需设备驱动;
基于方式一,可参考如下几篇博客:
https://blog.csdn.net/WCH_TechGroup/article/details/130093377在安卓/Linux主机上经常会遇到CPU原生SPI/I2C/GPIO Master资源通道不够或者功性能不满足实际产品需求的情况,基于USB2.0高速USB转接芯片CH347,配合厂商提供的USB转MPHSI(Multi Peripheral Serial Line)Master总线驱动(CH34X-MSPI-Master)可轻松实现为系统扩展SPI和I2C总线、GPIO Expander、中断信号等。_扩展spi芯片https://blog.csdn.net/WCH_TechGroup/article/details/130093377CH34X-MPHSI高速Master扩展应用—《i2c-tools使用》_PC技术小能手的博客-CSDN博客本文介绍,基于USB2.0高速USB转接芯片CH347,配合厂商提供的USB转MPHSI(Multi Protocol High-Speed Serial Interface)Master总线驱动(CH34X-MPHSI-Master)为系统扩展I2C总线的用法,除此之外,还可以扩展SPI总线和GPIO等资源。驱动软件正常工作后,会在系统下创建新的I2C Master,拥有独立的bus num,原I2C器件的设备驱动可直接挂载到该总线上,无需任何修改。https://blog.csdn.net/WCH_TechGroup/article/details/131538476CH34X-MPHSI高速Master扩展应用—I2C设备调试-CSDN博客本文介绍,基于USB2.0高速USB转接芯片CH347,配合厂商提供的USB转MPHSI(Multi Protocol High-Speed Serial Interface)Master总线驱动(CH34X-MPHSI-Master)为系统扩展I2C总线的用法,除此之外,还可以扩展SPI总线和GPIO等资源。驱动软件正常工作后,会在系统下创建新的I2C Master,拥有独立的bus num,原I2C器件的设备驱动可直接通过DTS配置文件或者sysfs节点挂载到该总线上,原有设备驱动无需任何修改。https://blog.csdn.net/WCH_TechGroup/article/details/133353169本篇博客仅对于方式二做重点介绍:
一、应用框图
二、资源列表
demo:应用软件示例
driver:驱动软件
lib:应用库文件,提供大部分CPU架构的动态库和静态库
三、工作原理
驱动软件正常工作时,会自动在系统/dev目录下创建字符设备,名称为:/dev/ch34x_pis*。基于此节点设备,配合 libch347 动态库,ch347demo应用程序实现对 CH347 芯片的硬件资源的访问。
此方式适用于不需要依赖于原有外设驱动工作的应用场景,使用字符设备实现对于外设的读写功能。类似于串口应用,通过访问 /dev/tty* 设备实现相应设备的:打开、关闭、读写等操作。
四、使用步骤
使用HID驱动模式,直接使用系统自带的 hidraw 驱动即可,使用厂商驱动模式,需要编译使用 CH341PAR_LINUX 资料中driver下的驱动文件,链接地址:
CH341PAR_LINUX.ZIP - 南京沁恒微电子股份有限公司USB转JTAG/SPI/I2C/并口/GPIO等接口的Linux设备驱动程序,支持CH341的USB转SPI/I2C/EPP并口/MEM并口等,支持CH347的480Mbps高速USB转JTAG/SPI/I2C/GPIO等,支持32/64位操作系统。https://www.wch.cn/downloads/CH341PAR_LINUX_ZIP.htmlGitHub - WCHSoftGroup/ch341par_linuxContribute to WCHSoftGroup/ch341par_linux development by creating an account on GitHub.https://github.com/WCHSoftGroup/ch341par_linux驱动使用流程如下:
(1)驱动加载
1. unzip CH341PAR.ZIP
2. cd driver
3. sudo make install
插入CH347硬件设备,此时会自动在 /dev 目录下创建字符设备:ch34x_pis*,如下所示:
至此,代表驱动程序和芯片工作正常。
(2)拷贝库文件至系统库路径下,此处以X64 CPU的 ch347 动态库为例:
sudo cp lib/x64/dynamic/libch347.so /usr/lib
(3)应用编程
/**
* CH347OpenDevice - open device
* @pathname: device path in /dev directory
*
* The function return positive file descriptor if successful, others if fail.
*/
extern int CH347OpenDevice(const char *pathname);
/**
* CH347CloseDevice - close device
* @fd: file descriptor of device
*
* The function return true if successful, false if fail.
*/
extern bool CH347CloseDevice(int fd);
/**
* CH347I2C_Set - configure i2c interface in stream mode
* @fd: file descriptor of device
* @iMode: stream mode
* ->bit0~2: set I2C SCL rate
* --> 000 : low rate 20KHz
* --> 001 : standard rate 100KHz
* --> 010 : fast rate 400KHz
* --> 011 : high rate 750KHz
* --> 100 : rate 50KHz
* --> 101 : standard rate 200KHz
* --> 110 : fast rate 1MHz
* other bits must keep 0
*
* The function return true if successful, false if fail.
*/
extern bool CH347I2C_Set(int fd, int iMode);
/**
* CH347I2C_SetStretch - I2C Clock Stretch function control
* @fd: file descriptor of device
* @enable: I2C Clock Stretch enable, 1 : enable, 0 : disable
*
* The function return true if successful, false if fail.
*/
extern bool CH347I2C_SetStretch(int fd, bool enable);
/**
* CH347I2C_SetDelaymS - delay operation
* @fd: file descriptor of device
* @iDelay: delay time in millseconds
*
* The function return true if successful, false if fail.
*/
extern bool CH347I2C_SetDelaymS(int fd, int iDelay);
/**
* CH347StreamI2C - write/read i2c in stream mode
* @fd: file descriptor of device
* @iWriteLength: write length
* @iWriteBuffer: pointer to write buffer
* @iReadLength: read length
* @oReadBuffer: pointer to read buffer
*
* The function return true if successful, false if fail.
*/
extern bool CH347StreamI2C(int fd, int iWriteLength, void *iWriteBuffer, int iReadLength, void *oReadBuffer);
/**
* CH347StreamI2C_RetAck - write/read i2c in stream mode
* @fd: file descriptor of device
* @iWriteLength: write length
* @iWriteBuffer: pointer to write buffer
* @iReadLength: read length
* @oReadBuffer: pointer to read buffer
* @retAck: pointer to available ack count
*
* The function return true if successful, false if fail.
*/
extern bool CH347StreamI2C_RetAck(int fd, int iWriteLength, void *iWriteBuffer, int iReadLength, void *oReadBuffer,
int *retAck);
/**
* CH347ReadEEPROM - read data from eeprom
* @fd: file descriptor of device
* @iEepromID: eeprom type
* @iAddr: address of eeprom
* @iLength: read length
* @oBuffer: pointer to read buffer
*
* The function return true if successful, false if fail.
*/
extern bool CH347ReadEEPROM(int fd, EEPROM_TYPE iEepromID, int iAddr, int iLength, uint8_t *oBuffer);
/**
* CH347WriteEEPROM - write data to eeprom
* @fd: file descriptor of device
* @iEepromID: eeprom type
* @iAddr: address of eeprom
* @iLength: write length
* @iBuffer: pointer to write buffer
*
* The function return true if successful, false if fail.
*/
extern bool CH347WriteEEPROM(int fd, EEPROM_TYPE iEepromID, int iAddr, int iLength, uint8_t *iBuffer);
如上API接口函数,根据不同的业务场景可以选用不同的函数。
CH347StreamI2C: 适用于多字节的设备地址,或设备地址后紧跟寄存器地址,或连续的多字节读写。
CH347StreamI2C_RetAck:与CH347StreamI2C接口函数功能相同,区别在于参数上增加返回此次I2C通信的设备ACK个数。
CH347ReadEEPROM/CH347WriteEEPROM: 适用于直接操作EEPROM存储器件。
操作流程:
注:
1、CH347I2C_SetDelaymS 用于设定I2C读写多字节时,字节间的间隔时间,可用于操作对数据通讯字节间有间隔的设备,此API为可选项。
2、CH347I2C_SetStretch 用于启用/禁用I2C时钟延展功能,该API仅针对CH347T芯片有效,CH347F 默认硬件自动开启时钟延展,此API为可选项。
CH347StreamI2C 函数说明
iWriteLength:I2C Write的字节长度
iWriteBuffer:I2C Write的缓冲区内容,该缓冲区内容会经过SDA信号线对外输出。首字节地址通常是设备地址及读写位。如设备地址是0x50,I2C写操作时首字节为:0x50 << 1 = 0xA0,I2C读操作时首字节为:0x50 << 1 | BIT(0) = 0xA1。
iReadLength:I2C Read的字节长度
oReadBuffer:API成功返回后,其内容是从SDA信号线上采集的数据。
返回值:当设备成功产生ACK应答,并且应用层参数传递无误,API返回成功,否则失败。
示例1:EEPROM 24C256的设备地址是:0x50, 从其3200H开始的地址读取6字节的数据。
uint8_t OutBuf[3] = {0xA0, 0x32, 0x00};
uint8_ InBuf[6];
CH347StreamI2C(fd, 3, OutBuf, 6, inBuf);
对应I2C总线时序如下:
示例2:EEPROM 24C256的设备地址是:0x50, 从其3200H开始的地址写入2个字节的数据,内容0x11,0x22。
uint8_t OutBuf[5];
OutBuf[0] = 0xA0;
OutBuf[1] = 0x32;
OutBuf[2] = 0x00;
OutBuf[3] = 0x11;
OutBuf[4] = 0x22;
CH347StreamI2C(fd, 5, OutBuf, 0, NULL);
对应I2C总线时序如下: