2 * Copyright (c) 2016, Texas Instruments Incorporated
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /** ============================================================================
36 * @brief SD driver interface
38 * The SD header file should be included in an application as follows:
40 * #include <ti/drivers/SD.h>
45 * The SD driver is designed to serve as an interface to perform basic
46 * transfers directly to the SD card.
48 * ## Opening the driver #
53 * handle = SD_open(index, NULL);
54 * if (handle == NULL) {
55 * System_printf("Error opening SD driver\n");
61 * This module serves as the main interface for TI-RTOS applications. Its
62 * purpose is to redirect the module's APIs to specific peripheral
63 * implementations which are specified using a pointer to a
66 * The SD driver interface module is joined (at link time) to a
67 * NULL-terminated array of SD_Config data structures named *SD_config*.
68 * *SD_config* is implemented in the application with each entry being an
69 * instance of a SD peripheral. Each entry in *SD_config* contains a:
70 * - (SD_FxnTable *) to a set of functions that implement a SD peripheral
71 * - (uintptr_t) data object that is associated with the SD_FxnTable
72 * - (uintptr_t) hardware attributes that are associated to the SD_FxnTable
76 * The SD driver interface produces log statements if
77 * instrumentation is enabled.
79 * Diagnostics Mask | Log details |
80 * ---------------- | ----------- |
81 * Diags_USER1 | basic operations performed |
82 * Diags_USER2 | detailed operations performed |
84 * ============================================================================
87 #ifndef ti_drivers_SD__include
88 #define ti_drivers_SD__include
97 * @defgroup SD_CONTROL SD_control command and status codes
102 * Common SD_control command code reservation offset.
103 * SD driver implementations should offset command codes with
104 * SD_CMD_RESERVED growing positively.
106 * Example implementation specific command codes:
108 * #define SDXYZ_CMD_COMMAND0 (SD_CMD_RESERVED + 0)
109 * #define SDXYZ_CMD_COMMAND1 (SD_CMD_RESERVED + 1)
112 #define SD_CMD_RESERVED (32)
115 * Common SD_control status code reservation offset.
116 * SD driver implementations should offset status codes with
117 * SD_STATUS_RESERVED growing negatively.
119 * Example implementation specific status codes:
121 * #define SDXYZ_STATUS_ERROR0 (SD_STATUS_RESERVED - 0)
122 * #define SDXYZ_STATUS_ERROR1 (SD_STATUS_RESERVED - 1)
123 * #define SDXYZ_STATUS_ERROR2 (SD_STATUS_RESERVED - 2)
126 #define SD_STATUS_RESERVED (-32)
129 * @defgroup SD_STATUS Status Codes
130 * SD_STATUS_* macros are general status codes returned by SD_control()
132 * @ingroup SD_CONTROL
136 * @brief Successful status code returned by SD_control().
138 * SD_control() returns SD_STATUS_SUCCESS if the control code was executed
141 #define SD_STATUS_SUCCESS (0)
144 * @brief Generic error status code returned by SD_control().
146 * SD_control() returns SD_STATUS_ERROR if the control code
147 * was not executed successfully.
149 #define SD_STATUS_ERROR (-1)
152 * @brief An error status code returned by SD_control() for
153 * undefined command codes.
155 * SD_control() returns SD_STATUS_UNDEFINEDCMD if the
156 * control code is not recognized by the driver implementation.
158 #define SD_STATUS_UNDEFINEDCMD (-2)
162 * @defgroup SD_CMD Command Codes
163 * SD_CMD_* macros are general command codes for SD_control(). Not all SD
164 * driver implementations support these command codes.
166 * @ingroup SD_CONTROL
169 /* Add SD_CMD_<commands> here */
176 * @brief SD Card type inserted
178 typedef enum SD_CardType_ {
179 SD_NOCARD = 0, /*!< Unrecognized Card */
180 SD_MMC = 1, /*!< Multi-media Memory Card (MMC) */
181 SD_SDSC = 2, /*!< Standard SDCard (SDSC) */
182 SD_SDHC = 3 /*!< High Capacity SDCard (SDHC) */
186 * @brief A handle that is returned from a SD_open() call.
188 typedef struct SD_Config_ *SD_Handle;
191 * @brief SD Parameters
193 * SD Parameters are used to with the SD_open() call.
194 * Default values for these parameters are set using SD_Params_init().
196 * @sa SD_Params_init()
200 typedef struct SD_Params_ {
201 void *custom; /*!< Custom argument used by driver implementation */
205 * @brief A function pointer to a driver specific implementation of
208 typedef void (*SD_CloseFxn) (SD_Handle handle);
211 * @brief A function pointer to a driver specific implementation of
214 typedef int_fast16_t (*SD_ControlFxn) (SD_Handle handle,
215 uint_fast16_t cmd, void *arg);
218 * @brief A function pointer to a driver specific implementation of
219 * SD_getNumSectorsFxn().
221 typedef uint_fast32_t (*SD_getNumSectorsFxn) (SD_Handle handle);
224 * @brief A function pointer to a driver specific implementation of
225 * SD_getSectorSizeFxn().
227 typedef uint_fast32_t (*SD_getSectorSizeFxn) (void);
230 * @brief A function pointer to a driver specific implementation of
233 typedef void (*SD_InitFxn) (SD_Handle handle);
236 * @brief A function pointer to a driver specific implementation of
237 * SD_initializeFxn().
239 typedef int_fast16_t (*SD_InitializeFxn) (SD_Handle handle);
242 * @brief A function pointer to a driver specific implementation of
245 typedef SD_Handle (*SD_OpenFxn) (SD_Handle handle, SD_Params *params);
248 * @brief A function pointer to a driver specific implementation of
251 typedef int_fast16_t (*SD_ReadFxn) (SD_Handle handle, void *buf,
252 int_fast32_t sector, uint_fast32_t secCount);
255 * @brief A function pointer to a driver specific implementation of
258 typedef int_fast16_t (*SD_WriteFxn) (SD_Handle handle, const void *buf,
259 int_fast32_t sector, uint_fast32_t secCount);
262 * @brief The definition of a SD function table that contains the
263 * required set of functions to control a specific SD driver
266 typedef struct SD_FxnTable_ {
267 /*! Function to close the specified peripheral */
268 SD_CloseFxn closeFxn;
269 /*! Function to implementation specific control function */
270 SD_ControlFxn controlFxn;
271 /*! Function to return the total number of sectors on the SD card */
272 SD_getNumSectorsFxn getNumSectorsFxn;
273 /*! Function to return the sector size used to address the SD card */
274 SD_getSectorSizeFxn getSectorSizeFxn;
275 /*! Function to initialize the given data object */
277 /*! Function to initialize the SD card */
278 SD_InitializeFxn initializeFxn;
279 /*! Function to open the specified peripheral */
281 /*! Function to read from the SD card */
283 /*! Function to write to the SD card */
284 SD_WriteFxn writeFxn;
288 * @brief SD Global configuration
290 * The SD_Config structure contains a set of pointers used
291 * to characterize the SD driver implementation.
293 * This structure needs to be defined before calling SD_init() and it must
294 * not be changed thereafter.
298 typedef struct SD_Config_ {
299 /*! Pointer to a table of driver-specific implementations of SD APIs */
300 SD_FxnTable const *fxnTablePtr;
302 /*! Pointer to a driver specific data object */
305 /*! Pointer to a driver specific hardware attributes structure */
310 * @brief Function to close a SD peripheral specified by the SD handle.
312 * @pre SD_open() had to be called first.
314 * @param handle A SD handle returned from SD_open
318 extern void SD_close(SD_Handle handle);
321 * @brief Function performs implementation specific features on a given
324 * Commands for SD_control can originate from SD.h or from implementation
325 * specific SD*.h (SDHostCC32XX.h etc.. ) files.
326 * While commands from SD.h are API portable across driver implementations,
327 * not all implementations may support all these commands.
328 * Conversely, commands from driver implementation specific SD*.h files add
329 * unique driver capabilities but are not API portable across all SD driver
332 * Commands supported by SD.h follow a SD*_CMD naming
335 * Commands supported by SD*.h follow a SD*_CMD naming
337 * Each control command defines arg differently. The types of arg are
338 * documented with each command.
340 * See @ref SD_CMD "SD_control command codes" for command codes.
342 * See @ref SD_STATUS "SD_control return status codes" for status codes.
344 * @pre SD_open() has to be called first.
346 * @param handle A SD handle returned from SD_open().
348 * @param cmd SD.h or SD*.h commands.
350 * @param arg An optional R/W (read/write) command argument
351 * accompanied with cmd.
353 * @return Implementation specific return codes. Negative values indicate
354 * unsuccessful operations.
358 extern int_fast16_t SD_control(SD_Handle handle, uint_fast16_t cmd, void *arg);
361 * @brief A function pointer to a driver specific implementation of
362 * SD_getNumSectors().
363 * Note: Total Card capacity is the (NumberOfSectors * SectorSize).
365 * @pre SD Card has been initialized using SD_initialize().
367 * @param handle A SD handle returned from SD_open().
369 * @return The total number of sectors on the SD card,
370 * or 0 if an error occurred.
372 * @sa SD_initialize()
374 extern uint_fast32_t SD_getNumSectors(SD_Handle handle);
377 * @brief Function to obtain the sector size used to access the SD card.
379 * @pre SD Card has been initialized using SD_initialize().
381 * @param handle A SD handle returned from SD_open().
383 * @return The sector size set for use during SD card read/write operations.
385 * @sa SD_initialize()
387 extern uint_fast32_t SD_getSectorSize(SD_Handle handle);
390 * @brief This function initializes the SD driver.
392 * @pre The SD_config structure must exist and be persistent before this
393 * function can be called. This function must also be called before
394 * any other SD driver APIs. This function call does not modify any
395 * peripheral registers.
397 extern void SD_init(void);
400 * @brief Function to initialize the SD_Params struct to its defaults.
402 * @param params A pointer to SD_Params structure for initialization.
404 extern void SD_Params_init(SD_Params *params);
407 * @brief A function pointer to a driver specific implementation of
410 * @pre SD controller has been opened by calling SD_open().
412 * @param handle A SD handle returned from SD_open().
414 * @return SD_STATUS_SUCCESS if no errors occurred during the initialization,
415 * SD_STATUS_ERROR otherwise.
417 extern int_fast16_t SD_initialize(SD_Handle handle);
420 * @brief A function pointer to a driver specific implementation of
423 * @pre SD controller has been initialized using SD_init().
425 * @param index Logical peripheral number for the SD indexed into
426 * the SD_config table.
428 * @param params Pointer to a parameter block, if NULL it will use
429 * default values. All the fields in this structure are
432 * @return A SD_Handle on success or a NULL on an error or if it has been
438 extern SD_Handle SD_open(uint_least8_t index, SD_Params *params);
441 * @brief A function pointer to a driver specific implementation of
444 * @pre SD controller has been opened and initialized by calling SD_open()
445 * followed by SD_initialize().
447 * @param handle A SD handle returned from SD_open().
449 * @param buf Pointer to a buffer to read data into.
451 * @param sector Starting sector on the disk to read from.
453 * @param secCount Number of sectors to be read.
455 * @return SD_STATUS_SUCCESS if no errors occurred during the write,
456 * SD_STATUS_ERROR otherwise.
458 * @sa SD_initialize()
460 extern int_fast16_t SD_read(SD_Handle handle, void *buf,
461 int_fast32_t sector, uint_fast32_t secCount);
464 * @brief A function pointer to a driver specific implementation of
467 * @pre SD controller has been opened and initialized by calling SD_open()
468 * followed by SD_initialize().
470 * @param handle A SD handle returned from SD_open().
472 * @param buf Pointer to a buffer containing data to write to disk.
474 * @param sector Starting sector on the disk to write to.
476 * @param secCount Number of sectors to be written.
478 * @return SD_STATUS_SUCCESS if no errors occurred during the write,
479 * SD_STATUS_ERROR otherwise.
481 * @sa SD_initialize()
483 extern int_fast16_t SD_write(SD_Handle handle, const void *buf,
484 int_fast32_t sector, uint_fast32_t secCount);
490 #endif /* ti_drivers_SD__include */