5 为什么main函数被执行2次?

问题章节:新1期  代码重定位

main.c:


int main(void)
{
uart0_init();
puts("hello\n\r");
return 0;

}

uart.c:

#include "s3c2440_soc.h"

/* 115200,8n1 */
void uart0_init()
{
/* �����������ڴ��� */
/* GPH2,3����TxD0, RxD0 */
GPHCON &= ~((3<<4) | (3<<6));
GPHCON |= ((2<<4) | (2<<6));
GPHUP &= ~((1<<2) | (1<<3));  /* ʹ���ڲ����� */

/* ������ */
/* UBRDIVn = (int)( UART clock / ( buad rate x 16) ) �C1
*  UART clock = 50M
*  UBRDIVn = (int)( 50000000 / ( 115200 x 16) ) �C1 = 26
*/
UCON0 = 0x00000005; /* PCLK,�ж�/��ѯģʽ */
UBRDIV0 = 26;
/* �������ݸ�ʽ */
ULCON0 = 0x00000003; /* 8n1: 8������λ, �޽���λ, 1��ֹͣλ */
/*  */
}
int puts(const char *s)
{
while (*s)
{
putchar(*s);
s++;
}
return 0;
}
int putchar(int c)
{
/* UTRSTAT0 */
/* UTXH0 */
while (!(UTRSTAT0 & (1<<2)));
UTXH0 = (unsigned char)c;
return 0;
}
void sdram_init(void)
{
BWSCON = 0x22000000;
BANKCON6 = 0x18001;
BANKCON7 = 0x18001;
REFRESH  = 0x8404f5;
BANKSIZE = 0xb1;
MRSRB6   = 0x20;
MRSRB7   = 0x20;
}

start.S:


.text
.global _start
_start:
/* 关闭看门狗 */
ldr r0, =0x53000000
ldr r1, =0
str r1, [r0]
/* 设置时钟
* Operating Frequency
* Fclk Up to 400MHz
* Hclk Up to 136MHz
* Pclk Up to 68MHz
* FCLK 400M
* HCLK 100M
* PCLK 50M
*/
ldr r0, =0x4C000014
ldr r1, =0x00000005
str r1, [r0]
ldr r0, =0x4C000004
ldr r1, =(92<<12) | (1<<4) | (1<<0)
str r1, [r0]
mrc p15,0,r0,c1,c0,0
orr r0,r0,#0xc0000000  //R1_nF:OR:R1_iA
mcr p15,0,r0,c1,c0,0

/* 设置栈 */
ldr r0, =0
ldr r1, [r0]
str r0, [r0]
ldr r2, [r0]
cmp r0, r2
ldr sp, =0x40000000 + 4096
moveq sp, #4096
streq r1, [r0]
bl sdram_init
ldr r1, =0  
ldr r2, =_start  
ldr r3, =__bss_start
cpy:
ldr r4, [r1]
str r4, [r2]
add r1, r1, #4
add r2, r2, #4
cmp r2, r3
ble cpy

/* 清除BSS段 */
ldr r1, =__bss_start
ldr r2, =__end
mov r3, #0
clean:
str r3, [r1]
add r1, r1, #4
cmp r1, r2
ble clean
//bl main
ldr pc, =main
halt:
b halt
链接脚本:
SECTIONS
{
. = 0x30000000;
. = ALIGN(4);

.text      :
{
  *(.text)
}
. = ALIGN(4);
.rodata : { *(.rodata) }
. = ALIGN(4);
.data : { *(.data) }
. = ALIGN(4);
__bss_start = .;
.bss : { *(.bss) *(.COMMON) }
__end = .;
}
运行结果:
attachments-2020-12-XefLZis15fc8c7c33c493.png
请先 登录 后评论

最佳答案 2020-12-21 16:12

  .s文件修改一下,或者main.c文件修改一下就行!

ldr lr,=halt    //加上这行
ldr pc, =main
halt:
b halt

或者

main
{    .....
     while(1);//加上这行
}


请先 登录 后评论

其它 2 个回答

星星之火 - 嵌入式工程师
擅长:答疑助手

确认是每次上电都打印2次吗?

会不会电源接触不好

请先 登录 后评论
lililala68

台湾威力彩不会电源接触不好,文件修改一下,体彩排列5或者main.c文件修改一下就行 

请先 登录 后评论
  • 2 关注
  • 0 收藏,1173 浏览
  • 可樂泡冰 提出于 2020-12-03 19:12

相似问题