iIC驱动编写之ap3216c传感器

I.MX6U-ALPHA 开发板上通过 I2C1 连接了一个三合一环境传感器:AP3216C,AP3216C
是由敦南科技推出的一款传感器,其支持环境光强度(ALS)、接近距离(PS)和红外线强度(IR)这
三个环境参数检测

ap3216c.h:

#ifndef AP3216C_H
#define AP3216C_H

#define AP3216C_ADDR        0X1E    /* AP3216C器件地址  */

/* AP3316C寄存器 */
#define AP3216C_SYSTEMCONG    0x00    /* 配置寄存器       */
#define AP3216C_INTSTATUS    0X01    /* 中断状态寄存器   */
#define AP3216C_INTCLEAR    0X02    /* 中断清除寄存器   */
#define AP3216C_IRDATALOW    0x0A    /* IR数据低字节     */
#define AP3216C_IRDATAHIGH    0x0B    /* IR数据高字节     */
#define AP3216C_ALSDATALOW    0x0C    /* ALS数据低字节    */
#define AP3216C_ALSDATAHIGH    0X0D    /* ALS数据高字节    */
#define AP3216C_PSDATALOW    0X0E    /* PS数据低字节     */
#define AP3216C_PSDATAHIGH    0X0F    /* PS数据高字节     */


#endif 

ap3216cApp.c:

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <linux/input.h>


int main(int argc, char *argv[])
{
    int fd,err;
    int ret = 0;
    char *filename;
    int databuf[3];
    unsigned short ir, als, ps;
    
    if (argc != 2) {
        printf("Error Usage!\r\n");
        return -1;
    }

    filename = argv[1];
    fd = open(filename, O_RDWR);
    if (fd < 0) {
        printf("Can't open file %s\r\n", filename);
        return -1;
    }

    
    while (1) {
        ret = read(fd, databuf, sizeof(databuf));
        if(ret == 0) {             /* 数据读取成功 */
            ir =  databuf[0];     /* ir传感器数据 */
            als = databuf[1];     /* als传感器数据 */
            ps =  databuf[2];     /* ps传感器数据 */
            printf("ir = %d, als = %d, ps = %d\r\n", ir, als, ps);
        }
        usleep(200000); /*100ms */
    }
    

    
    close(fd);
    return ret;
}
 

ap3216c.c:

#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 "ap3216creg.h"
#include <linux/delay.h>

#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"

struct ap3216c_dev{
    dev_t devid; //设备号
    struct cdev cdev; //字符设备
    struct class *class; //类
    struct device *device; //设备
    struct device_node *nd; //设备节点
    int major; //主设备号
    int minor; //次设备号
    void *private_data; //私有数据
    unsigned short ir, als, ps;        /* 三个光传感器数据 */
};

struct ap3216c_dev ap3216cdev;


/*
 * @description    : 从ap3216c读取多个寄存器数据
 * @param - dev:  ap3216c设备
 * @param - reg:  要读取的寄存器首地址
 * @param - val:  读取到的数据
 * @param - len:  要读取的数据长度
 * @return         : 操作结果
 */
//读取AP3216C的N个寄存器值
static int ap3216c_read_regs(struct ap3216c_dev *dev,u8 reg,void *val, int len)
{
    int ret;
    struct i2c_msg msg[2];
    struct i2c_client *client = (struct i2c_client*)dev->private_data;
    
    //根据I2C 读时序原理
    //msg[0]发送要读取的寄存器首地址
    msg[0].addr = client->addr;//从机地址,也就是ap3216c地址
    msg[0].flags = 0; //表示要发送的数据
    msg[0].buf = &reg; //要发送的数据,也就是寄存器地址
    msg[0].len = 1; //要发送的寄存器地址长度为1

    //msg[1]读取数据
    msg[1].addr = client->addr;//从机地址,也就是ap3216c地址
    msg[1].flags = I2C_M_RD; //表示读数据
    msg[1].buf = val; //接收从机发送的数据,缓冲区
    msg[1].len = len; //要读取的寄存器长度

    ret = i2c_transfer(client->adapter, msg, 2); //数据传输

}


/*
 * @description    : 向ap3216c多个寄存器写入数据
 * @param - dev:  ap3216c设备
 * @param - reg:  要写入的寄存器首地址
 * @param - val:  要写入的数据缓冲区
 * @param - len:  要写入的数据长度
 * @return       :   操作结果
 */
//向AP3216C写N个寄存器的数据
static int ap3216c_write_regs(struct ap3216c_dev *dev,u8 reg,u8 *buf,u8 len)
{
    u8 b[256];
    struct i2c_msg msg;
    struct i2c_client *client = (struct i2c_client *)dev->private_data;
    
    //根据i2c 写时序原理
    //构建要发送的数据,也就是寄存器首地址+实际的数据
    b[0] = reg;                    /* 寄存器首地址 */
    memcpy(&b[1],buf,len);        /* 将要写入的数据拷贝到数组b里面 */
        
    msg.addr = client->addr;    /* 从机地址,ap3216c地址 */
    msg.flags = 0;                /* 标记为写数据,表示要发送的数据 */

    msg.buf = b;                /* 发送的数据,也就是寄存器首地址+实际的数据 , 要写入的数据缓冲区*/
    msg.len = len + 1;            /* 要发送的数据地址长度:寄存器地址长度+实际数据长度*/

    return i2c_transfer(client->adapter, &msg, 1);

}

/*
 * @description    : 读取ap3216c指定寄存器值,读取一个寄存器
 * @param - dev:  ap3216c设备
 * @param - reg:  要读取的寄存器
 * @return       :   读取到的寄存器值
 */
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg)
{
    u8 data = 0;

    ap3216c_read_regs(dev, reg, &data, 1);
    return data;

}

/*
 * @description    : 读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!
 *                : 如果同时打开ALS,IR+PS的话两次数据读取的时间间隔要大于112.5ms
 * @param - ir    : ir数据
 * @param - ps     : ps数据
 * @param - ps     : als数据 
 * @return         : 无。
 */
void ap3216c_readdata(struct ap3216c_dev *dev)
{
    unsigned char i =0;
    unsigned char buf[6]; //6个寄存器
    
    /* 循环读取所有传感器数据 */
    for(i = 0; i < 6; i++)    
    {
       buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW + i);    
    }

    if(buf[0] & 0X80)     /* IR_OF位为1,则数据无效 */
        dev->ir = 0;                    
    else                 /* 读取IR传感器的数据           */
        dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0X03);             
    
    dev->als = ((unsigned short)buf[3] << 8) | buf[2];    /* 读取ALS传感器的数据              */  
    
    if(buf[4] & 0x40)    /* IR_OF位为1,则数据无效             */
        dev->ps = 0;                                                        
    else                 /* 读取PS传感器的数据    */
        dev->ps = ((unsigned short)(buf[5] & 0X3F) << 4) | (buf[4] & 0X0F); 
}


/*
 * @description    : 向ap3216c指定寄存器写入指定的值,写一个寄存器
 * @param - dev:  ap3216c设备
 * @param - reg:  要写的寄存器
 * @param - data: 要写入的值
 * @return   :    无
 */
static void ap3216c_write_reg(struct ap3216c_dev *dev, u8 reg, u8 data)
{
    u8 buf = 0;
    buf = data;
    ap3216c_write_regs(dev, reg, &buf, 1);
}

static int ap3216c_open(struct inode *inode, struct file *filp)
{
    unsigned char value = 0;
    filp->private_data = &ap3216cdev;
    

    //初始化AP3216C
    ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x04);        /* 复位AP3216C             */
    mdelay(50);                                                        /* AP3216C复位最少10ms     */
    ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0X03 );        /* 开启ALS、PS+IR         */

    value = ap3216c_read_reg(&ap3216cdev,AP3216C_SYSTEMCONG);
    printk("AP3216C_SYSTEMCONG = %#x\r\n",value);
    return 0;
}

static ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
    short data[3];
    long err = 0;

    struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;
    
    ap3216c_readdata(dev);

    data[0] = dev->ir;
    data[1] = dev->als;
    data[2] = dev->ps;
    err = copy_to_user(buf, data, sizeof(data));
    return 0;
}

static int ap3216c_release(struct inode *inode, struct file *filp)
{
    printk("ap3216c_release\r\n");
    return 0;
}

//ap3216c操作函数
static const struct file_operations ap3216c_ops = {
    .owner = THIS_MODULE,
    .open = ap3216c_open,
    .read = ap3216c_read,
    .release = ap3216c_release, 
};

static int ap3216c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
    int ret = 0;
    printk("ap3216c_probe\r\n");

    //搭建字符设备驱动框架
    //1.构建设备号
    if(ap3216cdev.major){
        ap3216cdev.devid = MKDEV(ap3216cdev.major,0);
        register_chrdev_region(ap3216cdev.devid,AP3216C_CNT,AP3216C_NAME);
    }else{
        alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);
        ap3216cdev.major = MAJOR(ap3216cdev.devid);
        ap3216cdev.minor = MINOR(ap3216cdev.devid);
    }

    printk("major:%d\r\n",ap3216cdev.major);
    printk("minor:%d\r\n",ap3216cdev.minor);
    //2.注册设备
    cdev_init(&ap3216cdev.cdev,&ap3216c_ops);
    cdev_add(&ap3216cdev.cdev,ap3216cdev.devid,AP3216C_CNT);

    //3.创建类
    ap3216cdev.class = class_create(THIS_MODULE,AP3216C_NAME);
    if(IS_ERR(ap3216cdev.class)){
        return PTR_ERR(ap3216cdev.class);
    }

    //4.创建设备
    ap3216cdev.device = device_create(ap3216cdev.class,NULL,ap3216cdev.devid,NULL,AP3216C_NAME);
    if(IS_ERR(ap3216cdev.device)){
        return PTR_ERR(ap3216cdev.device);
    }

    ap3216cdev.private_data = client;

    return 0;
}

static int ap3216c_remove(struct i2c_client *client)
{
    //删除设备
    cdev_del(&ap3216cdev.cdev);
    //删除设备号
    unregister_chrdev_region(ap3216cdev.devid,AP3216C_CNT);
    //注销类和设备
    device_destroy(ap3216cdev.class,ap3216cdev.devid);
    class_destroy(ap3216cdev.class);

    return 0;
}

//传统匹配方式ID列表
static struct i2c_device_id ap3216c_id[] = {
    {"alientek,ap3216c",0},
    {}
};

//设备树匹配表
static struct of_device_id ap3216c_of_match[] = {
    {.compatible = "alientek,ap3216c"},
    {}
};

//i2c_driver
static struct i2c_driver ap3216c_driver = {

    .probe = ap3216c_probe, //匹配成功调用此函数
    .remove= ap3216c_remove,
    .driver = {
        .name = "ap321c", //没有设备树时用名字和设备匹配
        .owner = THIS_MODULE,
        .of_match_table = ap3216c_of_match, //匹配表,有设备树时用匹配表和设备树里面的节点进行配对
    },
    .id_table = ap3216c_id,

};

//驱动入口函数
static int __init ap3216c_init(void)
{
    int ret = 0;

    ret = i2c_add_driver(&ap3216c_driver);  //向系统注册i2c

    return ret;
}

//驱动出口函数
static void __exit ap3216c_exit(void)
{
    i2c_del_driver(&ap3216c_driver);
}


module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DENG");

测试结果:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Okay,你想了解如何编写 MSP430 的 IIC 驱动。MSP430 是一款低功耗 MCU,它可以通过 I2C 或 SPI 等接口和其他设备进行通信。以下是一些用于编写 MSP430 IIC 驱动程序的步骤: 1. 确定 IIC 的时钟速率和设备地址 MSP430 可以通过内置的 USCI 模块(主要是 USCI_A0 和 USCI_B0)来实现 I2C 通信。在编写 IIC 驱动程序之前,您需要确认所需的 IIC 时钟速率和要访问的设备的地址。根据 IIC 总线规范,设备地址可以是 7 位或 10 位。 2. 初始化 USCI 模块 在使用 USCI 模块之前,需要配置模块的各种控制参数。这些参数包括时钟源、时钟分频器、数据位、停止位等。您可以使用 MSP430 提供的模板代码来初始化 USCI 模块,或根据您的特定需求自行编写初始化代码。 3. 发送和接收数据 一旦 USCI 模块成功初始化,您就可以使用 send() 和 receive() 函数来发送和接收 IIC 数据。在发送数据时,您需要指定要发送的设备地址和要发送的数据。在接收数据时,您需要指定设备地址并分配足够的缓冲区来保存接收到的数据。 4. 处理 IIC 中断 在实现 IIC 驱动程序时,您还需要考虑处理中断。当 MSP430 接收到 IIC 中断时,您可以在 ISR 中处理并执行相关操作。在处理中断时,您需要注意及时清除标志位、停止发送或接收数据等。 以上是一些基本的步骤,你可以从这里开始编写你的 MSP430 IIC 驱动程序。希望我的回答能够帮助到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值