Linux高级应用(十)控制蜂鸣器的应用程序
實驗步驟:
1、設計一個簡易蜂鳴器驅動
beep_drv.c
2、編譯驅動
Makefile
生成ko文件
3、查看驅動的信息
#modinfo beep_drv.ko
在ubuntu上
4、下載ko到試驗箱
5、安裝ko—蜂鳴器響
#insmod beep_drv.ko
#lsmod
如果ko安裝有錯,說申請不到GPIO
解決:
Device Drivers —>
Character devices —>
<> LED Support for FriendlyARM Mini210 GPIO LEDs
<> Buzzer driver for FriendlyARM Mini210 development boards
輸入“N”
改成:
Device Drivers —>
Character devices —>
< > LED Support for FriendlyARM Mini210 GPIO LEDs
< > Buzzer driver for FriendlyARM Mini210 development boards
重新編譯內核—>燒寫內核
6、卸載驅動—蜂鳴器
#rmmod beep_drv
#lsmod
=================================================================================
二、前面還沒有驗收的實驗,完成。
三、分析LED燈驅動
gec210_led.c
四、在project添加蜂鳴器的應用程序(驅動程序待定)
beep.h
#define BEEP_ON 1
#define BEEP_OFF 0
beep.c
int beep_ctrl(char beep_state)
{
參考:編寫一個控制蜂鳴器的應用程序
}
一、編寫一個控制蜂鳴器的應用程序
int main(void)
{
int fd_beep;
//1.打開蜂鳴器
fd_beep = open("/dev/beep_drv", O_RDWR);
if(fd_beep == -1)
{
perror(“open beep”);
return -1;
}
}
====================================================================================
二、蜂鳴器的原理圖
處理器的GPIO引腳GPD0_0用來控制蜂鳴器,該引腳輸出高電平,蜂鳴器響,引腳輸出低電平,蜂鳴器不響。
見原理圖。
====================================================================================
三、驅動程序設計思路
應用程序使用write(fd_beep, &beep_flag, 1);向驅動程序寫入了數據;在驅動程序中,應該有函數接收到這個數據,并利用這個數據來控制GPIO的輸出狀態。
====================================================================================
四、驅動程序的設計步驟
1、設計一個內核模塊—kernel module
內核模塊相當于是一個“盒子”,這個盒子里面裝的是一個驅動程序。驅動程序本身也是Linux內核中獨立的一個模塊。每設計一個驅動程序,就需要先設計一個模塊。
模塊編譯后會生成ko文件,模塊可安裝可卸載。
#insmod beep_drv.ko (insert module—安裝模塊,相當于安裝驅動,驅動安裝到Linux內核中)
#rmmod beep_drv(remove module—卸載模塊,相當于卸載驅動)
注意:驅動的安裝過程和卸載過程是一個反過程,驅動安裝從內核申請資源,驅動卸載釋放資源給內核。
module由三部分組成的,分別是:頭文件、驅動的入口和出口函數、驅動的描述
例:
//驅動的頭文件
#include <linux/init.h>
#include <linux/module.h>
//蜂鳴器驅動的安裝函數
static int __init mini210_beep_init(void)
{
//蜂鳴器的安裝過程
}
//蜂鳴器驅動的卸載函數
static void __exit mini210_beep_exit(void)
{
//蜂鳴器驅動的卸載過程
}
//模塊的入口和出口
module_init(mini210_beep_init);//#insmod beep_drv.ko–>驅動的入口
module_exit(mini210_beep_exit);//#rmmod beep_drv–>驅動的出口
//模塊的描述(驅動的描述)
MODULE_AUTHOR(“bobeyfeng@163.com”);
MODULE_DESCRIPTION(“S5PV210 BEEP Device Driver”);
MODULE_LICENSE(“GPL”);
MODULE_VERSION(“V1.0”);
2、申請和釋放GPIO口(GPD0_0引腳)
#include <linux/gpio.h>
1)申請GPIO
static inline int gpio_request(unsigned gpio, const char *label)
函數說明:
unsigned gpio----每個GPIO都有一個IO口號
const char *label ----- 自定義的GPIO名字
返回值:
成功,返回0;失敗,返回負數的錯誤碼
2)釋放GPIO
void gpio_free(unsigned gpio)
函數說明:
unsigned gpio----每個GPIO都有一個IO口號
3)蜂鳴器GPIO的IO號是多少?
GPIO口號是每個GPIO的標志,每個GPIO都有唯一的一個GPIO號,它相當于GPIO的ID。
內核源碼/arch/arm/mach-s5pv210/inlucde/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))
#define S5PV210_GPE0(_nr) (S5PV210_GPIO_E0_START + (_nr))
#define S5PV210_GPE1(_nr) (S5PV210_GPIO_E1_START + (_nr))
#define S5PV210_GPF0(_nr) (S5PV210_GPIO_F0_START + (_nr))
#define S5PV210_GPF1(_nr) (S5PV210_GPIO_F1_START + (_nr))
#define S5PV210_GPF2(_nr) (S5PV210_GPIO_F2_START + (_nr))
#define S5PV210_GPF3(_nr) (S5PV210_GPIO_F3_START + (_nr))
#define S5PV210_GPG0(_nr) (S5PV210_GPIO_G0_START + (_nr))
#define S5PV210_GPG1(_nr) (S5PV210_GPIO_G1_START + (_nr))
#define S5PV210_GPG2(_nr) (S5PV210_GPIO_G2_START + (_nr))
#define S5PV210_GPG3(_nr) (S5PV210_GPIO_G3_START + (_nr))
#define S5PV210_GPH0(_nr) (S5PV210_GPIO_H0_START + (_nr))
#define S5PV210_GPH1(_nr) (S5PV210_GPIO_H1_START + (_nr))
#define S5PV210_GPH2(_nr) (S5PV210_GPIO_H2_START + (_nr))
#define S5PV210_GPH3(_nr) (S5PV210_GPIO_H3_START + (_nr))
#define S5PV210_GPI(_nr) (S5PV210_GPIO_I_START + (_nr))
#define S5PV210_GPJ0(_nr) (S5PV210_GPIO_J0_START + (_nr))
#define S5PV210_GPJ1(_nr) (S5PV210_GPIO_J1_START + (_nr))
#define S5PV210_GPJ2(_nr) (S5PV210_GPIO_J2_START + (_nr))
#define S5PV210_GPJ3(_nr) (S5PV210_GPIO_J3_START + (_nr))
#define S5PV210_GPJ4(_nr) (S5PV210_GPIO_J4_START + (_nr))
#define S5PV210_MP01(_nr) (S5PV210_GPIO_MP01_START + (_nr))
#define S5PV210_MP02(_nr) (S5PV210_GPIO_MP02_START + (_nr))
#define S5PV210_MP03(_nr) (S5PV210_GPIO_MP03_START + (_nr))
#define S5PV210_MP04(_nr) (S5PV210_GPIO_MP04_START + (_nr))
#define S5PV210_MP05(_nr) (S5PV210_GPIO_MP05_START + (_nr))
#define S5PV210_MP06(_nr) (S5PV210_GPIO_MP06_START + (_nr))
#define S5PV210_MP07(_nr) (S5PV210_GPIO_MP07_START + (_nr))
#define S5PV210_MP10(_nr) (S5PV210_GPIO_MP10_START + (_nr))
#define S5PV210_MP11(_nr) (S5PV210_GPIO_MP11_START + (_nr))
#define S5PV210_MP12(_nr) (S5PV210_GPIO_MP12_START + (_nr))
#define S5PV210_MP13(_nr) (S5PV210_GPIO_MP13_START + (_nr))
#define S5PV210_MP14(_nr) (S5PV210_GPIO_MP14_START + (_nr))
#define S5PV210_MP15(_nr) (S5PV210_GPIO_MP15_START + (_nr))
#define S5PV210_MP16(_nr) (S5PV210_GPIO_MP16_START + (_nr))
#define S5PV210_MP17(_nr) (S5PV210_GPIO_MP17_START + (_nr))
#define S5PV210_MP18(_nr) (S5PV210_GPIO_MP18_START + (_nr))
#define S5PV210_MP20(_nr) (S5PV210_GPIO_MP20_START + (_nr))
#define S5PV210_MP21(_nr) (S5PV210_GPIO_MP21_START + (_nr))
#define S5PV210_MP22(_nr) (S5PV210_GPIO_MP22_START + (_nr))
#define S5PV210_MP23(_nr) (S5PV210_GPIO_MP23_START + (_nr))
#define S5PV210_MP24(_nr) (S5PV210_GPIO_MP24_START + (_nr))
#define S5PV210_MP25(_nr) (S5PV210_GPIO_MP25_START + (_nr))
#define S5PV210_MP26(_nr) (S5PV210_GPIO_MP26_START + (_nr))
#define S5PV210_MP27(_nr) (S5PV210_GPIO_MP27_START + (_nr))
#define S5PV210_MP28(_nr) (S5PV210_GPIO_MP28_START + (_nr))
#define S5PV210_ETC0(_nr) (S5PV210_GPIO_ETC0_START + (_nr))
#define S5PV210_ETC1(_nr) (S5PV210_GPIO_ETC1_START + (_nr))
#define S5PV210_ETC2(_nr) (S5PV210_GPIO_ETC2_START + (_nr))
#define S5PV210_ETC4(_nr) (S5PV210_GPIO_ETC4_START + (_nr))
所以得到GPD0_0引腳的GPIO號是S5PV210_GPD0(0)
3、配置GPIO為輸出還是輸出
int gpio_direction_output(unsigned gpio, int value)
int gpio_direction_input(unsigned gpio)
4、設置GPIO輸出值或者獲取GPIO的輸入值
int gpio_get_value(unsigned gpio)
void gpio_set_value(unsigned gpio, int value)
總結
以上是生活随笔為你收集整理的Linux高级应用(十)控制蜂鸣器的应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: resolver
- 下一篇: uni app 使用live-pushe