STM32F103ZE使用gcc编译实战

2021-10-31  本文已影响0人  itsenlin

背景

在学习openharmony过程中发现,liteos-m系统中的例子都是risc-v架构的,当前用的最多的还是cortex-m架构,就想着自己移植一个cortex-m架构的例子,手上正好有一个正点原子的战舰开发板(stm32f103zet6),就以这个开发板做为练手学习

不过之前都是通过keil直接使用ARMCC工具链进行编译的,而openharmony使用的是gcc/clang编译,所以首先将工具链切换成gcc,在网上看到一篇很不错的入门文章,在这里首先感谢下作者的分享,连接地址为:【教程】如何用GCC“零汇编”白嫖MDK

只不过这里要将文章中的ARM Cortex-M7换成ARM Cortex-M3,一步步按作者的操作来,是可以搭建一个空temple工程,但是加上业务代码之后发现无法工作,具体原因未知(有知道也请帮忙知会下,在这里多谢了:)),不过通过我的尝试最终解决了,下面就记录一下,整个工程代码会上传到csdn

业务代码实现

我的开发板中有两个LED灯,对应PB5和PE5两个管脚,这次就以跑马灯的实验来学习
增加如下业务代码

void LED_Init(void)
{
    RCC->APB2ENR|=1<<3;    //使能PORTB时钟       
    RCC->APB2ENR|=1<<6;    //使能PORTE时钟

    GPIOB->CRL&=0XFF0FFFFF; 
    GPIOB->CRL|=0X00300000;//PB.5 推挽输出
    GPIOB->ODR|=1<<5;      //PB.5 输出高电平

    GPIOE->CRL&=0XFF0FFFFF;
    GPIOE->CRL|=0X00300000;//PE.5 推挽输出
    GPIOE->ODR|=1<<5;      //PE.5 输出高电平
}
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "cmsis_compiler.h"
#include "stm32f10x.h"
#include "led.h"

void delay()
{
    for (int i = 0; i < 10000000; i++){
        __NOP();
    }
}

int main(void)
{
    LED_Init();

    while(1) {
        delay();
        GPIOB->ODR &= ~(1<<5);
        GPIOE->ODR |= 1<<5;          
        delay();
        GPIOB->ODR |= 1<<5;
        GPIOE->ODR &= ~(1<<5);
    }

    return 0;
}

__attribute__((noreturn))
void exit(int err_code) {
    while(1) {
        __NOP();
    }
}

遇到的问题及解决

也即问题出在CMSIS包中带的连接文件和启动文件有问题,因当前刚开始学习,具体啥问题还未搞清楚,以后搞清楚了再补充(有知道也希望能告知学习一下)

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20010000;    /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200;      /* required amount of heap  */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 64K
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 512K
}

/* 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) */
    *(.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 goes into FLASH */
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >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 (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >FLASH
 
  /* used by the startup to initialize data */
  _sidata = LOADADDR(.data);

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .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 */
  . = 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;
  } >RAM
 
  /* User_heap_stack section, used to check that there is enough RAM left */
  ._user_heap_stack :
  {
    . = ALIGN(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
  } >RAM
 
  /* Remove information from the standard libraries */
  /DISCARD/ :
  {
    libc.a ( * )
    libm.a ( * )
    libgcc.a ( * )
  }
 
  .ARM.attributes 0 : { *(.ARM.attributes) }
}
上一篇 下一篇

猜你喜欢

热点阅读