Samsung_tiny4412(驱动笔记08)----jiffies,timer,kthread,workqueue,tasklet

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

    4. 创建并唤醒一个内核线程: 

        t = kthread_run(thread_main, NULL, 'my_kthread%d', 0);

    5. kthread接口实例Demo:

        ...

        struct task_struct *t;

        int thread_main(void *data)

        {

            printk('pid = %dn', t->pid);

            while(1)

            {

                if(kthread_should_stop())

                    break;

        

                /*printk('thread running.n');*/

                msleep(3000);

            }

        

            return 123;

        }

        

        int __init test_init(void)

        {

            /**

             * t = kthread_create(thread_main, NULL, 'my_thread%d', 0);

             * if(IS_ERR(t))

             *     return PTR_ERR(t);

             *

             * wake_up_process(t);

             */

                t = kthread_run(thread_main, NULL, 'my_thread%d', 0);

            if(IS_ERR(t))

                return PTR_ERR(t);

        

            return 0;

        }

        

        void __exit test_exit(void)

        {

            int ret;

            ret = kthread_stop(t);

            printk('ret = %dn', ret);

        }

        ...   


五. workqueue接口:

    1. 两种创建工作队列方式:

        1. struct workqueue_struct *wq = create_workqueue('my_wq');

        2. struct workqueue_struct *wq = create_singlethread_workqueue('my_wq');

    2. 两种工作任务:

        1. 普通工作任务: struct work_struct t; INIT_WORK(&t, fn);

        2. 延时工作:     struct delayed_work t; INIT_DELAYED_WORK(&t[i], work_main);

    3. 两种提交普通工作的队列方式,返回值如果是0,表示任务已经在工作队列上了,还没有处理:

        1. 将任务t放到wq工作队列上处理:  queue_work(wq, &t);

        2. 将任务t放到系统工作队列(系统已经建立的工作队列)上处理: schedule_work(&t);

    4. 提交延时工作队列的方式: queue_delayed_work(wq, &t[i++], 3 * HZ));

    5. 让工作队列尽快执行完: flush_workqueue(wq);

    6. 销毁工作队列: destroy_workqueue(wq);

    7. workqueue接口实例Demo:

        ...

        #define NUM     2

        //定义延时工作

        struct delayed_work t[NUM];

        struct workqueue_struct *wq;

        

        void work_main(struct work_struct *work)

        {

            int i;

            static int cnt;

        

            for(i = 0; i < 3; i++)

            {

                printk('work, count = %dn', cnt++);

                //在进程上下文执行,可以sleep

                msleep(1000);

            }

        }

        

        static irqreturn_t irq_handler(int irq, void *arg)

        {

            /*printk('key1 down.n');*/

            static int i;

        

            if(i == NUM)

                i = 0;

        

            /*if(!schedule_work(&t[i++]))*/

            /*if(!queue_work(wq, &t[i++]))*/

            if(!queue_delayed_work(wq, &t[i++], 3 * HZ))

                printk('work is already in the queue.n');

        

            return IRQ_HANDLED;

        }

        

        int __init test_init(void)

        {

            int ret, i;

        

            //初始化工作结构

            for(i = 0; i < NUM; i++)

                /* INIT_WORK(&t[i], work_main); */

                INIT_DELAYED_WORK(&t[i], work_main);

        

            //创建工作队列

            /*wq = create_workqueue('my_workqueue');*/

            //只创建一个工作者线程

            wq = create_singlethread_workqueue('my_workqueue');

            if(!wq)

            {

                printk('workqueue create failed!n');

                ret = -ENOMEM;

                goto err0;

            }

        

            ret = request_irq(IRQ_EINT(26), irq_handler, 

                              IRQF_TRIGGER_FALLING, 'KEY1', NULL);

            if(ret)

            {

                printk('request_irq failed!n');

                goto err1;

            }

        

            return ret;

        

        err1:

            destroy_workqueue(wq);

        err0:

            return ret;

        }

        

        void __exit test_exit(void)

        {

            free_irq(IRQ_EINT(26), NULL);

            /*flush_workqueue(wq);*/

            destroy_workqueue(wq);

        }

        ...


六. tasklet接口:

    1. 定义并初始化: struct tasklet_struct t; tasklet_init(&t, fn, data);

    2. 提交任务, 提交的工作在软中断上下文执行: tasklet_schedule(t);

    3. 对应加锁,解锁方法:

        spin_lock_bh();

        spin_unlock_bh();

    4. tasklet接口实例Demo:

        #include <linux/module.h>

        #include

        #include

        #include

        #include

        

        #define DEV_NAME    'test'

        

        struct test_s {

            struct file_operations fops;

            struct tasklet_struct task;

            spinlock_t lock;

            int major;

        };

        typedef struct test_s test_t;

        

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

        {

            test_t *p;

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

        

            file->private_data = p;

        

            return 0;

        }

        

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

        {

            /*test_t *p = file->private_data;*/

        

            return 0;

        }

        

        int critical(const char *s, spinlock_t *lock)

        {

            int i;

            unsigned long flag;

            static int cnt = 0;

        

            /*spin_lock(lock);*/

            /*local_irq_disable();*/

            /*local_irq_save(flag);*/

            /*spin_lock_irq(lock);*/

[1] [2] [3]
关键字:Samsung  tiny4412 引用地址:Samsung_tiny4412(驱动笔记08)----jiffies,timer,kthread,workqueue,tasklet

上一篇:Samsung_tiny4412(驱动笔记09)----alloc_pages,kmalloc,vmalloc,kmem_cache,class
下一篇:Samsung_tiny4412(驱动笔记07)----spinlock,semaphore,atomic,mutex,completion,interrupt

推荐阅读最新更新时间:2026-03-13 00:32

Samsung_tiny4412(驱动笔记01)----linux 3.5,U-Boot,Busybox,SD卡启动环境搭建
/*********************************************************************************** * * linux 3.5,U-Boot,Busybox,SD卡启动环境搭建 * * 声明: * 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会 * 不对齐,从而影响阅读. * 2. 以下所有的shell命令都是在root权限下运行的; * 3. minicom(U-Boot)指的是用minicom连接开发板做为U-Boot的终端; * 4. 文中在需
[单片机]
Samsung_tiny4412(驱动笔记03)----字符设备驱动基本操作及调用流程
/*********************************************************************************** * * 字符设备驱动基本操作及调用流程 * * 声明: * 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会 * 不对齐,从而影响阅读. * 2. 以下所有的shell命令都是在root权限下运行的; *******************************************************************************
[单片机]
Samsung_tiny4412(驱动笔记04)----volatile,container_of,file_operations,file,inode
/*********************************************************************************** * * volatile,container_of,file_operations,file,inode * * 声明: * 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会 * 不对齐,从而影响阅读. * 2. 本文的结构体的注释主要是参考网络上的解释,几乎无任何个人理解,主要是为后续 * 代码编辑提供参考. ***********************
[单片机]
tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中
1、首先还是要将u-boot写入SD卡中从SD卡启动。 使用读卡器将SD插入电脑中,使用umount卸载u盘, fdisk -l显示其挂载点为 /dev/sdb1 切换到/home/bunfly/images/uboot_tiny4412/sd_fuse/tiny4412目录下,执行./sd_fusing.sh /dev/sdb 拨动最右边开关选择从SD卡启动。 附加编译过程:   1.进入开发板提供的源码文件包,解压uboot源码包。   cd /home/bunfly/source_code/   tar xf uboot_tiny4412-20130729.tgz   2.进入uboot文件夹,更改uboot中t
[单片机]
<font color='red'>tiny4412</font>学习<font color='red'>笔记</font>-将uboot、zImage、文件系统烧到emmc中
tiny4412 解决内核编译版本号问题
内核版本: linux-3.5 开发板: tiny4412 作者:彭东林 邮箱:pengdonglin137@163.com 问题: 由于我使用 git 管理内核代码,导致编译完成后内核版本变成了如下形式: Linux version 3.5.0-FriendlyARM-g5291689 自带的 ko 文件只认识 Linux version 3.5.0-FriendlyARM,所以导致 ko 加载失败。 解决办法一 重新把驱动模块编译一遍 解决办法二 Make menuconfig --- General setup --- Automatically append version information to
[单片机]
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-设备树(1)设备树基础知识及GPIO中断
硬件平台:tiny4412 系统:linux-4.4 文件系统:busybox-1.25 编译器: arm-none-linux-gnueabi-gcc(gcc version 4.8.3 20140320) uboot:友善自带uboot. 一、DTS引入 1.什么是DTS?为什么要引入DTS? DTS即Device Tree Source设备树源码,DeviceTree是一种描述硬件的数据结构,它起源于OpenFirmware (OF)。 在Linux2.6中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,比如板上的platform设备、resource、i
[单片机]
<font color='red'>tiny4412</font>学习(四)之移植linux-设备树(1)设备树基础知识及GPIO中断
小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

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

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

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