嵌入式软件设计:驱动程序举例

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
*unit->GPB_DAT = *unit->GPB_DAT | 0x1; //PB0置高电平,静音 } } ssize_t Buzzer_open(struct inode * inode, struct file * file) { MOD_INC_USE_COUNT; file->private_data = &buzzer_unit; return 0; }
}
else
buzzer_set_value(unit, temp);
return ret;
}
buzzer.c
struct file_operations Buzzer_Ctl_ops =
{
open:
Buzzer_open,
write:
Buzzer_write,
release:
Buzzer_release,
#$(WKDIR)/drivers
MODDEV
= buzzer.o
MODFILE
= buzzer.c
MODFILE_H =
CROSS=arm-linux-
CC = $(CROSS)gcc
AS = $(CROSS)as
LD = $(CROSS)ld
driver_makefile
MACRO = -DMODULE -D__KERNEL__ -DCONFIG_KERNELD ifdef DEBUG CFLAGS = -g endif CFLAGS = -O2 -fomit-frame-pointer CFLAGS += $(MACRO) -mapcs-32 -march=armv4 -mtune=arm9tdmi -fno-builtin INCLUDES = -I$(WKDIR)/kernel/include \
int fd, on, off; 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) {
}; static devfs_handle_t devfs_handle,devfs_buzzer_dir; static void __init init_hardware(struct unit *unit) {
*unit->GPB_CON &= ~0x2; *unit->GPB_CON |= 0x1; *unit->GPB_DAT |= 0x1; }
{
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;
= (u32 *)S3C2410_GPBCON, = (u32 *)S3C2410_GPBDAT,
buzzer.c
static void buzzer_set_value(struct unit *unit, u8 val) {
if(val==1) {
*unit->GPB_DAT = *unit->GPB_DAT & (~0x1);//PB0置低电平,鸣响 } else {
蜂鸣器驱动程序
蜂鸣器驱动程序
运行于S3C2410/Linux 2.4内核。S3C2410芯片的GPB0端口连接蜂鸣器,
当给GPB0口写低电平时,蜂鸣器鸣响,当给GPB0口写高电平时,蜂鸣器
静音。
Vcc
硬件电路如图所示
GPB0
R1 1.5K
SPEAKER
E B
Βιβλιοθήκη Baidu9013
C
#include <linux/module.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/types.h> #include <linux/delay.h> #include <linux/proc_fs.h> #include <asm/uaccess.h> #include <asm/hardware.h> #include <asm/arch-s3c2410/s3c2410.h> #define BUZZER_MAJOR 222 #define BUZZER_DEVNAME "buzzer"
buzzer.c
ssize_t Buzzer_release(struct inode * inode, struct file * file)
{
MOD_DEC_USE_COUNT;
return 0;
}
ssize_t Buzzer_write(struct file * file, const char * buf, size_t count, loff_t * offset)
devfs_unregister(devfs_buzzer_dir);
}
buzzer.c
module_init(init_buzzer); module_exit(clean_buzzer); MODULE_DESCRIPTION("EduKit III-2410 buzzer driver"); MODULE_AUTHOR("SSDUT"); MODULE_LICENSE("GPL");
driver_makefile
蜂鸣器测试程序
下面的程序用来测试蜂鸣器设备是否正常工作。
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <linux/fcntl.h>
int main(int argc, char** argv) {
//PBCON1=0 //PBCON0=1 PB0为输出 //PB0初始值为1,蜂鸣器静音
buzzer.c
static int __init init_buzzer()
{
int res;
printk("This is my Buzzer driver!\n");
res=devfs_register_chrdev(BUZZER_MAJOR,BUZZER_DEVNAME,
buzzer.c
#define GPB_CTL_BASE #define S3C2410_GPBCON #define S3C2410_GPBDAT
io_p2v(0x56000010) (GPB_CTL_BASE + 0x0) (GPB_CTL_BASE + 0x4)
struct unit {
u32 *GPB_CON; u32 *GPB_DAT; }; static struct unit buzzer_unit = { .GPB_CON .GPB_DAT };
init_hardware(&buzzer_unit);
return res;
}
static void __exit clean_buzzer()
{
devfs_unregister_chrdev(BUZZER_MAJOR,BUZZER_DEVNAME);
devfs_unregister(devfs_handle);
&Buzzer_Ctl_ops);
devfs_buzzer_dir = devfs_mk_dir(NULL, BUZZER_DEVNAME, NULL);
devfs_handle=devfs_register(devfs_buzzer_dir,"0",DEVFS_FL_DEFAULT,
BUZZE
R_MAJOR,0,S_IFCHR|S_IRUSR|S_IWUSR,&Buzzer_Ctl_ops, NULL);
buzzer.c
蜂鸣器驱动的makefile
利用下面的Makefile交叉编译驱动程序
# Makefile for the kernel buzzer driver (Module).
WKDIR
= /usr/local/src/edukit-2410
CROSSDIR = /usr
INSTALLDIR = /home/app
buzzer_test.c
中断按键驱动程序
中断按键驱动程序
运行于S3C2410/Linux 2.4内核。每当按下实验设备上的按键,即可触发一 个中断,然后在超级终端上显示一条信息。
硬件电路如图所示
#include <linux/module.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/types.h> #include <linux/delay.h> #include <linux/proc_fs.h> #include <asm/uaccess.h> #include <asm/hardware.h> #include <asm/arch-s3c2410/s3c2410.h> #include <asm-arm/arch-s3c2410/irqs.h> #include <linux/wait.h> #include <linux/interrupt.h> #include <linux/signal.h>
.GPF_CON
= (u32 *)S3C2410_GPFCON,
.rEXTINT0
= (u32 *)S3C2410_EXTINT0,
};
static int mysig(void);
eint0.c
void eint0_do_tasklet();
DECLARE_TASKLET(my_tasklet,eint0_do_tasklet,NULL);
printf("Can't not open %s\n",driver); return fd; }
buzzer_test.c
for(;i<10;i++) {
if(i%2==0) {
write(fd, &on, 1); printf("Buzzer ON!\n"); usleep(1000*1000); } else { write(fd, &off, 1); printf("Buzzer OFF!\n"); sleep(1); } } printf(" end.\n"); close(fd); return 0; }
eint0.c
#define MY_EINT0_SIG
50 // 自定义的信号MYSIG
#define EINT0_DEVNAME
"eint0"
#define EINT0_MAJOR
223
#define S3C2410_GPFCON
io_p2v(0x56000050)
#define S3C2410_EXTINT0
io_p2v(0x56000088)
static int pid;
// 用于保存应用程序的进程号,当中断发生时
//才能将MYSIG信号准确的发给应用程序
struct unit {
u32 *GPF_CON;
u32 *rEXTINT0;
};
static struct unit eint0_unit = {
void eint0_do_tasklet()
{
mysig();
}
//中断上半部,直接注册中断并被中断触发,在其中调度下半部
-I$(CROSSDIR)/arm-linux/include \ -I$(CROSSDIR)/lib/gcc-lib/arm-linux/2.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)
相关文档
最新文档