2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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 09:45:40 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
2017-03-01 23:32:32 -05:00
|
|
|
#include "py/mphal.h"
|
2017-01-27 07:11:59 -05:00
|
|
|
#include "lib/oofatfs/ff.h"
|
|
|
|
#include "extmod/vfs_fat.h"
|
2016-02-10 11:21:38 -05:00
|
|
|
|
2014-03-17 09:03:41 -04:00
|
|
|
#include "sdcard.h"
|
2014-03-30 07:30:35 -04:00
|
|
|
#include "pin.h"
|
2014-04-17 13:03:27 -04:00
|
|
|
#include "genhdr/pins.h"
|
2014-09-09 10:09:07 -04:00
|
|
|
#include "bufhelper.h"
|
2015-11-15 20:26:43 -05:00
|
|
|
#include "dma.h"
|
|
|
|
#include "irq.h"
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2014-03-30 07:30:35 -04:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-06-29 07:26:43 -04:00
|
|
|
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
|
2015-10-29 19:56:58 -04:00
|
|
|
|
2016-06-29 07:26:43 -04:00
|
|
|
// The F7 & L4 series calls the peripheral SDMMC rather than SDIO, so provide some
|
2015-10-29 19:56:58 -04:00
|
|
|
// #defines for backwards compatability.
|
|
|
|
|
|
|
|
#define SDIO SDMMC1
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
#define SDIO_TRANSFER_CLK_DIV SDMMC_TRANSFER_CLK_DIV
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-11-24 10:21:37 -05:00
|
|
|
// TODO: Since SDIO is fundamentally half-duplex, we really only need to
|
|
|
|
// tie up one DMA channel. However, the HAL DMA API doesn't
|
|
|
|
// seem to provide a convenient way to change the direction. I believe that
|
|
|
|
// its as simple as changing the CR register and the Init.Direction field
|
|
|
|
// and make DMA_SetConfig public.
|
|
|
|
|
2015-11-15 20:26:43 -05: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.
|
2014-03-30 07:30:35 -04:00
|
|
|
static SD_HandleTypeDef sd_handle;
|
2015-11-15 20:26:43 -05:00
|
|
|
static DMA_HandleTypeDef sd_rx_dma, sd_tx_dma;
|
2015-11-24 10:21:37 -05:00
|
|
|
|
2014-03-17 09:03:41 -04:00
|
|
|
void sdcard_init(void) {
|
|
|
|
// invalidate the sd_handle
|
|
|
|
sd_handle.Instance = NULL;
|
|
|
|
|
|
|
|
// 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-21 22:53:03 -05: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.
|
|
|
|
mp_hal_pin_config(&pin_C8, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
|
|
|
|
mp_hal_pin_config(&pin_C9, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
|
|
|
|
mp_hal_pin_config(&pin_C10, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
|
|
|
|
mp_hal_pin_config(&pin_C11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
|
|
|
|
mp_hal_pin_config(&pin_C12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
|
|
|
|
mp_hal_pin_config(&pin_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
|
2014-03-17 09:03:41 -04: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
|
2016-12-21 22:53:03 -05:00
|
|
|
mp_hal_pin_config(&MICROPY_HW_SDCARD_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_SDCARD_DETECT_PULL, 0);
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
|
|
|
|
// enable SDIO clock
|
|
|
|
__SDIO_CLK_ENABLE();
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
// NVIC configuration for SDIO interrupts
|
|
|
|
HAL_NVIC_SetPriority(SDIO_IRQn, IRQ_PRI_SDIO, IRQ_SUBPRI_SDIO);
|
|
|
|
HAL_NVIC_EnableIRQ(SDIO_IRQn);
|
|
|
|
|
|
|
|
// GPIO have already been initialised by sdcard_init
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) {
|
2015-11-15 20:26:43 -05:00
|
|
|
HAL_NVIC_DisableIRQ(SDIO_IRQn);
|
2014-03-17 09:03:41 -04:00
|
|
|
__SDIO_CLK_DISABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sdcard_is_present(void) {
|
2014-03-30 07:30:35 -04:00
|
|
|
return HAL_GPIO_ReadPin(MICROPY_HW_SDCARD_DETECT_PIN.gpio, MICROPY_HW_SDCARD_DETECT_PIN.pin_mask) == MICROPY_HW_SDCARD_DETECT_PRESENT;
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool sdcard_power_on(void) {
|
|
|
|
if (!sdcard_is_present()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-25 13:16:52 -04:00
|
|
|
if (sd_handle.Instance) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-17 09:03:41 -04:00
|
|
|
|
|
|
|
// SD device interface configuration
|
|
|
|
sd_handle.Instance = SDIO;
|
|
|
|
sd_handle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
|
|
|
|
sd_handle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
|
2016-10-09 23:23:21 -04:00
|
|
|
sd_handle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_ENABLE;
|
2014-03-17 09:03:41 -04:00
|
|
|
sd_handle.Init.BusWide = SDIO_BUS_WIDE_1B;
|
|
|
|
sd_handle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
|
|
|
|
sd_handle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
|
|
|
|
|
2014-10-19 19:04:27 -04:00
|
|
|
// init the SD interface, with retry if it's not ready yet
|
2014-03-17 09:03:41 -04:00
|
|
|
HAL_SD_CardInfoTypedef cardinfo;
|
2014-10-19 19:04:27 -04:00
|
|
|
for (int retry = 10; HAL_SD_Init(&sd_handle, &cardinfo) != SD_OK; retry--) {
|
|
|
|
if (retry == 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(50);
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// configure the SD bus width for wide operation
|
|
|
|
if (HAL_SD_WideBusOperation_Config(&sd_handle, SDIO_BUS_WIDE_4B) != SD_OK) {
|
|
|
|
HAL_SD_DeInit(&sd_handle);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
sd_handle.Instance = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdcard_power_off(void) {
|
2014-08-25 13:16:52 -04:00
|
|
|
if (!sd_handle.Instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HAL_SD_DeInit(&sd_handle);
|
2014-03-17 09:03:41 -04:00
|
|
|
sd_handle.Instance = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t sdcard_get_capacity_in_bytes(void) {
|
|
|
|
if (sd_handle.Instance == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
HAL_SD_CardInfoTypedef cardinfo;
|
|
|
|
HAL_SD_Get_CardInfo(&sd_handle, &cardinfo);
|
|
|
|
return cardinfo.CardCapacity;
|
|
|
|
}
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
void SDIO_IRQHandler(void) {
|
2015-06-10 09:25:54 -04:00
|
|
|
IRQ_ENTER(SDIO_IRQn);
|
2015-11-15 20:26:43 -05:00
|
|
|
HAL_SD_IRQHandler(&sd_handle);
|
2015-06-10 09:25:54 -04:00
|
|
|
IRQ_EXIT(SDIO_IRQn);
|
2015-11-15 20:26:43 -05:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:49:57 -04:00
|
|
|
mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
2014-03-17 09:03:41 -04:00
|
|
|
// check that SD card is initialised
|
|
|
|
if (sd_handle.Instance == NULL) {
|
2014-09-15 18:49:57 -04:00
|
|
|
return SD_ERROR;
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
HAL_SD_ErrorTypedef err = SD_OK;
|
2014-07-06 12:49:20 -04:00
|
|
|
|
2016-05-13 09:45:40 -04: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-15 20:26:43 -05:00
|
|
|
if (query_irq() == IRQ_STATE_ENABLED) {
|
2015-12-04 06:39:21 -05:00
|
|
|
// we must disable USB irqs to prevent MSC contention with SD card
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
|
|
|
|
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_init(&sd_rx_dma, &dma_SDIO_0_RX, &sd_handle);
|
2015-11-15 20:26:43 -05:00
|
|
|
sd_handle.hdmarx = &sd_rx_dma;
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-11-23 16:16:14 -05: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);
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
err = HAL_SD_ReadBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)dest, block_num, SDCARD_BLOCK_SIZE, num_blocks);
|
|
|
|
if (err == SD_OK) {
|
|
|
|
// wait for DMA transfer to finish, with a large timeout
|
|
|
|
err = HAL_SD_CheckReadOperation(&sd_handle, 100000000);
|
|
|
|
}
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(&dma_SDIO_0_RX);
|
2015-11-15 20:26:43 -05:00
|
|
|
sd_handle.hdmarx = NULL;
|
2015-12-04 06:39:21 -05:00
|
|
|
|
|
|
|
restore_irq_pri(basepri);
|
2015-11-15 20:26:43 -05:00
|
|
|
} else {
|
|
|
|
err = HAL_SD_ReadBlocks_BlockNumber(&sd_handle, (uint32_t*)dest, block_num, SDCARD_BLOCK_SIZE, num_blocks);
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
2016-05-13 09:45:40 -04: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 18:49:57 -04:00
|
|
|
return err;
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
|
2014-03-17 09:03:41 -04:00
|
|
|
// check that SD card is initialised
|
|
|
|
if (sd_handle.Instance == NULL) {
|
2015-11-15 20:26:43 -05:00
|
|
|
return SD_ERROR;
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
HAL_SD_ErrorTypedef err = SD_OK;
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-05-13 09:45:40 -04: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) {
|
|
|
|
return SD_ERROR;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
if (err != SD_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_del(uint8_t, src_aligned, SDCARD_BLOCK_SIZE);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
if (query_irq() == IRQ_STATE_ENABLED) {
|
2015-12-04 06:39:21 -05: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 05:53:25 -04:00
|
|
|
|
|
|
|
dma_init(&sd_tx_dma, &dma_SDIO_0_TX, &sd_handle);
|
2015-11-15 20:26:43 -05:00
|
|
|
sd_handle.hdmatx = &sd_tx_dma;
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-11-23 16:16:14 -05: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);
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
err = HAL_SD_WriteBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks);
|
|
|
|
if (err == SD_OK) {
|
|
|
|
// wait for DMA transfer to finish, with a large timeout
|
|
|
|
err = HAL_SD_CheckWriteOperation(&sd_handle, 100000000);
|
|
|
|
}
|
2016-03-22 06:28:35 -04:00
|
|
|
dma_deinit(&dma_SDIO_0_TX);
|
2015-11-15 20:26:43 -05:00
|
|
|
sd_handle.hdmatx = NULL;
|
2015-12-04 06:39:21 -05:00
|
|
|
|
|
|
|
restore_irq_pri(basepri);
|
2015-11-15 20:26:43 -05:00
|
|
|
} else {
|
|
|
|
err = HAL_SD_WriteBlocks_BlockNumber(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks);
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
2015-11-15 20:26:43 -05:00
|
|
|
return err;
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Micro Python bindings
|
2014-09-15 18:49:57 -04:00
|
|
|
//
|
2016-02-10 11:21:38 -05:00
|
|
|
// Expose the SD card as an object with the block protocol.
|
|
|
|
|
|
|
|
// there is a singleton SDCard object
|
|
|
|
const mp_obj_base_t pyb_sdcard_obj = {&pyb_sdcard_type};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// return singleton object
|
|
|
|
return (mp_obj_t)&pyb_sdcard_obj;
|
|
|
|
}
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC mp_obj_t sd_present(mp_obj_t self) {
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(sdcard_is_present());
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(sd_present_obj, sd_present);
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC mp_obj_t sd_power(mp_obj_t self, mp_obj_t state) {
|
2014-03-17 09:03:41 -04:00
|
|
|
bool result;
|
2014-03-30 08:35:08 -04:00
|
|
|
if (mp_obj_is_true(state)) {
|
2014-03-17 09:03:41 -04:00
|
|
|
result = sdcard_power_on();
|
|
|
|
} else {
|
|
|
|
sdcard_power_off();
|
|
|
|
result = true;
|
|
|
|
}
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(result);
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(sd_power_obj, sd_power);
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC mp_obj_t sd_info(mp_obj_t self) {
|
|
|
|
if (sd_handle.Instance == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
HAL_SD_CardInfoTypedef cardinfo;
|
|
|
|
HAL_SD_Get_CardInfo(&sd_handle, &cardinfo);
|
|
|
|
// cardinfo.SD_csd and cardinfo.SD_cid have lots of info but we don't use them
|
|
|
|
mp_obj_t tuple[3] = {
|
|
|
|
mp_obj_new_int_from_ull(cardinfo.CardCapacity),
|
|
|
|
mp_obj_new_int_from_uint(cardinfo.CardBlockSize),
|
|
|
|
mp_obj_new_int(cardinfo.CardType),
|
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(3, tuple);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(sd_info_obj, sd_info);
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-02-10 11:21:38 -05:00
|
|
|
// now obsolete, kept for backwards compatibility
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) {
|
2014-03-17 09:03:41 -04:00
|
|
|
uint8_t *dest = m_new(uint8_t, SDCARD_BLOCK_SIZE);
|
2014-09-15 18:49:57 -04:00
|
|
|
mp_uint_t ret = sdcard_read_blocks(dest, mp_obj_get_int(block_num), 1);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
2015-02-27 04:34:51 -05:00
|
|
|
m_del(uint8_t, dest, SDCARD_BLOCK_SIZE);
|
2014-09-15 18:49:57 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "sdcard_read_blocks failed [%u]", ret));
|
2014-03-17 09:03:41 -04:00
|
|
|
}
|
2014-09-15 18:49:57 -04:00
|
|
|
|
2014-03-17 09:03:41 -04:00
|
|
|
return mp_obj_new_bytearray_by_ref(SDCARD_BLOCK_SIZE, dest);
|
|
|
|
}
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read);
|
2014-03-17 09:03:41 -04:00
|
|
|
|
2016-02-10 11:21:38 -05:00
|
|
|
// now obsolete, kept for backwards compatibility
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC mp_obj_t sd_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t data) {
|
2014-09-09 10:09:07 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-09-15 18:49:57 -04:00
|
|
|
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
|
2014-09-09 10:09:07 -04:00
|
|
|
if (bufinfo.len % SDCARD_BLOCK_SIZE != 0) {
|
2014-09-15 18:49:57 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "writes must be a multiple of %d bytes", SDCARD_BLOCK_SIZE));
|
2014-09-09 10:09:07 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:49:57 -04:00
|
|
|
mp_uint_t ret = sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "sdcard_write_blocks failed [%u]", ret));
|
2014-09-09 10:09:07 -04:00
|
|
|
}
|
2014-09-15 18:49:57 -04:00
|
|
|
|
2014-09-09 10:09:07 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-09-15 18:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(sd_write_obj, sd_write);
|
2014-09-09 10:09:07 -04:00
|
|
|
|
2016-02-10 11:21:38 -05: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) {
|
2016-02-10 18:36:19 -05:00
|
|
|
case BP_IOCTL_INIT:
|
2016-02-10 11:21:38 -05:00
|
|
|
if (!sdcard_power_on()) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(-1); // error
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // success
|
|
|
|
|
2016-02-10 18:36:19 -05:00
|
|
|
case BP_IOCTL_DEINIT:
|
2016-02-10 11:21:38 -05:00
|
|
|
sdcard_power_off();
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // success
|
|
|
|
|
2016-02-10 18:36:19 -05:00
|
|
|
case BP_IOCTL_SYNC:
|
2016-02-10 11:21:38 -05:00
|
|
|
// nothing to do
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // success
|
|
|
|
|
2016-02-10 18:36:19 -05:00
|
|
|
case BP_IOCTL_SEC_COUNT:
|
2016-02-10 11:21:38 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(0); // TODO
|
|
|
|
|
2016-02-10 18:36:19 -05:00
|
|
|
case BP_IOCTL_SEC_SIZE:
|
2016-02-10 11:21:38 -05: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);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t pyb_sdcard_locals_dict_table[] = {
|
2014-03-26 17:47:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_present), (mp_obj_t)&sd_present_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_power), (mp_obj_t)&sd_power_obj },
|
2014-09-15 18:49:57 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&sd_info_obj },
|
2014-03-26 17:47:19 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&sd_read_obj },
|
2014-09-09 10:09:07 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&sd_write_obj },
|
2016-02-10 11:21:38 -05:00
|
|
|
// block device protocol
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_sdcard_readblocks_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_sdcard_writeblocks_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_sdcard_ioctl_obj },
|
2014-03-17 09:03:41 -04:00
|
|
|
};
|
|
|
|
|
2016-02-10 11:21:38 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_sdcard_locals_dict, pyb_sdcard_locals_dict_table);
|
2014-03-26 17:47:19 -04:00
|
|
|
|
2016-02-10 11:21:38 -05:00
|
|
|
const mp_obj_type_t pyb_sdcard_type = {
|
2014-03-17 09:03:41 -04:00
|
|
|
{ &mp_type_type },
|
2016-02-10 11:21:38 -05:00
|
|
|
.name = MP_QSTR_SDCard,
|
|
|
|
.make_new = pyb_sdcard_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&pyb_sdcard_locals_dict,
|
2014-03-17 09:03:41 -04:00
|
|
|
};
|
|
|
|
|
2017-01-30 20:18:08 -05:00
|
|
|
void sdcard_init_vfs(fs_user_mount_t *vfs, int part) {
|
2017-01-27 07:11:59 -05:00
|
|
|
vfs->base.type = &mp_fat_vfs_type;
|
2016-02-10 11:32:57 -05:00
|
|
|
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
|
2017-01-27 07:11:59 -05:00
|
|
|
vfs->fatfs.drv = vfs;
|
2017-01-30 20:18:08 -05:00
|
|
|
vfs->fatfs.part = part;
|
2016-02-10 11:32:57 -05:00
|
|
|
vfs->readblocks[0] = (mp_obj_t)&pyb_sdcard_readblocks_obj;
|
|
|
|
vfs->readblocks[1] = (mp_obj_t)&pyb_sdcard_obj;
|
|
|
|
vfs->readblocks[2] = (mp_obj_t)sdcard_read_blocks; // native version
|
|
|
|
vfs->writeblocks[0] = (mp_obj_t)&pyb_sdcard_writeblocks_obj;
|
|
|
|
vfs->writeblocks[1] = (mp_obj_t)&pyb_sdcard_obj;
|
|
|
|
vfs->writeblocks[2] = (mp_obj_t)sdcard_write_blocks; // native version
|
|
|
|
vfs->u.ioctl[0] = (mp_obj_t)&pyb_sdcard_ioctl_obj;
|
|
|
|
vfs->u.ioctl[1] = (mp_obj_t)&pyb_sdcard_obj;
|
|
|
|
}
|
|
|
|
|
2014-03-30 07:30:35 -04:00
|
|
|
#endif // MICROPY_HW_HAS_SDCARD
|