From b82fc8dcef5ff05fe76bce19c83857e5097ec868 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 12 Jan 2017 20:32:43 +0100 Subject: stmhal: fix wrong usage of gcc -print-libgcc-file-name --- stmhal/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index 03b00fbd3..f39818eb5 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -76,7 +76,7 @@ COPT += -Os -DNDEBUG endif # uncomment this if you want libgcc -#LIBS += $(shell $(CC) -print-libgcc-file-name) +#LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) SRC_LIB = $(addprefix lib/,\ libc/string0.c \ -- cgit v1.2.3 From 8678e3edfd8f56b90d0847ba0a9a6ba615463152 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 21 Jan 2017 23:21:37 +1100 Subject: stmhal: Convert to use builtin help function. --- stmhal/Makefile | 1 - stmhal/help.c | 20 ++------------------ stmhal/mpconfigport.h | 3 ++- 3 files changed, 4 insertions(+), 20 deletions(-) (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index f39818eb5..3735172dc 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -112,7 +112,6 @@ SRC_LIB = $(addprefix lib/,\ netutils/netutils.c \ timeutils/timeutils.c \ utils/pyexec.c \ - utils/pyhelp.c \ ) SRC_C = \ diff --git a/stmhal/help.c b/stmhal/help.c index 21e81ebc0..1d4803f75 100644 --- a/stmhal/help.c +++ b/stmhal/help.c @@ -24,11 +24,9 @@ * THE SOFTWARE. */ -#include +#include "py/builtin.h" -#include "lib/utils/pyhelp.h" - -STATIC const char *help_text = +const char *stmhal_help_text = "Welcome to MicroPython!\n" "\n" "For online help please visit http://micropython.org/help/.\n" @@ -70,17 +68,3 @@ STATIC const char *help_text = "\n" "For further help on a specific object, type help(obj)\n" ; - -STATIC mp_obj_t pyb_help(uint n_args, const mp_obj_t *args) { - if (n_args == 0) { - // print a general help message - printf("%s", help_text); - - } else { - // try to print something sensible about the given object - pyhelp_print_obj(args[0]); - } - - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, pyb_help); diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index c9b5480c8..100c38e4f 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -76,6 +76,8 @@ #define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_BUILTINS_EXECFILE (1) #define MICROPY_PY_BUILTINS_COMPILE (1) +#define MICROPY_PY_BUILTINS_HELP (1) +#define MICROPY_PY_BUILTINS_HELP_TEXT stmhal_help_text #define MICROPY_PY_ALL_SPECIAL_METHODS (1) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) @@ -130,7 +132,6 @@ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, -- cgit v1.2.3 From cffe00d6ab3406f4c0f1985176ccc730ba092d42 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 23 Jan 2017 14:37:49 +1100 Subject: stmhal: Add default frozen-bytecode directory and link lcd160cr driver. stmhal will now be built by default with frozen bytecode from scripts stored in the stmhal/modules/ directory. This can be disabled or changed to another directory by overridding the make variable FROZEN_MPY_DIR. --- stmhal/Makefile | 3 +++ stmhal/modules/lcd160cr.py | 1 + stmhal/modules/lcd160cr_test.py | 1 + 3 files changed, 5 insertions(+) create mode 120000 stmhal/modules/lcd160cr.py create mode 120000 stmhal/modules/lcd160cr_test.py (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index 3735172dc..9a9c50f06 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -15,6 +15,9 @@ include boards/$(BOARD)/mpconfigboard.mk # qstr definitions (must come before including py.mk) QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h $(BUILD)/modstm_qstr.h +# directory containing scripts to be frozen as bytecode +FROZEN_MPY_DIR ?= modules + # include py core make definitions include ../py/py.mk diff --git a/stmhal/modules/lcd160cr.py b/stmhal/modules/lcd160cr.py new file mode 120000 index 000000000..c0e180714 --- /dev/null +++ b/stmhal/modules/lcd160cr.py @@ -0,0 +1 @@ +../../drivers/display/lcd160cr.py \ No newline at end of file diff --git a/stmhal/modules/lcd160cr_test.py b/stmhal/modules/lcd160cr_test.py new file mode 120000 index 000000000..56f091351 --- /dev/null +++ b/stmhal/modules/lcd160cr_test.py @@ -0,0 +1 @@ +../../drivers/display/lcd160cr_test.py \ No newline at end of file -- cgit v1.2.3 From d6a2d0016793ad962a8f2b6da48f2a64de84b023 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 24 Jan 2017 16:58:50 +1100 Subject: stmhal: Add ability to have filesystem stored on external SPI flash. To use this feature a port should define MICROPY_HW_SPIFLASH_SIZE_BITS along with x_CS, x_SCK, x_MOSI, x_MISO (x=MICROPY_HW_SPIFLASH). This will then use external SPI flash on those pins instead of the internal flash. The SPI is done using the software implementation. There is currently only support for standard SPI (ie not dual or quad mode). --- stmhal/Makefile | 5 +++ stmhal/storage.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index 9a9c50f06..87236068b 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -117,6 +117,10 @@ SRC_LIB = $(addprefix lib/,\ utils/pyexec.c \ ) +DRIVERS_SRC_C = $(addprefix drivers/,\ + memory/spiflash.c \ + ) + SRC_C = \ main.c \ system_stm32.c \ @@ -256,6 +260,7 @@ endif OBJ = OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_O)) OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o)) diff --git a/stmhal/storage.c b/stmhal/storage.c index 14b504d71..60a7e9f48 100644 --- a/stmhal/storage.c +++ b/stmhal/storage.c @@ -38,6 +38,14 @@ #include "storage.h" #include "irq.h" +#if defined(MICROPY_HW_SPIFLASH_SIZE_BITS) +#define USE_INTERNAL (0) +#else +#define USE_INTERNAL (1) +#endif + +#if USE_INTERNAL + #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) #define CACHE_MEM_START_ADDR (0x10000000) // CCM data RAM, 64k @@ -158,19 +166,50 @@ static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) { return (uint8_t*)flash_addr; } +#else + +#include "drivers/memory/spiflash.h" +#include "genhdr/pins.h" + +#define FLASH_PART1_START_BLOCK (0x100) +#define FLASH_PART1_NUM_BLOCKS (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) + +static bool flash_is_initialised = false; + +STATIC const mp_spiflash_t spiflash = { + .cs = &MICROPY_HW_SPIFLASH_CS, + .spi = { + .base = {&mp_machine_soft_spi_type}, + .delay_half = MICROPY_PY_MACHINE_SPI_MIN_DELAY, + .polarity = 0, + .phase = 0, + .sck = &MICROPY_HW_SPIFLASH_SCK, + .mosi = &MICROPY_HW_SPIFLASH_MOSI, + .miso = &MICROPY_HW_SPIFLASH_MISO, + }, +}; + +#endif + void storage_init(void) { if (!flash_is_initialised) { + #if USE_INTERNAL flash_flags = 0; flash_cache_sector_id = 0; flash_tick_counter_last_write = 0; + #else + mp_spiflash_init((mp_spiflash_t*)&spiflash); + #endif flash_is_initialised = true; } + #if USE_INTERNAL // Enable the flash IRQ, which is used to also call our storage IRQ handler // It needs to go at a higher priority than all those components that rely on // the flash storage (eg higher than USB MSC). HAL_NVIC_SetPriority(FLASH_IRQn, IRQ_PRI_FLASH, IRQ_SUBPRI_FLASH); HAL_NVIC_EnableIRQ(FLASH_IRQn); + #endif } uint32_t storage_get_block_size(void) { @@ -182,6 +221,8 @@ uint32_t storage_get_block_count(void) { } void storage_irq_handler(void) { + #if USE_INTERNAL + if (!(flash_flags & FLASH_FLAG_DIRTY)) { return; } @@ -222,10 +263,14 @@ void storage_irq_handler(void) { // indicate a clean cache with LED off led_state(PYB_LED_R1, 0); } + + #endif } void storage_flush(void) { + #if USE_INTERNAL flash_cache_flush(); + #endif } static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) { @@ -264,6 +309,8 @@ static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_blo buf[15] = num_blocks >> 24; } +#if USE_INTERNAL + static uint32_t convert_block_to_flash_addr(uint32_t block) { if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) { // a block in partition 1 @@ -279,6 +326,8 @@ static uint32_t convert_block_to_flash_addr(uint32_t block) { return -1; } +#endif + bool storage_read_block(uint8_t *dest, uint32_t block) { //printf("RD %u\n", block); if (block == 0) { @@ -299,6 +348,8 @@ bool storage_read_block(uint8_t *dest, uint32_t block) { return true; } else { + #if USE_INTERNAL + // non-MBR block, get data from flash memory, possibly via cache uint32_t flash_addr = convert_block_to_flash_addr(block); if (flash_addr == -1) { @@ -308,6 +359,27 @@ bool storage_read_block(uint8_t *dest, uint32_t block) { uint8_t *src = flash_cache_get_addr_for_read(flash_addr); memcpy(dest, src, FLASH_BLOCK_SIZE); return true; + + #else + + // non-MBR block, get data from SPI flash + + if (block < FLASH_PART1_START_BLOCK || block >= FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) { + // bad block number + return false; + } + + // we must disable USB irqs to prevent MSC contention with SPI flash + uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS); + + mp_spiflash_read((mp_spiflash_t*)&spiflash, + (block - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE, FLASH_BLOCK_SIZE, dest); + + restore_irq_pri(basepri); + + return true; + + #endif } } @@ -318,6 +390,8 @@ bool storage_write_block(const uint8_t *src, uint32_t block) { return true; } else { + #if USE_INTERNAL + // non-MBR block, copy to cache uint32_t flash_addr = convert_block_to_flash_addr(block); if (flash_addr == -1) { @@ -327,6 +401,27 @@ bool storage_write_block(const uint8_t *src, uint32_t block) { uint8_t *dest = flash_cache_get_addr_for_write(flash_addr); memcpy(dest, src, FLASH_BLOCK_SIZE); return true; + + #else + + // non-MBR block, write to SPI flash + + if (block < FLASH_PART1_START_BLOCK || block >= FLASH_PART1_START_BLOCK + FLASH_PART1_NUM_BLOCKS) { + // bad block number + return false; + } + + // we must disable USB irqs to prevent MSC contention with SPI flash + uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS); + + int ret = mp_spiflash_write((mp_spiflash_t*)&spiflash, + (block - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE, FLASH_BLOCK_SIZE, src); + + restore_irq_pri(basepri); + + return ret == 0; + + #endif } } -- cgit v1.2.3 From 84c614e7290804e8b8d7ebff03ca35a4a4c5fcf1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 23:11:59 +1100 Subject: stmhal: Convert to use VFS sub-system and new ooFatFs component. This patch makes the following configuration changes: - MICROPY_FSUSERMOUNT is disabled, removing old mounting infrastructure - MICROPY_VFS is enabled, giving new VFS sub-system - MICROPY_VFS_FAT is enabled, giving uos.VfsFat type - MICROPY_FATFS_OO is enabled, to use new ooFatFs lib, R0.12b User facing API should be almost unchanged. Most notable changes are removal of os.mkfs (use os.VfsFat.mkfs instead) and pyb.mount doesn't allow unmounting by passing None as the device. --- stmhal/Makefile | 8 +- stmhal/builtin_open.c | 30 ------ stmhal/fatfs_port.c | 15 +-- stmhal/import.c | 35 ------ stmhal/main.c | 99 +++++++++-------- stmhal/modmachine.c | 20 ++-- stmhal/modpyb.c | 8 +- stmhal/moduos.c | 294 ++++---------------------------------------------- stmhal/mpconfigport.h | 16 ++- stmhal/sdcard.c | 5 +- stmhal/storage.c | 5 +- 11 files changed, 114 insertions(+), 421 deletions(-) delete mode 100644 stmhal/builtin_open.c delete mode 100644 stmhal/import.c (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index 87236068b..3431c3105 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -26,7 +26,7 @@ CMSIS_DIR=cmsis HAL_DIR=hal/$(MCU_SERIES) USBDEV_DIR=usbdev #USBHOST_DIR=usbhost -FATFS_DIR=lib/fatfs +FATFS_DIR=lib/oofatfs DFU=../tools/dfu.py # may need to prefix dfu-util with sudo USE_PYDFU ?= 1 @@ -109,8 +109,8 @@ SRC_LIB = $(addprefix lib/,\ libm/sf_erf.c \ libm/wf_lgamma.c \ libm/wf_tgamma.c \ - fatfs/ff.c \ - fatfs/option/ccsbcs.c \ + oofatfs/ff.c \ + oofatfs/option/unicode.c \ mp-readline/readline.c \ netutils/netutils.c \ timeutils/timeutils.c \ @@ -159,14 +159,12 @@ SRC_C = \ modutime.c \ modusocket.c \ modnetwork.c \ - import.c \ extint.c \ usrsw.c \ rng.c \ rtc.c \ flash.c \ storage.c \ - builtin_open.c \ sdcard.c \ fatfs_port.c \ lcd.c \ diff --git a/stmhal/builtin_open.c b/stmhal/builtin_open.c deleted file mode 100644 index 56b98ea61..000000000 --- a/stmhal/builtin_open.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * 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. - */ - -#include "py/runtime.h" -#include "extmod/vfs_fat.h" - -MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, fatfs_builtin_open); diff --git a/stmhal/fatfs_port.c b/stmhal/fatfs_port.c index 421ceddba..17ec3f726 100644 --- a/stmhal/fatfs_port.c +++ b/stmhal/fatfs_port.c @@ -24,23 +24,10 @@ * THE SOFTWARE. */ -#include "py/mphal.h" #include "py/runtime.h" -#include "lib/fatfs/ff.h" /* FatFs lower layer API */ -#include "lib/fatfs/diskio.h" /* FatFs lower layer API */ +#include "lib/oofatfs/ff.h" #include "rtc.h" -const PARTITION VolToPart[MICROPY_FATFS_VOLUMES] = { - {0, 1}, // Logical drive 0 ==> Physical drive 0, 1st partition - {1, 0}, // Logical drive 1 ==> Physical drive 1 (auto detection) - {2, 0}, // Logical drive 2 ==> Physical drive 2 (auto detection) - {3, 0}, // Logical drive 3 ==> Physical drive 3 (auto detection) - /* - {0, 2}, // Logical drive 2 ==> Physical drive 0, 2nd partition - {0, 3}, // Logical drive 3 ==> Physical drive 0, 3rd partition - */ -}; - DWORD get_fattime(void) { rtc_init_finalise(); RTC_TimeTypeDef time; diff --git a/stmhal/import.c b/stmhal/import.c deleted file mode 100644 index 2bc282e7b..000000000 --- a/stmhal/import.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * 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. - */ - -#include - -#include "py/lexer.h" -#include "lib/fatfs/ff.h" -#include "extmod/vfs_fat.h" - -mp_import_stat_t mp_import_stat(const char *path) { - return fat_vfs_import_stat(NULL, path); -} diff --git a/stmhal/main.c b/stmhal/main.c index 78afe54ef..1918658b7 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -37,7 +37,8 @@ #include "py/mphal.h" #include "lib/utils/pyexec.h" -#include "lib/fatfs/ff.h" +#include "lib/oofatfs/ff.h" +#include "extmod/vfs.h" #include "extmod/fsusermount.h" #include "systick.h" @@ -67,6 +68,7 @@ void SystemClock_Config(void); fs_user_mount_t fs_user_mount_flash; +mp_vfs_mount_t mp_vfs_mount_flash; void flash_error(int n) { for (int i = 0; i < n; i++) { @@ -168,17 +170,14 @@ static const char fresh_readme_txt[] = // avoid inlining to avoid stack usage within main() MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) { // init the vfs object - fs_user_mount_t *vfs = &fs_user_mount_flash; - vfs->str = "/flash"; - vfs->len = 6; - vfs->flags = 0; - pyb_flash_init_vfs(vfs); - - // put the flash device in slot 0 (it will be unused at this point) - MP_STATE_PORT(fs_user_mount)[0] = vfs; + fs_user_mount_t *vfs_fat = &fs_user_mount_flash; + vfs_fat->str = NULL; + vfs_fat->len = 0; + vfs_fat->flags = 0; + pyb_flash_init_vfs(vfs_fat); // try to mount the flash - FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1); + FRESULT res = f_mount(&vfs_fat->fatfs); if (reset_mode == 3 || res == FR_NO_FILESYSTEM) { // no filesystem, or asked to reset it, so create a fresh one @@ -187,33 +186,33 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) { led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); - res = f_mkfs("/flash", 0, 0); + uint8_t working_buf[_MAX_SS]; + res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); if (res == FR_OK) { // success creating fresh LFS } else { printf("PYB: can't create flash filesystem\n"); - MP_STATE_PORT(fs_user_mount)[0] = NULL; return; } // set label - f_setlabel("/flash/pybflash"); + f_setlabel(&vfs_fat->fatfs, "pybflash"); // create empty main.py FIL fp; - f_open(&fp, "/flash/main.py", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&vfs_fat->fatfs, &fp, "/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // create .inf driver file - f_open(&fp, "/flash/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&vfs_fat->fatfs, &fp, "/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n); f_close(&fp); // create readme file - f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&vfs_fat->fatfs, &fp, "/README.txt", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n); f_close(&fp); @@ -224,30 +223,25 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) { // mount sucessful } else { printf("PYB: can't mount flash\n"); - MP_STATE_PORT(fs_user_mount)[0] = NULL; return; } + // mount the flash device (there should be no other devices mounted at this point) + mp_vfs_mount_t *vfs = &mp_vfs_mount_flash; + vfs->str = "/flash"; + vfs->len = 6; + vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); + vfs->next = NULL; + MP_STATE_VM(vfs_mount_table) = vfs; + // The current directory is used as the boot up directory. // It is set to the internal flash filesystem by default. - f_chdrive("/flash"); + MP_STATE_PORT(vfs_cur) = vfs; // Make sure we have a /flash/boot.py. Create it if needed. FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - res = f_stat("/flash/boot.py", &fno); - if (res == FR_OK) { - if (fno.fattrib & AM_DIR) { - // exists as a directory - // TODO handle this case - // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation - } else { - // exists as a file, good! - } - } else { + res = f_stat(&vfs_fat->fatfs, "/boot.py", &fno); + if (res != FR_OK) { // doesn't exist, create fresh file // LED on to indicate creation of boot.py @@ -255,7 +249,7 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) { uint32_t start_tick = HAL_GetTick(); FIL fp; - f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&vfs_fat->fatfs, &fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes @@ -485,24 +479,29 @@ soft_reset: // if an SD card is present then mount it on /sd/ if (sdcard_is_present()) { // create vfs object - fs_user_mount_t *vfs = m_new_obj_maybe(fs_user_mount_t); - if (vfs == NULL) { + fs_user_mount_t *vfs_fat = m_new_obj_maybe(fs_user_mount_t); + mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t); + if (vfs == NULL || vfs_fat == NULL) { goto no_mem_for_sd; } - vfs->str = "/sd"; - vfs->len = 3; - vfs->flags = FSUSER_FREE_OBJ; - sdcard_init_vfs(vfs); - - // put the sd device in slot 1 (it will be unused at this point) - MP_STATE_PORT(fs_user_mount)[1] = vfs; + vfs_fat->str = NULL; + vfs_fat->len = 0; + vfs_fat->flags = FSUSER_FREE_OBJ; + sdcard_init_vfs(vfs_fat); - FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1); + FRESULT res = f_mount(&vfs_fat->fatfs); if (res != FR_OK) { printf("PYB: can't mount SD card\n"); - MP_STATE_PORT(fs_user_mount)[1] = NULL; - m_del_obj(fs_user_mount_t, vfs); + m_del_obj(fs_user_mount_t, vfs_fat); + m_del_obj(mp_vfs_mount_t, vfs); } else { + // mount the sd device after the internal flash + vfs->str = "/sd"; + vfs->len = 3; + vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); + vfs->next = NULL; + MP_STATE_VM(vfs_mount_table)->next = vfs; + // TODO these should go before the /flash entries in the path mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib)); @@ -520,7 +519,7 @@ soft_reset: #endif { // use SD card as current directory - f_chdrive("/sd"); + MP_STATE_PORT(vfs_cur) = vfs; } } no_mem_for_sd:; @@ -534,8 +533,8 @@ soft_reset: // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py if (reset_mode == 1 || reset_mode == 3) { const char *boot_py = "boot.py"; - FRESULT res = f_stat(boot_py, NULL); - if (res == FR_OK) { + mp_import_stat_t stat = mp_import_stat(boot_py); + if (stat == MP_IMPORT_STAT_FILE) { int ret = pyexec_file(boot_py); if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; @@ -597,8 +596,8 @@ soft_reset: } else { main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main)); } - FRESULT res = f_stat(main_py, NULL); - if (res == FR_OK) { + mp_import_stat_t stat = mp_import_stat(main_py); + if (stat == MP_IMPORT_STAT_FILE) { int ret = pyexec_file(main_py); if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; diff --git a/stmhal/modmachine.c b/stmhal/modmachine.c index 7a337adb6..aec8e29c5 100644 --- a/stmhal/modmachine.c +++ b/stmhal/modmachine.c @@ -25,6 +25,7 @@ */ #include +#include #include "modmachine.h" #include "py/gc.h" @@ -34,8 +35,9 @@ #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" #include "lib/utils/pyexec.h" -#include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" +#include "lib/oofatfs/ff.h" +#include "extmod/vfs.h" +#include "extmod/fsusermount.h" #include "gccollect.h" #include "irq.h" #include "rng.h" @@ -144,10 +146,16 @@ STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) { // free space on flash { - DWORD nclst; - FATFS *fatfs; - f_getfree("/flash", &nclst, &fatfs); - printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512)); + for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + if (strncmp("/flash", vfs->str, vfs->len) == 0) { + // assumes that it's a FatFs filesystem + fs_user_mount_t *vfs_fat = MP_OBJ_TO_PTR(vfs->obj); + DWORD nclst; + f_getfree(&vfs_fat->fatfs, &nclst); + printf("LFS free: %u bytes\n", (uint)(nclst * vfs_fat->fatfs.csize * 512)); + break; + } + } } if (n_args == 1) { diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index e602234fc..53b4335e7 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -35,8 +35,8 @@ #include "py/gc.h" #include "py/builtin.h" #include "lib/utils/pyexec.h" -#include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" #include "gccollect.h" #include "irq.h" #include "systick.h" @@ -61,7 +61,7 @@ #include "usb.h" #include "portmodules.h" #include "modmachine.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs.h" #include "extmod/utime_mphal.h" /// \function millis() @@ -166,7 +166,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&mp_utime_sleep_ms_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&mp_utime_sleep_us_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&mp_vfs_mount_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type }, diff --git a/stmhal/moduos.c b/stmhal/moduos.c index 158e530ff..976d7f089 100644 --- a/stmhal/moduos.c +++ b/stmhal/moduos.c @@ -31,15 +31,14 @@ #include "py/runtime.h" #include "py/objtuple.h" #include "py/objstr.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#include "extmod/vfs.h" +#include "extmod/vfs_fat.h" #include "genhdr/mpversion.h" -#include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" #include "timeutils.h" #include "rng.h" #include "uart.h" -#include "extmod/vfs_fat.h" -#include "sdcard.h" -#include "extmod/fsusermount.h" #include "portmodules.h" /// \module os - basic "operating system" services @@ -80,263 +79,13 @@ STATIC mp_obj_t os_uname(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); -/// \function chdir(path) -/// Change current directory. -STATIC mp_obj_t os_chdir(mp_obj_t path_in) { - const char *path; - path = mp_obj_str_get_str(path_in); - - FRESULT res = f_chdrive(path); - - if (res == FR_OK) { - res = f_chdir(path); - } - - if (res != FR_OK) { - // TODO should be mp_type_FileNotFoundError - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path)); - } - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir); - -/// \function getcwd() -/// Get the current directory. -STATIC mp_obj_t os_getcwd(void) { - char buf[MICROPY_ALLOC_PATH_MAX + 1]; - FRESULT res = f_getcwd(buf, sizeof buf); - - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); - } - - return mp_obj_new_str(buf, strlen(buf), false); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_getcwd_obj, os_getcwd); - -/// \function listdir([dir]) -/// With no argument, list the current directory. Otherwise list the given directory. -STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) { - bool is_str_type = true; - const char *path; - if (n_args == 1) { - if (mp_obj_get_type(args[0]) == &mp_type_bytes) { - is_str_type = false; - } - path = mp_obj_str_get_str(args[0]); - } else { - path = ""; - } - - // "hack" to list root directory - if (path[0] == '/' && path[1] == '\0') { - mp_obj_t dir_list = mp_obj_new_list(0, NULL); - for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL) { - mp_obj_list_append(dir_list, mp_obj_new_str(vfs->str + 1, vfs->len - 1, false)); - } - } - return dir_list; - } - - return fat_vfs_listdir(path, is_str_type); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir); - -/// \function mkdir(path) -/// Create a new directory. -STATIC mp_obj_t os_mkdir(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - FRESULT res = f_mkdir(path); - switch (res) { - case FR_OK: - return mp_const_none; - case FR_EXIST: - // TODO should be FileExistsError - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path)); - default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path)); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir); - -/// \function remove(path) -/// Remove a file. -STATIC mp_obj_t os_remove(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - // TODO check that path is actually a file before trying to unlink it - FRESULT res = f_unlink(path); - switch (res) { - case FR_OK: - return mp_const_none; - default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path)); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove); - -/// \function rename(old_path, new_path) -/// Rename a file -STATIC mp_obj_t os_rename(mp_obj_t path_in, mp_obj_t path_out) { - const char *old_path = mp_obj_str_get_str(path_in); - const char *new_path = mp_obj_str_get_str(path_out); - FRESULT res = f_rename(old_path, new_path); - if (res == FR_EXIST) { - // if new_path exists then try removing it - res = f_unlink(new_path); - if (res == FR_OK) { - // try to rename again - res = f_rename(old_path, new_path); - } - } - switch (res) { - case FR_OK: - return mp_const_none; - default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error renaming file '%s' to '%s'", old_path, new_path)); - } - -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename); - -/// \function rmdir(path) -/// Remove a directory. -STATIC mp_obj_t os_rmdir(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - // TODO check that path is actually a directory before trying to unlink it - FRESULT res = f_unlink(path); - switch (res) { - case FR_OK: - return mp_const_none; - default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing directory '%s'", path)); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir); - -// Checks for path equality, ignoring trailing slashes: -// path_equal(/, /) -> true -// path_equal(/flash//, /flash) -> true -// second argument must be in canonical form (meaning no trailing slash, unless it's just /) -STATIC bool path_equal(const char *path, const char *path_canonical) { - for (; *path_canonical != '\0' && *path == *path_canonical; ++path, ++path_canonical) { - } - if (*path_canonical != '\0') { - return false; - } - for (; *path == '/'; ++path) { - } - return *path == '\0'; -} - -/// \function stat(path) -/// Get the status of a file or directory. -STATIC mp_obj_t os_stat(mp_obj_t path_in) { - const char *path = mp_obj_str_get_str(path_in); - - FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - - FRESULT res; - if (path_equal(path, "/")) { - // stat root directory - fno.fsize = 0; - fno.fdate = 0; - fno.ftime = 0; - fno.fattrib = AM_DIR; - } else { - res = FR_NO_PATH; - for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && path_equal(path, vfs->str)) { - // stat mounted device directory - fno.fsize = 0; - fno.fdate = 0; - fno.ftime = 0; - fno.fattrib = AM_DIR; - res = FR_OK; - } - } - if (res == FR_NO_PATH) { - // stat normal file - res = f_stat(path, &fno); - } - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); - } - } - - mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL); - mp_int_t mode = 0; - if (fno.fattrib & AM_DIR) { - mode |= 0x4000; // stat.S_IFDIR - } else { - mode |= 0x8000; // stat.S_IFREG - } - mp_int_t seconds = timeutils_seconds_since_2000( - 1980 + ((fno.fdate >> 9) & 0x7f), - (fno.fdate >> 5) & 0x0f, - fno.fdate & 0x1f, - (fno.ftime >> 11) & 0x1f, - (fno.ftime >> 5) & 0x3f, - 2 * (fno.ftime & 0x1f) - ); - t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode - t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino - t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev - t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink - t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid - t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid - t->items[6] = MP_OBJ_NEW_SMALL_INT(fno.fsize); // st_size - t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime - t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime - t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime - - return t; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat); - -STATIC mp_obj_t os_statvfs(mp_obj_t path_in) { - const char *path = mp_obj_str_get_str(path_in); - - DWORD nclst; - FATFS *fatfs; - FRESULT res = f_getfree(path, &nclst, &fatfs); - if (res != FR_OK) { - goto error; - } - - mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL); - - t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * 512); // f_bsize - block size - t->items[1] = t->items[0]; // f_frsize - fragment size - t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2) * fatfs->csize); // f_blocks - total number of blocks - t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree - number of free blocks - t->items[4] = t->items[3]; // f_bavail - free blocks avail to unpriviledged users - t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files - # inodes - t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree - # free inodes - t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail - # free inodes avail to unpriviledges users - t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags - t->items[9] = MP_OBJ_NEW_SMALL_INT(_MAX_LFN); // f_namemax - - return t; - -error: - mp_raise_OSError(fresult_to_errno_table[res]); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs); - /// \function sync() /// Sync all filesystems. STATIC mp_obj_t os_sync(void) { - disk_ioctl(0, CTRL_SYNC, NULL); - disk_ioctl(1, CTRL_SYNC, NULL); - disk_ioctl(2, CTRL_SYNC, NULL); + for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + // this assumes that vfs->obj is fs_user_mount_t with block device functions + disk_ioctl(MP_OBJ_TO_PTR(vfs->obj), CTRL_SYNC, NULL); + } return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_0(mod_os_sync_obj, os_sync); @@ -384,16 +133,17 @@ STATIC const mp_map_elem_t os_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_uname), (mp_obj_t)&os_uname_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&os_chdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&os_getcwd_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&os_listdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_rename),(mp_obj_t)&os_rename_obj}, - { MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), (mp_obj_t)&os_statvfs_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove + { MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&mp_vfs_chdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&mp_vfs_chdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&mp_vfs_getcwd_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&mp_vfs_listdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&mp_vfs_mkdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&mp_vfs_remove_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_rename),(mp_obj_t)&mp_vfs_rename_obj}, + { MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&mp_vfs_rmdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&mp_vfs_stat_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), (mp_obj_t)&mp_vfs_statvfs_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&mp_vfs_remove_obj }, // unlink aliases to remove { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj }, @@ -406,9 +156,9 @@ STATIC const mp_map_elem_t os_module_globals_table[] = { // these are MicroPython extensions { MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&mod_os_dupterm_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_umount), (mp_obj_t)&fsuser_umount_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mkfs), (mp_obj_t)&fsuser_mkfs_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&mp_vfs_mount_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_umount), (mp_obj_t)&mp_vfs_umount_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_VfsFat), (mp_obj_t)&mp_fat_vfs_type }, }; STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 80cc1014b..53369a182 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -52,7 +52,7 @@ #define MICROPY_OPT_MPZ_BITWISE (1) // Python internal features -#define MICROPY_READER_FATFS (1) +#define MICROPY_READER_VFS (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_STACK_CHECK (1) @@ -69,6 +69,8 @@ #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_USE_INTERNAL_ERRNO (1) +#define MICROPY_VFS (1) +#define MICROPY_VFS_FAT (1) // control over Python builtins #define MICROPY_PY_FUNCTION_ATTRS (1) @@ -133,13 +135,21 @@ #endif // fatfs configuration used in ffconf.h +#define MICROPY_FATFS_OO (1) #define MICROPY_FATFS_ENABLE_LFN (1) #define MICROPY_FATFS_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ #define MICROPY_FATFS_USE_LABEL (1) #define MICROPY_FATFS_RPATH (2) -#define MICROPY_FATFS_VOLUMES (4) #define MICROPY_FATFS_MULTI_PARTITION (1) -#define MICROPY_FSUSERMOUNT (1) + +// TODO these should be generic, not bound to fatfs +#define mp_type_fileio fatfs_type_fileio +#define mp_type_textio fatfs_type_textio + +// use vfs's functions for import stat and builtin open +#define mp_import_stat mp_vfs_import_stat +#define mp_builtin_open mp_vfs_open +#define mp_builtin_open_obj mp_vfs_open_obj // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ diff --git a/stmhal/sdcard.c b/stmhal/sdcard.c index a93b2b056..52ede492b 100644 --- a/stmhal/sdcard.c +++ b/stmhal/sdcard.c @@ -28,7 +28,8 @@ #include "py/nlr.h" #include "py/runtime.h" -#include "lib/fatfs/ff.h" +#include "lib/oofatfs/ff.h" +#include "extmod/vfs_fat.h" #include "extmod/fsusermount.h" #include "mphalport.h" @@ -444,7 +445,9 @@ const mp_obj_type_t pyb_sdcard_type = { }; void sdcard_init_vfs(fs_user_mount_t *vfs) { + vfs->base.type = &mp_fat_vfs_type; vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->fatfs.drv = vfs; vfs->readblocks[0] = (mp_obj_t)&pyb_sdcard_readblocks_obj; vfs->readblocks[1] = (mp_obj_t)&pyb_sdcard_obj; vfs->readblocks[2] = (mp_obj_t)sdcard_read_blocks; // native version diff --git a/stmhal/storage.c b/stmhal/storage.c index 60a7e9f48..6130d6fb8 100644 --- a/stmhal/storage.c +++ b/stmhal/storage.c @@ -29,7 +29,8 @@ #include "py/obj.h" #include "py/runtime.h" -#include "lib/fatfs/ff.h" +#include "lib/oofatfs/ff.h" +#include "extmod/vfs_fat.h" #include "extmod/fsusermount.h" #include "systick.h" @@ -504,7 +505,9 @@ const mp_obj_type_t pyb_flash_type = { }; void pyb_flash_init_vfs(fs_user_mount_t *vfs) { + vfs->base.type = &mp_fat_vfs_type; vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->fatfs.drv = vfs; vfs->readblocks[0] = (mp_obj_t)&pyb_flash_readblocks_obj; vfs->readblocks[1] = (mp_obj_t)&pyb_flash_obj; vfs->readblocks[2] = (mp_obj_t)storage_read_blocks; // native version -- cgit v1.2.3 From 882ec01e42227445a63f6d5b38cac14d8635f2ad Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 31 Jan 2017 13:59:53 +1100 Subject: stmhal: Initial implementation of multithreading, currently disabled. This patch brings the _thread module to stmhal/pyboard. There is a very simple round-robin thread scheduler, which is disabled if there is only one thread (for efficiency when threading is not used). The scheduler currently switches threads at a rate of 250Hz using the systick timer and the pend-SV interrupt. The GIL is disabled so one must be careful to use lock objects to prevent concurrent access of objects. The threading is disabled by default, one can enabled it with the config option MICROPY_PY_THREAD to test it out. --- stmhal/Makefile | 2 + stmhal/gccollect.c | 11 +++++ stmhal/main.c | 19 ++++++-- stmhal/mpconfigport.h | 2 + stmhal/mpthreadport.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ stmhal/mpthreadport.h | 45 ++++++++++++++++++ stmhal/pendsv.c | 30 ++++++++++++ stmhal/pybthread.c | 97 +++++++++++++++++++++++++++++++++++++++ stmhal/pybthread.h | 62 +++++++++++++++++++++++++ stmhal/stm32_it.c | 6 +++ 10 files changed, 392 insertions(+), 5 deletions(-) create mode 100644 stmhal/mpthreadport.c create mode 100644 stmhal/mpthreadport.h create mode 100644 stmhal/pybthread.c create mode 100644 stmhal/pybthread.h (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index 3431c3105..a83f2c495 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -131,9 +131,11 @@ SRC_C = \ usbd_hid_interface.c \ usbd_msc_storage.c \ mphalport.c \ + mpthreadport.c \ irq.c \ pendsv.c \ systick.c \ + pybthread.c \ timer.c \ led.c \ pin.c \ diff --git a/stmhal/gccollect.c b/stmhal/gccollect.c index de76b71ac..8a7bbf27f 100644 --- a/stmhal/gccollect.c +++ b/stmhal/gccollect.c @@ -27,8 +27,10 @@ #include #include +#include "py/mpstate.h" #include "py/obj.h" #include "py/gc.h" +#include "py/mpthread.h" #include "gccollect.h" #include "systick.h" @@ -48,7 +50,16 @@ void gc_collect(void) { mp_uint_t sp = gc_helper_get_regs_and_sp(regs); // trace the stack, including the registers (since they live on the stack in this function) + #if MICROPY_PY_THREAD + gc_collect_root((void**)sp, ((uint32_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t)); + #else gc_collect_root((void**)sp, ((uint32_t)&_ram_end - sp) / sizeof(uint32_t)); + #endif + + // trace root pointers from any threads + #if MICROPY_PY_THREAD + mp_thread_gc_others(); + #endif // end the GC gc_collect_end(); diff --git a/stmhal/main.c b/stmhal/main.c index 722ca41b4..4ffa0d9ba 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -43,6 +43,7 @@ #include "systick.h" #include "pendsv.h" +#include "pybthread.h" #include "gccollect.h" #include "readline.h" #include "modmachine.h" @@ -67,6 +68,7 @@ void SystemClock_Config(void); +pyb_thread_t pyb_thread_main; fs_user_mount_t fs_user_mount_flash; mp_vfs_mount_t mp_vfs_mount_flash; @@ -419,11 +421,6 @@ STATIC uint update_reset_mode(uint reset_mode) { int main(void) { // TODO disable JTAG - // 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(); - mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024); - /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec @@ -457,6 +454,7 @@ int main(void) { #endif // basic sub-system init + pyb_thread_init(&pyb_thread_main); pendsv_init(); led_init(); #if MICROPY_HW_HAS_SWITCH @@ -502,6 +500,17 @@ soft_reset: storage_init(); } + // Python threading init + #if MICROPY_PY_THREAD + mp_thread_init(); + #endif + + // Stack limit should be less than real stack size, so we have a chance + // to recover from limit hit. (Limit is measured in bytes.) + // Note: stack control relies on main thread being initialised above + mp_stack_ctrl_init(); + mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024); + // GC init gc_init(&_heap_start, &_heap_end); diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 873215458..083f75418 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -102,6 +102,8 @@ #define MICROPY_PY_SYS_PLATFORM "pyboard" #endif #define MICROPY_PY_UERRNO (1) +#define MICROPY_PY_THREAD (0) +#define MICROPY_PY_THREAD_GIL (0) // extended modules #define MICROPY_PY_UCTYPES (1) diff --git a/stmhal/mpthreadport.c b/stmhal/mpthreadport.c new file mode 100644 index 000000000..97c19647c --- /dev/null +++ b/stmhal/mpthreadport.c @@ -0,0 +1,123 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * + * 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. + */ + +#include + +#include "py/mpconfig.h" +#include "py/mpstate.h" +#include "py/gc.h" +#include "py/mpthread.h" +#include "gccollect.h" + +#if MICROPY_PY_THREAD + +// the mutex controls access to the linked list +STATIC mp_thread_mutex_t thread_mutex; + +void mp_thread_init(void) { + mp_thread_mutex_init(&thread_mutex); + mp_thread_set_state(&mp_state_ctx.thread); +} + +void mp_thread_gc_others(void) { + mp_thread_mutex_lock(&thread_mutex, 1); + gc_collect_root((void**)&pyb_thread_cur, 1); + for (pyb_thread_t *th = pyb_thread_cur;; th = th->next) { + gc_collect_root(&th->arg, 1); + if (th != pyb_thread_cur) { + gc_collect_root(th->stack, th->stack_len); + } + if (th->next == pyb_thread_cur) { + break; + } + } + mp_thread_mutex_unlock(&thread_mutex); +} + +void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) { + if (*stack_size == 0) { + *stack_size = 4096; // default stack size + } else if (*stack_size < 2048) { + *stack_size = 2048; // minimum stack size + } + + // round stack size to a multiple of the word size + size_t stack_len = *stack_size / sizeof(uint32_t); + *stack_size = stack_len * sizeof(uint32_t); + + // allocate stack and linked-list node (must be done outside thread_mutex lock) + uint32_t *stack = m_new(uint32_t, stack_len); + pyb_thread_t *th = m_new_obj(pyb_thread_t); + + mp_thread_mutex_lock(&thread_mutex, 1); + + // create thread + uint32_t id = pyb_thread_new(th, stack, stack_len, entry, arg); + if (id == 0) { + mp_thread_mutex_unlock(&thread_mutex); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread")); + } + + mp_thread_mutex_unlock(&thread_mutex); + + // adjust stack_size to provide room to recover from hitting the limit + *stack_size -= 1024; +} + +void mp_thread_start(void) { +} + +void mp_thread_finish(void) { +} + +void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { + *mutex = 0; +} + +int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { + uint32_t irq_state = disable_irq(); + if (*mutex) { + // mutex is locked + if (!wait) { + enable_irq(irq_state); + return 0; // failed to lock mutex + } + while (*mutex) { + enable_irq(irq_state); + pyb_thread_yield(); + irq_state = disable_irq(); + } + } + *mutex = 1; + enable_irq(irq_state); + return 1; // have mutex +} + +void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) { + *mutex = 0; +} + +#endif // MICROPY_PY_THREAD diff --git a/stmhal/mpthreadport.h b/stmhal/mpthreadport.h new file mode 100644 index 000000000..4fef323eb --- /dev/null +++ b/stmhal/mpthreadport.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Damien P. George + * + * 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_STMHAL_MPTHREADPORT_H__ +#define __MICROPY_INCLUDED_STMHAL_MPTHREADPORT_H__ + +#include "py/mpthread.h" +#include "pybthread.h" + +typedef uint32_t mp_thread_mutex_t; + +void mp_thread_init(void); +void mp_thread_gc_others(void); + +static inline void mp_thread_set_state(void *state) { + pyb_thread_set_local(state); +} + +static inline struct _mp_state_thread_t *mp_thread_get_state(void) { + return pyb_thread_get_local(); +} + +#endif // __MICROPY_INCLUDED_STMHAL_MPTHREADPORT_H__ diff --git a/stmhal/pendsv.c b/stmhal/pendsv.c index 61fe95439..e6df84b29 100644 --- a/stmhal/pendsv.c +++ b/stmhal/pendsv.c @@ -89,6 +89,35 @@ void pendsv_isr_handler(void) { // sp[1]: 0xfffffff9 // sp[0]: ? +#if MICROPY_PY_THREAD + __asm volatile ( + "ldr r1, pendsv_object_ptr\n" + "ldr r0, [r1]\n" + "cmp r0, 0\n" + "beq .no_obj\n" + "str r0, [sp, #0]\n" // store to r0 on stack + "mov r0, #0\n" + "str r0, [r1]\n" // clear pendsv_object + "ldr r0, nlr_jump_ptr\n" + "str r0, [sp, #24]\n" // store to pc on stack + "bx lr\n" // return from interrupt; will return to nlr_jump + + ".no_obj:\n" // pendsv_object==NULL + "push {r4-r11, lr}\n" + "vpush {s16-s31}\n" + "mov r0, sp\n" // pass sp to save + "mov r4, lr\n" // save lr because we are making a call + "bl pyb_thread_next\n" // get next thread to execute + "mov lr, r4\n" // restore lr + "mov sp, r0\n" // switch stacks + "vpop {s16-s31}\n" + "pop {r4-r11, lr}\n" + "bx lr\n" // return from interrupt; will return to new thread + ".align 2\n" + "pendsv_object_ptr: .word pendsv_object\n" + "nlr_jump_ptr: .word nlr_jump\n" + ); +#else __asm volatile ( "ldr r0, pendsv_object_ptr\n" "ldr r0, [r0]\n" @@ -108,6 +137,7 @@ void pendsv_isr_handler(void) { "pendsv_object_ptr: .word pendsv_object\n" "nlr_jump_ptr: .word nlr_jump\n" ); +#endif /* uint32_t x[2] = {0x424242, 0xdeaddead}; diff --git a/stmhal/pybthread.c b/stmhal/pybthread.c new file mode 100644 index 000000000..9f9f82a45 --- /dev/null +++ b/stmhal/pybthread.c @@ -0,0 +1,97 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Damien P. George + * + * 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. + */ + +#include +#include + +#include "py/obj.h" +#include "gccollect.h" +#include "irq.h" +#include "pybthread.h" + +#if MICROPY_PY_THREAD + +int pyb_thread_enabled; +pyb_thread_t *pyb_thread_cur; + +void pyb_thread_init(pyb_thread_t *thread) { + pyb_thread_cur = thread; + pyb_thread_cur->sp = NULL; // will be set when this thread switches out + pyb_thread_cur->local_state = 0; // will be set by mp_thread_init + pyb_thread_cur->arg = NULL; + pyb_thread_cur->stack = &_heap_end; + pyb_thread_cur->stack_len = ((uint32_t)&_estack - (uint32_t)&_heap_end) / sizeof(uint32_t); + pyb_thread_cur->prev = thread; + pyb_thread_cur->next = thread; +} + +STATIC void pyb_thread_terminate(void) { + uint32_t irq_state = raise_irq_pri(IRQ_PRI_PENDSV); + pyb_thread_cur->prev->next = pyb_thread_cur->next; + pyb_thread_cur->next->prev = pyb_thread_cur->prev; + if (pyb_thread_cur->next == pyb_thread_cur->prev) { + pyb_thread_enabled = 0; + } + restore_irq_pri(irq_state); + pyb_thread_yield(); // should not return +} + +uint32_t pyb_thread_new(pyb_thread_t *thread, void *stack, size_t stack_len, void *entry, void *arg) { + uint32_t *stack_top = (uint32_t*)stack + stack_len; // stack is full descending + *--stack_top = 0x01000000; // xPSR (thumb bit set) + *--stack_top = (uint32_t)entry & 0xfffffffe; // pc (must have bit 0 cleared, even for thumb code) + *--stack_top = (uint32_t)pyb_thread_terminate; // lr + *--stack_top = 0; // r12 + *--stack_top = 0; // r3 + *--stack_top = 0; // r2 + *--stack_top = 0; // r1 + *--stack_top = (uint32_t)arg; // r0 + *--stack_top = 0xfffffff9; // lr (return to thread mode, non-FP, use MSP) + stack_top -= 8; // r4-r11 + stack_top -= 16; // s16-s31 (we assume all threads use FP registers) + thread->sp = stack_top; + thread->local_state = 0; + thread->arg = arg; + thread->stack = stack; + thread->stack_len = stack_len; + uint32_t irq_state = raise_irq_pri(IRQ_PRI_PENDSV); + pyb_thread_enabled = 1; + thread->next = pyb_thread_cur->next; + thread->prev = pyb_thread_cur; + pyb_thread_cur->next->prev = thread; + pyb_thread_cur->next = thread; + restore_irq_pri(irq_state); + return (uint32_t)thread; // success +} + +// should only be called from pendsv_isr_handler +void *pyb_thread_next(void *sp) { + pyb_thread_cur->sp = sp; + pyb_thread_cur = pyb_thread_cur->next; + return pyb_thread_cur->sp; +} + +#endif // MICROPY_PY_THREAD diff --git a/stmhal/pybthread.h b/stmhal/pybthread.h new file mode 100644 index 000000000..d4310c66a --- /dev/null +++ b/stmhal/pybthread.h @@ -0,0 +1,62 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Damien P. George + * + * 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_STMHAL_PYBTHREAD_H +#define MICROPY_INCLUDED_STMHAL_PYBTHREAD_H + +typedef struct _pyb_thread_t { + void *sp; + uint32_t local_state; + void *arg; // thread Python args, a GC root pointer + void *stack; // pointer to the stack + size_t stack_len; // number of words in the stack + struct _pyb_thread_t *prev; + struct _pyb_thread_t *next; +} pyb_thread_t; + +extern int pyb_thread_enabled; +extern pyb_thread_t *pyb_thread_cur; + +void pyb_thread_init(pyb_thread_t *th); +uint32_t pyb_thread_new(pyb_thread_t *th, void *stack, size_t stack_len, void *entry, void *arg); + +static inline uint32_t pyb_thread_get_id(void) { + return (uint32_t)pyb_thread_cur; +} + +static inline void pyb_thread_set_local(void *value) { + pyb_thread_cur->local_state = (uint32_t)value; +} + +static inline void *pyb_thread_get_local(void) { + return (void*)pyb_thread_cur->local_state; +} + +static inline void pyb_thread_yield(void) { + SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; +} + +#endif // MICROPY_INCLUDED_STMHAL_PYBTHREAD_H diff --git a/stmhal/stm32_it.c b/stmhal/stm32_it.c index e1129ac39..a6503d310 100644 --- a/stmhal/stm32_it.c +++ b/stmhal/stm32_it.c @@ -73,6 +73,7 @@ #include "py/obj.h" #include "pendsv.h" #include "irq.h" +#include "pybthread.h" #include "extint.h" #include "timer.h" #include "uart.h" @@ -287,6 +288,11 @@ void SysTick_Handler(void) { if (DMA_IDLE_ENABLED() && DMA_IDLE_TICK(uwTick)) { dma_idle_handler(uwTick); } + + // signal a thread switch at 4ms=250Hz + if (pyb_thread_enabled && (uwTick & 0x03) == 0x03) { + SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; + } } /******************************************************************************/ -- cgit v1.2.3 From 7ae9bee7905601a827f3d216300f917afbacf07e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 8 Feb 2017 11:50:22 +1100 Subject: stmhal/Makefile: Drop use of -mabi=aapcs-linux; link libgcc by default. The aapcs-linux ABI is not required, instead the default aapcs ABI is enough. And using the default ABI means that the provided libgcc will now link with the firmware without warnings about variable vs fixed enums. Although the binary size increases by about 1k, RAM usage is slightly decreased. And libgcc may prove useful in the future for things like long-long division. --- stmhal/Makefile | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index a83f2c495..edd5e0619 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -51,7 +51,7 @@ INC += -I../lib/mp-readline INC += -I../lib/netutils INC += -I../lib/timeutils -CFLAGS_CORTEX_M = -mthumb -mabi=aapcs-linux -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion +CFLAGS_CORTEX_M = -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion CFLAGS_MCU_f4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -DMCU_SERIES_F4 CFLAGS_MCU_f7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 -DMCU_SERIES_F7 CFLAGS_MCU_l4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -DMCU_SERIES_L4 @@ -64,7 +64,7 @@ CFLAGS += -Iboards/$(BOARD) CFLAGS += -DSTM32_HAL_H='' LDFLAGS = -nostdlib -L $(LD_DIR) -T $(LD_FILE) -Map=$(@:.elf=.map) --cref -LIBS = +LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) # Remove uncalled code from the final image. CFLAGS += -fdata-sections -ffunction-sections @@ -78,9 +78,6 @@ else COPT += -Os -DNDEBUG endif -# uncomment this if you want libgcc -#LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) - SRC_LIB = $(addprefix lib/,\ libc/string0.c \ libm/math.c \ -- cgit v1.2.3 From e5cc681cb1b5163b9ae3453df85344326baf9759 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 15 Feb 2017 16:39:30 +1100 Subject: stmhal: Use generic interrupt char code. --- stmhal/Makefile | 1 + stmhal/mphalport.c | 4 ---- stmhal/pendsv.c | 7 ++++--- stmhal/pendsv.h | 2 +- stmhal/usb.c | 14 +++----------- stmhal/usb.h | 1 - stmhal/usbd_cdc_interface.c | 22 +++++----------------- stmhal/usbd_cdc_interface.h | 1 - 8 files changed, 14 insertions(+), 38 deletions(-) (limited to 'stmhal/Makefile') diff --git a/stmhal/Makefile b/stmhal/Makefile index edd5e0619..76579d130 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -112,6 +112,7 @@ SRC_LIB = $(addprefix lib/,\ netutils/netutils.c \ timeutils/timeutils.c \ utils/pyexec.c \ + utils/interrupt_char.c \ ) DRIVERS_SRC_C = $(addprefix drivers/,\ diff --git a/stmhal/mphalport.c b/stmhal/mphalport.c index ca8e1c1bd..64c1164cc 100644 --- a/stmhal/mphalport.c +++ b/stmhal/mphalport.c @@ -21,10 +21,6 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) { mp_raise_OSError(mp_hal_status_to_errno_table[status]); } -void mp_hal_set_interrupt_char(int c) { - usb_vcp_set_interrupt_char(c); -} - int mp_hal_stdin_rx_chr(void) { for (;;) { #if 0 diff --git a/stmhal/pendsv.c b/stmhal/pendsv.c index e6df84b29..4c2a14de1 100644 --- a/stmhal/pendsv.c +++ b/stmhal/pendsv.c @@ -29,6 +29,7 @@ #include "py/mpstate.h" #include "py/runtime.h" +#include "lib/utils/interrupt_char.h" #include "pendsv.h" #include "irq.h" @@ -52,12 +53,12 @@ void pendsv_init(void) { // PENDSV feature. This will wait until all interrupts are finished then raise // the given exception object using nlr_jump in the context of the top-level // thread. -void pendsv_nlr_jump(void *o) { +void pendsv_kbd_intr(void) { if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_NULL) { - MP_STATE_VM(mp_pending_exception) = o; + mp_keyboard_interrupt(); } else { MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; - pendsv_object = o; + pendsv_object = &MP_STATE_VM(mp_kbd_exception); SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; } } diff --git a/stmhal/pendsv.h b/stmhal/pendsv.h index 7886d9f98..77c78d4c1 100644 --- a/stmhal/pendsv.h +++ b/stmhal/pendsv.h @@ -25,7 +25,7 @@ */ void pendsv_init(void); -void pendsv_nlr_jump(void *val); +void pendsv_kbd_intr(void); // since we play tricks with the stack, the compiler must not generate a // prelude for this function diff --git a/stmhal/usb.c b/stmhal/usb.c index 7eb3f179a..c413ce4ba 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -38,6 +38,7 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" +#include "py/mphal.h" #include "bufhelper.h" #include "usb.h" @@ -96,7 +97,7 @@ const mp_obj_tuple_t pyb_usb_hid_keyboard_obj = { }; void pyb_usb_init0(void) { - USBD_CDC_SetInterrupt(-1); + mp_hal_set_interrupt_char(-1); MP_STATE_PORT(pyb_hid_report_desc) = MP_OBJ_NULL; } @@ -141,15 +142,6 @@ bool usb_vcp_is_enabled(void) { return (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) != 0; } -void usb_vcp_set_interrupt_char(int c) { - if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { - if (c != -1) { - mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); - } - USBD_CDC_SetInterrupt(c); - } -} - int usb_vcp_recv_byte(uint8_t *c) { return USBD_CDC_Rx(c, 1, 0); } @@ -364,7 +356,7 @@ STATIC mp_obj_t pyb_usb_vcp_make_new(const mp_obj_type_t *type, size_t n_args, s } STATIC mp_obj_t pyb_usb_vcp_setinterrupt(mp_obj_t self_in, mp_obj_t int_chr_in) { - usb_vcp_set_interrupt_char(mp_obj_get_int(int_chr_in)); + mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_usb_vcp_setinterrupt_obj, pyb_usb_vcp_setinterrupt); diff --git a/stmhal/usb.h b/stmhal/usb.h index e153f0c6b..bc2b91c3d 100644 --- a/stmhal/usb.h +++ b/stmhal/usb.h @@ -61,7 +61,6 @@ void pyb_usb_init0(void); bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_HID_ModeInfoTypeDef *hid_info); void pyb_usb_dev_deinit(void); bool usb_vcp_is_enabled(void); -void usb_vcp_set_interrupt_char(int c); int usb_vcp_recv_byte(uint8_t *c); // if a byte is available, return 1 and put the byte in *c, else return 0 void usb_vcp_send_strn(const char* str, int len); void usb_vcp_send_strn_cooked(const char *str, int len); diff --git a/stmhal/usbd_cdc_interface.c b/stmhal/usbd_cdc_interface.c index 1f46b9dcc..1c12cdc1c 100644 --- a/stmhal/usbd_cdc_interface.c +++ b/stmhal/usbd_cdc_interface.c @@ -43,6 +43,7 @@ #include "py/mpstate.h" #include "py/obj.h" +#include "lib/utils/interrupt_char.h" #include "irq.h" #include "timer.h" #include "usb.h" @@ -79,8 +80,6 @@ static uint16_t UserTxBufPtrOutShadow = 0; // shadow of above static uint8_t UserTxBufPtrWaitCount = 0; // used to implement a timeout waiting for low-level USB driver static uint8_t UserTxNeedEmptyPacket = 0; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size -static int user_interrupt_char = -1; - /* Private function prototypes -----------------------------------------------*/ static int8_t CDC_Itf_Init (void); static int8_t CDC_Itf_DeInit (void); @@ -147,13 +146,6 @@ static int8_t CDC_Itf_Init(void) UserRxBufCur = 0; UserRxBufLen = 0; - /* NOTE: we cannot reset these here, because USBD_CDC_SetInterrupt - * may be called before this init function to set these values. - * This can happen if the USB enumeration occurs after the call to - * USBD_CDC_SetInterrupt. - user_interrupt_char = -1; - */ - return (USBD_OK); } @@ -339,7 +331,7 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) { uint32_t delta_len; - if (user_interrupt_char == -1) { + if (mp_interrupt_char == -1) { // no special interrupt character delta_len = *Len; @@ -350,10 +342,10 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) { uint8_t *src = Buf; uint8_t *buf_top = Buf + *Len; for (; src < buf_top; src++) { - if (*src == user_interrupt_char) { + if (*src == mp_interrupt_char) { char_found = true; - // raise exception when interrupts are finished - pendsv_nlr_jump(&MP_STATE_VM(mp_kbd_exception)); + // raise KeyboardInterrupt when interrupts are finished + pendsv_kbd_intr(); } else { if (char_found) { *dest = *src; @@ -385,10 +377,6 @@ int USBD_CDC_IsConnected(void) { return dev_is_connected; } -void USBD_CDC_SetInterrupt(int chr) { - user_interrupt_char = chr; -} - int USBD_CDC_TxHalfEmpty(void) { int32_t tx_waiting = (int32_t)UserTxBufPtrIn - (int32_t)UserTxBufPtrOut; if (tx_waiting < 0) { diff --git a/stmhal/usbd_cdc_interface.h b/stmhal/usbd_cdc_interface.h index 2ea1a42c4..d96861a7e 100644 --- a/stmhal/usbd_cdc_interface.h +++ b/stmhal/usbd_cdc_interface.h @@ -32,7 +32,6 @@ extern const USBD_CDC_ItfTypeDef USBD_CDC_fops; int USBD_CDC_IsConnected(void); -void USBD_CDC_SetInterrupt(int chr); int USBD_CDC_TxHalfEmpty(void); int USBD_CDC_Tx(const uint8_t *buf, uint32_t len, uint32_t timeout); -- cgit v1.2.3 From 75589272ef12d538ab7ce4f4453be85d826b5083 Mon Sep 17 00:00:00 2001 From: Krzysztof Blazewicz Date: Sun, 5 Mar 2017 13:28:27 +0100 Subject: all/Makefile: Remove -ansi from GCC flags, its ignored anyway. The -ansi flag is used for C dialect selection and it is equivalent to -std=c90. Because it goes right before -std=gnu99 it is ignored as for conflicting flags GCC always uses the last one. --- bare-arm/Makefile | 2 +- cc3200/Makefile | 2 +- esp8266/Makefile | 2 +- examples/embedding/Makefile.upylib | 2 +- minimal/Makefile | 4 ++-- mpy-cross/Makefile | 2 +- pic16bit/Makefile | 2 +- qemu-arm/Makefile | 2 +- stmhal/Makefile | 2 +- teensy/Makefile | 2 +- unix/Makefile | 2 +- windows/Makefile | 2 +- 12 files changed, 13 insertions(+), 13 deletions(-) (limited to 'stmhal/Makefile') diff --git a/bare-arm/Makefile b/bare-arm/Makefile index a57ccfd19..6ee855262 100644 --- a/bare-arm/Makefile +++ b/bare-arm/Makefile @@ -13,7 +13,7 @@ INC += -I.. INC += -I$(BUILD) CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) +CFLAGS = $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) #Debugging/Optimization ifeq ($(DEBUG), 1) diff --git a/cc3200/Makefile b/cc3200/Makefile index 4db3b9e8d..663496435 100644 --- a/cc3200/Makefile +++ b/cc3200/Makefile @@ -20,7 +20,7 @@ include ../py/mkenv.mk CROSS_COMPILE ?= arm-none-eabi- CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -march=armv7e-m -mabi=aapcs -mcpu=cortex-m4 -msoft-float -mfloat-abi=soft -fsingle-precision-constant -Wdouble-promotion -CFLAGS = -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os +CFLAGS = -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os CFLAGS += -g -ffunction-sections -fdata-sections -fno-common -fsigned-char -mno-unaligned-access CFLAGS += -Iboards/$(BOARD) CFLAGS += $(CFLAGS_MOD) diff --git a/esp8266/Makefile b/esp8266/Makefile index 5e61fc16d..4710f4cdc 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -41,7 +41,7 @@ CFLAGS_XTENSA = -fsingle-precision-constant -Wdouble-promotion \ -Wl,-EL -mlongcalls -mtext-section-literals -mforce-l32 \ -DLWIP_OPEN_SRC -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ +CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ $(CFLAGS_XTENSA) $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) LDSCRIPT = esp8266.ld diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index d8dbade3f..8b506e95e 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -20,7 +20,7 @@ INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized -CFLAGS = $(INC) $(CWARN) -ansi -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) # Debugging/Optimization ifdef DEBUG diff --git a/minimal/Makefile b/minimal/Makefile index 411751735..42c2d5312 100644 --- a/minimal/Makefile +++ b/minimal/Makefile @@ -22,9 +22,9 @@ ifeq ($(CROSS), 1) DFU = ../tools/dfu.py PYDFU = ../tools/pydfu.py CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) +CFLAGS = $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT) else -CFLAGS = -m32 $(INC) -Wall -Werror -ansi -std=gnu99 $(COPT) +CFLAGS = -m32 $(INC) -Wall -Werror -std=gnu99 $(COPT) endif #Debugging/Optimization diff --git a/mpy-cross/Makefile b/mpy-cross/Makefile index 3f9956620..3d5e5da2f 100644 --- a/mpy-cross/Makefile +++ b/mpy-cross/Makefile @@ -19,7 +19,7 @@ INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized -CFLAGS = $(INC) $(CWARN) -ansi -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS = $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables # Debugging/Optimization diff --git a/pic16bit/Makefile b/pic16bit/Makefile index cfe8af86e..add9a8495 100644 --- a/pic16bit/Makefile +++ b/pic16bit/Makefile @@ -21,7 +21,7 @@ INC += -I$(XC16)/include INC += -I$(XC16)/support/$(PARTFAMILY)/h CFLAGS_PIC16BIT = -mcpu=$(PART) -mlarge-code -CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT) +CFLAGS = $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT) #Debugging/Optimization ifeq ($(DEBUG), 1) diff --git a/qemu-arm/Makefile b/qemu-arm/Makefile index 61fc24364..e8f5b359e 100644 --- a/qemu-arm/Makefile +++ b/qemu-arm/Makefile @@ -15,7 +15,7 @@ INC += -I$(BUILD) INC += -I../tools/tinytest/ CFLAGS_CORTEX_M3 = -mthumb -mcpu=cortex-m3 -mfloat-abi=soft -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 $(CFLAGS_CORTEX_M3) $(COPT) \ +CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 $(CFLAGS_CORTEX_M3) $(COPT) \ -ffunction-sections -fdata-sections #Debugging/Optimization diff --git a/stmhal/Makefile b/stmhal/Makefile index 76579d130..3eef6ffb2 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -56,7 +56,7 @@ CFLAGS_MCU_f4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -DMCU_SERIES CFLAGS_MCU_f7 = $(CFLAGS_CORTEX_M) -mtune=cortex-m7 -mcpu=cortex-m7 -DMCU_SERIES_F7 CFLAGS_MCU_l4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -DMCU_SERIES_L4 -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_MOD) +CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_MOD) CFLAGS += -D$(CMSIS_MCU) CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(COPT) diff --git a/teensy/Makefile b/teensy/Makefile index 82e04a1ae..e613b8f27 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -36,7 +36,7 @@ INC += -I../lib/mp-readline INC += -I$(BUILD) INC += -Icore -CFLAGS = $(INC) -Wall -Wpointer-arith -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) +CFLAGS = $(INC) -Wall -Wpointer-arith -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) LDFLAGS = -nostdlib -T mk20dx256.ld -msoft-float -mfloat-abi=soft ifeq ($(USE_ARDUINO_TOOLCHAIN),1) diff --git a/unix/Makefile b/unix/Makefile index 83bd3f984..f28a4bcae 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -23,7 +23,7 @@ INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized -CFLAGS = $(INC) $(CWARN) -ansi -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) # Debugging/Optimization ifdef DEBUG diff --git a/windows/Makefile b/windows/Makefile index 68de66a07..72c97381b 100644 --- a/windows/Makefile +++ b/windows/Makefile @@ -15,7 +15,7 @@ INC += -I.. INC += -I$(BUILD) # compiler settings -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -DUNIX -D__USE_MINGW_ANSI_STDIO=1 $(CFLAGS_MOD) $(COPT) +CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -DUNIX -D__USE_MINGW_ANSI_STDIO=1 $(CFLAGS_MOD) $(COPT) LDFLAGS = $(LDFLAGS_MOD) -lm # Debugging/Optimization -- cgit v1.2.3 From 125eae1ba3c59b882fc95c82c95dccd2d93ceaa1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 24 Mar 2017 10:40:25 +1100 Subject: py/modbuiltins: For round() builtin use nearbyint instead of round. The C nearbyint function has exactly the semantics that Python's round() requires, whereas C's round() requires extra steps to handle rounding of numbers half way between integers. So using nearbyint reduces code size and potentially eliminates any source of errors in the handling of half-way numbers. Also, bare-metal implementations of nearbyint can be more efficient than round, so further code size is saved (and efficiency improved). nearbyint is provided in the C99 standard so it should be available on all supported platforms. --- esp8266/Makefile | 2 +- py/modbuiltins.c | 11 ++--------- qemu-arm/Makefile | 2 +- stmhal/Makefile | 2 +- 4 files changed, 5 insertions(+), 12 deletions(-) (limited to 'stmhal/Makefile') diff --git a/esp8266/Makefile b/esp8266/Makefile index 4710f4cdc..28dbf08a6 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -108,7 +108,7 @@ LIB_SRC_C = $(addprefix lib/,\ libc/string0.c \ libm/math.c \ libm/fmodf.c \ - libm/roundf.c \ + libm/nearbyintf.c \ libm/ef_sqrt.c \ libm/kf_rem_pio2.c \ libm/kf_sin.c \ diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 13312d229..541e733e5 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -473,18 +473,11 @@ STATIC mp_obj_t mp_builtin_round(size_t n_args, const mp_obj_t *args) { mp_float_t val = mp_obj_get_float(o_in); mp_float_t mult = MICROPY_FLOAT_C_FUN(pow)(10, num_dig); // TODO may lead to overflow - mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val * mult) / mult; + mp_float_t rounded = MICROPY_FLOAT_C_FUN(nearbyint)(val * mult) / mult; return mp_obj_new_float(rounded); } mp_float_t val = mp_obj_get_float(o_in); - mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val); - mp_int_t r = rounded; - // make rounded value even if it was halfway between ints - if (val - rounded == 0.5) { - r = (r + 1) & (~1); - } else if (val - rounded == -0.5) { - r &= ~1; - } + mp_float_t rounded = MICROPY_FLOAT_C_FUN(nearbyint)(val); #else mp_int_t r = mp_obj_get_int(o_in); #endif diff --git a/qemu-arm/Makefile b/qemu-arm/Makefile index e8f5b359e..d4bbe8d58 100644 --- a/qemu-arm/Makefile +++ b/qemu-arm/Makefile @@ -47,7 +47,7 @@ SRC_TEST_C = \ LIB_SRC_C = $(addprefix lib/,\ libm/math.c \ libm/fmodf.c \ - libm/roundf.c \ + libm/nearbyintf.c \ libm/ef_sqrt.c \ libm/kf_rem_pio2.c \ libm/kf_sin.c \ diff --git a/stmhal/Makefile b/stmhal/Makefile index 3eef6ffb2..51aa07f3c 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -86,7 +86,7 @@ SRC_LIB = $(addprefix lib/,\ libm/atanf.c \ libm/atan2f.c \ libm/fmodf.c \ - libm/roundf.c \ + libm/nearbyintf.c \ libm/log1pf.c \ libm/acoshf.c \ libm/asinhf.c \ -- cgit v1.2.3 From b6c7e4b143d96ff9f84ccb22d83b1e15ab084250 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 31 Mar 2017 22:29:39 +1100 Subject: all: Use full path name when including mp-readline/timeutils/netutils. This follows the pattern of how all other headers are now included, and makes it explicit where the header file comes from. This patch also removes -I options from Makefile's that specify the mp-readline/timeutils/ netutils directories, which are no longer needed. --- cc3200/application.mk | 3 --- cc3200/ftp/ftp.c | 2 +- cc3200/mods/moduos.c | 2 +- cc3200/mods/modusocket.c | 2 +- cc3200/mods/modutime.c | 2 +- cc3200/mods/pybrtc.c | 2 +- cc3200/mptask.c | 2 +- esp8266/Makefile | 3 --- esp8266/fatfs_port.c | 2 +- esp8266/machine_rtc.c | 2 +- esp8266/modnetwork.c | 2 +- esp8266/modutime.c | 2 +- examples/embedding/Makefile.upylib | 1 - extmod/modlwip.c | 2 +- lib/mp-readline/readline.c | 2 +- lib/netutils/netutils.c | 2 +- lib/timeutils/timeutils.c | 2 +- minimal/Makefile | 1 - pic16bit/Makefile | 1 - pic16bit/main.c | 2 +- py/py.mk | 3 --- stmhal/Makefile | 3 --- stmhal/input.c | 2 +- stmhal/main.c | 2 +- stmhal/modnwcc3k.c | 2 +- stmhal/modnwwiznet5k.c | 2 +- stmhal/moduos.c | 2 +- stmhal/modusocket.c | 2 +- stmhal/modutime.c | 2 +- teensy/Makefile | 1 - teensy/main.c | 2 +- unix/Makefile | 1 - 32 files changed, 23 insertions(+), 40 deletions(-) (limited to 'stmhal/Makefile') diff --git a/cc3200/application.mk b/cc3200/application.mk index b15dbde7f..5d25424e1 100644 --- a/cc3200/application.mk +++ b/cc3200/application.mk @@ -18,9 +18,6 @@ APP_INC += -Iutil APP_INC += -Ibootmgr APP_INC += -I$(BUILD) APP_INC += -I$(BUILD)/genhdr -APP_INC += -I../lib/mp-readline -APP_INC += -I../lib/netutils -APP_INC += -I../lib/timeutils APP_INC += -I../stmhal APP_CPPDEFINES = -Dgcc -DTARGET_IS_CC3200 -DSL_FULL -DUSE_FREERTOS diff --git a/cc3200/ftp/ftp.c b/cc3200/ftp/ftp.c index 22035d6b3..1febe291f 100644 --- a/cc3200/ftp/ftp.c +++ b/cc3200/ftp/ftp.c @@ -29,6 +29,7 @@ #include "py/mpstate.h" #include "py/obj.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" @@ -48,7 +49,6 @@ #include "fifo.h" #include "socketfifo.h" #include "updater.h" -#include "timeutils.h" #include "moduos.h" /****************************************************************************** diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index 5523c887f..add10ec6c 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -33,6 +33,7 @@ #include "py/objtuple.h" #include "py/objstr.h" #include "py/runtime.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "genhdr/mpversion.h" @@ -43,7 +44,6 @@ #include "random.h" #include "mpexception.h" #include "version.h" -#include "timeutils.h" #include "pybsd.h" #include "pybuart.h" diff --git a/cc3200/mods/modusocket.c b/cc3200/mods/modusocket.c index 4e2da6737..ca28bf7ba 100644 --- a/cc3200/mods/modusocket.c +++ b/cc3200/mods/modusocket.c @@ -34,7 +34,7 @@ #include "py/objstr.h" #include "py/runtime.h" #include "py/stream.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "modusocket.h" #include "mpexception.h" diff --git a/cc3200/mods/modutime.c b/cc3200/mods/modutime.c index 8e3c71402..48fde67e7 100644 --- a/cc3200/mods/modutime.c +++ b/cc3200/mods/modutime.c @@ -33,8 +33,8 @@ #include "py/obj.h" #include "py/smallint.h" #include "py/mphal.h" +#include "lib/timeutils/timeutils.h" #include "extmod/utime_mphal.h" -#include "timeutils.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" diff --git a/cc3200/mods/pybrtc.c b/cc3200/mods/pybrtc.c index 13d7a49cb..134bd440e 100644 --- a/cc3200/mods/pybrtc.c +++ b/cc3200/mods/pybrtc.c @@ -29,6 +29,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mperrno.h" +#include "lib/timeutils/timeutils.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" @@ -37,7 +38,6 @@ #include "pybrtc.h" #include "mpirq.h" #include "pybsleep.h" -#include "timeutils.h" #include "simplelink.h" #include "modnetwork.h" #include "modwlan.h" diff --git a/cc3200/mptask.c b/cc3200/mptask.c index 3c49a5603..d446711a2 100644 --- a/cc3200/mptask.c +++ b/cc3200/mptask.c @@ -33,6 +33,7 @@ #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" +#include "lib/mp-readline/readline.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "extmod/vfs.h" @@ -51,7 +52,6 @@ #include "lib/utils/pyexec.h" #include "gccollect.h" #include "gchelper.h" -#include "readline.h" #include "mperror.h" #include "simplelink.h" #include "modnetwork.h" diff --git a/esp8266/Makefile b/esp8266/Makefile index 28dbf08a6..a3da9d398 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -25,9 +25,6 @@ ESP_SDK = $(shell $(CC) -print-sysroot)/usr INC += -I. INC += -I.. INC += -I../stmhal -INC += -I../lib/mp-readline -INC += -I../lib/netutils -INC += -I../lib/timeutils INC += -I$(BUILD) INC += -I$(ESP_SDK)/include diff --git a/esp8266/fatfs_port.c b/esp8266/fatfs_port.c index 20c3235d4..02384f605 100644 --- a/esp8266/fatfs_port.c +++ b/esp8266/fatfs_port.c @@ -25,8 +25,8 @@ */ #include "py/obj.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" -#include "timeutils.h" #include "modmachine.h" DWORD get_fattime(void) { diff --git a/esp8266/machine_rtc.c b/esp8266/machine_rtc.c index 8c1fe0779..019b705ba 100644 --- a/esp8266/machine_rtc.c +++ b/esp8266/machine_rtc.c @@ -30,7 +30,7 @@ #include "py/nlr.h" #include "py/obj.h" #include "py/runtime.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" #include "user_interface.h" #include "modmachine.h" diff --git a/esp8266/modnetwork.c b/esp8266/modnetwork.c index 762717b35..627655b36 100644 --- a/esp8266/modnetwork.c +++ b/esp8266/modnetwork.c @@ -32,7 +32,7 @@ #include "py/objlist.h" #include "py/runtime.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "queue.h" #include "user_interface.h" #include "espconn.h" diff --git a/esp8266/modutime.c b/esp8266/modutime.c index 117720e6f..bdeb3bb45 100644 --- a/esp8266/modutime.c +++ b/esp8266/modutime.c @@ -34,8 +34,8 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/smallint.h" +#include "lib/timeutils/timeutils.h" #include "modmachine.h" -#include "timeutils.h" #include "user_interface.h" #include "extmod/utime_mphal.h" diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index 8b506e95e..873c0fd34 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -14,7 +14,6 @@ INC += -I. INC += -I.. INC += -I$(MPTOP) INC += -I$(MPTOP)/unix -#INC += -I../lib/timeutils INC += -I$(BUILD) # compiler settings diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 5709413de..fffabb98a 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -36,7 +36,7 @@ #include "py/mperrno.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "lwip/init.h" #include "lwip/timers.h" diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c index cbb99cc94..4b9875136 100644 --- a/lib/mp-readline/readline.c +++ b/lib/mp-readline/readline.c @@ -31,7 +31,7 @@ #include "py/mpstate.h" #include "py/repl.h" #include "py/mphal.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" #if 0 // print debugging info #define DEBUG_PRINT (1) diff --git a/lib/netutils/netutils.c b/lib/netutils/netutils.c index 3a4c600dc..a2ea31cf3 100644 --- a/lib/netutils/netutils.c +++ b/lib/netutils/netutils.c @@ -31,7 +31,7 @@ #include "py/obj.h" #include "py/nlr.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" // Takes an array with a raw IPv4 address and returns something like '192.168.0.1'. mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian) { diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index 19d3ddbdd..0af39a295 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -27,7 +27,7 @@ #include "py/obj.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" // LEAPOCH corresponds to 2000-03-01, which is a mod-400 year, immediately // after Feb 29. We calculate seconds as a signed integer relative to that. diff --git a/minimal/Makefile b/minimal/Makefile index 3b446beae..d61515797 100644 --- a/minimal/Makefile +++ b/minimal/Makefile @@ -14,7 +14,6 @@ endif INC += -I. INC += -I.. -INC += -I../lib/mp-readline INC += -I../stmhal INC += -I$(BUILD) diff --git a/pic16bit/Makefile b/pic16bit/Makefile index add9a8495..8c0c1c999 100644 --- a/pic16bit/Makefile +++ b/pic16bit/Makefile @@ -14,7 +14,6 @@ PART = 33FJ256GP506 INC += -I. INC += -I.. -INC += -I../lib/mp-readline INC += -I../stmhal INC += -I$(BUILD) INC += -I$(XC16)/include diff --git a/pic16bit/main.c b/pic16bit/main.c index f2a9debab..7de790069 100644 --- a/pic16bit/main.c +++ b/pic16bit/main.c @@ -35,7 +35,7 @@ #include "py/mphal.h" #include "py/mperrno.h" #include "lib/utils/pyexec.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" #include "board.h" #include "modpyb.h" diff --git a/py/py.mk b/py/py.mk index 37bb5d023..5ff1fd6a6 100644 --- a/py/py.mk +++ b/py/py.mk @@ -16,9 +16,6 @@ endif # some code is performance bottleneck and compiled with other optimization options CSUPEROPT = -O3 -INC += -I../lib -INC += -I../lib/netutils - # this sets the config file for FatFs CFLAGS_MOD += -DFFCONF_H=\"lib/oofatfs/ffconf.h\" diff --git a/stmhal/Makefile b/stmhal/Makefile index 51aa07f3c..09643be94 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -47,9 +47,6 @@ INC += -I$(CMSIS_DIR)/ INC += -I$(HAL_DIR)/inc INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc #INC += -I$(USBHOST_DIR) -INC += -I../lib/mp-readline -INC += -I../lib/netutils -INC += -I../lib/timeutils CFLAGS_CORTEX_M = -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion CFLAGS_MCU_f4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -DMCU_SERIES_F4 diff --git a/stmhal/input.c b/stmhal/input.c index 07e7ba35c..c78525cc9 100644 --- a/stmhal/input.c +++ b/stmhal/input.c @@ -26,7 +26,7 @@ #include "py/nlr.h" #include "py/obj.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) { if (n_args == 1) { diff --git a/stmhal/main.c b/stmhal/main.c index 8d076a08b..566c2db07 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -31,6 +31,7 @@ #include "py/stackctrl.h" #include "py/gc.h" #include "py/mphal.h" +#include "lib/mp-readline/readline.h" #include "lib/utils/pyexec.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" @@ -40,7 +41,6 @@ #include "pendsv.h" #include "pybthread.h" #include "gccollect.h" -#include "readline.h" #include "modmachine.h" #include "i2c.h" #include "spi.h" diff --git a/stmhal/modnwcc3k.c b/stmhal/modnwcc3k.c index ae403cacd..26c6cd48c 100644 --- a/stmhal/modnwcc3k.c +++ b/stmhal/modnwcc3k.c @@ -37,7 +37,7 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "pin.h" #include "genhdr/pins.h" diff --git a/stmhal/modnwwiznet5k.c b/stmhal/modnwwiznet5k.c index e3d5c6370..b32f16913 100644 --- a/stmhal/modnwwiznet5k.c +++ b/stmhal/modnwwiznet5k.c @@ -33,7 +33,7 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "pin.h" #include "genhdr/pins.h" diff --git a/stmhal/moduos.c b/stmhal/moduos.c index 0af682612..745c0d5d7 100644 --- a/stmhal/moduos.c +++ b/stmhal/moduos.c @@ -31,12 +31,12 @@ #include "py/runtime.h" #include "py/objtuple.h" #include "py/objstr.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" #include "genhdr/mpversion.h" -#include "timeutils.h" #include "rng.h" #include "uart.h" #include "portmodules.h" diff --git a/stmhal/modusocket.c b/stmhal/modusocket.c index d066501d0..fd60c5ad4 100644 --- a/stmhal/modusocket.c +++ b/stmhal/modusocket.c @@ -32,7 +32,7 @@ #include "py/objlist.h" #include "py/runtime.h" #include "py/mperrno.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #if MICROPY_PY_USOCKET diff --git a/stmhal/modutime.c b/stmhal/modutime.c index 2c7245080..97af35c49 100644 --- a/stmhal/modutime.c +++ b/stmhal/modutime.c @@ -31,9 +31,9 @@ #include "py/nlr.h" #include "py/smallint.h" #include "py/obj.h" +#include "lib/timeutils/timeutils.h" #include "extmod/utime_mphal.h" #include "systick.h" -#include "timeutils.h" #include "portmodules.h" #include "rtc.h" diff --git a/teensy/Makefile b/teensy/Makefile index e613b8f27..9f52cc3c7 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -32,7 +32,6 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -mfloat INC += -I. INC += -I.. INC += -I../stmhal -INC += -I../lib/mp-readline INC += -I$(BUILD) INC += -Icore diff --git a/teensy/main.c b/teensy/main.c index d62ae3bdb..41bbeb5d9 100644 --- a/teensy/main.c +++ b/teensy/main.c @@ -10,7 +10,7 @@ #include "py/mphal.h" #include "gccollect.h" #include "lib/utils/pyexec.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" #include "lexermemzip.h" #include "Arduino.h" diff --git a/unix/Makefile b/unix/Makefile index f28a4bcae..546985306 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -17,7 +17,6 @@ include ../py/py.mk INC += -I. INC += -I.. -INC += -I../lib/timeutils INC += -I$(BUILD) # compiler settings -- cgit v1.2.3 From 6ff0ecfffcea4e7aa6301c54870ae97aa4a0cf72 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 1 Jun 2017 15:43:20 +1000 Subject: ports: Convert from using stmhal's input() to core provided version. --- cc3200/application.mk | 1 - cc3200/mpconfigport.h | 2 +- esp8266/Makefile | 1 - esp8266/mpconfigport.h | 2 +- stmhal/Makefile | 1 - stmhal/input.c | 44 -------------------------------------------- stmhal/mpconfigport.h | 2 +- teensy/Makefile | 1 - teensy/mpconfigport.h | 2 +- 9 files changed, 4 insertions(+), 52 deletions(-) delete mode 100644 stmhal/input.c (limited to 'stmhal/Makefile') diff --git a/cc3200/application.mk b/cc3200/application.mk index 5d25424e1..1f54b764b 100644 --- a/cc3200/application.mk +++ b/cc3200/application.mk @@ -151,7 +151,6 @@ APP_LIB_SRC_C = $(addprefix lib/,\ APP_STM_SRC_C = $(addprefix stmhal/,\ bufhelper.c \ - input.c \ irq.c \ pybstdio.c \ ) diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h index 4d19900ae..4bd583a4b 100644 --- a/cc3200/mpconfigport.h +++ b/cc3200/mpconfigport.h @@ -80,6 +80,7 @@ #define MICROPY_VFS_FAT (1) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT cc3200_help_text #ifndef DEBUG @@ -142,7 +143,6 @@ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, \ // extra built in modules to add to the list of known ones diff --git a/esp8266/Makefile b/esp8266/Makefile index a3da9d398..cf4b288ca 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -94,7 +94,6 @@ SRC_C = \ STM_SRC_C = $(addprefix stmhal/,\ pybstdio.c \ - input.c \ ) EXTMOD_SRC_C = $(addprefix extmod/,\ diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index c286bdcdd..483f93d02 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -39,6 +39,7 @@ #define MICROPY_PY_BUILTINS_SLICE (1) #define MICROPY_PY_BUILTINS_SLICE_ATTRS (1) #define MICROPY_PY_BUILTINS_PROPERTY (1) +#define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT esp_help_text #define MICROPY_PY_BUILTINS_HELP_MODULES (1) @@ -147,7 +148,6 @@ void *esp_native_code_commit(void*, size_t); // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, // extra built in modules to add to the list of known ones diff --git a/stmhal/Makefile b/stmhal/Makefile index 09643be94..2d9d44afd 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -147,7 +147,6 @@ SRC_C = \ gccollect.c \ pybstdio.c \ help.c \ - input.c \ machine_i2c.c \ modmachine.c \ modpyb.c \ diff --git a/stmhal/input.c b/stmhal/input.c deleted file mode 100644 index c78525cc9..000000000 --- a/stmhal/input.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * 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. - */ - -#include "py/nlr.h" -#include "py/obj.h" -#include "lib/mp-readline/readline.h" - -STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) { - if (n_args == 1) { - mp_obj_print(args[0], PRINT_STR); - } - vstr_t line; - vstr_init(&line, 16); - int ret = readline(&line, ""); - if (line.len == 0 && ret == CHAR_CTRL_D) { - nlr_raise(mp_obj_new_exception(&mp_type_EOFError)); - } - return mp_obj_new_str_from_vstr(&mp_type_str, &line); -} - -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 444ce9303..ac3c1f247 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -87,6 +87,7 @@ #define MICROPY_PY_ALL_SPECIAL_METHODS (1) #define MICROPY_PY_BUILTINS_COMPILE (1) #define MICROPY_PY_BUILTINS_EXECFILE (1) +#define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY_BUILTINS_POW3 (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT stmhal_help_text @@ -159,7 +160,6 @@ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, // extra built in modules to add to the list of known ones diff --git a/teensy/Makefile b/teensy/Makefile index 9f52cc3c7..923ea77ec 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -93,7 +93,6 @@ SRC_C = \ STM_SRC_C = $(addprefix stmhal/,\ gccollect.c \ - input.c \ irq.c \ pin.c \ pin_named_pins.c \ diff --git a/teensy/mpconfigport.h b/teensy/mpconfigport.h index 8c4022046..de30924d9 100644 --- a/teensy/mpconfigport.h +++ b/teensy/mpconfigport.h @@ -14,6 +14,7 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_OPT_COMPUTED_GOTO (1) +#define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_PY_BUILTINS_HELP (1) #define MICROPY_PY_BUILTINS_HELP_TEXT teensy_help_text @@ -31,7 +32,6 @@ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \ // extra built in modules to add to the list of known ones extern const struct _mp_obj_module_t os_module; -- cgit v1.2.3