Skip to content

Commit

Permalink
esp8266: Implement task-based, event-driven interface with UART.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pfalcon committed Jan 16, 2015
1 parent 0abb560 commit f12ea7c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions esp8266/esp_mphal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ void HAL_Delay(uint32_t Delay);
void mp_hal_set_interrupt_char(int c);
uint32_t mp_hal_get_cpu_freq(void);

#define UART_TASK_ID 0
void uart_task_init();

#endif // _INCLUDED_MPHAL_H_
7 changes: 7 additions & 0 deletions esp8266/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ void user_init(void) {

printf("\n");

#if MICROPY_REPL_EVENT_DRIVEN
pyexec_friendly_repl_init();
uart_task_init();
return;
goto soft_reset;
#else
for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
if (pyexec_raw_repl() != 0) {
Expand All @@ -65,6 +71,7 @@ void user_init(void) {
}

goto soft_reset;
#endif
}

mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
Expand Down
3 changes: 2 additions & 1 deletion esp8266/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#define MICROPY_MEM_STATS (0)
#define MICROPY_DEBUG_PRINTERS (0)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_STACK_CHECK (1)
#define MICROPY_STACK_CHECK (0)
#define MICROPY_REPL_EVENT_DRIVEN (1)
#define MICROPY_HELPER_REPL (1)
#define MICROPY_HELPER_LEXER_UNIX (0)
#define MICROPY_ENABLE_SOURCE_LINE (1)
Expand Down
20 changes: 20 additions & 0 deletions esp8266/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "uart_register.h"
#include "etshal.h"
#include "c_types.h"
#include "user_interface.h"
#include "esp_mphal.h"

#define RX_BUF_SIZE (256)

Expand All @@ -27,6 +29,8 @@ static uint16_t rx_buf_in;
static uint16_t rx_buf_out;
static uint8_t rx_buf[RX_BUF_SIZE];

static os_event_t uart_evt_queue[16];

static void uart0_rx_intr_handler(void *para);

/******************************************************************************
Expand Down Expand Up @@ -148,11 +152,15 @@ static void uart0_rx_intr_handler(void *para) {
read_chars:
while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
#if 1 //MICROPY_REPL_EVENT_DRIVEN is not available here
system_os_post(UART_TASK_ID, 0, RcvChar);
#else
uint16_t rx_buf_in_next = (rx_buf_in + 1) % RX_BUF_SIZE;
if (rx_buf_in_next != rx_buf_out) {
rx_buf[rx_buf_in] = RcvChar;
rx_buf_in = rx_buf_in_next;
}
#endif
}
}
}
Expand Down Expand Up @@ -189,3 +197,15 @@ void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) {
void ICACHE_FLASH_ATTR uart_reattach() {
uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
}

// Task-based UART interface

int pyexec_friendly_repl_process_char(int c);

void uart_task_handler(os_event_t *evt) {
pyexec_friendly_repl_process_char(evt->par);
}

void uart_task_init() {
system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue));
}

0 comments on commit f12ea7c

Please sign in to comment.