Ubuntu 22.04 GCC Arm 12.2.rel1編譯 DAPLink

2023-02-03 06:01:02

ARMmbed / DAPLink 專案

倉庫地址 https://github.com/ARMmbed/DAPLink

Arm Mbed 應該屬於Arm的機構或者是Arm資助的機構. 常用的 DAPLink 基本上都是從這個專案派生的. 倉庫主要是使用 Keil, 對 GCC 的支援是 2020 年才正式合併進來的 #750.

目錄結構

├── docs                  # 檔案
├── mbed-os.lib
├── projectfiles          # 根據不同的編譯器, 對各個project生成的編譯組態檔
│   ├── make_gcc_arm
│   └── uvision
├── projects.yaml         # project 列表
├── records
│   ├── board
│   ├── daplink
│   ├── family
│   ├── hic_hal
│   ├── rtos
│   ├── tools             # 對不同編譯器的預設編譯引數
│   └── usb
├── requirements.txt      # python依賴
├── source
│   ├── board
│   ├── cmsis-core
│   ├── daplink           # daplink的原始碼
│   ├── family            # 針對各廠商各型號的flash_blob.c 和 target.c
│   │   ├── ambiq
│   │   ├── arm
│   │   ├── freescale
│   │   ├── maxim
│   │   ├── nordic
│   │   ├── nuvoton
│   │   ├── nxp
│   │   ├── realtek
│   │   ├── renesas
│   │   ├── st
│   │   └── toshiba
│   ├── hic_hal
│   ├── rtos
│   ├── rtos2
│   ├── rtos_none
│   ├── target
│   └── usb
├── test
├── tools

Ubuntu 下使用 GCC Arm 編譯

參考說明 How to Build

GCC Arm 工具鏈

下載地址

環境設定

安裝python3

下載 GCC Arm 工具鏈, 並解壓到合適的目錄

匯出專案, 在專案下建立 virtualenv

$ git clone https://github.com/mbedmicro/DAPLink
$ cd DAPLink
$ pip install virtualenv
$ virtualenv venv

啟用虛擬環境(因為要使用progen)

$ source venv/bin/activate

第一次執行(如果後面倉庫有更新也需要執行)

(venv) $ pip install -r requirements.txt

編譯

方式一

PATH=$PATH:/opt/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi/bin/;python tools/progen_compile.py -t make_gcc_arm --clean -v --parallel stm32f103xb_if

方式二

progen generate -t make_gcc_arm -p stm32f103xb_bl
PATH=$PATH:/opt/arm-gnu-toolchain-12.2.rel1-x86_64-arm-none-eabi/bin/;make -C projectfiles/make_gcc_arm/stm32f103xb_bl

遇到的問題

用 gcc arm 10.3 可以順利編譯, 如果用 gcc arm 11.2 會報 SysTick 錯誤, 如果用 11.3 或 12.2 編譯, 會報這樣的錯誤

Compiling ../../../source/usb/msc/usbd_core_msc.c
Compiling ../../../source/usb/msc/usbd_msc.c
Compiling ../../../source/usb/usbd_core.c
Assembling ../../../source/hic_hal/stm32/stm32f103xb/gcc/startup_stm32f103xb.S
Linking build/stm32f103xb_bl.elf
/opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-closer.o): in function `_close_r':
/data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/closer.c:47: warning: _close is not implemented and will always fail
/opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-lseekr.o): in function `_lseek_r':
/data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/lseekr.c:49: warning: _lseek is not implemented and will always fail
/opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-readr.o): in function `_read_r':
/data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/readr.c:49: warning: _read is not implemented and will always fail
/opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-writer.o): in function `_write_r':
/data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/writer.c:49: warning: _write is not implemented and will always fail
Memory region         Used Size  Region Size  %age Used
    m_interrupts:          1 KB         1 KB    100.00%
          m_text:         46 KB        46 KB    100.00%
        m_cfgrom:          0 GB         1 KB      0.00%
          m_data:        9168 B      20224 B     45.33%
        m_cfgram:         142 B        256 B     55.47%
collect2: error: ld returned 1 exit status
make: *** [Makefile:283: build/stm32f103xb_bl.elf] Error 1
make: Leaving directory '/home/milton/WorkC/DAPLink/projectfiles/make_gcc_arm/stm32f103xb_bl'

需要做以下的修改

  1. 編輯 vi source/daplink/sdk_stub.c

增加以下內容

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
    (void)file;
    //int DataIdx;
    //for (DataIdx = 0; DataIdx < len; DataIdx++)
    //{
    //    __io_putchar(*ptr++);
    //}
    return len;
}

__attribute__((weak)) int _read(int file, char *ptr, int len)
{
    (void)file;
    //int DataIdx;
    //for (DataIdx = 0; DataIdx < len; DataIdx++)
    //{
    //    *ptr++ = __io_getchar();
    //}
    return len;
}

__attribute__((weak)) int _isatty(int fd)
{
    if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
        return 1;

    errno = EBADF;
    return 0;
}

__attribute__((weak)) int _close(int fd)
{
    if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
        return 0;

    errno = EBADF;
    return -1;
}

__attribute__((weak)) int _lseek(int fd, int ptr, int dir)
{
    (void)fd;
    (void)ptr;
    (void)dir;

    errno = EBADF;
    return -1;
}
  1. 修改 records/tools/gcc_arm.yaml

在 ld_flags 下增加 -Wl,--no-warn-rwx-segment

之後就能順利編譯了

相關連結