I2C-BH1750
1、设备地址
- 引脚说明
VCC | 5V |
---|---|
GND | GND |
SCL | PB6 |
SDA | PB7 |
ADDR | VCC/GND |
bh1750设备的地址由引脚 ADDR 来决定
-
ADDR接GND
当ADDR引脚接地时设备bh1750的地址为:0x23(7bit)
-
ADDR接VCC
当ADDR引脚接地时设备bh1750的地址为:0x5c(7bit)
2、工作模式
BH1750FVI模块共有六种工作模式:连续H-分辨率模式、连续H-分辨率模式2、连续L-分辨率模式、一次H分辨率模式、一次H分辨率模式2、一次性L分辨率模式
手册中建议使用 连续H-分辨率模式
连续H-分辨率模式 每次转换时间为120ms
3、读取数据流程
每个芯片都有固有的读取数据流程,下面我们来看一下bh1750模块的读取流程,以连续H-分辨率模式为例大致流程为:通电指令 -> 工作模式指令 -> 获取数据
4、ROM指令
5、读写时序
以连续H-分辨率模式并且ADDR与GND相连的读写时序如下:
总上,读取数据全过程为:
第一步:通过I2C总线,主机给bh1750设备发送数据:0x01 ——> (Power on)
第二步:通过I2C总线,主机给bh1750设备发送数据:0x10 ——> (Continuously H-resolution mode)
第三步:通过I2C总线,主机读取设备bh1750采集到的数据
6、添加设备信息
找到开发板对应的设备树文件dts,在i2c1的节点下面添加bh1750设备信息(我这里将bh1750连接到了i2c1控制器下):
&i2c1 {
clock-frequency = <100000>;
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_i2c1>;
status = "okay";
bh1750@23 {
compatible = "gy302,bh1750";
reg = <0x23>;
};
7、驱动代码
#include <asm-generic/errno-base.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/miscdevice.h>
#include "acpi/acoutput.h"
#include "asm-generic/int-ll64.h"
#include "linux/export.h"
#include "linux/printk.h"
#include "uapi/linux/i2c.h"
#include <linux/delay.h>
//Rom指令
#define POWER_ON 0x01
#define H_RESOLUTION_MODE 0x10
struct bh1750_device {
void *private_data;
};
static struct bh1750_device bh1750_dev;
static int bh1750_read_regs(struct bh1750_device *dev, void *val, int len)
{
int ret = 0;
struct i2c_client *client = (struct i2c_client *)dev->private_data;
struct i2c_msg msgs[1] = {
[0] = { //寄存器的值
.addr = client->addr,
.flags = I2C_M_RD,
.buf = val,
.len = len,
}
};
ret = i2c_transfer(client->adapter, msgs, 1);
if (ret != 1) {
printk("i2c rd failed=%d len=%d\n",ret, len);
ret = -EINVAL;
} else {
ret = 0;
}
return ret;
}
/* bh1750写数据 */
static int bh1750_write_regs(struct bh1750_device *dev, u8 *buf, u8 len)
{
struct i2c_client *client = (struct i2c_client *)dev->private_data;
u8 data[256];
struct i2c_msg msgs;
memcpy(data, buf, len);
msgs.addr = client->addr;
msgs.flags = 0;
msgs.buf = data;
msgs.len = len;
return i2c_transfer(client->adapter, &msgs, 1);
}
static void bh1750_write_reg(struct bh1750_device *dev, u8 data)
{
int ret;
ret = bh1750_write_regs(dev, &data, 1);
if (ret != 1) {
printk("i2c write fail: %d\n", ret);
}
}
/* bh1750字符集 */
int bh1750_open(struct inode *inode, struct file *file)
{
bh1750_write_reg(&bh1750_dev, POWER_ON);
bh1750_write_reg(&bh1750_dev, H_RESOLUTION_MODE);
mdelay(200);
return 0;
}
ssize_t bh1750_read(struct file* filp, char __user* buf, size_t count, loff_t* ppos)
{
u8 data[2];
int err;
bh1750_read_regs(&bh1750_dev, data, 2);
err = copy_to_user(buf, data, sizeof(data));
return 0;
}
int bh1750_close(struct inode *inode, struct file *file)
{
return 0;
}
struct file_operations bh1750_fops = {
.open = bh1750_open,
.release = bh1750_close,
.read = bh1750_read,
};
struct miscdevice bh1750_misc = {
.name = "bh1750",
.fops = &bh1750_fops,
.minor = 255,
};
static const struct of_device_id bh1750_of_device_id[] = {
{ .compatible = "gy302,bh1750" },
{ /* END OF LIST */ }
};
struct i2c_device_id bh1750_device_id[] = {
{.name = "gy302,bh1750", },
{ /* END OF LIST */ }
};
int bh1750_probe(struct i2c_client *client, const struct i2c_device_id *dev)
{
int ret;
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
ret = misc_register(&bh1750_misc);
if (ret < 0) {
printk(KERN_WARNING "ac.o: Unable to register misc device\n");
goto MISC_REGISTER_FAIL;
}
/* 获得iic_client */
bh1750_dev.private_data = client;
printk("bh1750 addr: %#x\r\n", client->addr);
return 0;
MISC_REGISTER_FAIL:
return ret;
}
int bh1750_remove(struct i2c_client *client)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
misc_deregister(&bh1750_misc);
return 0;
}
struct i2c_driver bh1750_driver = {
.id_table = bh1750_device_id,
.driver = {
.owner = THIS_MODULE,
.name = "bh1750",
.of_match_table = bh1750_of_device_id,
},
.probe = bh1750_probe,
.remove = bh1750_remove,
};
/* 入口 */
static int __init bh1750_driver_init(void)
{
return i2c_add_driver(&bh1750_driver);
}
static void __exit bh1750_driver_exit(void)
{
i2c_del_driver(&bh1750_driver);
}
module_init(bh1750_driver_init);
module_exit(bh1750_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lighting master");
应用层代码就你们自己写吧~