/** * @brief Initializes the SD card device. * @param None * @retval SD status */ uint8_t BSP_SD_Init(void) { uint8_t sd_state = MSD_OK; /* uSD device interface configuration */ uSdHandle.Instance = SDMMC1; uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING; uSdHandle.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE; uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_1B; uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE; uSdHandle.Init.ClockDiv = SDMMC_TRANSFER_CLK_DIV; /* Msp SD Detect pin initialization */ SD_Detect_MspInit(); /* Check if the SD card is plugged in the slot */ if (BSP_SD_IsDetected() != SD_PRESENT) { return MSD_ERROR_SD_NOT_PRESENT; } /* Msp SD initialization */ BSP_SD_MspInit(&uSdHandle, NULL); /* HAL SD initialization */ if (HAL_SD_Init(&uSdHandle) != HAL_OK) { sd_state = MSD_ERROR; } /* Configure SD Bus width */ if (sd_state == MSD_OK) { /* Enable wide operation */ if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SDMMC_BUS_WIDE_4B) != HAL_OK) { sd_state = MSD_ERROR; } else { sd_state = MSD_OK; } } return sd_state; }
/** * @brief Initializes the SD card device. * @param None * @retval SD status */ static uint8_t SD_Init(void) { uint8_t SD_state = MSD_OK; /* uSD device interface configuration */ uSdHandle.Instance = SDMMC1; uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING; uSdHandle.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE; uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_1B; uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE; uSdHandle.Init.ClockDiv = SDMMC_TRANSFER_CLK_DIV; /* Msp SD Detect pin initialization */ SD_Detect_MspInit(); /* Check if SD card is present */ if(SD_IsDetected() != SD_PRESENT) return MSD_ERROR_SD_NOT_PRESENT; /* Init GPIO and NVIC */ SD_MspInit(); /* HAL SD initialization */ if(HAL_SD_Init(&uSdHandle, &uSdCardInfo) != SD_OK) SD_state = MSD_ERROR; /* Configure SD Bus width */ if (SD_state == MSD_OK) { /* Enable wide operation */ if(HAL_SD_WideBusOperation_Config(&uSdHandle, SDMMC_BUS_WIDE_4B) != SD_OK) SD_state = MSD_ERROR; else SD_state = MSD_OK; } return SD_state; }