Nuttx for STM32-p103 and lm3s6965evb over Qemu

Ciocirlan Stefan-Dan - SRIC

Introduction

The goal of the project is to emulate some target machines for Nuttx over Qemu. It will make the development of Nuttx and the testing of the machines more easy. It also offer templates for adding other machines and debuging them.

Devices

Emulated Devices

Cortex M3

LM3S6965 (NRND) Stellaris LM3S Microcontroller

lm3s6965.jpg

”“The Stellaris family of microcontrollers—the first ARM® CortexTM-M3 based controllers—brings high-performance 32-bit computing to cost-sensitive embedded microcontroller applications. These pioneering parts deliver customers 32-bit performance at a cost equivalent to legacy 8- and 16-bit devices, all in a package with a small footprint. The Stellaris family offers efficient performance and extensive integration, favorably positioning the device into cost-conscious applications requiring significant control-processing and connectivity capabilities. The Stellaris LM3S6000 series combines both a 10/100 Ethernet Media Access Control (MAC) and Physical (PHY) layer, marking the first time that integrated connectivity is available with an ARM Cortex-M3 MCU and the only integrated 10/100 Ethernet MAC and PHY available in an ARM architecture MCU. The LM3S6965 microcontroller is targeted for industrial applications, including remote monitoring, electronic point-of-sale machines, test and measurement equipment, network appliances and switches, factory automation, HVAC and building control, gaming equipment, motion control, medical instrumentation, and fire and security. For applications requiring extreme conservation of power, the LM3S6965 microcontroller features a battery-backed Hibernation module to efficiently power down the LM3S6965 to a low-power state during extended periods of inactivity. With a power-up/power-down sequencer, a continuous time counter (RTC), a pair of match registers, an APB interface to the system bus, and dedicated non-volatile memory, the Hibernation module positions the LM3S6965 microcontroller perfectly for battery applications. In addition, the LM3S6965 microcontroller offers the advantages of ARM's widely available development tools, System-on-Chip (SoC) infrastructure IP applications, and a large user community. Additionally, the microcontroller uses ARM's Thumb®-compatible Thumb-2 instruction set to reduce memory requirements and, thereby, cost. Finally, the LM3S6965 microcontroller is code-compatible to all members of the extensive Stellaris family; providing flexibility to fit our customers' precise needs.”” This info and more can be found in the datasheet at lm3s6965.pdf

STM32-P103

stm32-p103.jpg ”“STM32-P103 board is development board which allows you to explore thee features of the ARM Cortex M3 STM32F103RBT6 microcontroller produced by ST Microelectronics Inc. The board has SD/MMC card connector and allows USB Mass storage device demo to be evaluated. The RS232 driver and connector allows USB to Virtual COM port demo to be evaluated. The CAN port and driver allows CAN applications to be developed. The UEXT connector allows access to all other UEXT modules produced by OLIMEX (like MOD-MP3, MOD-NRF24LR, MOD-NOKIA6610, etc) to be connected easily. In the prototype area the customer can solder his own custom circuits and interface them to USB, CAN, RS232 etc.”” This info and more can be found in the datasheet at stm32-p103.pdf

Cortex M4

STM32-F4 Discovery

stm32-f407.jpeg

”“The STM32F4DISCOVERY Discovery kit allows users to easily develop applications with the STM32F407VG high performance microcontroller with the ARM ® Cortex ® -M4 32-bit core. It includes everything required either for beginners or for experienced users to get quickly started. Based on STM32F407VG, it includes an ST-LINK/V2 or ST-LINK/V2-A embedded debug tool, two ST-MEMS digital accelerometers, a digital microphone, one audio DAC with integrated class D speaker driver, LEDs, push buttons and a USB OTG micro-AB connector.”” This info and more can be found in the datasheet at stm32-f407.pdf

Physical Devices

A device that runs at least Ubuntu 10.04 LTS and can build the qemu source code. I used a Lenovo Thinkpad x250 (Ubuntu 16.04 LTS) and a PC with intel cpu i3-2100 and 4GB DDR3 (Ubuntu 14.04 LTS).

Technologies

Nuttx

NuttX is a real-time operating system (RTOS) with an emphasis on standards compliance and small footprint. Scalable from 8-bit to 32-bit microcontroller environments, the primary governing standards in NuttX are Posix and ANSI standards. Additional standard APIs from Unix and other common RTOS's (such as VxWorks) are adopted for functionality not available under these standards, or for functionality that is not appropriate for deeply-embedded environments (such as fork()).

NuttX was first released in 2007 by Gregory Nutt under the permissive BSD license.

NuttX is a real timed embedded operating system (RTOS). Its goals are:

  • Small Footprint -Usable in all but the tightest micro-controller environments, The focus is on the tiny-to-small, deeply embedded environment.
  • Rich Feature OS Set -The goal is to provide implementations of most standard POSIX OS interfaces to support a rich, multi-threaded development environment for deeply embedded processors. NON-GOALS: It is not a goal to provide the level of OS features like those provided by Linux. In order to work with smaller MCUs, small footprint must be more important than an extensive feature set. But standard compliance is more important than small footprint. Surely a smaller RTOS could be produced by ignoring standards. Think of NuttX is a tiny Linux work-alike with a much reduced feature set.
  • Highly Scalable -Fully scalable from tiny (8-bit) to moderate embedded (32-bit). Scalability with rich feature set is accomplished with: Many tiny source files, link from static libraries, highly configurable, use of weak symbols when available.
  • Standards Compliance - NuttX strives to achieve a high degree of standards compliance. The primary governing standards are POSIX and ANSI standards. Additional standard APIs from Unix and other common RTOS's are adopted for functionality not available under these standards or for functionality that is not appropriate for the deeply-embedded RTOS (such as fork()). Because of this standards conformance, software developed under other standard OSs (such as Linux) should port easily to NuttX.
  • Real-Time - Fully pre-emptible; fixed priority, round-robin, and “sporadic” scheduling.
  • Totally Open -Non-restrictive BSD license.
  • GNU Toolchains -Compatible GNU toolchains based on buildroot available for download to provide a complete development environment for many architectures.

Qemu

QEMU (short for Quick Emulator) is a free and open-source emulator that performs hardware virtualization.

QEMU is a hosted virtual machine monitor: it emulates the machine's processor through dynamic binary translation and provides a set of different hardware and device models for the machine, enabling it to run a variety of guest operating systems. It also can be used with KVM to run virtual machines at near-native speed (by taking advantage of hardware extensions such as IntelVT). QEMU can also do emulation for user-level processes, allowing applications compiled for one architecture to run on another.

QEMU has multiple operating modes:

  • User-mode emulation - In this mode QEMU runs single Linux or Darwin/macOS programs that were compiled for a different instruction set. System calls are thunked for endianness and for 32/64 bit mismatches. Fast cross-compilation and cross-debugging are the main targets for user-mode emulation.
  • System emulation - In this mode QEMU emulates a full computer system, including peripherals. It can be used to provide virtual hosting of several virtual computers on a single computer. QEMU can boot many guest operating systems, including Linux, Solaris, Microsoft Windows, DOS, and BSD;[4] it supports emulating several instruction sets, including x86, MIPS, 32-bit ARMv7, ARMv8, PowerPC, SPARC, ETRAX CRIS and MicroBlaze.
  • KVM Hosting - Here QEMU deals with the setting up and migration of KVM images. It is still involved in the emulation of hardware, but the execution of the guest is done by KVM as requested by QEMU.
  • Xen Hosting - QEMU is involved only in the emulation of hardware; the execution of the guest is done within Xen and is totally hidden from QEMU.

We used the System emulation operating mode. For Cortex-M4 I used a fork from qemu https://gnu-mcu-eclipse.github.io/qemu/

Setup

Requirements

1. git

sudo apt-get install git

2. qemu

sudo apt-get --no-install-recommends -y build-dep qemu

If is not working!

sudo sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
sudo apt-get update
sudo apt-get --no-install-recommends -y build-dep qemu

3. nuttx

sudo apt-get install automake bison build-essential flex gcc-arm-none-eabi gperf libncurses5-dev libtool libusb-dev libusb-1.0.0-dev
sudo apt-get install libgmp-dev libgmp3-dev libmpfr-dev libmpc-dev texinfo
sudo apt-get install opencd

4. qemu gnu muc eclipse

sudo apt-get install automake bison build-essential flex gcc-arm-none-eabi gperf libncurses5-dev libtool libusb-dev libusb-1.0.0-dev
sudo apt-get install libgmp-dev libgmp3-dev libmpfr-dev libmpc-dev texinfo
sudo apt-get install opencd
sudo apt-get install docker
rm -rf ~/Downloads/qemu-build.git
git clone --recurse-submodules https://github.com/gnu-mcu-eclipse/qemu-build.git ~/Downloads/qemu-build.git
bash ~/Downloads/qemu-build.git/scripts/build.sh preload-images

Qemu

To debug and create the entire functionalities for our machines you need the source code for Qemu. I used a version of Qemu which is targeting the arm devices and stm32 boards.

git clone https://github.com/beckus/qemu_stm32.git qemu-arm

The site for more demos and tutorials is at Link For Qemu_stm32 For making the stm32-p103 works we need to make a little change in Qemu source code.

cd qemu-arm
gedit hw/arm/stm32_p103.c

At the line 129 I had to change the following:

/* Connect RS232 to UART */
    stm32_uart_connect(
            (Stm32Uart *)uart2,
            serial_hds[0],
            STM32_USART2_NO_REMAP);
 
/* These additional UARTs have not been tested yet... */
    stm32_uart_connect(
            (Stm32Uart *)uart1,
            serial_hds[1],
            STM32_USART1_NO_REMAP);

in:

/* Connect RS232 to UART */
    stm32_uart_connect(
            (Stm32Uart *)uart1,
            serial_hds[0],
            STM32_USART1_NO_REMAP);
 
/* These additional UARTs have not been tested yet... */
    stm32_uart_connect(
            (Stm32Uart *)uart2,
            serial_hds[1],
            STM32_USART2_NO_REMAP);

After you need to configure the qemu with debuging features:

./configure --enable-debug --target-list="arm-softmmu" --disable-werror --extra-cflags=-DDEBUG_CLKTREE --extra-cflags=-DDEBUG_STM32_RCC --extra-cflags=-DDEBUG_STM32_UART --extra-cflags=-DDEBUG_STM32_TIMER

Add –extra-cflags=-DDEBUG_GIC only if you want to see the interupts from the cpu and its state.

Build Qemu

make

Nuttx

For Nuttx we also need the source code. And some auxiliary modules if we want to use them later.

mkdir nuttx_env
cd nuttx_env
git clone https://bitbucket.org/nuttx/nuttx.git nuttx
git clone https://bitbucket.org/nuttx/buildroot.git buildroot
git clone https://bitbucket.org/nuttx/nonbsd.git nonbsd
git clone https://bitbucket.org/nuttx/pascal.git Pascal
git clone https://bitbucket.org/nuttx/tools.git tools
git clone https://bitbucket.org/nuttx/uclibc.git uClibc++
git clone https://bitbucket.org/nuttx/apps.git apps

Nx module was moved in the apps repository. If you want an older version, you can find it at The nx module.

cd nuttx/
cd tools/

for STM32-p103:

./configure.sh stm32f103-minimum/nsh

for lm3s6965:

./configure.sh lm3s6965-ek/nsh

for stm32-f4discovery:

./configure.sh stm32f4discovery/nsh

Use the menuconfig to select buildtool and applications.

cd ..
make menuconfig

In menuconfig enable (minimum):

Build Setup/Build Host Platform/Linux
Build Setup/Debug Options/Generate Debug Symbols
System type/Toolchain Selection/Generic GNU EABI toolchain under Linux (or other POSIX enviroment)

You can enable other features but be ready to implement some functions. Build:

make

Qemu MCU Eclipse

First you need to build for the first time

bash ~/Downloads/qemu-build.git/scripts/build.sh --linux64 --develop --debug

Now in your home directory you have an directory called Work and in inside a folder called qemu-REALEASE_VERSION. It depends on your release version. The executable is located at ~/Work/qemu-REALEASE_VERSION/install/centos64/qemu/bin/qemu-system-gnuarmeclipse. The code that is being build is located at ~/Work/qemu-REALEASE_VERSION/qemu.git. You can modify as you want this code and after you can reuse the command shown before for building the new code. I made a fork with some USART implementation for the devices at https://github.com/sdcioc/qemu.

Run

for running the machine(stm32-p103/lm3s6965) on Qemu with gdb debuging(-s)

./qemu-arm/arm-softmmu/qemu-system-arm -M stm32-p103 -kernel ./nuttx_env/nuttx/nuttx.bin -s

for running the machine(stm32-f407) on Qemu with gdb debuging(-s)

~/Work/qemu-REALEASE_VERSION/install/centos64/qemu/bin/qemu-system-gnuarmeclipse  -M STM42F4-Discovery -kernel ./nuttx_env/nuttx/nuttx.bin --cpu cortex-m4 -s --serial pty --serial pty --serial pty --serial pty  --serial pty --serial pty --serial pty --serial pty --serial pty --serial pty -S

After you can use the elf file from directory to use gdb

arm-none-eabi-gdb ./nuttx_env/nuttx/nuttx

In gdb use:

target remote localhost:1234

Extra

Add a new machine in qemu

For Qemu MCU Eclipse for the stm32 type machine you can find a template in gnu-mcu-eclpise/develop/template

Add a new machine in nuttx

if you want to add a new machine to nuttx the next tutorial it can help you http://www.nuttx.org/Documentation/NuttxPortingGuide.html

Add you own app to nuttx

Use the template for app nuttx_env/apps/examples/hello and after use the menuconfig to add the app from Aplication configuration/Examples/YOUR_APP

Tutorials for building nuttx with buildroot

Network

If you want to implement the stelaris ethernet follow http://www.zilogic.com/blog/building-nuttx.html

Other modules for stm32

If you want to test different functionalitties for stm32-f4 discovery follow http://jeremyherbert.net/get/stm32f4_getting_started

Bibliography

iothings/proiecte/2018/qemu.txt · Last modified: 2021/12/06 18:08 by dan.tudose
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0