From d633928a160ee029fa1a16f10e62f6628020aaef Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 9 Apr 2019 20:23:01 -0400 Subject: [PATCH] Don't let a background task call run_background_tasks() --- main.c | 3 +++ ports/atmel-samd/background.c | 13 ++++++++++++ ports/atmel-samd/background.h | 1 + ports/nrf/background.c | 12 +++++++++++ ports/nrf/background.h | 35 +++++++++++++++++++++++++++++++ ports/nrf/supervisor/qspi_flash.c | 3 +-- 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 ports/nrf/background.h diff --git a/main.c b/main.c index 77498d7dee..3b7e90a7a6 100755 --- a/main.c +++ b/main.c @@ -43,6 +43,7 @@ #include "lib/mp-readline/readline.h" #include "lib/utils/pyexec.h" +#include "background.h" #include "mpconfigboard.h" #include "shared-module/displayio/__init__.h" #include "supervisor/cpu.h" @@ -86,6 +87,8 @@ void start_mp(supervisor_allocation* heap) { reset_status_led(); autoreload_stop(); + background_tasks_reset(); + // Stack limit should be less than real stack size, so we have a chance // to recover from limit hit. (Limit is measured in bytes.) mp_stack_ctrl_init(); diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index a8c4e38918..4e88d34e79 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -42,8 +42,20 @@ volatile uint64_t last_finished_tick = 0; bool stack_ok_so_far = true; +static bool running_background_tasks = false; + +void background_tasks_reset(void) { + running_background_tasks = false; +} + void run_background_tasks(void) { + // Don't call ourselves recursively. + if (running_background_tasks) { + return; + } assert_heap_ok(); + running_background_tasks = true; + #if (defined(SAMD21) && defined(PIN_PA02)) || defined(SAMD51) audio_dma_background(); #endif @@ -56,6 +68,7 @@ void run_background_tasks(void) { #endif filesystem_background(); usb_background(); + running_background_tasks = false; assert_heap_ok(); last_finished_tick = ticks_ms; diff --git a/ports/atmel-samd/background.h b/ports/atmel-samd/background.h index 8d1316e731..d9866a6abc 100644 --- a/ports/atmel-samd/background.h +++ b/ports/atmel-samd/background.h @@ -29,6 +29,7 @@ #include +void background_tasks_reset(void); void run_background_tasks(void); void run_background_vm_tasks(void); bool background_tasks_ok(void); diff --git a/ports/nrf/background.c b/ports/nrf/background.c index ea6e846b31..3fb5febd31 100644 --- a/ports/nrf/background.c +++ b/ports/nrf/background.c @@ -33,13 +33,25 @@ #include "shared-module/displayio/__init__.h" #endif +static bool running_background_tasks = false; + +void background_tasks_reset(void) { + running_background_tasks = false; +} + void run_background_tasks(void) { + // Don't call ourselves recursively. + if (running_background_tasks) { + return; + } + running_background_tasks = true; filesystem_background(); usb_background(); #ifdef CIRCUITPY_DISPLAYIO displayio_refresh_displays(); #endif + running_background_tasks = false; assert_heap_ok(); } diff --git a/ports/nrf/background.h b/ports/nrf/background.h new file mode 100644 index 0000000000..d53681c0fd --- /dev/null +++ b/ports/nrf/background.h @@ -0,0 +1,35 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_NRF_BACKGROUND_H +#define MICROPY_INCLUDED_NRF_BACKGROUND_H + +#include + +void background_tasks_reset(void); +void run_background_tasks(void); + +#endif // MICROPY_INCLUDED_NRF_BACKGROUND_H diff --git a/ports/nrf/supervisor/qspi_flash.c b/ports/nrf/supervisor/qspi_flash.c index 51a9ec0329..bb449d881c 100644 --- a/ports/nrf/supervisor/qspi_flash.c +++ b/ports/nrf/supervisor/qspi_flash.c @@ -47,8 +47,7 @@ bool spi_flash_command(uint8_t command) { .wipwait = false, .wren = false }; - nrfx_qspi_cinstr_xfer(&cinstr_cfg, NULL, NULL); - return true; + return nrfx_qspi_cinstr_xfer(&cinstr_cfg, NULL, NULL) == NRFX_SUCCESS; } bool spi_flash_read_command(uint8_t command, uint8_t* response, uint32_t length) {