[smart210] firstled.s 代码分析及编译分析

平台:smart210(tiny210v2)

CPU:S5PV210

目标:led灯按照一定频率闪烁

1.代码如下(如果不能编译,请去掉注释):

.globl _start

_start:
    ldr r1, =0xE0200280     //config the GPJ2CON[0to15] as output
    ldr r0, =0x00001111        
    str r0, [r1]
    mov r2, #0x1000          //r2=8
led_blink:
     ldr r1, =0xE0200284    //set the GPJ2DAT[0to3] as 0,4 leds would light on!
     mov r0, #0xa                
     str r0, [r1]

     bl delay

     ldr r1, =0xE0200284    //set the GPJ2DAT[0to3] as 1,4 leds would light off!
     mov r0, #0x5 
     str r0, [r1]

     bl delay

     sub r2,r2,#1        //r2=r2-1
     cmp r2,#0            //if r2!=0 jump to led_blink
     bne led_blink        //then the led_blink will turn 8 times

halt:
    b halt            //while(1)

delay:
    mov r0,#0xf000000        //r0=0xf000000
delay_loop:
    cmp r0,#0            //if r0=0
    sub r0,r0,#1        //r0=r0-1
    bne delay_loop        //after "cmp r0,#0" ,if r0!=0 jump to delay_loop 
                //then the delay_loop will turn 32 times
    mov pc,lr            //come back the main

2.使用以下命令编译链接与反汇编

arm-linux-gcc -c -o firstled.o firstled.s    //对.s文件进行预处理,编译,汇编,生成.o文件
arm-linux-ld -Ttext 0x0 -o led.elf firstled.o    //链接.o文件,并设置代码段起始运行地址为0x0 ,生成.elf
arm-linux-objcopy -O binary led.elf led.bin    //将.elf文件复制一份,并以二进制.bin形式保存,该形式可在开发板上使用
arm-linux-objdump -D led.elf > led_elf.dis    //将.elf文件反汇编成.dis文件,方便调试程序

3.使用以下命令给led.bin添加头部

./mkmini210 led.bin led210.bin

其中,mkmini210由程序mkv210_image.c编译而来,mkv210_image.c内容如下

/* 在BL0阶段,Irom内固化的代码读取nandflash或SD卡前16K的内容,
 * 并比对前16字节中的校验和是否正确,正确则继续,错误则停止。*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUFSIZE                 (16*1024)
#define IMG_SIZE                (16*1024)
#define SPL_HEADER_SIZE         16
#define SPL_HEADER              "S5PC110 HEADER  "

int main (int argc, char *argv[])
{
    FILE        *fp;
    char        *Buf, *a;
    int        BufLen;
    int        nbytes, fileLen;
    unsigned int    checksum, count;
    int        i;
    
    // 1. 3个参数
    if (argc != 3)
    {
        printf("Usage: mkbl1 <source file> <destination file>\n");
        return -1;
    }

    // 2. 分配16K的buffer
    BufLen = BUFSIZE;
    Buf = (char *)malloc(BufLen);
    if (!Buf)
    {
        printf("Alloc buffer failed!\n");
        return -1;
    }

    memset(Buf, 0x00, BufLen);

    // 3. 读源bin到buffer
    // 3.1 打开源bin
    fp = fopen(argv[1], "rb");
    if( fp == NULL)
    {
        printf("source file open error\n");
        free(Buf);
        return -1;
    }
    // 3.2 获取源bin长度
    fseek(fp, 0L, SEEK_END);
    fileLen = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    // 3.3 源bin长度不得超过16K-16byte
    count = (fileLen < (IMG_SIZE - SPL_HEADER_SIZE))
        ? fileLen : (IMG_SIZE - SPL_HEADER_SIZE);
    // 3.4 buffer[0~15]存放"S5PC110 HEADER  "
    memcpy(&Buf[0], SPL_HEADER, SPL_HEADER_SIZE);
    // 3.5 读源bin到buffer[16]
    nbytes = fread(Buf + SPL_HEADER_SIZE, 1, count, fp);
    if ( nbytes != count )
    {
        printf("source file read error\n");
        free(Buf);
        fclose(fp);
        return -1;
    }
    fclose(fp);

    // 4. 计算校验和
     // 4.1 从第16byte开始统计buffer中共有几个1
    a = Buf + SPL_HEADER_SIZE;
    for(i = 0, checksum = 0; i < IMG_SIZE - SPL_HEADER_SIZE; i++)
        checksum += (0x000000FF) & *a++;
    // 4.2 将校验和保存在buffer[8~15]
    a = Buf + 8;
    *( (unsigned int *)a ) = checksum;

    // 5. 拷贝buffer中的内容到目的bin
    // 5.1 打开目的bin
    fp = fopen(argv[2], "wb");
    if (fp == NULL)
    {
        printf("destination file open error\n");
        free(Buf);
        return -1;
    }
    // 5.2 将16k的buffer拷贝到目的bin中
    a = Buf;
    nbytes    = fwrite( a, 1, BufLen, fp);
    if ( nbytes != BufLen )
    {
        printf("destination file write error\n");
        free(Buf);
        fclose(fp);
        return -1;
    }

    free(Buf);
    fclose(fp);

    return 0;
}

这段代码完成的工作是:

第一步 分配16k 的buffer;
第二步 将led.bin 读到buffer的第16byte开始的地方;
第三步 计算校验和,并将校验和保存在buffer第8~11byte中;
第四步 将16k 的buffer拷贝到led210.bin 中;

4.下载led210.bin到smart210

这里使用minitools下载.bin文件,操作过程比较简单就不再赘述了

5.运行效果是4个LED交替闪烁,频率约为0.8s左右

posted @ 2013-07-21 13:19  轻羽  阅读(245)  评论(0编辑  收藏  举报