|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright 2020 Sony Semiconductor Solutions Corporation |
| 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 <fcntl.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <arch/chip/pin.h> |
| 30 | + |
| 31 | +#include "py/mperrno.h" |
| 32 | +#include "py/runtime.h" |
| 33 | + |
| 34 | +#include "shared-bindings/sdioio/SDCard.h" |
| 35 | +#include "shared-bindings/util.h" |
| 36 | + |
| 37 | +#define DATA_PINS_NUM 4 |
| 38 | + |
| 39 | +void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, |
| 40 | + const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command, |
| 41 | + uint8_t num_data, mcu_pin_obj_t **data, uint32_t frequency) { |
| 42 | + struct geometry geo; |
| 43 | + |
| 44 | + if (clock->number != PIN_SDIO_CLK || command->number != PIN_SDIO_CMD) { |
| 45 | + mp_raise_ValueError(translate("Invalid pins")); |
| 46 | + } |
| 47 | + |
| 48 | + uint8_t data_pins_num = 0; |
| 49 | + for (uint8_t i = 0; i < DATA_PINS_NUM; i++) { |
| 50 | + if (data[i]->number != PIN_SDIO_DATA0 || data[i]->number != PIN_SDIO_DATA1 || |
| 51 | + data[i]->number != PIN_SDIO_DATA2 || data[i]->number != PIN_SDIO_DATA3) { |
| 52 | + data_pins_num++; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (data_pins_num != DATA_PINS_NUM) { |
| 57 | + mp_raise_ValueError(translate("Invalid pins")); |
| 58 | + } |
| 59 | + |
| 60 | + if (open_blockdriver("/dev/mmcsd0", 0, &self->inode) < 0) { |
| 61 | + mp_raise_ValueError(translate("Could not initialize SDCard")); |
| 62 | + } |
| 63 | + |
| 64 | + self->inode->u.i_bops->geometry(self->inode, &geo); |
| 65 | + |
| 66 | + claim_pin(clock); |
| 67 | + claim_pin(command); |
| 68 | + self->clock_pin = clock; |
| 69 | + self->command_pin = command; |
| 70 | + for (uint8_t i = 0; i < DATA_PINS_NUM; i++) { |
| 71 | + claim_pin(data[i]); |
| 72 | + self->data_pins[i] = data[i]; |
| 73 | + } |
| 74 | + |
| 75 | + self->count = geo.geo_nsectors; |
| 76 | + self->frequency = frequency; |
| 77 | + self->width = num_data; |
| 78 | +} |
| 79 | + |
| 80 | +void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) { |
| 81 | + close_blockdriver(self->inode); |
| 82 | + self->inode = NULL; |
| 83 | + |
| 84 | + reset_pin_number(self->clock_pin->number); |
| 85 | + reset_pin_number(self->command_pin->number); |
| 86 | + for (uint8_t i = 0; i < DATA_PINS_NUM; i++) { |
| 87 | + reset_pin_number(self->data_pins[i]->number); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +bool common_hal_sdioio_sdcard_deinited(sdioio_sdcard_obj_t *self) { |
| 92 | + return self->inode == NULL; |
| 93 | +} |
| 94 | + |
| 95 | +bool common_hal_sdioio_sdcard_configure(sdioio_sdcard_obj_t *self, uint32_t baudrate, uint8_t width) { |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | +uint32_t common_hal_sdioio_sdcard_get_frequency(sdioio_sdcard_obj_t* self) { |
| 100 | + return self->frequency; |
| 101 | +} |
| 102 | + |
| 103 | +uint8_t common_hal_sdioio_sdcard_get_width(sdioio_sdcard_obj_t* self) { |
| 104 | + return self->width; |
| 105 | +} |
| 106 | + |
| 107 | +uint32_t common_hal_sdioio_sdcard_get_count(sdioio_sdcard_obj_t* self) { |
| 108 | + return self->count; |
| 109 | +} |
| 110 | + |
| 111 | +STATIC void check_whole_block(mp_buffer_info_t *bufinfo) { |
| 112 | + if (bufinfo->len % 512) { |
| 113 | + mp_raise_ValueError(translate("Buffer length must be a multiple of 512")); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +int common_hal_sdioio_sdcard_readblocks(sdioio_sdcard_obj_t* self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 118 | + if (common_hal_sdioio_sdcard_deinited(self)) { |
| 119 | + raise_deinited_error(); |
| 120 | + } |
| 121 | + check_whole_block(bufinfo); |
| 122 | + |
| 123 | + return self->inode->u.i_bops->read(self->inode, bufinfo->buf, start_block, bufinfo->len / 512); |
| 124 | +} |
| 125 | + |
| 126 | +int common_hal_sdioio_sdcard_writeblocks(sdioio_sdcard_obj_t* self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 127 | + if (common_hal_sdioio_sdcard_deinited(self)) { |
| 128 | + raise_deinited_error(); |
| 129 | + } |
| 130 | + check_whole_block(bufinfo); |
| 131 | + |
| 132 | + return self->inode->u.i_bops->write(self->inode, bufinfo->buf, start_block, bufinfo->len / 512);; |
| 133 | +} |
| 134 | + |
| 135 | +void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) { |
| 136 | + never_reset_pin_number(self->clock_pin->number); |
| 137 | + never_reset_pin_number(self->command_pin->number); |
| 138 | + for (uint8_t i = 0; i < DATA_PINS_NUM; i++) { |
| 139 | + never_reset_pin_number(self->data_pins[i]->number); |
| 140 | + } |
| 141 | +} |
0 commit comments