diff --git a/keywords.txt b/keywords.txt index f9fd880..a99c872 100644 --- a/keywords.txt +++ b/keywords.txt @@ -43,3 +43,9 @@ FAT_TYPE_FAT12 LITERAL1 FAT_TYPE_FAT16 LITERAL1 FAT_TYPE_FAT32 LITERAL1 FAT_TYPE_UNK LITERAL1 +SD_CARD_TYPE_UNK LITERAL1 +SD_CARD_TYPE_UKN LITERAL1 +SD_CARD_TYPE_SD1 LITERAL1 +SD_CARD_TYPE_SD2 LITERAL1 +SD_CARD_TYPE_SDHC LITERAL1 +SD_CARD_TYPE_SECURED LITERAL1 diff --git a/src/SD.cpp b/src/SD.cpp index f220f47..a5e99a9 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -65,11 +65,12 @@ SDClass SD; */ bool SDClass::begin(uint32_t detect, uint32_t level) { + bool status = false; /*##-1- Initializes SD IOs #############################################*/ if (_card.init(detect, level)) { - return _fatFs.init(); + status = _fatFs.init(); } - return false; + return status; } /** @@ -79,11 +80,12 @@ bool SDClass::begin(uint32_t detect, uint32_t level) */ bool SDClass::end(void) { + bool status = false; /*##-1- DeInitializes SD IOs ###########################################*/ if (_fatFs.deinit()) { - return _card.deinit(); + status = _card.deinit(); } - return false; + return status; } /** @@ -95,11 +97,7 @@ bool SDClass::exists(const char *filepath) { FILINFO fno; - if (f_stat(filepath, &fno) != FR_OK) { - return false; - } else { - return true; - } + return (f_stat(filepath, &fno) != FR_OK) ? false : true; } /** @@ -110,11 +108,7 @@ bool SDClass::exists(const char *filepath) bool SDClass::mkdir(const char *filepath) { FRESULT res = f_mkdir(filepath); - if ((res != FR_OK) && (res != FR_EXIST)) { - return false; - } else { - return true; - } + return ((res != FR_OK) && (res != FR_EXIST)) ? false : true; } /** @@ -124,11 +118,7 @@ bool SDClass::mkdir(const char *filepath) */ bool SDClass::rmdir(const char *filepath) { - if (f_unlink(filepath) != FR_OK) { - return false; - } else { - return true; - } + return (f_unlink(filepath) != FR_OK) ? false : true; } /** @@ -184,11 +174,7 @@ File SDClass::open(const char *filepath, uint8_t mode /* = FA_READ */) */ bool SDClass::remove(const char *filepath) { - if (f_unlink(filepath) != FR_OK) { - return false; - } else { - return true; - } + return (f_unlink(filepath) != FR_OK) ? false : true; } File SDClass::openRoot(void) @@ -344,10 +330,7 @@ int File::read() { UINT byteread; int8_t data; - if (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) { - return data; - } - return -1; + return (f_read(_fil, (void *)&data, 1, (UINT *)&byteread) == FR_OK) ? data : -1; } /** @@ -359,11 +342,7 @@ int File::read() int File::read(void *buf, size_t len) { UINT bytesread; - - if (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) { - return bytesread; - } - return -1; + return (f_read(_fil, buf, len, (UINT *)&bytesread) == FR_OK) ? bytesread : -1; } /** @@ -447,15 +426,11 @@ uint32_t File::position() */ bool File::seek(uint32_t pos) { - if (pos > size()) { - return false; - } else { - if (f_lseek(_fil, pos) != FR_OK) { - return false; - } else { - return true; - } + bool status = false; + if (pos <= size()) { + status = (f_lseek(_fil, pos) != FR_OK) ? false : true; } + return status; } /** @@ -529,10 +504,12 @@ char *File::name() /** * @brief Check if the file is directory or normal file - * @retval TRUE if directory else FALSE + * @retval true if directory else false */ bool File::isDirectory() { + // Assume not a directory + bool status = false; FILINFO fno; if (_name == NULL) { Error_Handler(); @@ -542,21 +519,25 @@ bool File::isDirectory() #else if (_dir.fs != 0) #endif - return true; + { + status = true; + } #if (_FATFS == 68300) || (_FATFS == 80286) else if (_fil->obj.fs != 0) #else else if (_fil->fs != 0) #endif - return false; - // if not init get info - if (f_stat(_name, &fno) == FR_OK) { - if (fno.fattrib & AM_DIR) { - return true; + { + status = false; + } else { + // if not init get info + if (f_stat(_name, &fno) == FR_OK) { + if (fno.fattrib & AM_DIR) { + status = true; + } } } - // Assume not a directory - return false; + return status; } File File::openNextFile(uint8_t mode) @@ -569,35 +550,41 @@ File File::openNextFile(uint8_t mode) fno.lfname = lfn; fno.lfsize = sizeof(lfn); #endif - while (1) { + bool found = false; + File filtmp = File(); + while (!found) { res = f_readdir(&_dir, &fno); if (res != FR_OK || fno.fname[0] == 0) { - return File(res); - } - if (fno.fname[0] == '.') { - continue; - } + filtmp._res = res; + found = true; + } else { + if (fno.fname[0] == '.') { + continue; + } #if _USE_LFN && (_FATFS != 68300 && _FATFS != 80286) - fn = *fno.lfname ? fno.lfname : fno.fname; + fn = *fno.lfname ? fno.lfname : fno.fname; #else - fn = fno.fname; + fn = fno.fname; #endif - size_t name_len = strlen(_name); - char *fullPath = (char *)malloc(name_len + strlen(fn) + 2); - if (fullPath != NULL) { - // Avoid twice '/' - if ((name_len > 0) && (_name[name_len - 1] == '/')) { - sprintf(fullPath, "%s%s", _name, fn); + size_t name_len = strlen(_name); + char *fullPath = (char *)malloc(name_len + strlen(fn) + 2); + if (fullPath != NULL) { + // Avoid twice '/' + if ((name_len > 0) && (_name[name_len - 1] == '/')) { + sprintf(fullPath, "%s%s", _name, fn); + } else { + sprintf(fullPath, "%s/%s", _name, fn); + } + filtmp = SD.open(fullPath, mode); + free(fullPath); + found = true; } else { - sprintf(fullPath, "%s/%s", _name, fn); + filtmp._res = FR_NOT_ENOUGH_CORE; + found = true; } - File filtmp = SD.open(fullPath, mode); - free(fullPath); - return filtmp; - } else { - return File(FR_NOT_ENOUGH_CORE); } } + return filtmp; } void File::rewindDirectory(void) diff --git a/src/STM32SD.h b/src/STM32SD.h index 4c76f74..2009ca5 100644 --- a/src/STM32SD.h +++ b/src/STM32SD.h @@ -157,11 +157,19 @@ class SDClass { { return _fatFs.fatType(); } - + /** \return Pointer to SD card object. */ + Sd2Card *card() + { + return &_card; + } + /** \return Pointer to FatFs object. */ + SdFatFs *fatFs() + { + return &_fatFs; + } private: Sd2Card _card; SdFatFs _fatFs; - }; extern SDClass SD; diff --git a/src/Sd2Card.cpp b/src/Sd2Card.cpp index 0b06b17..a06b4c8 100644 --- a/src/Sd2Card.cpp +++ b/src/Sd2Card.cpp @@ -54,41 +54,40 @@ Sd2Card::Sd2Card() bool Sd2Card::init(uint32_t detect, uint32_t level) { + bool status = true; if (detect != SD_DETECT_NONE) { PinName p = digitalPinToPinName(detect); if ((p == NC) || \ BSP_SD_DetectPin(p, level) != MSD_OK) { - return false; + status = false; } } #if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U) - PinName sd_en = digitalPinToPinName(SD_TRANSCEIVER_EN); - PinName sd_sel = digitalPinToPinName(SD_TRANSCEIVER_SEL); - if (BSP_SD_TransceiverPin(set_GPIO_Port_Clock(STM_PORT(sd_en)), - STM_LL_GPIO_PIN(sd_en), - set_GPIO_Port_Clock(STM_PORT(sd_sel)), - STM_LL_GPIO_PIN(sd_sel)) == MSD_ERROR) { - return false; + if (status == true) { + PinName sd_en = digitalPinToPinName(SD_TRANSCEIVER_EN); + PinName sd_sel = digitalPinToPinName(SD_TRANSCEIVER_SEL); + if (BSP_SD_TransceiverPin(set_GPIO_Port_Clock(STM_PORT(sd_en)), + STM_LL_GPIO_PIN(sd_en), + set_GPIO_Port_Clock(STM_PORT(sd_sel)), + STM_LL_GPIO_PIN(sd_sel)) == MSD_ERROR) { + status = false; + } } #endif - if (BSP_SD_Init() == MSD_OK) { - BSP_SD_GetCardInfo(&_SdCardInfo); - return true; + if ((status == true) && (BSP_SD_Init() == MSD_OK)) { + status = BSP_SD_GetCardInfo(&_SdCardInfo); } - return false; + return status; } bool Sd2Card::deinit(void) { - if (BSP_SD_DeInit() == MSD_OK) { - return true; - } - return false; + return (BSP_SD_DeInit() == MSD_OK) ? true : false; } uint8_t Sd2Card::type(void) const { - uint8_t cardType = SD_CARD_TYPE_UKN; + uint8_t cardType = SD_CARD_TYPE_UNK; switch (_SdCardInfo.CardType) { case CARD_SDSC: switch (_SdCardInfo.CardVersion) { @@ -99,7 +98,7 @@ uint8_t Sd2Card::type(void) const cardType = SD_CARD_TYPE_SD2; break; default: - cardType = SD_CARD_TYPE_UKN; + cardType = SD_CARD_TYPE_UNK; } break; case CARD_SDHC_SDXC: @@ -109,8 +108,7 @@ uint8_t Sd2Card::type(void) const cardType = SD_CARD_TYPE_SECURED; break; default: - cardType = SD_CARD_TYPE_UKN; + cardType = SD_CARD_TYPE_UNK; } return cardType; } - diff --git a/src/Sd2Card.h b/src/Sd2Card.h index 69d6818..77236a9 100644 --- a/src/Sd2Card.h +++ b/src/Sd2Card.h @@ -40,7 +40,9 @@ #include "bsp_sd.h" // card types to match Arduino definition -#define SD_CARD_TYPE_UKN 0 +#define SD_CARD_TYPE_UNK 0 +// back compatibility +#define SD_CARD_TYPE_UKN SD_CARD_TYPE_UNK /** Standard capacity V1 SD card */ #define SD_CARD_TYPE_SD1 1 /** Standard capacity V2 SD card */ diff --git a/src/SdFatFs.cpp b/src/SdFatFs.cpp index a3bbf09..6a5a79e 100644 --- a/src/SdFatFs.cpp +++ b/src/SdFatFs.cpp @@ -39,29 +39,30 @@ bool SdFatFs::init(void) { - + bool status = false; /*##-1- Link the SD disk I/O driver ########################################*/ if (FATFS_LinkDriver(&SD_Driver, _SDPath) == 0) { /*##-2- Register the file system object to the FatFs module ##############*/ if (f_mount(&_SDFatFs, (TCHAR const *)_SDPath, 1) == FR_OK) { /* FatFs Initialization done */ - return true; + status = true; } } - return false; + return status; } bool SdFatFs::deinit(void) { + bool status = false; /*##-1- Unregister the file system object to the FatFs module ##############*/ if (f_unmount((TCHAR const *)_SDPath) == FR_OK) { /*##-2- Unlink the SD disk I/O driver ####################################*/ if (FATFS_UnLinkDriver(_SDPath) == 0) { /* FatFs deInitialization done */ - return true; + status = true; } } - return false; + return status; } uint8_t SdFatFs::fatType(void) diff --git a/src/bsp_sd.c b/src/bsp_sd.c index 59d813f..d55a2cb 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -135,9 +135,9 @@ SD_PinName_t SD_PinNames = { #if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x02050000) /** * @brief Get the SD card device instance from pins - * @retval SD status + * @retval boolean true if successful, false otherwise */ -uint8_t BSP_SD_GetInstance(void) +bool BSP_SD_GetInstance(void) { SD_TypeDef *sd_d0 = NP; SD_TypeDef *sd_d1 = NP; @@ -145,6 +145,7 @@ uint8_t BSP_SD_GetInstance(void) SD_TypeDef *sd_d3 = NP; SD_TypeDef *sd_cmd = NP; SD_TypeDef *sd_ck = NP; + bool res = true; /* If a pin is not defined, use the first pin available in the associated PinMap_SD_* arrays */ if (SD_PinNames.pin_d0 == NC) { @@ -201,67 +202,67 @@ uint8_t BSP_SD_GetInstance(void) #endif sd_cmd == NP || sd_ck == NP) { core_debug("ERROR: at least one SD pin has no peripheral\n"); - return MSD_ERROR; - } - - SD_TypeDef *sd_d01 = pinmap_merge_peripheral(sd_d0, sd_d1); - SD_TypeDef *sd_d23 = pinmap_merge_peripheral(sd_d2, sd_d3); - SD_TypeDef *sd_cx = pinmap_merge_peripheral(sd_cmd, sd_ck); - SD_TypeDef *sd_dx = pinmap_merge_peripheral(sd_d01, sd_d23); - SD_TypeDef *sd_base = pinmap_merge_peripheral(sd_dx, sd_cx); - if (sd_d01 == NP || + res = false; + } else { + SD_TypeDef *sd_d01 = pinmap_merge_peripheral(sd_d0, sd_d1); + SD_TypeDef *sd_d23 = pinmap_merge_peripheral(sd_d2, sd_d3); + SD_TypeDef *sd_cx = pinmap_merge_peripheral(sd_cmd, sd_ck); + SD_TypeDef *sd_dx = pinmap_merge_peripheral(sd_d01, sd_d23); + SD_TypeDef *sd_base = pinmap_merge_peripheral(sd_dx, sd_cx); + if (sd_d01 == NP || #if SD_BUS_WIDE == SD_BUS_WIDE_4B - sd_d23 == NP || + sd_d23 == NP || #endif - sd_cx == NP || sd_dx == NP || sd_base == NP) { - core_debug("ERROR: SD pins mismatch\n"); - return MSD_ERROR; - } - uSdHandle.Instance = sd_base; + sd_cx == NP || sd_dx == NP || sd_base == NP) { + core_debug("ERROR: SD pins mismatch\n"); + res = false; + } + uSdHandle.Instance = sd_base; #if defined(SDMMC1) || defined(SDMMC2) #if !defined(SDMMC_CKIN_NA) - if (SD_PinNames.pin_ckin != NC) { - SD_TypeDef *sd_ckin = pinmap_peripheral(SD_PinNames.pin_ckin, PinMap_SD_CKIN); - if (pinmap_merge_peripheral(sd_ckin, sd_base) == NP) { - core_debug("ERROR: SD CKIN pin mismatch\n"); - return MSD_ERROR; + if (res && (SD_PinNames.pin_ckin != NC)) { + SD_TypeDef *sd_ckin = pinmap_peripheral(SD_PinNames.pin_ckin, PinMap_SD_CKIN); + if (pinmap_merge_peripheral(sd_ckin, sd_base) == NP) { + core_debug("ERROR: SD CKIN pin mismatch\n"); + res = false; + } } - } #endif #if !defined(SDMMC_CDIR_NA) - if (SD_PinNames.pin_cdir != NC) { - SD_TypeDef *sd_cdir = pinmap_peripheral(SD_PinNames.pin_cdir, PinMap_SD_CDIR); - if (pinmap_merge_peripheral(sd_cdir, sd_base) == NP) { - core_debug("ERROR: SD CDIR pin mismatch\n"); - return MSD_ERROR; + if ((res && SD_PinNames.pin_cdir != NC)) { + SD_TypeDef *sd_cdir = pinmap_peripheral(SD_PinNames.pin_cdir, PinMap_SD_CDIR); + if (pinmap_merge_peripheral(sd_cdir, sd_base) == NP) { + core_debug("ERROR: SD CDIR pin mismatch\n"); + res = false; + } } - } #endif #if !defined(SDMMC_D0DIR_NA) - if (SD_PinNames.pin_cdir != NC) { - SD_TypeDef *sd_d0dir = pinmap_peripheral(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR); - if (pinmap_merge_peripheral(sd_d0dir, sd_base) == NP) { - core_debug("ERROR: SD DODIR pin mismatch\n"); - return MSD_ERROR; + if (res && (SD_PinNames.pin_cdir != NC)) { + SD_TypeDef *sd_d0dir = pinmap_peripheral(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR); + if (pinmap_merge_peripheral(sd_d0dir, sd_base) == NP) { + core_debug("ERROR: SD DODIR pin mismatch\n"); + res = false; + } } - } #endif #if !defined(SDMMC_D123DIR_NA) - if (SD_PinNames.pin_cdir != NC) { - SD_TypeDef *sd_d123dir = pinmap_peripheral(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); - if (pinmap_merge_peripheral(sd_d123dir, sd_base) == NP) { - core_debug("ERROR: SD D123DIR pin mismatch\n"); - return MSD_ERROR; + if (res && (SD_PinNames.pin_cdir != NC)) { + SD_TypeDef *sd_d123dir = pinmap_peripheral(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); + if (pinmap_merge_peripheral(sd_d123dir, sd_base) == NP) { + core_debug("ERROR: SD D123DIR pin mismatch\n"); + res = false; + } } - } #endif #endif /* SDMMC1 || SDMMC2 */ - /* Are all pins connected to the same SDx instance? */ - if (uSdHandle.Instance == NP) { - core_debug("ERROR: SD pins mismatch\n"); - return MSD_ERROR; + /* Are all pins connected to the same SDx instance? */ + if (uSdHandle.Instance == NP) { + core_debug("ERROR: SD pins mismatch\n"); + res = false; + } } - return MSD_OK; + return res; } #endif /* STM32_CORE_VERSION && (STM32_CORE_VERSION > 0x02050000) */ @@ -279,8 +280,8 @@ uint8_t BSP_SD_Init(void) #if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x02050000) uSdHandle.Instance = SD_INSTANCE; #else - if (BSP_SD_GetInstance() == MSD_ERROR) { - return MSD_ERROR; + if (!BSP_SD_GetInstance()) { + sd_state = MSD_ERROR; } #endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */ @@ -298,32 +299,33 @@ uint8_t BSP_SD_Init(void) #else uSdHandle.Init.TranceiverPresent = SD_TRANSCEIVER_ENABLE; #endif - BSP_SD_Transceiver_MspInit(&uSdHandle, NULL); + if (sd_state == MSD_OK) { + BSP_SD_Transceiver_MspInit(&uSdHandle, NULL); + } #endif - if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) { + if ((sd_state == MSD_OK) && (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL)) { /* Msp SD Detect pin initialization */ BSP_SD_Detect_MspInit(&uSdHandle, NULL); if (BSP_SD_IsDetected() != SD_PRESENT) { /* Check if SD card is present */ - return MSD_ERROR_SD_NOT_PRESENT; + sd_state = 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, SD_BUS_WIDE) != HAL_OK) { + /* Msp SD initialization */ + BSP_SD_MspInit(&uSdHandle, NULL); + + /* HAL SD initialization */ + if (HAL_SD_Init(&uSdHandle) != HAL_OK) { sd_state = MSD_ERROR; - } else { - sd_state = MSD_OK; + } + + /* Configure SD Bus width */ + if (sd_state == MSD_OK) { + /* Enable wide operation */ + if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SD_BUS_WIDE) != HAL_OK) { + sd_state = MSD_ERROR; + } } } } @@ -341,29 +343,27 @@ uint8_t BSP_SD_DeInit(void) #if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x02050000) uSdHandle.Instance = SD_INSTANCE; #else - if (BSP_SD_GetInstance() == MSD_ERROR) { - return MSD_ERROR; - } -#endif - - /* HAL SD deinitialization */ - if (HAL_SD_DeInit(&uSdHandle) != HAL_OK) { + if (!BSP_SD_GetInstance()) { sd_state = MSD_ERROR; - } - - /* Msp SD deinitialization */ - BSP_SD_MspDeInit(&uSdHandle, NULL); + } else +#endif + { + /* HAL SD deinitialization */ + if (HAL_SD_DeInit(&uSdHandle) != HAL_OK) { + sd_state = MSD_ERROR; + } + /* Msp SD deinitialization */ + BSP_SD_MspDeInit(&uSdHandle, NULL); - if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) { - BSP_SD_Detect_MspDeInit(&uSdHandle, NULL); - } + if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) { + BSP_SD_Detect_MspDeInit(&uSdHandle, NULL); + } #if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U) - BSP_SD_Transceiver_MspDeInit(&uSdHandle, NULL); + BSP_SD_Transceiver_MspDeInit(&uSdHandle, NULL); #endif - - - return sd_state; + } + return sd_state; } #if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U) @@ -377,14 +377,16 @@ uint8_t BSP_SD_DeInit(void) */ uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef *selport, uint32_t selpin) { + uint8_t sd_state = MSD_OK; if ((enport != 0) && (selport != 0)) { SD_trans_en_ll_gpio_pin = enpin; SD_trans_en_gpio_port = enport; SD_trans_sel_ll_gpio_pin = selpin; SD_trans_sel_gpio_port = selport; - return MSD_OK; + } else { + sd_state = MSD_ERROR; } - return MSD_ERROR; + return sd_state; } #endif @@ -396,15 +398,17 @@ uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef */ uint8_t BSP_SD_DetectPin(PinName p, uint32_t level) { + uint8_t sd_state = MSD_OK; GPIO_TypeDef *port = set_GPIO_Port_Clock(STM_PORT(p)); uint32_t pin = STM_LL_GPIO_PIN(p); if (port != 0) { SD_detect_ll_gpio_pin = pin; SD_detect_gpio_port = port; SD_detect_level = level; - return MSD_OK; + } else { + sd_state = MSD_ERROR; } - return MSD_ERROR; + return sd_state; } /** @@ -427,11 +431,7 @@ uint8_t BSP_SD_IsDetected(void) */ uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) { - if (HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK) { - return MSD_ERROR; - } else { - return MSD_OK; - } + return (HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK) ? MSD_ERROR : MSD_OK; } /** @@ -444,11 +444,7 @@ uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBloc */ uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) { - if (HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK) { - return MSD_ERROR; - } else { - return MSD_OK; - } + return (HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK) ? MSD_ERROR : MSD_OK; } /** @@ -459,11 +455,7 @@ uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBl */ uint8_t BSP_SD_Erase(uint64_t StartAddr, uint64_t EndAddr) { - if (HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) != HAL_OK) { - return MSD_ERROR; - } else { - return MSD_OK; - } + return (HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) != HAL_OK) ? MSD_ERROR : MSD_OK; } /** @@ -725,11 +717,12 @@ uint8_t BSP_SD_GetCardState(void) /** * @brief Get SD information about specific SD card. * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure + * @retval boolean true if successful, false otherwise */ -void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo) +bool BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo) { /* Get SD card Information */ - HAL_SD_GetCardInfo(&uSdHandle, CardInfo); + return (HAL_SD_GetCardInfo(&uSdHandle, CardInfo) == HAL_OK); } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/src/bsp_sd.h b/src/bsp_sd.h index 3828ee6..d75c2b9 100644 --- a/src/bsp_sd.h +++ b/src/bsp_sd.h @@ -58,12 +58,12 @@ Please update the core or install previous library version." #define USE_SD_TRANSCEIVER 1 #endif -/*SD Card information structure */ +/* SD Card information structure */ #define BSP_SD_CardInfo HAL_SD_CardInfoTypeDef /* For backward compatibility */ #define SD_CardInfo BSP_SD_CardInfo -/*SD status structure definition */ +/* SD status structure definition */ #define MSD_OK ((uint8_t)0x00) #define MSD_ERROR ((uint8_t)0x01) #define MSD_ERROR_SD_NOT_PRESENT ((uint8_t)0x02) @@ -179,7 +179,7 @@ uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBloc uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout); uint8_t BSP_SD_Erase(uint64_t StartAddr, uint64_t EndAddr); uint8_t BSP_SD_GetCardState(void); -void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo); +bool BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo); uint8_t BSP_SD_IsDetected(void); /* These __weak function can be surcharged by application code in case the current settings (e.g. DMA stream)