Skip to content

Commit 61fa7c8

Browse files
committed
esp8266: Switch back to accumulating input data via ring buffer.
But now it's generic ring buffer implemented via ringbuf.h, and is intended for any type of input, including dupterm's, not just UART. The general process work like this: an interrupt-driven input source puts data into input_buf, and then signals new data available via call to mp_hal_signal_input().
1 parent 2e75a17 commit 61fa7c8

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

esp8266/esp_mphal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ extern void ets_wdt_disable(void);
3939
extern void wdt_feed(void);
4040
extern void ets_delay_us();
4141

42+
STATIC byte input_buf_array[256];
43+
ringbuf_t input_buf = {input_buf_array, sizeof(input_buf_array)};
4244
void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len);
4345
const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked};
4446

@@ -151,3 +153,7 @@ void __assert_func(const char *file, int line, const char *func, const char *exp
151153
nlr_raise(mp_obj_new_exception_msg(&mp_type_AssertionError,
152154
"C-level assert"));
153155
}
156+
157+
void mp_hal_signal_input(void) {
158+
system_os_post(UART_TASK_ID, 0, 0);
159+
}

esp8266/esp_mphal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@
2727
#ifndef _INCLUDED_MPHAL_H_
2828
#define _INCLUDED_MPHAL_H_
2929

30+
#include "py/ringbuf.h"
31+
3032
struct _mp_print_t;
3133
// Structure for UART-only output via mp_printf()
3234
extern const struct _mp_print_t mp_debug_print;
3335

36+
extern ringbuf_t input_buf;
37+
// Call this after putting data to input_buf
38+
void mp_hal_signal_input(void);
39+
3440
void mp_hal_init(void);
3541
void mp_hal_rtc_init(void);
3642
void mp_hal_feed_watchdog(void);

esp8266/uart.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,18 @@ static void uart0_rx_intr_handler(void *para) {
159159
read_chars:
160160
#if 1 //MICROPY_REPL_EVENT_DRIVEN is not available here
161161
ETS_UART_INTR_DISABLE();
162-
system_os_post(UART_TASK_ID, 0, 0);
162+
163+
while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
164+
uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
165+
ringbuf_put(&input_buf, RcvChar);
166+
}
167+
168+
mp_hal_signal_input();
169+
170+
// Clear pending FIFO interrupts
171+
WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
172+
ETS_UART_INTR_ENABLE();
173+
163174
#else
164175
while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
165176
uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
@@ -224,7 +235,7 @@ void mp_keyboard_interrupt(void);
224235
int interrupt_char;
225236
void uart_task_handler(os_event_t *evt) {
226237
int c, ret = 0;
227-
while ((c = uart_rx_one_char(UART_REPL)) >= 0) {
238+
while ((c = ringbuf_get(&input_buf)) >= 0) {
228239
if (c == interrupt_char) {
229240
mp_keyboard_interrupt();
230241
}
@@ -234,11 +245,6 @@ void uart_task_handler(os_event_t *evt) {
234245
}
235246
}
236247

237-
// Clear pending FIFO interrupts
238-
WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
239-
// Enable UART interrupts, so our task will receive events again from IRQ handler
240-
ETS_UART_INTR_ENABLE();
241-
242248
if (ret & PYEXEC_FORCED_EXIT) {
243249
soft_reset();
244250
}

0 commit comments

Comments
 (0)