1 /* 2 * Copyright 2022 NXP 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * Common utility functions for SD subsystem 9 */ 10 11 #ifndef ZEPHYR_SUBSYS_SD_UTILS_H_ 12 #define ZEPHYR_SUBSYS_SD_UTILS_H_ 13 14 #include <zephyr/kernel.h> 15 #include <zephyr/sd/sd.h> 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 /** 22 * Custom SD return codes. Used internally to indicate conditions that may 23 * not be errors, but are abnormal return conditions 24 */ 25 enum sd_return_codes { 26 SD_RETRY = 1, 27 SD_NOT_SDIO = 2, 28 SD_RESTART = 3, 29 }; 30 31 /* Checks SD status return codes */ sd_check_response(struct sdhc_command * cmd)32static inline int sd_check_response(struct sdhc_command *cmd) 33 { 34 if (cmd->response_type == SD_RSP_TYPE_R1) { 35 return (cmd->response[0U] & SD_R1_ERR_FLAGS); 36 } 37 return 0; 38 } 39 40 /* Delay function for SD subsystem */ sd_delay(unsigned int millis)41static inline void sd_delay(unsigned int millis) 42 { 43 k_msleep(millis); 44 } 45 46 /* 47 * Helper function to retry sending command to SD card 48 * Will retry command if return code equals SD_RETRY 49 */ sd_retry(int (* cmd)(struct sd_card * card),struct sd_card * card,int retries)50static inline int sd_retry(int(*cmd)(struct sd_card *card), 51 struct sd_card *card, 52 int retries) 53 { 54 int ret = -ETIMEDOUT; 55 56 while (retries-- >= 0) { 57 /* Try cmd */ 58 ret = cmd(card); 59 /** 60 * Functions have 3 possible responses: 61 * 0: success 62 * SD_RETRY: retry command 63 * other: does not retry 64 */ 65 if (ret != SD_RETRY) { 66 break; 67 } 68 } 69 return ret == SD_RETRY ? -ETIMEDOUT : ret; 70 } 71 72 #ifdef __cplusplus 73 } 74 #endif 75 76 #endif /* ZEPHYR_SUBSYS_SD_UTILS_H_ */ 77