Author:杨正date:2016.9.21
目的
在u-boot中添加驱动程序。
详细举例介绍
在uboot中操作寄存器,实现对gpio及外围设备的控制有两种方法,一种是直接在arch/arm/lib/board.c中添加对寄存器的操作代码,如:
#define muxctrl_reg5 0x200f0014
#define GPIO6_DIR 0x201a0400
#define GPIO6_1_DATA 0x201a0008
#define GPIO6_1 (1 << 1)
#define readl(addr) (*(volatile unsigned int*)(addr))
#define writel(val, addr) ((*(volatile unsigned int *) (addr)) = (val))
int clear_irled(void)
{
unsigned int reg_val;
reg_val = writel(0, muxctrl_reg5); // set gpio mode
reg_val = readl(GPIO6_DIR);
reg_val |= GPIO6_1;
writel(reg_val, GPIO6_DIR);
reg_val = readl(GPIO6_1_DATA);
reg_val &= ~GPIO6_1;
writel(reg_val, GPIO6_1_DATA);
return 0;
}
void start_armboot (void)
{
init_fnc_t **init_fnc_ptr;
char *s;
#ifdef CONFIG_HAS_SLAVE
char *e;
#endif
#if defined(CONFIG_VFD) || defined(CONFIG_LCD)
unsigned long addr;
#endif
#ifdef CONFIG_HI3516A // defined in the include/configs/hi3516a.h
clear_irled(); // clear ir led, add by yangzheng 2016.9.21
#endif
另一种方法:
1、在driver/下新建hi_gpio目录,如:
[yangzheng@centos6 hi_gpio]$ ls
hi_gpio.c Makefile
hi_gpio.c内容如下:
[yangzheng@centos6 hi_gpio]$ cat hi_gpio.c
/*********************************************************************************
* Copyright: (C) 2016 Yang Zheng
* All rights reserved.
*
* Filename: hi_gpio.c
* Description: This file
*
* Version: 1.0.0(09/21/2016~)
* Author: Yang Zheng
* ChangeLog: 1, Release initial version on "09/21/2016 05:41:41 PM"
*
********************************************************************************/
#include
#define readl(addr) (*(volatile unsigned int *) (addr))
#define writel(val, addr) (*(volatile unsigned int *) (addr) = (val))
#define muxctrl_reg5 0x200f0014
#define GPIO6_DIR 0x201a0400
#define GPIO6_1_DATA 0x201a0008
#define GPIO6_1 1 << 1
#define REG_SET 1
#define REG_CLR 0
#ifdef DEBUG
#define DPRINTF(args...) printf(args)
#else
#define DPRINTF(args...)
#endif
int clear_irled(void)
{
unsigned int reg_val;
reg_val = writel(REG_CLR, muxctrl_reg5); // set gpio mode
reg_val = readl(GPIO6_DIR);
reg_val |= GPIO6_1;
writel(reg_val, GPIO6_DIR);
writel(REG_CLR, GPIO6_1_DATA);
DPRINTF("clear ir led...\n");
return 0;
}
Makefile如下(可以拷贝driver目录下的各模块模板):
[yangzheng@centos6 hi_gpio]$ cat Makefile
#
# Copyright 2000-2008
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
include $(TOPDIR)/config.mk
LIB := $(obj)libhi_gpio.a
COBJS-$(CONFIG_HI3516A_GPIO) += hi_gpio.o
COBJS := $(COBJS-y)
SRCS := $(COBJS:.o=.c)
OBJS := $(addprefix $(obj),$(COBJS))
all: $(LIB)
$(LIB): $(obj).depend $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)
#########################################################################
# defines $(obj).depend target
include $(SRCTREE)/rules.mk
sinclude $(obj).depend
########################################################################
2、在顶层Makefile添加如下代码:
LIBS += drivers/hi_gpio/libhi_gpio.a
3、在include/configs/hi3516a.h中添加如下代码:
#define CONFIG_HI3516A_GPIO
在include下增加hi_gpio.h文件,内容如下:
[yangzheng@centos6 u-boot-2010.06]$ cat include/hi_gpio.h
/********************************************************************************
* Copyright: (C) 2016 Yang Zheng
* All rights reserved.
*
* Filename: hi_gpio.h
* Description: This head file is control hisi gpio
*
* Version: 1.0.0(09/21/2016~)
* Author: Yang Zheng
* ChangeLog: 1, Release initial version on "09/21/2016 06:09:49 PM"
*
********************************************************************************/
#ifndef __HI_GPIO_H__
#define __HI_GPIO_H__
extern int clear_irled(void);
#endif
4、在arch/arm/lib/board.c 里面调用即可,如:
[yangzheng@centos6 u-boot-2010.06]$ vim arch/arm/lib/board.c
void start_armboot (void)
{
init_fnc_t **init_fnc_ptr;
char *s;
#ifdef CONFIG_HAS_SLAVE
char *e;
#endif
#if defined(CONFIG_VFD) || defined(CONFIG_LCD)
unsigned long addr;
#endif
#ifdef CONFIG_HI3516A_GPIO
clear_irled(); // clear ir led, add by yangzheng 2016.9.21
#endif
……
重新编译即可,调试uboot的方法:
如果设备有网口,可用tftp服务下载:
sf probe 0
mw.b 82000000 ff 0x80000
tftp 82000000 u-boot.bin
go 82000000
如果没有网口,可用串口下载:
sf probe 0
mw.b 82000000 ff 0x80000
loady 82000000 u-boot.bin
go 82000000