1.编写LED的驱动,蜂鸣器的驱动,风扇的驱动
2应用程序开三个线程,第一个线程实现三个灯的流水,第二个线程实现蜂鸣器控制,第三个线程实现风扇的控制
驱动程序
led.c
#include
#include
#include
#include
#include
#include "head/led.h"
#include
unsigned int major;
char kbuf[128] = {};
static volatile int stop_led_sequence = 0;
unsigned int *vir_moder_e;
unsigned int *vir_odr_e;
unsigned int *vir_moder_f;
unsigned int *vir_odr_f;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char __user *ubuf,size_t size,loff_t *loff_t)
{
unsigned long ret = copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filad\n");
return -ret;
}
return ret;
}
ssize_t mycdev_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loff_t)
{
unsigned long ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filad\n");
return -ret;
}
if(kbuf[0] == '1')
{
(*vir_odr_e) |= (0x1 << 10);
(*vir_odr_f) &= (~(0x1 << 10));
(*vir_odr_e) &= (~(0x1 << 8));
if(stop_led_sequence){return -1;}
msleep(1000);
(*vir_odr_f) |= (0x1 << 10);
(*vir_odr_e) &= (~(0x1 << 10));
(*vir_odr_e) &= (~(0x1 << 8));
if(stop_led_sequence){return -1;}
msleep(1000);
(*vir_odr_e) |= (0x1 << 8);
(*vir_odr_f) &= (~(0x1 << 10));
(*vir_odr_e) &= (~(0x1 << 10));
if(stop_led_sequence){return -1;}
msleep(1000);
}
else if(kbuf[0] == '0')
{
stop_led_sequence = 1;
(*vir_odr_e) &= (~(0x1 << 10));
(*vir_odr_f) &= (~(0x1 << 10));
(*vir_odr_e) &= (~(0x1 << 8));
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops=
{
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
/**********字符设备驱动注册**********/
major = register_chrdev(0,"mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
}
printk("字符设备驱动注册成功 major=%d\n",major);
/**********物理内存映射**********/
vir_moder_e = ioremap(PHY_GPIOE_MODER,4);
if(vir_moder_e == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_odr_e = ioremap(PHY_GPIOE_ODR,4);
if(vir_odr_e == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_moder_f = ioremap(PHY_GPIOF_MODER,4);
if(vir_moder_f == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_odr_f = ioremap(PHY_GPIOF_ODR,4);
if(vir_odr_f == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_rcc = ioremap(PHY_RCC_GPIOE,4);
if(vir_rcc == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
printk("物理内存映射成功\n");
/**********led1初始化***********/
(*vir_rcc) |= (0x1 << 4);
(*vir_moder_e) &= (~(0x3 << 20));
(*vir_moder_e) |= (0x1 << 20);
(*vir_odr_e) &= (~(0x1 << 10));
/********* led2初始化**********/
(*vir_rcc) |= (0x1 << 5);
(*vir_moder_f) &= (~(0x3 << 20));
(*vir_moder_f) |= (0x1 << 20);
(*vir_odr_f) &= (~(0x1 << 10));
/********* led3初始化**********/
(*vir_moder_e) &= (~(0x3 << 16));
(*vir_moder_e) |= (0x1 << 16);
(*vir_odr_e) &= (~(0x1 << 8));
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
led.h
#ifndef __LED_H__
#define __LED_H__
#define PHY_RCC_GPIOE 0X50000A28
#define PHY_GPIOE_MODER 0X50006000
#define PHY_GPIOE_ODR 0X50006014
#define PHY_GPIOF_MODER 0X50007000
#define PHY_GPIOF_ODR 0X50007014
#endif
fan.h
#ifndef __FAN_H__
#define __FAN_H__
#define PHY_GPIOE_MODER 0X50006000
#define PHY_GPIOE_ODR 0X50006014
#define PHY_RCC_GPIOE 0X50000A28
#endif
fan.c
#include
#include
#include
#include
#include
#include "head/fan.h"
unsigned int major;
char kbuf[128] = {};
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char __user *ubuf,size_t size,loff_t *loff_t)
{
unsigned long ret = copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filad\n");
return -ret;
}
return ret;
}
ssize_t mycdev_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loff_t)
{
unsigned long ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filad\n");
return -ret;
}
if(kbuf[1] == '1')
{
(*vir_odr) |= (0x1 << 9);
}
else if(kbuf[1] == '0')
{
(*vir_odr) &= (~(0x1 << 9));
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops=
{
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
/**********字符设备驱动注册**********/
major = register_chrdev(0,"mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
}
printk("字符设备驱动注册成功 major=%d\n",major);
/**********物理内存映射**********/
vir_moder = ioremap(PHY_GPIOE_MODER,4);
if(vir_moder == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_odr = ioremap(PHY_GPIOE_ODR,4);
if(vir_odr == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_rcc = ioremap(PHY_RCC_GPIOE,4);
if(vir_rcc == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
/**********fan初始化**********/
//RCC外设时钟使能
(*vir_rcc) |= (0x1 << 4);
//PE9设置为输出模式
(*vir_moder) &= (~(0x3 << 18));
(*vir_moder) |= (0x1 << 18);
//PE9默认输出低电平
(*vir_odr) &= (~(0x1 << 9));
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
beep.h
#ifndef __BEEP_H__
#define __BEEP_H__
#define PHY_GPIOB_MODER 0X50003000
#define PHY_GPIOB_ODR 0X50003014
#define PHY_RCC_GPIOB 0X50000A28
#endif
beep.c
#include
#include
#include
#include
#include
#include "head/beep.h"
unsigned int major;
char kbuf[128] = {};
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char __user *ubuf,size_t size,loff_t *loff_t)
{
unsigned long ret = copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filad\n");
return -ret;
}
return ret;
}
ssize_t mycdev_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loff_t)
{
unsigned long ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filad\n");
return -ret;
}
if(kbuf[2] == '1')
{
(*vir_odr) |= (0x1 << 6);
}
else if(kbuf[2] == '0')
{
(*vir_odr) &= (~(0x1 << 6));
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops=
{
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
major = register_chrdev(0,"mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
}
printk("字符设备驱动注册成功 major=%d\n",major);
/**********物理内存映射**********/
vir_moder = ioremap(PHY_GPIOB_MODER,4);
if(vir_moder == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_odr = ioremap(PHY_GPIOB_ODR,4);
if(vir_odr == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
vir_rcc = ioremap(PHY_RCC_GPIOB,4);
if(vir_rcc == NULL)
{
printk("物理内存映射失败%d\n",__LINE__);
}
/**********fan初始化**********/
//RCC外设时钟使能
(*vir_rcc) |= (0x1 << 1);
//PB6设置为输出模式
(*vir_moder) &= (~(0x3 << 12));
(*vir_moder) |= (0x1 << 12);
//PB6默认输出低电平
(*vir_odr) &= (~(0x1 << 6));
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用程序
app.c
#include
#include
#include
#include
#include
#include
#include
#include
char buf[128] = {0};
void cdev_ctl(void)
{
printf("请输入控制命令: 1 开 0 关>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
}
void *led_task(void *arg)
{
int fd = open("/dev/led",O_RDWR);
if(fd < 0)
{
printf("设备文件打开失败\n");
return NULL;
}
while (1)
{
write(fd, buf , sizeof(buf));
}
close(fd);
}
void *fan_task(void *arg)
{
int fd = open("/dev/fan",O_RDWR);
if(fd < 0)
{
printf("设备文件打开失败\n");
return NULL;
}
while (1)
{
write(fd, buf , sizeof(buf));
}
close(fd);
}
void *beep_task(void *arg)
{
int fd = open("/dev/beep",O_RDWR);
if(fd < 0)
{
printf("设备文件打开失败\n");
return NULL;
}
while (1)
{
write(fd, buf , sizeof(buf));
}
close(fd);
}
int main(int argc, char const *argv[])
{
pthread_t led_tid, fan_tid, beep_tid;
if(pthread_create(&led_tid, NULL, led_task, NULL) != 0)
{
printf("led线程创建失败\n");
return -1;
}
if(pthread_create(&fan_tid, NULL, fan_task, NULL) != 0)
{
printf("fan线程创建失败\n");
return -1;
}
if(pthread_create(&beep_tid, NULL, beep_task, NULL) != 0)
{
printf("beep线程创建失败\n");
return -1;
}
while (1)
{
cdev_ctl();
}
pthread_detach(led_tid);
pthread_detach(fan_tid);
pthread_detach(beep_tid);
return 0;
}