注意点:

  • 如果在设置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. [AngularJS] $scope.$watch

    /** * Created by Answer1215 on 11/13/2014. */ function MainCtrl($scope){ function isLongEnough (pwd) ...

  2. 用一段直径为1的原木制作截面是矩形的横梁,问梁的宽度x和高度y是怎样的比例时,才能使梁的强度最大?

    解:根据经验,梁的强度与高度平方成正比,和宽度成反比,可以写成 q=y2x 又有x2+y2=1,得到 q=(1-x2)x=x-x3 而导数q'=1-3x2,当q'=0时,q有极值 q'=0时,x=1/ ...

  3. docker下搭建gitlab

    [root@localhost ~]# docker run \ > --name='gitlab' \ > -itd \ > --link gitlab_mysql:mysql \ ...

  4. 会说话的HTML--语义化杂谭-TGideas-腾讯游戏官方设计团队

    家里有个熊孩子,经常会有一些意想不到的事情发生:回家的时候,他会笑呵呵冲过来,大声喊着“臭爸爸”:你让他把鞋穿上,他会提起鞋子往楼下扔...在小孩的世界里,他虽然会说话,但不一定明白其中的意思,不能正 ...

  5. iOS 状态栏和导航条配置

    iOS 状态栏和导航条配置 一:隐藏: [self.navigationController setNavigationBarHidden:YES animated:YES]; [[UIApplica ...

  6. Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器)

    Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器) 本篇博客介绍Cocos2d-x 为我们提供的一个类--AssetsManager在Lua中的使用样例,效果 ...

  7. 算法笔记_020:深度优先查找(Java)

    目录 1 问题描述 2 解决方案 2.1 蛮力法 1 问题描述 深度优先查找(depth-first search,DFS)可以从任意顶点开始访问图的顶点,然后把该顶点标记为已访问.在每次迭代的时候, ...

  8. Android File类 根据官方文档理解(转)

    File有四个构造函数        public File(File dir,String name)             参数为File和String,File制定构造的新的File对象的路径 ...

  9. LDAP 中 CN,OU,DC 的含意

    CN, OU, DC 都是 LDAP 连接服务器的端字符串中的区别名称(DN, Distinguished Name) LDAP连接服务器的连接字串格式为:ldap://servername/DN   ...

  10. jquery 常用api 小结2

    *一)jQuery常用方法API实战 (1)DOM简述与分类 A)DOM是一种标准,它独立于平台,语言,浏览器. B)如果项目中,你完全按照DOM标准写代码,你就能在各大主流的浏览器中操作标准控件. ...