I am driving a 32 GB SD card with Keil MDK5 Flash File System v6.14.1 with core LFN debug mode.Init, Mounting, and also read operations from SD card works properly. But creating or deleting a file and then open it in write mode doesn't work at all.
device: stm32H750vbt6 with ARM CORTEX-M7
core clock (frequency): 480e6
SDMMC1 configured clock (frequency): 100e6
RTOS type is CMSIS-RTOS2 Keil RTX version 5 (RTX5)
Number of open files that configured in FS_Config.c file: 5
Heap Size = 0x2000 > 5 * (608)
Stack Size: 0x400
Card Detect Input pin and Write Protect Input pin: not available
initialization code:
//file fsys_init.c #include "fsys_init.h" SD_HandleTypeDef hsd1; //fsys_init called in app_main thraed void fsys_init(void){ static volatile fsStatus error_tp; error_tp = finit ("M:"); if (error_tp != fsOK) fsys_error_handler(fsys_finit_error, error_tp); osDelay(10); // Mount the M: drive. error_tp = fsOK + 1; while(error_tp != fsOK ){ error_tp = fmount ("M:"); } //HAL_SD_MspInit(&hsd1); } /** * @brief SD MSP Initialization * This function configures the hardware resources used in this example * @param hsd: SD handle pointer * @retval None */ void HAL_SD_MspInit(SD_HandleTypeDef* hsd) { GPIO_InitTypeDef GPIO_InitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; if(hsd->Instance==SDMMC1) { /* USER CODE BEGIN SDMMC1_MspInit 0 */ /* USER CODE END SDMMC1_MspInit 0 */ /** Initializes the peripherals clock */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SDMMC; PeriphClkInitStruct.PLL2.PLL2M = 1; PeriphClkInitStruct.PLL2.PLL2N = 25; PeriphClkInitStruct.PLL2.PLL2P = 2; PeriphClkInitStruct.PLL2.PLL2Q = 2; PeriphClkInitStruct.PLL2.PLL2R = 2; PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3; PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE; PeriphClkInitStruct.PLL2.PLL2FRACN = 0.0; PeriphClkInitStruct.SdmmcClockSelection = RCC_SDMMCCLKSOURCE_PLL2; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { fsys_error_handler(0, 0); } /* Peripheral clock enable */ __HAL_RCC_SDMMC1_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); /**SDMMC1 GPIO Configuration PC8 ------> SDMMC1_D0 PC9 ------> SDMMC1_D1 PC10 ------> SDMMC1_D2 PC11 ------> SDMMC1_D3 PC12 ------> SDMMC1_CK PD2 ------> SDMMC1_CMD */ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11 |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_SDIO1; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; 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_SDIO1; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /* SDMMC1 interrupt Init */ HAL_NVIC_SetPriority(SDMMC1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(SDMMC1_IRQn); /* USER CODE BEGIN SDMMC1_MspInit 1 */ /* USER CODE END SDMMC1_MspInit 1 */ } } void fsys_error_handler(fsys_error id, fsStatus error_tp){ __asm("BKPT #0"); }
After debugging the following events occurred when I tried to open and create "M:\filesys\Dump_file.logo" in write mode:
Event, Time (sec), Component, Event Property, Value 0, 0.00039830, EvCtrl, EventRecorderInitialize, Restart Count = 1 1, 0.00040655, EvCtrl, EventRecorderStart, 2, 0.00236864, EvCtrl, EventRecorderInitialize, Restart Count = 2 3, 0.00237939, EvCtrl, EventRecorderStart, 4, 0.00262214, FsCore, finit, drive=0x0800AFBC 5, 0.23689052, FsCore, fmount, drive=0x0800AFBC 6, 6.21071641, FsCore, sys_open, name=0x080048C8 openmode=0x00000004 7, 6.34828006, FsFAT, FileIsNonExistent, drive=M0 8, 6.47961456, FsMcMCI, TransferError, instance=0 events=0x00000084 9, 6.52405997, FsMcMCI, TransferError, instance=0 events=0x00000084 10, 6.56378528, FsMcMCI, TransferError, instance=0 events=0x00000084 11, 6.60354884, FsMcMCI, TransferError, instance=0 events=0x00000084 12, 6.64327516, FsMcMCI, TransferError, instance=0 events=0x00000084 13, 6.68302375, FsMcMCI, TransferError, instance=0 events=0x00000084 14, 6.72276447, FsMcMCI, TransferError, instance=0 events=0x00000084 15, 6.76250128, FsMcMCI, TransferError, instance=0 events=0x00000084 16, 6.80345253, FsFAT, SectorWriteFailed, drive=M0 sector=32640 count=1
The event TransferError is generated when data transfer is active and data block CRC check fails or Memory Card Control Layer does not receive ARM_MCI_EVENT_TRANSFER_COMPLETE event from the MCI driver.
Clever engineers, please help me to solve these bad events!!
sd card info:
Hi,apparently MCI driver is not able to successfully transfer data between MCU and SD card. You can debug MCI driver interrupt routine to check exactly which driver event is sent to FileSystem when TransferError appears. In case if CRC checking is failing I would check pin configuration for driving SD card.
Usually DAT0, DAT1, DAT2, DAT3 and CMD lines should have pull up resistors configured and there should be no pull up/pull down resistor on CLK line.I don't know how does the external interface look like on your PCB but if there are no external resistors then you must configure MCU internal pull ups.
Hi Vladimir, thanks for the explanation.
Here you can see the external interface schematic on my PCB:
I'm using stm32h7_mci CMSIS-Driver to drive the SD module,There is no problem when I want to read the SD card data, but the driver can not write(transfer) any data to it.
This looks ok and makes me wonder why data write transfer fails...Based on TransferError event value, the command is sent out successfully (event value 0x04 == ARM_MCI_EVENT_COMMAND_COMPLETE), but right after that the error event is received (value 0x80 == ARM_MCI_EVENT_TRANSFER_ERROR)This implies that something might be wrong with the interface (hardware looks ok), pin configuration or even with the MCI driver.Did you check or compare your software config with the File_Demo example for any STM32H7 evaluation board?
I tested this program before and the data was transmitted correctly. It made me wonder too and I wasted a lot of time on it. The memory card did not respond after transferring data and sending the write command, and the problem was solved by replacing the old memory card.
Well, memory cards can fail too. If it was old and/or with a lot of transferred data the flash memory can wear out. Thanks for reporting what solved the problem!
Thanks for your detailed response.