|
@ -36,7 +36,8 @@ |
|
|
|
|
|
|
|
|
// use USB drivers
|
|
|
// use USB drivers
|
|
|
|
|
|
|
|
|
extern "C" { int8_t SD_MSC_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len); |
|
|
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); |
|
|
int8_t SD_MSC_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len); |
|
|
extern SD_HandleTypeDef hsd; |
|
|
extern SD_HandleTypeDef hsd; |
|
|
} |
|
|
} |
|
@ -75,7 +76,18 @@ |
|
|
#error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported" |
|
|
#error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported" |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
// Fixed
|
|
|
|
|
|
#define SDIO_D0_PIN PC8 |
|
|
|
|
|
#define SDIO_D1_PIN PC9 |
|
|
|
|
|
#define SDIO_D2_PIN PC10 |
|
|
|
|
|
#define SDIO_D3_PIN PC11 |
|
|
|
|
|
#define SDIO_CK_PIN PC12 |
|
|
|
|
|
#define SDIO_CMD_PIN PD2 |
|
|
|
|
|
|
|
|
SD_HandleTypeDef hsd; // create SDIO structure
|
|
|
SD_HandleTypeDef hsd; // create SDIO structure
|
|
|
|
|
|
// F4 support one dma for RX and another for TX.
|
|
|
|
|
|
// But Marlin will never do read and write at same time, so we use always one dma for both.
|
|
|
|
|
|
DMA_HandleTypeDef hdma_sdio; |
|
|
|
|
|
|
|
|
/*
|
|
|
/*
|
|
|
SDIO_INIT_CLK_DIV is 118 |
|
|
SDIO_INIT_CLK_DIV is 118 |
|
@ -96,7 +108,7 @@ |
|
|
|
|
|
|
|
|
// Target Clock, configurable. Default is 18MHz, from STM32F1
|
|
|
// Target Clock, configurable. Default is 18MHz, from STM32F1
|
|
|
#ifndef SDIO_CLOCK |
|
|
#ifndef SDIO_CLOCK |
|
|
#define SDIO_CLOCK 18000000 /* 18 MHz */ |
|
|
#define SDIO_CLOCK 18000000 // 18 MHz
|
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
// SDIO retries, configurable. Default is 3, from STM32F1
|
|
|
// SDIO retries, configurable. Default is 3, from STM32F1
|
|
@ -120,24 +132,21 @@ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void go_to_transfer_speed() { |
|
|
void go_to_transfer_speed() { |
|
|
SD_InitTypeDef Init; |
|
|
|
|
|
|
|
|
|
|
|
/* Default SDIO peripheral configuration for SD card initialization */ |
|
|
/* Default SDIO peripheral configuration for SD card initialization */ |
|
|
Init.ClockEdge = hsd.Init.ClockEdge; |
|
|
hsd.Init.ClockEdge = hsd.Init.ClockEdge; |
|
|
Init.ClockBypass = hsd.Init.ClockBypass; |
|
|
hsd.Init.ClockBypass = hsd.Init.ClockBypass; |
|
|
Init.ClockPowerSave = hsd.Init.ClockPowerSave; |
|
|
hsd.Init.ClockPowerSave = hsd.Init.ClockPowerSave; |
|
|
Init.BusWide = hsd.Init.BusWide; |
|
|
hsd.Init.BusWide = hsd.Init.BusWide; |
|
|
Init.HardwareFlowControl = hsd.Init.HardwareFlowControl; |
|
|
hsd.Init.HardwareFlowControl = hsd.Init.HardwareFlowControl; |
|
|
Init.ClockDiv = clock_to_divider(SDIO_CLOCK); |
|
|
hsd.Init.ClockDiv = clock_to_divider(SDIO_CLOCK); |
|
|
|
|
|
|
|
|
/* Initialize SDIO peripheral interface with default configuration */ |
|
|
/* Initialize SDIO peripheral interface with default configuration */ |
|
|
SDIO_Init(hsd.Instance, Init); |
|
|
SDIO_Init(hsd.Instance, hsd.Init); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void SD_LowLevel_Init(void) { |
|
|
void SD_LowLevel_Init(void) { |
|
|
uint32_t tempreg; |
|
|
uint32_t tempreg; |
|
|
|
|
|
|
|
|
__HAL_RCC_SDIO_CLK_ENABLE(); |
|
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
|
|
|
__HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
|
|
|
__HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
|
|
|
|
|
|
|
|
@ -163,11 +172,45 @@ |
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2; |
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2; |
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
|
|
|
|
|
|
|
|
#if DISABLED(STM32F1xx) |
|
|
// Setup DMA
|
|
|
// TODO: use __HAL_RCC_SDIO_RELEASE_RESET() and __HAL_RCC_SDIO_CLK_ENABLE();
|
|
|
#if defined(STM32F1xx) |
|
|
RCC->APB2RSTR &= ~RCC_APB2RSTR_SDIORST_Msk; // take SDIO out of reset
|
|
|
hdma_sdio.Init.Mode = DMA_NORMAL; |
|
|
RCC->APB2ENR |= RCC_APB2RSTR_SDIORST_Msk; // enable SDIO clock
|
|
|
hdma_sdio.Instance = DMA2_Channel4; |
|
|
// Enable the DMA2 Clock
|
|
|
HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn); |
|
|
|
|
|
#elif defined(STM32F4xx) |
|
|
|
|
|
hdma_sdio.Init.Mode = DMA_PFCTRL; |
|
|
|
|
|
hdma_sdio.Instance = DMA2_Stream3; |
|
|
|
|
|
hdma_sdio.Init.Channel = DMA_CHANNEL_4; |
|
|
|
|
|
hdma_sdio.Init.FIFOMode = DMA_FIFOMODE_ENABLE; |
|
|
|
|
|
hdma_sdio.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
|
|
|
|
|
hdma_sdio.Init.MemBurst = DMA_MBURST_INC4; |
|
|
|
|
|
hdma_sdio.Init.PeriphBurst = DMA_PBURST_INC4; |
|
|
|
|
|
HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn); |
|
|
|
|
|
#endif |
|
|
|
|
|
HAL_NVIC_EnableIRQ(SDIO_IRQn); |
|
|
|
|
|
hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE; |
|
|
|
|
|
hdma_sdio.Init.MemInc = DMA_MINC_ENABLE; |
|
|
|
|
|
hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; |
|
|
|
|
|
hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; |
|
|
|
|
|
hdma_sdio.Init.Priority = DMA_PRIORITY_LOW; |
|
|
|
|
|
__HAL_LINKDMA(&hsd, hdmarx, hdma_sdio); |
|
|
|
|
|
__HAL_LINKDMA(&hsd, hdmatx, hdma_sdio); |
|
|
|
|
|
|
|
|
|
|
|
#if defined(STM32F1xx) |
|
|
|
|
|
__HAL_RCC_SDIO_CLK_ENABLE(); |
|
|
|
|
|
__HAL_RCC_DMA2_CLK_ENABLE(); |
|
|
|
|
|
#else |
|
|
|
|
|
__HAL_RCC_SDIO_FORCE_RESET(); |
|
|
|
|
|
delay(2); |
|
|
|
|
|
__HAL_RCC_SDIO_RELEASE_RESET(); |
|
|
|
|
|
delay(2); |
|
|
|
|
|
__HAL_RCC_SDIO_CLK_ENABLE(); |
|
|
|
|
|
|
|
|
|
|
|
__HAL_RCC_DMA2_FORCE_RESET(); |
|
|
|
|
|
delay(2); |
|
|
|
|
|
__HAL_RCC_DMA2_RELEASE_RESET(); |
|
|
|
|
|
delay(2); |
|
|
|
|
|
__HAL_RCC_DMA2_CLK_ENABLE(); |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
//Initialize the SDIO (with initial <400Khz Clock)
|
|
|
//Initialize the SDIO (with initial <400Khz Clock)
|
|
@ -179,6 +222,7 @@ |
|
|
|
|
|
|
|
|
// Power up the SDIO
|
|
|
// Power up the SDIO
|
|
|
SDIO_PowerState_ON(SDIO); |
|
|
SDIO_PowerState_ON(SDIO); |
|
|
|
|
|
hsd.Instance = SDIO; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
|
|
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
|
@ -222,106 +266,81 @@ |
|
|
if (!status) break; |
|
|
if (!status) break; |
|
|
if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
|
|
if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
|
|
} |
|
|
} |
|
|
|
|
|
go_to_transfer_speed(); |
|
|
} |
|
|
} |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
/*
|
|
|
|
|
|
void init_SDIO_pins(void) { |
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0}; |
|
|
|
|
|
|
|
|
|
|
|
// SDIO GPIO Configuration
|
|
|
static bool SDIO_ReadWriteBlock_DMA(uint32_t block, const uint8_t *src, uint8_t *dst) { |
|
|
// PC8 ------> SDIO_D0
|
|
|
if(HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) return false; |
|
|
// PC12 ------> SDIO_CK
|
|
|
|
|
|
// PD2 ------> SDIO_CMD
|
|
|
|
|
|
|
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_8; |
|
|
TERN_(USE_WATCHDOG, HAL_watchdog_refresh()); |
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
|
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL; |
|
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
|
|
|
|
|
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO; |
|
|
|
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
|
|
|
|
|
|
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_12; |
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
|
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL; |
|
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
|
|
|
|
|
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO; |
|
|
|
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
|
|
|
|
|
|
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2; |
|
|
HAL_StatusTypeDef ret; |
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
|
|
if (src) { |
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL; |
|
|
hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH; |
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
|
|
HAL_DMA_Init(&hdma_sdio); |
|
|
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO; |
|
|
ret = HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)src, block, 1); |
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY; |
|
|
|
|
|
HAL_DMA_Init(&hdma_sdio); |
|
|
|
|
|
ret = HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)dst, block, 1); |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
|
//bool SDIO_init() { 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) { |
|
|
if (ret != HAL_OK) { |
|
|
hsd.Instance = SDIO; |
|
|
HAL_DMA_Abort_IT(&hdma_sdio); |
|
|
uint8_t retryCnt = SDIO_READ_RETRIES; |
|
|
HAL_DMA_DeInit(&hdma_sdio); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
bool status; |
|
|
uint32_t timeout = millis() + 500; |
|
|
for (;;) { |
|
|
// Wait the transfer
|
|
|
TERN_(USE_WATCHDOG, HAL_watchdog_refresh()); |
|
|
while (hsd.State != HAL_SD_STATE_READY) { |
|
|
status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
|
|
|
if (millis() > timeout) { |
|
|
status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
|
HAL_DMA_Abort_IT(&hdma_sdio); |
|
|
if (!status) break; // return passing status
|
|
|
HAL_DMA_DeInit(&hdma_sdio); |
|
|
if (!--retryCnt) break; // return failing status if retries are exhausted
|
|
|
return false; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
return status; |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
while (__HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TC_FLAG_INDEX(&hdma_sdio)) != 0 |
|
|
return (bool) ((status_read | status_card) ? 1 : 0); |
|
|
|| __HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TE_FLAG_INDEX(&hdma_sdio)) != 0) { /* nada */ } |
|
|
|
|
|
|
|
|
if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false; |
|
|
HAL_DMA_Abort_IT(&hdma_sdio); |
|
|
if (blockAddress >= SdCard.LogBlockNbr) return false; |
|
|
HAL_DMA_DeInit(&hdma_sdio); |
|
|
if ((0x03 & (uint32_t)data)) return false; // misaligned data
|
|
|
|
|
|
|
|
|
|
|
|
if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; } |
|
|
timeout = millis() + 500; |
|
|
|
|
|
while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) |
|
|
|
|
|
if (millis() > timeout) return false; |
|
|
|
|
|
|
|
|
if (!SDIO_CmdReadSingleBlock(blockAddress)) { |
|
|
return true; |
|
|
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); |
|
|
|
|
|
dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {} |
|
|
bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) { |
|
|
|
|
|
uint8_t retries = SDIO_READ_RETRIES; |
|
|
dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); |
|
|
while (retries--) if (SDIO_ReadWriteBlock_DMA(block, NULL, dst)) return true; |
|
|
|
|
|
|
|
|
if (SDIO->STA & SDIO_STA_RXDAVL) { |
|
|
|
|
|
while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO; |
|
|
|
|
|
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); |
|
|
|
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) { |
|
|
bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) { |
|
|
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); |
|
|
uint8_t retries = SDIO_READ_RETRIES; |
|
|
|
|
|
while (retries--) if (SDIO_ReadWriteBlock_DMA(block, src, NULL)) return true; |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
#if defined(STM32F1xx) |
|
|
} |
|
|
#define DMA_IRQ_HANDLER DMA2_Channel4_5_IRQHandler |
|
|
|
|
|
#elif defined(STM32F4xx) |
|
|
|
|
|
#define DMA_IRQ_HANDLER DMA2_Stream3_IRQHandler |
|
|
|
|
|
#else |
|
|
|
|
|
#error "Unknown STM32 architecture." |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) { |
|
|
extern "C" void SDIO_IRQHandler(void) { HAL_SD_IRQHandler(&hsd); } |
|
|
hsd.Instance = SDIO; |
|
|
extern "C" void DMA_IRQ_HANDLER(void) { HAL_DMA_IRQHandler(&hdma_sdio); } |
|
|
uint8_t retryCnt = SDIO_READ_RETRIES; |
|
|
|
|
|
bool status; |
|
|
|
|
|
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_GetCardState(&hsd); // make sure all is OK
|
|
|
|
|
|
if (!status) break; // return passing status
|
|
|
|
|
|
if (!--retryCnt) break; // return failing status if retries are exhausted
|
|
|
|
|
|
} |
|
|
|
|
|
return status; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif // !USBD_USE_CDC_COMPOSITE
|
|
|
#endif // !USBD_USE_CDC_COMPOSITE
|
|
|
#endif // SDIO_SUPPORT
|
|
|
#endif // SDIO_SUPPORT
|
|
|