|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 NXP |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT O
3262
R OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "modzephyr.h" |
| 28 | +#include "py/runtime.h" |
| 29 | + |
| 30 | +#if MICROPY_VFS |
| 31 | +
D966
#include "extmod/vfs.h" |
| 32 | +#endif |
| 33 | + |
| 34 | +#ifdef CONFIG_DISK_ACCESS |
| 35 | +#include <disk/disk_access.h> |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifdef CONFIG_DISK_ACCESS |
| 39 | +typedef struct _zephyr_disk_access_obj_t { |
| 40 | + mp_obj_base_t base; |
| 41 | + const char *pdrv; |
| 42 | + int block_size; |
| 43 | + int block_count; |
| 44 | +} zephyr_disk_access_obj_t; |
| 45 | + |
| 46 | +STATIC void zephyr_disk_access_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 47 | + zephyr_disk_access_obj_t *self = self_in; |
| 48 | + mp_printf(print, "DiskAccess(%s)", self->pdrv); |
| 49 | +} |
| 50 | + |
| 51 | +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) { |
| 52 | + mp_arg_check_num(n_args, n_kw, 1, 1, false); |
| 53 | + zephyr_disk_access_obj_t *self = m_new_obj(zephyr_disk_access_obj_t); |
| 54 | + self->base.type = type; |
| 55 | + self->pdrv = mp_obj_str_get_str(args[0]); |
| 56 | + |
| 57 | + if (disk_access_init(self->pdrv) != 0) { |
| 58 | + mp_raise_ValueError("disk not found"); |
| 59 | + } |
| 60 | + |
| 61 | + if (disk_access_ioctl(self->pdrv, DISK_IOCTL_GET_SECTOR_SIZE, &self->block_size)) { |
| 62 | + mp_raise_ValueError("unable to get sector size"); |
| 63 | + } |
| 64 | + |
| 65 | + if (disk_access_ioctl(self->pdrv, DISK_IOCTL_GET_SECTOR_COUNT, &self->block_count)) { |
| 66 | + mp_raise_ValueError("unable to get block count"); |
| 67 | + } |
| 68 | + |
| 69 | + return MP_OBJ_FROM_PTR(self); |
| 70 | +} |
| 71 | + |
| 72 | +STATIC mp_obj_t zephyr_disk_access_readblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) { |
| 73 | + zephyr_disk_access_obj_t *self = self_in; |
| 74 | + mp_buffer_info_t bufinfo; |
| 75 | + int ret; |
| 76 | + |
| 77 | + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); |
| 78 | + ret = disk_access_read(self->pdrv, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / self->block_size); |
| 79 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 80 | +} |
| 81 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_readblocks_obj, zephyr_disk_access_readblocks); |
| 82 | + |
| 83 | +STATIC mp_obj_t zephyr_disk_access_writeblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) { |
| 84 | + zephyr_disk_access_obj_t *self = self_in; |
| 85 | + mp_buffer_info_t bufinfo; |
| 86 | + int ret; |
| 87 | + |
| 88 | + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); |
| 89 | + ret = disk_access_write(self->pdrv, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / self->block_size); |
| 90 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 91 | +} |
| 92 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_writeblocks_obj, zephyr_disk_access_writeblocks); |
| 93 | + |
| 94 | +STATIC mp_obj_t zephyr_disk_access_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) { |
| 95 | + zephyr_disk_access_obj_t *self = self_in; |
| 96 | + mp_int_t cmd = mp_obj_get_int(cmd_in); |
| 97 | + int buf; |
| 98 | + int ret; |
| 99 | + |
| 100 | + switch (cmd) { |
| 101 | + case MP_BLOCKDEV_IOCTL_INIT: |
| 102 | + case MP_BLOCKDEV_IOCTL_DEINIT: |
| 103 | + return MP_OBJ_NEW_SMALL_INT(0); |
| 104 | + |
| 105 | + case MP_BLOCKDEV_IOCTL_SYNC: |
| 106 | + ret = disk_access_ioctl(self->pdrv, DISK_IOCTL_CTRL_SYNC, &buf); |
| 107 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 108 | + |
| 109 | + case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: |
| 110 | + return MP_OBJ_NEW_SMALL_INT(self->block_count); |
| 111 | + |
| 112 | + case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: |
| 113 | + return MP_OBJ_NEW_SMALL_INT(self->block_size); |
| 114 | + |
| 115 | + default: |
| 116 | + return MP_OBJ_NEW_SMALL_INT(-1); |
| 117 | + } |
| 118 | +} |
| 119 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_ioctl_obj, zephyr_disk_access_ioctl); |
| 120 | + |
| 121 | +STATIC const mp_rom_map_elem_t zephyr_disk_access_locals_dict_table[] = { |
| 122 | + { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&zephyr_disk_access_readblocks_obj) }, |
| 123 | + { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&zephyr_disk_access_writeblocks_obj) }, |
| 124 | + { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&zephyr_disk_access_ioctl_obj) }, |
| 125 | +}; |
| 126 | +STATIC MP_DEFINE_CONST_DICT(zephyr_disk_access_locals_dict, zephyr_disk_access_locals_dict_table); |
| 127 | + |
| 128 | +const mp_obj_type_t zephyr_disk_access_type = { |
| 129 | + { &mp_type_type }, |
| 130 | + .name = MP_QSTR_DiskAccess, |
| 131 | + .print = zephyr_disk_access_print, |
| 132 | + .make_new = zephyr_disk_access_make_new, |
| 133 | + .locals_dict = (mp_obj_dict_t*)&zephyr_disk_access_locals_dict, |
| 134 | +}; |
| 135 | +#endif // CONFIG_DISK_ACCESS |
0 commit comments