diff --git a/ports/zephyr/Makefile b/ports/zephyr/Makefile index 53882f3d26..b1c1866788 100644 --- a/ports/zephyr/Makefile +++ b/ports/zephyr/Makefile @@ -46,6 +46,7 @@ SRC_C = main.c \ machine_i2c.c \ machine_pin.c \ uart_core.c \ + zephyr_storage.c \ lib/utils/stdout_helpers.c \ lib/utils/printf.c \ lib/utils/pyexec.c \ diff --git a/ports/zephyr/modzephyr.c b/ports/zephyr/modzephyr.c index 781eb22035..e24bdd0b9f 100644 --- a/ports/zephyr/modzephyr.c +++ b/ports/zephyr/modzephyr.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2017 Linaro Limited + * Copyright (c) 2019 NXP * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,6 +32,7 @@ #include #include +#include "modzephyr.h" #include "py/runtime.h" STATIC mp_obj_t mod_is_preempt_thread(void) { @@ -90,6 +92,9 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { #ifdef CONFIG_NET_SHELL { MP_ROM_QSTR(MP_QSTR_shell_net_iface), MP_ROM_PTR(&mod_shell_net_iface_obj) }, #endif + #ifdef CONFIG_DISK_ACCESS + { MP_ROM_QSTR(MP_QSTR_DiskAccess), MP_ROM_PTR(&zephyr_disk_access_type) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table); diff --git a/ports/zephyr/modzephyr.h b/ports/zephyr/modzephyr.h new file mode 100644 index 0000000000..28cb3dfe7c --- /dev/null +++ b/ports/zephyr/modzephyr.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 NXP + * + * 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_ZEPHYR_MODZEPHYR_H +#define MICROPY_INCLUDED_ZEPHYR_MODZEPHYR_H + +#include "py/obj.h" + +#ifdef CONFIG_DISK_ACCESS +extern const mp_obj_type_t zephyr_disk_access_type; +#endif + +#endif // MICROPY_INCLUDED_ZEPHYR_MODZEPHYR_H diff --git a/ports/zephyr/prj_mimxrt1050_evk.conf b/ports/zephyr/prj_mimxrt1050_evk.conf new file mode 100644 index 0000000000..23066977f3 --- /dev/null +++ b/ports/zephyr/prj_mimxrt1050_evk.conf @@ -0,0 +1,3 @@ +# Required for zephyr.DiskAccess block devices +CONFIG_DISK_ACCESS=y +CONFIG_DISK_ACCESS_SDHC=y diff --git a/ports/zephyr/zephyr_storage.c b/ports/zephyr/zephyr_storage.c new file mode 100644 index 0000000000..cd672dc0e9 --- /dev/null +++ b/ports/zephyr/zephyr_storage.c @@ -0,0 +1,135 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 NXP + * + * 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 "modzephyr.h" +#include "py/runtime.h" + +#if MICROPY_VFS +#include "extmod/vfs.h" +#endif + +#ifdef CONFIG_DISK_ACCESS +#include +#endif + +#ifdef CONFIG_DISK_ACCESS +typedef struct _zephyr_disk_access_obj_t { + mp_obj_base_t base; + const char *pdrv; + int block_size; + int block_count; +} zephyr_disk_access_obj_t; + +STATIC void zephyr_disk_access_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + zephyr_disk_access_obj_t *self = self_in; + mp_printf(print, "DiskAccess(%s)", self->pdrv); +} + +STATIC mp_obj_t zephyr_disk_access_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, 1, false); + zephyr_disk_access_obj_t *self = m_new_obj(zephyr_disk_access_obj_t); + self->base.type = type; + self->pdrv = mp_obj_str_get_str(args[0]); + + if (disk_access_init(self->pdrv) != 0) { + mp_raise_ValueError("disk not found"); + } + + if (disk_access_ioctl(self->pdrv, DISK_IOCTL_GET_SECTOR_SIZE, &self->block_size)) { + mp_raise_ValueError("unable to get sector size"); + } + + if (disk_access_ioctl(self->pdrv, DISK_IOCTL_GET_SECTOR_COUNT, &self->block_count)) { + mp_raise_ValueError("unable to get block count"); + } + + return MP_OBJ_FROM_PTR(self); +} + +STATIC mp_obj_t zephyr_disk_access_readblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) { + zephyr_disk_access_obj_t *self = self_in; + mp_buffer_info_t bufinfo; + int ret; + + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); + ret = disk_access_read(self->pdrv, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / self->block_size); + return MP_OBJ_NEW_SMALL_INT(ret); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_readblocks_obj, zephyr_disk_access_readblocks); + +STATIC mp_obj_t zephyr_disk_access_writeblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) { + zephyr_disk_access_obj_t *self = self_in; + mp_buffer_info_t bufinfo; + int ret; + + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + ret = disk_access_write(self->pdrv, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / self->block_size); + return MP_OBJ_NEW_SMALL_INT(ret); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_writeblocks_obj, zephyr_disk_access_writeblocks); + +STATIC mp_obj_t zephyr_disk_access_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) { + zephyr_disk_access_obj_t *self = self_in; + mp_int_t cmd = mp_obj_get_int(cmd_in); + int buf; + int ret; + + switch (cmd) { + case MP_BLOCKDEV_IOCTL_INIT: + case MP_BLOCKDEV_IOCTL_DEINIT: + return MP_OBJ_NEW_SMALL_INT(0); + + case MP_BLOCKDEV_IOCTL_SYNC: + ret = disk_access_ioctl(self->pdrv, DISK_IOCTL_CTRL_SYNC, &buf); + return MP_OBJ_NEW_SMALL_INT(ret); + + case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: + return MP_OBJ_NEW_SMALL_INT(self->block_count); + + case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: + return MP_OBJ_NEW_SMALL_INT(self->block_size); + + default: + return MP_OBJ_NEW_SMALL_INT(-1); + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_ioctl_obj, zephyr_disk_access_ioctl); + +STATIC const mp_rom_map_elem_t zephyr_disk_access_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&zephyr_disk_access_readblocks_obj) }, + { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&zephyr_disk_access_writeblocks_obj) }, + { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&zephyr_disk_access_ioctl_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(zephyr_disk_access_locals_dict, zephyr_disk_access_locals_dict_table); + +const mp_obj_type_t zephyr_disk_access_type = { + { &mp_type_type }, + .name = MP_QSTR_DiskAccess, + .print = zephyr_disk_access_print, + .make_new = zephyr_disk_access_make_new, + .locals_dict = (mp_obj_dict_t*)&zephyr_disk_access_locals_dict, +}; +#endif // CONFIG_DISK_ACCESS