Keil中配置GCC编译器开发STM32
一、下载编译链:
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
在此我下载的是:gcc-arm-none-eabi-10.3-2021.10-win32.zip
二、安装GCC编译链
直接解压到D盘:
三、配置Keil工程选项:
1、选择使用GCC进行编译
2、配置编译选项
1)配置CC的编译规则
2)Assembler规则
3)Linker选项
四、STM32F103RCTX_FLASH.ld文件(基于STM32F103RCTX系列,其他系列只需要简单修改即可)
/* Entry Point */
ENTRY(Reset_Handler)/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */_Min_Heap_Size = 0x200 ; /* required amount of heap */
_Min_Stack_Size = 0x400 ; /* required amount of stack *//* Memories definition */
MEMORY
{RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48KFLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K
}
/* Sections */
SECTIONS
{/* The startup code into "FLASH" Rom type memory */.isr_vector :{. = ALIGN(4);KEEP(*(.isr_vector)) /* Startup code */. = ALIGN(4);} >FLASH/* The program code and other data into "FLASH" Rom type memory */.text :{. = ALIGN(4);*(.text) /* .text sections (code) */*(.text*) /* .text* sections (code) */*(.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/* Constant data into "FLASH" Rom type memory */.rodata :{. = ALIGN(4);*(.rodata) /* .rodata sections (constants, strings, etc.) */*(.rodata*) /* .rodata* sections (constants, strings, etc.) */. = ALIGN(4);} >FLASH.ARM.extab : { . = ALIGN(4);*(.ARM.extab* .gnu.linkonce.armextab.*). = ALIGN(4);} >FLASH.ARM : {. = ALIGN(4);__exidx_start = .;*(.ARM.exidx*)__exidx_end = .;. = ALIGN(4);} >FLASH.preinit_array :{. = ALIGN(4);PROVIDE_HIDDEN (__preinit_array_start = .);KEEP (*(.preinit_array*))PROVIDE_HIDDEN (__preinit_array_end = .);. = ALIGN(4);} >FLASH.init_array :{. = ALIGN(4);PROVIDE_HIDDEN (__init_array_start = .);KEEP (*(SORT(.init_array.*)))KEEP (*(.init_array*))PROVIDE_HIDDEN (__init_array_end = .);. = ALIGN(4);} >FLASH.fini_array :{. = ALIGN(4);PROVIDE_HIDDEN (__fini_array_start = .);KEEP (*(SORT(.fini_array.*)))KEEP (*(.fini_array*))PROVIDE_HIDDEN (__fini_array_end = .);. = ALIGN(4);} >FLASH/* Used by the startup to initialize data */_sidata = LOADADDR(.data);/* Initialized data sections into "RAM" Ram type memory */.data : {. = 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 AT> FLASH/* Uninitialized data section into "RAM" Ram type memory */. = ALIGN(4);.bss :{/* This is used by the startup in order to initialize the .bss section */_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;} >RAM/* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */._user_heap_stack :{. = ALIGN(8);PROVIDE ( end = . );PROVIDE ( _end = . );. = . + _Min_Heap_Size;. = . + _Min_Stack_Size;. = ALIGN(8);} >RAM/* Remove information from the compiler libraries *//DISCARD/ :{libc.a ( * )libm.a ( * )libgcc.a ( * )}.ARM.attributes 0 : { *(.ARM.attributes) }
}
五、启动(代码)文件修改
六、编译运行
1、strexb/strexh错误
解决方法:需要修改core_cm3.c文件里面的内容
/*** @brief STR Exclusive (8 bit)** @param value value to store* @param *addr address pointer* @return successful / failed** Exclusive STR command for 8 bit values*/
uint32_t __STREXB(uint8_t value, uint8_t *addr)
{uint32_t result=0;// __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );__ASM volatile ("strexb %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );return(result);
}/*** @brief STR Exclusive (16 bit)** @param value value to store* @param *addr address pointer* @return successful / failed** Exclusive STR command for 16 bit values*/
uint32_t __STREXH(uint16_t value, uint16_t *addr)
{uint32_t result=0;// __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );__ASM volatile ("strexh %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );return(result);
}
3、sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk’ 错误
这是因为工程使用了sprintf/printf等C库函数,没有导入标准的C库函数导致的
需要导入syscall.c文件
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;
}
到此编译后没有出现错误了!
七、 编译成功后生成的是.elf和.hex文件,如果需要生成.bin文件,可以在User里面添加一个bat执行文件
Bat文件内容为:
D:\gcc-arm-none-eabi-10.3-2021.10-win32\gcc-arm-none-eabi-10.3-2021.10\arm-none-eabi\bin\objcopy.exe -O binary AppPrj.elf AppPrj_debug.bin
备注:
此工程配置后虽然能够正常运行程序,但在我电脑无法进行正常debug,很困惑。
Keil中配置GCC编译器开发STM32相关推荐
- Keil5配置GCC编译器编译STM32工程
Keil一般使用ARMCC编译MCU工程代码.偶然听说Keil也是支持内嵌GCC编译器的.于是尝试了网上博客所述的一些方法,最终找到了一篇博客 http://blog.csdn.net/lan1205 ...
- 最近在 vscode 中借助 gcc 编译器来配置 c
最近在 vscode 中借助 gcc 编译器来配置 c 语言开发环境时,发现中文编码存在乱码问题.再加上最近学习到多字节字符与宽字符,搅在一起,搞得很乱,就把自己的理解写下来,供有需者参考吧. 1. ...
- 【C 语言】Windows 下使用 gcc 编译器 ( 常用的编译器 | Qt 中的 gcc 编译器 | 独立安装 MinGW )
文章目录 一.常用的编译器 二.使用 Qt 中的 gcc 编译器 三.独立安装 MinGW 一.常用的编译器 常用编译器 : MSVC GCC MinGW CLANG ; MSVC : 微软 Micr ...
- 如何安装KEIL并配置好51与STM32的环境
Keil编辑和STM32cubemx配置STM32控制步进电机 \\\插播一条:文章末尾有惊喜哟~/// 使用Keil ARM V5 IDE编程和STM32CUBEMX代码配置实现STM32F103和 ...
- 怎么在VS Code中配置C/C++开发环境?
DevWeekly收集整理每周优质开发者内容,包括开源项目.工具资源.技术文章等方面,每周五首发于DevWeekly,欢迎大家Star并收藏! 原文链接:DevWeekly | 第1期:什么是时间复杂 ...
- VS2017中配置QT5.12开发环境
VS2017中配置QT5.12开发环境 一.安装VS2017 二.安装QT5.12.0 1.下载 2.安装 二.在VS2017中配置QT 三.在VS2017中创建QT工程 一.安装VS2017 关于V ...
- Keil5.15使用GCC编译器编译STM32工程
一.下载GCC编译器 GCC编译器下载地址 二.安装GCC 我们可以把GCC解压到keil的安装目录下面.如下图 1 2 三.配置Keil使用GCC编译器 1.配置CC编译规则,Misc Contr ...
- 使用STM32标准库构建VSCode+gcc+openOCD开发STM32
目前为止,使用STM32的平台比较流行的是keil for Arm和IAR for ARM,这两个平台都比较类似,集成编辑.编译和调试环境,俗称IDE.用户只需简单的操作就能编译和调试STM32,非常 ...
- Windows G++ | VisualStudioCodeC语言环境配置(GCC编译器)
VSCode GCC编译器 软件安装 VSCode(代码编辑器) MinGW(代码编译器) 配置环境变量 配置可执行文件环境变量 添加LIBRARY_PATH环境变量 配置C_INCLUDE_PATH ...
最新文章
- UI设计培训完之后可以去哪些公司工作
- 4、以太网基础知识——ICMP协议详解
- 《CCNP SWITCH 300-115认证考试指南》——1.3节模块化网络的设计
- 邮件服务器对接移动设备 企业邮箱随身邮
- RabbitMQ管理(2)——用户管理
- SAP odata模型缓存校验机制
- python学习笔记2---内置函数
- 制作字幕html怎么使用教程,anSuber字幕编辑软件使用详解
- python编写字典库_用Python生成MySql数据字典
- Allure报告的安装及环境变量的配置和在pytest中调用
- 流式数据、批式数据、实时数据、历史数据的区别
- iPhone手机热点连接不稳定
- HTML+CSS+JS
- mysql创建数据库utf_MYSQL创建utf-8格式的数据库_MySQL
- 深入解读 Spark 宽依赖和窄依赖(ShuffleDependency NarrowDependency)
- ARMV8体系结构简介:exclusive monitor
- 加密的rar文件不知道密码怎么解压
- Pdf 模板制作,itextpdf
- c++ 制作五子棋游戏
- 前端工程师是做什么的?