注意参考内核例程
一、驱动
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <asm/io.h>
#include <linux/device.h>
static int major;
static struct class *led_class;
static volatile unsigned int *RCC_PLL4CR;
static volatile unsigned int *RCC_MP_AHB4ENSETR;
static volatile unsigned int *GPIOA_MODER;
static volatile unsigned int *GPIOA_ODR;
static volatile unsigned int *GPIOA_BSRR;
static ssize_t led_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
printk("%s %s %d\n",__FILE__, __FUNCTION__,__LINE__);
char val;
copy_from_user(&val, buf, 1);
if(val)
{
*GPIOA_BSRR = (1<<26);
}
else
{
*GPIOA_BSRR = (1<<10);
}
return 1;
}
static int led_open(struct inode *inode, struct file *filp)
{
*RCC_PLL4CR |= (1<<0);
while((*RCC_PLL4CR & (1<<1)) == 0);
*RCC_MP_AHB4ENSETR |= (1<<0);
*GPIOA_MODER &= ~(3<<20);
*GPIOA_MODER |= (0x01<<20);
return 0;
}
static const struct file_operations led_fops = {
.owner = THIS_MODULE,
.write = led_write,
.open = led_open,
};
static int __init led_init(void)
{
major = register_chrdev(0, "tanzhenwen_led", &led_fops);
if (major < 0) {
printk("could not get major number\n");
class_destroy(led_class);
return major;
}
led_class = class_create(THIS_MODULE, "led_calss");
device_create(led_class, NULL, MKDEV(major, 0), NULL, "led_drv");
RCC_PLL4CR = ioremap(0x50000000+0x894, 4);
RCC_MP_AHB4ENSETR = ioremap(0x50000000 + 0xA28, 4);
GPIOA_MODER = ioremap(0x50002000 + 0x00, 4);
GPIOA_ODR = ioremap(0x50002000 + 0x14, 4);;
GPIOA_BSRR = ioremap(0x50002000 + 0x18, 4);
return 0;
}
static void __exit led_exit(void)
{
iounmap(RCC_PLL4CR);
iounmap(RCC_MP_AHB4ENSETR);
iounmap(GPIOA_MODER);
iounmap(GPIOA_ODR);
iounmap(GPIOA_BSRR);
unregister_chrdev(major, "tanzhenwen_led");
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
二、测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int fd;
char status = 0;
if(argc != 3 )
{
printf("Usage: %s <dev> <on|off>\n",argv[0]);
printf(" eg: %s /dev/led_drv on\n");
printf(" eg: %s /dev/led_drv off\n");
}
fd = open(argv[1],O_RDWR);
if(fd < 0)
{
printf("can not open %s\n",argv[1]);
return -1;
}
if(strcmp(argv[2],"on") == 0)
{
status = 1;
}
else
{
status = 0;
}
write(fd,&status,1);
return 0;
}
三、测试
让心跳灯不闪烁
echo none > /sys/class/leds/heartbeat/trigger
cat /proc/devices可以看见tanzhenwen_led字符设备节点 执行ls /dev/led_drv -al,可以看见具有led_drv的设备。
执行./led_test /dev/led_drv off以及./led_test /dev/led_drv on可以观察到LED灯的亮灭。
|