2020-09-04 02:35 AM
I'm aware this SD-card question appears beaten to death but from all postings I've read most appear to be SPI related, which I'm not using as trying to use SDIO comms. Having tried my best for over a week now, I feel it's about time I reached out.
I think one of the biggest areas of confusion for me is the output of the SD-card in my setup under two scenarios: running and step-by-step debugging; the former appears to be truncating the output of the card while the latter shows the full response - please see images attached:
Normal running....
Step-by-step debugging....
I've started with a vanilla project where only the SD-card (SDMMC2) is defined with the following parameters:
Mode: 1-bit
Clock transition: Rising transition
SDMMC Clock output: Disable the power save for the clock
SDMMC H/W flow control: The hardware control flow is disabled
SDMMC clock divide factor: 0
Is external transceiver present: No
SDMMC global interrupt: Enabled
PA0: SDMMC_CMD
PB14: SDMMC_D0
PC1: SDMMC_CLK
I'm also tring to accomplish this using FATFS, with the following configuration:
Mode: SD Card
FATFS version: R0.12c
FS_READONLY: Disabled
FS_MINIMIZE: Disabled
USE_STRFUNC: Enabled
USE_FIND: Disabled
USE_MKFS: Enabled
USE_FASTSEEK: Enabled
USE_EXPAND: Disabled
USE_CHMOD: Disabled
USE_LABEL: Disabled
USE_FORWARD: Disabled
CODE_PAGE: Latin 1
USE_LFN: Disabled
MAX_LFN: 255
LFN_UNICODE: ANSI/OEM
STRF_ENCODE: UTF-8
FS_RPATH: Disabled
VOLUMES: 1
MAX_SS: 4096
MIN_SS: 512
MULTI_PARTITION: Disabled
USE_TRIM: Disabled
FS_NOFSINFO: 0
FS_TINY: Disabled
FS_EXFAT: Disabled
FS_NORTC: Dynamic timestamp
FS_REENTRANT: Disabled
FS_TIMEOUT: 1000
FS_LOCK: 2
Detect_SDIO: GPIO:Input PG5 (tied to GND for card detection)
SDMMC instance: SDMMC2
Use dma template: Disabled
BSP code for SD: Generic
The clock is configured for a full speed capability 480 MHz via 64 MHz HSI RC, but with DIVP2, DIVQ2 and DIVR2 clocked down to 16 MHz as I'm using 3.3V and not looking to use the higher transfer speed. The high speed clock (HSE) in the RCC configuration has been set to BYPASS Clock Source.
Finally, I've increased the Minimum Heap and Stack sizes to 0x2000 and 0x4000 respectively.
My code is fairly straight forward:
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//#include "ff.h" /* Tried this in conjunction with other includes */
//#include "diskio.h" /* Tried this in conjunction with other includes */
/* USER CODE END Includes */
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDMMC2_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
/* Fatfs object */
FATFS FatFs;
/* File object */
FIL fil;
/* File path */
char pathSD[12];
/* Select SD-card */
uint32_t *data_PortG = 0x58021818;
*data_PortG |= 0x00010000; /* Clear bit 0 of Port G to enable the CS# of the SD-Card connector */
*data_PortG &= 0xFFFFFFFE; /* Just in case */
/* Mount drive */
uint8_t fatResult = 0;
fatResult = (uint8_t)f_mount(&FatFs, (TCHAR const*)pathSD, 1);
if(fatResult == FR_OK)
{
/* Everything worked out but we never get here */
}
else
{
*data_PortG |= 0x00000001; /* Signal a problem by deactivating CS# */
}
/* USER CODE END 2 */
.
.
.
}
Note that normally I manipulate the GPIO ports directly in my main program for the fastest transitions, which is why there's a direct memory reference to Port G.