Skip to content

Commit f12ea7c

Browse files
committed
esp8266: Implement task-based, event-driven interface with UART.
This enables proper interfacing with underlying OS - MicroPython doesn't run the main loop, OS does, MicroPython just gets called when some event takes place.
1 parent 0abb560 commit f12ea7c

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

esp8266/esp_mphal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ void HAL_Delay(uint32_t Delay);
4040
void mp_hal_set_interrupt_char(int c);
4141
uint32_t mp_hal_get_cpu_freq(void);
4242

43+
#define UART_TASK_ID 0
44+
void uart_task_init();
45+
4346
#endif // _INCLUDED_MPHAL_H_

esp8266/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ void user_init(void) {
5252

5353
printf("\n");
5454

55+
#if MICROPY_REPL_EVENT_DRIVEN
56+
pyexec_friendly_repl_init();
57+
uart_task_init();
58+
return;
59+
goto soft_reset;
60+
#else
5561
for (;;) {
5662
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
5763
if (pyexec_raw_repl() != 0) {
@@ -65,6 +71,7 @@ void user_init(void) {
6571
}
6672

6773
goto soft_reset;
74+
#endif
6875
}
6976

7077
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {

esp8266/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#define MICROPY_MEM_STATS (0)
1010
#define MICROPY_DEBUG_PRINTERS (0)
1111
#define MICROPY_ENABLE_GC (1)
12-
#define MICROPY_STACK_CHECK (1)
12+
#define MICROPY_STACK_CHECK (0)
13+
#define MICROPY_REPL_EVENT_DRIVEN (1)
1314
#define MICROPY_HELPER_REPL (1)
1415
#define MICROPY_HELPER_LEXER_UNIX (0)
1516
#define MICROPY_ENABLE_SOURCE_LINE (1)

esp8266/uart.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "uart_register.h"
1717
#include "etshal.h"
1818
#include "c_types.h"
19+
#include "user_interface.h"
20+
#include "esp_mphal.h"
1921

2022
#define RX_BUF_SIZE (256)
2123

@@ -27,6 +29,8 @@ static uint16_t rx_buf_in;
2729
static uint16_t rx_buf_out;
2830
static uint8_t rx_buf[RX_BUF_SIZE];
2931

32+
static os_event_t uart_evt_queue[16];
33+
3034
static void uart0_rx_intr_handler(void *para);
3135

3236
/******************************************************************************
@@ -148,11 +152,15 @@ static void uart0_rx_intr_handler(void *para) {
148152
read_chars:
149153
while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
150154
RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
155+
#if 1 //MICROPY_REPL_EVENT_DRIVEN is not available here
156+
system_os_post(UART_TASK_ID, 0, RcvChar);
157+
#else
151158
uint16_t rx_buf_in_next = (rx_buf_in + 1) % RX_BUF_SIZE;
152159
if (rx_buf_in_next != rx_buf_out) {
153160
rx_buf[rx_buf_in] = RcvChar;
154161
rx_buf_in = rx_buf_in_next;
155162
}
163+
#endif
156164
}
157165
}
158166
}
@@ -189,3 +197,15 @@ void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) {
189197
void ICACHE_FLASH_ATTR uart_reattach() {
190198
uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
191199
}
200+
201+
// Task-based UART interface
202+
203+
int pyexec_friendly_repl_process_char(int c);
204+
205+
void uart_task_handler(os_event_t *evt) {
206+
pyexec_friendly_repl_process_char(evt->par);
207+
}
208+
209+
void uart_task_init() {
210+
system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue));
211+
}

0 commit comments

Comments
 (0)