|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython 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 | +#include <stdbool.h> |
| 27 | + |
| 28 | +#include "shared-bindings/_sdcardio/SDIOSDCard.h" |
| 29 | +#include "py/mperrno.h" |
| 30 | +#include "py/runtime.h" |
| 31 | + |
| 32 | +#include "shared-bindings/microcontroller/__init__.h" |
| 33 | +#include "boards/board.h" |
| 34 | +#include "supervisor/shared/translate.h" |
| 35 | +#include "common-hal/microcontroller/Pin.h" |
| 36 | + |
| 37 | +#ifndef DEBUG_SDIO |
| 38 | +#define DEBUG_SDIO (0) |
| 39 | +#endif |
| 40 | + |
| 41 | +#if DEBUG_SDIO |
| 42 | +#define DEBUG_PRINT(...) ((void)mp_printf(&mp_plat_print, __VA_ARGS__)) |
| 43 | +#else |
| 44 | +#define DEBUG_PRINT(...) ((void)0) |
| 45 | +#endif |
| 46 | + |
| 47 | +void common_hal_sdcardio_sdio_construct(sdcardio_sdiosdcard_obj_t *self, |
| 48 | + const mcu_pin_obj_t * clock, const mcu_pin_obj_t * command, |
| 49 | + uint8_t num_data, mcu_pin_obj_t ** data, uint32_t frequency) { |
| 50 | + // constructor arguments are ignored, and the hard-wired SD slot on the board is used. |
| 51 | + |
| 52 | + GPIO_InitTypeDef GPIO_InitStruct; |
| 53 | + |
| 54 | + // /* GPIOC and GPIOD Periph clock enable */ |
| 55 | + // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | SD_DETECT_GPIO_CLK, ENABLE); |
| 56 | + |
| 57 | + /* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */ |
| 58 | + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; |
| 59 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| 60 | + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 61 | + GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 62 | + GPIO_InitStruct.Alternate = 12; // is there an identifier for this? |
| 63 | + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 64 | + |
| 65 | + /* Configure PD.02 CMD line */ |
| 66 | + GPIO_InitStruct.Pin = GPIO_PIN_2; |
| 67 | + HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
| 68 | + |
| 69 | + /* Configure PC.12 pin: CLK pin */ |
| 70 | + GPIO_InitStruct.Pin = GPIO_PIN_12; |
| 71 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 72 | + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 73 | + |
| 74 | +#define SD_DETECT_PIN GPIO_PIN_12 |
| 75 | +#define SD_DETECT_GPIO_PORT GPIOB |
| 76 | + |
| 77 | + /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */ |
| 78 | + GPIO_InitStruct.Pin = SD_DETECT_PIN; |
| 79 | + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 80 | + GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 81 | + HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStruct); |
| 82 | + |
| 83 | + __HAL_RCC_SDIO_CLK_ENABLE(); |
| 84 | + |
| 85 | + self->handle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV; |
| 86 | + self->handle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; |
| 87 | + self->handle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; |
| 88 | + self->handle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; |
| 89 | + self->handle.Init.BusWide = SDIO_BUS_WIDE_1B; |
| 90 | + self->handle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; |
| 91 | + self->handle.Instance = SDIO; |
| 92 | + |
| 93 | + HAL_StatusTypeDef r = HAL_SD_Init(&self->handle); |
| 94 | + if (r != HAL_OK) { |
| 95 | + mp_raise_ValueError_varg(translate("SDIO Init Error %d"), (int)r); |
| 96 | + } |
| 97 | + |
| 98 | + HAL_SD_CardInfoTypeDef info; |
| 99 | + r = HAL_SD_GetCardInfo(&self->handle, &info); |
| 100 | + if (r != HAL_OK) { |
| 101 | + mp_raise_ValueError_varg(translate("SDIO GetCardInfo Error %d"), (int)r); |
| 102 | + } |
| 103 | + |
| 104 | + if ((r = HAL_SD_ConfigWideBusOperation(&self->handle, SDIO_BUS_WIDE_4B)) == HAL_SD_ERROR_NONE) { |
| 105 | + DEBUG_PRINT("Switched bus to 4B mode\n"); |
| 106 | + self->handle.Init.BusWide = SDIO_BUS_WIDE_4B; |
| 107 | + self->width = 4; |
| 108 | + } else { |
| 109 | + DEBUG_PRINT("WideBus_Enable returned %r, leaving at 1B mode\n", (int)r); |
| 110 | + self->width = 1; |
| 111 | + } |
| 112 | + |
| 113 | + self->capacity = info.BlockNbr * (info.BlockSize / 512); |
| 114 | + self->frequency = 25000000; |
| 115 | + |
| 116 | + return; |
| 117 | +} |
| 118 | + |
| 119 | +uint32_t common_hal_sdcardio_sdio_get_count(sdcardio_sdiosdcard_obj_t *self) { |
| 120 | + return self->capacity; |
| 121 | +} |
| 122 | + |
| 123 | +uint32_t common_hal_sdcardio_sdio_get_frequency(sdcardio_sdiosdcard_obj_t *self) { |
| 124 | + return self->frequency; // self->frequency; |
| 125 | +} |
| 126 | + |
| 127 | +uint8_t common_hal_sdcardio_sdio_get_width(sdcardio_sdiosdcard_obj_t *self) { |
| 128 | + return self->width; // self->width; |
| 129 | +} |
| 130 | + |
| 131 | +STATIC void check_for_deinit(sdcardio_sdiosdcard_obj_t *self) { |
| 132 | +} |
| 133 | + |
| 134 | +STATIC void check_whole_block(mp_buffer_info_t *bufinfo) { |
| 135 | + if (bufinfo->len % 512) { |
| 136 | + mp_raise_ValueError(translate("Buffer must be a multiple of 512 bytes")); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +STATIC void wait_write_complete(sdcardio_sdiosdcard_obj_t *self) { |
| 141 | + if (self->state_programming) { |
| 142 | + HAL_SD_CardStateTypedef st = HAL_SD_CARD_PROGRAMMING; |
| 143 | + for (int i=0; i < 50000 && st == HAL_SD_CARD_PROGRAMMING; i++) { |
| 144 | + st = HAL_SD_GetCardState(&self->handle); |
| 145 | + HAL_Delay(10); |
| 146 | + }; |
| 147 | + self->state_programming = false; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +STATIC void debug_print_state(sdcardio_sdiosdcard_obj_t *self, const char *what) { |
| 152 | +#if DEBUG_SDIO |
| 153 | + HAL_SD_CardStateTypedef st = HAL_SD_GetCardState(&self->handle); |
| 154 | + DEBUG_PRINT("%s, st=0x%x State=0x%x ErrorCode=0x%x\n", what, (int)st, self->handle.State, self->handle.ErrorCode); |
| 155 | +#endif |
| 156 | +} |
| 157 | + |
| 158 | +int common_hal_sdcardio_sdio_writeblocks(sdcardio_sdiosdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 159 | + check_for_deinit(self); |
| 160 | + check_whole_block(bufinfo); |
| 161 | + wait_write_complete(self); |
| 162 | + self->state_programming = true; |
| 163 | + common_hal_mcu_disable_interrupts(); |
| 164 | + HAL_StatusTypeDef r = HAL_SD_WriteBlocks(&self->handle, bufinfo->buf, start_block, bufinfo->len / 512, 1000); |
| 165 | + common_hal_mcu_enable_interrupts(); |
| 166 | + if (r != HAL_OK) { |
| 167 | + debug_print_state(self, "after writeblocks error"); |
| 168 | + return -EIO; |
| 169 | + } |
| 170 | + // debug_print_state(self, "after writeblocks OK"); |
| 171 | + // debug_print_state(self, "after writeblocks complete"); |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
| 175 | +int common_hal_sdcardio_sdio_readblocks(sdcardio_sdiosdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 176 | + check_for_deinit(self); |
| 177 | + check_whole_block(bufinfo); |
| 178 | + wait_write_complete(self); |
| 179 | + common_hal_mcu_disable_interrupts(); |
| 180 | + HAL_StatusTypeDef r = HAL_SD_ReadBlocks(&self->handle, bufinfo->buf, start_block, bufinfo->len / 512, 1000); |
| 181 | + common_hal_mcu_enable_interrupts(); |
| 182 | + if (r != HAL_OK) { |
| 183 | + debug_print_state(self, "after readblocks error"); |
| 184 | + return -EIO; |
| 185 | + } |
| 186 | + return 0; |
| 187 | +} |
| 188 | + |
| 189 | +bool common_hal_sdcardio_sdio_configure(sdcardio_sdiosdcard_obj_t *self, uint32_t frequency, uint8_t bits) { |
| 190 | + check_for_deinit(self); |
| 191 | + return true; |
| 192 | +} |
| 193 | + |
| 194 | +bool common_hal_sdcardio_sdio_deinited(sdcardio_sdiosdcard_obj_t *self) { |
| 195 | + return false; |
| 196 | +} |
| 197 | + |
| 198 | +void common_hal_sdcardio_sdio_deinit(sdcardio_sdiosdcard_obj_t *self) { |
| 199 | +} |
| 200 | + |
| 201 | +void common_hal_sdcardio_sdio_never_reset(sdcardio_sdiosdcard_obj_t *self) { |
| 202 | +} |
| 203 | + |
0 commit comments