aboutsummaryrefslogtreecommitdiff
path: root/ports/qemu-arm/uart.c
diff options
context:
space:
mode:
authorDamien George2018-09-24 12:09:28 +1000
committerDamien George2019-02-12 13:50:01 +1100
commite7332b05841549a41614e522285639dcaa7bd526 (patch)
tree339ca8a643891c44fb3fbcec2c4ba1fb765bc21e /ports/qemu-arm/uart.c
parent775c7b86f017b81d31c3d463aec1b18088dbcdab (diff)
qemu-arm: Rework to run bare-metal on boards with Cortex-M CPUs.
Adds support for 3 Cortex-M boards, selectable via "BOARD" in the Makefile: - microbit, Cortex-M0 via nRF51822 - netduino2, Cortex-M3 via STM32F205 - mps2-an385, Cortex-M3 via FPGA netduino2 is the default board because it's supported by older qemu versions (down to at least 2.5.0).
Diffstat (limited to 'ports/qemu-arm/uart.c')
-rw-r--r--ports/qemu-arm/uart.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/ports/qemu-arm/uart.c b/ports/qemu-arm/uart.c
new file mode 100644
index 000000000..a0ce737c0
--- /dev/null
+++ b/ports/qemu-arm/uart.c
@@ -0,0 +1,78 @@
+#include <stdint.h>
+#include <stddef.h>
+
+#include "uart.h"
+
+#if defined(QEMU_SOC_STM32)
+
+typedef struct _UART_t {
+ volatile uint32_t SR;
+ volatile uint32_t DR;
+} UART_t;
+
+#define UART0 ((UART_t*)(0x40011000))
+
+void uart_init(void) {
+}
+
+void uart_tx_strn(const char *buf, size_t len) {
+ for (size_t i = 0; i < len; ++i) {
+ UART0->DR = buf[i];
+ }
+}
+
+#elif defined(QEMU_SOC_NRF51)
+
+typedef struct _UART_t {
+ volatile uint32_t r0[2];
+ volatile uint32_t STARTTX; // 0x008
+ volatile uint32_t r1[(0x500 - 0x008) / 4 - 1];
+ volatile uint32_t ENABLE; // 0x500
+ volatile uint32_t r2[(0x51c - 0x500) / 4 - 1];
+ volatile uint32_t TXD; // 0x51c
+} UART_t;
+
+#define UART0 ((UART_t*)(0x40002000))
+
+void uart_init(void) {
+ UART0->ENABLE = 4;
+ UART0->STARTTX = 1;
+}
+
+void uart_tx_strn(const char *buf, size_t len) {
+ for (size_t i = 0; i < len; ++i) {
+ UART0->TXD = buf[i];
+ }
+}
+
+#elif defined(QEMU_SOC_MPS2)
+
+#define UART_STATE_TXFULL (1 << 0)
+
+#define UART_CTRL_TX_EN (1 << 0)
+#define UART_CTRL_RX_EN (1 << 1)
+
+typedef struct _UART_t {
+ volatile uint32_t DATA;
+ volatile uint32_t STATE;
+ volatile uint32_t CTRL;
+ volatile uint32_t INTSTATUS;
+ volatile uint32_t BAUDDIV;
+} UART_t;
+
+#define UART0 ((UART_t*)(0x40004000))
+
+void uart_init(void) {
+ UART0->BAUDDIV = 16;
+ UART0->CTRL = UART_CTRL_TX_EN;
+}
+
+void uart_tx_strn(const char *buf, size_t len) {
+ for (size_t i = 0; i < len; ++i) {
+ while (UART0->STATE & UART_STATE_TXFULL) {
+ }
+ UART0->DATA = buf[i];
+ }
+}
+
+#endif