展讯7715平台安卓7.0控制io口驱动

来源:互联网 发布:类似蝰蛇音效的软件 编辑:程序博客网 时间:2024/04/30 12:50

题记

经过近一个月的努力,从学习linux命令,到学习编译安卓源码,再到学习什么是驱动,各种bin文件,驱动文件,Makefile,Android.mk,Kconfig等等,让我感觉乱七八糟的,今天把终于可以控制得了io口了,所以蹭下班把这一个月的学习成果梳理一下.

任务

上周三给布置的任务,写一个驱动,控制io口的高低.用的是安卓7.0的源码,平台是展讯的7715.

过程

既然选择了驱动,那就从最简单的一步一步开始搞呗,先上网查了很多的资料,包括最简单的驱动示例,还有各种视频讲解,了解了驱动干嘛的,然后大概框架是什么等等,有了一个整体的思路.我先是按着韦东山的视频上面最简单的读写字符串进行结构的编写,但是发现好多问题,毕竟很多年前的视频了,内核的各种API接口已经发生了很大的变化,然后一步一步通过上网查,问经理,各种编译出错,从最开始的头大到现在的知道怎么去找错误,怎么一步一步的去修改,期间是很痛苦的,linux的基本命令不会用,git也用不好,来回跳的自己都不知在哪,然后一遍一遍的去拷源代码,在编译,浪费了挺多的时间的.(一定得好好在学习一下git)
烧写也是从最开始的window系统慢慢的转到了linux系统下面,经理当时教我们在window下面烧写,但是编译全是在linux下面,所以一直拷贝很麻烦,然后抽出了半天的时间实验了一下linux下面的烧写.哇,感觉发现新大陆似的,板子通上电,连上linux,四五条命令搞定,即简单还省时间,真的太方便了.


烧写命令:
1: sudo adb reboot bootloader 命令, 通过 adb 使 tbox 重启后进入 fastboot模式;
2: sudo fastboot devices 查看 tbox 重启后是否进入到 fastboot 模式;
3: sudo fastboot flash system system_b128k_p2k.img 烧写 system 分区,
userdata 分区的命令:sudo fastboot flash userdata userdata_b128k_p2k.img,
boot 分区命令:sudo fastboot flash boot boot.img
4:sudo fastboot reboot 重启进入 normal 模式.

学到的一些指令cat proc/kmsg  //查看平台上面的打印日志adb remount    //重新加载使其可读可写grep -rn ""    //查找文件夹内文件有某些内容的东西

驱动代码

/*驱动一般放在kernel/drivers/misc/路径下面,需要写一个Kconfig和Makefile文件*/#include <linux/module.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/init.h>#include <linux/cdev.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#include <linux/ioctl.h>#include <linux/kernel.h>#include <linux/delay.h>#include <asm/uaccess.h>#include <asm/irq.h>#include <asm/ioctl.h>#include <linux/device.h>#include <linux/gpio.h> #define DEV_IOC_MAGIC '0xee' //幻数#define DEV_IOCPRINT  _IO(DEV_IOC_MAGIC, 1)#define DEV_IO_HIGH   _IO(DEV_IOC_MAGIC, 2)#define DEV_IO_LOW    _IO(DEV_IOC_MAGIC, 3)#define DEV_IOC_MAXNR 3#define gpio 94static struct class *firstdev_class;static struct device *firstdev_class_dev;static int first_dev_open(struct inode *inode,struct file *file){    printk("first_dev_open\n");    return nonseekable_open(inode, file);   //不明白这句话是干什么的}static long first_dev_ioctl(struct file *file, unsigned int cmd,unsigned long arg){    if (_IOC_TYPE(cmd) != DEV_IOC_MAGIC)         return -1;    if (_IOC_NR(cmd) > DEV_IOC_MAXNR)         return -1;                          //进行判断接收到的数据对不对    switch(cmd)    {        case DEV_IOCPRINT:            printk("<--- 11111111111111111111111111111111--->\n\n");        break;        case DEV_IO_HIGH:        {            gpio_direction_output (gpio,1);            printk("<--------------Now,GPIO is HIGH-------------->\n\n");        }        break;        case DEV_IO_LOW:        {            gpio_direction_output (gpio_u1_tx,0);            printk("<-------------Now,GPIO is LOW---------------->\n\n");        }        break;        default:          return -EINVAL;    }    //return nonseekable_open(inode, file);}static struct file_operations first_dev_fops = {    .owner = THIS_MODULE,     //一个宏    .open  = first_dev_open,    .unlocked_ioctl = first_dev_ioctl,};int major;static struct cdev first_cdev;static struct class *cls;static int first_dev_init(void){    dev_t devid;    if (major) {        devid = MKDEV(major, 0);        register_chrdev_region(devid, 1, "dsx");      } else {        alloc_chrdev_region(&devid, 0, 1, "dsx");         major = MAJOR(devid);                         }    cdev_init(&first_cdev, &first_dev_fops);    cdev_add(&first_cdev, devid, 1);    cls = class_create(THIS_MODULE, "dsx");    device_create(cls, NULL, MKDEV(major, 0), NULL, "dsx");    return 0;}static void first_dev_exit(vodi){    unregister_chrdev(major,"dsx"); // 卸载    device_unregister(firstdev_class_dev);    class_destroy(firstdev_class);}module_init(first_dev_init);module_exit(first_dev_exit);MODULE_LICENSE("GPL");

应用程序代码

/*应用程序一般放在/system/core/下面新建个文件夹,需要写一个Android.mk*/#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include <stdio.h>#include <linux/ioctl.h>#define DEV_IOC_MAGIC '0xee' //定义幻数#define DEV_IOCPRINT  _IO(DEV_IOC_MAGIC, 1)#define DEV_IO_HIGH   _IO(DEV_IOC_MAGIC, 2)#define DEV_IO_LOW    _IO(DEV_IOC_MAGIC, 3)#define DEV_IOC_MAXNR 3int main(int argc , char **argv){    int cmd;    int fd;    int choice = 0;    fd = open("/dev/dsx", O_RDWR);    if(fd < 0)        printf("ni  haishi  dabukai  ya !\n");    /*命令1:打印串口信息*/    while(1)    {        printf("Please input choice: \n 1: DEV_IOCPRINT; \n 2: DEV_IO_HIGH\n 3: DEV_IO_LOW \n 4: EXIT \n");        scanf("%d", &choice);        if(choice == 4)        {            printf("gpio test exit\n");            break;        }        switch(choice)        {            case 1:            {                printf("<--- Call DEV_IOCPRINT --->\n");                cmd = DEV_IOCPRINT;                if (ioctl(fd, cmd) < 0)                {                    printf("Call cmd MEMDEV_IOCPRINT fail\n");                }                break;            }            case 2:            {                printf("<--- Call DEV_IO_HIGH --->\n");                cmd = DEV_IO_HIGH;                if (ioctl(fd, cmd) < 0)                {                    printf("Call cmd DEV_IO_HIGH fail\n");                }                break;            }            case 3:            {                printf("<--- Call DEV_IO_LOW --->\n");                cmd = DEV_IO_LOW;                if (ioctl(fd, cmd) < 0)                {                    printf("Call cmd DEV_IO_LOW fail\n");                }                break;            }            default:                break;          }        printf("\n\n===================================\n");    }    return 0;}

然后通过电路图找到了一个普通的io口,通过控制高低,测的高的时候是2.8v,低的时候直接为0v.哈哈,搞完了,有总莫名的开心,一个月了,终于有了一丁丁点的进度了,慢慢来吧.期待明天的任务安排.

原创粉丝点击