Commit fb431bf5 authored by Damien George's avatar Damien George
Browse files

stmhal: Add SD card support.

Just low-level read/write support.  No filesystem yet.
parent c44115f8
......@@ -78,6 +78,7 @@ SRC_C = \
exti.c \
usrsw.c \
rtc.c \
sdcard.c \
# lcd.c \
# servo.c \
......@@ -86,7 +87,6 @@ SRC_C = \
# accel.c \
# timer.c \
# audio.c \
# sdcard.c \
# i2c.c \
# adc.c \
# file.c \
......@@ -106,9 +106,11 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
stm32f4xx_hal_rcc_ex.c \
stm32f4xx_hal_rtc.c \
stm32f4xx_hal_rtc_ex.c \
stm32f4xx_hal_sd.c \
stm32f4xx_hal_tim.c \
stm32f4xx_hal_tim_ex.c \
stm32f4xx_hal_uart.c \
stm32f4xx_ll_sdmmc.c \
stm32f4xx_ll_usb.c \
)
......
......@@ -29,13 +29,13 @@
#include "usrsw.h"
#include "usb.h"
#include "rtc.h"
#include "sdcard.h"
#if 0
#include "ff.h"
#include "lexerfatfs.h"
#include "servo.h"
#include "lcd.h"
#include "storage.h"
#include "sdcard.h"
#include "accel.h"
#include "timer.h"
#include "pybwlan.h"
......@@ -194,23 +194,6 @@ int main(void) {
#endif
#if 0
#if MICROPY_HW_HAS_SDCARD
{
// configure SDIO pins to be high to start with (apparently makes it more robust)
// FIXME this is not making them high, it just makes them outputs...
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// Configure PD.02 CMD line
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
#endif
#if defined(NETDUINO_PLUS_2)
{
GPIO_InitTypeDef GPIO_InitStructure;
......@@ -245,11 +228,11 @@ int main(void) {
rtc_init();
#endif
#if 0
// more sub-system init
#if MICROPY_HW_HAS_SDCARD
sdcard_init();
#endif
#if 0
storage_init();
#endif
......
......@@ -22,11 +22,11 @@
#include "usrsw.h"
#include "rtc.h"
#include "usart.h"
#include "sdcard.h"
#if 0
#include "servo.h"
#include "storage.h"
#include "usb.h"
#include "sdcard.h"
#include "accel.h"
#include "i2c.h"
#include "adc.h"
......@@ -258,11 +258,11 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_switch), (mp_obj_t)&pyb_switch_obj },
#endif
#if 0
#if MICROPY_HW_HAS_SDCARD
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj },
#endif
#if 0
#if MICROPY_HW_HAS_MMA7660
{ MP_OBJ_NEW_QSTR(MP_QSTR_accel), (mp_obj_t)&pyb_accel_read_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_accel_read), (mp_obj_t)&pyb_accel_read_all_obj },
......
// TODO make it work with DMA
#include <stm32f4xx_hal.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "sdcard.h"
static SD_HandleTypeDef sd_handle;
#define SDCARD_DETECT_GPIO_PORT (GPIOA)
#define SDCARD_DETECT_GPIO_PIN (GPIO_PIN_8)
#define SDCARD_DETECT_GPIO_PULL (GPIO_PULLUP)
#define SDCARD_DETECT_GPIO_PRESENT (GPIO_PIN_RESET)
#define __SDCARD_DETECT_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
void sdcard_init(void) {
GPIO_InitTypeDef GPIO_Init_Structure;
// 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
GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
GPIO_Init_Structure.Pull = GPIO_PULLUP;
GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
GPIO_Init_Structure.Alternate = GPIO_AF12_SDIO;
GPIO_Init_Structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOC, &GPIO_Init_Structure);
GPIO_Init_Structure.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure);
// configure the SD card detect pin
// we do this here so we can detect if the SD card is inserted before powering it on
GPIO_Init_Structure.Mode = GPIO_MODE_INPUT;
GPIO_Init_Structure.Pull = SDCARD_DETECT_GPIO_PULL;
GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
GPIO_Init_Structure.Pin = SDCARD_DETECT_GPIO_PIN;
HAL_GPIO_Init(SDCARD_DETECT_GPIO_PORT, &GPIO_Init_Structure);
}
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
// enable SDIO clock
__SDIO_CLK_ENABLE();
// GPIO have already been initialised by sdcard_init
// interrupts are not used at the moment
// they are needed only for DMA transfer (I think...)
}
void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) {
__SDIO_CLK_DISABLE();
}
bool sdcard_is_present(void) {
return HAL_GPIO_ReadPin(SDCARD_DETECT_GPIO_PORT, SDCARD_DETECT_GPIO_PIN) == SDCARD_DETECT_GPIO_PRESENT;
}
bool sdcard_power_on(void) {
if (!sdcard_is_present()) {
return false;
}
// SD device interface configuration
sd_handle.Instance = SDIO;
sd_handle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
sd_handle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
sd_handle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
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;
// init the SD interface
HAL_SD_CardInfoTypedef cardinfo;
if (HAL_SD_Init(&sd_handle, &cardinfo) != SD_OK) {
goto error;
}
// 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) {
HAL_SD_DeInit(&sd_handle);
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;
}
bool sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
// check that dest pointer is aligned on a 4-byte boundary
if (((uint32_t)dest & 3) != 0) {
return false;
}
// check that SD card is initialised
if (sd_handle.Instance == NULL) {
return false;
}
if (HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks) != SD_OK) {
return false;
}
return true;
}
bool sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
// check that src pointer is aligned on a 4-byte boundary
if (((uint32_t)src & 3) != 0) {
return false;
}
// check that SD card is initialised
if (sd_handle.Instance == NULL) {
return false;
}
if (HAL_SD_WriteBlocks(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks) != SD_OK) {
return false;
}
return true;
}
#if 0
DMA not implemented
bool sdcard_read_blocks_dma(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
// check that dest pointer is aligned on a 4-byte boundary
if (((uint32_t)dest & 3) != 0) {
return false;
}
// check that SD card is initialised
if (sd_handle.Instance == NULL) {
return false;
}
// do the read
if (HAL_SD_ReadBlocks_DMA(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE) != SD_OK) {
return false;
}
// wait for DMA transfer to finish, with a large timeout
if (HAL_SD_CheckReadOperation(&sd_handle, 100000000) != SD_OK) {
return false;
}
return true;
}
bool sdcard_write_blocks_dma(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
// check that src pointer is aligned on a 4-byte boundary
if (((uint32_t)src & 3) != 0) {
return false;
}
// check that SD card is initialised
if (sd_handle.Instance == NULL) {
return false;
}
SD_Error status;
status = HAL_SD_WriteBlock_DMA(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks);
if (status != SD_OK) {
return false;
}
// wait for DMA transfer to finish, with a large timeout
status = HAL_SD_CheckWriteOperation(&sd_handle, 100000000);
if (status != SD_OK) {
return false;
}
return true;
}
#endif
/******************************************************************************/
// Micro Python bindings
static mp_obj_t sd_present(mp_obj_t self) {
return MP_BOOL(sdcard_is_present());
}
static MP_DEFINE_CONST_FUN_OBJ_1(sd_present_obj, sd_present);
static mp_obj_t sd_power(mp_obj_t self, mp_obj_t state) {
bool result;
if (rt_is_true(state)) {
result = sdcard_power_on();
} else {
sdcard_power_off();
result = true;
}
return MP_BOOL(result);
}
static MP_DEFINE_CONST_FUN_OBJ_2(sd_power_obj, sd_power);
static mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) {
uint8_t *dest = m_new(uint8_t, SDCARD_BLOCK_SIZE);
if (!sdcard_read_blocks(dest, mp_obj_get_int(block_num), 1)) {
m_free(dest, SDCARD_BLOCK_SIZE);
return mp_const_none;
}
return mp_obj_new_bytearray_by_ref(SDCARD_BLOCK_SIZE, dest);
}
static MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read);
static const mp_method_t sdcard_methods[] = {
{ "present", &sd_present_obj },
{ "power", &sd_power_obj },
{ "read", &sd_read_obj },
{ NULL, NULL },
};
static const mp_obj_type_t sdcard_type = {
{ &mp_type_type },
.name = MP_QSTR_SDcard,
.methods = sdcard_methods,
};
const mp_obj_base_t pyb_sdcard_obj = {&sdcard_type};
// this is a fixed size and should not be changed
#define SDCARD_BLOCK_SIZE (512)
void sdcard_init(void);
bool sdcard_is_present(void);
bool sdcard_power_on(void);
void sdcard_power_off(void);
uint64_t sdcard_get_capacity_in_bytes(void);
bool sdcard_read_block(uint8_t *dest, uint32_t block_num);
bool sdcard_write_block(const uint8_t *src, uint32_t block_num);
extern const struct _mp_obj_base_t pyb_sdcard_obj;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment