Skip to content

Commit 2b62ee8

Browse files
committed
qemu-arm/uart: Implement uart_rx_chr.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 5c02e03 commit 2b62ee8

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

ports/qemu-arm/uart.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,29 @@
3131

3232
#if defined(QEMU_SOC_STM32)
3333

34+
#define UART_SR_RXNE (1 << 5)
35+
#define UART_CR1_UE (1 << 13)
36+
#define UART_CR1_TE (1 << 3)
37+
#define UART_CR1_RE (1 << 2)
38+
3439
typedef struct _UART_t {
3540
volatile uint32_t SR;
3641
volatile uint32_t DR;
42+
volatile uint32_t BRR;
43+
volatile uint32_t CR1;
3744
} UART_t;
3845

3946
#define UART0 ((UART_t *)(0x40011000))
4047

4148
void uart_init(void) {
49+
UART0->CR1 = UART_CR1_UE | UART_CR1_TE | UART_CR1_RE;
50+
}
51+
52+
int uart_rx_chr(void) {
53+
if (!(UART0->SR & UART_SR_RXNE)) {
54+
return UART_RX_NO_CHAR;
55+
}
56+
return UART0->DR;
4257
}
4358

4459
void uart_tx_strn(const char *buf, size_t len) {
@@ -50,21 +65,34 @@ void uart_tx_strn(const char *buf, size_t len) {
5065
#elif defined(QEMU_SOC_NRF51)
5166

5267
typedef struct _UART_t {
53-
volatile uint32_t r0[2];
68+
volatile uint32_t STARTRX; // 0x000
69+
volatile uint32_t STOPRX; // 0x004
5470
volatile uint32_t STARTTX; // 0x008
55-
volatile uint32_t r1[(0x500 - 0x008) / 4 - 1];
71+
volatile uint32_t r0[(0x108 - 0x008) / 4 - 1];
72+
volatile uint32_t RXDRDY; // 0x108
73+
volatile uint32_t r1[(0x500 - 0x108) / 4 - 1];
5674
volatile uint32_t ENABLE; // 0x500
57-
volatile uint32_t r2[(0x51c - 0x500) / 4 - 1];
75+
volatile uint32_t r2[(0x518 - 0x500) / 4 - 1];
76+
volatile uint32_t RXD; // 0x518
5877
volatile uint32_t TXD; // 0x51c
5978
} UART_t;
6079

6180
#define UART0 ((UART_t *)(0x40002000))
6281

6382
void uart_init(void) {
6483
UART0->ENABLE = 4;
84+
UART0->STARTRX = 1;
6585
UART0->STARTTX = 1;
6686
}
6787

88+
int uart_rx_chr(void) {
89+
if (!UART0->RXDRDY) {
90+
return UART_RX_NO_CHAR;
91+
}
92+
UART0->RXDRDY = 0;
93+
return UART0->RXD;
94+
}
95+
6896
void uart_tx_strn(const char *buf, size_t len) {
6997
for (size_t i = 0; i < len; ++i) {
7098
UART0->TXD = buf[i];
@@ -74,6 +102,7 @@ void uart_tx_strn(const char *buf, size_t len) {
74102
#elif defined(QEMU_SOC_MPS2)
75103

76104
#define UART_STATE_TXFULL (1 << 0)
105+
#define UART_STATE_RXFULL (1 << 1)
77106

78107
#define UART_CTRL_TX_EN (1 << 0)
79108
#define UART_CTRL_RX_EN (1 << 1)
@@ -90,7 +119,14 @@ typedef struct _UART_t {
90119

91120
void uart_init(void) {
92121
UART0->BAUDDIV = 16;
93-
UART0->CTRL = UART_CTRL_TX_EN;
122+
UART0->CTRL = UART_CTRL_TX_EN | UART_CTRL_RX_EN;
123+
}
124+
125+
int uart_rx_chr(void) {
126+
if (!(UART0->STATE & UART_STATE_RXFULL)) {
127+
return UART_RX_NO_CHAR;
128+
}
129+
return UART0->DATA;
94130
}
95131

96132
void uart_tx_strn(const char *buf, size_t len) {
@@ -104,7 +140,9 @@ void uart_tx_strn(const char *buf, size_t len) {
104140
#elif defined(QEMU_SOC_IMX6)
105141

106142
#define UART_UCR1_UARTEN (1 << 0)
143+
#define UART_UCR2_RXEN (1 << 1)
107144
#define UART_UCR2_TXEN (1 << 2)
145+
#define UART_UTS1_RXEMPTY (1 << 5)
108146

109147
typedef struct _UART_t {
110148
volatile uint32_t URXD; // 0x00
@@ -113,13 +151,22 @@ typedef struct _UART_t {
113151
volatile uint32_t r1[15];
114152
volatile uint32_t UCR1; // 0x80
115153
volatile uint32_t UCR2; // 0x84
154+
volatile uint32_t r2[11];
155+
volatile uint32_t UTS1; // 0xb4
116156
} UART_t;
117157

118158
#define UART1 ((UART_t *)(0x02020000))
119159

120160
void uart_init(void) {
121161
UART1->UCR1 = UART_UCR1_UARTEN;
122-
UART1->UCR2 = UART_UCR2_TXEN;
162+
UART1->UCR2 = UART_UCR2_TXEN | UART_UCR2_RXEN;
163+
}
164+
165+
int uart_rx_chr(void) {
166+
if (UART1->UTS1 & UART_UTS1_RXEMPTY) {
167+
return UART_RX_NO_CHAR;
168+
}
169+
return UART1->URXD & 0xff;
123170
}
124171

125172
void uart_tx_strn(const char *buf, size_t len) {

ports/qemu-arm/uart.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828

2929
#include <stddef.h>
3030

31+
// Returned from uart_rx_chr when there are no chars available.
32+
#define UART_RX_NO_CHAR (-1)
33+
3134
void uart_init(void);
35+
int uart_rx_chr(void);
3236
void uart_tx_strn(const char *buf, size_t len);
3337

3438
#endif // MICROPY_INCLUDED_QEMU_ARM_UART_H

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy