嵌入式软件设计概述 (14).ppt
《嵌入式软件设计概述 (14).ppt》由会员分享,可在线阅读,更多相关《嵌入式软件设计概述 (14).ppt(55页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、蜂鸣器驱动程序蜂鸣器驱动程序运行于S3C2410/Linux 2.4内核。S3C2410芯片的GPB0端口连接蜂鸣器,当给GPB0口写低电平时,蜂鸣器鸣响,当给GPB0口写高电平时,蜂鸣器静音。蜂鸣器驱动程序蜂鸣器驱动程序硬件电路如图所示#include#include#include#include#include#include#include#include#include#include#include#include#define BUZZER_MAJOR 222#define BUZZER_DEVNAME buzzerbuzzer.c#define GPB_CTL_BASE io_
2、p2v(0 x56000010)#define S3C2410_GPBCON (GPB_CTL_BASE+0 x0)#define S3C2410_GPBDAT (GPB_CTL_BASE+0 x4)struct unit u32*GPB_CON;u32*GPB_DAT;static struct unit buzzer_unit=.GPB_CON=(u32*)S3C2410_GPBCON,.GPB_DAT=(u32*)S3C2410_GPBDAT,;buzzer.cstatic void buzzer_set_value(struct unit*unit,u8 val)if(val=1)*u
3、nit-GPB_DAT=*unit-GPB_DAT&(0 x1);/PB0置低电平,鸣响 else*unit-GPB_DAT=*unit-GPB_DAT|0 x1;/PB0置高电平,静音ssize_t Buzzer_open(struct inode*inode,struct file*file)MOD_INC_USE_COUNT;file-private_data=&buzzer_unit;return 0;buzzer.cssize_t Buzzer_release(struct inode*inode,struct file*file)MOD_DEC_USE_COUNT;return 0
4、;ssize_t Buzzer_write(struct file*file,const char*buf,size_t count,loff_t*offset)char temp;int ret;struct unit*unit=(struct unit*)file-private_data;ret=copy_from_user(&temp,buf,count);if(ret!=0)printk(wrong!n);return-EFAULT;else buzzer_set_value(unit,temp);return ret;buzzer.cstruct file_operations B
5、uzzer_Ctl_ops=open:Buzzer_open,write:Buzzer_write,release:Buzzer_release,;static devfs_handle_t devfs_handle,devfs_buzzer_dir;static void _init init_hardware(struct unit*unit)*unit-GPB_CON&=0 x2;/PBCON1=0 *unit-GPB_CON|=0 x1;/PBCON0=1 PB0为输出*unit-GPB_DAT|=0 x1;/PB0初始值为1,蜂鸣器静音buzzer.cstatic int _init
6、 init_buzzer()int res;printk(This is my Buzzer driver!n);res=devfs_register_chrdev(BUZZER_MAJOR,BUZZER_DEVNAME,&Buzzer_Ctl_ops);devfs_buzzer_dir=devfs_mk_dir(NULL,BUZZER_DEVNAME,NULL);devfs_handle=devfs_register(devfs_buzzer_dir,0,DEVFS_FL_DEFAULT,BUZZER_MAJOR,0,S_IFCHR|S_IRUSR|S_IWUSR,&Buzzer_Ctl_o
7、ps,NULL);init_hardware(&buzzer_unit);return res;static void _exit clean_buzzer()devfs_unregister_chrdev(BUZZER_MAJOR,BUZZER_DEVNAME);devfs_unregister(devfs_handle);devfs_unregister(devfs_buzzer_dir);buzzer.cmodule_init(init_buzzer);module_exit(clean_buzzer);MODULE_DESCRIPTION(EduKit III-2410 buzzer
8、driver);MODULE_AUTHOR(SSDUT);MODULE_LICENSE(GPL);buzzer.c利用下面的Makefile交叉编译驱动程序蜂鸣器驱动的蜂鸣器驱动的makefile#Makefile for the kernel buzzer driver(Module).WKDIR=/usr/local/src/edukit-2410CROSSDIR=/usrINSTALLDIR=/home/app#$(WKDIR)/driversMODDEV=buzzer.oMODFILE=buzzer.cMODFILE_H=CROSS=arm-linux-CC=$(CROSS)gccAS
9、=$(CROSS)asLD=$(CROSS)lddriver_makefileMACRO=-DMODULE-D_KERNEL_-DCONFIG_KERNELDifdef DEBUGCFLAGS=-gendifCFLAGS=-O2-fomit-frame-pointerCFLAGS+=$(MACRO)-mapcs-32-march=armv4-mtune=arm9tdmi-fno-builtinINCLUDES=-I$(WKDIR)/kernel/include-I$(CROSSDIR)/arm-linux/include-I$(CROSSDIR)/lib/gcc-lib/arm-linux/2
10、.95.3/include$(MODDEV):$(MODFILE)$(MODFILE_H)Makefile$(CC)$(CFLAGS)$(INCLUDES)-o$-c$install:$(MODDEV)mkdir-p$(INSTALLDIR)cp-target-dir=$(INSTALLDIR)$(MODDEV)clean:-rm-f$(MODDEV)driver_makefile下面的程序用来测试蜂鸣器设备是否正常工作。蜂鸣器测试程序蜂鸣器测试程序#include#include#include#include int main(int argc,char*argv)int fd,on,of
11、f;int i=0;static char*driver=/dev/buzzer/0;on=1;off=0;printf(BUZZER test example base on Linux.n);fd=open(driver,O_RDWR);if(fd 0)printf(Cant not open%sn,driver);return fd;buzzer_test.cfor(;i10;i+)if(i%2=0)write(fd,&on,1);printf(Buzzer ON!n);usleep(1000*1000);elsewrite(fd,&off,1);printf(Buzzer OFF!n)
12、;sleep(1);printf(end.n);close(fd);return 0;buzzer_test.c运行于S3C2410/Linux 2.4内核。每当按下实验设备上的按键,即可触发一个中断,然后在超级终端上显示一条信息。中断按键驱动程序中断按键驱动程序硬件电路如图所示#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include eint0.c#define MY_EINT0_SIG 50
13、/自定义的信号MYSIG#define EINT0_DEVNAME eint0#define EINT0_MAJOR 223#define S3C2410_GPFCON io_p2v(0 x56000050)#define S3C2410_EXTINT0io_p2v(0 x56000088)static int pid;/用于保存应用程序的进程号,当中断发生时/才能将MYSIG信号准确的发给应用程序struct unit u32*GPF_CON;u32*rEXTINT0;static struct unit eint0_unit=.GPF_CON=(u32*)S3C2410_GPFCON,.r
14、EXTINT0=(u32*)S3C2410_EXTINT0,;static int mysig(void);eint0.cvoid eint0_do_tasklet();DECLARE_TASKLET(my_tasklet,eint0_do_tasklet,NULL);void eint0_do_tasklet()mysig();/中断上半部,直接注册中断并被中断触发,在其中调度下半部static void eint0_handler(int irq,void*dev_id,struct pt_regs*reg)/此函数用来调度一个tasklet运行tasklet_schedule(&my_t
15、asklet);static int mysig(void)siginfo_t info;struct task_struct*p;/task_struct即PCB,用来描述每一个进程eint0.cread_lock(&tasklist_lock);/减1,为负则加锁,等待锁变量变为1,否则 /遍历(环形)进程表中的每一个进程:for_each_task(p)if(p-pid=pid)read_unlock(&tasklist_lock);goto find_ps;read_unlock(&tasklist_lock);printk(can not find processn);return-
16、1;find_ps:send_sig_info(MY_EINT0_SIG,&info,p);return 0;eint0.cssize_t eint0_open(struct inode*inode,struct file*file)MOD_INC_USE_COUNT;return 0;ssize_t eint0_release(struct inode*inode,struct file*file)MOD_DEC_USE_COUNT;return 0;ssize_t eint0_read(struct file*file,char*buf,size_t count,loff_t*offset
17、)return 0;ssize_t eint0_write(struct file*file,const char*buf,size_t count,loff_t*offset)int ret,temp;eint0.cret=copy_from_user(&temp,buf,count);pid=temp;return 0;struct file_operations eint0_Ctl_ops=open:eint0_open,read:eint0_read,write:eint0_write,release:eint0_release,;static void _init init_hard
18、ware(struct unit*unit)*unit-GPF_CON&=(0 x3);*unit-GPF_CON|=(1rEXTINT0&=(3rEXTINT0|=(11);static devfs_handle_t devfs_handle,devfs_eint0_dir;eint0.cstatic int _init init_eint0()int res;printk(This is my Eint0 driver!n);devfs_register_chrdev(EINT0_MAJOR,EINT0_DEVNAME,&eint0_Ctl_ops);devfs_eint0_dir=dev
19、fs_mk_dir(NULL,EINT0_DEVNAME,NULL);devfs_handle=devfs_register(devfs_eint0_dir,0,DEVFS_FL_DEFAULT,EINT0_MAJOR,0,S_IFCHR|S_IRUSR|S_IWUSR,&eint0_Ctl_ops,NULL);init_hardware(&eint0_unit);res=request_irq(IRQ_EINT0,eint0_handler,SA_INTERRUPT,EINT0_DEVNAME,NULL);if(res 0)printk(The IRQ Wrong!n);return res
20、;eint0.cstatic void _exit clean_eint0()devfs_unregister_chrdev(EINT0_MAJOR,EINT0_DEVNAME);devfs_unregister(devfs_handle);devfs_unregister(devfs_eint0_dir);free_irq(IRQ_EINT0,NULL);module_init(init_eint0);module_exit(clean_eint0);MODULE_DESCRIPTION(EduKitIII-2410 eint0 driver);MODULE_AUTHOR(SSDUT);MO
21、DULE_LICENSE(GPL);eint0.c利用下面的Makefile交叉编译驱动程序中断按键驱动的中断按键驱动的makefile#Makefile for the eint0 driver.WKDIR=/usr/local/src/edukit-2410CROSSDIR=/usrINSTALLDIR=/home/app#$(WKDIR)/driversMODDEV=eint0.oMODFILE=eint0.cMODFILE_H=CROSS=arm-linux-CC=$(CROSS)gccAS=$(CROSS)asLD=$(CROSS)ldMACRO=-DMODULE-D_KERNEL_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 嵌入式软件设计概述 14 嵌入式 软件设计 概述 14
限制150内