/****************************************************************************
*
* list_head,proc file system,GPIO,ioremap
*
* 声明:
* 1. 本系列文档是在vim下编辑,请尽量是用vim来阅读,在其它编辑器下可能会
* 不对齐,从而影响阅读.
* 2. 本文中有些源代码没有全部帖出来,主要是因为篇幅太大的原因;
* 3. 基于2中的原因,本文借鉴了python中的缩进代码风格进行代码的体现:
* 1. 有些代码中的'...'代表省略了不影响阅读的代码;
* 2. 如下代码缩进代表在一个函数内部的代码,至于在什么函数里,不影响阅读:
* ... //省略代码
* struct test_s {
* };
* ... //省略代码
*
* //进入临界区之前加锁 }
* spin_lock(&p->lock); |
* | |
* /* 有效代码 */ |-->|采用缩进,代表在一个函数内
* | |的代码
* //出临界区之后解锁 |
* spin_unlock(&p->lock); }
*
* ... //省略代码
* int __init test_init(void)
* {
* ... //省略代码
* }
* ... //省略代码
*
****************************************************************************/
\\\\\--*目录*--//////////
| 一. list_head常用接口:
| 二. proc文件系统相关操作:
| 三. gpio_request相关操作:
| 四. ioremap相关操作:
| 五. LED驱动写法:
| 六. 测试LED驱动:
\\\\\\\\///////////////
一. list_head常用接口:
1. 定义内核链表头,并初始化:
LIST_HEAD(test_head);
2. 两种链表添加方式:
1. 头插: list_add();
2. 尾插: list_add_tail();
3. 两种特殊的for each循环遍历,内部使用了typeof查询类型,再加上container_of:
1. list_for_each_entry();
2. list_for_each_entry_reverse();
4. 普通的for each循环遍历:
list_for_each();
5. 内核链表Demo:
... // '...'代表省略一些不影响分析代码的代码
struct test_list {
int data;
struct list_head entry; //内核链表
};
//定义一个内核链表头,并初始化
LIST_HEAD(test_head);
struct test_list item[NUM];
int __init test_init(void)
{
int i;
struct test_list *tail;
struct list_head *p;
for(i = 0; i < NUM; i++)
{
item[i].data = i;
//头插方式
/*list_add(&item[i].entry, &test_head);*/
//尾插方式
list_add_tal(&item[i].entry, &test_head);
}
//遍历链表,这两个宏里面都使用了typeof来配合container_of获取结构体指针
/*list_for_each_entry(tail, &test_head, entry)*/
list_for_each_entry_reverse(tail, &test_head, entry)
{
printk('%d ', tail->data);
}
printk('n');
//没有像上面那样得到结构体指针,需要自己使用container_of获取结构体指针
list_for_each(p, &test_head)
{
tail = container_of(p, struct test_list, entry);
printk('%d ', tail->data);
}
printk('n');
return 0;
}
...
二. proc文件系统相关操作: //这里的资料不足,需要另外参考网络资料
1. 头文件:
#include <linux/proc_fs.h>
#include 2. 创建文件: proc_create(); 3. 创建目录: proc_mkdir(); 4. 删除文件,目录: remove_proc_entry(); 5. 访问方法,此处的资料不够,需要另外查相关资料: 1. seq_printf(); 2. single_open(); 3. single_release(); 4. seq_read(); 5. seq_lseek(); 6. proc文件系统访问Demo: #include #include #include static int num = 11223344; static int my_show(struct seq_file *file, void *data) { return seq_printf(file, 'num is %dn', num); } static int test_open(struct inode *inode, struct file *file) { return single_open(file, my_show, NULL); } struct file_operations fops = { .owner = THIS_MODULE, .open = test_open, .release = single_release, .read = seq_read, .llseek = seq_lseek, }; struct proc_dir_entry *pde; int __init test_init(void) { //在proc文件系统上创建空目录 pde = proc_mkdir('test_dir', NULL); if(!pde) return -EFAULT; //在proc文件系统指定目录上创建文件 /** * 1. 'proc_test' : 创建的文件名; * 2. 0644 : 八进制,表示创建的文件权限; * 3. pde : 前面创建文件夹的指针; * 4. &fops : 文件操作指针; */ proc_create('proc_test', 0644, pde, &fops); return 0; } void __exit test_exit(void) { //先删除proc文件系统上的目录 remove_proc_entry('proc_test', pde); //删除proc文件系统上proc_test目录的文件
上一篇:Samsung_tiny4412(驱动笔记07)----spinlock,semaphore,atomic,mutex,completion,interrupt
下一篇:Samsung_tiny4412(驱动笔记05)----Makefile,open,read,write,lseek,poll,ioctl,fasync
- 热门资源推荐
- 热门放大器推荐
- 使用 Analog Devices 的 LTC1147 的参考设计
- LF33ABV 3.3V低压灯泡典型应用
- LTC3245EDE 宽输入范围低噪声 3.6V 电源的典型应用电路
- 非军用无人机、机器人或 RC 2S1P 电池管理解决方案参考设计
- LT6654AHS6-1.25 电压基准的典型应用,具有升压输出电流和电流限制
- ADM7150CP-EVALZ,用于评估 ADM7150 线性稳压器的评估板
- AD8175-EVALZ,基于 AD8175 三路视频交叉点开关的评估板
- LT3970EMS-3.3 1.8V 降压转换器的典型应用
- EV-ADF4360-0EB1Z,用于 ADF4360-0 集成 PLL 和 VCO 频率合成器的评估板
- MC34072DR2G 二阶低通有源滤波器的典型应用



现代雷达系统的信号设计
MCP6L71T-E/MS

BFR340T






京公网安备 11010802033920号