From 3f42f32648b7dfb5ac9aa9fb12a546999219b3da Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Sat, 28 Mar 2015 21:46:48 +0100 Subject: [PATCH] cc3200: Remove duplicated code from moduos. Error reporting is also changed from detailed to terse, as with the rest of the CC3200's modules. All this combined saves ~200 bytes. --- cc3200/mods/modnetwork.h | 5 +++ cc3200/mods/modpyb.c | 13 ------ cc3200/mods/moduos.c | 88 ++++++++++++++++++++-------------------- cc3200/mods/moduos.h | 7 ++-- cc3200/mods/modutime.h | 6 +-- cc3200/mods/pybpin.h | 4 ++ cc3200/mods/pybsd.c | 4 +- 7 files changed, 61 insertions(+), 66 deletions(-) diff --git a/cc3200/mods/modnetwork.h b/cc3200/mods/modnetwork.h index 00e10b2c71..4b4b63cd50 100644 --- a/cc3200/mods/modnetwork.h +++ b/cc3200/mods/modnetwork.h @@ -25,6 +25,9 @@ * THE SOFTWARE. */ +#ifndef MODNETWORK_H_ +#define MODNETWORK_H_ + #define MOD_NETWORK_IPV4ADDR_BUF_SIZE (4) // Forward declaration @@ -79,3 +82,5 @@ void mod_network_parse_ipv4_addr(mp_obj_t addr_in, uint8_t *out_ip); mp_uint_t mod_network_parse_inet_addr(mp_obj_t addr_in, uint8_t *out_ip); mp_obj_t mod_network_format_ipv4_addr(uint8_t *ip); mp_obj_t mod_network_format_inet_addr(uint8_t *ip, mp_uint_t port); + +#endif // MODNETWORK_H_ diff --git a/cc3200/mods/modpyb.c b/cc3200/mods/modpyb.c index f31d247f2a..07a25776ab 100644 --- a/cc3200/mods/modpyb.c +++ b/cc3200/mods/modpyb.c @@ -233,17 +233,6 @@ STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart); -/// \function mkdisk('path') -/// Formats the selected drive, useful when the filesystem has been damaged beyond repair -STATIC mp_obj_t pyb_mkdisk(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - if (FR_OK != f_mkfs(path, 1, 0)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_mkdisk_obj, pyb_mkdisk); - MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c STATIC const mp_map_elem_t pyb_module_globals_table[] = { @@ -268,8 +257,6 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mkdisk), (mp_obj_t)&pyb_mkdisk_obj }, #if MICROPY_HW_ENABLE_RNG { MP_OBJ_NEW_QSTR(MP_QSTR_rng), (mp_obj_t)&pyb_rng_get_obj }, diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index b071413d1e..d5a245977a 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -40,6 +40,7 @@ #include "modutime.h" #include "random.h" #include "sd_diskio.h" +#include "mpexception.h" /// \module os - basic "operating system" services /// @@ -54,6 +55,9 @@ /// On boot up, the current directory is `/SFLASH` if no SD card is inserted, /// otherwise it is `/SD`. +/****************************************************************************** + DEFINE PRIVATE FUNCTIONS + ******************************************************************************/ STATIC bool sd_in_root(void) { #if MICROPY_HW_HAS_SDCARD return sd_disk_ready(); @@ -62,6 +66,10 @@ STATIC bool sd_in_root(void) { #endif } +/******************************************************************************/ +// Micro Python bindings +// + /// \function chdir(path) /// Change current directory. STATIC mp_obj_t os_chdir(mp_obj_t path_in) { @@ -73,10 +81,9 @@ STATIC mp_obj_t os_chdir(mp_obj_t path_in) { if (res == FR_OK) { res = f_chdir(path); } - // TODO: Warn if too many open files... + 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)); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } return mp_const_none; @@ -127,8 +134,7 @@ STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) { res = f_opendir(&dir, path); /* Open the directory */ 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)); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } mp_obj_t dir_list = mp_obj_new_list(0, NULL); @@ -168,44 +174,28 @@ STATIC mp_obj_t os_mkdir(mp_obj_t path_o) { 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)); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); + break; default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path)); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir); /// \function remove(path) -/// Remove a file. +/// Remove a file or a directory 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)); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove); -/// \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 @@ -282,11 +272,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat); /// \function sync() /// Sync all filesystems. -mp_obj_t os_sync(void) { +STATIC mp_obj_t os_sync(void) { sflash_disk_flush(); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync); #if MICROPY_HW_ENABLE_RNG /// \function urandom(n) @@ -304,26 +294,36 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); #endif +/// \function mkdisk('path') +/// Formats the selected drive, useful when the filesystem has been damaged beyond repair +STATIC mp_obj_t os_mkdisk(mp_obj_t path_o) { + const char *path = mp_obj_str_get_str(path_o); + if (FR_OK != f_mkfs(path, 1, 0)) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdisk_obj, os_mkdisk); + STATIC const mp_map_elem_t os_module_globals_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) }, + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) }, - { 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_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_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove - - { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_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_rmdir), (mp_obj_t)&os_remove_obj }, // rmdir aliases to remove + { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove + { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_obj }, +#if MICROPY_HW_ENABLE_RNG + { MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj }, +#endif + { MP_OBJ_NEW_QSTR(MP_QSTR_mkdisk), (mp_obj_t)&os_mkdisk_obj }, /// \constant sep - separation character used in paths - { MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) }, - -#if MICROPY_HW_ENABLE_RNG - { MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj }, -#endif + { MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) }, }; STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); diff --git a/cc3200/mods/moduos.h b/cc3200/mods/moduos.h index 8ce872c4b1..7e01148d71 100644 --- a/cc3200/mods/moduos.h +++ b/cc3200/mods/moduos.h @@ -25,9 +25,8 @@ * THE SOFTWARE. */ -#ifndef MODUTIME_H_ -#define MODUTIME_H_ +#ifndef MODUOS_H_ +#define MODUOS_H_ -MP_DECLARE_CONST_FUN_OBJ(os_sync_obj); -#endif // MODUTIME_H_ +#endif // MODUOS_H_ diff --git a/cc3200/mods/modutime.h b/cc3200/mods/modutime.h index bc2be9cfa2..17481a0644 100644 --- a/cc3200/mods/modutime.h +++ b/cc3200/mods/modutime.h @@ -25,8 +25,8 @@ * THE SOFTWARE. */ -#ifndef MODUOS_H_ -#define MODUOS_H_ +#ifndef MODUTIME_H_ +#define MODUTIME_H_ typedef struct { uint16_t tm_year; // i.e. 2014 @@ -45,4 +45,4 @@ extern mp_uint_t mod_time_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp extern void mod_time_seconds_since_2000_to_struct_time(mp_uint_t t, mod_struct_time *tm); -#endif // MODUOS_H_ +#endif // MODUTIME_H_ diff --git a/cc3200/mods/pybpin.h b/cc3200/mods/pybpin.h index d5a7d42dac..417e91bf62 100644 --- a/cc3200/mods/pybpin.h +++ b/cc3200/mods/pybpin.h @@ -25,6 +25,9 @@ * THE SOFTWARE. */ +#ifndef PYBPIN_H_ +#define PYBPIN_H_ + // This file requires pin_defs_xxx.h (which has port specific enums and // defines, so we include it here. It should never be included directly @@ -73,3 +76,4 @@ uint32_t pin_get_mode(const pin_obj_t *self); uint32_t pin_get_type(const pin_obj_t *self); uint32_t pin_get_strenght(const pin_obj_t *self); +#endif // PYBPIN_H_ diff --git a/cc3200/mods/pybsd.c b/cc3200/mods/pybsd.c index e8aceeccab..daac95301d 100644 --- a/cc3200/mods/pybsd.c +++ b/cc3200/mods/pybsd.c @@ -73,7 +73,7 @@ STATIC mp_obj_t pybsd_disable (mp_obj_t self_in); STATIC mp_obj_t pybsd_enable (mp_obj_t self_in); /****************************************************************************** - DECLARE PUBLIC FUNCTIONS + DEFINE PUBLIC FUNCTIONS ******************************************************************************/ __attribute__ ((section (".boot"))) void pybsd_init0 (void) { @@ -86,7 +86,7 @@ void pybsd_deinit (void) { } /****************************************************************************** - DECLARE PRIVATE FUNCTIONS + DEFINE PRIVATE FUNCTIONS ******************************************************************************/ /// Initalizes the sd card driver STATIC void pybsd_init (pybsd_obj_t *self) {