注意点:

  • 如果在设置I2C_SLAVE的时候,提示device_busy,可以使用I2C_SLAVE_FORCE, 在驱动里面二者对应同一个case语句
  • 应用层可以调用接口:i2c_smbus_write_word_data(fd, __, __);和i2c_smbus_read_word_data(fd,__);

分享

问题如下

1. 应用程序中直接fd句柄是整个I2C0总线的文件句柄,而只是在set地址的时候,将I2C address设置下去,后面操作该芯片还是操作全局I2C0总线上这个文件,并没有指定去操作该芯片,这其中是怎么做到的?这样的话,如果在一个main程序中, 操作同一总线上不同i2c设备,而文件句柄是同一个,这要如何操作?

--------------------------------------------------------------------------------------------

驱动方面

首先配置I2C内核驱动,将pca9555的源码built-in进入(这里根据需要可能要配thermal的驱动),然后在devicetree中根据pca9555硬件I2C地址配置节点。

测试源码

// I2C test program for a PCA9555

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h> // I2C Linux device handle
int g_i2cFile; // open the Linux device
void i2cOpen()
{
g_i2cFile = open("/dev/i2c-0", O_RDWR);
if (g_i2cFile < ) {
perror("i2cOpen");
exit();
}
} // close the Linux device
void i2cClose()
{
close(g_i2cFile);
} // set the I2C slave address for all subsequent I2C device transfers
void i2cSetAddress(int address)
{
if (ioctl(g_i2cFile, I2C_SLAVE, address) < ) {
perror("i2cSetAddress");
exit();
}
} // write a 16 bit value to a register pair
// write low byte of value to register reg,
// and high byte of value to register reg+1
void pca9555WriteRegisterPair(uint8_t reg, uint16_t value)
{
uint8_t data[];
data[] = reg;
data[] = value & 0xff;
data[] = (value >> ) & 0xff;
if (write(g_i2cFile, data, ) != ) {
perror("pca9555SetRegisterPair");
}
} // read a 16 bit value from a register pair
uint16_t pca9555ReadRegisterPair(uint8_t reg)
{
uint8_t data[];
data[] = reg;
if (write(g_i2cFile, data, ) != ) {
perror("pca9555ReadRegisterPair set register");
}
if (read(g_i2cFile, data, ) != ) {
perror("pca9555ReadRegisterPair read value");
}
return data[] | (data[] << );
} // set IO ports to input, if the corresponding direction bit is 1,
// otherwise set it to output
void pca9555SetInputDirection(uint16_t direction)
{
pca9555WriteRegisterPair(, direction);
} // set the IO port outputs
void pca9555SetOutput(uint16_t value)
{
pca9555WriteRegisterPair(, value);
} // read the IO port inputs
uint16_t pca9555GetInput()
{
return pca9555ReadRegisterPair();
} int main(int argc, char** argv)
{
// test output value
int v = ; // direction of the LED animation
int directionLeft = ; // open Linux I2C device
i2cOpen(); // set address of the PCA9555
i2cSetAddress(0x20); // set input for IO pin 15, rest output
pca9555SetInputDirection( << ); // LED animation loop
while () {
// if button is pressed, invert output
int xor;
if (pca9555GetInput() & 0x8000) {
xor = ;
} else {
xor = 0xffff;
} // set current output
pca9555SetOutput(v ^ xor); // animate LED position
if (directionLeft) {
v <<= ;
} else {
v >>= ;
}
if (v == 0x6000) {
directionLeft = ;
}
if (v == ) {
directionLeft = ;
} // wait 100 ms for next animation step
usleep();
} // close Linux I2C device
i2cClose(); return ;
}

[I2C]pca9555应用层测试代码的更多相关文章

  1. 25 【python入门指南】如何编写测试代码

    python如何编写测试代码 python内置了unittest,使得写应用层的单元测试变得超乎寻常的简单. 1,执行单个测试函数 #!/bin/python import unittest clas ...

  2. .NET单元测试的艺术-3.测试代码

    开篇:上一篇我们学习单元测试和核心技术:存根.模拟对象和隔离框架,它们是我们进行高质量单元测试的技术基础.本篇会集中在管理和组织单元测试的技术,以及如何确保在真实项目中进行高质量的单元测试. 系列目录 ...

  3. mysql锁 实战测试代码

    存储引擎 支持的锁定 MyISAM 表级锁 MEMORY 表级锁 InnoDB 行级锁 BDB 页面锁 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低.行级锁:开销 ...

  4. 使用Microsoft Fakes隔离测试代码

    在单元测试(Unit Test)中我们遇到的问题之一是:假如被测试组件(类或项目)为A,组件A依赖于组件B,那么在组件A的单元测试ATest中测试A时,也需要依赖于B,在B发生改动后,就可能影响到A的 ...

  5. iOS开发:XCTest单元测试(附上一个单例的测试代码)

    测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...

  6. 在内核中异步请求设备固件firmware的测试代码

    在内核中异步请求设备固件firmware的测试代码 static void ghost_load_firmware_callback(const struct firmware *fw, void * ...

  7. x264测试代码

    建立一个工程,将头文件,库文件加载到工程,测试代码如下:#include <iostream>#include <string>#include "stdint.h& ...

  8. Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)

    MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...

  9. Git合并开发代码分支到测试代码分支

    ——转载请注明出自天外归云的博客园 用TortoiseGit下载代码到本地 首先需要在本机安装好TortoiseGit.然后在随便哪个路径下比如D盘,右键“Git Clone”: 然后URL处选择项目 ...

随机推荐

  1. Android面试,BroadCastReceiver的两种注册方式的异同

    在Android手机应用程序中开发中,需要用到BroadcastReceiver来监听广播的消息.在自定义好BroadcastReceiver ,需要对其进行注册,注册有两种方法: 一种是在代码当中注 ...

  2. RS查询报错之递归公用表表达式不包含顶级 UNION ALL运算符

    在FM里面涉及模型的时候,修改了物理层的查询SQL如下 select * from TARGET_VISIT_GH where ghksdm in(select dept_id from DIM_BI ...

  3. (转) 问题解决:Apache: You don't have permission to access / on this server

    问题解决:Apache: You don't have permission to access / on this server 转自:http://blog.csdn.net/crazyboy20 ...

  4. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. .NET破解之爱奇迪(三)

    本教程只能用于学习研究,不可进行任何商业用途.如有使用,请购买正版,尊重他人劳动成果和知识产权! .NET破解之爱奇迪(一) .NET破解之爱奇迪(二) 一打开软件,就看到各种注册和未注册提示信息,就 ...

  6. POI按行读取word,并去掉属性标签内容:超链接

    public String readDoc(File file) { StringBuffer buffer = new StringBuffer(); InputStream input = nul ...

  7. 彻底告别加解密模块代码拷贝-JCE核心Cpiher详解

    前提 javax.crypto.Cipher,翻译为密码,其实叫做密码器更加合适.Cipher是JCA(Java Cryptographic Extension,Java加密扩展)的核心,提供基于多种 ...

  8. Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  9. linux命令创建和修改用户及密码

    linux下创建用户 1.添加ftp用户 useradd ftpname -d /home/ftp passwd ftppwd 以下操作都以root权限进行: service vsftpd start ...

  10. Linux下connect超时处理

    1.前言 最近在写一个测试工具,要求快速的高效率的扫描出各个服务器开放了哪些端口.当时想了一下,ping只能检测ip,判断服务器的网络是连通的,而不能判断是否开放了端口.我们知道端口属于网络的传输层, ...