Samsung_tiny4412(驱动笔记05)----Makefile,open,read,write,lseek,poll,ioctl,fasync

发布者:Meilin8888最新更新时间:2025-01-15 来源: cnblogs关键字:Samsung  tiny4412 手机看文章 扫描二维码
随时随地手机看文章

/***********************************************************************************

 *                    

 *               Makefile,open,read,write,lseek,poll,ioctl,fasync

 *

 * 声明:

 *      1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会

 *         不对齐,从而影响阅读.

 *

 **********************************************************************************/


                        \\\\\\\--*目录*--//////////////

                        |  一. Makefile大致写法:             

                        |  二. 获取进程task_struct的方法:    

                        |  三. open 大致写法:                

                        |  四. read 大致写法:                

                        |  五. write 大致写法:               

                        |  六. lseek 大致写法:               

                        |  七. poll 大致写法:                

                        |  八. ioctl 大致写法:               

                        |  九. close 大致写法:               

                        |  十. fasync 大致写法:              

                        |  十一. 等待队列API:                

                        |  十二. 驱动wait_queue poll fasync: 

                        |  十三. 应用wait_queue poll fasync: 

                        \\\\\\\\\///////////////////


一. Makefile大致写法:

    ...

    # 定义一个CC变量,赋值为arm-linux-

    CC = arm-linux-         

    # .c文件依赖的.h文件存放的目录,这里是:当前目录下include文件夹

    INCLUDE_DIRS = include


    # 如果变量objs在此之前未声明定义,那么就声明定义,并赋值objs为hello.o

    # 如果变量objs在此之前已声明定义,那么把hello.o放在objs字符串后面

    objs += hello.o         

    

    # 1. hello : 下面命令要生成的目标,当然也有可能是伪指令;

    # 2. $(objs) : 依赖文件,只有依赖文件都存在的情况下才会执行下面的指令;

    # 3. $(CC)gcc $< -o $@ : 编译依赖文件产生目标文件;

    #    1. $(CC) : 取CC变量里的值;

    #    2. $<    : 依赖文件($(objs))

    #    3. $@    : 目标文件(hello)

    hello : $(objs) 

        $(CC)gcc $< -o $@

    

    # 1. 生成的目标文件简写,将当前文件下所有的.c编译成对应的.o文件;

    # 2. -I$(INCLUDE_DIRS) : .c编译时需要的头文件所在的目录

    %.o : %.c 

        $(CC)gcc -c $< -o $@ -I$(INCLUDE_DIRS)

    

    # 伪命令声明,这样即使存在clean文件,也会把clean当命令使用

    .PHONY:clean

    clean:

        rm *.o hello

    ...


二. 获取进程task_struct的方法:

    ...

    int ret;

    struct thread_info *info;

    struct task_struct *task;

    

    /** 

     * 获取当前进程的进程描述符(PCB)算法,主要是因为thread_info里有task_struct指针,

     * 而thread_info放在了内核线程栈的起始地方,而内核线程栈是8k对齐,

     * 所以thread_finfo的首地址是: ((unsigned int)&ret & ~(0x2000 - 1))

     */

    info = (void *)((unsigned int)&ret & ~(0x2000 - 1));

    task = info->task;

    printk('pid = %d, [%s]n', task->pid, task->comm); /* task->comm 运行的程序名 */

    

    // current 当前进程的PCB指针,系统自带提供的全局变量

    printk('pid = %d, [%s]n', current->pid, current->comm);

    ...


三. open 大致写法:

    1. int (*open) (struct inode *, struct file *);

    2. 代码模型:

        static int test_open(struct inode *inode, struct file *file) 

        { 

            /**

             * 一般open函数里面都要把私有数据绑定到file->private_data上

             */

            test_t *p; 

            p = container_of(file->f_op, test_t, fops); 

            file->private_data = p; 

         

            //声明当前设备不支持llseek方法调整读写位置 

            //nonseekable_open(inode, file);

            return 0; 

        } 


四. read 大致写法:

    1. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

    2. 代码模型:

        static ssize_t test_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

        {

            int ret;

            test_t *p = file->private_data;

            

            /**

             * 检查可读数据是否足够

             */

            if(count > BUF_SIZE - *pos)

                count = BUF_SIZE - *pos;

            

            ret = copy_to_user(buf, p->buf + *pos, count);

            if(ret)

                return -EFAULT;

            

            *pos += count;

        

            return count; //返回读到的数据个数

        }



五. write 大致写法:

    1. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

    2. 代码模型:

        static ssize_t test_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

        {

            test_t *p = file->private_data;

        

            if(p->is_empty)

            {

                /* 进入深度睡眠其中方法中的一种 */

                current->state = TASK_UNINTERRUPTIBLE;

                schedule();

            }

            return count;

        }


六. lseek 大致写法:

    1. loff_t (*llseek) (struct file *, loff_t, int);

    2. 2.6版本内核不实现该方法的话,使用内核默认的llseek函数,当前版本必须实现该方法;

    3. 代码模型:

        static loff_t test_llseek(struct file *file, loff_t offset, int whence)

        {                       

            loff_t pos = file->f_pos;

        

            switch(whence)

            {

                case SEEK_SET:

                    pos = offset;

                    break;

                case SEEK_CUR:

                    pos += offset;

                    break;

                case SEEK_END:

                    pos = BUF_SIZE + offset;

                    break;

                default:

                    return -EINVAL; //参数非法

            }

        

            file->f_pos = pos;

        

            //返回lseek之后的文件读写位置(相对文件开始处)

            return pos;

        }


七. poll 大致写法:

    1. poll,epoll,select系统调用的后端,都用作查询对一个或多个文件描述符的读写是否阻塞

    2. unsigned int (*poll) (struct file *, struct poll_table_struct *);

    3. 代码模型:

        static unsigned int test_poll(struct file *file, struct poll_table_struct *table)

        {

            unsigned int poll = 0;

            test_t *p = file->private_data;

            /*printk('In test_poll.n');*/

[1] [2] [3]
关键字:Samsung  tiny4412 引用地址:Samsung_tiny4412(驱动笔记05)----Makefile,open,read,write,lseek,poll,ioctl,fasync

上一篇:Samsung_tiny4412(驱动笔记06)----list_head,proc file system,GPIO,ioremap
下一篇:Samsung_tiny4412(驱动笔记04)----volatile,container_of,file_operations,file,inode

推荐阅读最新更新时间:2026-03-22 11:00

Samsung_tiny4412(驱动笔记03)----字符设备驱动基本操作及调用流程
/*********************************************************************************** * * 字符设备驱动基本操作及调用流程 * * 声明: * 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会 * 不对齐,从而影响阅读. * 2. 以下所有的shell命令都是在root权限下运行的; *******************************************************************************
[单片机]
Samsung_tiny4412(驱动笔记10)----mdev,bus,device,driver,platform
/*********************************************************************************** * * mdev,bus,device,driver,platform * * 声明: * 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会 * 不对齐,从而影响阅读. * 2. 由于本人水平有限,很难阐述清楚bus device driver platform的关系 * 所以强烈要求您详细参考本次提供的预热文章. * **********
[单片机]
Samsung_tiny4412(驱动笔记02)----ASM with C,MMU,Exception,GIC
/**************************************************************************** * * ASM with C,MMU,Exception,GIC * * 声明: * 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会 * 不对齐,从而影响阅读. * 2. 以下所有的shell命令都是在root权限下运行的; * 3. 文中在需要往文件中写入内容的时候使用了如下2方式: * 1.如果文件不存在,创建文件;如果存在,以覆盖的方式往文件中添加内容: *
[单片机]
Tiny4412 支持 adb reboot-bootloader
硬件版本: Tiny4412ADK + S700 4GB u-boot 版本: u-boot-2010-12 linux版本: Linux-3.0.8 版本一 支持 adb reboot bootloader patch:http://pan.baidu.com/s/1i3KfCI5 版本二 支持按住K1键,然后开机,进入fastboot模式 patch: http://pan.baidu.com/s/1hqgmnQ0
[单片机]
tiny4412 串口驱动分析一 --- u-boot中的串口驱动
开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 u-boot:U-Boot 2010.12 Linux内核版本:linux-3.0.31 Android版本:android-4.1.2 我们以tiny4412为例分析串口驱动,下面我们从u-boot开始分析,然后再分析到Linux。 串口初始化 关于这部分代码流程参考件: tiny4412 u-boot 启动.pdf ,这里主要分析函数:uart_asm_init 在初始化串口驱动之前已经进行了系统时钟以及内存的初始化。下面的代码取自board/samsung/tiny441
[单片机]
<font color='red'>tiny4412</font> 串口<font color='red'>驱动</font>分析一 --- u-boot中的串口<font color='red'>驱动</font>
基于tiny4412的Linux内核移植 ---- 調試方法
平臺 Linux-4.4.4 uboot使用的是友善自帶的(爲了支持uImage和設備樹做了稍許修改) 概述 這篇博客主要用於匯總一下調試方法。 正文 1. dnw下載 目前我將uboot燒寫到SD卡中,然後使用dnw將kernel、根文件系統以及設備樹鏡像下載到內存中,爲了提高效率,可以使用下面的方法: 在uboot中添加環境變量: setenv dnw_up 'dnw 0x40600000; dnw 0x41000000; dnw 0x42000000; bootm 0x40600000 0x41000000 0x42000000' 進入uboot終端後,執行如下命令: run d
[单片机]
基于tiny4412的Linux内核移植 -- PWM子系统学习(八)
平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本:Linux-4.4.0 (支持device tree) u-boot版本:友善之臂自带的 U-Boot 2010.12 (为支持uImage启动,做了少许改动) busybox版本:busybox 1.25 交叉编译工具链: arm-none-linux-gnueabi-gcc (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-29)) 实验二、用蜂鸣器测试backlight 一般LCD的背光的亮度调节都是通过控制输入给背光控制
[单片机]
串口编程(基于tiny4412
参考: http://www.cnblogs.com/wblyuyang/archive/2011/11/21/2257544.html http://www.cppblog.com/amazon/archive/2010/01/28/106644.html serial_demo.c #include stdio.h #include stdlib.h #include unistd.h #include sys/types.h #include sys/stat.h #include fcntl.h //文件控制定义 #include termios.h //终端控制定义 #include errno
[单片机]
小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved