DTS

Github: ror16

Email: BuddyZhang1 buddy.zhang@aliyun.com

目录


源码分析

/**
 * ror16 - rotate a 16-bit value right
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u16 ror16(__u16 word, unsigned int shift)
{
        return (word >> shift) | (word << (16 - shift));
}

ror16() 函数的作用是将一个 16 位数据循环右移 shift 位。函数 将 16 位数据右移 shift 位之后或上 16 位数据左移 “16 - shift” 的值,组成一个循环右移的新 16 位数.


实践

驱动源码

/*
 * Bitops
 *
 * (C) 2020.01.09 BuddyZhang1 <buddy.zhang@aliyun.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/bitops.h>

/* Module initialize entry */
static int __init Demo_init(void)
{
        __u16 word = 0x1234;
        __u16 data;

        data = ror16(word, 1);
        printk("Default: %#hx shift  1 -> %#x\n", word, data);
        data = ror16(word, 2);
        printk("Default: %#hx shift  2 -> %#x\n", word, data);
        data = ror16(word, 4);
        printk("Default: %#hx shift  4 -> %#x\n", word, data);
        data = ror16(word, 8);
        printk("Default: %#hx shift  8 -> %#x\n", word, data);
        data = ror16(word, 12);
        printk("Default: %#hx shift 12 -> %#x\n", word, data);

        return 0;
}

/* Module exit entry */
static void __exit Demo_exit(void)
{
}

module_init(Demo_init);
module_exit(Demo_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("BiscuitOS <buddy.zhang@aliyun.com>");
MODULE_DESCRIPTION("Bitops Device driver");

驱动安装

驱动的安装很简单,首先将驱动放到 drivers/BiscuitOS/ 目录下,命名为 bitmap.c, 然后修改 Kconfig 文件,添加内容参考如下:

diff --git a/drivers/BiscuitOS/Kconfig b/drivers/BiscuitOS/Kconfig
index 4edc5a5..1a9abee 100644
--- a/drivers/BiscuitOS/Kconfig
+++ b/drivers/BiscuitOS/Kconfig
@@ -6,4 +6,14 @@ if BISCUITOS_DRV
config BISCUITOS_MISC
        bool "BiscuitOS misc driver"
+config BISCUITOS_BITMAP
+       bool "bitmap"
+
+if BISCUITOS_BITMAP
+
+config DEBUG_BISCUITOS_BITMAP
+       bool "ror16"
+
+endif # BISCUITOS_BITMAP
+
endif # BISCUITOS_DRV

接着修改 Makefile,请参考如下修改:

diff --git a/drivers/BiscuitOS/Makefile b/drivers/BiscuitOS/Makefile
index 82004c9..9909149 100644
--- a/drivers/BiscuitOS/Makefile
+++ b/drivers/BiscuitOS/Makefile
@@ -1 +1,2 @@
obj-$(CONFIG_BISCUITOS_MISC)     += BiscuitOS_drv.o
+obj-$(CONFIG_BISCUITOS_BITMAP)     += bitmap.o
--

驱动配置

驱动配置请参考下面文章中关于驱动配置一节。在配置中,勾选如下选项,如下:

Device Driver--->
    [*]BiscuitOS Driver--->
        [*]bitmap
            [*]ror16()

具体过程请参考:

Linux 5.0 开发环境搭建 – 驱动配置

驱动编译

驱动编译也请参考下面文章关于驱动编译一节:

Linux 5.0 开发环境搭建 – 驱动编译

驱动运行

驱动的运行,请参考下面文章中关于驱动运行一节:

Linux 5.0 开发环境搭建 – 驱动运行

启动内核,并打印如下信息:

Default: 0x1234 shift  1 -> 0x91a
Default: 0x1234 shift  2 -> 0x48d
Default: 0x1234 shift  4 -> 0x4123
Default: 0x1234 shift  8 -> 0x3412
Default: 0x1234 shift 12 -> 0x2341

驱动分析

将一个 16 位数据循环右移 shift 位。


附录

BiscuitOS Home

BiscuitOS Driver

BiscuitOS Kernel Build

Linux Kernel

Bootlin: Elixir Cross Referencer

搭建高效的 Linux 开发环境

赞赏一下吧 🙂

MMU