Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

ESP32: Add control for the Ctrl-F hot key #139

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion esp32/fatfs/src/drivers/sflash_diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static bool sflash_write (void) {
DRESULT sflash_disk_init (void) {
if (!sflash_init_done) {
// this is how we diferentiate flash sizes in Pycom modules
if (esp_get_revision() > 0) {
if (spi_flash_get_chip_size() > (4 * 1024 * 1024)) {
sflash_start_address = SFLASH_START_ADDR_8MB;
sflash_fs_sector_count = SFLASH_FS_SECTOR_COUNT_8MB;
} else {
Expand Down
1 change: 1 addition & 0 deletions esp32/hal/esp32_mphal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uint64_t mp_hal_ticks_ms_non_blocking(void);
uint64_t mp_hal_ticks_us_non_blocking(void);
void mp_hal_delay_ms(uint32_t delay);
void mp_hal_set_interrupt_char(int c);
void mp_hal_set_reset_char(int c);
void mp_hal_reset_safe_and_boot(bool reset);

#endif // _INCLUDED_MPHAL_H_
4 changes: 3 additions & 1 deletion esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include "mperror.h"
#include "machtimer.h"

#include "esp_spi_flash.h"


TaskHandle_t mpTaskHandle;
TaskHandle_t svTaskHandle;
Expand Down Expand Up @@ -121,7 +123,7 @@ void app_main(void) {
mperror_pre_init();

// differentiate the Flash Size (either 8MB or 4MB) based on ESP32 rev id
micropy_hw_flash_size = (esp_get_revision() > 0 ? 0x800000 : 0x400000);
micropy_hw_flash_size = (spi_flash_get_chip_size() > (4 * 1024 * 1024) ? 0x800000 : 0x400000);

// propagating the Flash Size in the global variable (used in multiple IDF modules)
g_rom_flashchip.chip_size = micropy_hw_flash_size;
Expand Down
2 changes: 1 addition & 1 deletion esp32/mods/machuart.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ STATIC IRAM_ATTR void UARTRxCallback(int uart_id, int rx_byte) {
if (mp_interrupt_char == rx_byte) {
// raise an exception when interrupts are finished
mp_keyboard_interrupt();
} else if (CHAR_CTRL_F == rx_byte) {
} else if (mp_reset_char == rx_byte) {
servers_reset_and_safe_boot();
}
}
Expand Down
4 changes: 2 additions & 2 deletions esp32/telnet/telnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,10 @@ static void telnet_parse_input (uint8_t *str, int32_t *len) {

// in this case the server is not operating in binary mode
if (ch > 127 || ch == 0 || (telnet_data.state == E_TELNET_STE_LOGGED_IN &&
(ch == mp_interrupt_char || ch == CHAR_CTRL_F))) {
(ch == mp_interrupt_char || ch == mp_reset_char))) {
if (ch == mp_interrupt_char) {
mp_keyboard_interrupt();
} else if (ch == CHAR_CTRL_F) {
} else if (ch == mp_reset_char) {
*str++ = CHAR_CTRL_D;
mp_hal_reset_safe_and_boot(false);
_str++;
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/interrupt_char.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "py/mpstate.h"

int mp_interrupt_char;
int mp_reset_char;

void mp_hal_set_interrupt_char(int c) {
if (c != -1) {
Expand All @@ -36,6 +37,10 @@ void mp_hal_set_interrupt_char(int c) {
mp_interrupt_char = c;
}

void mp_hal_set_reset_char(int c) {
mp_reset_char = c;
}

void mp_keyboard_interrupt(void) {
MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(mp_kbd_exception);
}
2 changes: 2 additions & 0 deletions lib/utils/interrupt_char.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
*/

extern int mp_interrupt_char;
extern int mp_reset_char;
void mp_hal_set_interrupt_char(int c);
void mp_hal_set_reset_char(int c);
void mp_keyboard_interrupt(void);
2 changes: 2 additions & 0 deletions lib/utils/pyexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind,
start = mp_hal_ticks_ms();
mp_call_function_0(module_fun);
mp_hal_set_interrupt_char(-1); // disable interrupt
mp_hal_set_reset_char(CHAR_CTRL_F); // enable reset char
nlr_pop();
ret = 1;
if (exec_flags & EXEC_FLAG_PRINT_EOF) {
Expand All @@ -97,6 +98,7 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind,
// uncaught exception
// FIXME it could be that an interrupt happens just before we disable it here
mp_hal_set_interrupt_char(-1); // disable interrupt
mp_hal_set_reset_char(CHAR_CTRL_F); // enable reset char, might be wrong here
// print EOF after normal output
if (exec_flags & EXEC_FLAG_PRINT_EOF) {
mp_hal_stdout_tx_strn("\x04", 1);
Expand Down
14 changes: 11 additions & 3 deletions py/modmicropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_
#endif

#if MICROPY_KBD_EXCEPTION
STATIC mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) {
mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in));
STATIC mp_obj_t mp_micropython_kbd_intr(size_t n_args, const mp_obj_t *args) {
int c = mp_obj_get_int(args[0]);
mp_hal_set_interrupt_char(c);
if (n_args == 1) {
if (c == -1) {
mp_hal_set_reset_char(-1);
}
} else {
mp_hal_set_reset_char(mp_obj_get_int(args[1]));
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_kbd_intr_obj, 1, 2, mp_micropython_kbd_intr);
#endif

STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
Expand Down