ChibiOS/HAL  6.1.0
hal_sdc.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_sdc.h
19  * @brief SDC Driver macros and structures.
20  *
21  * @addtogroup SDC
22  * @{
23  */
24 
25 #ifndef HAL_SDC_H
26 #define HAL_SDC_H
27 
28 #if (HAL_USE_SDC == TRUE) || defined(__DOXYGEN__)
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 /**
35  * @name SD card types
36  * @{
37  */
38 #define SDC_MODE_CARDTYPE_MASK 0xFU
39 #define SDC_MODE_CARDTYPE_SDV11 0U
40 #define SDC_MODE_CARDTYPE_SDV20 1U
41 #define SDC_MODE_CARDTYPE_MMC 2U
42 #define SDC_MODE_HIGH_CAPACITY 0x10U
43 /** @} */
44 
45 /**
46  * @name SDC bus error conditions
47  * @{
48  */
49 #define SDC_NO_ERROR 0U
50 #define SDC_CMD_CRC_ERROR 1U
51 #define SDC_DATA_CRC_ERROR 2U
52 #define SDC_DATA_TIMEOUT 4U
53 #define SDC_COMMAND_TIMEOUT 8U
54 #define SDC_TX_UNDERRUN 16U
55 #define SDC_RX_OVERRUN 32U
56 #define SDC_STARTBIT_ERROR 64U
57 #define SDC_OVERFLOW_ERROR 128U
58 #define SDC_UNHANDLED_ERROR 0xFFFFFFFFU
59 /** @} */
60 
61 /*===========================================================================*/
62 /* Driver pre-compile time settings. */
63 /*===========================================================================*/
64 
65 /**
66  * @name SDC configuration options
67  * @{
68  */
69 /**
70  * @brief Number of initialization attempts before rejecting the card.
71  * @note Attempts are performed at 10mS intervals.
72  */
73 #if !defined(SDC_INIT_RETRY) || defined(__DOXYGEN__)
74 #define SDC_INIT_RETRY 100
75 #endif
76 
77 /**
78  * @brief Include support for MMC cards.
79  * @note MMC support is not yet implemented so this option must be kept
80  * at @p FALSE.
81  */
82 #if !defined(SDC_MMC_SUPPORT) || defined(__DOXYGEN__)
83 #define SDC_MMC_SUPPORT FALSE
84 #endif
85 
86 /**
87  * @brief Delays insertions.
88  * @details If enabled this options inserts delays into the MMC waiting
89  * routines releasing some extra CPU time for the threads with
90  * lower priority, this may slow down the driver a bit however.
91  */
92 #if !defined(SDC_NICE_WAITING) || defined(__DOXYGEN__)
93 #define SDC_NICE_WAITING TRUE
94 #endif
95 
96 /**
97  * @brief OCR initialization constant for V20 cards.
98  */
99 #if !defined(SDC_INIT_OCR_V20) || defined(__DOXYGEN__)
100 #define SDC_INIT_OCR_V20 0x50FF8000U
101 #endif
102 
103 /**
104  * @brief OCR initialization constant for non-V20 cards.
105  */
106 #if !defined(SDC_INIT_OCR) || defined(__DOXYGEN__)
107 #define SDC_INIT_OCR 0x80100000U
108 #endif
109 /** @} */
110 
111 /*===========================================================================*/
112 /* Derived constants and error checks. */
113 /*===========================================================================*/
114 
115 /*===========================================================================*/
116 /* Driver data structures and types. */
117 /*===========================================================================*/
118 
119 /**
120  * @brief Type of SDIO bus mode.
121  */
122 typedef enum {
123  SDC_MODE_1BIT = 0,
124  SDC_MODE_4BIT,
125  SDC_MODE_8BIT
126 } sdcbusmode_t;
127 
128 /**
129  * @brief Max supported clock.
130  */
131 typedef enum {
132  SDC_CLK_25MHz = 0,
133  SDC_CLK_50MHz
134 } sdcbusclk_t;
135 
136 #include "hal_sdc_lld.h"
137 
138 /*===========================================================================*/
139 /* Driver macros. */
140 /*===========================================================================*/
141 
142 /**
143  * @name Macro Functions
144  * @{
145  */
146 /**
147  * @brief Returns the card insertion status.
148  * @note This macro wraps a low level function named
149  * @p sdc_lld_is_card_inserted(), this function must be
150  * provided by the application because it is not part of the
151  * SDC driver.
152  *
153  * @param[in] sdcp pointer to the @p SDCDriver object
154  * @return The card state.
155  * @retval false card not inserted.
156  * @retval true card inserted.
157  *
158  * @api
159  */
160 #define sdcIsCardInserted(sdcp) (sdc_lld_is_card_inserted(sdcp))
161 
162 /**
163  * @brief Returns the write protect status.
164  * @note This macro wraps a low level function named
165  * @p sdc_lld_is_write_protected(), this function must be
166  * provided by the application because it is not part of the
167  * SDC driver.
168  *
169  * @param[in] sdcp pointer to the @p SDCDriver object
170  * @return The card state.
171  * @retval false not write protected.
172  * @retval true write protected.
173  *
174  * @api
175  */
176 #define sdcIsWriteProtected(sdcp) (sdc_lld_is_write_protected(sdcp))
177 /** @} */
178 
179 /*===========================================================================*/
180 /* External declarations. */
181 /*===========================================================================*/
182 
183 #ifdef __cplusplus
184 extern "C" {
185 #endif
186  void sdcInit(void);
187  void sdcObjectInit(SDCDriver *sdcp);
188  void sdcStart(SDCDriver *sdcp, const SDCConfig *config);
189  void sdcStop(SDCDriver *sdcp);
190  bool sdcConnect(SDCDriver *sdcp);
191  bool sdcDisconnect(SDCDriver *sdcp);
192  bool sdcRead(SDCDriver *sdcp, uint32_t startblk,
193  uint8_t *buf, uint32_t n);
194  bool sdcWrite(SDCDriver *sdcp, uint32_t startblk,
195  const uint8_t *buf, uint32_t n);
197  bool sdcSync(SDCDriver *sdcp);
198  bool sdcGetInfo(SDCDriver *sdcp, BlockDeviceInfo *bdip);
199  bool sdcErase(SDCDriver *sdcp, uint32_t startblk, uint32_t endblk);
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 #endif /* HAL_USE_SDC == TRUE */
206 
207 #endif /* HAL_SDC_H */
208 
209 /** @} */
PLATFORM SDC subsystem low level driver header.
bool _sdc_wait_for_transfer_state(SDCDriver *sdcp)
Wait for the card to complete pending operations.
Definition: hal_sdc.c:503
bool sdcConnect(SDCDriver *sdcp)
Performs the initialization procedure on the inserted card.
Definition: hal_sdc.c:623
void sdcInit(void)
SDC Driver initialization.
Definition: hal_sdc.c:542
bool sdcDisconnect(SDCDriver *sdcp)
Brings the driver in a state safe for card removal.
Definition: hal_sdc.c:762
bool sdcErase(SDCDriver *sdcp, uint32_t startblk, uint32_t endblk)
Erases the supplied blocks.
Definition: hal_sdc.c:954
bool sdcRead(SDCDriver *sdcp, uint32_t startblk, uint8_t *buf, uint32_t n)
Reads one or more blocks.
Definition: hal_sdc.c:805
Block device info.
Definition: hal_ioblock.h:55
sdcflags_t sdcGetAndClearErrors(SDCDriver *sdcp)
Returns the errors mask associated to the previous operation.
Definition: hal_sdc.c:872
void sdcObjectInit(SDCDriver *sdcp)
Initializes the standard part of a SDCDriver structure.
Definition: hal_sdc.c:554
sdcbusclk_t
Max supported clock.
Definition: hal_sdc.h:131
bool sdcSync(SDCDriver *sdcp)
Waits for card idle condition.
Definition: hal_sdc.c:896
bool sdcGetInfo(SDCDriver *sdcp, BlockDeviceInfo *bdip)
Returns the media info.
Definition: hal_sdc.c:927
sdcbusmode_t
Type of SDIO bus mode.
Definition: hal_sdc.h:122
uint32_t sdcflags_t
SDC Driver condition flags type.
Definition: hal_sdc_lld.h:68
bool sdcWrite(SDCDriver *sdcp, uint32_t startblk, const uint8_t *buf, uint32_t n)
Writes one or more blocks.
Definition: hal_sdc.c:842
Structure representing an SDC driver.
Definition: hal_sdc_lld.h:113
void sdcStart(SDCDriver *sdcp, const SDCConfig *config)
Configures and activates the SDC peripheral.
Definition: hal_sdc.c:573
void sdcStop(SDCDriver *sdcp)
Deactivates the SDC peripheral.
Definition: hal_sdc.c:593
Driver configuration structure.
Definition: hal_sdc_lld.h:79