Getting Started with Loongson Embedded Development
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:
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:
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.
💡 FAE Insights
Technical Logic
Successful embedded development follows a systematic approach: First, establish a working baseline using reference BSP and evaluation hardware. This proves your toolchain and hardware are functional. Second, incrementally modify the configuration for your specific requirements - custom kernel drivers, application-specific root filesystem, etc. Third, implement your application logic using cross-compilation and remote debugging. Fourth, optimize for your constraints - boot time, power consumption, memory usage. Finally, prepare for production with secure boot, update mechanisms, and manufacturing tools. Each step builds on the previous, minimizing risk and enabling early validation.
📋 Customer Cases
IoT Device Startup
Technology
Challenge
A startup developing an IoT gateway product needed to set up embedded development environment for Loongson 2K1000 but lacked experience with the architecture and toolchain.
Solution
Provided step-by-step guidance following this development guide. Set up cross-compilation environment, configured network boot for rapid iteration, and established GDB debugging workflow.
Customer Feedback
"Development environment operational within one week. Team productive on Loongson platform within two weeks. Product successfully developed and entered production on schedule."
Frequently Asked Questions
1. What host PC operating system is recommended for Loongson development?
Linux is strongly recommended as the host PC operating system for Loongson development. Ubuntu 20.04 LTS or later is the most tested and supported distribution. Other Linux distributions including Debian, Fedora, and CentOS also work well. Windows Subsystem for Linux (WSL2) can be used for some development tasks but may have limitations with hardware debugging. macOS is not officially supported but may work with additional configuration. For the best development experience with full toolchain support and debugging capabilities, use a native Linux installation.
2. Can I use Docker for Loongson cross-compilation?
Yes, Docker can be used effectively for Loongson cross-compilation environments. Using Docker containers ensures consistent build environments across different developer machines and CI/CD systems. Pre-built Docker images with Loongson toolchain are available from community repositories. You can also create custom Docker images with your specific toolchain version and dependencies. Docker works well for automated builds and ensures reproducibility. For hardware debugging and device access, you may need to run containers with privileged mode or specific device mappings.
3. How do I debug kernel panics on Loongson boards?
Debugging kernel panics requires capturing the oops message and analyzing the stack trace. First, ensure serial console is connected and capturing boot messages. When panic occurs, the oops message will be printed to console - capture this output. The message includes register state, stack trace, and offending code location. Use addr2line with your vmlinux to translate addresses to source code lines. For persistent issues, enable KDB or KGDB for interactive debugging. Configure kdump to capture crash dumps for post-mortem analysis. Our FAE team can assist with complex kernel debugging scenarios.
4. What is the best way to transfer files to the target board?
Multiple methods are available for file transfer to Loongson target boards. For development, NFS root filesystem allows direct access to host files. SCP/SFTP provides secure file transfer over network. TFTP is useful for kernel and bootloader updates. USB storage devices work for offline transfer. For production, consider implementing OTA (Over-The-Air) update mechanisms. The best method depends on your workflow - NFS for active development, SCP for occasional transfers, USB for field updates. Ensure network connectivity is configured for network-based methods.
5. What are common mistakes when working with Loongson?
Common mistakes include insufficient planning and requirements analysis, not following reference designs closely enough, inadequate testing at each development phase, overlooking thermal and power design requirements, and not engaging technical support early. Other issues include insufficient validation of software compatibility, inadequate documentation of design decisions, and not planning for long-term software maintenance. We recommend following our development guides, using reference designs as starting points, and engaging our FAE team for design review.