[I2C]pca9555应用层测试代码
注意点:
- 如果在设置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应用层测试代码的更多相关文章
- 25 【python入门指南】如何编写测试代码
python如何编写测试代码 python内置了unittest,使得写应用层的单元测试变得超乎寻常的简单. 1,执行单个测试函数 #!/bin/python import unittest clas ...
- .NET单元测试的艺术-3.测试代码
开篇:上一篇我们学习单元测试和核心技术:存根.模拟对象和隔离框架,它们是我们进行高质量单元测试的技术基础.本篇会集中在管理和组织单元测试的技术,以及如何确保在真实项目中进行高质量的单元测试. 系列目录 ...
- mysql锁 实战测试代码
存储引擎 支持的锁定 MyISAM 表级锁 MEMORY 表级锁 InnoDB 行级锁 BDB 页面锁 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高,并发度最低.行级锁:开销 ...
- 使用Microsoft Fakes隔离测试代码
在单元测试(Unit Test)中我们遇到的问题之一是:假如被测试组件(类或项目)为A,组件A依赖于组件B,那么在组件A的单元测试ATest中测试A时,也需要依赖于B,在B发生改动后,就可能影响到A的 ...
- iOS开发:XCTest单元测试(附上一个单例的测试代码)
测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
- 在内核中异步请求设备固件firmware的测试代码
在内核中异步请求设备固件firmware的测试代码 static void ghost_load_firmware_callback(const struct firmware *fw, void * ...
- x264测试代码
建立一个工程,将头文件,库文件加载到工程,测试代码如下:#include <iostream>#include <string>#include "stdint.h& ...
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)
MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...
- Git合并开发代码分支到测试代码分支
——转载请注明出自天外归云的博客园 用TortoiseGit下载代码到本地 首先需要在本机安装好TortoiseGit.然后在随便哪个路径下比如D盘,右键“Git Clone”: 然后URL处选择项目 ...
随机推荐
- Hive配置与操作实践
Hive配置与操作实践 @(Hadoop) 安装hive hive的安装十分简单,只需要在一台服务器上部署即可. 上传hive安装包,解压缩,将其配入环境变量. mysql的设置 在要作为元数据库的m ...
- UNIX网络编程读书笔记:端口号、套接口对和套接口
端口号 端口号(port number):16位整数,用来区分不同的进程. 服务器使用的端口号:TCP和UDP定义了一组众所周知的端口(well-known port),用于标识众所周知的服务. 客户 ...
- Flutter 动画使用
旋转动画 透明度变换动画 在Android中,可以通过View.animate()对视图进行动画处理,那在Flutter中怎样才能对Widget进行处理 在Flutter中,可以通过动画库给wid ...
- 专业版Unity技巧分享:使用定制资源配置文件 ScriptableObject
http://unity3d.9tech.cn/news/2014/0116/39639.html 通常,在游戏的开发过程中,最终会建立起一些组件,通过某种形式的配置文件接收一些数据.这些可能是程序级 ...
- ionic 调用手机的打电话功能
1.需求描述 在ionic项目用调用手机的打电话功能.开始还想找cordova和ng-cordova的插件那,现在H5实现起来特别方便. 2.准备 在cordova中所有的URL Schemes 都是 ...
- JavaScript严格模式下this指向
一般认为:严格模式下this不允许指向全局对象.是函数体是否处于严格模式! 如:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mod ...
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法【转发】
Html.partial和RenderPartial的用法与区别Html.partial和RenderPartial都是输出html片段,区别在于Partial是将视图内容直接生成一个字符串并返回(相 ...
- 理解邮件传输协议(SMTP、POP3、IMAP、MIME)
http://blog.csdn.net/xyang81/article/details/7672745 电子邮件需要在邮件客户端和邮件服务器之间,以及两个邮件服务器之间进行传递,就必须遵循一定的规则 ...
- webservice接口示例(spring+xfire+webservice)
webservice接口示例(spring+xfire+webservice) CreateTime--2018年4月2日17:36:07 Author:Marydon 一.准备工作 1.1 ja ...
- 【LeetCode】96. Unique Binary Search Trees (2 solutions)
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...