如何在uboot中添加驱动程序

https://blog.csdn.net/yangzheng_yz/article/details/52817670

Author:杨正date:2016.9.21

目的

在u-boot中添加驱动程序。

 

详细举例介绍

在uboot中操作寄存器,实现对gpio及外围设备的控制有两种方法,一种是直接在arch/arm/lib/board.c中添加对寄存器的操作代码,如:

 
  1. #define muxctrl_reg5 0x200f0014

  2. #define GPIO6_DIR 0x201a0400

  3. #define GPIO6_1_DATA 0x201a0008

  4. #define GPIO6_1 (1 << 1)

  5. #define readl(addr) (*(volatile unsigned int*)(addr))

  6. #define writel(val, addr) ((*(volatile unsigned int *) (addr)) = (val))

  7.  
  8. int clear_irled(void)

  9. {

  10. unsigned int reg_val;

  11.  
  12. reg_val = writel(0, muxctrl_reg5); // set gpio mode

  13.  
  14. reg_val = readl(GPIO6_DIR);

  15. reg_val |= GPIO6_1;

  16. writel(reg_val, GPIO6_DIR);

  17.  
  18. reg_val = readl(GPIO6_1_DATA);

  19. reg_val &= ~GPIO6_1;

  20. writel(reg_val, GPIO6_1_DATA);

  21.  
  22. return 0;

  23. }

  24. void start_armboot (void)

  25. {

  26. init_fnc_t **init_fnc_ptr;

  27. char *s;

  28. #ifdef CONFIG_HAS_SLAVE

  29. char *e;

  30. #endif

  31. #if defined(CONFIG_VFD) || defined(CONFIG_LCD)

  32. unsigned long addr;

  33. #endif

  34.  
  35. #ifdef CONFIG_HI3516A // defined in the include/configs/hi3516a.h

  36. clear_irled(); // clear ir led, add by yangzheng 2016.9.21

  37. #endif

 

 

另一种方法

1、在driver/下新建hi_gpio目录,如:
[yangzheng@centos6 hi_gpio]$ ls

hi_gpio.c  Makefile


hi_gpio.c内容如下:

 
  1. [yangzheng@centos6 hi_gpio]$ cat hi_gpio.c

  2. /*********************************************************************************

  3. * Copyright: (C) 2016 Yang Zheng

  4. * All rights reserved.

  5. *

  6. * Filename: hi_gpio.c

  7. * Description: This file

  8. *

  9. * Version: 1.0.0(09/21/2016~)

  10. * Author: Yang Zheng

  11. * ChangeLog: 1, Release initial version on "09/21/2016 05:41:41 PM"

  12. *

  13. ********************************************************************************/

  14. #include

  15.  
  16. #define readl(addr) (*(volatile unsigned int *) (addr))

  17. #define writel(val, addr) (*(volatile unsigned int *) (addr) = (val))

  18.  
  19. #define muxctrl_reg5 0x200f0014

  20. #define GPIO6_DIR 0x201a0400

  21. #define GPIO6_1_DATA 0x201a0008

  22. #define GPIO6_1 1 << 1

  23. #define REG_SET 1

  24. #define REG_CLR 0

  25.  
  26. #ifdef DEBUG

  27. #define DPRINTF(args...) printf(args)

  28. #else

  29. #define DPRINTF(args...)

  30. #endif

  31.  
  32. int clear_irled(void)

  33. {

  34. unsigned int reg_val;

  35.  
  36. reg_val = writel(REG_CLR, muxctrl_reg5); // set gpio mode

  37.  
  38. reg_val = readl(GPIO6_DIR);

  39. reg_val |= GPIO6_1;

  40. writel(reg_val, GPIO6_DIR);

  41.  
  42. writel(REG_CLR, GPIO6_1_DATA);

  43.  
  44. DPRINTF("clear ir led...\n");

  45.  
  46. return 0;

  47. }

 

 

Makefile如下(可以拷贝driver目录下的各模块模板):

 
  1. [yangzheng@centos6 hi_gpio]$ cat Makefile

  2. #

  3. # Copyright 2000-2008

  4. # Wolfgang Denk, DENX Software Engineering, wd@denx.de.

  5. #

  6. # See file CREDITS for list of people who contributed to this

  7. # project.

  8. #

  9. # This program is free software; you can redistribute it and/or

  10. # modify it under the terms of the GNU General Public License as

  11. # published by the Free Software Foundation; either version 2 of

  12. # the License, or (at your option) any later version.

  13. #

  14. # This program is distributed in the hope that it will be useful,

  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of

  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

  17. # GNU General Public License for more details.

  18. #

  19. # You should have received a copy of the GNU General Public License

  20. # along with this program; if not, write to the Free Software

  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston,

  22. # MA 02111-1307 USA

  23. #

  24.  
  25. include $(TOPDIR)/config.mk

  26.  
  27. LIB := $(obj)libhi_gpio.a

  28.  
  29. COBJS-$(CONFIG_HI3516A_GPIO) += hi_gpio.o

  30.  
  31. COBJS := $(COBJS-y)

  32. SRCS := $(COBJS:.o=.c)

  33. OBJS := $(addprefix $(obj),$(COBJS))

  34.  
  35. all: $(LIB)

  36.  
  37. $(LIB): $(obj).depend $(OBJS)

  38. $(AR) $(ARFLAGS) $@ $(OBJS)

  39.  
  40.  
  41. #########################################################################

  42.  
  43. # defines $(obj).depend target

  44. include $(SRCTREE)/rules.mk

  45.  
  46. sinclude $(obj).depend

  47.  
  48. ########################################################################

 

 


2、在顶层Makefile添加如下代码:

LIBS += drivers/hi_gpio/libhi_gpio.a

3、在include/configs/hi3516a.h中添加如下代码:
#define CONFIG_HI3516A_GPIO

在include下增加hi_gpio.h文件,内容如下:

 

 
  1. [yangzheng@centos6 u-boot-2010.06]$ cat include/hi_gpio.h

  2. /********************************************************************************

  3. * Copyright: (C) 2016 Yang Zheng

  4. * All rights reserved.

  5. *

  6. * Filename: hi_gpio.h

  7. * Description: This head file is control hisi gpio

  8. *

  9. * Version: 1.0.0(09/21/2016~)

  10. * Author: Yang Zheng

  11. * ChangeLog: 1, Release initial version on "09/21/2016 06:09:49 PM"

  12. *

  13. ********************************************************************************/

  14. #ifndef __HI_GPIO_H__

  15. #define __HI_GPIO_H__

  16.  
  17. extern int clear_irled(void);

  18. #endif

 

 

4、在arch/arm/lib/board.c 里面调用即可,如:

 

 
  1. [yangzheng@centos6 u-boot-2010.06]$ vim arch/arm/lib/board.c

  2. void start_armboot (void)

  3. {

  4. init_fnc_t **init_fnc_ptr;

  5. char *s;

  6. #ifdef CONFIG_HAS_SLAVE

  7. char *e;

  8. #endif

  9. #if defined(CONFIG_VFD) || defined(CONFIG_LCD)

  10. unsigned long addr;

  11. #endif

  12.  
  13. #ifdef CONFIG_HI3516A_GPIO

  14. clear_irled(); // clear ir led, add by yangzheng 2016.9.21

  15. #endif

  16. ……

 

 

重新编译即可,调试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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值