前面我们编写了hello world的程序,接下来继续研究GPIO功能,通过GPIO来控制LED的亮灭,这在单片机中应该算是十分简单的一个程序了,但是在Linux系统中控制GPIO没有那么简单,难点就在于GPIO地址的获取,也是我一直在纠结的问题。
一、GPIO地址
二、硬件平台
三、编写驱动代码
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include "bcm2835.h"
// Blinks on RPi Plug P1 pin 11 (which is GPIO pin 17)
#define PIN RPI_GPIO_P1_11
int open_state = 0; //文件打开状态
static int leds_open(struct inode *inode, struct file *filp)
{
if(open_state == 0)
{
open_state = 1;
printk("Open file suc!\n");
return 0;
}
else
{
printk("The file has opened!\n");
return -1;
}
}
static int leds_ioctl(struct file*filp, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case 0:
bcm2835_gpio_clr(PIN);
printk("LED OFF!\n");
break;
case 1:
bcm2835_gpio_set(PIN);
printk("LED ON!\n");
break;
default:
return-EINVAL;
}
return 0;
}
static int leds_release(struct inode *inode, struct file *filp)
{
if(open_state == 1)
{
open_state = 0;
printk("close file suc!\n");
return 0;
}
else
{
printk("The file has closed!\n");
return -1;
}
}
static const struct file_operations leds_fops = {
.owner = THIS_MODULE,
.open = leds_open,
.unlocked_ioctl = leds_ioctl,
.release = leds_release,
};
static struct miscdevice misc = {
.minor =MISC_DYNAMIC_MINOR,
.name ="my_leds",
.fops =&leds_fops,
};
static int __init leds_init(void)
{
int ret;
//注册混杂设备
ret =misc_register(&misc);
//配置功能选择寄存器为输出
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
//设置输出电平为高电平,LED亮
bcm2835_gpio_set(PIN);
printk("ledsinit.\n");
return ret;
}
static void leds_exit(void)
{
//LED灭
bcm2835_gpio_clr(PIN);
misc_deregister(&misc);
printk("leds_exit\n");
}
module_init(leds_init);
module_exit(leds_exit);
MODULE_AUTHOR("Hu Chunxu");
MODULE_LICENSE("GPL");
硬件相关操作:#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include "bcm2835.h"
int bcm2835_gpio_fsel(uint8_t pin, uint8_t mode)
{
//初始化GPIOB功能选择寄存器的物理地址
volatile uint32_t * bcm2835_gpio = (volatile uint32_t *)ioremap(BCM2835_GPIO_BASE, 16);
volatile uint32_t * bcm2835_gpio_fsel = bcm2835_gpio + BCM2835_GPFSEL0/4 + (pin/10);
uint8_t shift = (pin % 10) * 3;
uint32_t value = mode << shift;
*bcm2835_gpio_fsel = *bcm2835_gpio_fsel | value;
printk("fsel address: 0x%lx : %x\n", bcm2835_gpio_fsel, *bcm2835_gpio_fsel);
return 0;
}
int bcm2835_gpio_set(uint8_t pin)
{
//GPIO输出功能物理地址
volatile uint32_t * bcm2835_gpio = (volatile uint32_t *)ioremap(BCM2835_GPIO_BASE, 16);
volatile uint32_t * bcm2835_gpio_set = bcm2835_gpio + BCM2835_GPSET0/4 + pin/32;
uint8_t shift = pin % 32;
uint32_t value = 1 << shift;
*bcm2835_gpio_set = *bcm2835_gpio_set | value;
printk("set address: 0x%lx : %x\n", bcm2835_gpio_set, *bcm2835_gpio_set);
return 0;
}
int bcm2835_gpio_clr(uint8_t pin)
{
//GPIO清除功能物理地址
volatile uint32_t * bcm2835_gpio = (volatile uint32_t *)ioremap(BCM2835_GPIO_BASE, 16);
volatile uint32_t * bcm2835_gpio_clr = bcm2835_gpio + BCM2835_GPCLR0/4 + pin/32;
uint8_t shift = pin % 32;
uint32_t value = 1 << shift;
*bcm2835_gpio_clr = *bcm2835_gpio_clr | value;
printk("clr address: 0x%lx : %x\n", bcm2835_gpio_clr, *bcm2835_gpio_clr);
return 0;
}
应用测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int on;
int fd;
if (argc != 2 || sscanf(argv[1],"%d", &on) != 1 ||on < 0 || on > 1 ) {
fprintf(stderr, "Usage:%s 0|1\n",argv[0]);
exit(1);
}
fd = open("/dev/my_leds", 0);
if (fd < 0) {
perror("open device leds");
exit(1);
}
/*通过ioctl来控制灯的亮、灭*/
if(on){
printf("turn on leds!\n");
ioctl(fd, 1);
}
else {
printf("turn off leds!\n");
ioctl(fd, 0);
}
close(fd);
return 0;
}
分别编译,插入模块,然后运行测试程序,可以控制LED的亮灭了。
----------------------------------------------------------------
欢迎大家转载我的文章。
转载请注明:转自古-月
欢迎继续关注我的博客