Skip to content

Commit dd2adda

Browse files
authored
Merge pull request #1 from Guozhanxin/master
【添加】添加第一个版本的文件
2 parents b8c0b3d + 7dbb2f1 commit dd2adda

File tree

5 files changed

+338
-2
lines changed

5 files changed

+338
-2
lines changed

README.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
1-
# pcf8574
2-
Remote 8-bit I/O expander for I2C-bus
1+
# pcf8574 软件包
2+
3+
## 介绍
4+
5+
`pcf8574` 软件包是 RT-Thread 针对 I2C 并行口扩展电路 PCF8574T 推出的一个软件包,兼容 PCF8574A。使用这个软件包,可以在 RT-Thread 上非常方便的使用该器件,并且支持一个 I2C 总线上挂载多个 PCF8574T。
6+
7+
本文主要介绍该软件包的使用方式、API,以及 `MSH` 测试命令。
8+
9+
### 目录结构
10+
11+
```
12+
pcf8574
13+
│ README.md // 软件包说明
14+
│ pcf8574.c // 源文件
15+
│ pcf8574.h // 头文件
16+
│ pcf8574_sample.c // 软件包使用示例代码
17+
│ SConscript // RT-Thread 默认的构建脚本
18+
│ LICENSE // 许可证文件
19+
```
20+
21+
### 许可证
22+
23+
pcf8574 遵循 Apache-2.0 许可,详见 `LICENSE` 文件。
24+
25+
### 依赖
26+
27+
- RT_Thread 3.0+
28+
- i2c 设备驱动
29+
30+
## 获取方式
31+
32+
使用 `pcf8574 package` 需要在 RT-Thread 的包管理中选中它,具体路径如下:
33+
34+
```
35+
RT-Thread online packages
36+
peripheral libraries and drivers --->
37+
pcf8574: Remote 8-bit I/O expander for I2C-bus --->
38+
```
39+
40+
进入 pcf8574 软件包的配置菜单按自己的需求进行具体的配置
41+
42+
```
43+
--- pcf8574: Remote 8-bit I/O expander for I2C-bus
44+
[*] Enable pcf8574 sample
45+
Version (latest) --->
46+
```
47+
48+
**Enable pcf8574 sample** :开启 pcf8574 使用示例
49+
50+
配置完成后让 RT-Thread 的包管理器自动更新,或者使用 pkgs --update 命令更新包到 BSP 中。
51+
52+
## 使用方法
53+
54+
pcf8574 软件包的使用流程一般如下:
55+
56+
1. 初始化 pcf8574 设备 `pcf8574_init`
57+
2. 进行 IO 的操作
58+
- 使用 API `pcf8574_port_read/pcf8574_port_write` 同时操作 8 路 IO
59+
- 使用 API `pcf8574_pin_read/pcf8574_pin_write` 单独操作其中一 路 IO
60+
61+
详细的使用方法可以参考[pcf8574 示例程序](pcf8574_sample.c)
62+
63+
## MSH 测试命令
64+
65+
如果开启了 pcf8574 软件包的示例程序,就会导出 `pcf8574_sample` 命令到控制台。调用之后默认会在 `i2c1`总线上探测地址为 `0x20` 的 PCF8574 设备,并会操作扩展端口的第 0 口进行测试。运行结果如下:
66+
67+
```
68+
msh >pcf8574_sample
69+
[D/pcf8574] pcf8574 init done
70+
The value of pcf8574.P0 is 0
71+
The value of pcf8574.P0 is 1
72+
msh >
73+
```
74+
75+
## 注意事项
76+
77+
暂无。
78+
79+
## 联系方式
80+
81+
- 维护:[guozhanxin](https://github.com/Guozhanxin)
82+
- 主页:<https://github.com/RT-Thread-packages/pcf8574 >

SConscript

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from building import *
2+
Import('rtconfig')
3+
4+
src = []
5+
cwd = GetCurrentDir()
6+
7+
# add pcf8574 src files.
8+
if GetDepend('PKG_USING_PCF8574'):
9+
src += Glob('pcf8574.c')
10+
11+
if GetDepend('PKG_USING_PCF8574_SAMPLE'):
12+
src += Glob('pcf8574_sample.c')
13+
14+
# add pcf8574 include path.
15+
path = [cwd]
16+
17+
# add src and include to group.
18+
group = DefineGroup('pcf8574', src, depend = ['PKG_USING_PCF8574'], CPPPATH = path)
19+
20+
Return('group')

pcf8574.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*
7+
* Change Logs:
8+
* Date Author Notes
9+
* 2018-11-21 SummerGift first version
10+
* 2018-11-22 flybreak Make the first version of pcf8574's package
11+
*/
12+
13+
#include "pcf8574.h"
14+
15+
#define DBG_ENABLE
16+
#define DBG_SECTION_NAME "pcf8574"
17+
#define DBG_LEVEL DBG_INFO
18+
#define DBG_COLOR
19+
#include <rtdbg.h>
20+
21+
uint8_t pcf8574_port_read(pcf8574_device_t dev)
22+
{
23+
uint8_t value;
24+
25+
rt_device_read(&dev->bus->parent, dev->i2c_addr, &value, 1);
26+
27+
return value;
28+
}
29+
30+
void pcf8574_port_write(pcf8574_device_t dev, uint8_t value)
31+
{
32+
rt_device_write(&dev->bus->parent, dev->i2c_addr, &value, 1);
33+
}
34+
35+
uint8_t pcf8574_pin_read(pcf8574_device_t dev, uint8_t bit)
36+
{
37+
uint8_t data;
38+
data = pcf8574_port_read(dev);
39+
40+
if (data & (1 << bit))
41+
return 1;
42+
else
43+
return 0;
44+
}
45+
46+
void pcf8574_pin_write(pcf8574_device_t dev, uint8_t bit, uint8_t value)
47+
{
48+
uint8_t data;
49+
data = pcf8574_port_read(dev);
50+
51+
if (value == 0)
52+
data &= ~(1 << bit);
53+
else
54+
data |= 1 << bit;
55+
56+
pcf8574_port_write(dev, data);
57+
}
58+
59+
pcf8574_device_t pcf8574_init(const char *dev_name, rt_uint8_t i2c_addr)
60+
{
61+
uint8_t buffer[] = { 0xFF };
62+
pcf8574_device_t dev = RT_NULL;
63+
64+
RT_ASSERT(dev_name);
65+
66+
dev = rt_calloc(1, sizeof(struct pcf8574_device));
67+
if (dev == RT_NULL)
68+
{
69+
LOG_E("Can't allocate memory for pcf8574 device on '%s' ", dev_name);
70+
goto __exit;
71+
}
72+
73+
dev->bus = (struct rt_i2c_bus_device *)rt_device_find(dev_name);
74+
if (dev->bus == RT_NULL)
75+
{
76+
LOG_E("i2c_bus %s for PCF8574 not found!", dev_name);
77+
goto __exit;
78+
}
79+
80+
if (i2c_addr != RT_NULL)
81+
dev->i2c_addr = i2c_addr;
82+
else
83+
dev->i2c_addr = PCF8574_ADDR_DEFAULT;
84+
85+
if (rt_device_open(&dev->bus->parent, RT_NULL) != RT_EOK)
86+
{
87+
LOG_D("i2c_bus %s for PCF8574 opened failed!", dev_name);
88+
goto __exit;
89+
}
90+
91+
rt_device_write(&dev->bus->parent, dev->i2c_addr, &buffer, 1);
92+
93+
LOG_D("pcf8574 init done", dev_name);
94+
return dev;
95+
96+
__exit:
97+
if (dev != RT_NULL)
98+
rt_free(dev);
99+
100+
return RT_NULL;
101+
}
102+
103+
void pcf8574_deinit(struct pcf8574_device *dev)
104+
{
105+
RT_ASSERT(dev);
106+
107+
rt_free(dev);
108+
}

pcf8574.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*
7+
* Change Logs:
8+
* Date Author Notes
9+
* 2018-11-21 SummerGift first version
10+
* 2018-11-22 flybreak Make the first version of pcf8574's package
11+
*/
12+
13+
#ifndef __PCF8574_H
14+
#define __PCF8574_H
15+
16+
#include <rtthread.h>
17+
#include <rtdevice.h>
18+
19+
#define PCF8574_ADDR_DEFAULT 0x20
20+
21+
/* pcf8574 device structure */
22+
struct pcf8574_device
23+
{
24+
struct rt_i2c_bus_device *bus;
25+
rt_uint8_t i2c_addr;
26+
};
27+
typedef struct pcf8574_device *pcf8574_device_t;
28+
29+
/**
30+
* This function initialize the pcf8574 device.
31+
*
32+
* @param dev_name the name of i2c bus device
33+
* @param i2c_addr the i2c device address for i2c communication,RT_NULL use default address
34+
*
35+
* @return the pointer of device structure, RT_NULL reprensents initialization failed.
36+
*/
37+
pcf8574_device_t pcf8574_init(const char *dev_name, rt_uint8_t i2c_addr);
38+
39+
/**
40+
* This function releases memory
41+
*
42+
* @param dev the pointer of device structure
43+
*/
44+
void pcf8574_deinit(struct pcf8574_device *dev);
45+
46+
/**
47+
* This function read the data port of pcf8574.
48+
*
49+
* @param dev the pointer of device structure
50+
*
51+
* @return the state of data port. 0xFF meas all pin is high.
52+
*/
53+
uint8_t pcf8574_port_read(pcf8574_device_t dev);
54+
55+
/**
56+
* This function sets the status of the data port.
57+
*
58+
* @param dev the pointer of device structure
59+
* @param port_val the port value you want to set, 0xFF meas all pin output high.
60+
*/
61+
void pcf8574_port_write(pcf8574_device_t dev, uint8_t port_val);
62+
63+
/**
64+
* This function read the specified port pin of the pcf8574.
65+
*
66+
* @param dev the pointer of device structure
67+
* @param pin the specified pin of the data port
68+
*
69+
* @return the status of the specified data port pin, 0 is low, 1 is high.
70+
*/
71+
uint8_t pcf8574_pin_read(pcf8574_device_t dev, uint8_t pin);
72+
73+
/**
74+
* This function sets the status of the specified port pin.
75+
*
76+
* @param dev the pointer of device structure
77+
* @param pin_val the specified pin value you want to set, 0 is low, 1 is high.
78+
*/
79+
void pcf8574_pin_write(pcf8574_device_t dev, uint8_t pin, uint8_t pin_val);
80+
81+
#endif
82+

pcf8574_sample.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*
7+
* Change Logs:
8+
* Date Author Notes
9+
* 2018-11-22 flybreak Make the first version of pcf8574's package
10+
*/
11+
12+
#include <rtthread.h>
13+
#include "pcf8574.h"
14+
15+
#define TEST_IO 0 //(0-7)
16+
#define I2C_BUS "i2c1"
17+
18+
int pcf8574_sample(void)
19+
{
20+
rt_uint8_t value;
21+
pcf8574_device_t dev = RT_NULL;
22+
23+
dev = pcf8574_init(I2C_BUS, RT_NULL);
24+
25+
if (dev == RT_NULL)
26+
return -1;
27+
28+
pcf8574_pin_write(dev, TEST_IO, 0);
29+
30+
value = pcf8574_pin_read(dev, TEST_IO);
31+
rt_kprintf("The value of pcf8574.P%d is %d\n", TEST_IO, value);
32+
33+
rt_thread_mdelay(1000);
34+
35+
pcf8574_pin_write(dev, TEST_IO, 1);
36+
37+
value = pcf8574_pin_read(dev, TEST_IO);
38+
rt_kprintf("The value of pcf8574.P%d is %d\n", TEST_IO, value);
39+
40+
pcf8574_deinit(dev);
41+
42+
return 0;
43+
}
44+
#ifdef FINSH_USING_MSH
45+
MSH_CMD_EXPORT(pcf8574_sample, a pcf8574 sample);
46+
#endif

0 commit comments

Comments
 (0)