|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Jeff Epler for Adafruit Industries |
| 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 OR 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 "py/obj.h" |
| 28 | +#include "py/objproperty.h" |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "py/objarray.h" |
| 31 | + |
| 32 | +#include "shared-bindings/sdcardio/SDCard.h" |
| 33 | +#include "shared-module/sdcardio/SDCard.h" |
| 34 | +#include "common-hal/busio/SPI.h" |
| 35 | +#include "shared-bindings/busio/SPI.h" |
| 36 | +#include "shared-bindings/microcontroller/Pin.h" |
| 37 | +#include "supervisor/flash.h" |
| 38 | + |
| 39 | +//| class SDCard: |
| 40 | +//| """SD Card Block Interface |
| 41 | +//| |
| 42 | +//| Controls an SD card over SPI. This built-in module has higher read |
| 43 | +//| performance than the library adafruit_sdcard, but it is only compatible with |
| 44 | +//| `busio.SPI`, not `bitbangio.SPI`. Usually an SDCard object is used |
| 45 | +//| with ``storage.VfsFat`` to allow file I/O to an SD card.""" |
| 46 | +//| |
| 47 | +//| def __init__(bus:busio.SPI, cs=digitalio.DigitalInOut, baudrate=8000000): |
| 48 | +//| """Construct an SPI SD Card object with the given properties |
| 49 | +//| |
| 50 | +//| :param busio.SPI spi: The SPI bus |
| 51 | +//| :param microcontroller.Pin cs: The chip select connected to the card |
| 52 | +//| :param int baudrate: The SPI data rate to use after card setup |
| 53 | +//| :param busio.SDIO sdio: The SDIO bus. Mutually exclusive with spi and cs. |
| 54 | +//| |
| 55 | +//| Note that during detection and configuration, a hard-coded low baudrate is used. |
| 56 | +//| Data transfers use the specified baurate (rounded down to one that is supported by |
| 57 | +//| the microcontroller) |
| 58 | +//| |
| 59 | +//| Example usage: |
| 60 | +//| |
| 61 | +//| .. code-block:: python |
| 62 | +//| |
| 63 | +//| import os |
| 64 | +//| |
| 65 | +//| import board |
| 66 | +//| import sdcardio |
| 67 | +//| import storage |
| 68 | +//| |
| 69 | +//| sd = sdcardio.SDCard(board.SPI(), board.SD_CS) |
| 70 | +//| vfs = storage.VfsFat(sd) |
| 71 | +//| storage.mount(vfs, '/sd') |
| 72 | +//| os.listdir('/sd')""" |
| 73 | + |
| 74 | +STATIC mp_obj_t sdcardio_sdcard_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 75 | + enum { ARG_spi, ARG_cs, ARG_baudrate, ARG_sdio, NUM_ARGS }; |
| 76 | + static const mp_arg_t allowed_args[] = { |
| 77 | + { MP_QSTR_spi, MP_ARG_OBJ, {.u_obj = mp_const_none } }, |
| 78 | + { MP_QSTR_cs, MP_ARG_OBJ, {.u_obj = mp_const_none } }, |
| 79 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 8000000} }, |
| 80 | + { MP_QSTR_sdio, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_int = 8000000} }, |
| 81 | + }; |
| 82 | + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); |
| 83 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 84 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 85 | + |
| 86 | + busio_spi_obj_t *spi = validate_obj_is_spi_bus(args[ARG_spi].u_obj); |
| 87 | + mcu_pin_obj_t *cs = validate_obj_is_free_pin(args[ARG_cs].u_obj); |
| 88 | + |
| 89 | + sdcardio_sdcard_obj_t *self = m_new_obj(sdcardio_sdcard_obj_t); |
| 90 | + self->base.type = &sdcardio_SDCard_type; |
| 91 | + |
| 92 | + common_hal_sdcardio_sdcard_construct(self, spi, cs, args[ARG_baudrate].u_int); |
| 93 | + |
| 94 | + return self; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +//| def count() -> int: |
| 99 | +//| """Returns the total number of sectors |
| 100 | +//| |
| 101 | +//| Due to technical limitations, this is a function and not a property. |
| 102 | +//| |
| 103 | +//| :return: The number of 512-byte blocks, as a number""" |
| 104 | +//| |
| 105 | +mp_obj_t sdcardio_sdcard_count(mp_obj_t self_in) { |
| 106 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 107 | + return mp_obj_new_int_from_ull(common_hal_sdcardio_sdcard_get_blockcount(self)); |
| 108 | +} |
| 109 | +MP_DEFINE_CONST_FUN_OBJ_1(sdcardio_sdcard_count_obj, sdcardio_sdcard_count); |
| 110 | + |
| 111 | +//| def deinit() -> None: |
| 112 | +//| """Disable permanently. |
| 113 | +//| |
| 114 | +//| :return: None""" |
| 115 | +//| |
| 116 | +mp_obj_t sdcardio_sdcard_deinit(mp_obj_t self_in) { |
| 117 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 118 | + common_hal_sdcardio_sdcard_deinit(self); |
| 119 | + return mp_const_none; |
| 120 | +} |
| 121 | +MP_DEFINE_CONST_FUN_OBJ_1(sdcardio_sdcard_deinit_obj, sdcardio_sdcard_deinit); |
| 122 | + |
| 123 | + |
| 124 | +//| def readblocks(start_block: int, buf: bytearray) -> None: |
| 125 | +//| |
| 126 | +//| """Read one or more blocks from the card |
| 127 | +//| |
| 128 | +//| :param int start_block: The block to start reading from |
| 129 | +//| :param bytearray buf: The buffer to write into. Length must be multiple of 512. |
| 130 | +//| |
| 131 | +//| :return: None""" |
| 132 | +//| |
| 133 | + |
| 134 | +mp_obj_t sdcardio_sdcard_readblocks(mp_obj_t self_in, mp_obj_t start_block_in, mp_obj_t buf_in) { |
| 135 | + uint32_t start_block = mp_obj_get_int(start_block_in); |
| 136 | + mp_buffer_info_t bufinfo; |
| 137 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); |
| 138 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 139 | + int result = common_hal_sdcardio_sdcard_readblocks(self, start_block, &bufinfo); |
| 140 | + if (result < 0) { |
| 141 | + mp_raise_OSError(-result); |
| 142 | + } |
| 143 | + return mp_const_none; |
| 144 | +} |
| 145 | + |
| 146 | +MP_DEFINE_CONST_FUN_OBJ_3(sdcardio_sdcard_readblocks_obj, sdcardio_sdcard_readblocks); |
| 147 | + |
| 148 | +//| def writeblocks(start_block: int, buf: bytearray) -> None: |
| 149 | +//| |
| 150 | +//| """Write one or more blocks to the card |
| 151 | +//| |
| 152 | +//| :param int start_block: The block to start writing from |
| 153 | +//| :param bytearray buf: The buffer to read from. Length must be multiple of 512. |
| 154 | +//| |
| 155 | +//| :return: None""" |
| 156 | +//| |
| 157 | + |
| 158 | +mp_obj_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, mp_obj_t start_block_in, mp_obj_t buf_in) { |
| 159 | + uint32_t start_block = mp_obj_get_int(start_block_in); |
| 160 | + mp_buffer_info_t bufinfo; |
| 161 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); |
| 162 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 163 | + int result = common_hal_sdcardio_sdcard_writeblocks(self, start_block, &bufinfo); |
| 164 | + if (result < 0) { |
| 165 | + mp_raise_OSError(-result); |
| 166 | + } |
| 167 | + return mp_const_none; |
| 168 | +} |
| 169 | +MP_DEFINE_CONST_FUN_OBJ_3(sdcardio_sdcard_writeblocks_obj, sdcardio_sdcard_writeblocks); |
| 170 | + |
| 171 | +STATIC const mp_rom_map_elem_t sdcardio_sdcard_locals_dict_table[] = { |
| 172 | + { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&sdcardio_sdcard_count_obj) }, |
| 173 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&sdcardio_sdcard_deinit_obj) }, |
| 174 | + { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&sdcardio_sdcard_readblocks_obj) }, |
| 175 | + { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&sdcardio_sdcard_writeblocks_obj) }, |
| 176 | +}; |
| 177 | +STATIC MP_DEFINE_CONST_DICT(sdcardio_sdcard_locals_dict, sdcardio_sdcard_locals_dict_table); |
| 178 | + |
| 179 | +const mp_obj_type_t sdcardio_SDCard_type = { |
| 180 | + { &mp_type_type }, |
| 181 | + .name = MP_QSTR_SDCard, |
| 182 | + .make_new = sdcardio_sdcard_make_new, |
| 183 | + .locals_dict = (mp_obj_dict_t*)&sdcardio_sdcard_locals_dict, |
| 184 | +}; |
0 commit comments