This guide walks you through the complete process of setting up an embedded development environment for Loongson processors, from initial toolchain installation to deploying applications on target hardware.

Prerequisites

Before starting, ensure you have:

  • Loongson evaluation board or target hardware
  • Development host PC running Linux (Ubuntu 20.04 or later recommended)
  • Serial console cable (USB-to-UART) for debugging
  • Ethernet cable for network connectivity
  • Minimum 50GB free disk space for toolchain and source code

Step 1: Install Development Toolchain

The Loongson toolchain includes GCC compiler, GDB debugger, and binutils optimized for LoongArch architecture.

Option A: Using Pre-built Toolchain

Download the latest pre-built toolchain from Loongson or distribution repositories:

``bash

Add Loongson repository

wget -O - https://repo.loongson.cn/loongnix/loongnix.gpg | sudo apt-key add -

echo 'deb https://repo.loongson.cn/loongnix stable main' | sudo tee /etc/apt/sources.list.d/loongnix.list

Install toolchain

sudo apt update

sudo apt install gcc-loongarch64-linux-gnu gdb-multiarch

`

Option B: Building Toolchain from Source

For custom toolchain configurations, build from source:

`bash

Download crosstool-NG

git clone https://github.com/crosstool-ng/crosstool-ng

cd crosstool-ng

./bootstrap

./configure --prefix=/usr/local

make && sudo make install

Configure and build LoongArch toolchain

ct-ng loongarch64-unknown-linux-gnu

ct-ng build

`

Step 2: Set Up Target Hardware

Connect your Loongson evaluation board:

  • Connect serial console cable to UART0 for boot messages and console access
  • Connect Ethernet cable for network boot and file transfer
  • Connect power supply (typically 12V DC for embedded boards)
  • Connect USB devices as needed (keyboard, mouse, storage)
  • Serial Console Setup

    Configure minicom or screen for serial console access:

    `bash

    Using minicom

    sudo minicom -D /dev/ttyUSB0 -b 115200

    Using screen

    sudo screen /dev/ttyUSB0 115200

    `

    Step 3: Build and Configure U-Boot

    U-Boot is the standard bootloader for Loongson embedded platforms.

    `bash

    Clone U-Boot source

    git clone https://github.com/loongson/u-boot-loongson

    cd u-boot-loongson

    Configure for your board

    make loongson_2k1000_defconfig

    Build U-Boot

    make CROSS_COMPILE=loongarch64-linux-gnu- -j$(nproc)

    Flash to SPI flash (using flash programmer or in-system update)

    `

    Step 4: Build Linux Kernel

    Build a customized Linux kernel for your application:

    `bash

    Clone Linux source

    git clone https://github.com/loongson/linux-loongson

    cd linux-loongson

    Checkout stable branch

    git checkout loongson-5.10

    Configure kernel

    make ARCH=loongarch CROSS_COMPILE=loongarch64-linux-gnu- loongson2k_defconfig

    Customize configuration if needed

    make ARCH=loongarch CROSS_COMPILE=loongarch64-linux-gnu- menuconfig

    Build kernel

    make ARCH=loongarch CROSS_COMPILE=loongarch64-linux-gnu- -j$(nproc)

    Build device tree blob

    make ARCH=loongarch CROSS_COMPILE=loongarch64-linux-gnu- dtbs

    `

    Step 5: Create Root Filesystem

    Build a minimal root filesystem using Buildroot or Yocto:

    Using Buildroot

    `bash

    Clone Buildroot

    git clone https://github.com/buildroot/buildroot

    cd buildroot

    Configure for Loongson

    make loongson_2k1000_defconfig

    Customize packages

    make menuconfig

    Build rootfs

    make -j$(nproc)

    `

    The resulting root filesystem will be in output/images/rootfs.tar.gz

    Step 6: Deploy and Test

    Deploy the built components to your target board:

    Network Boot (TFTP + NFS)

    For development, use network boot for rapid iteration:

    `bash

    On host PC, configure TFTP server

    sudo apt install tftpd-hpa

    sudo cp arch/loongarch/boot/vmlinuz /var/lib/tftpboot/

    Configure NFS server for rootfs

    sudo apt install nfs-kernel-server

    echo '/path/to/rootfs *(rw,sync,no_root_squash)' | sudo tee -a /etc/exports

    sudo exportfs -a

    `

    In U-Boot prompt:

    `

    setenv ipaddr 192.168.1.100

    setenv serverip 192.168.1.1

    setenv bootfile vmlinuz

    tftpboot ${kernel_addr_r} ${bootfile}

    setenv bootargs console=ttyS0,115200 root=/dev/nfs nfsroot=192.168.1.1:/path/to/rootfs rw ip=192.168.1.100

    bootm ${kernel_addr_r}

    `

    Step 7: Cross-Compile Applications

    Develop and cross-compile applications for Loongson:

    `bash

    Set up environment

    export CROSS_COMPILE=loongarch64-linux-gnu-

    export ARCH=loongarch

    Compile application

    ${CROSS_COMPILE}gcc -o myapp myapp.c

    Transfer to target

    scp myapp root@192.168.1.100:/usr/bin/

    `

    Step 8: Debugging Setup

    Configure debugging with GDB:

    On Target Board

    `bash

    Install gdbserver on target

    gdbserver :1234 /usr/bin/myapp

    `

    On Host PC

    `bash

    Launch cross-GDB

    loongarch64-linux-gnu-gdb myapp

    Connect to target

    (gdb) target remote 192.168.1.100:1234

    (gdb) break main

    (gdb) continue

    ``

    Development Workflow

    For efficient development, establish this workflow:

  • Edit code on host PC with your preferred IDE
  • Build using cross-compiler
  • Transfer to target via network or storage
  • Test on target hardware
  • Debug using GDB if issues arise
  • Iterate until functionality is complete
  • Common Issues and Solutions

    Issue: Kernel panic during boot

    Solution: Verify device tree matches your board revision. Check kernel configuration includes required drivers.

    Issue: Serial console shows garbage characters

    Solution: Verify baud rate matches between U-Boot and terminal settings (typically 115200).

    Issue: Network boot fails

    Solution: Check Ethernet cable connection. Verify TFTP server is running and firewall allows TFTP traffic.

    Issue: Application crashes with illegal instruction

    Solution: Ensure application is compiled for correct LoongArch version. Check for architecture-specific code.

    Next Steps

    After mastering basic development:

    • Explore kernel driver development for custom hardware
    • Set up Yocto for production root filesystem builds
    • Implement secure boot for production systems
    • Optimize power management for battery-powered designs
    • Profile application performance and optimize

    Our FAE team is available to assist with development challenges and provide guidance for your specific project requirements.