|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Scott Shawcroft |
| 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 | +// This file contains all of the Python API definitions for the |
| 28 | +// sdioio.SDCard class. |
| 29 | + |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include "shared-bindings/microcontroller/Pin.h" |
| 33 | +#include "shared-bindings/sdioio/SDCard.h" |
| 34 | +#include "shared-bindings/util.h" |
| 35 | + |
| 36 | +#include "lib/utils/buffer_helper.h" |
| 37 | +#include "lib/utils/context_manager_helpers.h" |
| 38 | +#include "py/mperrno.h" |
| 39 | +#include "py/objproperty.h" |
| 40 | +#include "py/runtime.h" |
| 41 | +#include "supervisor/shared/translate.h" |
| 42 | + |
| 43 | +//| class SDCard: |
| 44 | +//| """SD Card Block Interface with SDIO |
| 45 | +//| |
| 46 | +//| Controls an SD card over SDIO. SDIO is a parallel protocol designed |
| 47 | +//| for SD cards. It uses a clock pin, a command pin, and 1 or 4 |
| 48 | +//| data pins. It can be operated at a high frequency such as |
| 49 | +//| 25MHz. Usually an SDCard object is used with ``storage.VfsFat`` |
| 50 | +//| to allow file I/O to an SD card.""" |
| 51 | +//| |
| 52 | +//| def __init__(*, clock: digitalio.DigitalInOut, command: digitalio.DigitalInOut, data: List[digitalio.DigitalInOut], frequency: int): |
| 53 | +//| """Construct an SDIO SD Card object with the given properties |
| 54 | +//| |
| 55 | +//| :param ~microcontroller.Pin clock: the pin to use for the clock. |
| 56 | +//| :param ~microcontroller.Pin command: the pin to use for the command. |
| 57 | +//| :param data: A sequence of pins to use for data. |
| 58 | +//| :param frequency: The frequency of the bus in Hz |
| 59 | +//| |
| 60 | +//| Example usage: |
| 61 | +//| |
| 62 | +//| .. code-block:: python |
| 63 | +//| |
| 64 | +//| import os |
| 65 | +//| |
| 66 | +//| import board |
| 67 | +//| import sdioio |
| 68 | +//| import storage |
| 69 | +//| |
| 70 | +//| sd = sdioio.SDCard( |
| 71 | +//| clock=board.SDIO_CLOCK, |
| 72 | +//| command=board.SDIO_COMMAND, |
| 73 | +//| data=board.SDIO_DATA, |
| 74 | +//| frequency=25000000) |
| 75 | +//| vfs = storage.VfsFat(sd) |
| 76 | +//| storage.mount(vfs, '/sd') |
| 77 | +//| os.listdir('/sd')""" |
| 78 | +//| ... |
| 79 | +//| |
| 80 | + |
| 81 | +STATIC mp_obj_t sdioio_sdcard_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 82 | + sdioio_sdcard_obj_t *self = m_new_obj(sdioio_sdcard_obj_t); |
| 83 | + self->base.type = &sdioio_SDCard_type; |
| 84 | + enum { ARG_clock, ARG_command, ARG_data, ARG_frequency, NUM_ARGS }; |
| 85 | + static const mp_arg_t allowed_args[] = { |
| 86 | + { MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ }, |
| 87 | + { MP_QSTR_command, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ }, |
| 88 | + { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ }, |
| 89 | + { MP_QSTR_frequency, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_INT }, |
| 90 | + }; |
| 91 | + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); |
| 92 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 93 | + |
| 94 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 95 | + |
| 96 | + const mcu_pin_obj_t* clock = validate_obj_is_free_pin(args[ARG_clock].u_obj); |
| 97 | + const mcu_pin_obj_t* command = validate_obj_is_free_pin(args[ARG_command].u_obj); |
| 98 | + mcu_pin_obj_t *data_pins[4]; |
| 99 | + uint8_t num_data; |
| 100 | + validate_list_is_free_pins(MP_QSTR_data, data_pins, MP_ARRAY_SIZE(data_pins), args[ARG_data].u_obj, &num_data); |
| 101 | + |
| 102 | + common_hal_sdioio_sdcard_construct(self, clock, command, num_data, data_pins, args[ARG_frequency].u_int); |
| 103 | + return MP_OBJ_FROM_PTR(self); |
| 104 | +} |
| 105 | + |
| 106 | +STATIC void check_for_deinit(sdioio_sdcard_obj_t *self) { |
| 107 | + if (common_hal_sdioio_sdcard_deinited(self)) { |
| 108 | + raise_deinited_error(); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +//| def configure(*, frequency=0, width=0) -> None: |
| 113 | +//| """Configures the SDIO bus. |
| 114 | +//| |
| 115 | +//| :param int frequency: the desired clock rate in Hertz. The actual clock rate may be higher or lower due to the granularity of available clock settings. Check the `frequency` attribute for the actual clock rate. |
| 116 | +//| :param int width: the number of data lines to use. Must be 1 or 4 and must also not exceed the number of data lines at construction |
| 117 | +//| |
| 118 | +//| .. note:: Leaving a value unspecified or 0 means the current setting is kept""" |
| 119 | +//| |
| 120 | +STATIC mp_obj_t sdioio_sdcard_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 121 | + enum { ARG_frequency, ARG_width, NUM_ARGS }; |
| 122 | + static const mp_arg_t allowed_args[] = { |
| 123 | + { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 124 | + { MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 125 | + }; |
| 126 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 127 | + check_for_deinit(self); |
| 128 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 129 | + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); |
| 130 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 131 | + |
| 132 | + mp_int_t frequency = args[ARG_frequency].u_int; |
| 133 | + if (frequency < 0) { |
| 134 | + mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_baudrate); |
| 135 | + } |
| 136 | + |
| 137 | + uint8_t width = args[ARG_width].u_int; |
| 138 | + if (width != 0 && width != 1 && width != 4) { |
| 139 | + mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_width); |
| 140 | + } |
| 141 | + |
| 142 | + if (!common_hal_sdioio_sdcard_configure(self, frequency, width)) { |
| 143 | + mp_raise_OSError(MP_EIO); |
| 144 | + } |
| 145 | + return mp_const_none; |
| 146 | +} |
| 147 | +MP_DEFINE_CONST_FUN_OBJ_KW(sdioio_sdcard_configure_obj, 1, sdioio_sdcard_configure); |
| 148 | + |
| 149 | +//| def count() -> int: |
| 150 | +//| """Returns the total number of sectors |
| 151 | +//| |
| 152 | +//| Due to technical limitations, this is a function and not a property. |
| 153 | +//| |
| 154 | +//| :return: The number of 512-byte blocks, as a number""" |
| 155 | +//| |
| 156 | +STATIC mp_obj_t sdioio_sdcard_count(mp_obj_t self_in) { |
| 157 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 158 | + check_for_deinit(self); |
| 159 | + return MP_OBJ_NEW_SMALL_INT(common_hal_sdioio_sdcard_get_count(self)); |
| 160 | +} |
| 161 | +MP_DEFINE_CONST_FUN_OBJ_1(sdioio_sdcard_count_obj, sdioio_sdcard_count); |
| 162 | + |
| 163 | +//| def readblocks(start_block: int, buf: bytearray) -> None: |
| 164 | +//| |
| 165 | +//| """Read one or more blocks from the card |
| 166 | +//| |
| 167 | +//| :param int start_block: The block to start reading from |
| 168 | +//| :param bytearray buf: The buffer to write into. Length must be multiple of 512. |
| 169 | +//| |
| 170 | +//| :return: None""" |
| 171 | +mp_obj_t sdioio_sdcard_readblocks(mp_obj_t self_in, mp_obj_t start_block_in, mp_obj_t buf_in) { |
| 172 | + uint32_t start_block = mp_obj_get_int(start_block_in); |
| 173 | + mp_buffer_info_t bufinfo; |
| 174 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); |
| 175 | + sdioio_sdcard_obj_t *self = (sdioio_sdcard_obj_t*)self_in; |
| 176 | + int result = common_hal_sdioio_sdcard_readblocks(self, start_block, &bufinfo); |
| 177 | + if (result < 0) { |
| 178 | + mp_raise_OSError(-result); |
| 179 | + } |
| 180 | + return mp_const_none; |
| 181 | +} |
| 182 | + |
| 183 | +MP_DEFINE_CONST_FUN_OBJ_3(sdioio_sdcard_readblocks_obj, sdioio_sdcard_readblocks); |
| 184 | + |
| 185 | +//| def writeblocks(start_block: int, buf: bytearray) -> None: |
| 186 | +//| |
| 187 | +//| """Write one or more blocks to the card |
| 188 | +//| |
| 189 | +//| :param int start_block: The block to start writing from |
| 190 | +//| :param bytearray buf: The buffer to read from. Length must be multiple of 512. |
| 191 | +//| |
| 192 | +//| :return: None""" |
| 193 | +//| |
| 194 | +mp_obj_t sdioio_sdcard_writeblocks(mp_obj_t self_in, mp_obj_t start_block_in, mp_obj_t buf_in) { |
| 195 | + uint32_t start_block = mp_obj_get_int(start_block_in); |
| 196 | + mp_buffer_info_t bufinfo; |
| 197 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); |
| 198 | + sdioio_sdcard_obj_t *self = (sdioio_sdcard_obj_t*)self_in; |
| 199 | + int result = common_hal_sdioio_sdcard_writeblocks(self, start_block, &bufinfo); |
| 200 | + if (result < 0) { |
| 201 | + mp_raise_OSError(-result); |
| 202 | + } |
| 203 | + return mp_const_none; |
| 204 | +} |
| 205 | + |
| 206 | +MP_DEFINE_CONST_FUN_OBJ_3(sdioio_sdcard_writeblocks_obj, sdioio_sdcard_writeblocks); |
| 207 | + |
| 208 | +//| @property |
| 209 | +//| def frequency(self) -> int: |
| 210 | +//| """The actual SDIO bus frequency. This may not match the frequency |
| 211 | +//| requested due to internal limitations.""" |
| 212 | +//| ... |
| 213 | +//| |
| 214 | +STATIC mp_obj_t sdioio_sdcard_obj_get_frequency(mp_obj_t self_in) { |
| 215 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 216 | + check_for_deinit(self); |
| 217 | + return MP_OBJ_NEW_SMALL_INT(common_hal_sdioio_sdcard_get_frequency(self)); |
| 218 | +} |
| 219 | +MP_DEFINE_CONST_FUN_OBJ_1(sdioio_sdcard_get_frequency_obj, sdioio_sdcard_obj_get_frequency); |
| 220 | + |
| 221 | +const mp_obj_property_t sdioio_sdcard_frequency_obj = { |
| 222 | + .base.type = &mp_type_property, |
| 223 | + .proxy = {(mp_obj_t)&sdioio_sdcard_get_frequency_obj, |
| 224 | + (mp_obj_t)&mp_const_none_obj, |
| 225 | + (mp_obj_t)&mp_const_none_obj}, |
| 226 | +}; |
| 227 | + |
| 228 | +//| @property |
| 229 |
10000
+//| def width(self) -> int: |
| 230 | +//| """The actual SDIO bus width, in bits""" |
| 231 | +//| ... |
| 232 | +//| |
| 233 | +STATIC mp_obj_t sdioio_sdcard_obj_get_width(mp_obj_t self_in) { |
| 234 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 235 | + check_for_deinit(self); |
| 236 | + return MP_OBJ_NEW_SMALL_INT(common_hal_sdioio_sdcard_get_width(self)); |
| 237 | +} |
| 238 | +MP_DEFINE_CONST_FUN_OBJ_1(sdioio_sdcard_get_width_obj, sdioio_sdcard_obj_get_width); |
| 239 | + |
| 240 | +const mp_obj_property_t sdioio_sdcard_width_obj = { |
| 241 | + .base.type = &mp_type_property, |
| 242 | + .proxy = {(mp_obj_t)&sdioio_sdcard_get_width_obj, |
| 243 | + (mp_obj_t)&mp_const_none_obj, |
| 244 | + (mp_obj_t)&mp_const_none_obj}, |
| 245 | +}; |
| 246 | + |
| 247 | +//| def deinit() -> None: |
| 248 | +//| """Disable permanently. |
| 249 | +//| |
| 250 | +//| :return: None""" |
| 251 | +STATIC mp_obj_t sdioio_sdcard_obj_deinit(mp_obj_t self_in) { |
| 252 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 253 | + common_hal_sdioio_sdcard_deinit(self); |
| 254 | + return mp_const_none; |
| 255 | +} |
| 256 | +MP_DEFINE_CONST_FUN_OBJ_1(sdioio_sdcard_deinit_obj, sdioio_sdcard_obj_deinit); |
| 257 | + |
| 258 | +//| def __enter__(self, ) -> Any: |
| 259 | +//| """No-op used by Context Managers. |
| 260 | +//| Provided by context manager helper.""" |
| 261 | +//| ... |
| 262 | +//| |
| 263 | + |
| 264 | +//| def __exit__(self, ) -> Any: |
| 265 | +//| """Automatically deinitializes the hardware when exiting a context. See |
| 266 | +//| :ref:`lifetime-and-contextmanagers` for more info.""" |
| 267 | +//| ... |
| 268 | +//| |
| 269 | +STATIC mp_obj_t sdioio_sdcard_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 270 | + (void)n_args; |
| 271 | + common_hal_sdioio_sdcard_deinit(args[0]); |
| 272 | + return mp_const_none; |
| 273 | +} |
| 274 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(sdioio_sdcard_obj___exit___obj, 4, 4, sdioio_sdcard_obj___exit__); |
| 275 | + |
| 276 | +STATIC const mp_rom_map_elem_t sdioio_sdcard_locals_dict_table[] = { |
| 277 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&sdioio_sdcard_deinit_obj) }, |
| 278 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, |
| 279 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&sdioio_sdcard_obj___exit___obj) }, |
| 280 | + |
| 281 | + { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&sdioio_sdcard_configure_obj) }, |
| 282 | + { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&sdioio_sdcard_frequency_obj) }, |
| 283 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&sdioio_sdcard_width_obj) }, |
| 284 | + |
| 285 | + { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&sdioio_sdcard_count_obj) }, |
| 286 | + { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&sdioio_sdcard_readblocks_obj) }, |
| 287 | + { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&sdioio_sdcard_writeblocks_obj) }, |
| 288 | + |
| 289 | +// Methods in STM HAL: |
| 290 | +// InitCard |
| 291 | +// ReadBlocks |
| 292 | +// WriteBlocks |
| 293 | +// Erase |
| 294 | +// GetCardState |
| 295 | +// GetCardCID |
| 296 | +// GetCardCSD |
| 297 | +// GetCardInfo |
| 298 | +// GetState |
| 299 | +// GetError |
| 300 | +// Abort |
| 301 | + |
| 302 | +}; |
| 303 | +STATIC MP_DEFINE_CONST_DICT(sdioio_sdcard_locals_dict, sdioio_sdcard_locals_dict_table); |
| 304 | + |
| 305 | +const mp_obj_type_t sdioio_SDCard_type = { |
| 306 | + { &mp_type_type }, |
| 307 | + .name = MP_QSTR_SDCard, |
| 308 | + .make_new = sdioio_sdcard_make_new, |
| 309 | + .locals_dict = (mp_obj_dict_t*)&sdioio_sdcard_locals_dict, |
| 310 | +}; |
0 commit comments