BCM_GPIO驱动测试

在写内核驱动的时候,最好先在uboot上,进行裸板测试,验证寄存器,再移植到内核中,这样可以熟悉寄存器,也排除内核中的一些干扰。
/***********************************************************
 * led.c
 * 53344中有16个GPIO,但是却不是在统一个GPIO寄存器中设置的, * GPIO0-GPIO3是以CMIC开头的寄存器, * GPIO4-GPIO16才是以GPIO开头的寄存器。 ********************************************************
*/ typedef volatile unsigned int U32; #define GPIO_INPUT *(U32 *)0x18000060 #define GPIO_OUT *(U32 *)0x18000064 #define GPIO_OUT_EN *(U32 *)0x18000068 #define CMIC_GP_DATA_IN *(U32 *)0x48002000 #define CMIC_GP_DATA_OUT *(U32 *)0x48002004 #define CMIC_GP_OUT_EN *(U32 *)0x48002008 #define CMIC_GP_INT_TYPE *(U32 *)0x4800200c void configure_output(int gpio); void configure_input(int gpio); void led_on(int gpio); void led_off(int gpio); void delay(void); void delays(int count); int _start(void) { configure_output(0); configure_output(1); configure_output(2); CMIC_GP_OUT_EN |= 0xf; CMIC_GP_DATA_OUT &= ~0xf; while (1) { led_on(0); led_on(1); led_on(2); delay(); led_off(0); led_off(1); led_off(2); delay(); } return 0; } void configure_output(int gpio) { GPIO_OUT_EN |= (1 << gpio); } void configure_input(int gpio) { GPIO_OUT_EN &= ~(1 << gpio); } void led_on(int gpio) { GPIO_OUT &= ~(1 << gpio); } void led_off(int gpio) { GPIO_OUT |= (1 << gpio); } void delay(void) { __asm__ __volatile__ ( "ldr r0 , =0x4ffffff \n" "delayloop: \n" "subs r0 , r0 , #1 \n" "bne delayloop \n" :::"r0" ); } void delays(int count) { __asm__ __volatile__ ( "mov r0 , %0 \n" "delaysloop: \n" "subs r0 , r0 , #1 \n" "bne delaysloop \n" ::"r"(count) :"r0" );

Makefile, 链接脚本led.lds用于指定代码运行的位子

all:
    arm-linux-gcc -c  led.c  -o  led.o -fno-builtin
    arm-linux-ld  -T led.lds  led.o  -o  led
    arm-linux-objcopy -O binary led  led.bin
clean:
    rm -rf led  led.bin   *.o  

 链接脚本led.lds,用于指定代码运行的地址,以及编译的时候,如果有多个文件,每个文件存放在内存中的位置。

 当一个文件的时候,上面中间一条,也可以直接写成如下的写法,用于指定起始地址。

arm-linux-ld  -T0x61000000  led.o  -o  led

链接脚本,生成方法

arm-linux-ld --verbose > led.lds

再根据实际修改生成的文件。

/* Script for -z combreloc: combine and sort reloc sections */
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",
          "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(_start)
SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");
SECTIONS
{
    . = 0x61000000 ;   /*  程序的起始地址 */
    
    .text :
    {   
        led.o(.text) ; 
        *(.text) ; 
    }   
    align = 4 ; 
}

将生成的led.bin下载到板中,然后

go 0x61000000

 

posted @ 2015-10-27 21:40  SuperTao1024  阅读(1141)  评论(0编辑  收藏  举报