linux内核的外部接口函数,linux内核中GPIO的使用(二)--标准接口函数
在linux内核中,有一些基本模块可以使用标准的接口函数来操作,比如GPIO、interrupt、clock,所谓的标准接口函数是指一些与硬件平台无关的、linux下做驱动通用的函数,
常用的有:
gpio_request();gpio_free();gpio_direction_output();gpio_direction_input();gpio_set_value();gpio_get_value()等;
函数分析:
1)GPIO申请:
int gpio_request(unsigned gpio, const char *label)
参数分析:
unsigned gpio:gpio端口号;
const char *label:给该gpio端口起的名字;
返回值:
成功返回0;失败返回负数的错误码;
不同平台的gpio端口号是不同的,比如在s5pv210平台上,在内核源码的arch/arm/mach-s5pv210/include/mach下有一个gpio.h的头文件,其中有:
/* S5PV210 GPIO number definitions */
#define S5PV210_GPA0(_nr) (S5PV210_GPIO_A0_START + (_nr))
#define S5PV210_GPA1(_nr) (S5PV210_GPIO_A1_START + (_nr))
#define S5PV210_GPB(_nr) (S5PV210_GPIO_B_START + (_nr))
#define S5PV210_GPC0(_nr) (S5PV210_GPIO_C0_START + (_nr))
#define S5PV210_GPC1(_nr) (S5PV210_GPIO_C1_START + (_nr))
#define S5PV210_GPD0(_nr) (S5PV210_GPIO_D0_START + (_nr))
#define S5PV210_GPD1(_nr) (S5PV210_GPIO_D1_START + (_nr))
…..
例如端口GPJD0_1就对应宏定义的S5PV210_GPD0(1)。
2)GPIO的初始化
初始化为输出,并设置输出电平:
int gpio_direction_output(unsigned gpio, int value);
初始化为输入:
int gpio_direction_input(unsigned gpio);
3)GPIO的输出电平设置:
void gpio_set_value(unsigned gpio, int value);
4)得到GPIO的电平
int gpio_get_value(unsigned gpio);
5)释放GPIO
void gpio_free(unsigned gpio);
下面做一个简单的led驱动,实现对基于S5PV210平台的4盏LED灯控制,对应的GPIO口为GPJ2_0、GPJ2_0、GPJ2_0、GPJ2_0。
static int __init leddev_init(void)
{
int ret;
/*************led gpio request and initial**************/
int i;
for( i = 0; i < 4; i++)
{
ret = gpio_request(S5PV210_GPJ2(i), "led_gpio");
if(ret < 0)
{
printk("S5PV210_GPJ2(%d)_request error!\n", i);
goto err_gpio_request;
}
gpio_direction_output(S5PV210_GPJ2(i), 1);
}
printk("led initial success!\n");
if(cdev_Major) //初始值不为0,则手动分配设备号
{
cdev_num = MKDEV(cdev_Major, cdev_Minor);
ret= register_chrdev_region(cdev_num, 1, cdev_name);
}
else
{
ret = alloc_chrdev_region(&cdev_num, cdev_Minor, 1, cdev_name); //动态分配设备号
cdev_Major = MAJOR(cdev_num);
}
if(ret < 0)
{
printk(KERN_WARNING"can not get major %d\n", cdev_Major);
goto err_leddev_region;
}
cdev_init(&leddev, &leddev_fops);//建立字符设备与文件操作集的联系
gpiodev.owner = THIS_MODULE;
ret = cdev_add(&leddev, cdev_num, 1);
if(ret)
{
printk("error while register device!\n");
goto err_gpiodev_add;
}
/******************auto create device file****************/
class_ret = class_create(THIS_MODULE, "ledclass");
if(class_ret == NULL)
{
printk("class_create error!\n");
goto err_class_creat;
}
device_ret = device_create(class_ret, NULL, cdev_num, NULL, "leddev");
if(device_ret == NULL)
{
printk("device_create error!\n");
goto err_device_create;
}
return 0;
err_device_create:
class_destroy(class_ret);
err_class_creat:
err_leddev_add:
unregister_chrdev_region(MKDEV(cdev_Major, cdev_Minor), 1);
err_leddev_region:
err_gpio_request:
for( ; i > 0; i--)
{
gpio_free(S5PV210_GPJ2(i - 1));
}
return ret;
}
static void __exit leddev_exit(void)
{
int i;
printk("exit from leddev!\n");
for(i = 0 ; i < 4; i++)
{
gpio_free(S5PV210_GPJ2(i));
}
class_destroy(class_ret);
device_destroy(class_ret, cdev_num);
unregister_chrdev_region(MKDEV(cdev_Major, cdev_Minor), 1);
cdev_del(&leddev);
}
module_init(leddev_init);
module_exit(leddev_exit);
MODULE_AUTHOR("Mr.Huang");
MODULE_DESCRIPTION("This is a leddev for testing!");
MODULE_LICENSE("GPL");
linux内核的外部接口函数,linux内核中GPIO的使用(二)--标准接口函数相关推荐
- python字符串find函数实现_python中实现查找字符串的find函数
原博文 2018-06-19 19:26 − 第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2 ...
- c ++类成员函数_C ++编程中的数据成员和成员函数
c ++类成员函数 C ++中的数据成员和成员函数 (Data members and Member functions in C++) "Data Member" and &qu ...
- js 闭包函数 构造函数_JavaScript中的闭包,库里函数和酷抽象
js 闭包函数 构造函数 In this article, we will talk about closures and curried functions and we'll play aroun ...
- php用于字符串函数是,php中用于查找字符串的常用函数
php中用于查找字符串的常用函数,包括strstr.stristr.strpos.substr等,本文主要为大家介绍strstr的使用,感兴趣的朋友不妨参考下. 一.strstr 查找字符串的首次出 ...
- java sign函数_C / C ++中是否有标准的符号函数(signum,sgn)?
我想要一个返回负数为-1和正数为+1的函数. http://en.wikipedia.org/wiki/Sign_function编写我自己的很容易,但是似乎应该放在标准库的某个地方. 编辑:具体来说 ...
- c语言中怎么调用自己定义的函数,c语言中怎么调用自己定义的函数?
一,函数调用的一般形式为: 函数名(实参列表); 实参可以是常数.变量.表达式等,多个实参用逗号,分隔. 在C语言中,函数调用的方式有多种,例如: 在函数调用中还应该注意的一个问题是求值顺序的问题.所 ...
- static函数在内存中只有一份,普通函数在每个被调用中维持一份拷贝?
转载自:CSDN-悠悠红茶 作为函数,不管其是不是static的,在代码段中都只有一份.每次对函数的调用, 一般而言都是一条call指令(当然还要参数入栈等动作)而已. 只有inline函数,会在每次 ...
- python isalpha函数用法_python中string模块各属性以及函数的用法
任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求 ...
- python自定义一个函数求序列中的偶数_使用range函数创建一个从2到10的偶数列表 alist...
[多选题]在Python中添加注释可以使用哪些符号? [简答题]给定一个包含若干个整数的列表alist,要求返回输出列表blist,blist中的元素为 与alist对应位置上的元素之外其余各元素的乘 ...
- python内置函数返回序列中最大元素_Python 内置函数 ____________ 用来返回序列中的最大元素。_学小易找答案...
[单选题]5. an official group of people who have joined together for a particular purpose [单选题]Excel 201 ...
最新文章
- web自适应尺寸方法
- This function or variable may be unsafe
- 【阿里云课程】循环神经网络:RNN及其改进
- Codeforces 1326F Wise Men (容斥原理、状压 DP、子集和变换、划分数)
- chrome 取消常用缩略图_怎么去掉 Chrome 新标签页的缩略图?
- JMetro版本5发布
- easyUI 日期控件修改...
- Linux启动tomcat命令行关闭后服务会停止
- webpack 编译stylus_webpack 文档更新日志(9.12-9.29)
- online_judge_1478
- 多元回归的拟合优度:R^2
- 学习HALCON视觉必会的入门知识| 东哥机器视觉培训
- 冷门但好用的 Python 库推荐一波
- picoCTF,Reverse Engineering,逆向类,42/55
- 悟空问答python反爬_悟_刘德华_高音质在线试听_悟歌词|歌曲下载_酷狗音乐
- 网页设计~登陆页面源代码
- 孤立的知识是没有用途的,融汇贯通、学以致用才是正道
- Failed to execute goal on project hello: Could not resolve dependencies for project com.hu:hello:jar
- 交通标志的检测与识别
- 中职对口升学计算机专业满分多少,浅谈如何提高中职计算机专业对口升学高考升学率...