RT-Thread 软件包-软件包分类-外设-pcf8574①
RT-Thread 软件包-软件包分类-外设-pcf8574①
pcf8574 软件包
中文页
介绍
pcf8574
软件包是 RT-Thread 针对 I2C 并行口扩展电路 PCF8574T 推出的一个软件包,兼容 PCF8574A。使用这个软件包,可以在 RT-Thread 上非常方便的使用该器件,并且支持一个 I2C 总线上挂载多个 PCF8574T。
本文主要介绍该软件包的使用方式、API,以及 MSH
测试命令。
目录结构
pcf8574
│ README.md // 软件包说明
│ pcf8574.c // 源文件
│ pcf8574.h // 头文件
│ pcf8574_sample.c // 软件包使用示例代码
│ SConscript // RT-Thread 默认的构建脚本
│ LICENSE // 许可证文件
许可证
pcf8574 遵循 Apache-2.0 许可,详见 LICENSE
文件。
依赖
- RT_Thread 3.0+
- i2c 设备驱动
获取方式
使用 pcf8574 package
需要在 RT-Thread 的包管理中选中它,具体路径如下:
RT-Thread online packages
peripheral libraries and drivers --->
pcf8574: Remote 8-bit I/O expander for I2C-bus --->
进入 pcf8574 软件包的配置菜单按自己的需求进行具体的配置
--- pcf8574: Remote 8-bit I/O expander for I2C-bus
[*] Enable pcf8574 sample
Version (latest) --->
Enable pcf8574 sample :开启 pcf8574 使用示例
配置完成后让 RT-Thread 的包管理器自动更新,或者使用 pkgs --update 命令更新包到 BSP 中。
使用方法
pcf8574 软件包的使用流程一般如下:
- 初始化 pcf8574 设备
pcf8574_init
- 进行 IO 的操作
- 使用 API
pcf8574_port_read/pcf8574_port_write
同时操作 8 路 IO - 使用 API
pcf8574_pin_read/pcf8574_pin_write
单独操作其中一 路 IO
- 使用 API
详细的使用方法可以参考pcf8574 示例程序 。
MSH 测试命令
如果开启了 pcf8574 软件包的示例程序,就会导出 pcf8574_sample
命令到控制台。调用之后默认会在 i2c1
总线上探测地址为 0x20
的 PCF8574 设备,并会操作扩展端口的第 0 口进行测试。运行结果如下:
msh >pcf8574_sample
[D/pcf8574] pcf8574 init done
The value of pcf8574.P0 is 0
The value of pcf8574.P0 is 1
msh >
注意事项
暂无。
pcf8574 package
English
Introduction
The pcf8574
software package is a software package developed by RT-Thread for the I2C parallel port expansion circuit PCF8574T, compatible with PCF8574A. Using this software package, you can use the device on RT-Thread very conveniently, and support multiple PCF8574Ts mounted on an I2C bus.
This article mainly introduces the use of the package, API, and MSH
test commands.
Directory Structure
pcf8574
│ README.md // package description
│ pcf8574.c // source file
│ pcf8574.h // header file
│ pcf8574_sample.c // Sample code for software package
│ SConscript // RT-Thread default build script
│ LICENSE // License file
License
pcf8574 complies with the Apache-2.0 license, see the LICENSE
file for details.
Dependence
- RT_Thread 3.0+
- i2c device driver
method of obtaining
To use pcf8574 package
, you need to select it in the package management of RT-Thread. The specific path is as follows:
RT-Thread online packages
peripheral libraries and drivers --->
pcf8574: Remote 8-bit I/O expander for I2C-bus --->
Enter the configuration menu of the pcf8574 software package for specific configuration according to your needs
--- pcf8574: Remote 8-bit I/O expander for I2C-bus
[*] Enable pcf8574 sample
Version (latest) --->
Enable pcf8574 sample: Enable pcf8574 sample
After the configuration is complete, let the RT-Thread package manager automatically update, or use the pkgs --update command to update the package to the BSP.
Instructions
The use process of pcf8574 software package is generally as follows:
- Initialize pcf8574 device
pcf8574_init
- Perform IO operations
- Use API
pcf8574_port_read/pcf8574_port_write
to operate 8 channels of IO at the same time - Use API
pcf8574_pin_read/pcf8574_pin_write
to operate one of the IOs separately
- Use API
For detailed usage, please refer to pcf8574 sample program.
MSH Test Command
If the sample program of the pcf8574 software package is enabled, the command pcf8574_sample
will be exported to the console. After the call, it will detect the PCF8574 device with the address of 0x20
on the i2c1
bus by default, and will operate the port 0 of the expansion port for testing. The results are as follows:
msh >pcf8574_sample
[D/pcf8574] pcf8574 init done
The value of pcf8574.P0 is 0
The value of pcf8574.P0 is 1
msh>
Precautions
Nothing.
示例代码
…\pcf8574.c
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
*
* Change Logs:
* Date Author Notes
* 2018-11-21 SummerGift first version
* 2018-11-22 flybreak Make the first version of pcf8574's package
*/
#include "pcf8574.h"
#define DBG_ENABLE
#define DBG_SECTION_NAME "pcf8574"
#define DBG_LEVEL DBG_INFO
#define DBG_COLOR
#include <rtdbg.h>
rt_uint8_t pcf8574_port_read(pcf8574_device_t dev)
{
rt_uint8_t value;
rt_device_read(&dev->bus->parent, dev->i2c_addr, &value, 1);
return value;
}
void pcf8574_port_write(pcf8574_device_t dev, rt_uint8_t value)
{
rt_device_write(&dev->bus->parent, dev->i2c_addr, &value, 1);
}
rt_uint8_t pcf8574_pin_read(pcf8574_device_t dev, rt_uint8_t bit)
{
rt_uint8_t data;
data = pcf8574_port_read(dev);
if (data & (1 << bit))
return 1;
else
return 0;
}
void pcf8574_pin_write(pcf8574_device_t dev, rt_uint8_t bit, rt_uint8_t value)
{
rt_uint8_t data;
data = pcf8574_port_read(dev);
if (value == 0)
data &= ~(1 << bit);
else
data |= 1 << bit;
pcf8574_port_write(dev, data);
}
pcf8574_device_t pcf8574_init(const char *dev_name, rt_uint8_t i2c_addr)
{
rt_uint8_t buffer[] = { 0xFF };
pcf8574_device_t dev = RT_NULL;
RT_ASSERT(dev_name);
dev = rt_calloc(1, sizeof(struct pcf8574_device));
if (dev == RT_NULL)
{
LOG_E("Can't allocate memory for pcf8574 device on '%s' ", dev_name);
goto __exit;
}
dev->bus = (struct rt_i2c_bus_device *)rt_device_find(dev_name);
if (dev->bus == RT_NULL)
{
LOG_E("i2c_bus %s for PCF8574 not found!", dev_name);
goto __exit;
}
if (i2c_addr != RT_NULL)
dev->i2c_addr = i2c_addr;
else
dev->i2c_addr = PCF8574_ADDR_DEFAULT;
if (rt_device_open(&dev->bus->parent, RT_NULL) != RT_EOK)
{
LOG_D("i2c_bus %s for PCF8574 opened failed!", dev_name);
goto __exit;
}
rt_device_write(&dev->bus->parent, dev->i2c_addr, &buffer, 1);
LOG_D("pcf8574 init done", dev_name);
return dev;
__exit:
if (dev != RT_NULL)
rt_free(dev);
return RT_NULL;
}
void pcf8574_deinit(struct pcf8574_device *dev)
{
RT_ASSERT(dev);
rt_free(dev);
}