add pcf8574 i2c
This commit is contained in:
@@ -7,29 +7,37 @@
|
||||
#define DBG_LEVEL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct chrg_i2c_t chrgi2c[TOTAL_I2C] = {
|
||||
struct chrg_i2c_t chrgi2c[TOTAL_I2CS] = {
|
||||
[IDX_I2C_1] = {
|
||||
.dev = RT_NULL,
|
||||
.name = DEV_NAME_I2C1,
|
||||
.slave = SLAVE_PCF8574_ADDR,
|
||||
.opened = 0,
|
||||
.port_value = 0,
|
||||
.index = IDX_I2C_1,
|
||||
},
|
||||
[IDX_I2C_2] = {
|
||||
.dev = RT_NULL,
|
||||
.name = DEV_NAME_I2C2,
|
||||
.slave = SLAVE_PCF8574_ADDR,
|
||||
.opened = 0,
|
||||
.port_value = 0,
|
||||
.index = IDX_I2C_2,
|
||||
},
|
||||
[IDX_I2C_3] = {
|
||||
.dev = RT_NULL,
|
||||
.name = DEV_NAME_I2C3,
|
||||
.slave = SLAVE_PCF8574_ADDR,
|
||||
.opened = 0,
|
||||
.port_value = 0,
|
||||
.index = IDX_I2C_3,
|
||||
},
|
||||
[IDX_I2C_4] = {
|
||||
.dev = RT_NULL,
|
||||
.name = DEV_NAME_I2C4,
|
||||
.slave = SLAVE_PCF8574_ADDR,
|
||||
.opened = 0,
|
||||
.port_value = 0,
|
||||
.index = IDX_I2C_4,
|
||||
}
|
||||
};
|
||||
@@ -83,14 +91,24 @@ static int chrg_i2c_init (void)
|
||||
{
|
||||
struct chrg_i2c_t *pI2C = RT_NULL;
|
||||
int i = 0;
|
||||
rt_err_t ret = RT_EOK;
|
||||
|
||||
for (i = 0; i < TOTAL_I2C; i++) {
|
||||
for (i = 0; i < TOTAL_I2CS; i++) {
|
||||
pI2C = &chrgi2c[i];
|
||||
pI2C->dev = (struct rt_i2c_bus_device *)rt_device_find(pI2C->name);
|
||||
if (RT_NULL == pI2C->dev) {
|
||||
LOG_E("dev '%s' not found.", pI2C->name);
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = rt_device_open(&pI2C->dev->parent, RT_NULL);
|
||||
if (RT_EOK != ret) {
|
||||
LOG_E("open '%s' failed.", pI2C->name);
|
||||
pI2C->opened = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
pI2C->opened = 1;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define SLAVE_PCF8574_ADDR 0x40
|
||||
#define SLAVE_PCF8574_ADDR 0x20
|
||||
|
||||
#define DEV_NAME_I2C1 "i2c1"
|
||||
#define DEV_NAME_I2C2 "i2c2"
|
||||
@@ -17,7 +17,7 @@ typedef enum {
|
||||
IDX_I2C_3, // 2
|
||||
IDX_I2C_4, // 3
|
||||
|
||||
TOTAL_I2C
|
||||
TOTAL_I2CS
|
||||
} eIDX_I2C;
|
||||
|
||||
|
||||
@@ -25,11 +25,13 @@ struct chrg_i2c_t {
|
||||
struct rt_i2c_bus_device *dev;
|
||||
const char *name;
|
||||
rt_uint8_t slave; // slave chip addr
|
||||
eIDX_I2C index;
|
||||
|
||||
rt_uint8_t opened; // 已开启I2C
|
||||
rt_uint8_t port_value; // pcf8574 IO值
|
||||
eIDX_I2C index; // 索引
|
||||
|
||||
};
|
||||
|
||||
extern struct chrg_i2c_t chrgi2c[TOTAL_I2C];
|
||||
extern struct chrg_i2c_t chrgi2c[TOTAL_I2CS];
|
||||
|
||||
extern rt_uint8_t i2c_read_byte(struct chrg_i2c_t *pI2C, rt_uint8_t addr);
|
||||
|
||||
|
||||
@@ -3,12 +3,14 @@
|
||||
#include <rtdevice.h>
|
||||
|
||||
#include "chrg_intr.h"
|
||||
#include "chrg_i2c.h"
|
||||
#include "chrg_pcf8574.h"
|
||||
|
||||
#define LOG_TAG "chrg.intr"
|
||||
#define DBG_LEVEL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct chrg_intr_t chrgintr[TOTAL_INTR] = {
|
||||
struct chrg_intr_t chrgintr[TOTAL_INTRS] = {
|
||||
[IDX_INTR_I2C1] = {
|
||||
.name = NAME_INTR_I2C1,
|
||||
.index = IDX_INTR_I2C1,
|
||||
@@ -76,11 +78,12 @@ static void chrg_intr_callback (void *args)
|
||||
struct chrg_intr_t *pINTR = (struct chrg_intr_t *)args;
|
||||
|
||||
if (RT_NULL != pINTR) {
|
||||
pINTR->value = rt_pin_read(pINTR->pin);
|
||||
if (pINTR->delay) {
|
||||
rt_timer_start(pINTR->tmr_fd);
|
||||
}
|
||||
LOG_I("pin '%s' intr %d.", pINTR->name, pINTR->value);
|
||||
chrg_pcf8574_get_port((eIDX_I2C)pINTR->index);
|
||||
//pINTR->value = rt_pin_read(pINTR->pin);
|
||||
//if (pINTR->delay) {
|
||||
// rt_timer_start(pINTR->tmr_fd);
|
||||
//}
|
||||
//LOG_I("pin '%s' intr %d.", pINTR->name, pINTR->value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -90,7 +93,7 @@ static int chrg_intr_init (void)
|
||||
int i = 0;
|
||||
struct chrg_intr_t *pINTR = RT_NULL;
|
||||
|
||||
for (i = 0; i < TOTAL_INTR; i++) {
|
||||
for (i = 0; i < TOTAL_INTRS; i++) {
|
||||
pINTR = &chrgintr[i];
|
||||
|
||||
rt_pin_mode(pINTR->pin, pINTR->mode);
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include "drv_gpio.h"
|
||||
|
||||
#include "chrg_i2c.h"
|
||||
|
||||
#define NAME_INTR_I2C1 "i2c1irq"
|
||||
#define NAME_INTR_I2C2 "i2c2irq"
|
||||
#define NAME_INTR_I2C3 "i2c3irq"
|
||||
@@ -17,12 +19,12 @@
|
||||
#define PIN_INTR_I2C4 GET_PIN(B, 13)
|
||||
|
||||
typedef enum {
|
||||
IDX_INTR_I2C1 = 0, // 0
|
||||
IDX_INTR_I2C2, // 1
|
||||
IDX_INTR_I2C3, // 2
|
||||
IDX_INTR_I2C4, // 3
|
||||
IDX_INTR_I2C1 = IDX_I2C_1, // 0 中断脚关联 I2C1
|
||||
IDX_INTR_I2C2 = IDX_I2C_2, // 1 中断脚关联 I2C2
|
||||
IDX_INTR_I2C3 = IDX_I2C_3, // 2 中断脚关联 I2C3
|
||||
IDX_INTR_I2C4 = IDX_I2C_4, // 3 中断脚关联 I2C4
|
||||
|
||||
TOTAL_INTR
|
||||
TOTAL_INTRS
|
||||
} eIDX_INTR;
|
||||
|
||||
struct chrg_intr_t {
|
||||
@@ -36,7 +38,7 @@ struct chrg_intr_t {
|
||||
eIDX_INTR index;
|
||||
};
|
||||
|
||||
extern struct chrg_intr_t chrgintr[TOTAL_INTR];
|
||||
extern struct chrg_intr_t chrgintr[TOTAL_INTRS];
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -14,13 +14,10 @@ typedef enum {
|
||||
IDX_TTY_NORTH, // 1
|
||||
IDX_TTY_SOUTH, // 2
|
||||
|
||||
TOTAL_TTY
|
||||
TOTAL_TTYS
|
||||
} eIDX_TTY;
|
||||
|
||||
|
||||
//#define TTY_USING_TEST //使用测试功能
|
||||
//#define TTY_USING_SAMPLE_SLAVE //使用从机示例
|
||||
//#define TTY_USING_SAMPLE_MASTER //使用主机示例
|
||||
//#define TTY_USING_DMA_RX //使用DMA接收
|
||||
//#define TTY_USING_INT_TX //使用中断发送
|
||||
//#define TTY_USING_DMA_TX //使用DMA发送
|
||||
|
||||
@@ -1,4 +1,132 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "chrg_i2c.h"
|
||||
#include "chrg_pcf8574.h"
|
||||
|
||||
#define LOG_TAG "chrg.pcf"
|
||||
#define DBG_LEVEL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
|
||||
// 读取所有8口IO状态
|
||||
rt_uint8_t chrg_pcf8574_get_port (eIDX_I2C idx)
|
||||
{
|
||||
rt_uint8_t value = 0;
|
||||
if (idx >= TOTAL_I2CS) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_ssize_t ret = 0;
|
||||
struct chrg_i2c_t *pI2C = &chrgi2c[idx];
|
||||
|
||||
if (!pI2C->opened) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = rt_device_read(&pI2C->dev->parent, pI2C->slave, &value, 1);
|
||||
if (ret <= 0) {
|
||||
LOG_E("read failed. [%d]", ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pI2C->port_value = value;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// 读取指定管脚状态值
|
||||
rt_uint8_t chrg_pcf8574_get_pin (eIDX_I2C idx, uint8_t bit)
|
||||
{
|
||||
rt_uint8_t data = chrg_pcf8574_get_port(idx);
|
||||
if (data & (1 << bit)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 设置所有8口IO状态值
|
||||
int chrg_pcf8574_set_port (eIDX_I2C idx, uint8_t value)
|
||||
{
|
||||
if (idx >= TOTAL_I2CS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_ssize_t ret = 0;
|
||||
struct chrg_i2c_t *pI2C = &chrgi2c[idx];
|
||||
|
||||
if (!pI2C->opened) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = rt_device_write(&pI2C->dev->parent, pI2C->slave, &value, 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 设置指定管脚状态值
|
||||
int chrg_pcf8574_set_pin (eIDX_I2C idx, uint8_t bit, uint8_t value)
|
||||
{
|
||||
if (idx >= TOTAL_I2CS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
struct chrg_i2c_t *pI2C = &chrgi2c[idx];
|
||||
|
||||
if (!pI2C->opened) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_uint8_t data = chrg_pcf8574_get_port(idx);
|
||||
if (value == 0) {
|
||||
data &= ~(1 << bit);
|
||||
} else {
|
||||
data |= 1 << bit;
|
||||
}
|
||||
|
||||
ret = chrg_pcf8574_set_port(idx, data);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 1
|
||||
int pcf_test (int argc, char **argv)
|
||||
{
|
||||
if (argc < 3) {
|
||||
rt_kprintf("help : %s <get|set> <0|1|2|3> [0, 255]", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int idx = atoi(argv[2]);
|
||||
if ((idx > 3)||(idx < 0)) {
|
||||
rt_kprintf("help : %s <get|set> <0|1|2|3> [0, 255]", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "set") == 0) {
|
||||
if (argc != 4) {
|
||||
rt_kprintf("help : %s <get|set> <0|1|2|3> [0, 255]", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
rt_uint8_t val = (rt_uint8_t)atoi(argv[3]);
|
||||
chrg_pcf8574_set_port((eIDX_I2C)idx, val);
|
||||
} else if (strcmp(argv[1], "get") == 0) {
|
||||
rt_uint8_t data = chrg_pcf8574_get_port((eIDX_I2C)idx);
|
||||
rt_kprintf("value : 0x%02X", data);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
MSH_CMD_EXPORT(pcf_test, test pcf8574 port/pin.);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,22 @@
|
||||
#ifndef __CHRG_PCF8574_H__
|
||||
#define __CHRG_PCF8574_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "chrg_i2c.h"
|
||||
|
||||
// 读取所有8口IO状态
|
||||
extern rt_uint8_t chrg_pcf8574_get_port (eIDX_I2C idx);
|
||||
|
||||
// 读取指定管脚状态值
|
||||
extern rt_uint8_t chrg_pcf8574_get_pin (eIDX_I2C idx, uint8_t bit);
|
||||
|
||||
// 设置所有8口IO状态值
|
||||
extern int chrg_pcf8574_set_port (eIDX_I2C idx, uint8_t value);
|
||||
|
||||
// 设置指定管脚状态值
|
||||
extern int chrg_pcf8574_set_pin (eIDX_I2C idx, uint8_t bit, uint8_t value);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -43,6 +43,8 @@ int chrg_pkg_send (eIDX_TTY idx, rt_uint8_t *data, rt_uint16_t len)
|
||||
case IDX_TTY_SOUTH:
|
||||
// pTTY = chrgsouth.tty;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((RT_NULL != pTTY)&&(RT_NULL != pTTY->dev)) {
|
||||
|
||||
Reference in New Issue
Block a user