|
| 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 | +//| .. currentmodule:: sdcardio |
| 40 | +//| |
| 41 | +//| :class:`SDCard` -- SD Card Block Interface |
| 42 | +//| =========================================== |
| 43 | +//| |
| 44 | + |
| 45 | +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) { |
| 46 | + enum { ARG_spi, ARG_cs, ARG_baudrate, NUM_ARGS }; |
| 47 | + static const mp_arg_t allowed_args[] = { |
| 48 | + { MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 49 | + { MP_QSTR_cs, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 50 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 1320000} }, |
| 51 | + }; |
| 52 | + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); |
| 53 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 54 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 55 | + |
| 56 | + busio_spi_obj_t *spi = validate_obj_is_spi_bus(args[ARG_spi].u_obj); |
| 57 | + mcu_pin_obj_t *cs = validate_obj_is_free_pin(args[ARG_cs].u_obj); |
| 58 | + |
| 59 | + sdcardio_sdcard_obj_t *self = m_new_obj(sdcardio_sdcard_obj_t); |
| 60 | + self->base.type = &sdcardio_SDCard_type; |
| 61 | + |
| 62 | + common_hal_sdcardio_sdcard_construct(self, spi, cs, args[ARG_baudrate].u_int); |
| 63 | + |
| 64 | + return self; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +//| .. method:: count |
| 69 | +//| |
| 70 | +//| Returns the total number of sectors |
| 71 | +//| |
| 72 | +//| :return: The number of 512-byte blocks |
| 73 | +//| :rtpye: int |
| 74 | +//| |
| 75 | +mp_obj_t sdcardio_sdcard_count(mp_obj_t self_in) { |
| 76 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 77 | + return mp_obj_new_int_from_ull(common_hal_sdcardio_sdcard_get_blockcount(self)); |
| 78 | +} |
| 79 | +MP_DEFINE_CONST_FUN_OBJ_1(sdcardio_sdcard_count_obj, sdcardio_sdcard_count); |
| 80 | + |
| 81 | +//| .. method:: deinit |
| 82 | +//| |
| 83 | +//| Disable permanently. |
| 84 | +//| |
| 85 | +//| |
| 86 | +mp_obj_t sdcardio_sdcard_deinit(mp_obj_t self_in) { |
| 87 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 88 | + common_hal_sdcardio_sdcard_deinit(self); |
| 89 | + return mp_const_none; |
| 90 | +} |
| 91 | +MP_DEFINE_CONST_FUN_OBJ_1(sdcardio_sdcard_deinit_obj, sdcardio_sdcard_deinit); |
| 92 | + |
| 93 | + |
| 94 | +//| .. method:: readblocks |
| 95 | +//| |
| 96 | +//| Read one or more blocks from the card |
| 97 | +//| |
| 98 | +//| :param int start_block: The block to start reading from |
| 99 | +//| :param bytearray buf: The buffer to write into. Length must be multiple of 512. |
| 100 | +//| |
| 101 | + |
| 102 | +mp_obj_t sdcardio_sdcard_readblocks(mp_obj_t self_in, mp_obj_t start_block_in, mp_obj_t buf_in) { |
| 103 | + uint32_t start_block = mp_obj_get_int(start_block_in); |
| 104 | + mp_buffer_info_t bufinfo; |
| 105 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); |
| 106 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 107 | + int result = common_hal_sdcardio_sdcard_readblocks(self, start_block, &bufinfo); |
| 108 | + if (result < 0) { |
| 109 | + mp_raise_OSError(-result); |
| 110 | + } |
| 111 | + return mp_const_none; |
| 112 | +} |
| 113 | + |
| 114 | +MP_DEFINE_CONST_FUN_OBJ_3(sdcardio_sdcard_readblocks_obj, sdcardio_sdcard_readblocks); |
| 115 | + |
| 116 | +//| .. method:: writeblocks |
| 117 | +//| |
| 118 | +//| Write one or more blocks to the card |
| 119 | +//| |
| 120 | +//| :param int start_block: The block to start writing from |
| 121 | +//| :param bytearray buf: The buffer to read from. Length must be multiple of 512. |
| 122 | +//| |
| 123 | + |
| 124 | +mp_obj_t sdcardio_sdcard_writeblocks(mp_obj_t self_in, mp_obj_t start_block_in, mp_obj_t buf_in) { |
| 125 | + uint32_t start_block = mp_obj_get_int(start_block_in); |
| 126 | + mp_buffer_info_t bufinfo; |
| 127 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); |
| 128 | + sdcardio_sdcard_obj_t *self = (sdcardio_sdcard_obj_t*)self_in; |
| 129 | + int result = common_hal_sdcardio_sdcard_writeblocks(self, start_block, &bufinfo); |
| 130 | + if (result < 0) { |
| 131 | + mp_raise_OSError(-result); |
| 132 | + } |
| 133 | + return mp_const_none; |
| 134 | +} |
| 135 | +MP_DEFINE_CONST_FUN_OBJ_3(sdcardio_sdcard_writeblocks_obj, sdcardio_sdcard_writeblocks); |
| 136 | + |
| 137 | +STATIC const mp_rom_map_elem_t sdcardio_sdcard_locals_dict_table[] = { |
| 138 | + { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&sdcardio_sdcard_count_obj) }, |
| 139 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&sdcardio_sdcard_deinit_obj) }, |
| 140 | + { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&sdcardio_sdcard_readblocks_obj) }, |
| 141 | + { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&sdcardio_sdcard_writeblocks_obj) }, |
| 142 | +}; |
| 143 | +STATIC MP_DEFINE_CONST_DICT(sdcardio_sdcard_locals_dict, sdcardio_sdcard_locals_dict_table); |
| 144 | + |
| 145 | +const mp_obj_type_t sdcardio_SDCard_type = { |
| 146 | + { &mp_type_type }, |
| 147 | + .name = MP_QSTR_SDCard, |
| 148 | + .make_new = sdcardio_sdcard_make_new, |
| 149 | + .locals_dict = (mp_obj_dict_t*)&sdcardio_sdcard_locals_dict, |
| 150 | +}; |
0 commit comments