|
|
@ -27,14 +27,37 @@
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
|
|
//#include "SdMscDriver.h"
|
|
|
|
#if NONE(STM32F103xE, STM32F103xG, STM32F4xx, STM32F7xx)
|
|
|
|
|
|
|
|
#error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//#include "usbd_msc_bot.h"
|
|
|
|
#ifdef USBD_USE_CDC_COMPOSITE
|
|
|
|
//#include "usbd_msc_scsi.h"
|
|
|
|
|
|
|
|
//#include "usbd_msc_composite.h"
|
|
|
|
|
|
|
|
//#include "usbd_msc_cdc_composite.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#include "usbd_msc_data.h"
|
|
|
|
// use USB drivers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" { int8_t SD_MSC_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
|
|
|
|
|
|
|
int8_t SD_MSC_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
|
|
|
|
|
|
|
extern SD_HandleTypeDef hsd;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SDIO_Init() {
|
|
|
|
|
|
|
|
if (hsd.State == HAL_SD_STATE_READY) return 1; // return passing status
|
|
|
|
|
|
|
|
return 0; // return failing status
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SDIO_ReadBlock(uint32_t block, uint8_t *src) {
|
|
|
|
|
|
|
|
int8_t status = SD_MSC_Read(0, (uint8_t*)src, block, 1); // read one 512 byte block
|
|
|
|
|
|
|
|
return (bool) status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
|
|
|
|
|
|
|
int8_t status = SD_MSC_Write(0, (uint8_t*)src, block, 1); // write one 512 byte block
|
|
|
|
|
|
|
|
return (bool) status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#else // !USBD_USE_CDC_COMPOSITE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// use local drivers
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(STM32F103xE) || defined(STM32F103xG)
|
|
|
|
#if defined(STM32F103xE) || defined(STM32F103xG)
|
|
|
|
#include <stm32f1xx_hal_rcc_ex.h>
|
|
|
|
#include <stm32f1xx_hal_rcc_ex.h>
|
|
|
@ -55,14 +78,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
SD_HandleTypeDef hsd; // create SDIO structure
|
|
|
|
SD_HandleTypeDef hsd; // create SDIO structure
|
|
|
|
|
|
|
|
|
|
|
|
#define TRANSFER_CLOCK_DIV ((uint8_t)SDIO_INIT_CLK_DIV/40)
|
|
|
|
#define TRANSFER_CLOCK_DIV (uint8_t(SDIO_INIT_CLK_DIV) / 40)
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef USBD_OK
|
|
|
|
#ifndef USBD_OK
|
|
|
|
#define USBD_OK 0
|
|
|
|
#define USBD_OK 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void go_to_transfer_speed() {
|
|
|
|
void go_to_transfer_speed() {
|
|
|
|
|
|
|
|
|
|
|
|
SD_InitTypeDef Init;
|
|
|
|
SD_InitTypeDef Init;
|
|
|
|
|
|
|
|
|
|
|
|
/* Default SDIO peripheral configuration for SD card initialization */
|
|
|
|
/* Default SDIO peripheral configuration for SD card initialization */
|
|
|
@ -78,7 +100,6 @@ void go_to_transfer_speed() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SD_LowLevel_Init(void) {
|
|
|
|
void SD_LowLevel_Init(void) {
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t tempreg;
|
|
|
|
uint32_t tempreg;
|
|
|
|
|
|
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
@ -93,7 +114,7 @@ void SD_LowLevel_Init(void) {
|
|
|
|
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
|
|
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(SDIO_D1_PIN) && defined(SDIO_D2_PIN) && defined(SDIO_D3_PIN) // define D1-D3 only if have a four bit wide SDIO bus
|
|
|
|
#if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // define D1-D3 only if have a four bit wide SDIO bus
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
|
GPIO_InitStruct.Pull = 1; // GPIO_NOPULL;
|
|
|
|
GPIO_InitStruct.Pull = 1; // GPIO_NOPULL;
|
|
|
@ -113,7 +134,7 @@ void SD_LowLevel_Init(void) {
|
|
|
|
|
|
|
|
|
|
|
|
//Initialize the SDIO (with initial <400Khz Clock)
|
|
|
|
//Initialize the SDIO (with initial <400Khz Clock)
|
|
|
|
tempreg = 0; //Reset value
|
|
|
|
tempreg = 0; //Reset value
|
|
|
|
tempreg |= SDIO_CLKCR_CLKEN; //Clock is enabled
|
|
|
|
tempreg |= SDIO_CLKCR_CLKEN; // Clock enabled
|
|
|
|
tempreg |= (uint32_t)0x76; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
|
|
|
|
tempreg |= (uint32_t)0x76; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
|
|
|
|
// Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
|
|
|
|
// Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
|
|
|
|
SDIO->CLKCR = tempreg;
|
|
|
|
SDIO->CLKCR = tempreg;
|
|
|
@ -122,17 +143,12 @@ void SD_LowLevel_Init(void) {
|
|
|
|
SDIO->POWER = 0x03;
|
|
|
|
SDIO->POWER = 0x03;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
|
|
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
|
|
|
UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
|
|
|
|
UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
|
|
|
|
__HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
|
|
|
|
__HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constexpr uint8_t SD_RETRY_COUNT = (1
|
|
|
|
constexpr uint8_t SD_RETRY_COUNT = 1 + 2 * ENABLED(SD_CHECK_AND_RETRY);
|
|
|
|
#if ENABLED(SD_CHECK_AND_RETRY)
|
|
|
|
|
|
|
|
+ 2
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SDIO_Init() {
|
|
|
|
bool SDIO_Init() {
|
|
|
|
//init SDIO and get SD card info
|
|
|
|
//init SDIO and get SD card info
|
|
|
@ -153,7 +169,7 @@ bool SDIO_Init() {
|
|
|
|
|
|
|
|
|
|
|
|
go_to_transfer_speed();
|
|
|
|
go_to_transfer_speed();
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(SDIO_D1_PIN) && defined(SDIO_D2_PIN) && defined(SDIO_D3_PIN) // go to 4 bit wide mode if pins are defined
|
|
|
|
#if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
|
|
|
|
retry_Cnt = retryCnt;
|
|
|
|
retry_Cnt = retryCnt;
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
|
|
|
|
if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
|
|
|
@ -208,18 +224,17 @@ void init_SDIO_pins(void) {
|
|
|
|
//bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
|
|
|
|
//bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
|
|
|
|
|
|
|
|
|
|
|
|
bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
|
|
|
bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
|
|
|
bool status;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hsd.Instance = SDIO;
|
|
|
|
hsd.Instance = SDIO;
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t retryCnt = SD_RETRY_COUNT;
|
|
|
|
uint8_t retryCnt = SD_RETRY_COUNT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool status;
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
bool status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
|
|
|
|
status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
|
|
|
|
status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
|
|
status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
|
|
if (!status) return false; // return passing status
|
|
|
|
if (!status) break; // return passing status
|
|
|
|
if (!--retryCnt) return true; // return failing status if retries are exhausted
|
|
|
|
if (!--retryCnt) break; // return failing status if retries are exhausted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
return (bool) ((status_read | status_card) ? 1 : 0);
|
|
|
|
return (bool) ((status_read | status_card) ? 1 : 0);
|
|
|
@ -257,18 +272,18 @@ bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
|
|
|
bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
|
|
|
bool status;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hsd.Instance = SDIO;
|
|
|
|
hsd.Instance = SDIO;
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t retryCnt = SD_RETRY_COUNT;
|
|
|
|
uint8_t retryCnt = SD_RETRY_COUNT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool status;
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
|
|
|
|
status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
|
|
|
|
status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
|
|
status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
|
|
if (!status) return (bool) status; // return passing status
|
|
|
|
if (!status) break; // return passing status
|
|
|
|
if (!--retryCnt) return (bool) status; // return failing status if retries are exhausted
|
|
|
|
if (!--retryCnt) break; // return failing status if retries are exhausted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // !USBD_USE_CDC_COMPOSITE
|
|
|
|
#endif // SDIO_SUPPORT
|
|
|
|
#endif // SDIO_SUPPORT
|
|
|
|