forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsd_utils.h
67 lines (57 loc) · 1.17 KB
/
sd_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright 2022 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Common utility functions for SD subsystem
*/
#ifndef ZEPHYR_SUBSYS_SD_UTILS_H_
#define ZEPHYR_SUBSYS_SD_UTILS_H_
#include <zephyr/kernel.h>
#include <zephyr/sd/sd.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Custom SD return codes. Used internally to indicate conditions that may
* not be errors, but are abnormal return conditions
*/
enum sd_return_codes {
SD_RETRY = 1,
SD_NOT_SDIO = 2,
SD_RESTART = 3,
};
/* Delay function for SD subsystem */
static inline void sd_delay(unsigned int millis)
{
k_msleep(millis);
}
/*
* Helper function to retry sending command to SD card
* Will retry command if return code equals SD_RETRY
*/
static inline int sd_retry(int(*cmd)(struct sd_card *card),
struct sd_card *card,
int retries)
{
int ret = -ETIMEDOUT;
while (retries-- >= 0) {
/* Try cmd */
ret = cmd(card);
/**
* Functions have 3 possible responses:
* 0: success
* SD_RETRY: retry command
* other: does not retry
*/
if (ret != SD_RETRY) {
break;
}
}
return ret == SD_RETRY ? -ETIMEDOUT : ret;
}
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_SUBSYS_SD_UTILS_H_ */