天嵌 ARM开发社区

 找回密码
 注册
查看: 5703|回复: 7

uboot延时问题的讨论

[复制链接]
qiumengjie 发表于 2013-6-5 12:54:35 | 显示全部楼层 |阅读模式
本帖最后由 qiumengjie 于 2013-6-5 12:57 编辑

uboot延时.doc


#if defined(CONFIG_BOOTDELAY) &&(CONFIG_BOOTDELAY >= 0)
       s= getenv ("bootdelay");
       bootdelay= s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
       printf("### main_loop entered: bootdelay=%d\n\n", bootdelay);
更改为
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY>= 0)
       s= getenv ("bootdelay");
      
printf("s is %s \n",s);
printf("s is %c \n",s);
printf("s is %d \n",s);
printf("*s is %s \n",*s);
printf("*s is %c \n",*s);
printf("*s is %d \n",*s);
printf("simple_strtol is %d\n",simple_strtol(s,NULL,10));
       bootdelay= (s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY);
       printf("###main_loop entered: bootdelay=%d\n\n", bootdelay);
打印结果
s is <NULL>
s is
s is 0
*s is 熷馃????
 楼主| qiumengjie 发表于 2013-6-5 12:58:29 | 显示全部楼层
*s is  
*s is 18
### main_loop entered: bootdelay=0
此时 发现虽然我之前设置了CONFIG_BOOTDELAY  5,但是延时却是0;
执行 printenv  发现 环境变量中没有 bootdelay 。
因此 执行 setenv bootdelay 10
                        Saveenv
重启开发板 发现打印信息如下
s is 10
s is .
s is 869012526
*s is ?
*s is 1
*s is 49
### main_loop entered: bootdelay=10
此时 可以延时10s 后启动内核
通过分析上述描述 发现,无论我是否执行了 setenv bootdelay 10
bootdelay = (s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY);
这句代码都是转向 执行(int)simple_strtol(s, NULL, 10) 来得到bootdelay的值;

因此我在代码前再次添加
int a ,b ;
a = CONFIG_BOOTDELAY;
printf("CONFIG_BOOTDELAY is %x \n",a);
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
        s = getenv ("bootdelay");
        if(s==NULL)
                b=0;
        else
                b=1;
printf("b is %d \n",b);
printf("s is %s \n",s);
printf("s is %c \n",s);
printf("s is %d \n",s);
printf("*s is %s \n",*s);
printf("*s is %c \n",*s);
printf("*s is %d \n",*s);
printf("simple_strtol is %d \n",simple_strtol(s,NULL,10));
        bootdelay = (b ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY);

        printf("### main_loop entered: bootdelay=%d\n\n", bootdelay);

此时打印信息为

CONFIG_BOOTDELAY is 3
b is 1
s is 10
s is .
s is 869012526
*s is ?
*s is 1
*s is 49
### main_loop entered: bootdelay=10
即当设置了环境变量时 从(int)simple_strtol(s, NULL, 10) 返回延时时间;

当没有设置环境变量时,打印信息如下
b is 0
s is <NULL>
s is
s is 0
*s is 熷馃?馃?馃??
 楼主| qiumengjie 发表于 2013-6-5 12:59:53 | 显示全部楼层
qiumengjie 发表于 2013-6-5 12:58
*s is  
*s is 18
### main_loop entered: bootdelay=0

*s is  
*s is 18
### main_loop entered: bootdelay=3
此时 bootdelay的 延时时间从 CONFIG_BOOTDELAY 取值。


通过以上分析。求各位回复
bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;

这个代码是否有BUG ?!

求答案 求回复!
谢谢!
亚瑟王 发表于 2013-6-5 18:19:24 | 显示全部楼层
qiumengjie 发表于 2013-6-5 12:59
*s is  
*s is 18
### main_loop entered: bootdelay=3

s=getenv("xxx");中的s是一个字符串。要将字符串转变为数字需要使用函数simple_strol。
有了这个解释之后,其它的我相信你能给分析得出来的。
 楼主| qiumengjie 发表于 2013-6-6 12:16:28 | 显示全部楼层
亚瑟王 发表于 2013-6-5 18:19
s=getenv("xxx");中的s是一个字符串。要将字符串转变为数字需要使用函数simple_strol。
有了这个解释之后 ...

S 是字符串,这个我知道的。。。
bootdelay= s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
这句话的意思是 s 返回是NULL ,就执行CONFIG_BOOTDELAY ,返回是一个指向字符串的指针,就执行
   (int)simple_strtol(s, NULL, 10),将字符串转换为整型数。
   
但实际运行情况是,无论S 返回的是NULL,还是字符串,这个语句都是在执行
(int)simple_strtol(s, NULL, 10) ,根本没有选择 CONFIG_BOOTDELAY;

 楼主| qiumengjie 发表于 2013-6-6 12:20:49 | 显示全部楼层
qiumengjie 发表于 2013-6-6 12:16
S 是字符串,这个我知道的。。。
bootdelay= s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY ...

当我在函数中添加
if(s==NULL)
                b=0;
        else
                b=1;
并将bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
改为bootdelay = n ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
此时,才可以正确的执行上面的语句,即返回 S 为NULL,延时时间设置为CONFIG_BOOTDELAY。若返回为一个字符串,则设置时间为 (int)simple_strtol(s, NULL, 10);
问题从而产生。望解释,为何会有这样的结果。


亚瑟王 发表于 2013-6-6 16:00:45 | 显示全部楼层
qiumengjie 发表于 2013-6-6 12:16
S 是字符串,这个我知道的。。。
bootdelay= s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY ...

你实际试一下嘛。
 楼主| qiumengjie 发表于 2013-6-7 09:52:12 | 显示全部楼层
亚瑟王 发表于 2013-6-6 16:00
你实际试一下嘛。

我测试过了,测试结果都贴在上面。。。
S = NULL 的时候 照样执行的是(int)simple_strtol(s, NULL, 10);
求对比
求解释
   求答案
     求真知
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号 )

GMT+8, 2024-4-28 22:01 , Processed in 1.046875 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表