天天看點

Linux驅動開發 - LED驅動

驅動程式

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define LED_MAJOR 200  /* 主裝置号 */
#define LED_NAME "led" /* 裝置名字 */

#define LEDOFF 0 /* 關燈 */
#define LEDON 1	 /* 開燈 */

/* 寄存器實體位址 */
#define CCM_CCGR1_BASE (0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIR_BASE (0X0209C004)

/* 映射後的寄存器虛拟位址指針 */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

/*
 * @description		: LED打開/關閉
 * @param - sta 	: LEDON(0) 打開LED,LEDOFF(1) 關閉LED
 * @return 			: 無
 */
void led_switch(u8 sta)
{
	u32 val = 0;
	if (sta == LEDON)
	{
		val = readl(GPIO1_DR);
		val &= ~(1 << 3);
		writel(val, GPIO1_DR);
	}
	else if (sta == LEDOFF)
	{
		val = readl(GPIO1_DR);
		val |= (1 << 3);
		writel(val, GPIO1_DR);
	}
}

/*
 * @description		: 打開裝置
 * @param - inode 	: 傳遞給驅動的inode
 * @param - filp 	: 裝置檔案,file結構體有個叫做private_data的成員變量
 * 					  一般在open的時候将private_data指向裝置結構體。
 * @return 			: 0 成功;其他 失敗
 */
static int led_open(struct inode *inode, struct file *filp)
{
	return 0;
}

/*
 * @description		: 從裝置讀取資料
 * @param - filp 	: 要打開的裝置檔案(檔案描述符)
 * @param - buf 	: 傳回給使用者空間的資料緩沖區
 * @param - cnt 	: 要讀取的資料長度
 * @param - offt 	: 相對于檔案首位址的偏移
 * @return 			: 讀取的位元組數,如果為負值,表示讀取失敗
 */
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	return 0;
}

/*
 * @description		: 向裝置寫資料
 * @param - filp 	: 裝置檔案,表示打開的檔案描述符
 * @param - buf 	: 要寫給裝置寫入的資料
 * @param - cnt 	: 要寫入的資料長度
 * @param - offt 	: 相對于檔案首位址的偏移
 * @return 			: 寫入的位元組數,如果為負值,表示寫入失敗
 */
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;

	retvalue = copy_from_user(databuf, buf, cnt);
	if (retvalue < 0)
	{
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0]; /* 擷取狀态值 */

	if (ledstat == LEDON)
	{
		led_switch(LEDON); /* 打開LED燈 */
	}
	else if (ledstat == LEDOFF)
	{
		led_switch(LEDOFF); /* 關閉LED燈 */
	}
	return 0;
}

/*
 * @description		: 關閉/釋放裝置
 * @param - filp 	: 要關閉的裝置檔案(檔案描述符)
 * @return 			: 0 成功;其他 失敗
 */
static int led_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/* 裝置操作函數 */
static struct file_operations led_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.read = led_read,
	.write = led_write,
	.release = led_release,
};

/*
 * @description	: 驅動出口函數
 * @param 		: 無
 * @return 		: 無
 */
static int __init led_init(void)
{
	int retvalue = 0;
	u32 val = 0;

	/* 初始化LED */
	/* 1、寄存器位址映射 */
	IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
	SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
	SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);
	GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
	GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);

	/* 2、使能GPIO1時鐘 */
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 26); /* 清楚以前的設定 */
	val |= (3 << 26);  /* 設定新值 */
	writel(val, IMX6U_CCM_CCGR1);

	/* 3、設定GPIO1_IO03的複用功能,将其複用為
	 *    GPIO1_IO03,最後設定IO屬性。
	 */
	writel(5, SW_MUX_GPIO1_IO03);

	/*寄存器SW_PAD_GPIO1_IO03設定IO屬性
	 *bit 16:0 HYS關閉
	 *bit [15:14]: 00 預設下拉
	 *bit [13]: 0 kepper功能
	 *bit [12]: 1 pull/keeper使能
	 *bit [11]: 0 關閉開路輸出
	 *bit [7:6]: 10 速度100Mhz
	 *bit [5:3]: 110 R0/6驅動能力
	 *bit [0]: 0 低轉換率
	 */
	writel(0x10B0, SW_PAD_GPIO1_IO03);

	/* 4、設定GPIO1_IO03為輸出功能 */
	val = readl(GPIO1_GDIR);
	val &= ~(1 << 3); /* 清除以前的設定 */
	val |= (1 << 3);  /* 設定為輸出 */
	writel(val, GPIO1_GDIR);

	/* 5、預設關閉LED */
	val = readl(GPIO1_DR);
	val |= (1 << 3);
	writel(val, GPIO1_DR);

	/* 6、注冊字元裝置驅動 */
	retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
	if (retvalue < 0)
	{
		printk("register chrdev failed!\r\n");
		return -EIO;
	}
	return 0;
}

/*
 * @description	: 驅動出口函數
 * @param 		: 無
 * @return 		: 無
 */
static void __exit led_exit(void)
{
	/* 取消映射 */
	iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO1_IO03);
	iounmap(SW_PAD_GPIO1_IO03);
	iounmap(GPIO1_DR);
	iounmap(GPIO1_GDIR);

	/* 登出字元裝置驅動 */
	unregister_chrdev(LED_MAJOR, LED_NAME);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lk");

           

函數說明

  • led_switch

    • 這個函數的功能是實際去調用readl和writel函數
    • readl–>從記憶體映射的 I/O 空間讀取資料,readl 從 I/O 讀取 32 位資料 ( 4 位元組 )
    • writel–>往記憶體映射的 I/O 空間上寫資料,wirtel I/O 上寫入 32 位資料 (4位元組)
      //讀操作函數一共有三個
      u8 readb(const volatile void __iomem *addr)
      u16 readw(const volatile void __iomem *addr)
      u32 readl(const volatile void __iomem *addr)
      //readb、 readw 和 readl 這三個函數分别對應 8bit、 16bit 和 32bit 讀操作,參數 addr 就是要讀取寫記憶體位址,傳回值就是讀取到的資料。
      //寫操作函數一共有三個
      void writeb(u8 value, volatile void __iomem *addr)
      void writew(u16 value, volatile void __iomem *addr)
      void writel(u32 value, volatile void __iomem *addr)
      //writeb、 writew 和 writel 這三個函數分别對應 8bit、 16bit 和 32bit 寫操作,參數 value 是要寫入的數值, addr 是要寫入的位址。
                 
  • led_open

    • 打開裝置(對于驅動來說傳遞給驅動的inode)
  • led_read

    • 從裝置讀取資料(這裡LED隻涉及下發資料)
  • led_write

    • 向裝置寫資料(這裡就是接收到應用程式寫入的指令之後,将值傳給

      led_switch

  • led_release

    • 關閉/釋放裝置
  • led_fops

    • 相當于映射函數吧
  • led_init

    • 先對寄存器位址映射(根據原理圖及資料手冊确定I/O和位址)
    • 使能GPIO1時鐘(相關寄存器功能設定)
    • 設定GPIO1_IO03為輸出功能
    • 注冊字元裝置驅動(向核心注冊一個字元裝置)完成注冊後,在 /proc/devices 中的第一個字元裝置我們就看到了
  • led_exit

    • 取消映射
    • 登出字元裝置驅動

應用程式

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"

#define LEDOFF 	0
#define LEDON 	1

/*
 * @description		: main主程式
 * @param - argc 	: argv數組元素個數
 * @param - argv 	: 具體參數
 * @return 			: 0 成功;其他 失敗
 */
int main(int argc, char *argv[])
{
	int fd, retvalue;
	char *filename;
	unsigned char databuf[1];
	
	if(argc != 3){
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	/* 打開led驅動 */
	fd = open(filename, O_RDWR);
	if(fd < 0){
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

	databuf[0] = atoi(argv[2]);	/* 要執行的操作:打開或關閉 */

	/* 向/dev/led檔案寫入資料 */
	retvalue = write(fd, databuf, sizeof(databuf));
	if(retvalue < 0){
		printf("LED Control Failed!\r\n");
		close(fd);
		return -1;
	}

	retvalue = close(fd); /* 關閉檔案 */
	if(retvalue < 0){
		printf("file %s close failed!\r\n", argv[1]);
		return -1;
	}
	return 0;
}
           

對 led 的驅動檔案進行最基本的打開、關閉、寫操作

運作效果

led.ko和 ledApp這兩個檔案拷貝到 /lib/modules/4.1.15-g0423506 目錄中

insmod led.ko //加載驅動

建立“/dev/led”裝置節點

mknod /dev/led c 200 0

./ledApp /dev/led 1 // 打開LED

./ledApp /dev/led 0 // 關閉LED

繼續閱讀