diff options
Diffstat (limited to 'cc3200/mods')
| -rw-r--r-- | cc3200/mods/modmachine.c | 6 | ||||
| -rw-r--r-- | cc3200/mods/modnetwork.c | 5 | ||||
| -rw-r--r-- | cc3200/mods/moduhashlib.c | 4 | ||||
| -rw-r--r-- | cc3200/mods/moduos.c | 472 | ||||
| -rw-r--r-- | cc3200/mods/moduos.h | 16 | ||||
| -rw-r--r-- | cc3200/mods/modusocket.c | 298 | ||||
| -rw-r--r-- | cc3200/mods/modussl.c | 1 | ||||
| -rw-r--r-- | cc3200/mods/modutime.c | 63 | ||||
| -rw-r--r-- | cc3200/mods/modwlan.c | 299 | ||||
| -rw-r--r-- | cc3200/mods/modwlan.h | 15 | ||||
| -rw-r--r-- | cc3200/mods/pybadc.c | 9 | ||||
| -rw-r--r-- | cc3200/mods/pybflash.c | 109 | ||||
| -rw-r--r-- | cc3200/mods/pybflash.h | 35 | ||||
| -rw-r--r-- | cc3200/mods/pybi2c.c | 93 | ||||
| -rw-r--r-- | cc3200/mods/pybpin.c | 8 | ||||
| -rw-r--r-- | cc3200/mods/pybrtc.c | 15 | ||||
| -rw-r--r-- | cc3200/mods/pybsd.c | 51 | ||||
| -rw-r--r-- | cc3200/mods/pybspi.c | 5 | ||||
| -rw-r--r-- | cc3200/mods/pybtimer.c | 5 | ||||
| -rw-r--r-- | cc3200/mods/pybuart.c | 27 | ||||
| -rw-r--r-- | cc3200/mods/pybwdt.c | 5 |
21 files changed, 612 insertions, 929 deletions
diff --git a/cc3200/mods/modmachine.c b/cc3200/mods/modmachine.c index 8a57c2eb4..fd1485607 100644 --- a/cc3200/mods/modmachine.c +++ b/cc3200/mods/modmachine.c @@ -26,7 +26,6 @@ */ #include <stdint.h> -#include "std.h" #include "py/mpstate.h" #include "py/runtime.h" @@ -112,10 +111,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info) #endif STATIC mp_obj_t machine_freq(void) { - mp_obj_t tuple[1] = { - mp_obj_new_int(HAL_FCPU_HZ), - }; - return mp_obj_new_tuple(1, tuple); + return mp_obj_new_int(HAL_FCPU_HZ); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); diff --git a/cc3200/mods/modnetwork.c b/cc3200/mods/modnetwork.c index 89d61e939..d44f75aca 100644 --- a/cc3200/mods/modnetwork.c +++ b/cc3200/mods/modnetwork.c @@ -25,12 +25,11 @@ * THE SOFTWARE. */ -#include <std.h> - #include "py/mpstate.h" #include "py/obj.h" #include "py/nlr.h" #include "py/runtime.h" +#include "py/mperrno.h" #include "py/mphal.h" #include "modnetwork.h" #include "mpexception.h" @@ -101,7 +100,7 @@ STATIC mp_obj_t network_server_make_new(const mp_obj_type_t *type, size_t n_args // check the server id if (args[0].u_obj != MP_OBJ_NULL) { if (mp_obj_get_int(args[0].u_obj) != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } } diff --git a/cc3200/mods/moduhashlib.c b/cc3200/mods/moduhashlib.c index c22cc8a06..e1145e4d8 100644 --- a/cc3200/mods/moduhashlib.c +++ b/cc3200/mods/moduhashlib.c @@ -93,7 +93,7 @@ STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest) { self->digested = false; } } else { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } } @@ -106,7 +106,7 @@ STATIC mp_obj_t hash_read (mp_obj_t self_in) { } } else if (self->c_size < self->b_size) { // it's a fixed len block which is still incomplete - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } if (!self->digested) { diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index ffe2b551a..ed8879bf3 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -33,15 +33,17 @@ #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" #include "moduos.h" -#include "diskio.h" #include "sflash_diskio.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs.h" +#include "extmod/vfs_fat.h" #include "random.h" #include "mpexception.h" #include "version.h" -#include "timeutils.h" #include "pybsd.h" #include "pybuart.h" @@ -59,155 +61,20 @@ /****************************************************************************** DECLARE PRIVATE DATA ******************************************************************************/ -STATIC uint32_t os_num_mounted_devices; STATIC os_term_dup_obj_t os_term_dup_obj; /****************************************************************************** - DECLARE PRIVATE FUNCTIONS - ******************************************************************************/ -STATIC void unmount (os_fs_mount_t *mount_obj); -STATIC bool path_equal(const char *path, const char *path_canonical); -STATIC void append_dir_item (mp_obj_t dirlist, const char *item, bool string); -STATIC void mount (mp_obj_t device, const char *path, uint pathlen, bool readonly); - -/****************************************************************************** DEFINE PUBLIC FUNCTIONS ******************************************************************************/ -void moduos_init0 (void) { - // initialize the mount objects list - mp_obj_list_init(&MP_STATE_PORT(mount_obj_list), 0); - os_num_mounted_devices = 0; -} - -os_fs_mount_t *osmount_find_by_path (const char *path) { - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (!strcmp(path, mount_obj->path)) { - return mount_obj; - } - } - return NULL; -} - -os_fs_mount_t *osmount_find_by_volume (uint8_t vol) { - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (vol == mount_obj->vol) { - return mount_obj; - } - } - return NULL; -} - -os_fs_mount_t *osmount_find_by_device (mp_obj_t device) { - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (device == mount_obj->device) { - return mount_obj; - } - } - return NULL; -} - void osmount_unmount_all (void) { + //TODO + /* for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); unmount(mount_obj); } -} - -/****************************************************************************** - DEFINE PRIVATE FUNCTIONS - ******************************************************************************/ - -// 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'; -} - -STATIC void append_dir_item (mp_obj_t dirlist, const char *item, bool string) { - // make a string object for this entry - mp_obj_t entry_o; - if (string) { - entry_o = mp_obj_new_str(item, strlen(item), false); - } else { - entry_o = mp_obj_new_bytes((const byte*)item, strlen(item)); - } - - // add the entry to the list - mp_obj_list_append(dirlist, entry_o); -} - -STATIC void mount (mp_obj_t device, const char *path, uint pathlen, bool readonly) { - // is the mount point already in use? - FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - // cannot mount twice or on existing paths - if (f_stat(path, &fno) == FR_OK || osmount_find_by_device(device)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); - } - - // create a new object - os_fs_mount_t *self = m_new_obj(os_fs_mount_t); - self->device = device; - self->path = path; - self->pathlen = pathlen; - self->vol = os_num_mounted_devices + 1; // '/flash' is volume 0 - - if (device == (mp_obj_t)&pybsd_obj) { - // need to make it different to NULL, otherwise it's read only by default - self->writeblocks[0] = mp_const_none; - self->sync[0] = MP_OBJ_NULL; // no need to sync the SD card - self->count[0] = MP_OBJ_NULL; - } else { - // load block protocol methods - mp_load_method(device, MP_QSTR_readblocks, self->readblocks); - mp_load_method_maybe(device, MP_QSTR_writeblocks, self->writeblocks); - mp_load_method_maybe(device, MP_QSTR_sync, self->sync); - mp_load_method(device, MP_QSTR_count, self->count); - } - - // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL. - // User can specify read-only device by: - // 1. readonly=True keyword argument - // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already) - if (readonly) { - self->writeblocks[0] = MP_OBJ_NULL; - } - - // we need to add it before doing the actual mount, so that the volume can be found - mp_obj_list_append(&MP_STATE_PORT(mount_obj_list), self); - - // actually mount it - if (f_mount(&self->fatfs, self->path, 1) != FR_OK) { - // remove it and raise - mp_obj_list_remove(&MP_STATE_PORT(mount_obj_list), self); - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - - // mount succeeded, increment the count - os_num_mounted_devices++; -} - -STATIC void unmount (os_fs_mount_t *mount_obj) { - // remove it from the list and then call FatFs - f_mount (NULL, mount_obj->path, 1); - mp_obj_list_remove(&MP_STATE_PORT(mount_obj_list), mount_obj); - os_num_mounted_devices--; + */ } /******************************************************************************/ @@ -239,193 +106,6 @@ 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) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir); - -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); - -STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) { - bool is_str_type = true; - const char *path; - mp_obj_t dir_list = mp_obj_new_list(0, NULL); - - 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 the root directory - if (path[0] == '/' && path[1] == '\0') { - // add 'flash' to the list - append_dir_item (dir_list, "flash", is_str_type); - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - append_dir_item (dir_list, &mount_obj->path[1], is_str_type); - } - } else { - FRESULT res; - DIR dir; - FILINFO fno; - #if _USE_LFN - char lfn_buf[_MAX_LFN + 1]; - fno.lfname = lfn_buf; - fno.lfsize = sizeof(lfn_buf); - #endif - - res = f_opendir(&dir, path); /* Open the directory */ - if (res != FR_OK) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - - for ( ; ; ) { - res = f_readdir(&dir, &fno); /* Read a directory item */ - if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */ - if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */ - - #if _USE_LFN - char *fn = *fno.lfname ? fno.lfname : fno.fname; - #else - char *fn = fno.fname; - #endif - - // add the entry to the list - append_dir_item (dir_list, fn, is_str_type); - } - f_closedir(&dir); - } - - return dir_list; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir); - -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: - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); - break; - default: - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir); - -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); - switch (res) { - case FR_OK: - return mp_const_none; - default: - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename); - -STATIC mp_obj_t os_remove(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - FRESULT res = f_unlink(path); - switch (res) { - case FR_OK: - return mp_const_none; - default: - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove); - -STATIC mp_obj_t os_stat(mp_obj_t path_in) { - const char *path = mp_obj_str_get_str(path_in); - bool isbuilt_in = false; - FILINFO fno; - FRESULT res; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - - // check on the user mounted devices - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (path_equal(path, mount_obj->path)) { - isbuilt_in = true; - break; - } - } - - if (path_equal(path, "/") || path_equal(path, "/flash") || isbuilt_in) { - // stat built-in directory - fno.fsize = 0; - fno.fdate = 0; - fno.ftime = 0; - fno.fattrib = AM_DIR; - } else if ((res = f_stat(path, &fno)) != 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_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_int(fno.fsize); // st_size - t->items[7] = mp_obj_new_int(seconds); // st_atime - t->items[8] = t->items[7]; // st_mtime - t->items[9] = t->items[7]; // st_ctime - return t; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat); - STATIC mp_obj_t os_sync(void) { sflash_disk_flush(); return mp_const_none; @@ -443,110 +123,6 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); -STATIC mp_obj_t os_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - static const mp_arg_t mount_args[] = { - { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, - }; - - // parse args - mp_obj_t device = pos_args[0]; - mp_obj_t mount_point = pos_args[1]; - mp_arg_val_t args[MP_ARRAY_SIZE(mount_args)]; - mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(mount_args), mount_args, args); - - // get the mount point - mp_uint_t pathlen; - const char *path_in = mp_obj_str_get_data(mount_point, &pathlen); - if (pathlen == 0) { - goto invalid_args; - } - - char *path = m_new(char, pathlen + 1); - memcpy(path, path_in, pathlen); - path[pathlen] = '\0'; - - // "remove" any extra slahes at the end - while (path[(pathlen - 1)] == '/') { - path[--pathlen] = '\0'; - } - - // is the mount point valid? - if (pathlen < 2 || path[0] !='/' || strchr(&path[1], '/')) { - goto invalid_args; - } - - // now mount it - mount(device, path, pathlen, args[0].u_bool); - - return mp_const_none; - -invalid_args: - mp_raise_msg(&mp_type_OSError, mpexception_value_invalid_arguments); -} -MP_DEFINE_CONST_FUN_OBJ_KW(os_mount_obj, 2, os_mount); - -STATIC mp_obj_t os_unmount(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - - // '/flash' cannot be unmounted, also not the current working directory - if (path_equal(path, "/flash")) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); - } - - // now unmount it - os_fs_mount_t *mount_obj; - if ((mount_obj = osmount_find_by_path(path))) { - unmount (mount_obj); - } else { - mp_raise_ValueError(mpexception_value_invalid_arguments); - } - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_unmount_obj, os_unmount); - -STATIC mp_obj_t os_mkfs(mp_obj_t device) { - const char *path = "/__mkfs__mnt__"; - os_fs_mount_t *mount_obj = NULL; - bool unmt = false; - FRESULT res; - - if (MP_OBJ_IS_STR_OR_BYTES(device)) { - path = mp_obj_str_get_str(device); - // otherwise the relative path check will pass... - if (path[0] != '/') { - mp_raise_msg(&mp_type_OSError, mpexception_value_invalid_arguments); - } - } else { - // mount it briefly - mount(device, path, strlen(path), false); - unmt = true; - } - - byte sfd = 0; - if (!memcmp(path, "/flash", strlen("/flash"))) { - sfd = 1; - } else if ((mount_obj = osmount_find_by_path(path))) { - if (mount_obj->device != (mp_obj_t)&pybsd_obj && - mp_obj_get_int(mp_call_method_n_kw(0, 0, mount_obj->count)) < 2048) { - sfd = 1; - } - } - - // now format the device - res = f_mkfs(path, sfd, 0); - - if (unmt && mount_obj) { - unmount (mount_obj); - } - - if (res != FR_OK) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkfs_obj, os_mkfs); - STATIC mp_obj_t os_dupterm(uint n_args, const mp_obj_t *args) { if (n_args == 0) { if (MP_STATE_PORT(os_term_dup_obj) == MP_OBJ_NULL) { @@ -576,26 +152,28 @@ 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_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_rename), (mp_obj_t)&os_rename_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_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_ilistdir), (mp_obj_t)&mp_vfs_ilistdir_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_rename), (mp_obj_t)&mp_vfs_rename_obj}, + { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&mp_vfs_remove_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_unlink), (mp_obj_t)&mp_vfs_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_urandom), (mp_obj_t)&os_urandom_obj }, // MicroPython additions - { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&os_mount_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_unmount), (mp_obj_t)&os_unmount_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mkfs), (mp_obj_t)&os_mkfs_obj }, + // removed: mkfs + // renamed: unmount -> umount + { 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 }, { MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&os_dupterm_obj }, - - /// \constant sep - separation character used in paths - { 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 1ebf16cdf..4c8bc9659 100644 --- a/cc3200/mods/moduos.h +++ b/cc3200/mods/moduos.h @@ -28,22 +28,11 @@ #ifndef MODUOS_H_ #define MODUOS_H_ -#include "ff.h" +#include "py/obj.h" /****************************************************************************** DEFINE PUBLIC TYPES ******************************************************************************/ -typedef struct _os_fs_mount_t { - mp_obj_t device; - const char *path; - mp_uint_t pathlen; - mp_obj_t readblocks[4]; - mp_obj_t writeblocks[4]; - mp_obj_t sync[2]; - mp_obj_t count[2]; - FATFS fatfs; - uint8_t vol; -} os_fs_mount_t; typedef struct _os_term_dup_obj_t { mp_obj_t stream_o; @@ -54,9 +43,6 @@ typedef struct _os_term_dup_obj_t { /****************************************************************************** DECLARE PUBLIC FUNCTIONS ******************************************************************************/ -void moduos_init0 (void); -os_fs_mount_t *osmount_find_by_path (const char *path); -os_fs_mount_t *osmount_find_by_volume (uint8_t vol); void osmount_unmount_all (void); #endif // MODUOS_H_ diff --git a/cc3200/mods/modusocket.c b/cc3200/mods/modusocket.c index 9554c2608..ca28bf7ba 100644 --- a/cc3200/mods/modusocket.c +++ b/cc3200/mods/modusocket.c @@ -34,12 +34,254 @@ #include "py/objstr.h" #include "py/runtime.h" #include "py/stream.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" -#include "modwlan.h" #include "modusocket.h" #include "mpexception.h" +/******************************************************************************/ +// The following set of macros and functions provide a glue between the CC3100 +// simplelink layer and the functions/methods provided by the usocket module. +// They were historically in a separate file because usocket was designed to +// work with multiple NICs, and the wlan_XXX functions just provided one +// particular NIC implementation (that of the CC3100). But the CC3200 port only +// supports a single NIC (being the CC3100) so it's unnecessary and inefficient +// to provide an intermediate wrapper layer. Hence the wlan_XXX functions +// are provided below as static functions so they can be inlined directly by +// the corresponding usocket calls. + +#define WLAN_MAX_RX_SIZE 16000 +#define WLAN_MAX_TX_SIZE 1476 + +#define MAKE_SOCKADDR(addr, ip, port) SlSockAddr_t addr; \ + addr.sa_family = SL_AF_INET; \ + addr.sa_data[0] = port >> 8; \ + addr.sa_data[1] = port; \ + addr.sa_data[2] = ip[3]; \ + addr.sa_data[3] = ip[2]; \ + addr.sa_data[4] = ip[1]; \ + addr.sa_data[5] = ip[0]; + +#define UNPACK_SOCKADDR(addr, ip, port) port = (addr.sa_data[0] << 8) | addr.sa_data[1]; \ + ip[0] = addr.sa_data[5]; \ + ip[1] = addr.sa_data[4]; \ + ip[2] = addr.sa_data[3]; \ + ip[3] = addr.sa_data[2]; + +STATIC int wlan_gethostbyname(const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family) { + uint32_t ip; + int result = sl_NetAppDnsGetHostByName((_i8 *)name, (_u16)len, (_u32*)&ip, (_u8)family); + out_ip[0] = ip; + out_ip[1] = ip >> 8; + out_ip[2] = ip >> 16; + out_ip[3] = ip >> 24; + return result; +} + +STATIC int wlan_socket_socket(mod_network_socket_obj_t *s, int *_errno) { + int16_t sd = sl_Socket(s->sock_base.u_param.domain, s->sock_base.u_param.type, s->sock_base.u_param.proto); + if (sd < 0) { + *_errno = sd; + return -1; + } + s->sock_base.sd = sd; + return 0; +} + +STATIC void wlan_socket_close(mod_network_socket_obj_t *s) { + // this is to prevent the finalizer to close a socket that failed when being created + if (s->sock_base.sd >= 0) { + modusocket_socket_delete(s->sock_base.sd); + sl_Close(s->sock_base.sd); + s->sock_base.sd = -1; + } +} + +STATIC int wlan_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) { + MAKE_SOCKADDR(addr, ip, port) + int ret = sl_Bind(s->sock_base.sd, &addr, sizeof(addr)); + if (ret != 0) { + *_errno = ret; + return -1; + } + return 0; +} + +STATIC int wlan_socket_listen(mod_network_socket_obj_t *s, mp_int_t backlog, int *_errno) { + int ret = sl_Listen(s->sock_base.sd, backlog); + if (ret != 0) { + *_errno = ret; + return -1; + } + return 0; +} + +STATIC int wlan_socket_accept(mod_network_socket_obj_t *s, mod_network_socket_obj_t *s2, byte *ip, mp_uint_t *port, int *_errno) { + // accept incoming connection + int16_t sd; + SlSockAddr_t addr; + SlSocklen_t addr_len = sizeof(addr); + + sd = sl_Accept(s->sock_base.sd, &addr, &addr_len); + // save the socket descriptor + s2->sock_base.sd = sd; + if (sd < 0) { + *_errno = sd; + return -1; + } + + // return ip and port + UNPACK_SOCKADDR(addr, ip, *port); + return 0; +} + +STATIC int wlan_socket_connect(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) { + MAKE_SOCKADDR(addr, ip, port) + int ret = sl_Connect(s->sock_base.sd, &addr, sizeof(addr)); + if (ret != 0) { + *_errno = ret; + return -1; + } + return 0; +} + +STATIC int wlan_socket_send(mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno) { + mp_int_t bytes = 0; + if (len > 0) { + bytes = sl_Send(s->sock_base.sd, (const void *)buf, len, 0); + } + if (bytes <= 0) { + *_errno = bytes; + return -1; + } + return bytes; +} + +STATIC int wlan_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno) { + int ret = sl_Recv(s->sock_base.sd, buf, MIN(len, WLAN_MAX_RX_SIZE), 0); + if (ret < 0) { + *_errno = ret; + return -1; + } + return ret; +} + +STATIC int wlan_socket_sendto( mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) { + MAKE_SOCKADDR(addr, ip, port) + int ret = sl_SendTo(s->sock_base.sd, (byte*)buf, len, 0, (SlSockAddr_t*)&addr, sizeof(addr)); + if (ret < 0) { + *_errno = ret; + return -1; + } + return ret; +} + +STATIC int wlan_socket_recvfrom(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) { + SlSockAddr_t addr; + SlSocklen_t addr_len = sizeof(addr); + mp_int_t ret = sl_RecvFrom(s->sock_base.sd, buf, MIN(len, WLAN_MAX_RX_SIZE), 0, &addr, &addr_len); + if (ret < 0) { + *_errno = ret; + return -1; + } + UNPACK_SOCKADDR(addr, ip, *port); + return ret; +} + +STATIC int wlan_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) { + int ret = sl_SetSockOpt(s->sock_base.sd, level, opt, optval, optlen); + if (ret < 0) { + *_errno = ret; + return -1; + } + return 0; +} + +STATIC int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int *_errno) { + int ret; + bool has_timeout; + if (timeout_s == 0 || timeout_s == -1) { + SlSockNonblocking_t option; + if (timeout_s == 0) { + // set non-blocking mode + option.NonblockingEnabled = 1; + } else { + // set blocking mode + option.NonblockingEnabled = 0; + } + ret = sl_SetSockOpt(s->sock_base.sd, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &option, sizeof(option)); + has_timeout = false; + } else { + // set timeout + struct SlTimeval_t timeVal; + timeVal.tv_sec = timeout_s; // seconds + timeVal.tv_usec = 0; // microseconds. 10000 microseconds resolution + ret = sl_SetSockOpt(s->sock_base.sd, SL_SOL_SOCKET, SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal)); + has_timeout = true; + } + + if (ret != 0) { + *_errno = ret; + return -1; + } + + s->sock_base.has_timeout = has_timeout; + return 0; +} + +STATIC int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno) { + mp_int_t ret; + if (request == MP_STREAM_POLL) { + mp_uint_t flags = arg; + ret = 0; + int32_t sd = s->sock_base.sd; + + // init fds + SlFdSet_t rfds, wfds, xfds; + SL_FD_ZERO(&rfds); + SL_FD_ZERO(&wfds); + SL_FD_ZERO(&xfds); + + // set fds if needed + if (flags & MP_STREAM_POLL_RD) { + SL_FD_SET(sd, &rfds); + } + if (flags & MP_STREAM_POLL_WR) { + SL_FD_SET(sd, &wfds); + } + if (flags & MP_STREAM_POLL_HUP) { + SL_FD_SET(sd, &xfds); + } + + // call simplelink's select with minimum timeout + SlTimeval_t tv; + tv.tv_sec = 0; + tv.tv_usec = 1; + int32_t nfds = sl_Select(sd + 1, &rfds, &wfds, &xfds, &tv); + + // check for errors + if (nfds == -1) { + *_errno = nfds; + return -1; + } + + // check return of select + if (SL_FD_ISSET(sd, &rfds)) { + ret |= MP_STREAM_POLL_RD; + } + if (SL_FD_ISSET(sd, &wfds)) { + ret |= MP_STREAM_POLL_WR; + } + if (SL_FD_ISSET(sd, &xfds)) { + ret |= MP_STREAM_POLL_HUP; + } + } else { + *_errno = MP_EINVAL; + ret = MP_STREAM_ERROR; + } + return ret; +} + /****************************************************************************** DEFINE PRIVATE CONSTANTS ******************************************************************************/ @@ -95,13 +337,13 @@ void modusocket_socket_delete (int16_t sd) { } void modusocket_enter_sleep (void) { - fd_set socketset; + SlFdSet_t socketset; int16_t maxfd = 0; for (int i = 0; i < MOD_NETWORK_MAX_SOCKETS; i++) { int16_t sd; if ((sd = modusocket_sockets[i].sd) >= 0) { - FD_SET(sd, &socketset); + SL_FD_SET(sd, &socketset); maxfd = (maxfd > sd) ? maxfd : sd; } } @@ -133,9 +375,9 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t // create socket object mod_network_socket_obj_t *s = m_new_obj_with_finaliser(mod_network_socket_obj_t); s->base.type = (mp_obj_t)&socket_type; - s->sock_base.u_param.domain = AF_INET; - s->sock_base.u_param.type = SOCK_STREAM; - s->sock_base.u_param.proto = IPPROTO_TCP; + s->sock_base.u_param.domain = SL_AF_INET; + s->sock_base.u_param.type = SL_SOCK_STREAM; + s->sock_base.u_param.proto = SL_IPPROTO_TCP; s->sock_base.u_param.fileno = -1; s->sock_base.has_timeout = false; s->sock_base.cert_req = false; @@ -180,7 +422,7 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE); // call the NIC to bind the socket - int _errno; + int _errno = 0; if (wlan_socket_bind(self, ip, port, &_errno) != 0) { mp_raise_OSError(-_errno); } @@ -217,8 +459,8 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) { // accept the incoming connection uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE]; - mp_uint_t port; - int _errno; + mp_uint_t port = 0; + int _errno = 0; if (wlan_socket_accept(self, socket2, ip, &port, &_errno) != 0) { mp_raise_OSError(-_errno); } @@ -277,8 +519,8 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { int _errno; mp_int_t ret = wlan_socket_recv(self, (byte*)vstr.buf, len, &_errno); if (ret < 0) { - if (_errno == EAGAIN && self->sock_base.has_timeout) { - mp_raise_msg(&mp_type_TimeoutError, "timed out"); + if (_errno == MP_EAGAIN && self->sock_base.has_timeout) { + mp_raise_OSError(MP_ETIMEDOUT); } mp_raise_OSError(-_errno); } @@ -304,7 +546,7 @@ STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_ mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE); // call the nic to sendto - int _errno; + int _errno = 0; mp_int_t ret = wlan_socket_sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno); if (ret < 0) { mp_raise_OSError(-_errno); @@ -319,12 +561,12 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) { vstr_t vstr; vstr_init_len(&vstr, mp_obj_get_int(len_in)); byte ip[4]; - mp_uint_t port; - int _errno; + mp_uint_t port = 0; + int _errno = 0; mp_int_t ret = wlan_socket_recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno); if (ret < 0) { - if (_errno == EAGAIN && self->sock_base.has_timeout) { - mp_raise_msg(&mp_type_TimeoutError, "timed out"); + if (_errno == MP_EAGAIN && self->sock_base.has_timeout) { + mp_raise_OSError(MP_ETIMEDOUT); } mp_raise_OSError(-_errno); } @@ -495,19 +737,19 @@ STATIC const mp_obj_type_t socket_type = { // function usocket.getaddrinfo(host, port) /// \function getaddrinfo(host, port) STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { - mp_uint_t hlen; + size_t hlen; const char *host = mp_obj_str_get_data(host_in, &hlen); mp_int_t port = mp_obj_get_int(port_in); // ipv4 only uint8_t out_ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE]; - int32_t result = wlan_gethostbyname(host, hlen, out_ip, AF_INET); + int32_t result = wlan_gethostbyname(host, hlen, out_ip, SL_AF_INET); if (result < 0) { mp_raise_OSError(-result); } mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); - tuple->items[0] = MP_OBJ_NEW_SMALL_INT(AF_INET); - tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM); + tuple->items[0] = MP_OBJ_NEW_SMALL_INT(SL_AF_INET); + tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SL_SOCK_STREAM); tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_LITTLE); @@ -521,19 +763,15 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_usocket_getaddrinfo_obj }, - // class exceptions - { MP_OBJ_NEW_QSTR(MP_QSTR_error), (mp_obj_t)&mp_type_OSError }, - { MP_OBJ_NEW_QSTR(MP_QSTR_timeout), (mp_obj_t)&mp_type_TimeoutError }, - // class constants - { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(SL_AF_INET) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SL_SOCK_STREAM) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SL_SOCK_DGRAM) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_SEC), MP_OBJ_NEW_SMALL_INT(SL_SEC_SOCKET) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_TCP), MP_OBJ_NEW_SMALL_INT(IPPROTO_TCP) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_UDP), MP_OBJ_NEW_SMALL_INT(IPPROTO_UDP) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_TCP), MP_OBJ_NEW_SMALL_INT(SL_IPPROTO_TCP) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_UDP), MP_OBJ_NEW_SMALL_INT(SL_IPPROTO_UDP) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table); diff --git a/cc3200/mods/modussl.c b/cc3200/mods/modussl.c index 7a4787b6b..95ecdbce7 100644 --- a/cc3200/mods/modussl.c +++ b/cc3200/mods/modussl.c @@ -25,7 +25,6 @@ */ #include <stdint.h> -#include <std.h> #include "simplelink.h" #include "py/mpconfig.h" diff --git a/cc3200/mods/modutime.c b/cc3200/mods/modutime.c index 7ea8df1ef..48fde67e7 100644 --- a/cc3200/mods/modutime.c +++ b/cc3200/mods/modutime.c @@ -33,7 +33,8 @@ #include "py/obj.h" #include "py/smallint.h" #include "py/mphal.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" +#include "extmod/utime_mphal.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" @@ -41,7 +42,6 @@ #include "prcm.h" #include "systick.h" #include "pybrtc.h" -#include "mpsystick.h" #include "mpexception.h" #include "utils.h" @@ -102,7 +102,7 @@ STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime); STATIC mp_obj_t time_mktime(mp_obj_t tuple) { - mp_uint_t len; + size_t len; mp_obj_t *elem; mp_obj_get_array(tuple, &len, &elem); @@ -131,50 +131,6 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { } MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); -STATIC mp_obj_t time_sleep_ms (mp_obj_t ms_in) { - mp_int_t ms = mp_obj_get_int(ms_in); - if (ms > 0) { - mp_hal_delay_ms(ms); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms); - -STATIC mp_obj_t time_sleep_us (mp_obj_t usec_in) { - mp_int_t usec = mp_obj_get_int(usec_in); - if (usec > 0) { - UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec)); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us); - -STATIC mp_obj_t time_ticks_ms(void) { - // We want to "cast" the 32 bit unsigned into a 30-bit small-int - return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & MP_SMALL_INT_POSITIVE_MASK); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms); - -STATIC mp_obj_t time_ticks_us(void) { - // We want to "cast" the 32 bit unsigned into a 30-bit small-int - return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds() & MP_SMALL_INT_POSITIVE_MASK); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us); - -STATIC mp_obj_t time_ticks_cpu(void) { - // We want to "cast" the 32 bit unsigned into a 30-bit small-int - return MP_OBJ_NEW_SMALL_INT((SysTickPeriodGet() - SysTickValueGet()) & MP_SMALL_INT_POSITIVE_MASK); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu); - -STATIC mp_obj_t time_ticks_diff(mp_obj_t t0, mp_obj_t t1) { - // We want to "cast" the 32 bit unsigned into a 30-bit small-int - uint32_t start = mp_obj_get_int(t0); - uint32_t end = mp_obj_get_int(t1); - return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff); - STATIC const mp_map_elem_t time_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) }, @@ -184,12 +140,13 @@ STATIC const mp_map_elem_t time_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj }, // MicroPython additions - { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&time_ticks_ms_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&time_ticks_us_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&mp_utime_sleep_ms_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mp_utime_sleep_us_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_add), (mp_obj_t)&mp_utime_ticks_add_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/cc3200/mods/modwlan.c b/cc3200/mods/modwlan.c index a85921cc5..dacf59a7c 100644 --- a/cc3200/mods/modwlan.c +++ b/cc3200/mods/modwlan.c @@ -26,7 +26,7 @@ #include <stdint.h> #include <stdbool.h> -#include "std.h" +#include <stdio.h> #include "simplelink.h" #include "py/ioctl.h" @@ -36,13 +36,8 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mphal.h" -#include "inc/hw_types.h" -#include "inc/hw_ints.h" -#include "inc/hw_memmap.h" -#include "rom_map.h" -#include "prcm.h" -#include "timeutils.h" -#include "netutils.h" +#include "lib/timeutils/timeutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "modusocket.h" #include "modwlan.h" @@ -118,26 +113,6 @@ typedef enum{ #define ASSERT_ON_ERROR(x) ASSERT((x) >= 0) -#define IPV4_ADDR_STR_LEN_MAX (16) - -#define WLAN_MAX_RX_SIZE 16000 -#define WLAN_MAX_TX_SIZE 1476 - -#define MAKE_SOCKADDR(addr, ip, port) sockaddr addr; \ - addr.sa_family = AF_INET; \ - addr.sa_data[0] = port >> 8; \ - addr.sa_data[1] = port; \ - addr.sa_data[2] = ip[3]; \ - addr.sa_data[3] = ip[2]; \ - addr.sa_data[4] = ip[1]; \ - addr.sa_data[5] = ip[0]; - -#define UNPACK_SOCKADDR(addr, ip, port) port = (addr.sa_data[0] << 8) | addr.sa_data[1]; \ - ip[0] = addr.sa_data[5]; \ - ip[1] = addr.sa_data[4]; \ - ip[2] = addr.sa_data[3]; \ - ip[3] = addr.sa_data[2]; - /****************************************************************************** DECLARE PRIVATE DATA ******************************************************************************/ @@ -162,7 +137,9 @@ STATIC const mp_irq_methods_t wlan_irq_methods; /****************************************************************************** DECLARE PUBLIC DATA ******************************************************************************/ +#ifdef SL_PLATFORM_MULTI_THREADED OsiLockObj_t wlan_LockObj; +#endif /****************************************************************************** DECLARE PRIVATE FUNCTIONS @@ -397,14 +374,18 @@ void SimpleLinkSockEventHandler(SlSockEvent_t *pSock) { __attribute__ ((section (".boot"))) void wlan_pre_init (void) { // create the wlan lock + #ifdef SL_PLATFORM_MULTI_THREADED ASSERT(OSI_OK == sl_LockObjCreate(&wlan_LockObj, "WlanLock")); + #endif } void wlan_first_start (void) { if (wlan_obj.mode < 0) { CLR_STATUS_BIT_ALL(wlan_obj.status); wlan_obj.mode = sl_Start(0, 0, 0); + #ifdef SL_PLATFORM_MULTI_THREADED sl_LockObjUnlock (&wlan_LockObj); + #endif } // get the mac address @@ -513,7 +494,9 @@ void wlan_update(void) { void wlan_stop (uint32_t timeout) { wlan_servers_stop(); + #ifdef SL_PLATFORM_MULTI_THREADED sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER); + #endif sl_Stop(timeout); wlan_clear_data(); wlan_obj.mode = -1; @@ -569,11 +552,15 @@ STATIC void wlan_clear_data (void) { STATIC void wlan_reenable (SlWlanMode_t mode) { // stop and start again + #ifdef SL_PLATFORM_MULTI_THREADED sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER); + #endif sl_Stop(SL_STOP_TIMEOUT); wlan_clear_data(); wlan_obj.mode = sl_Start(0, 0, 0); + #ifdef SL_PLATFORM_MULTI_THREADED sl_LockObjUnlock (&wlan_LockObj); + #endif ASSERT (wlan_obj.mode == mode); } @@ -787,7 +774,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) { wlan_validate_mode(mode); // get the ssid - mp_uint_t ssid_len = 0; + size_t ssid_len = 0; const char *ssid = NULL; if (args[1].u_obj != NULL) { ssid = mp_obj_str_get_data(args[1].u_obj, &ssid_len); @@ -796,7 +783,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) { // get the auth config uint8_t auth = SL_SEC_TYPE_OPEN; - mp_uint_t key_len = 0; + size_t key_len = 0; const char *key = NULL; if (args[2].u_obj != mp_const_none) { mp_obj_t *sec; @@ -811,8 +798,9 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) { wlan_validate_channel(channel); // get the antenna type - uint8_t antenna = args[4].u_int; + uint8_t antenna = 0; #if MICROPY_HW_ANTENNA_DIVERSITY + antenna = args[4].u_int; wlan_validate_antenna(antenna); #endif @@ -828,7 +816,9 @@ STATIC const mp_arg_t wlan_init_args[] = { { MP_QSTR_ssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_auth, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, + #if MICROPY_HW_ANTENNA_DIVERSITY { MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ANTENNA_TYPE_INTERNAL} }, + #endif }; STATIC mp_obj_t wlan_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { // parse args @@ -847,7 +837,7 @@ STATIC mp_obj_t wlan_make_new(const mp_obj_type_t *type, size_t n_args, size_t n if (n_args > 1 || n_kw > 0) { // check the peripheral id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // start the peripheral wlan_init_helper(self, &args[1]); @@ -871,7 +861,7 @@ STATIC mp_obj_t wlan_scan(mp_obj_t self_in) { // check for correct wlan mode if (wlan_obj.mode == ROLE_AP) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } Sl_WlanNetworkEntry_t wlanEntry; @@ -925,7 +915,7 @@ STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_ // check for the correct wlan mode if (wlan_obj.mode == ROLE_AP) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } // parse args @@ -933,13 +923,13 @@ STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); // get the ssid - mp_uint_t ssid_len; + size_t ssid_len; const char *ssid = mp_obj_str_get_data(args[0].u_obj, &ssid_len); wlan_validate_ssid_len(ssid_len); // get the auth config uint8_t auth = SL_SEC_TYPE_OPEN; - mp_uint_t key_len = 0; + size_t key_len = 0; const char *key = NULL; if (args[1].u_obj != mp_const_none) { mp_obj_t *sec; @@ -973,7 +963,7 @@ STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_ modwlan_Status_t status; status = wlan_do_connect (ssid, ssid_len, bssid, auth, key, key_len, timeout); if (status == MODWLAN_ERROR_TIMEOUT) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_ETIMEDOUT); } else if (status == MODWLAN_ERROR_INVALID_PARAMS) { mp_raise_ValueError(mpexception_value_invalid_arguments); } @@ -1004,7 +994,7 @@ STATIC mp_obj_t wlan_ifconfig (mp_uint_t n_args, const mp_obj_t *pos_args, mp_ma // check the interface id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_EPERM); } // get the configuration @@ -1088,7 +1078,7 @@ STATIC mp_obj_t wlan_ssid (mp_uint_t n_args, const mp_obj_t *args) { if (n_args == 1) { return mp_obj_new_str((const char *)self->ssid, strlen((const char *)self->ssid), false); } else { - mp_uint_t len; + size_t len; const char *ssid = mp_obj_str_get_data(args[1], &len); wlan_validate_ssid_len(len); wlan_set_ssid(ssid, len, false); @@ -1112,7 +1102,7 @@ STATIC mp_obj_t wlan_auth (mp_uint_t n_args, const mp_obj_t *args) { } else { // get the auth config uint8_t auth = SL_SEC_TYPE_OPEN; - mp_uint_t key_len = 0; + size_t key_len = 0; const char *key = NULL; if (args[1] != mp_const_none) { mp_obj_t *sec; @@ -1235,13 +1225,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_irq_obj, 1, wlan_irq); // strcpy(urn, p); // // if (sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, len, (unsigned char *)urn) < 0) { -// mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); +// mp_raise_OSError(MP_EIO); // } // } // else { // // get the URN // if (sl_NetAppGet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, &len, (uint8_t *)urn) < 0) { -// mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); +// mp_raise_OSError(MP_EIO); // } // return mp_obj_new_str(urn, (len - 1), false); // } @@ -1255,9 +1245,9 @@ STATIC mp_obj_t wlan_print_ver(void) { byte config_opt = SL_DEVICE_GENERAL_VERSION; byte config_len = sizeof(ver); sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &config_opt, &config_len, (byte*)&ver); - printf("NWP: %d.%d.%d.%d\n", ver.NwpVersion[0], ver.NwpVersion[1], ver.NwpVersion[2], ver.NwpVersion[3]); - printf("MAC: %d.%d.%d.%d\n", ver.ChipFwAndPhyVersion.FwVersion[0], ver.ChipFwAndPhyVersion.FwVersion[1], - ver.ChipFwAndPhyVersion.FwVersion[2], ver.ChipFwAndPhyVersion.FwVersion[3]); + printf("NWP: %d.%d.%d.%d\n", (int)ver.NwpVersion[0], (int)ver.NwpVersion[1], (int)ver.NwpVersion[2], (int)ver.NwpVersion[3]); + printf("MAC: %d.%d.%d.%d\n", (int)ver.ChipFwAndPhyVersion.FwVersion[0], (int)ver.ChipFwAndPhyVersion.FwVersion[1], + (int)ver.ChipFwAndPhyVersion.FwVersion[2], (int)ver.ChipFwAndPhyVersion.FwVersion[3]); printf("PHY: %d.%d.%d.%d\n", ver.ChipFwAndPhyVersion.PhyVersion[0], ver.ChipFwAndPhyVersion.PhyVersion[1], ver.ChipFwAndPhyVersion.PhyVersion[2], ver.ChipFwAndPhyVersion.PhyVersion[3]); return mp_const_none; @@ -1289,8 +1279,10 @@ STATIC const mp_map_elem_t wlan_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_WEP), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WEP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_WPA), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPA_WPA2) }, { MP_OBJ_NEW_QSTR(MP_QSTR_WPA2), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPA_WPA2) }, + #if MICROPY_HW_ANTENNA_DIVERSITY { MP_OBJ_NEW_QSTR(MP_QSTR_INT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_INTERNAL) }, { MP_OBJ_NEW_QSTR(MP_QSTR_EXT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_EXTERNAL) }, + #endif { MP_OBJ_NEW_QSTR(MP_QSTR_ANY_EVENT), MP_OBJ_NEW_SMALL_INT(MODWLAN_WIFI_EVENT_ANY) }, }; STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table); @@ -1310,220 +1302,3 @@ STATIC const mp_irq_methods_t wlan_irq_methods = { .disable = wlan_lpds_irq_disable, .flags = wlan_irq_flags, }; - -/******************************************************************************/ -// Micro Python bindings; WLAN socket - -int wlan_gethostbyname(const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family) { - uint32_t ip; - int result = sl_NetAppDnsGetHostByName((_i8 *)name, (_u16)len, (_u32*)&ip, (_u8)family); - out_ip[0] = ip; - out_ip[1] = ip >> 8; - out_ip[2] = ip >> 16; - out_ip[3] = ip >> 24; - return result; -} - -int wlan_socket_socket(mod_network_socket_obj_t *s, int *_errno) { - int16_t sd = sl_Socket(s->sock_base.u_param.domain, s->sock_base.u_param.type, s->sock_base.u_param.proto); - if (sd < 0) { - *_errno = sd; - return -1; - } - s->sock_base.sd = sd; - return 0; -} - -void wlan_socket_close(mod_network_socket_obj_t *s) { - // this is to prevent the finalizer to close a socket that failed when being created - if (s->sock_base.sd >= 0) { - modusocket_socket_delete(s->sock_base.sd); - sl_Close(s->sock_base.sd); - s->sock_base.sd = -1; - } -} - -int wlan_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) { - MAKE_SOCKADDR(addr, ip, port) - int ret = sl_Bind(s->sock_base.sd, &addr, sizeof(addr)); - if (ret != 0) { - *_errno = ret; - return -1; - } - return 0; -} - -int wlan_socket_listen(mod_network_socket_obj_t *s, mp_int_t backlog, int *_errno) { - int ret = sl_Listen(s->sock_base.sd, backlog); - if (ret != 0) { - *_errno = ret; - return -1; - } - return 0; -} - -int wlan_socket_accept(mod_network_socket_obj_t *s, mod_network_socket_obj_t *s2, byte *ip, mp_uint_t *port, int *_errno) { - // accept incoming connection - int16_t sd; - sockaddr addr; - socklen_t addr_len = sizeof(addr); - - sd = sl_Accept(s->sock_base.sd, &addr, &addr_len); - // save the socket descriptor - s2->sock_base.sd = sd; - if (sd < 0) { - *_errno = sd; - return -1; - } - - // return ip and port - UNPACK_SOCKADDR(addr, ip, *port); - return 0; -} - -int wlan_socket_connect(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) { - MAKE_SOCKADDR(addr, ip, port) - int ret = sl_Connect(s->sock_base.sd, &addr, sizeof(addr)); - if (ret != 0) { - *_errno = ret; - return -1; - } - return 0; -} - -int wlan_socket_send(mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno) { - mp_int_t bytes = 0; - if (len > 0) { - bytes = sl_Send(s->sock_base.sd, (const void *)buf, len, 0); - } - if (bytes <= 0) { - *_errno = bytes; - return -1; - } - return bytes; -} - -int wlan_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno) { - int ret = sl_Recv(s->sock_base.sd, buf, MIN(len, WLAN_MAX_RX_SIZE), 0); - if (ret < 0) { - *_errno = ret; - return -1; - } - return ret; -} - -int wlan_socket_sendto( mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) { - MAKE_SOCKADDR(addr, ip, port) - int ret = sl_SendTo(s->sock_base.sd, (byte*)buf, len, 0, (sockaddr*)&addr, sizeof(addr)); - if (ret < 0) { - *_errno = ret; - return -1; - } - return ret; -} - -int wlan_socket_recvfrom(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) { - sockaddr addr; - socklen_t addr_len = sizeof(addr); - mp_int_t ret = sl_RecvFrom(s->sock_base.sd, buf, MIN(len, WLAN_MAX_RX_SIZE), 0, &addr, &addr_len); - if (ret < 0) { - *_errno = ret; - return -1; - } - UNPACK_SOCKADDR(addr, ip, *port); - return ret; -} - -int wlan_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) { - int ret = sl_SetSockOpt(s->sock_base.sd, level, opt, optval, optlen); - if (ret < 0) { - *_errno = ret; - return -1; - } - return 0; -} - -int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int *_errno) { - int ret; - bool has_timeout; - if (timeout_s == 0 || timeout_s == -1) { - SlSockNonblocking_t option; - if (timeout_s == 0) { - // set non-blocking mode - option.NonblockingEnabled = 1; - } else { - // set blocking mode - option.NonblockingEnabled = 0; - } - ret = sl_SetSockOpt(s->sock_base.sd, SOL_SOCKET, SO_NONBLOCKING, &option, sizeof(option)); - has_timeout = false; - } else { - // set timeout - struct SlTimeval_t timeVal; - timeVal.tv_sec = timeout_s; // seconds - timeVal.tv_usec = 0; // microseconds. 10000 microseconds resolution - ret = sl_SetSockOpt(s->sock_base.sd, SOL_SOCKET, SO_RCVTIMEO, &timeVal, sizeof(timeVal)); - has_timeout = true; - } - - if (ret != 0) { - *_errno = ret; - return -1; - } - - s->sock_base.has_timeout = has_timeout; - return 0; -} - -int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno) { - mp_int_t ret; - if (request == MP_STREAM_POLL) { - mp_uint_t flags = arg; - ret = 0; - int32_t sd = s->sock_base.sd; - - // init fds - fd_set rfds, wfds, xfds; - FD_ZERO(&rfds); - FD_ZERO(&wfds); - FD_ZERO(&xfds); - - // set fds if needed - if (flags & MP_STREAM_POLL_RD) { - FD_SET(sd, &rfds); - } - if (flags & MP_STREAM_POLL_WR) { - FD_SET(sd, &wfds); - } - if (flags & MP_STREAM_POLL_HUP) { - FD_SET(sd, &xfds); - } - - // call simplelink's select with minimum timeout - SlTimeval_t tv; - tv.tv_sec = 0; - tv.tv_usec = 1; - int32_t nfds = sl_Select(sd + 1, &rfds, &wfds, &xfds, &tv); - - // check for errors - if (nfds == -1) { - *_errno = nfds; - return -1; - } - - // check return of select - if (FD_ISSET(sd, &rfds)) { - ret |= MP_STREAM_POLL_RD; - } - if (FD_ISSET(sd, &wfds)) { - ret |= MP_STREAM_POLL_WR; - } - if (FD_ISSET(sd, &xfds)) { - ret |= MP_STREAM_POLL_HUP; - } - } else { - *_errno = EINVAL; - ret = MP_STREAM_ERROR; - } - return ret; -} diff --git a/cc3200/mods/modwlan.h b/cc3200/mods/modwlan.h index 5899f87b2..3bfd1fbbf 100644 --- a/cc3200/mods/modwlan.h +++ b/cc3200/mods/modwlan.h @@ -97,19 +97,4 @@ extern bool wlan_is_connected (void); extern void wlan_set_current_time (uint32_t seconds_since_2000); extern void wlan_off_on (void); -extern int wlan_gethostbyname(const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family); -extern int wlan_socket_socket(mod_network_socket_obj_t *s, int *_errno); -extern void wlan_socket_close(mod_network_socket_obj_t *s); -extern int wlan_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno); -extern int wlan_socket_listen(mod_network_socket_obj_t *s, mp_int_t backlog, int *_errno); -extern int wlan_socket_accept(mod_network_socket_obj_t *s, mod_network_socket_obj_t *s2, byte *ip, mp_uint_t *port, int *_errno); -extern int wlan_socket_connect(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno); -extern int wlan_socket_send(mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno); -extern int wlan_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno); -extern int wlan_socket_sendto( mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno); -extern int wlan_socket_recvfrom(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno); -extern int wlan_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno); -extern int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int *_errno); -extern int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno); - #endif /* MODWLAN_H_ */ diff --git a/cc3200/mods/pybadc.c b/cc3200/mods/pybadc.c index e63bebbd0..696e7650b 100644 --- a/cc3200/mods/pybadc.c +++ b/cc3200/mods/pybadc.c @@ -33,6 +33,7 @@ #include "py/runtime.h" #include "py/binary.h" #include "py/gc.h" +#include "py/mperrno.h" #include "bufhelper.h" #include "inc/hw_types.h" #include "inc/hw_adc.h" @@ -104,7 +105,7 @@ STATIC void pyb_adc_init (pyb_adc_obj_t *self) { STATIC void pyb_adc_check_init(void) { // not initialized if (!pyb_adc_obj.enabled) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } } @@ -149,7 +150,7 @@ STATIC mp_obj_t adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ // check the peripheral id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // check the number of bits @@ -206,7 +207,7 @@ STATIC mp_obj_t adc_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t if (args[0].u_obj != MP_OBJ_NULL) { ch_id = mp_obj_get_int(args[0].u_obj); if (ch_id >= PYB_ADC_NUM_CHANNELS) { - mp_raise_ValueError(mpexception_os_resource_not_avaliable); + mp_raise_ValueError(mpexception_value_invalid_arguments); } else if (args[1].u_obj != mp_const_none) { uint pin_ch_id = pin_find_peripheral_type (args[1].u_obj, PIN_FN_ADC, 0); if (ch_id != pin_ch_id) { @@ -277,7 +278,7 @@ STATIC mp_obj_t adc_channel_value(mp_obj_t self_in) { // the channel must be enabled if (!self->enabled) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } // wait until a new value is available diff --git a/cc3200/mods/pybflash.c b/cc3200/mods/pybflash.c new file mode 100644 index 000000000..f5af79dbf --- /dev/null +++ b/cc3200/mods/pybflash.c @@ -0,0 +1,109 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-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 <stdint.h> +//#include <string.h> + +#include "py/runtime.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#include "extmod/vfs_fat.h" + +#include "fatfs/src/drivers/sflash_diskio.h" +#include "mods/pybflash.h" + +/******************************************************************************/ +// MicroPython bindings to expose the internal flash as an object with the +// block protocol. + +// there is a singleton Flash object +STATIC const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type}; + +STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + // check arguments + mp_arg_check_num(n_args, n_kw, 0, 0, false); + + // return singleton object + return (mp_obj_t)&pyb_flash_obj; +} + +STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); + DRESULT res = sflash_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SFLASH_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks); + +STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + DRESULT res = sflash_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SFLASH_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks); + +STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) { + mp_int_t cmd = mp_obj_get_int(cmd_in); + switch (cmd) { + case BP_IOCTL_INIT: return MP_OBJ_NEW_SMALL_INT(sflash_disk_init() != RES_OK); + case BP_IOCTL_DEINIT: sflash_disk_flush(); return MP_OBJ_NEW_SMALL_INT(0); + case BP_IOCTL_SYNC: sflash_disk_flush(); return MP_OBJ_NEW_SMALL_INT(0); + case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(SFLASH_SECTOR_COUNT); + case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(SFLASH_SECTOR_SIZE); + default: return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl); + +STATIC const mp_map_elem_t pyb_flash_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_flash_readblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_flash_writeblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_flash_ioctl_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); + +const mp_obj_type_t pyb_flash_type = { + { &mp_type_type }, + .name = MP_QSTR_Flash, + .make_new = pyb_flash_make_new, + .locals_dict = (mp_obj_t)&pyb_flash_locals_dict, +}; + +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)sflash_disk_read; // native version + vfs->writeblocks[0] = (mp_obj_t)&pyb_flash_writeblocks_obj; + vfs->writeblocks[1] = (mp_obj_t)&pyb_flash_obj; + vfs->writeblocks[2] = (mp_obj_t)sflash_disk_write; // native version + vfs->u.ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj; + vfs->u.ioctl[1] = (mp_obj_t)&pyb_flash_obj; +} diff --git a/cc3200/mods/pybflash.h b/cc3200/mods/pybflash.h new file mode 100644 index 000000000..6f8ddf291 --- /dev/null +++ b/cc3200/mods/pybflash.h @@ -0,0 +1,35 @@ +/* + * 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_CC3200_MODS_PYBFLASH_H +#define MICROPY_INCLUDED_CC3200_MODS_PYBFLASH_H + +#include "py/obj.h" + +extern const mp_obj_type_t pyb_flash_type; + +void pyb_flash_init_vfs(fs_user_mount_t *vfs); + +#endif // MICROPY_INCLUDED_CC3200_MODS_PYBFLASH_H diff --git a/cc3200/mods/pybi2c.c b/cc3200/mods/pybi2c.c index 32451802d..9fc97d914 100644 --- a/cc3200/mods/pybi2c.c +++ b/cc3200/mods/pybi2c.c @@ -30,6 +30,7 @@ #include "py/mpstate.h" #include "py/runtime.h" +#include "py/mperrno.h" #include "py/mphal.h" #include "bufhelper.h" #include "inc/hw_types.h" @@ -58,8 +59,6 @@ typedef struct _pyb_i2c_obj_t { /****************************************************************************** DEFINE CONSTANTS ******************************************************************************/ -#define PYBI2C_MASTER (0) - #define PYBI2C_MIN_BAUD_RATE_HZ (50000) #define PYBI2C_MAX_BAUD_RATE_HZ (400000) @@ -78,7 +77,6 @@ typedef struct _pyb_i2c_obj_t { DECLARE PRIVATE DATA ******************************************************************************/ STATIC pyb_i2c_obj_t pyb_i2c_obj = {.baudrate = 0}; -STATIC const mp_obj_t pyb_i2c_def_pin[2] = {&pin_GP13, &pin_GP23}; /****************************************************************************** DECLARE PRIVATE FUNCTIONS @@ -144,7 +142,7 @@ STATIC bool pyb_i2c_transaction(uint cmd) { STATIC void pyb_i2c_check_init(pyb_i2c_obj_t *self) { // not initialized if (!self->baudrate) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } } @@ -256,7 +254,7 @@ STATIC void pyb_i2c_read_into (mp_arg_val_t *args, vstr_t *vstr) { // receive the data if (!pyb_i2c_read(args[0].u_int, (byte *)vstr->buf, vstr->len)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_EIO); } } @@ -275,8 +273,10 @@ STATIC void pyb_i2c_readmem_into (mp_arg_val_t *args, vstr_t *vstr) { if (pyb_i2c_mem_addr_write (i2c_addr, (byte *)&mem_addr, mem_addr_size)) { // Read the specified length of data if (!pyb_i2c_read (i2c_addr, (byte *)vstr->buf, vstr->len)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_EIO); } + } else { + mp_raise_OSError(MP_EIO); } } @@ -286,33 +286,34 @@ STATIC void pyb_i2c_readmem_into (mp_arg_val_t *args, vstr_t *vstr) { STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { pyb_i2c_obj_t *self = self_in; if (self->baudrate > 0) { - mp_printf(print, "I2C(0, I2C.MASTER, baudrate=%u)", self->baudrate); + mp_printf(print, "I2C(0, baudrate=%u)", self->baudrate); } else { mp_print_str(print, "I2C(0)"); } } -STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, const mp_arg_val_t *args) { - // verify that mode is master - if (args[0].u_int != PYBI2C_MASTER) { - goto invalid_args; - } +STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_scl, ARG_sda, ARG_freq }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); // make sure the baudrate is between the valid range - self->baudrate = MIN(MAX(args[1].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ); + self->baudrate = MIN(MAX(args[ARG_freq].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ); // assign the pins - mp_obj_t pins_o = args[2].u_obj; - if (pins_o != mp_const_none) { - mp_obj_t *pins; - if (pins_o == MP_OBJ_NULL) { - // use the default pins - pins = (mp_obj_t *)pyb_i2c_def_pin; - } else { - mp_obj_get_array_fixed_n(pins_o, 2, &pins); - } - pin_assign_pins_af (pins, 2, PIN_TYPE_STD_PU, PIN_FN_I2C, 0); + mp_obj_t pins[2] = {&pin_GP13, &pin_GP23}; // default (SDA, SCL) pins + if (args[ARG_scl].u_obj != MP_OBJ_NULL) { + pins[1] = args[ARG_scl].u_obj; + } + if (args[ARG_sda].u_obj != MP_OBJ_NULL) { + pins[0] = args[ARG_sda].u_obj; } + pin_assign_pins_af(pins, 2, PIN_TYPE_STD_PU, PIN_FN_I2C, 0); // init the I2C bus i2c_init(self); @@ -321,44 +322,34 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, const mp_arg_val_t *arg pyb_sleep_add ((const mp_obj_t)self, (WakeUpCB_t)i2c_init); return mp_const_none; - -invalid_args: - mp_raise_ValueError(mpexception_value_invalid_arguments); } -STATIC const mp_arg_t pyb_i2c_init_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} }, - { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, - { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, -}; STATIC mp_obj_t pyb_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // check the id argument, if given + if (n_args > 0) { + if (all_args[0] != MP_OBJ_NEW_SMALL_INT(0)) { + mp_raise_OSError(MP_ENODEV); + } + --n_args; + ++all_args; + } + // parse args mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args); - mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args)]; - mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_i2c_init_args, args); - - // check the peripheral id - if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); - } // setup the object pyb_i2c_obj_t *self = &pyb_i2c_obj; self->base.type = &pyb_i2c_type; // start the peripheral - pyb_i2c_init_helper(self, &args[1]); + pyb_i2c_init_helper(self, n_args, all_args, &kw_args); return (mp_obj_t)self; } -STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - // parse args - mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args) - 1]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_i2c_init_args[1], args); - return pyb_i2c_init_helper(pos_args[0], args); +STATIC mp_obj_t pyb_i2c_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + return pyb_i2c_init_helper(pos_args[0], n_args - 1, pos_args + 1, kw_args); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init); @@ -445,7 +436,7 @@ STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m // send the data if (!pyb_i2c_write(args[0].u_int, bufinfo.buf, bufinfo.len, args[2].u_bool)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_EIO); } // return the number of bytes written @@ -486,7 +477,7 @@ STATIC mp_obj_t pyb_i2c_readfrom_mem_into(mp_uint_t n_args, const mp_obj_t *pos_ // get the buffer to read into vstr_t vstr; pyb_i2c_readmem_into (args, &vstr); - return mp_obj_new_int(vstr.len); + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_into_obj, 1, pyb_i2c_readfrom_mem_into); @@ -510,11 +501,10 @@ STATIC mp_obj_t pyb_i2c_writeto_mem(mp_uint_t n_args, const mp_obj_t *pos_args, // write the register address to write to. if (pyb_i2c_mem_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len)) { - // return the number of bytes written - return mp_obj_new_int(bufinfo.len); + return mp_const_none; } - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_EIO); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_mem_obj, 1, pyb_i2c_writeto_mem); @@ -529,9 +519,6 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem), (mp_obj_t)&pyb_i2c_readfrom_mem_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem_into), (mp_obj_t)&pyb_i2c_readfrom_mem_into_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_writeto_mem), (mp_obj_t)&pyb_i2c_writeto_mem_obj }, - - // class constants - { MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(PYBI2C_MASTER) }, }; STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table); diff --git a/cc3200/mods/pybpin.c b/cc3200/mods/pybpin.c index ac0ecef25..c2a469117 100644 --- a/cc3200/mods/pybpin.c +++ b/cc3200/mods/pybpin.c @@ -684,13 +684,6 @@ STATIC mp_obj_t pin_value(mp_uint_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value); -STATIC mp_obj_t pin_toggle(mp_obj_t self_in) { - pin_obj_t *self = self_in; - MAP_GPIOPinWrite(self->port, self->bit, ~MAP_GPIOPinRead(self->port, self->bit)); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_toggle_obj, pin_toggle); - STATIC mp_obj_t pin_id(mp_obj_t self_in) { pin_obj_t *self = self_in; return MP_OBJ_NEW_QSTR(self->name); @@ -913,7 +906,6 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = { // instance methods { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pin_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&pin_value_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&pin_toggle_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&pin_id_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_mode), (mp_obj_t)&pin_mode_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_pull), (mp_obj_t)&pin_pull_obj }, diff --git a/cc3200/mods/pybrtc.c b/cc3200/mods/pybrtc.c index db00f4939..134bd440e 100644 --- a/cc3200/mods/pybrtc.c +++ b/cc3200/mods/pybrtc.c @@ -25,11 +25,11 @@ * THE SOFTWARE. */ -#include <std.h> - #include "py/mpconfig.h" #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" @@ -38,7 +38,6 @@ #include "pybrtc.h" #include "mpirq.h" #include "pybsleep.h" -#include "timeutils.h" #include "simplelink.h" #include "modnetwork.h" #include "modwlan.h" @@ -197,7 +196,7 @@ STATIC uint pyb_rtc_datetime_s_us(const mp_obj_t datetime, uint32_t *seconds) { // set date and time mp_obj_t *items; - uint len; + size_t len; mp_obj_get_array(datetime, &len, &items); // verify the tuple @@ -294,7 +293,7 @@ STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_ // check the peripheral id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // setup the object @@ -362,7 +361,7 @@ STATIC mp_obj_t pyb_rtc_alarm (mp_uint_t n_args, const mp_obj_t *pos_args, mp_ma // check the alarm id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } uint32_t f_seconds; @@ -397,7 +396,7 @@ STATIC mp_obj_t pyb_rtc_alarm_left (mp_uint_t n_args, const mp_obj_t *args) { // only alarm id 0 is available if (n_args > 1 && mp_obj_get_int(args[1]) != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // get the current time @@ -415,7 +414,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_alarm_left_obj, 1, 2, pyb_rtc STATIC mp_obj_t pyb_rtc_alarm_cancel (mp_uint_t n_args, const mp_obj_t *args) { // only alarm id 0 is available if (n_args > 1 && mp_obj_get_int(args[1]) != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // disable the alarm pyb_rtc_disable_alarm(); diff --git a/cc3200/mods/pybsd.c b/cc3200/mods/pybsd.c index b8d5b4cc1..306baea8b 100644 --- a/cc3200/mods/pybsd.c +++ b/cc3200/mods/pybsd.c @@ -27,6 +27,10 @@ #include "py/mpconfig.h" #include "py/obj.h" #include "py/runtime.h" +#include "py/mperrno.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#include "extmod/vfs_fat.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" @@ -36,8 +40,6 @@ #include "prcm.h" #include "gpio.h" #include "sdhost.h" -#include "ff.h" -#include "diskio.h" #include "sd_diskio.h" #include "pybsd.h" #include "mpexception.h" @@ -107,7 +109,7 @@ STATIC mp_obj_t pyb_sd_init_helper (pybsd_obj_t *self, const mp_arg_val_t *args) pyb_sd_hw_init (self); if (sd_disk_init() != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_EIO); } // register it with the sleep module @@ -132,7 +134,7 @@ STATIC mp_obj_t pyb_sd_make_new(const mp_obj_type_t *type, size_t n_args, size_t // check the peripheral id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // setup and initialize the object @@ -163,9 +165,50 @@ STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_sd_deinit_obj, pyb_sd_deinit); +STATIC mp_obj_t pyb_sd_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); + DRESULT res = sd_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_readblocks_obj, pyb_sd_readblocks); + +STATIC mp_obj_t pyb_sd_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + DRESULT res = sd_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_writeblocks_obj, pyb_sd_writeblocks); + +STATIC mp_obj_t pyb_sd_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) { + mp_int_t cmd = mp_obj_get_int(cmd_in); + switch (cmd) { + case BP_IOCTL_INIT: + case BP_IOCTL_DEINIT: + case BP_IOCTL_SYNC: + // nothing to do + return MP_OBJ_NEW_SMALL_INT(0); // success + + case BP_IOCTL_SEC_COUNT: + return MP_OBJ_NEW_SMALL_INT(sd_disk_info.ulNofBlock * (sd_disk_info.ulBlockSize / 512)); + + case BP_IOCTL_SEC_SIZE: + return MP_OBJ_NEW_SMALL_INT(SD_SECTOR_SIZE); + + default: // unknown command + return MP_OBJ_NEW_SMALL_INT(-1); // error + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_ioctl_obj, pyb_sd_ioctl); + STATIC const mp_map_elem_t pyb_sd_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_sd_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_sd_deinit_obj }, + // block device protocol + { MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_sd_readblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_sd_writeblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_sd_ioctl_obj }, }; STATIC MP_DEFINE_CONST_DICT(pyb_sd_locals_dict, pyb_sd_locals_dict_table); diff --git a/cc3200/mods/pybspi.c b/cc3200/mods/pybspi.c index 6fc7ee62d..3cd384266 100644 --- a/cc3200/mods/pybspi.c +++ b/cc3200/mods/pybspi.c @@ -30,6 +30,7 @@ #include "py/mpstate.h" #include "py/runtime.h" +#include "py/mperrno.h" #include "bufhelper.h" #include "inc/hw_types.h" #include "inc/hw_mcspi.h" @@ -131,7 +132,7 @@ STATIC void pybspi_rx (pyb_spi_obj_t *self, void *data) { STATIC void pybspi_transfer (pyb_spi_obj_t *self, const char *txdata, char *rxdata, uint32_t len, uint32_t *txchar) { if (!self->baudrate) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } // send and receive the data MAP_SPICSEnable(GSPI_BASE); @@ -234,7 +235,7 @@ STATIC mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_ // check the peripheral id if (args[0].u_int != 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // setup the object diff --git a/cc3200/mods/pybtimer.c b/cc3200/mods/pybtimer.c index 0eeb6eefd..d25ac6c2b 100644 --- a/cc3200/mods/pybtimer.c +++ b/cc3200/mods/pybtimer.c @@ -34,6 +34,7 @@ #include "py/nlr.h" #include "py/runtime.h" #include "py/gc.h" +#include "py/mperrno.h" #include "py/mphal.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" @@ -329,7 +330,7 @@ STATIC mp_obj_t pyb_timer_make_new(const mp_obj_type_t *type, size_t n_args, siz // create a new Timer object int32_t timer_idx = mp_obj_get_int(args[0]); if (timer_idx < 0 || timer_idx > (PYBTIMER_NUM_TIMERS - 1)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } pyb_timer_obj_t *tim = &pyb_timer_obj[timer_idx]; @@ -370,7 +371,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp // verify that the timer has been already initialized if (!tim->config) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } if (channel_n != TIMER_A && channel_n != TIMER_B && channel_n != (TIMER_A | TIMER_B)) { // invalid channel diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index fe710be92..92bb3e46e 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -279,7 +279,7 @@ STATIC void UARTGenericIntHandler(uint32_t uart_id) { STATIC void uart_check_init(pyb_uart_obj_t *self) { // not initialized if (!self->baudrate) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } } @@ -375,10 +375,13 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, const mp_arg_val_t *a config |= UART_CONFIG_PAR_NONE; } else { uint parity = mp_obj_get_int(args[2].u_obj); - if (parity != UART_CONFIG_PAR_ODD && parity != UART_CONFIG_PAR_EVEN) { + if (parity == 0) { + config |= UART_CONFIG_PAR_EVEN; + } else if (parity == 1) { + config |= UART_CONFIG_PAR_ODD; + } else { goto error; } - config |= parity; } // stop bits config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO); @@ -388,7 +391,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, const mp_arg_val_t *a uint flowcontrol = UART_FLOWCONTROL_NONE; if (pins_o != mp_const_none) { mp_obj_t *pins; - mp_uint_t n_pins = 2; + size_t n_pins = 2; if (pins_o == MP_OBJ_NULL) { // use the default pins pins = (mp_obj_t *)pyb_uart_def_pin[self->uart_id]; @@ -454,7 +457,7 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size if (args[0].u_obj == MP_OBJ_NULL) { if (args[5].u_obj != MP_OBJ_NULL) { mp_obj_t *pins; - mp_uint_t n_pins = 2; + size_t n_pins = 2; mp_obj_get_array(args[5].u_obj, &n_pins, &pins); // check the Tx pin (or the Rx if Tx is None) if (pins[0] == mp_const_none) { @@ -471,7 +474,7 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size } if (uart_id > PYB_UART_1) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } // get the correct uart instance @@ -577,8 +580,6 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, // class constants - { MP_OBJ_NEW_QSTR(MP_QSTR_EVEN), MP_OBJ_NEW_SMALL_INT(UART_CONFIG_PAR_EVEN) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ODD), MP_OBJ_NEW_SMALL_INT(UART_CONFIG_PAR_ODD) }, { MP_OBJ_NEW_QSTR(MP_QSTR_RX_ANY), MP_OBJ_NEW_SMALL_INT(UART_TRIGGER_RX_ANY) }, }; @@ -596,8 +597,8 @@ STATIC mp_uint_t pyb_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, i // wait for first char to become available if (!uart_rx_wait(self)) { - // return EAGAIN error to indicate non-blocking (then read() method returns None) - *errcode = EAGAIN; + // return MP_EAGAIN error to indicate non-blocking (then read() method returns None) + *errcode = MP_EAGAIN; return MP_STREAM_ERROR; } @@ -619,7 +620,7 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t // write the data if (!uart_tx_strn(self, buf, size)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); + mp_raise_OSError(MP_EIO); } return size; } @@ -639,7 +640,7 @@ STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t a ret |= MP_STREAM_POLL_WR; } } else { - *errcode = EINVAL; + *errcode = MP_EINVAL; ret = MP_STREAM_ERROR; } return ret; @@ -664,7 +665,7 @@ const mp_obj_type_t pyb_uart_type = { .name = MP_QSTR_UART, .print = pyb_uart_print, .make_new = pyb_uart_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &uart_stream_p, .locals_dict = (mp_obj_t)&pyb_uart_locals_dict, diff --git a/cc3200/mods/pybwdt.c b/cc3200/mods/pybwdt.c index b4c847268..76c701ca0 100644 --- a/cc3200/mods/pybwdt.c +++ b/cc3200/mods/pybwdt.c @@ -29,6 +29,7 @@ #include "py/mpconfig.h" #include "py/obj.h" #include "py/runtime.h" +#include "py/mperrno.h" #include "py/mphal.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" @@ -100,14 +101,14 @@ STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_ mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_wdt_init_args, args); if (args[0].u_obj != mp_const_none && mp_obj_get_int(args[0].u_obj) > 0) { - mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable); + mp_raise_OSError(MP_ENODEV); } uint timeout_ms = args[1].u_int; if (timeout_ms < PYBWDT_MIN_TIMEOUT_MS) { mp_raise_ValueError(mpexception_value_invalid_arguments); } if (pyb_wdt_obj.running) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); + mp_raise_OSError(MP_EPERM); } // Enable the WDT peripheral clock |
