embeddedcontainers / zephyr

Performance-optimized container images for building Zephyr RTOS applications.
MIT License
32 stars 1 forks source link

Flash/Debug STM32 from devcontainer #7

Closed kaizoku-oh closed 3 months ago

kaizoku-oh commented 4 months ago

Hello, did anyone manage to flash an STM32 board from a devcontainer environment It seems like the docker image is missing STM32CubeProgrammer (I don't know if that's supposed to come with zephyr toolchain or it should be installed separately) I'd be happy to contribute. Thanks in advance.

image

arifbalik commented 4 months ago

I'm pretty sure it does not come with the container, it is quite challenging to download ST software from headless machines since ST requires login and EULA. This one is a dirty solution but I've installed it using on a different machine with similar setup + GUI and then moved the binaries to the container

kaizoku-oh commented 4 months ago

Yes, I agree STM32CubeProgrammer download and install is not straight forward and I shoud figure out a way to install it on the container by just moving a local copy of the installation to specific directories in the container.

kaizoku-oh commented 4 months ago

And how about debugging from inside the vscode devcontainer? Since the debugger (referenced by "stlinkPath") uses ST-LINK_gdbserver which is a tool part of the STM32CubeIDE installation, I guess I should find a way to also copy that tool to the container with its dependencies, right?

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "cwd": "${workspaceFolder}",
            "executable": "${workspaceFolder}/app/build/zephyr/zephyr.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "stlink",
            // This one
            "stlinkPath": "/opt/st/stm32cubeide_1.14.0/plugins/com.st.stm32cube.ide.mcu.externaltools.stlink-gdb-server.linux64_2.1.100.202310302101/tools/bin/ST-LINK_gdbserver",
            "device": "STM32U575ZI",
            "interface": "swd",
            "runToEntryPoint": "main",
            "preLaunchTask": "Build (application)",
            "gdbPath": "${userHome}/zephyr-sdk-0.16.4/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb",
            "armToolchainPath": "${userHome}/zephyr-sdk-0.16.4/arm-zephyr-eabi/arm-zephyr-eabi/bin",
        }
    ]
}
arifbalik commented 4 months ago

I haven't used STCubeProgrammer to debug anything yet. I just opened an OpenOCD server on my host and connected to it with gdb and vscode cortex-debug plugin I think. It was a long time ago so I don't remember specifics but I do have my launch.json file;

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Remote STM32",
            "cwd": "${workspaceRoot}",
            "executable": "./build/zephyr/zephyr.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "external",
            "gdbTarget": "192.168.1.123:3333",
            "device": "STM32F0xx",
            "runToEntryPoint": "main", 
            "svdFile": "./esdc8034/boards/arm/esdc8034/stm32f030.svd",
            "showDevDebugOutput": "raw",
            "preLaunchTask": "build_ecool_app",
            "preLaunchCommands": [
                "monitor reset halt",
                "monitor stm32f0x unlock 0",
                "monitor stm32f0x mass_erase 0",
                "monitor program C:/Users/arifb/build/zephyr/zephyr.elf verify reset"
            ],

        }
    ]
}

Edit: It was something like this;

openocd -f interface/stlink-v2.cfg -f target/stm32f0x.cfg -c "gdb_port 3333" -c "tcl_port 6666" -c "telnet_port 4444" -c "bindto 0.0.0.0"

Btw idk if it is relevant but I use a modified version of the containers, I only have one Dockerfile.

# Base image and Zephyr SDK version
FROM debian:stable-slim

# Build arguments for SDK version and install directory
ARG ZEPHYR_SDK_VERSION=0.16.4
ARG ZEPHYR_SDK_INSTALL_DIR=/opt/zephyr-sdk-${ZEPHYR_SDK_VERSION}
ARG ZEPHYR_SDK_TOOLCHAINS="-t arm-zephyr-eabi -t xtensa-espressif_esp32s3_zephyr-elf"

# Environment setup for Python virtual environment
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Install core dependencies
RUN apt-get -y update && apt-get -y install --no-install-recommends \
  ca-certificates \
  cmake \
  device-tree-compiler \
  git \
  ninja-build \
  python3 \
  python3-pip \
  python3-venv \
  wget \
  xz-utils \
  sudo \
  gnupg \
  gdb-multiarch

RUN ln -s /usr/bin/gdb-multiarch /usr/bin/arm-none-eabi-gdb

# Setup Python environment
RUN python3 -m venv $VIRTUAL_ENV

# Install Zephyr SDK
RUN export sdk_file_name="zephyr-sdk-${ZEPHYR_SDK_VERSION}_linux-$(uname -m)_minimal.tar.xz" && \
  wget -q "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${ZEPHYR_SDK_VERSION}/${sdk_file_name}" && \
  mkdir -p ${ZEPHYR_SDK_INSTALL_DIR} && \
  tar -xvf ${sdk_file_name} -C ${ZEPHYR_SDK_INSTALL_DIR} --strip-components=1 && \
  ${ZEPHYR_SDK_INSTALL_DIR}/setup.sh -c ${ZEPHYR_SDK_TOOLCHAINS} && \
  rm ${sdk_file_name}

# Cleanup
RUN apt-get remove -y --purge wget xz-utils && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/*

# Install Python packages
RUN pip install --no-cache-dir wheel west pyelftools

# Add user for Zephyr development
RUN useradd -d /home/zephyr -ms /bin/bash -g root -G sudo zephyr && \
    echo "zephyr:root" | chpasswd && \
    mkdir /home/zephyr/workspace && \
    chown zephyr:root /home/zephyr/workspace

USER zephyr
aniltirli commented 3 months ago

i am able to debug stm32 with devcontainer in vscode and use editor to step by step debug, i haven't tried to use docker images in this repo but i used ghcr.io/zephyrproject-rtos/ci:latest docker image (https://github.com/zephyrproject-rtos/docker-image) as base, vscode extension marus25.cortex-debug and openocd comes within the docker image, you don't need ST IDE here. required toolchain comes with the docker image

devcontainer.json

{
    "name": "zephyr-template",
    "image": "ghcr.io/zephyrproject-rtos/ci:latest",
    "workspaceMount": "source=${localEnv:HOME}/projects/zephyr-stm32,target=/workdir,type=bind,consistency=cached",
    "workspaceFolder": "/workdir",
    "runArgs": [
        "--device", "/dev/bus/usb/"
    ],
    "postCreateCommand": "west init zephyrproject/ && cd zephyrproject/ && west update",
    "customizations": {
        "vscode": {
            "extensions": [
                "ms-vscode.cpptools-extension-pack",
                "marus25.cortex-debug"
            ]
        }
    }
}

launch.json

{
    "configurations": [
    {
        "cwd": "${workspaceRoot}/<application_folder>",
        "executable": "build/zephyr/zephyr.elf",
        "name": "Debug with OpenOCD",
        "request": "launch",
        "type": "cortex-debug",
        "serverpath": "/opt/toolchains/zephyr-sdk-0.16.4/sysroots/x86_64-pokysdk-linux/usr/bin/openocd",
        "servertype": "openocd",
        "searchDir": [],
        "runToEntryPoint": "main",
        "device": "<device_name>",
        "showDevDebugOutput": "none",
        "configFiles": [
         "/workdir/zephyrproject/zephyr/boards/arm/<device_name>/support/openocd.cfg"
        ]
    }
    ]
}

settings.json

{
    "cortex-debug.armToolchainPath": "/opt/toolchains/zephyr-sdk-0.16.4/arm-zephyr-eabi/bin/",
    "cortex-debug.armToolchainPrefix": "arm-zephyr-eabi",
    "files.associations": {
        "locale": "c",
        "pwm.h": "c",
        "typeinfo": "c",
        "stm32_ll_bus.h": "c"
    },
    "cmake.sourceDirectory": "/workdir/<application_folder>"
}
kaizoku-oh commented 3 months ago

i am able to debug stm32 with devcontainer in vscode and use editor to step by step debug, i haven't tried to use docker images in this repo but i used ghcr.io/zephyrproject-rtos/ci:latest docker image (https://github.com/zephyrproject-rtos/docker-image) as base, vscode extension marus25.cortex-debug and openocd comes within the docker image, you don't need ST IDE here. required toolchain comes with the docker image

devcontainer.json

{
  "name": "zephyr-template",
  "image": "ghcr.io/zephyrproject-rtos/ci:latest",
    "workspaceMount": "source=${localEnv:HOME}/projects/zephyr-stm32,target=/workdir,type=bind,consistency=cached",
    "workspaceFolder": "/workdir",
  "runArgs": [
        "--device", "/dev/bus/usb/"
    ],
  "postCreateCommand": "west init zephyrproject/ && cd zephyrproject/ && west update",
  "customizations": {
      "vscode": {
          "extensions": [
              "ms-vscode.cpptools-extension-pack",
              "marus25.cortex-debug"
          ]
      }
  }
}

launch.json

{
    "configurations": [
    {
        "cwd": "${workspaceRoot}/<application_folder>",
        "executable": "build/zephyr/zephyr.elf",
        "name": "Debug with OpenOCD",
        "request": "launch",
        "type": "cortex-debug",
        "serverpath": "/opt/toolchains/zephyr-sdk-0.16.4/sysroots/x86_64-pokysdk-linux/usr/bin/openocd",
        "servertype": "openocd",
        "searchDir": [],
        "runToEntryPoint": "main",
        "device": "<device_name>",
        "showDevDebugOutput": "none",
        "configFiles": [
         "/workdir/zephyrproject/zephyr/boards/arm/<device_name>/support/openocd.cfg"
        ]
    }
    ]
}

settings.json

{
    "cortex-debug.armToolchainPath": "/opt/toolchains/zephyr-sdk-0.16.4/arm-zephyr-eabi/bin/",
    "cortex-debug.armToolchainPrefix": "arm-zephyr-eabi",
    "files.associations": {
        "locale": "c",
        "pwm.h": "c",
        "typeinfo": "c",
        "stm32_ll_bus.h": "c"
    },
    "cmake.sourceDirectory": "/workdir/<application_folder>"
}

I practically did the same, here is my repo: https://github.com/kaizoku-oh/zephyr-online-workspace