2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-04-01 05:55:04 +01:00
|
|
|
* Copyright (c) 2013-2019 Damien P. George
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-05-13 14:45:40 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/runtime.h"
|
2017-03-02 04:32:32 +00:00
|
|
|
#include "py/mphal.h"
|
2017-01-27 12:11:59 +00:00
|
|
|
#include "lib/oofatfs/ff.h"
|
|
|
|
#include "extmod/vfs_fat.h"
|
2016-02-10 16:21:38 +00:00
|
|
|
|
2014-03-17 13:03:41 +00:00
|
|
|
#include "sdcard.h"
|
2014-03-30 12:30:35 +01:00
|
|
|
#include "pin.h"
|
2018-07-20 04:57:58 +01:00
|
|
|
#include "pin_static_af.h"
|
2014-09-09 15:09:07 +01:00
|
|
|
#include "bufhelper.h"
|
2015-11-16 01:26:43 +00:00
|
|
|
#include "dma.h"
|
|
|
|
#include "irq.h"
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_SDCARD || MICROPY_HW_ENABLE_MMCARD
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2018-03-24 19:23:35 +00:00
|
|
|
#if defined(STM32F7) || defined(STM32H7) || defined(STM32L4)
|
2015-10-29 23:56:58 +00:00
|
|
|
|
2021-04-29 03:08:20 +01:00
|
|
|
// The H7/F7/L4 have 2 SDMMC peripherals, but at the moment this driver only supports
|
|
|
|
// using one of them in a given build, selected by MICROPY_HW_SDCARD_SDMMC.
|
2017-03-30 07:55:21 +01:00
|
|
|
|
2021-04-29 03:08:20 +01:00
|
|
|
#if MICROPY_HW_SDCARD_SDMMC == 2
|
2017-03-30 07:55:21 +01:00
|
|
|
#define SDIO SDMMC2
|
2020-10-02 20:44:15 +01:00
|
|
|
#define SDMMC_IRQHandler SDMMC2_IRQHandler
|
2017-03-30 07:55:21 +01:00
|
|
|
#define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC2_CLK_ENABLE()
|
|
|
|
#define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC2_CLK_DISABLE()
|
2021-04-29 03:08:20 +01:00
|
|
|
#define SDMMC_FORCE_RESET() __HAL_RCC_SDMMC2_FORCE_RESET()
|
|
|
|
#define SDMMC_RELEASE_RESET() __HAL_RCC_SDMMC2_RELEASE_RESET()
|
2017-03-30 07:55:21 +01:00
|
|
|
#define SDMMC_IRQn SDMMC2_IRQn
|
2018-09-11 08:20:34 +01:00
|
|
|
#define SDMMC_DMA dma_SDMMC_2
|
2021-04-29 03:08:20 +01:00
|
|
|
#define STATIC_AF_SDCARD_CK STATIC_AF_SDMMC2_CK
|
|
|
|
#define STATIC_AF_SDCARD_CMD STATIC_AF_SDMMC2_CMD
|
|
|
|
#define STATIC_AF_SDCARD_D0 STATIC_AF_SDMMC2_D0
|
|
|
|
#define STATIC_AF_SDCARD_D1 STATIC_AF_SDMMC2_D1
|
|
|
|
#define STATIC_AF_SDCARD_D2 STATIC_AF_SDMMC2_D2
|
|
|
|
#define STATIC_AF_SDCARD_D3 STATIC_AF_SDMMC2_D3
|
2021-12-13 13:04:57 +00:00
|
|
|
#define STATIC_AF_SDCARD_D4 STATIC_AF_SDMMC2_D4
|
|
|
|
#define STATIC_AF_SDCARD_D5 STATIC_AF_SDMMC2_D5
|
|
|
|
#define STATIC_AF_SDCARD_D6 STATIC_AF_SDMMC2_D6
|
|
|
|
#define STATIC_AF_SDCARD_D7 STATIC_AF_SDMMC2_D7
|
2017-03-30 07:55:21 +01:00
|
|
|
#else
|
|
|
|
#define SDIO SDMMC1
|
2020-10-02 20:44:15 +01:00
|
|
|
#define SDMMC_IRQHandler SDMMC1_IRQHandler
|
2017-03-30 07:55:21 +01:00
|
|
|
#define SDMMC_CLK_ENABLE() __HAL_RCC_SDMMC1_CLK_ENABLE()
|
|
|
|
#define SDMMC_CLK_DISABLE() __HAL_RCC_SDMMC1_CLK_DISABLE()
|
2021-04-29 03:08:20 +01:00
|
|
|
#define SDMMC_FORCE_RESET() __HAL_RCC_SDMMC1_FORCE_RESET()
|
|
|
|
#define SDMMC_RELEASE_RESET() __HAL_RCC_SDMMC1_RELEASE_RESET()
|
2017-03-30 07:55:21 +01:00
|
|
|
#define SDMMC_IRQn SDMMC1_IRQn
|
2018-09-11 08:20:34 +01:00
|
|
|
#define SDMMC_DMA dma_SDIO_0
|
2021-04-29 03:08:20 +01:00
|
|
|
#define STATIC_AF_SDCARD_CK STATIC_AF_SDMMC1_CK
|
|
|
|
#define STATIC_AF_SDCARD_CMD STATIC_AF_SDMMC1_CMD
|
|
|
|
#define STATIC_AF_SDCARD_D0 STATIC_AF_SDMMC1_D0
|
|
|
|
#define STATIC_AF_SDCARD_D1 STATIC_AF_SDMMC1_D1
|
|
|
|
#define STATIC_AF_SDCARD_D2 STATIC_AF_SDMMC1_D2
|
|
|
|
#define STATIC_AF_SDCARD_D3 STATIC_AF_SDMMC1_D3
|
2021-12-13 13:04:57 +00:00
|
|
|
#define STATIC_AF_SDCARD_D4 STATIC_AF_SDMMC1_D4
|
|
|
|
#define STATIC_AF_SDCARD_D5 STATIC_AF_SDMMC1_D5
|
|
|
|
#define STATIC_AF_SDCARD_D6 STATIC_AF_SDMMC1_D6
|
|
|
|
#define STATIC_AF_SDCARD_D7 STATIC_AF_SDMMC1_D7
|
2017-03-30 07:55:21 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-29 12:26:43 +01:00
|
|
|
// The F7 & L4 series calls the peripheral SDMMC rather than SDIO, so provide some
|
2015-10-29 23:56:58 +00:00
|
|
|
// #defines for backwards compatability.
|
|
|
|
|
|
|
|
#define SDIO_CLOCK_EDGE_RISING SDMMC_CLOCK_EDGE_RISING
|
|
|
|
#define SDIO_CLOCK_EDGE_FALLING SDMMC_CLOCK_EDGE_FALLING
|
|
|
|
|
|
|
|
#define SDIO_CLOCK_BYPASS_DISABLE SDMMC_CLOCK_BYPASS_DISABLE
|
|
|
|
#define SDIO_CLOCK_BYPASS_ENABLE SDMMC_CLOCK_BYPASS_ENABLE
|
|
|
|
|
|
|
|
#define SDIO_CLOCK_POWER_SAVE_DISABLE SDMMC_CLOCK_POWER_SAVE_DISABLE
|
|
|
|
#define SDIO_CLOCK_POWER_SAVE_ENABLE SDMMC_CLOCK_POWER_SAVE_ENABLE
|
|
|
|
|
|
|
|
#define SDIO_BUS_WIDE_1B SDMMC_BUS_WIDE_1B
|
|
|
|
#define SDIO_BUS_WIDE_4B SDMMC_BUS_WIDE_4B
|
|
|
|
#define SDIO_BUS_WIDE_8B SDMMC_BUS_WIDE_8B
|
|
|
|
|
|
|
|
#define SDIO_HARDWARE_FLOW_CONTROL_DISABLE SDMMC_HARDWARE_FLOW_CONTROL_DISABLE
|
|
|
|
#define SDIO_HARDWARE_FLOW_CONTROL_ENABLE SDMMC_HARDWARE_FLOW_CONTROL_ENABLE
|
|
|
|
|
2018-03-24 19:23:35 +00:00
|
|
|
#if defined(STM32H7)
|
|
|
|
#define SDIO_TRANSFER_CLK_DIV SDMMC_NSpeed_CLK_DIV
|
|
|
|
#define SDIO_USE_GPDMA 0
|
|
|
|
#else
|
2015-10-29 23:56:58 +00:00
|
|
|
#define SDIO_TRANSFER_CLK_DIV SDMMC_TRANSFER_CLK_DIV
|
2018-03-24 19:23:35 +00:00
|
|
|
#define SDIO_USE_GPDMA 1
|
|
|
|
#endif
|
2015-10-29 23:56:58 +00:00
|
|
|
|
2017-03-30 07:55:21 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
// These are definitions for F4 MCUs so there is a common macro across all MCUs.
|
|
|
|
|
|
|
|
#define SDMMC_CLK_ENABLE() __SDIO_CLK_ENABLE()
|
|
|
|
#define SDMMC_CLK_DISABLE() __SDIO_CLK_DISABLE()
|
|
|
|
#define SDMMC_IRQn SDIO_IRQn
|
2020-10-02 20:44:15 +01:00
|
|
|
#define SDMMC_IRQHandler SDIO_IRQHandler
|
2018-09-11 08:20:34 +01:00
|
|
|
#define SDMMC_DMA dma_SDIO_0
|
2018-03-24 19:23:35 +00:00
|
|
|
#define SDIO_USE_GPDMA 1
|
2021-04-29 03:08:20 +01:00
|
|
|
#define STATIC_AF_SDCARD_CK STATIC_AF_SDIO_CK
|
|
|
|
#define STATIC_AF_SDCARD_CMD STATIC_AF_SDIO_CMD
|
|
|
|
#define STATIC_AF_SDCARD_D0 STATIC_AF_SDIO_D0
|
|
|
|
#define STATIC_AF_SDCARD_D1 STATIC_AF_SDIO_D1
|
|
|
|
#define STATIC_AF_SDCARD_D2 STATIC_AF_SDIO_D2
|
|
|
|
#define STATIC_AF_SDCARD_D3 STATIC_AF_SDIO_D3
|
2021-12-13 13:04:57 +00:00
|
|
|
#define STATIC_AF_SDCARD_D4 STATIC_AF_SDIO_D4
|
|
|
|
#define STATIC_AF_SDCARD_D5 STATIC_AF_SDIO_D5
|
|
|
|
#define STATIC_AF_SDCARD_D6 STATIC_AF_SDIO_D6
|
|
|
|
#define STATIC_AF_SDCARD_D7 STATIC_AF_SDIO_D7
|
2017-03-30 07:55:21 +01:00
|
|
|
|
2015-10-29 23:56:58 +00:00
|
|
|
#endif
|
|
|
|
|
2017-06-15 11:48:21 +01:00
|
|
|
// If no custom SDIO pins defined, use the default ones
|
2021-04-29 03:08:20 +01:00
|
|
|
#ifndef MICROPY_HW_SDCARD_CK
|
|
|
|
#define MICROPY_HW_SDCARD_D0 (pin_C8)
|
|
|
|
#define MICROPY_HW_SDCARD_D1 (pin_C9)
|
|
|
|
#define MICROPY_HW_SDCARD_D2 (pin_C10)
|
|
|
|
#define MICROPY_HW_SDCARD_D3 (pin_C11)
|
|
|
|
#define MICROPY_HW_SDCARD_CK (pin_C12)
|
|
|
|
#define MICROPY_HW_SDCARD_CMD (pin_D2)
|
2017-06-15 11:48:21 +01:00
|
|
|
#endif
|
|
|
|
|
2021-12-13 13:04:57 +00:00
|
|
|
// Define a constant to select the bus width.
|
|
|
|
#if MICROPY_HW_SDCARD_BUS_WIDTH == 4
|
|
|
|
#define SDIO_BUS_WIDE_VALUE SDIO_BUS_WIDE_4B
|
|
|
|
#elif MICROPY_HW_SDCARD_BUS_WIDTH == 8
|
|
|
|
#define SDIO_BUS_WIDE_VALUE SDIO_BUS_WIDE_8B
|
|
|
|
#endif
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#define PYB_SDMMC_FLAG_SD (0x01)
|
|
|
|
#define PYB_SDMMC_FLAG_MMC (0x02)
|
|
|
|
#define PYB_SDMMC_FLAG_ACTIVE (0x04)
|
|
|
|
|
|
|
|
static uint8_t pyb_sdmmc_flags;
|
|
|
|
|
2015-11-16 01:26:43 +00:00
|
|
|
// TODO: I think that as an optimization, we can allocate these dynamically
|
|
|
|
// if an sd card is detected. This will save approx 260 bytes of RAM
|
|
|
|
// when no sdcard was being used.
|
2019-04-01 05:55:04 +01:00
|
|
|
static union {
|
|
|
|
SD_HandleTypeDef sd;
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
MMC_HandleTypeDef mmc;
|
|
|
|
#endif
|
|
|
|
} sdmmc_handle;
|
2015-11-24 15:21:37 +00:00
|
|
|
|
2014-03-17 13:03:41 +00:00
|
|
|
void sdcard_init(void) {
|
2019-04-01 05:55:04 +01:00
|
|
|
// Set SD/MMC to no mode and inactive
|
|
|
|
pyb_sdmmc_flags = 0;
|
2014-03-17 13:03:41 +00:00
|
|
|
|
|
|
|
// configure SD GPIO
|
|
|
|
// we do this here an not in HAL_SD_MspInit because it apparently
|
|
|
|
// makes it more robust to have the pins always pulled high
|
2016-12-22 03:53:03 +00:00
|
|
|
// Note: the mp_hal_pin_config function will configure the GPIO in
|
|
|
|
// fast mode which can do up to 50MHz. This should be plenty for SDIO
|
|
|
|
// which clocks up to 25MHz maximum.
|
2021-04-29 03:08:20 +01:00
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_CK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_CK);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_CMD, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_CMD);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D0);
|
2021-12-13 13:04:57 +00:00
|
|
|
#if MICROPY_HW_SDCARD_BUS_WIDTH >= 4
|
2021-04-29 03:08:20 +01:00
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D1);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D2);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D3);
|
2021-12-13 13:04:57 +00:00
|
|
|
#if MICROPY_HW_SDCARD_BUS_WIDTH == 8
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D4, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D4);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D5, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D5);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D6, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D6);
|
|
|
|
mp_hal_pin_config_alt_static(MICROPY_HW_SDCARD_D7, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, STATIC_AF_SDCARD_D7);
|
|
|
|
#endif
|
2017-03-30 07:55:21 +01:00
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
|
|
|
|
// configure the SD card detect pin
|
|
|
|
// we do this here so we can detect if the SD card is inserted before powering it on
|
2019-10-05 04:27:14 +01:00
|
|
|
#if defined(MICROPY_HW_SDCARD_DETECT_PIN)
|
2018-03-28 06:13:21 +01:00
|
|
|
mp_hal_pin_config(MICROPY_HW_SDCARD_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_SDCARD_DETECT_PULL, 0);
|
2019-10-05 04:27:14 +01:00
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 06:08:14 +00:00
|
|
|
void sdcard_select_sd(void) {
|
|
|
|
pyb_sdmmc_flags |= PYB_SDMMC_FLAG_SD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdcard_select_mmc(void) {
|
|
|
|
pyb_sdmmc_flags |= PYB_SDMMC_FLAG_MMC;
|
|
|
|
}
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
STATIC void sdmmc_msp_init(void) {
|
2014-03-17 13:03:41 +00:00
|
|
|
// enable SDIO clock
|
2017-03-30 07:55:21 +01:00
|
|
|
SDMMC_CLK_ENABLE();
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2018-03-24 19:23:35 +00:00
|
|
|
#if defined(STM32H7)
|
|
|
|
// Reset SDMMC
|
2021-04-29 03:08:20 +01:00
|
|
|
SDMMC_FORCE_RESET();
|
|
|
|
SDMMC_RELEASE_RESET();
|
2018-12-29 14:28:34 +00:00
|
|
|
#endif
|
2018-03-24 19:23:35 +00:00
|
|
|
|
2015-11-16 01:26:43 +00:00
|
|
|
// NVIC configuration for SDIO interrupts
|
2018-05-02 05:41:02 +01:00
|
|
|
NVIC_SetPriority(SDMMC_IRQn, IRQ_PRI_SDIO);
|
2017-03-30 07:55:21 +01:00
|
|
|
HAL_NVIC_EnableIRQ(SDMMC_IRQn);
|
2015-11-16 01:26:43 +00:00
|
|
|
|
|
|
|
// GPIO have already been initialised by sdcard_init
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
void sdmmc_msp_deinit(void) {
|
2017-03-30 07:55:21 +01:00
|
|
|
HAL_NVIC_DisableIRQ(SDMMC_IRQn);
|
|
|
|
SDMMC_CLK_DISABLE();
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
|
|
|
|
sdmmc_msp_init();
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) {
|
|
|
|
sdmmc_msp_deinit();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
void HAL_MMC_MspInit(MMC_HandleTypeDef *hsd) {
|
|
|
|
sdmmc_msp_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_MMC_MspDeInit(MMC_HandleTypeDef *hsd) {
|
|
|
|
sdmmc_msp_deinit();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool sdcard_is_present(void) {
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
2014-03-17 13:03:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
2019-10-05 04:27:14 +01:00
|
|
|
#if defined(MICROPY_HW_SDCARD_DETECT_PIN)
|
2022-03-17 06:07:49 +00:00
|
|
|
return mp_hal_pin_read(MICROPY_HW_SDCARD_DETECT_PIN) == MICROPY_HW_SDCARD_DETECT_PRESENT;
|
2019-10-05 04:27:14 +01:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
2019-04-01 05:55:04 +01:00
|
|
|
}
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
STATIC HAL_StatusTypeDef sdmmc_init_sd(void) {
|
2014-03-17 13:03:41 +00:00
|
|
|
// SD device interface configuration
|
2019-04-01 05:55:04 +01:00
|
|
|
sdmmc_handle.sd.Instance = SDIO;
|
|
|
|
sdmmc_handle.sd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
|
2018-03-24 19:23:35 +00:00
|
|
|
#ifndef STM32H7
|
2019-04-01 05:55:04 +01:00
|
|
|
sdmmc_handle.sd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
|
2018-03-24 19:23:35 +00:00
|
|
|
#endif
|
2019-04-01 05:55:04 +01:00
|
|
|
sdmmc_handle.sd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_ENABLE;
|
|
|
|
sdmmc_handle.sd.Init.BusWide = SDIO_BUS_WIDE_1B;
|
|
|
|
sdmmc_handle.sd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
|
|
|
|
sdmmc_handle.sd.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2014-10-20 00:04:27 +01:00
|
|
|
// init the SD interface, with retry if it's not ready yet
|
2019-04-01 05:55:04 +01:00
|
|
|
HAL_StatusTypeDef status;
|
|
|
|
for (int retry = 10; (status = HAL_SD_Init(&sdmmc_handle.sd)) != HAL_OK; retry--) {
|
2014-10-20 00:04:27 +01:00
|
|
|
if (retry == 0) {
|
2019-04-01 05:55:04 +01:00
|
|
|
return status;
|
2014-10-20 00:04:27 +01:00
|
|
|
}
|
2017-03-02 04:02:57 +00:00
|
|
|
mp_hal_delay_ms(50);
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 13:04:57 +00:00
|
|
|
#if MICROPY_HW_SDCARD_BUS_WIDTH >= 4
|
|
|
|
// configure the SD bus width for 4/8-bit wide operation
|
|
|
|
status = HAL_SD_ConfigWideBusOperation(&sdmmc_handle.sd, SDIO_BUS_WIDE_VALUE);
|
2019-04-01 05:55:04 +01:00
|
|
|
if (status != HAL_OK) {
|
|
|
|
HAL_SD_DeInit(&sdmmc_handle.sd);
|
|
|
|
return status;
|
|
|
|
}
|
2019-08-02 03:54:32 +01:00
|
|
|
#endif
|
2019-04-01 05:55:04 +01:00
|
|
|
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
STATIC HAL_StatusTypeDef sdmmc_init_mmc(void) {
|
|
|
|
// MMC device interface configuration
|
|
|
|
sdmmc_handle.mmc.Instance = SDIO;
|
|
|
|
sdmmc_handle.mmc.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
|
|
|
|
#ifndef STM32H7
|
|
|
|
sdmmc_handle.mmc.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
|
|
|
|
#endif
|
|
|
|
sdmmc_handle.mmc.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_ENABLE;
|
|
|
|
sdmmc_handle.mmc.Init.BusWide = SDIO_BUS_WIDE_1B;
|
|
|
|
sdmmc_handle.mmc.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
|
|
|
|
sdmmc_handle.mmc.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
|
|
|
|
|
|
|
|
// Init the SDIO interface
|
|
|
|
HAL_StatusTypeDef status = HAL_MMC_Init(&sdmmc_handle.mmc);
|
|
|
|
if (status != HAL_OK) {
|
|
|
|
return status;
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 13:07:34 +00:00
|
|
|
#ifdef MICROPY_HW_MMCARD_LOG_BLOCK_NBR
|
|
|
|
// A board can override the number of logical blocks (card capacity) if needed.
|
|
|
|
// This is needed when a card is high capacity because the extended CSD command
|
|
|
|
// is not supported by the current version of the HAL.
|
|
|
|
sdmmc_handle.mmc.MmcCard.LogBlockNbr = MICROPY_HW_MMCARD_LOG_BLOCK_NBR;
|
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2021-12-13 13:04:57 +00:00
|
|
|
#if MICROPY_HW_SDCARD_BUS_WIDTH >= 4
|
|
|
|
// Configure the SDIO bus width for 4/8-bit wide operation
|
2019-04-01 05:55:04 +01:00
|
|
|
#ifdef STM32F7
|
|
|
|
sdmmc_handle.mmc.Init.ClockBypass = SDIO_CLOCK_BYPASS_ENABLE;
|
|
|
|
#endif
|
2021-12-13 13:04:57 +00:00
|
|
|
status = HAL_MMC_ConfigWideBusOperation(&sdmmc_handle.mmc, SDIO_BUS_WIDE_VALUE);
|
2019-04-01 05:55:04 +01:00
|
|
|
if (status != HAL_OK) {
|
|
|
|
HAL_MMC_DeInit(&sdmmc_handle.mmc);
|
|
|
|
return status;
|
|
|
|
}
|
2019-08-02 03:54:32 +01:00
|
|
|
#endif
|
2019-04-01 05:55:04 +01:00
|
|
|
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool sdcard_power_on(void) {
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_ACTIVE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-04 02:10:54 +01:00
|
|
|
#ifdef MICROPY_BOARD_SDCARD_POWER
|
|
|
|
MICROPY_BOARD_SDCARD_POWER
|
|
|
|
#endif
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
HAL_StatusTypeDef status = HAL_ERROR;
|
|
|
|
switch (pyb_sdmmc_flags) {
|
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
case PYB_SDMMC_FLAG_SD:
|
|
|
|
if (sdcard_is_present()) {
|
|
|
|
status = sdmmc_init_sd();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
case PYB_SDMMC_FLAG_MMC:
|
|
|
|
status = sdmmc_init_mmc();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == HAL_OK) {
|
|
|
|
pyb_sdmmc_flags |= PYB_SDMMC_FLAG_ACTIVE;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sdcard_power_off(void) {
|
2019-04-01 05:55:04 +01:00
|
|
|
switch (pyb_sdmmc_flags) {
|
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
case PYB_SDMMC_FLAG_ACTIVE | PYB_SDMMC_FLAG_SD:
|
|
|
|
HAL_SD_DeInit(&sdmmc_handle.sd);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
case PYB_SDMMC_FLAG_ACTIVE | PYB_SDMMC_FLAG_MMC:
|
|
|
|
HAL_MMC_DeInit(&sdmmc_handle.mmc);
|
|
|
|
break;
|
|
|
|
#endif
|
2014-08-25 18:16:52 +01:00
|
|
|
}
|
2019-04-01 05:55:04 +01:00
|
|
|
pyb_sdmmc_flags &= ~PYB_SDMMC_FLAG_ACTIVE;
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t sdcard_get_capacity_in_bytes(void) {
|
2019-04-01 05:55:04 +01:00
|
|
|
switch (pyb_sdmmc_flags) {
|
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
case PYB_SDMMC_FLAG_ACTIVE | PYB_SDMMC_FLAG_SD: {
|
|
|
|
HAL_SD_CardInfoTypeDef cardinfo;
|
|
|
|
HAL_SD_GetCardInfo(&sdmmc_handle.sd, &cardinfo);
|
|
|
|
return (uint64_t)cardinfo.LogBlockNbr * (uint64_t)cardinfo.LogBlockSize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
case PYB_SDMMC_FLAG_ACTIVE | PYB_SDMMC_FLAG_MMC: {
|
|
|
|
HAL_MMC_CardInfoTypeDef cardinfo;
|
|
|
|
HAL_MMC_GetCardInfo(&sdmmc_handle.mmc, &cardinfo);
|
|
|
|
return (uint64_t)cardinfo.LogBlockNbr * (uint64_t)cardinfo.LogBlockSize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void sdmmc_irq_handler(void) {
|
|
|
|
switch (pyb_sdmmc_flags) {
|
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
case PYB_SDMMC_FLAG_ACTIVE | PYB_SDMMC_FLAG_SD:
|
|
|
|
HAL_SD_IRQHandler(&sdmmc_handle.sd);
|
2019-05-23 04:55:19 +01:00
|
|
|
break;
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
case PYB_SDMMC_FLAG_ACTIVE | PYB_SDMMC_FLAG_MMC:
|
|
|
|
HAL_MMC_IRQHandler(&sdmmc_handle.mmc);
|
2019-05-23 04:55:19 +01:00
|
|
|
break;
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 20:44:15 +01:00
|
|
|
void SDMMC_IRQHandler(void) {
|
|
|
|
IRQ_ENTER(SDMMC_IRQn);
|
2019-04-01 05:55:04 +01:00
|
|
|
sdmmc_irq_handler();
|
2020-10-02 20:44:15 +01:00
|
|
|
IRQ_EXIT(SDMMC_IRQn);
|
2015-11-16 01:26:43 +00:00
|
|
|
}
|
2017-03-30 07:55:21 +01:00
|
|
|
|
2018-09-12 07:55:46 +01:00
|
|
|
STATIC void sdcard_reset_periph(void) {
|
|
|
|
// Fully reset the SDMMC peripheral before calling HAL SD DMA functions.
|
|
|
|
// (There could be an outstanding DTIMEOUT event from a previous call and the
|
|
|
|
// HAL function enables IRQs before fully configuring the SDMMC peripheral.)
|
2019-04-01 05:55:04 +01:00
|
|
|
SDIO->DTIMER = 0;
|
|
|
|
SDIO->DLEN = 0;
|
|
|
|
SDIO->DCTRL = 0;
|
|
|
|
SDIO->ICR = SDMMC_STATIC_FLAGS;
|
2018-09-12 07:55:46 +01:00
|
|
|
}
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
STATIC HAL_StatusTypeDef sdcard_wait_finished(uint32_t timeout) {
|
2017-08-29 08:03:28 +01:00
|
|
|
// Wait for HAL driver to be ready (eg for DMA to finish)
|
|
|
|
uint32_t start = HAL_GetTick();
|
2018-02-01 01:20:45 +00:00
|
|
|
for (;;) {
|
|
|
|
// Do an atomic check of the state; WFI will exit even if IRQs are disabled
|
|
|
|
uint32_t irq_state = disable_irq();
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
if (sdmmc_handle.mmc.State != HAL_MMC_STATE_BUSY) {
|
|
|
|
enable_irq(irq_state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (sdmmc_handle.sd.State != HAL_SD_STATE_BUSY) {
|
|
|
|
enable_irq(irq_state);
|
|
|
|
break;
|
|
|
|
}
|
2018-02-01 01:20:45 +00:00
|
|
|
}
|
|
|
|
__WFI();
|
|
|
|
enable_irq(irq_state);
|
2017-08-29 08:03:28 +01:00
|
|
|
if (HAL_GetTick() - start >= timeout) {
|
|
|
|
return HAL_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 01:20:45 +00:00
|
|
|
|
2017-08-29 08:03:28 +01:00
|
|
|
// Wait for SD card to complete the operation
|
|
|
|
for (;;) {
|
2019-04-01 05:55:04 +01:00
|
|
|
uint32_t state;
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
MP_STATIC_ASSERT((uint32_t)HAL_SD_CARD_TRANSFER == (uint32_t)HAL_MMC_CARD_TRANSFER);
|
|
|
|
MP_STATIC_ASSERT((uint32_t)HAL_SD_CARD_SENDING == (uint32_t)HAL_MMC_CARD_SENDING);
|
|
|
|
MP_STATIC_ASSERT((uint32_t)HAL_SD_CARD_RECEIVING == (uint32_t)HAL_MMC_CARD_RECEIVING);
|
|
|
|
MP_STATIC_ASSERT((uint32_t)HAL_SD_CARD_PROGRAMMING == (uint32_t)HAL_MMC_CARD_PROGRAMMING);
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
state = HAL_MMC_GetCardState(&sdmmc_handle.mmc);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
state = HAL_SD_GetCardState(&sdmmc_handle.sd);
|
|
|
|
}
|
|
|
|
|
2017-08-29 08:03:28 +01:00
|
|
|
if (state == HAL_SD_CARD_TRANSFER) {
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
if (!(state == HAL_SD_CARD_SENDING || state == HAL_SD_CARD_RECEIVING || state == HAL_SD_CARD_PROGRAMMING)) {
|
|
|
|
return HAL_ERROR;
|
|
|
|
}
|
|
|
|
if (HAL_GetTick() - start >= timeout) {
|
|
|
|
return HAL_TIMEOUT;
|
|
|
|
}
|
2018-02-01 01:20:45 +00:00
|
|
|
__WFI();
|
2017-08-29 08:03:28 +01:00
|
|
|
}
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-15 23:49:57 +01:00
|
|
|
mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
2014-03-17 13:03:41 +00:00
|
|
|
// check that SD card is initialised
|
2019-04-01 05:55:04 +01:00
|
|
|
if (!(pyb_sdmmc_flags & PYB_SDMMC_FLAG_ACTIVE)) {
|
2017-08-29 08:03:28 +01:00
|
|
|
return HAL_ERROR;
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 08:03:28 +01:00
|
|
|
HAL_StatusTypeDef err = HAL_OK;
|
2014-07-06 17:49:20 +01:00
|
|
|
|
2016-05-13 14:45:40 +01:00
|
|
|
// check that dest pointer is aligned on a 4-byte boundary
|
|
|
|
uint8_t *orig_dest = NULL;
|
|
|
|
uint32_t saved_word;
|
|
|
|
if (((uint32_t)dest & 3) != 0) {
|
|
|
|
// Pointer is not aligned so it needs fixing.
|
|
|
|
// We could allocate a temporary block of RAM (as sdcard_write_blocks
|
|
|
|
// does) but instead we are going to use the dest buffer inplace. We
|
|
|
|
// are going to align the pointer, save the initial word at the aligned
|
|
|
|
// location, read into the aligned memory, move the memory back to the
|
|
|
|
// unaligned location, then restore the initial bytes at the aligned
|
|
|
|
// location. We should have no trouble doing this as those initial
|
|
|
|
// bytes at the aligned location should be able to be changed for the
|
|
|
|
// duration of this function call.
|
|
|
|
orig_dest = dest;
|
|
|
|
dest = (uint8_t *)((uint32_t)dest & ~3);
|
|
|
|
saved_word = *(uint32_t *)dest;
|
|
|
|
}
|
|
|
|
|
2015-11-16 01:26:43 +00:00
|
|
|
if (query_irq() == IRQ_STATE_ENABLED) {
|
2015-12-04 11:39:21 +00:00
|
|
|
// we must disable USB irqs to prevent MSC contention with SD card
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
|
|
|
|
|
2018-03-24 19:23:35 +00:00
|
|
|
#if SDIO_USE_GPDMA
|
2018-09-11 08:23:03 +01:00
|
|
|
DMA_HandleTypeDef sd_dma;
|
2019-04-01 05:55:04 +01:00
|
|
|
dma_init(&sd_dma, &SDMMC_DMA, DMA_PERIPH_TO_MEMORY, &sdmmc_handle);
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
sdmmc_handle.mmc.hdmarx = &sd_dma;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sdmmc_handle.sd.hdmarx = &sd_dma;
|
|
|
|
}
|
2018-03-24 19:23:35 +00:00
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2016-11-23 21:16:14 +00:00
|
|
|
// make sure cache is flushed and invalidated so when DMA updates the RAM
|
|
|
|
// from reading the peripheral the CPU then reads the new data
|
|
|
|
MP_HAL_CLEANINVALIDATE_DCACHE(dest, num_blocks * SDCARD_BLOCK_SIZE);
|
|
|
|
|
2018-09-12 07:55:46 +01:00
|
|
|
sdcard_reset_periph();
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
err = HAL_MMC_ReadBlocks_DMA(&sdmmc_handle.mmc, dest, block_num, num_blocks);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = HAL_SD_ReadBlocks_DMA(&sdmmc_handle.sd, dest, block_num, num_blocks);
|
|
|
|
}
|
2017-08-29 08:03:28 +01:00
|
|
|
if (err == HAL_OK) {
|
2019-04-01 05:55:04 +01:00
|
|
|
err = sdcard_wait_finished(60000);
|
2015-11-16 01:26:43 +00:00
|
|
|
}
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2018-03-24 19:23:35 +00:00
|
|
|
#if SDIO_USE_GPDMA
|
2018-09-11 08:20:34 +01:00
|
|
|
dma_deinit(&SDMMC_DMA);
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
sdmmc_handle.mmc.hdmarx = NULL;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sdmmc_handle.sd.hdmarx = NULL;
|
|
|
|
}
|
2018-03-24 19:23:35 +00:00
|
|
|
#endif
|
2015-12-04 11:39:21 +00:00
|
|
|
|
|
|
|
restore_irq_pri(basepri);
|
2015-11-16 01:26:43 +00:00
|
|
|
} else {
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
err = HAL_MMC_ReadBlocks(&sdmmc_handle.mmc, dest, block_num, num_blocks, 60000);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = HAL_SD_ReadBlocks(&sdmmc_handle.sd, dest, block_num, num_blocks, 60000);
|
|
|
|
}
|
2017-08-29 08:03:28 +01:00
|
|
|
if (err == HAL_OK) {
|
2019-04-01 05:55:04 +01:00
|
|
|
err = sdcard_wait_finished(60000);
|
2017-08-29 08:03:28 +01:00
|
|
|
}
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 14:45:40 +01:00
|
|
|
if (orig_dest != NULL) {
|
|
|
|
// move the read data to the non-aligned position, and restore the initial bytes
|
|
|
|
memmove(orig_dest, dest, num_blocks * SDCARD_BLOCK_SIZE);
|
|
|
|
memcpy(dest, &saved_word, orig_dest - dest);
|
|
|
|
}
|
|
|
|
|
2014-09-15 23:49:57 +01:00
|
|
|
return err;
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:26:43 +00:00
|
|
|
mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
|
2014-03-17 13:03:41 +00:00
|
|
|
// check that SD card is initialised
|
2019-04-01 05:55:04 +01:00
|
|
|
if (!(pyb_sdmmc_flags & PYB_SDMMC_FLAG_ACTIVE)) {
|
2017-08-29 08:03:28 +01:00
|
|
|
return HAL_ERROR;
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 08:03:28 +01:00
|
|
|
HAL_StatusTypeDef err = HAL_OK;
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2016-05-13 14:45:40 +01:00
|
|
|
// check that src pointer is aligned on a 4-byte boundary
|
|
|
|
if (((uint32_t)src & 3) != 0) {
|
|
|
|
// pointer is not aligned, so allocate a temporary block to do the write
|
|
|
|
uint8_t *src_aligned = m_new_maybe(uint8_t, SDCARD_BLOCK_SIZE);
|
|
|
|
if (src_aligned == NULL) {
|
2017-08-29 08:03:28 +01:00
|
|
|
return HAL_ERROR;
|
2016-05-13 14:45:40 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < num_blocks; ++i) {
|
|
|
|
memcpy(src_aligned, src + i * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE);
|
|
|
|
err = sdcard_write_blocks(src_aligned, block_num + i, 1);
|
2017-08-29 08:03:28 +01:00
|
|
|
if (err != HAL_OK) {
|
2016-05-13 14:45:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_del(uint8_t, src_aligned, SDCARD_BLOCK_SIZE);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-11-16 01:26:43 +00:00
|
|
|
if (query_irq() == IRQ_STATE_ENABLED) {
|
2015-12-04 11:39:21 +00:00
|
|
|
// we must disable USB irqs to prevent MSC contention with SD card
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
|
2016-05-06 10:53:25 +01:00
|
|
|
|
2018-03-24 19:23:35 +00:00
|
|
|
#if SDIO_USE_GPDMA
|
2018-09-11 08:23:03 +01:00
|
|
|
DMA_HandleTypeDef sd_dma;
|
2019-04-01 05:55:04 +01:00
|
|
|
dma_init(&sd_dma, &SDMMC_DMA, DMA_MEMORY_TO_PERIPH, &sdmmc_handle);
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
sdmmc_handle.mmc.hdmatx = &sd_dma;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sdmmc_handle.sd.hdmatx = &sd_dma;
|
|
|
|
}
|
2018-03-24 19:23:35 +00:00
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2016-11-23 21:16:14 +00:00
|
|
|
// make sure cache is flushed to RAM so the DMA can read the correct data
|
|
|
|
MP_HAL_CLEAN_DCACHE(src, num_blocks * SDCARD_BLOCK_SIZE);
|
|
|
|
|
2018-09-12 07:55:46 +01:00
|
|
|
sdcard_reset_periph();
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
err = HAL_MMC_WriteBlocks_DMA(&sdmmc_handle.mmc, (uint8_t *)src, block_num, num_blocks);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = HAL_SD_WriteBlocks_DMA(&sdmmc_handle.sd, (uint8_t *)src, block_num, num_blocks);
|
|
|
|
}
|
2017-08-29 08:03:28 +01:00
|
|
|
if (err == HAL_OK) {
|
2019-04-01 05:55:04 +01:00
|
|
|
err = sdcard_wait_finished(60000);
|
2015-11-16 01:26:43 +00:00
|
|
|
}
|
2018-03-24 19:23:35 +00:00
|
|
|
|
|
|
|
#if SDIO_USE_GPDMA
|
2018-09-11 08:20:34 +01:00
|
|
|
dma_deinit(&SDMMC_DMA);
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
sdmmc_handle.mmc.hdmatx = NULL;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sdmmc_handle.sd.hdmatx = NULL;
|
|
|
|
}
|
2018-03-24 19:23:35 +00:00
|
|
|
#endif
|
2015-12-04 11:39:21 +00:00
|
|
|
|
|
|
|
restore_irq_pri(basepri);
|
2015-11-16 01:26:43 +00:00
|
|
|
} else {
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
err = HAL_MMC_WriteBlocks(&sdmmc_handle.mmc, (uint8_t *)src, block_num, num_blocks, 60000);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = HAL_SD_WriteBlocks(&sdmmc_handle.sd, (uint8_t *)src, block_num, num_blocks, 60000);
|
|
|
|
}
|
2017-08-29 08:03:28 +01:00
|
|
|
if (err == HAL_OK) {
|
2019-04-01 05:55:04 +01:00
|
|
|
err = sdcard_wait_finished(60000);
|
2017-08-29 08:03:28 +01:00
|
|
|
}
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:26:43 +00:00
|
|
|
return err;
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 08:22:17 +01:00
|
|
|
// MicroPython bindings
|
2014-09-15 23:49:57 +01:00
|
|
|
//
|
2019-04-01 05:55:04 +01:00
|
|
|
// Expose the SD card or MMC as an object with the block protocol.
|
2016-02-10 16:21:38 +00:00
|
|
|
|
2022-03-17 06:11:44 +00:00
|
|
|
#if !BUILDING_MBOOT
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
// There are singleton SDCard/MMCard objects
|
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
2016-02-10 16:21:38 +00:00
|
|
|
const mp_obj_base_t pyb_sdcard_obj = {&pyb_sdcard_type};
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
const mp_obj_base_t pyb_mmcard_obj = {&pyb_mmcard_type};
|
|
|
|
#endif
|
2016-02-10 16:21:38 +00:00
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
2016-02-10 16:21:38 +00:00
|
|
|
STATIC mp_obj_t pyb_sdcard_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("peripheral used by MMCard"));
|
2019-04-01 05:55:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pyb_sdmmc_flags |= PYB_SDMMC_FLAG_SD;
|
|
|
|
|
2016-02-10 16:21:38 +00:00
|
|
|
// return singleton object
|
2018-07-08 14:25:11 +01:00
|
|
|
return MP_OBJ_FROM_PTR(&pyb_sdcard_obj);
|
2016-02-10 16:21:38 +00:00
|
|
|
}
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
STATIC mp_obj_t pyb_mmcard_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_SD) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("peripheral used by SDCard"));
|
2019-04-01 05:55:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pyb_sdmmc_flags |= PYB_SDMMC_FLAG_MMC;
|
|
|
|
|
|
|
|
// return singleton object
|
|
|
|
return MP_OBJ_FROM_PTR(&pyb_mmcard_obj);
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC mp_obj_t sd_present(mp_obj_t self) {
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(sdcard_is_present());
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(sd_present_obj, sd_present);
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC mp_obj_t sd_power(mp_obj_t self, mp_obj_t state) {
|
2014-03-17 13:03:41 +00:00
|
|
|
bool result;
|
2014-03-30 13:35:08 +01:00
|
|
|
if (mp_obj_is_true(state)) {
|
2014-03-17 13:03:41 +00:00
|
|
|
result = sdcard_power_on();
|
|
|
|
} else {
|
|
|
|
sdcard_power_off();
|
|
|
|
result = true;
|
|
|
|
}
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(result);
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(sd_power_obj, sd_power);
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC mp_obj_t sd_info(mp_obj_t self) {
|
2019-04-01 05:55:04 +01:00
|
|
|
if (!(pyb_sdmmc_flags & PYB_SDMMC_FLAG_ACTIVE)) {
|
2014-09-15 23:49:57 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-04-01 05:55:04 +01:00
|
|
|
uint32_t card_type;
|
|
|
|
uint32_t log_block_nbr;
|
|
|
|
uint32_t log_block_size;
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
|
|
|
if (pyb_sdmmc_flags & PYB_SDMMC_FLAG_MMC) {
|
|
|
|
card_type = sdmmc_handle.mmc.MmcCard.CardType;
|
|
|
|
log_block_nbr = sdmmc_handle.mmc.MmcCard.LogBlockNbr;
|
|
|
|
log_block_size = sdmmc_handle.mmc.MmcCard.LogBlockSize;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
card_type = sdmmc_handle.sd.SdCard.CardType;
|
|
|
|
log_block_nbr = sdmmc_handle.sd.SdCard.LogBlockNbr;
|
|
|
|
log_block_size = sdmmc_handle.sd.SdCard.LogBlockSize;
|
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
// cardinfo.SD_csd and cardinfo.SD_cid have lots of info but we don't use them
|
|
|
|
mp_obj_t tuple[3] = {
|
2019-04-01 05:55:04 +01:00
|
|
|
mp_obj_new_int_from_ull((uint64_t)log_block_nbr * (uint64_t)log_block_size),
|
|
|
|
mp_obj_new_int_from_uint(log_block_size),
|
|
|
|
mp_obj_new_int(card_type),
|
2014-09-15 23:49:57 +01:00
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(3, tuple);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(sd_info_obj, sd_info);
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2016-02-10 16:21:38 +00:00
|
|
|
// now obsolete, kept for backwards compatibility
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) {
|
2014-03-17 13:03:41 +00:00
|
|
|
uint8_t *dest = m_new(uint8_t, SDCARD_BLOCK_SIZE);
|
2014-09-15 23:49:57 +01:00
|
|
|
mp_uint_t ret = sdcard_read_blocks(dest, mp_obj_get_int(block_num), 1);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
2015-02-27 09:34:51 +00:00
|
|
|
m_del(uint8_t, dest, SDCARD_BLOCK_SIZE);
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_Exception, MP_ERROR_TEXT("sdcard_read_blocks failed [%u]"), ret);
|
2014-03-17 13:03:41 +00:00
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
|
2014-03-17 13:03:41 +00:00
|
|
|
return mp_obj_new_bytearray_by_ref(SDCARD_BLOCK_SIZE, dest);
|
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read);
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2016-02-10 16:21:38 +00:00
|
|
|
// now obsolete, kept for backwards compatibility
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC mp_obj_t sd_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t data) {
|
2014-09-09 15:09:07 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-09-15 23:49:57 +01:00
|
|
|
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
|
2014-09-09 15:09:07 +01:00
|
|
|
if (bufinfo.len % SDCARD_BLOCK_SIZE != 0) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("writes must be a multiple of %d bytes"), SDCARD_BLOCK_SIZE);
|
2014-09-09 15:09:07 +01:00
|
|
|
}
|
|
|
|
|
2014-09-15 23:49:57 +01:00
|
|
|
mp_uint_t ret = sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_Exception, MP_ERROR_TEXT("sdcard_write_blocks failed [%u]"), ret);
|
2014-09-09 15:09:07 +01:00
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
|
2014-09-09 15:09:07 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-09-15 23:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(sd_write_obj, sd_write);
|
2014-09-09 15:09:07 +01:00
|
|
|
|
2016-02-10 16:21:38 +00:00
|
|
|
STATIC mp_obj_t pyb_sdcard_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
|
|
|
|
mp_uint_t ret = sdcard_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
|
|
|
|
return mp_obj_new_bool(ret == 0);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sdcard_readblocks_obj, pyb_sdcard_readblocks);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_sdcard_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
|
|
|
mp_uint_t ret = sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
|
|
|
|
return mp_obj_new_bool(ret == 0);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sdcard_writeblocks_obj, pyb_sdcard_writeblocks);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_sdcard_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
|
|
|
|
mp_int_t cmd = mp_obj_get_int(cmd_in);
|
|
|
|
switch (cmd) {
|
2019-10-29 01:25:30 +00:00
|
|
|
case MP_BLOCKDEV_IOCTL_INIT:
|
2016-02-10 16:21:38 +00:00
|
|
|
if (!sdcard_power_on()) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(-1); // error
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // success
|
|
|
|
|
2019-10-29 01:25:30 +00:00
|
|
|
case MP_BLOCKDEV_IOCTL_DEINIT:
|
2016-02-10 16:21:38 +00:00
|
|
|
sdcard_power_off();
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // success
|
|
|
|
|
2019-10-29 01:25:30 +00:00
|
|
|
case MP_BLOCKDEV_IOCTL_SYNC:
|
2016-02-10 16:21:38 +00:00
|
|
|
// nothing to do
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // success
|
|
|
|
|
2019-10-29 01:25:30 +00:00
|
|
|
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
|
2018-04-13 14:26:58 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(sdcard_get_capacity_in_bytes() / SDCARD_BLOCK_SIZE);
|
2016-02-10 16:21:38 +00:00
|
|
|
|
2019-10-29 01:25:30 +00:00
|
|
|
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
|
2016-02-10 16:21:38 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(SDCARD_BLOCK_SIZE);
|
|
|
|
|
|
|
|
default: // unknown command
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(-1); // error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sdcard_ioctl_obj, pyb_sdcard_ioctl);
|
|
|
|
|
2017-05-06 08:03:40 +01:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_sdcard_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_present), MP_ROM_PTR(&sd_present_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_power), MP_ROM_PTR(&sd_power_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&sd_info_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&sd_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&sd_write_obj) },
|
2016-02-10 16:21:38 +00:00
|
|
|
// block device protocol
|
2017-05-06 08:03:40 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&pyb_sdcard_readblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&pyb_sdcard_writeblocks_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&pyb_sdcard_ioctl_obj) },
|
2014-03-17 13:03:41 +00:00
|
|
|
};
|
|
|
|
|
2016-02-10 16:21:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_sdcard_locals_dict, pyb_sdcard_locals_dict_table);
|
2014-03-26 21:47:19 +00:00
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#if MICROPY_HW_ENABLE_SDCARD
|
2021-07-14 05:38:38 +01:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_sdcard_type,
|
|
|
|
MP_QSTR_SDCard,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 15:31:23 +01:00
|
|
|
make_new, pyb_sdcard_make_new,
|
2022-06-24 07:27:46 +01:00
|
|
|
locals_dict, &pyb_sdcard_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_MMCARD
|
2021-07-14 05:38:38 +01:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_mmcard_type,
|
|
|
|
MP_QSTR_MMCard,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 15:31:23 +01:00
|
|
|
make_new, pyb_mmcard_make_new,
|
2022-06-24 07:27:46 +01:00
|
|
|
locals_dict, &pyb_sdcard_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif
|
2014-03-17 13:03:41 +00:00
|
|
|
|
2017-01-31 01:18:08 +00:00
|
|
|
void sdcard_init_vfs(fs_user_mount_t *vfs, int part) {
|
2019-04-01 05:55:04 +01:00
|
|
|
pyb_sdmmc_flags = (pyb_sdmmc_flags & PYB_SDMMC_FLAG_ACTIVE) | PYB_SDMMC_FLAG_SD; // force SD mode
|
2017-01-27 12:11:59 +00:00
|
|
|
vfs->base.type = &mp_fat_vfs_type;
|
2019-09-07 05:03:41 +01:00
|
|
|
vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NATIVE | MP_BLOCKDEV_FLAG_HAVE_IOCTL;
|
2017-01-27 12:11:59 +00:00
|
|
|
vfs->fatfs.drv = vfs;
|
2020-10-09 21:02:46 +01:00
|
|
|
#if MICROPY_FATFS_MULTI_PARTITION
|
2017-01-31 01:18:08 +00:00
|
|
|
vfs->fatfs.part = part;
|
2020-10-09 21:02:46 +01:00
|
|
|
#endif
|
2019-09-07 05:03:41 +01:00
|
|
|
vfs->blockdev.readblocks[0] = MP_OBJ_FROM_PTR(&pyb_sdcard_readblocks_obj);
|
|
|
|
vfs->blockdev.readblocks[1] = MP_OBJ_FROM_PTR(&pyb_sdcard_obj);
|
|
|
|
vfs->blockdev.readblocks[2] = MP_OBJ_FROM_PTR(sdcard_read_blocks); // native version
|
|
|
|
vfs->blockdev.writeblocks[0] = MP_OBJ_FROM_PTR(&pyb_sdcard_writeblocks_obj);
|
|
|
|
vfs->blockdev.writeblocks[1] = MP_OBJ_FROM_PTR(&pyb_sdcard_obj);
|
|
|
|
vfs->blockdev.writeblocks[2] = MP_OBJ_FROM_PTR(sdcard_write_blocks); // native version
|
|
|
|
vfs->blockdev.u.ioctl[0] = MP_OBJ_FROM_PTR(&pyb_sdcard_ioctl_obj);
|
|
|
|
vfs->blockdev.u.ioctl[1] = MP_OBJ_FROM_PTR(&pyb_sdcard_obj);
|
2016-02-10 16:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 06:11:44 +00:00
|
|
|
#endif // !BUILDING_MBOOT
|
|
|
|
|
2019-04-01 05:55:04 +01:00
|
|
|
#endif // MICROPY_HW_ENABLE_SDCARD || MICROPY_HW_ENABLE_MMCARD
|