linux驱动第一课-helloword

Linux驱动属于内核的一部分,开发时可设计成模块,内核模块在linux系统运行时可以加载和卸载模块
1、驱动编写的注意事项:

(1)不能使用C库和C标准头文件  
(2)使用GNU-C 
(3)没有内存保护机制
(4)不能处理浮点运算
(5)注意并发互斥性和可移植性

2、内核模块如何编写需要包含头文件

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

要实现加载函数卸载函数
加载函数模板:(函数返回0表示加载成功)

 int hello_init(void)
{
    :
    :
    :
    return 0;//返回0表示加载成功
}

卸载函数模板:(函数没有返回值)

void hello_ exit(void)
{
  :
  :
}

使用以下两个宏来修饰加载函数卸载函数

medule. init(hello_init);//修饰加裁函数
module_ exit(hello_exit);//修饰卸教函数
:内核中打印函数使用printk,用法和printf几乎一样

例子:

#include <linux/init.h> //要包含的头文件
#include <linux/module.h>//要包含的头文件

//加载函数
static int __init helloword_init(void)   
{   
   //加载函数执行时,会打印helloword!!                        
   printk("helloword!!\n");
return 0;//返回值0
}
//卸载函数
static void __exit helloword_exit(void)
{
   //卸载函数执行时,会打印goodbye!!  
   printk("goodbye!!\n");
}
module_init(helloword_init);//修饰加裁函数
module_exit(helloword_exit);//修饰卸教函数
MODULE_LICENSE("GPL");

在此说明:

这部分的
------->make menuconfig
------->makefile
------->kconfig
的配置要自己去配置,总要
自己去查阅资料,动手练习的。
在我的博客:helloword分栏中《基于RK3288平台的第一个helloword程序》有具体的介绍。
《基于RK3288平台的第一个helloword程序》地址:
https://editor.csdn.net/md/?articleId=102548515

祝学习驱动的人早日上手!!!!

猜你喜欢

转载自blog.csdn.net/qq_38312843/article/details/107345729