企业做网站系统/网站查询ip
一、安装下载arm-gcc 编译器:
二、在keil中配置gcc:
三、配置工程选项
1.配置gcc编译规则:
Misc Controls : -mcpu=cortex-m3 -mthumb -fdata-sections -ffunction-sections
注:
1.这里我用的cortex-m3,如果你是m4内核就改成4)
2.-mthumb的意义是:使用这个编译选项生成的目标文件是Thumb的
3.-fdata-sections和-ffunction-sections (-ffunction-sections 和 -fdata-sections 将每个函数或符号创建为一个sections,其中每个sections名与function或data名保持一致)
2.配置汇编规则:
Misc Controls : -mcpu=cortex-m3 -mthumb
3配置链接规则:
这里要添加链接脚本,一般可以在官方提供的固件库包找到类似的(链接规则)
Misc Controls : -Wl,--gc-sections
注:
1.-wl, 表示后面的参数 --gc-sections 传递给链接器( -Wl,--gc-sections 指示链接器去掉不用的section(其中-wl, 表示后面的参数 --gc-sections 传递给链接器),这样就能减少最终的可执行程序的大小了。)
4.找到适合自己芯片的链接文件(在官方例程有):
其内容:
/*
*****************************************************************************
**
** File : stm32_flash.ld
**
** Abstract : Linker script for STM32F207IG Device with
** 1MByte FLASH, 128KByte SRAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : STMicroelectronics STM32
**
** Environment : Atollic TrueSTUDIO(R)
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
** (c)Copyright Atollic AB.
** You may use this file as-is or modify it according to the needs of your
** project. Distribution of this file (unmodified or modified) is not
** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
** rights to distribute the assembled, compiled & linked contents of this
** file as part of an application binary file, provided that it is built
** using the Atollic TrueSTUDIO(R) toolchain.
**
*****************************************************************************
*//* Entry Point */
ENTRY(Reset_Handler)/* Highest address of the user mode stack */
_estack = 0x20020000; /* end of 128K SRAM *//* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0; /* required amount of heap */
_Min_Stack_Size = 0x200; /* required amount of stack *//* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K
}/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)KEEP (*(.init))
KEEP (*(.fini)). = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(.fini_array*))
KEEP (*(SORT(.fini_array.*)))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH/* used by the startup to initialize data */
_sidata = .;/* Initialized data sections goes into RAM, load LMA copy after code */
.data : AT ( _sidata )
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON). = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAMPROVIDE ( end = _ebss );
PROVIDE ( _end = _ebss );/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(4);
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(4);
} >RAM/* MEMORY_bank1 section, code must be located here explicitly */
/* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
.memory_b1_text :
{
*(.mb1text) /* .mb1text sections (code) */
*(.mb1text*) /* .mb1text* sections (code) */
*(.mb1rodata) /* read-only data (constants) */
*(.mb1rodata*)
} >MEMORY_B1/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}.ARM.attributes 0 : { *(.ARM.attributes) }
}
注意:
编译报错
ld.exe: section .ARM.exidx LMA [08009914,0800991b] overlaps section .data LMA [08009914,08009fe3]
/* 添加.ARM.exidx段 */
.ARM.exidx : {
. = ALIGN(4);
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
. = ALIGN(4);
} >FLASH能解决报错问题,运行不了程序(需要找到自己的链接文件)
四、启动代码,使用GCC专用的.S文件
使用GCC编译器需要的启动代码不同与AMRCC,不过官方已经有提供了相关代码,如下图:
五、解决编译报错:
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
添加 syscall.c
/**
*****************************************************************************
**
** File : syscalls.c
**
** Abstract : System Workbench Minimal System calls file
**
** For more information about which c-functions
** need which of these lowlevel functions
** please consult the Newlib libc-manual
**
** Environment : System Workbench for MCU
**
** Distribution: The file is distributed ¡°as is,¡± without any warranty
** of any kind.
**
** (c)Copyright System Workbench for MCU.
** You may use this file as-is or modify it according to the needs of your
** project. Distribution of this file (unmodified or modified) is not
** permitted. System Workbench for MCU permit registered System Workbench(R) users the
** rights to distribute the assembled, compiled & linked contents of this
** file as part of an application binary file, provided that it is built
** using the System Workbench for MCU toolchain.
**
*****************************************************************************
*//* Includes */
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
/* Variables */
//#undef errno
extern int errno;
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));register char * stack_ptr asm("sp");
char *__env[1] = { 0 };
char **environ = __env;
/* Functions */
void initialise_monitor_handles()
{
}int _getpid(void)
{
return 1;
}int _kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}void _exit (int status)
{
_kill(status, -1);
while (1) {} /* Make sure we hang here */
}int _read (int file, char *ptr, int len)
{
int DataIdx;for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}return len;
}int _write(int file, char *ptr, int len)
{
int DataIdx;for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}caddr_t _sbrk(int incr)
{
extern char end asm("end");
static char *heap_end;
char *prev_heap_end;if (heap_end == 0)
heap_end = &end;prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr)
{
// write(1, "Heap and stack collision\n", 25);
// abort();
errno = ENOMEM;
return (caddr_t) -1;
}heap_end += incr;
return (caddr_t) prev_heap_end;
}int _close(int file)
{
return -1;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}int _isatty(int file)
{
return 1;
}int _lseek(int file, int ptr, int dir)
{
return 0;
}int _open(char *path, int flags, ...)
{
/* Pretend like we always fail */
return -1;
}int _wait(int *status)
{
errno = ECHILD;
return -1;
}int _unlink(char *name)
{
errno = ENOENT;
return -1;
}int _times(struct tms *buf)
{
return -1;
}int _stat(char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}int _link(char *old, char *new)
{
errno = EMLINK;
return -1;
}int _fork(void)
{
errno = EAGAIN;
return -1;
}int _execve(char *name, char **argv, char **env)
{
errno = ENOMEM;
return -1;
}
编译通过,成功运行: