第二篇:初步开发LED程序(树莓派Pi 3B)
1、驱动环境准备
2、编写驱动rasp_led.c代码
3、编写Makefile
4、编译内核文件
5、编写应用程序
1、驱动环境准备
[参考本博客可以实现驱动环境搭建](https://blog.csdn.net/weixin_38165614/article/details/107317616)
2、编写驱动rasp_led.c代码
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#define PIN 18
#define BCM2835_GPIO_BASE 0x3f200000
//32 32 4Bit function register
#define BCM2835_GPIOReg_GPFSEL1 0x00000004
//gpio set 1 output register
#define BCM2835_GPIOReg_GPSET0 0x0000001c
//gpio set 0 output register
#define BCM2835_GPIOReg_GPCLR0 0x00000028
#define DEVICE_NAME "led"
#define LED_MAJOR 231
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
static volatile unsigned long *gpcon;
static volatile unsigned long *gpset;
static volatile unsigned long *gpclr;
static int pi_leds_open(struct inode *inode, struct file *file)
{
//config your led pin function mode
gpcon = (volatile unsigned long *)ioremap(0x3f200004,4);
gpset = (volatile unsigned long *)ioremap(0x3f20001C,4);
gpclr = (volatile unsigned long *)ioremap(0x3f200028,4);
return 0;
}
static int pi_leds_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
if (arg > 3) {
return -EINVAL;
}
switch(cmd) {
case IOCTL_LED_ON:
// set your led 0
*gpcon = *gpcon | (1<<24); //set pin18 to output function mode
*gpset = *gpset | (1<<18); //set pin18 output high
return 0;
case IOCTL_LED_OFF:
// set your led 1
*gpcon = *gpcon | (1<<24); //set pin18 to output function mode
*gpclr = *gpclr | (1<<18); //set pin18 output low
return 0;
default:
return -EINVAL;
}
}
static struct file_operations pi_leds_fops = {
.owner = THIS_MODULE,
.open = pi_leds_open,
.unlocked_ioctl = pi_leds_ioctl,
};
static int __init pi_leds_init(void)
{
int ret;
ret = register_chrdev(LED_MAJOR, "led", &pi_leds_fops);
if (ret < 0) {
printk("led can't register major number\n");
return ret;
}
printk("led is initialized \n");
printk("led device number is : %d \n",ret);
return 0;
}
static void __exit pi_leds_exit(void)
{
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
printk("led is goodbye\n");
}
module_init(pi_leds_init);
module_exit(pi_leds_exit);
MODULE_LICENSE("GPL");
3、编写Makefile
ifneq ($(KERNELRELEASE),)
obj-m := rasp_led.o
else
PWD :=$(shell pwd)
KDIR := /home/ysl/work/rastools/linux-rpi-4.14.y
all:
make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*
endif
4、编译内核文件
编译驱动文件
make
生成rasp_led.ko 模块,把内核模块插入系统
sudo insmod rasp_led.ko
sudo mknod /dev/led c 231 0
sudo chmod 777 /dev/led
查看调试信息
dmesg
输出
...........
led is initialized
.......
内核模块已经生成,可以使用。
5、编写应用程序
树莓派安装GCC后编译应用程序test_led.c
apt install gcc
vim test_led.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<linux/ioctl.h>
int main(int argc, char **argv)
{
int dev_fd;
int on;
if (argc != 2 || sscanf(argv[1],"%d", &on) != 1 ||on < 0 || on > 1 ) {
fprintf(stderr, "Usage:%s 0|1\n",argv[0]);
exit(1);
}
dev_fd = open("/dev/led",O_RDWR);
if( dev_fd == -1){
printf("Cann't open file \n");
return 1;
}
/*通过ioctl来控制灯的亮、灭*/
if(on){
printf("turn on leds!\n");
ioctl(dev_fd, 1);
}
else {
printf("turn off leds!\n");
ioctl(dev_fd, 0);
}
close(dev_fd);
return 0;
}
保存文件 ,生成可执行文件
gcc test_led.c -o test_led
chmod 777 test_led
./test_led
至此LED驱动已经编写完成
如果对你有帮助,请给予作者一丝丝奖励