Использование printf на STM32F405

Я хочу использовать функции printf () и snprintf () из stdlib на моей встроенной платформе (OLIMEX P405) im, используя набор инструментов GNU arm-none-eabi-gcc версии 7.2.1. Я реализовал syscall.c и использовал gdb, чтобы подтвердить, что функция _sbrk вызывается, когда я использую printf (), однако _sbrk вызывается дважды, и после второго раза я больше не могу отлаживать. (Думаю, в libc нет отладочных символов?)

Breakpoint 2, _sbrk (incr=1048) at /home/../../../lib/src/syscall.c:123
(gdb) c
Continuing.

Breakpoint 2, _sbrk (incr=460) at /home/../../../lib/src/syscall.c:123
(gdb) c
Continuing. 

Поскольку функции в syscall.c вызываются, я думаю, что я скомпилировал и связал все правильно. (Не уверен, что)

Я думаю, что проблема может быть в реализации _sbrk или скрипта компоновщика.

Вопрос: Почему ПК никогда не достигает _write()? Или почему _sbrk() терпит неудачу?

скрипт компоновщика:

/* Entry Point */
ENTRY(Boot_Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20020000;    /* end of 128K RAM on AHB bus*/

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0;      /* required amount of heap  */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 1024K
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 192K
  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 */
    _exit = .;
  } >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;
  } >RAM

  /* User_heap_stack section, used to check that there is enough RAM left */
  ._user_heap_stack :
  {
    . = ALIGN(4);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _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) }
}

syscall.c

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "stdio.h"
#include "stm32f4xx_usart.h"

/***************************************************************************/

int _open(const char *name, int flags, int mode){
  return -1;
}

int _read(int file, char * ptr, int len) {
  ptr = ptr;
  len = len;
  errno = EINVAL;
  return -1;
}

/***************************************************************************/

int _lseek(int file, int ptr, int dir) {
  file = file;
  ptr = ptr;
  dir = dir;
  return 0;
}

/***************************************************************************/

int _write(int file, char * ptr, int len) {
  int index;
  if (!ptr) {
    return 0;
  }
  for (index = 0; index < len; index++) {
    while (!(USART3->SR & 0x00000040));
    USART_SendData(USART3, ptr[index]);
  }
  return len;
}

/***************************************************************************/

int _close(int file) {
  return 0;
}

/***************************************************************************/

/* Register name faking - works in collusion with the linker.  */

caddr_t _sbrk(int incr) {

    extern char _ebss; // Defined by the linker
    static char *heap_end;
    char *prev_heap_end;

    if (heap_end == 0) {
        heap_end = &_ebss;
    }
    prev_heap_end = heap_end;
    char * stack = (char*) __get_MSP();
     if (heap_end + incr >  stack)
     {
         _write (1, "Heap and stack collision\n", 25);
         errno = ENOMEM;
         return  (caddr_t) -1;
         //abort ();
     }

    heap_end += incr;
    return (caddr_t) prev_heap_end;
}



/***************************************************************************/

int _fstat(int file, struct stat * st) {
  file = file;
  memset (st, 0, sizeof (* st));
  st->st_mode = S_IFCHR;
  return 0;
}

/***************************************************************************/

int _isatty(int fd) {
  fd = fd;
  return 1;
}

int _kill(int pid, int sig) {
  errno=EINVAL;
  return(-1);
}

int _getpid(void) {
  return 1;
}

Makefile:

ifndef prog
prog+=main.c
endif

TARGET:=boot
# TODO change to your ARM gcc toolchain path
TOOLCHAIN_ROOT:=~/Downloads/gcc-arm-none-eabi-7-2017-q4-major
TOOLCHAIN_PATH:=$(TOOLCHAIN_ROOT)/bin
TOOLCHAIN_PREFIX:=arm-none-eabi

# Optimization level, can be [0, 1, 2, 3, s].
OPTLVL:=0
DBG:=-g

STARTUP:=$(CURDIR)/lib
LINKER_SCRIPT:=$(CURDIR)/stm32_flash.ld

INCLUDE=-I$(CURDIR)/lib
INCLUDE+=-I$(CURDIR)/inc
INCLUDE+=-I$(CURDIR)/lib/inc
INCLUDE+=-I$(CURDIR)/lib/inc/peripherals
INCLUDE+=-I$(CURDIR)/lib/inc/core
INCLUDE+=-I./newlib-1.18.0/newlib/libc/include
BUILD_DIR = $(CURDIR)/build
BIN_DIR = $(CURDIR)/bin

# vpath is used so object files are written to the current directory instead
# of the same directory as their source files
vpath %.c   $(CURDIR)/lib/src/peripherals \
            $(CURDIR)/lib/src/ \
            $(CURDIR)/lib/ \
            $(CURDIR)/src

vpath %.s $(STARTUP)
ASRC=startup_stm32f4xx.s

# Project Source Files
SRC+=$(notdir $(prog))
SRC+=stm32f4xx_it.c
SRC+=system_stm32f4xx.c
SRC+=syscall.c

# Standard Peripheral Source Files
SRC+=misc.c
SRC+=stm32f4xx_dcmi.c
#SRC+=stm32f4xx_hash.c
SRC+=stm32f4xx_rtc.c
SRC+=stm32f4xx_adc.c
SRC+=stm32f4xx_dma.c
#SRC+=stm32f4xx_hash_md5.c
#SRC+=stm32f4xx_sai.c
SRC+=stm32f4xx_can.c
#SRC+=stm32f4xx_dma2d.c
#SRC+=stm32f4xx_hash_sha1.c
SRC+=stm32f4xx_sdio.c
#SRC+=stm32f4xx_cec.c
#SRC+=stm32f4xx_dsi.c
SRC+=stm32f4xx_i2c.c
#SRC+=stm32f4xx_spdifrx.c
SRC+=stm32f4xx_crc.c
SRC+=stm32f4xx_exti.c
SRC+=stm32f4xx_iwdg.c
SRC+=stm32f4xx_spi.c
#SRC+=stm32f4xx_cryp.c
SRC+=stm32f4xx_flash.c
#SRC+=stm32f4xx_lptim.c
SRC+=stm32f4xx_syscfg.c
#SRC+=stm32f4xx_cryp_aes.c
#SRC+=stm32f4xx_flash_ramfunc.c
#SRC+=stm32f4xx_ltdc.c
SRC+=stm32f4xx_tim.c
#SRC+=stm32f4xx_cryp_des.c
#SRC+=stm32f4xx_fmc.c
SRC+=stm32f4xx_pwr.c
SRC+=stm32f4xx_usart.c
#SRC+=stm32f4xx_cryp_tdes.c
#SRC+=stm32f4xx_fmpi2c.c
#SRC+=stm32f4xx_qspi.c
SRC+=stm32f4xx_wwdg.c
SRC+=stm32f4xx_dac.c
SRC+=stm32f4xx_fsmc.c
SRC+=stm32f4xx_rcc.c
SRC+=stm32f4xx_dbgmcu.c
SRC+=stm32f4xx_gpio.c
SRC+=stm32f4xx_rng.c

CDEFS=-DUSE_STDPERIPH_DRIVER
CDEFS+=-DSTM32F4XX
CDEFS+=-DSTM32F40_41xxx
CDEFS+=-DHSE_VALUE=8000000
CDEFS+=-D__FPU_PRESENT=1
CDEFS+=-D__FPU_USED=1
CDEFS+=-DARM_MATH_CM4

MCUFLAGS=-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -finline-functions -Wdouble-promotion -std=gnu99 -nostdlib -nostartfiles -specs=nosys.specs
COMMONFLAGS=-O$(OPTLVL) $(DBG) -Wall -ffunction-sections -fdata-sections
CFLAGS=$(COMMONFLAGS) $(MCUFLAGS) $(INCLUDE) $(CDEFS)

LDLIBS=-lm -lc -lgcc 
LDFLAGS=$(MCUFLAGS) -u _scanf_float -u _printf_float -fno-exceptions -Wl,--gc-sections -T$(LINKER_SCRIPT)
#LDFLAGS=$(MCUFLAGS) -u _scanf_float -u _printf_float -fno-exceptions -Wl,--gc-sections,-T$(LINKER_SCRIPT),-Map,$(BIN_DIR)/$(TARGET).map


CC=$(TOOLCHAIN_PATH)/$(TOOLCHAIN_PREFIX)-gcc
LD=$(TOOLCHAIN_PATH)/$(TOOLCHAIN_PREFIX)-gcc 
OBJCOPY=$(TOOLCHAIN_PATH)/$(TOOLCHAIN_PREFIX)-objcopy
AS=$(TOOLCHAIN_PATH)/$(TOOLCHAIN_PREFIX)-as
AR=$(TOOLCHAIN_PATH)/$(TOOLCHAIN_PREFIX)-ar
GDB=$(TOOLCHAIN_PATH)/$(TOOLCHAIN_PREFIX)-gdb

OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o)

$(BUILD_DIR)/%.o: %.c
    @echo [CC] $(notdir $<)
    @$(CC) $(CFLAGS) $< -c -o $@

all: $(OBJ)
    @echo [AS] $(ASRC)
    @$(AS) -o $(ASRC:%.s=$(BUILD_DIR)/%.o) $(STARTUP)/$(ASRC)
    @echo [LD] $(TARGET).elf
    @$(CC) -o $(BIN_DIR)/$(TARGET).elf $(LDFLAGS) $(OBJ) $(ASRC:%.s=$(BUILD_DIR)/%.o) $(LDLIBS)
    @echo [HEX] $(TARGET).hex
    @$(OBJCOPY) -O ihex $(BIN_DIR)/$(TARGET).elf $(BIN_DIR)/$(TARGET).hex
    @echo [BIN] $(TARGET).bin
    @$(OBJCOPY) -O binary $(BIN_DIR)/$(TARGET).elf $(BIN_DIR)/$(TARGET).bin
    @echo [SREC] $(TARGET).srec
    @$(OBJCOPY) -O srec $(BIN_DIR)/$(TARGET).elf $(BIN_DIR)/$(TARGET).srec

syscall.o:
    $(CC) $(CFLAGS) -c -o $@ $^

.PHONY: clean
clean:
    @echo [RM] OBJ
    @rm -f $(OBJ)
    @rm -f $(BUILD_DIR)/*.o
    @echo [RM] BIN
    @rm -f $(BIN_DIR)/$(TARGET).elf
    @rm -f $(BIN_DIR)/$(TARGET).hex
    @rm -f $(BIN_DIR)/$(TARGET).bin

.PHONY: flash
flash:
    @openocd -f openocd.cfg -c "program $(BIN_DIR)/$(TARGET).elf reset verify exit"

.PHONY: help
help:   
    @echo "make:"
    @echo " Specify a program to compile by setting prog=<program>"
    @echo " e.g.: make prog=blink.c"
    @echo " Specify BL=1 to allow bootloading"
    @echo " e.g.: make prog=blink.c BL=1"
    @echo "make flash:"
    @echo " Flashes the previously compiled .elf file to the target using OpenOCD and the OLIMEX ARM-USB-OCD debugger"
    @echo "make flash_bootloader:"
    @echo " Flashes the bootlader binary using OpenOCD and the OLIMEX ARM-USB-OCD debugger"
    @echo "sudo make load:"
    @echo " Flashes the previously compiled .srec file to the target using the bootloader. Requires sudo! Reset the target when prompted."

0 ответов

Другие вопросы по тегам