1

I am a very beginner of QEMU and mcu programming, recently practicing uart control on QEMU. What I’m trying to do is virtually connect QEMU with host PC through USART2.

I have learned that option -serial is probably able to do so and tried adding
-serial mon:stdio => nothing happened
-serial stdio => error, but nothing shown in console.

Processor: STM32F407VG
Board: STM32F4-Discovery
IDE: Eclipse IDE
Host device os: macOS Catalina 10.15.7

Following are my codes and run configuration

#include "stm32f4xx.h"

void UART2_Init(void);
void UART2_Write(int ch);
void delayMs(int delay);

int main(void){

    UART2_Init();
    while(1){
        UART2_Write('H');
        delayMs(500);
        UART2_Write('i');
        delayMs(500);
    }
}

void UART2_Init(void){

    RCC -> APB1ENR |= 0x20000;  // Enable usart2 clock
    RCC -> AHB1ENR |= 0x8;      // Enable PD5 clock
    GPIOD -> AFR[0] = 0x700000; // access PD5 AF7
    GPIOD -> MODER |= 0x800;

    USART2 -> BRR = 0x0683;     // Set buad rate to 9600
    USART2 -> CR1 =  0x8;       // Tx enable
    USART2 -> CR1 =  0x2000;    // USART enable
}

void UART2_Write(int ch){
    // wait until Tx buffer is empty
    while(!(USART2 -> SR & 0x80)){
        USART2 -> DR = (ch & 0xff);
    }
}

enter image description here

greg-449
  • 109,219
  • 232
  • 102
  • 145
Leo Chen
  • 11
  • 2
  • UART functions of Cortex-M in QEMU are unavailable at the time of writing this post. They're commented out from the [uart.c](https://github.com/xpack-dev-tools/qemu/blob/7798c04541b9a5e7b6363f0cbcba8f1346373bcb/hw/cortexm/stm32/usart.c#L1300) file. I'm afraid there's nothing to do if you don't want to implement it by yourself. – rudolfdobias Jan 26 '21 at 19:05

0 Answers0