|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2024 Jacob Rigby |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include <stdbool.h> |
| 8 | +#include "esp_err.h" |
| 9 | + |
| 10 | +//#include "ports/espressif/esp-idf/components/esp_driver_sdmmc/include/driver/sdmmc_host.h" |
| 11 | +#include "driver/sdmmc_host.h" |
| 12 | +#include "ports/espressif/esp-idf/components/sdmmc/include/sdmmc_cmd.h" |
| 13 | + |
| 14 | +#include "shared-bindings/microcontroller/Pin.h" |
| 15 | +#include "shared-bindings/util.h" |
| 16 | +#include "shared-bindings/sdioio/SDCard.h" |
| 17 | +#include "py/mperrno.h" |
| 18 | +#include "py/runtime.h" |
| 19 | + |
| 20 | +#include "esp_log.h" |
| 21 | +static const char *TAG = "SDCard.c"; |
| 22 | + |
| 23 | +static bool sdio_host_init = false; |
| 24 | + |
| 25 | +static void common_hal_sdioio_sdcard_check_for_deinit(sdioio_sdcard_obj_t *self) { |
| 26 | + if (common_hal_sdioio_sdcard_deinited(self)) { |
| 27 | + raise_deinited_error(); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +static int check_pins(const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command, const uint8_t num_data, const mcu_pin_obj_t **data) { |
| 32 | + if (CONFIG_SOC_SDMMC_USE_GPIO_MATRIX) { |
| 33 | + // ESP32-S3 and P4 can use any pin for any SDMMC func in either slot |
| 34 | + // Default to SLOT_1 for SD cards |
| 35 | + ESP_LOGI(TAG, "Using chip with CONFIG_SOC_SDMMC_USE_GPIO_MATRIX"); |
| 36 | + return SDMMC_HOST_SLOT_1; |
| 37 | + } |
| 38 | + if (command->number == GPIO_NUM_11 && clock->number == GPIO_NUM_6 && data[0]->number == GPIO_NUM_7) { |
| 39 | + // Might be slot 0 |
| 40 | + if (num_data == 1 || (num_data == 4 && data[1]->number == GPIO_NUM_8 && data[2]->number == GPIO_NUM_9 && data[3]->number == GPIO_NUM_10)) { |
| 41 | + return SDMMC_HOST_SLOT_0; |
| 42 | + } |
| 43 | + } else if (command->number == GPIO_NUM_15 && clock->number == GPIO_NUM_14 && data[0]->number == 2) { |
| 44 | + // Might be slot 1 |
| 45 | + if (num_data == 1 || (num_data == 4 && data[1]->number == GPIO_NUM_4 && data[2]->number == GPIO_NUM_12 && data[3]->number == GPIO_NUM_13)) { |
| 46 | + return SDMMC_HOST_SLOT_1; |
| 47 | + } |
| 48 | + } |
| 49 | + return -1; |
| 50 | +} |
| 51 | + |
| 52 | +void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, |
| 53 | + const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command, |
| 54 | + uint8_t num_data, const mcu_pin_obj_t **data, uint32_t frequency) { |
| 55 | + if (num_data != 4 && num_data != 1) { |
| 56 | + mp_raise_ValueError(MP_ERROR_TEXT("SDIO: cards have either 1 or 4 data lines")); |
| 57 | + } |
| 58 | + |
| 59 | + self->num_data = num_data; |
| 60 | + int sd_slot = check_pins(clock, command, num_data, data); |
| 61 | + if (sd_slot != SDMMC_HOST_SLOT_0 && sd_slot != SDMMC_HOST_SLOT_1) { |
| 62 | + // Bad pin combo |
| 63 | + raise_ValueError_invalid_pins(); |
| 64 | + } |
| 65 | + |
| 66 | + if (frequency > 40000000) { |
| 67 | + // Higher than max 40Mhz frequency |
| 68 | + mp_raise_ValueError(MP_ERROR_TEXT("SDIO: requested frequency out of range")); |
| 69 | + } |
| 70 | + |
| 71 | + ESP_LOGI(TAG, "Using slot %d", sd_slot); |
| 72 | + self->slot = (uint8_t)sd_slot; |
| 73 | + esp_err_t err = ESP_OK; |
| 74 | + |
| 75 | + sdmmc_host_t host = SDMMC_HOST_DEFAULT(); |
| 76 | + host.max_freq_khz = frequency / 1000; |
| 77 | + |
| 78 | + sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); |
| 79 | + slot_config.width = 1; |
| 80 | + slot_config.clk = clock->number; |
| 81 | + claim_pin(clock); |
| 82 | + self->clock = clock->number; |
| 83 | + slot_config.cmd = command->number; |
| 84 | + claim_pin(command); |
| 85 | + self->command = command->number; |
| 86 | + slot_config.d0 = data[0]->number; |
| 87 | + self->data[0] = data[0]->number; |
| 88 | + claim_pin(data[0]); |
| 89 | + if (num_data == 4) { |
| 90 | + slot_config.width = 4; |
| 91 | + slot_config.d1 = data[1]->number; |
| 92 | + claim_pin(data[1]); |
| 93 | + self->data[1] = data[1]->number; |
| 94 | + slot_config.d2 = data[2]->number; |
| 95 | + claim_pin(data[2]); |
| 96 | + self->data[2] = data[2]->number; |
| 97 | + slot_config.d3 = data[3]->number; |
| 98 | + claim_pin(data[3]); |
| 99 | + self->data[3] = data[3]->number; |
| 100 | + } |
| 101 | + |
| 102 | + ESP_LOGI(TAG, "slot_config:\nwidth: %d, clk: %d, cmd: %d\nd0: %d, d1: %d, d2: %d, d3: %d", |
| 103 | + slot_config.width, slot_config.clk, slot_config.cmd, |
| 104 | + slot_config.d0, slot_config.d1, slot_config.d2, slot_config.d3); |
| 105 | + |
| 106 | + if (!sdio_host_init) { |
| 107 | + err = sdmmc_host_init(); |
| 108 | + if (err != ESP_OK) { |
| 109 | + mp_raise_OSError_msg_varg(MP_ERROR_TEXT("SDIO Init Error %x"), err); |
| 110 | + } |
| 111 | + // sdio_host_init = true; |
| 112 | + } |
| 113 | + err = sdmmc_host_init_slot(sd_slot, &slot_config); |
| 114 | + if (err != ESP_OK) { |
| 115 | + ESP_LOGW(TAG, "Failed to initialize SDMMC slot: %x", err); |
| 116 | + mp_raise_OSError_msg_varg(MP_ERROR_TEXT("SDIO Init Error %x"), err); |
| 117 | + } |
| 118 | + // sdmmc_card_t card; |
| 119 | + // self->card = malloc(sizeof(sdmmc_card_t)); |
| 120 | + err = sdmmc_card_init(&host, &self->card); |
| 121 | + if (err != ESP_OK) { |
| 122 | + ESP_LOGW(TAG, "Failed to initialize SDMMC card: %x", err); |
| 123 | + mp_raise_OSError_msg_varg(MP_ERROR_TEXT("SDIO Init Error %x"), err); |
| 124 | + } |
| 125 | + |
| 126 | + common_hal_sdioio_sdcard_check_for_deinit(self); |
| 127 | + |
| 128 | + ESP_LOGI(TAG, "Initialized SD card with ID %d:%d-%s", |
| 129 | + self->card.cid.mfg_id, self->card.cid.oem_id, self->card.cid.name); |
| 130 | + |
| 131 | + ESP_LOGI(TAG, "Number of sectors: %d with sector_size: %d", |
| 132 | + self->card.csd.capacity, self->card.csd.sector_size); |
| 133 | + |
| 134 | + self->frequency = self->card.real_freq_khz; |
| 135 | + ESP_LOGI(TAG, "Real frequency is %lu", self->frequency); |
| 136 | + self->capacity = self->card.csd.capacity; // Reported number of sectors |
| 137 | + ESP_LOGI(TAG, "Reported capacity is %lu", self->capacity); |
| 138 | + |
| 139 | + return; |
| 140 | +} |
| 141 | + |
| 142 | +uint32_t common_hal_sdioio_sdcard_get_count(sdioio_sdcard_obj_t *self) { |
| 143 | + return self->capacity; |
| 144 | +} |
| 145 | + |
| 146 | +uint32_t common_hal_sdioio_sdcard_get_frequency(sdioio_sdcard_obj_t *self) { |
| 147 | + return self->frequency; |
| 148 | +} |
| 149 | + |
| 150 | +uint8_t common_hal_sdioio_sdcard_get_width(sdioio_sdcard_obj_t *self) { |
| 151 | + return self->num_data; |
| 152 | +} |
| 153 | + |
| 154 | +static void check_whole_block(mp_buffer_info_t *bufinfo, int sector_size) { |
| 155 | + if (bufinfo->len % sector_size) { |
| 156 | + mp_raise_ValueError_varg(MP_ERROR_TEXT("Buffer must be a multiple of %d bytes"), sector_size); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +int common_hal_sdioio_sdcard_writeblocks(sdioio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 161 | + common_hal_sdioio_sdcard_check_for_deinit(self); |
| 162 | + check_whole_block(bufinfo, self->card.csd.sector_size); |
| 163 | + esp_err_t err; |
| 164 | + ESP_LOGI(TAG, "in common_hal_sdioio_sdcard_writeblocks"); |
| 165 | + // err = sdmmc_io_write_blocks(&self->card, 1, start_block, bufinfo->buf, bufinfo->len); |
| 166 | + err = sdmmc_write_sectors(&self->card, bufinfo->buf, start_block, bufinfo->len / self->card.csd.sector_size); |
| 167 | + if (err != ESP_OK) { |
| 168 | + ESP_LOGW(TAG, "Failed to write blocks with err 0x%X", err); |
| 169 | + } |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +int common_hal_sdioio_sdcard_readblocks(sdioio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 174 | + common_hal_sdioio_sdcard_check_for_deinit(self); |
| 175 | + check_whole_block(bufinfo, self->card.csd.sector_size); |
| 176 | + esp_err_t err; |
| 177 | + ESP_LOGI(TAG, "in common_hal_sdioio_sdcard_readblocks"); |
| 178 | + // err = sdmmc_io_read_blocks(&self->card, 1, start_block, bufinfo->buf, bufinfo->len); |
| 179 | + err = sdmmc_read_sectors(&self->card, bufinfo->buf, start_block, bufinfo->len / self->card.csd.sector_size); |
| 180 | + if (err != ESP_OK) { |
| 181 | + ESP_LOGW(TAG, "Failed to read blocks with err 0x%X", err); |
| 182 | + } |
| 183 | + return 0; |
| 184 | +} |
| 185 | + |
| 186 | +bool common_hal_sdioio_sdcard_configure(sdioio_sdcard_obj_t *self, uint32_t frequency, uint8_t bits) { |
| 187 | + return true; |
| 188 | +} |
| 189 | + |
| 190 | +bool common_hal_sdioio_sdcard_deinited(sdioio_sdcard_obj_t *self) { |
| 191 | + return self->command == COMMON_HAL_MCU_NO_PIN; |
| 192 | +} |
| 193 | + |
| 194 | +void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) { |
| 195 | + if (common_hal_sdioio_sdcard_deinited(self)) { |
| 196 | + return; |
| 197 | + } |
| 198 | + sdmmc_host_deinit(); |
| 199 | + reset_pin_number(self->command); |
| 200 | + self->command = COMMON_HAL_MCU_NO_PIN; |
| 201 | + reset_pin_number(self->clock); |
| 202 | + self->clock = COMMON_HAL_MCU_NO_PIN; |
| 203 | + reset_pin_number(self->data[0]); |
| 204 | + self->data[0] = COMMON_HAL_MCU_NO_PIN; |
| 205 | + if (self->num_data == 4) { |
| 206 | + reset_pin_number(self->data[1]); |
| 207 | + self->data[1] = COMMON_HAL_MCU_NO_PIN; |
| 208 | + reset_pin_number(self->data[2]); |
| 209 | + self->data[2] = COMMON_HAL_MCU_NO_PIN; |
| 210 | + reset_pin_number(self->data[3]); |
| 211 | + self->data[3] = COMMON_HAL_MCU_NO_PIN; |
| 212 | + } |
| 213 | + return; |
| 214 | +} |
| 215 | + |
| 216 | +void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) { |
| 217 | + if (common_hal_sdioio_sdcard_deinited(self)) { |
| 218 | + return; |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +void sdioio_reset() { |
| 223 | + return; |
| 224 | +} |
0 commit comments