天天看點

RK3288 LED驅動編寫開始編寫LED驅動程式

開始編寫LED驅動程式

  • 我之前學習了rk3288 的led驅動編寫的準備
  • 然後也編寫了led驅動的架構。
  • 現在隻要把架構适當的填充具體的硬體操作,就可以實作led的電燈了。

1.需要用到的函數ioremap

因為linux核心并不像單片機一樣是直接用寄存器位址控制的,作業系統有記憶體尋址的架構。是以我們需要把實體位址轉換成Linux可以操作的位址。這個轉換的過程就需要調用ioremap函數,下面的它的函數原型:

#include <asm/io.h>				//需要使用時包含頭檔案
void __iomen *ioremap(resource_size_t res_cookie, size_t size);
void iounmap(volatile void __iomen *cookie);

/* virtual_addr = ioremap(physical_addr, size);
 * 把physical_addr開始的size映射為虛拟位址,傳回值時該段虛拟位址的首位址。
 * physical_addr和size都會按頁取整
 */
           

2.firefly3288 led原理圖

RK3288 LED驅動編寫開始編寫LED驅動程式

WORK_LED使用GPIO8_A1,POWER_LED使用GPIO8_A2。

這兩個LED引腳在高電平時熄滅,低電平時點亮。

3.Led初始化步驟

  1. 使能GPIO對應的CRU控制器
    RK3288 LED驅動編寫開始編寫LED驅動程式
    RK3288 LED驅動編寫開始編寫LED驅動程式
    CRU_CLKGATE14_CON的b[8]位為0時使能,這裡要注意31:16為時掩碼位,必須把b[24]位設定為1。才能避免被屏蔽。
/* rk3288 GPIO8_A1 */
/* a. 使能 GPIO8
* set CRU to enable GPIO8
* CRU_CLKGATE14_CON 0xFF760000 + 0x198
* (1<<(8+16)) | (0<<8)
*/
           
  1. 設定GPIO可寫,并且引腳用作GPIO功能
    RK3288 LED驅動編寫開始編寫LED驅動程式
    RK3288 LED驅動編寫開始編寫LED驅動程式
    設定GRF_GPIO8A_IOMUX的b[3:2]和b[4]為0,把GPIO_A1和GPIO_A2作為GPIO。同時b[19:18]和b[21:20]設定為0b11。
/* b. 設定 GPIO8_A1 用于 GPIO
* set PMU/GRF to configure GPIO8_A1 as GPIO
* GRF_GPIO8A_IOMUX 0xFF770000 + 0x0080
* bit[3:2] = 0b00
* (3<<(2+16)) | (0<<2)
*/
           
  1. 把對應的GPIO設定為輸出
    RK3288 LED驅動編寫開始編寫LED驅動程式
    設定 GPIO_SWPORTA_DDR 寄存器 b[1]為 1 b[2]為1,把 GPIO8_A1 和GPIO8_A2設定為輸出引腳。
/* c. 設定 GPIO8_A1 作為 output 引腳
* set GPIO_SWPORTA_DDR to configure GPIO8_A1 as output
* GPIO_SWPORTA_DDR 0xFF7F0000 + 0x0004
* bit[1] = 0b1
*/
           
  1. 設定GPIO預設的電平為高電平
    RK3288 LED驅動編寫開始編寫LED驅動程式
    設定 GPIO_SWPORTA_DR 寄存器 b[1]為 1,讓 GPIO8_A1 輸出高電平。
/* d. 設定 GPIO8_A1 輸出高電平
* set GPIO_SWPORTA_DR to configure GPIO8_A1 output 1
* GPIO_SWPORTA_DR 0xFF7F0000 + 0x0000
* bit[1] = 0b1
*/
/* e. 設定 GPIO8_A1 輸出低電平
* set GPIO_SWPORTA_DR to configure GPIO8_A1 output 0
* GPIO_SWPORTA_DR 0xFF7F0000 + 0x0000
* bit[1] = 0b0
*/
           

Led操作步驟

和上面第四步時一樣的。

源碼

Makefile

KERN_DIR = /home/book/Documents/100ask_firefly-rk3288/linux-4.4

all:
	make -C $(KERN_DIR) M=`pwd` modules
	$(CROSS_COMPILE)gcc -o led_app led_app.c  -Wno-error=unused-but-set-variable

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f hello_drv_test


myled_drv-y:=led_drv.o board_demoo.o
obj-m += myled_drv.o
           

led_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>

#include "led_ops.h"

static int major = 0;
static struct class *led_class;
struct led_operations *p_led_ops;

static int led_drv_open(struct inode *node, struct file *file)
{
	int minor = iminor(node);
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	p_led_ops->init(minor);	
	return 0;
}

static ssize_t led_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	char status;
	struct inode *inode = file_inode(file);
	int minor = iminor(inode);

	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(&status, buf, 1);
	printk("write status:%x \n", status);

	p_led_ops->ctl(minor, status);
	
	return 1;
}

static int led_drv_close(struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}


static struct file_operations led_drv = {
	.owner = THIS_MODULE,
	.open = led_drv_open,
	.read = led_drv_read,
	.write = led_drv_write,
	.release = led_drv_close,
};

static int __init led_drv_init(void)
{
	int err;
	int i = 0;
	major = register_chrdev(0, "myled", &led_drv);

	led_class = class_create(THIS_MODULE, "led_class");
	err = PTR_ERR(led_class);
	if(IS_ERR(led_class)) {
		unregister_chrdev(major, "myled");	
		printk(KERN_WARNING "class creatge failed %d\n", err);
		return -1;
	}


	p_led_ops = get_board_led_ops();
	for(i=0;i<p_led_ops->num;i++)
		device_create(led_class, NULL, MKDEV(major, i), NULL, "myled%d", i);
	
	printk("%s %sled_drv_init\n", __FILE__, __FUNCTION__);
	return 0;
}

static void __exit led_drv_exit(void)
{
	int i;
	printk("%s %sled_drv_exit\n", __FILE__, __FUNCTION__);
	for(i=0;i<p_led_ops->num;i++)
		device_destroy(led_class, MKDEV(major, i));

	class_destroy(led_class); 
	unregister_chrdev(major, "myled");	
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chen");
           

board_demoo.c

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/io.h>

#include "led_ops.h"

#define CRU_BASE_PHY_ADDRESS   ((unsigned long)(0xff760000))
#define GRF_BASE_PHY_ADDRESS   ((unsigned long)(0xff770000))
#define GPIO8_BASE_PHY_ADDRESS ((unsigned long)(0xff7f0000))


#define CRU_CLKGATE14_PHY_CON (0x0198)
#define GRF_GPIO8A_PHY_IOMUX  (0x0080)
#define GPIO_SWPORTA_PHY_DR   (0x0000)
#define GPIO_SWPORTA_PHY_DDR  (0x0004)

static volatile unsigned int *CRU_CLKGATE14_CON;
static volatile unsigned int *GRF_GPIO8A_IOMUX;
static volatile unsigned int *GPIO8_SWPORTA_DDR;
static volatile unsigned int *GPIO8_SWPORTA_DR;

static int board_demoo_led_init(int which)
{
	printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);

	if(!CRU_CLKGATE14_CON) {
		CRU_CLKGATE14_CON = ioremap(CRU_BASE_PHY_ADDRESS + CRU_CLKGATE14_PHY_CON, 4);
		GRF_GPIO8A_IOMUX  = ioremap(GRF_BASE_PHY_ADDRESS + GRF_GPIO8A_PHY_IOMUX, 4);
		GPIO8_SWPORTA_DDR = ioremap(GPIO8_BASE_PHY_ADDRESS + GPIO_SWPORTA_PHY_DDR, 4);
		GPIO8_SWPORTA_DR  = ioremap(GPIO8_BASE_PHY_ADDRESS + GPIO_SWPORTA_PHY_DR, 4);
	}

	if(which == 0) {
		*CRU_CLKGATE14_CON = (1<<(8+16)) | (0<<8);
		*GRF_GPIO8A_IOMUX  |= (3<<(2+16)) | (0<<2);
		*GPIO8_SWPORTA_DDR |= (1<<1);
	} else if(which == 1) {
		*CRU_CLKGATE14_CON = (1<<(8+16)) | (0<<8);
		*GRF_GPIO8A_IOMUX  |= (3<<(4+16)) | (0<<4);
		*GPIO8_SWPORTA_DDR |= (1<<2);
	}
	return 0;
}

static int board_demoo_led_ctl(int which, char status)
{
	printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status?"on":"off");
	if(which == 0) {
		if(status) {		/* on: output 0 */
			*GPIO8_SWPORTA_DR &= ~(1<<1);
		} else {			/* off: output 1 */
			*GPIO8_SWPORTA_DR |= (1<<1);
		}
	} else if(which == 1) {
		if(status) {
			*GPIO8_SWPORTA_DR &= ~(1<<2);
		} else {
			*GPIO8_SWPORTA_DR |= (1<<2);
		}
	}
	return 0;
}

static struct led_operations board_demoo_led_ops = {
	.num = 2,
	.init = board_demoo_led_init,
	.ctl  = board_demoo_led_ctl,
};

struct led_operations *get_board_led_ops(void)
{
	return &board_demoo_led_ops;
}
           

led_ops.h

#ifndef __LED_OPS_H_
#define __LED_OPS_H_

struct led_operations {
	int num;
	int (*init)(int which);			/* init led, which:led num  */
	int (*ctl)(int which, char status);		/* control led, whic:led num,status:0-On 1-Off */
};

struct led_operations *get_board_led_ops(void);

#endif
           

led_app.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

void showUsage(void)
{
	printf("app [dev_path] [on,off]\n");
}

int main(int argc, char *argv[])
{
	char status;
	if(argc < 3) {
		showUsage();
		return -1;
	}

	int fd = open(argv[1], O_RDWR);
	if(fd < 0) {
		printf("app open device failed path:%s", argv[1]);
		return -1;
	}

	if(0 == strcmp(argv[2], "on")) {
		status = 1;
		int ret = write(fd, &status, 1);
		if(ret <= 0) {
			printf("app write device fialed %s",argv[2]);
			return -1;
		} else {
			printf("app write device %x", status);
		}

	} else if(0 == strcmp(argv[2], "off")) {
		status = 0;
		int ret = write(fd, &status, 1);
		if(ret <= 0) {
			printf("app write device fialed %s",argv[2]);
			return -1;
		} else {
			printf("app write device %x", status);
		}
	}
	
	return 0;
}
           

繼續閱讀