Note: This was tested with QEMU emulator version 8.2.2
sudo apt install qemu-system-arm
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/usart.h>
static void usart_setup(void)
{
rcc_periph_clock_enable(RCC_USART1);
/* Setup UART parameters. */
usart_set_baudrate(USART1, 115200);
usart_set_databits(USART1, 8);
usart_set_stopbits(USART1, USART_STOPBITS_1);
usart_set_mode(USART1, USART_MODE_TX);
usart_set_parity(USART1, USART_PARITY_NONE);
usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
/* Finally enable the USART. */
usart_enable(USART1);
}
int _write(int file, char *ptr, int len)
{
int i;
if (file == 1) {
for (i = 0; i < len; i++)
usart_send_blocking(USART1, ptr[i]);
return i;
}
errno = EIO;
return -1;
}
int main (void)
{
usart_setup();
printf("Hello to qemu from stm32f1\r\n");
while (1)
{
;
}
}
At the time of writing initializing the HSE or HSI did not work, qemu was stuck in the initialization. For qemu it is not needed it seems.
qemu can be started like this:
qemu-system-arm -M stm32vldiscovery -kernel rem_workdir/arm_stm32f1/deploy/test_project_nohash/test_project.bin -nographicThe output should be printed directly to console.
Multiple serials can be started like this:
qemu-system-arm -M stm32vldiscovery -kernel rem_workdir/arm_stm32f1/deploy/test_project_nohash/test_project.bin -nographic -serial tcp::45457,server,nowait -serial tcp::45458,server,nowaitIt seems qemu will map one USART after the other to telnet as it is specified here: https://github.com/qemu/qemu/blob/master/hw/arm/stm32f100_soc.c#L39C54-L39C64
The serial output will then be redirected to (USART1)
telnet 127.0.0.1 45457and (USART2)
telnet 127.0.0.1 45458Note: Multiple serials were not tested, but from what I read it should work:
https://lists.nongnu.org/archive/html/qemu-discuss/2024-02/msg00032.html
apt-get install build-essential libglib2.0-dev zlib1g-dev libpixman-1-dev
apt-get install libsdl2*git clone https://github.com/gnu-mcu-eclipse/qemu.git
cd qemu
git submodule update --init dtcmkdir build
cd build
../configure --target-list="gnuarmeclipse-softmmu" --prefix=<qemu_build_dir>/_installed --disable-werror --with-sdlabi="2.0"
make && make installThe qemu needs some jpg files and json configs prepared in the work directory (where qemu is started) so you need to copy the files (from the qemu bin dir, we use the olimex STM32-H103 board):
cd <qemu_build_dir>/_installed/bin
cp ../../../gnu-mcu-eclipse/devices/STM32F103xx-qemu.json .
cp ../../../gnu-mcu-eclipse/graphics/STM32-H103.jpg ../qemu-system-gnuarmeclipse --verbose --board STM32-H103 --image <image_deploy_dir>/test_project.elfqemu-system-gnuarmeclipse -s --verbose --board STM32-H103 --image <image_deploy_dir>/test_project.elfarm-none-eabi-gdb <image_deploy_dir>/test_project.elftarget remote 172.17.0.1:1234You can now set breakpoints etc. - do some default gdb to debug your program!