SDSPI.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32 /*!*****************************************************************************
33  * @file SDSPI.h
34  *
35  * @brief SDSPI driver interface
36  *
37  * The SDSPI header file should be included in an application as follows:
38  * @code
39  * #include <ti/drivers/SDSPI.h>
40  * @endcode
41  *
42  * # Overview #
43  * The SDSPI FatFs driver is used to communicate with SD (Secure Digital)
44  * cards via SPI (Serial Peripheral Interface).
45  *
46  * The SDSPI driver is a FatFs driver module for the FatFs middleware
47  * module. With the exception of the standard driver APIs -
48  * SDSPI_open(), SDSPI_close(), and SDSPI_init() - the SDSPI driver
49  * is exclusively used by FatFs module to handle the low-level hardware
50  * communications.
51  *
52  * The SDSPI driver only supports one SSI (SPI) peripheral at a given time.
53  * It does not utilize interrupts.
54  *
55  * The SDSPI driver is polling based for performance reasons and due to the
56  * relatively high SPI bus bit rate. This means it does not utilize the
57  * SPI's peripheral interrupts, and it consumes the entire CPU time when
58  * communicating with the SPI bus. Data transfers to or from the SD card
59  * are typically 512 bytes, which could take a significant amount of time
60  * to complete. During this time, only higher priority Tasks, Swis, and
61  * Hwis can preempt Tasks making calls that use the FatFs.
62  *
63  * # Usage #
64  * Before any FatFs or C I/O APIs can be used, the application needs to
65  * open the SDSPI driver. The SDSPI_open() function ensures that the SDSPI
66  * disk functions get registered with the FatFs module that subsequently
67  * mounts the FatFs volume to that particular drive.
68  *
69  * @code
70  * SDSPI_Handle sdspiHandle;
71  * SDSPI_Params sdspiParams;
72  * UInt peripheralNum = 0;
73  * UInt FatFsDriveNum = 0;
74  *
75  * SDSPI_Params_init(&sdspiParams);
76  *
77  * sdspiHandle = SDSPI_open(peripheralNum, FatFsDriveNum, &sdspiParams);
78  * if (sdspiHandle == NULL) {
79  * System_abort("Error opening SDSPI\n");
80  * }
81  * @endcode
82  *
83  * Similarly, the SDSPI_close() function unmounts the FatFs volume and
84  * unregisters SDSPI disk functions.
85  *
86  * @code
87  * SDSPI_close(sdspiHandle);
88  * @endcode
89  *
90  * Note that it is up to the application to ensure the no FatFs or C I/O
91  * APIs are called before the SDSPI driver has been opened or after the
92  * SDSPI driver has been closed.
93  *
94  * ### SDSPI Driver Configuration #
95  * The SDSPI driver requires the application to initialize board-specific
96  * portions of the SDSPI and provide the SDSPI driver with the SDSPI_config
97  * structure.
98  *
99  * #### Board-Specific Configuration #
100  *
101  * The SDSPI_init() initializes the SDSPI driver snd any board-specific
102  * SDSPI peripheral settings.
103  *
104  * #### SDSPI_config Structure #
105  *
106  * The <*board*>.c file declares the SDSPI_config structure. This
107  * structure must be provided to the SDSPI driver. It must be initialized
108  * before the SDSPI_init() function is called and cannot be changed
109  * afterwards.
110  *
111  * The SDSPI driver interface defines a configuration data structure:
112  *
113  * @code
114  * typedef struct SDSPI_Config_ {
115  * SDSPI_FxnTable const *fxnTablePtr;
116  * void *object;
117  * void const *hwAttrs;
118  * } SDSPI_Config;
119  * @endcode
120  *
121  * # Operation #
122  *
123  * The SDSPI driver is a driver designed to hook into FatFs. It implements a
124  * set of functions that FatFs needs to call to perform basic block data
125  * transfers.
126  *
127  * A SDSPI driver peripheral implementation doesn't require RTOS protection
128  * primitives due to the resource protection provided with FatFs. The only
129  * functions that can be called by the application are the standard driver
130  * framework functions (_open, _close, etc...).
131  *
132  * Once the driver has been opened, the application may used the FatFs APIs or
133  * the standard C runtime file I/O calls (fopen, fclose, etc...). Once the
134  * driver has been closed, ensure the application does NOT make any file I/O
135  * calls.
136  *
137  * ### Opening the SDSPI driver #
138  *
139  * @code
140  * SDSPI_Handle handle;
141  * SDSPI_Params params;
142  *
143  * SDSPI_Params_init(&params);
144  * params.bitRate = someNewBitRate;
145  * handle = SDSPI_open(someSDSPI_configIndexValue, &params);
146  * if (!handle) {
147  * System_printf("SDSPI did not open");
148  * }
149  * @endcode
150  *
151  * # Implementation #
152  *
153  * The SDSPI driver interface module is joined (at link time) to an
154  * array of SDSPI_Config data structures named *SDSPI_config*.
155  * SDSPI_config is implemented in the application with each entry being an
156  * instance of a SDSPI peripheral. Each entry in *SDSPI_config* contains a:
157  * - (SDSPI_FxnTable *) to a set of functions that implement a SDSPI peripheral
158  * - (void *) data object that is associated with the SDSPI_FxnTable
159  * - (void *) hardware attributes that are associated with the SDSPI_FxnTable
160  *
161  * The SDSPI APIs are redirected to the device specific implementations
162  * using the SDSPI_FxnTable pointer of the SDSPI_config entry.
163  * In order to use device specific functions of the SDSPI driver directly,
164  * link in the correct driver library for your device and include the
165  * device specific SDSPI driver header file (which in turn includes SDSPI.h).
166  * For example, for the MSP432 family of devices, you would include the
167  * following header file:
168  * @code
169  * #include <ti/drivers/sdspi/SDSPIMSP432.h>
170  * @endcode
171  *
172  *******************************************************************************
173  */
174 
175 #ifndef ti_drivers_SDSPI__include
176 #define ti_drivers_SDSPI__include
177 
178 #ifdef __cplusplus
179 extern "C" {
180 #endif
181 
182 #include <stdint.h>
183 
201 #define SDSPI_CMD_RESERVED (32)
202 
215 #define SDSPI_STATUS_RESERVED (-32)
216 
230 #define SDSPI_STATUS_SUCCESS (0)
231 
238 #define SDSPI_STATUS_ERROR (-1)
239 
247 #define SDSPI_STATUS_UNDEFINEDCMD (-2)
248 
258 /* Add SDSPI_CMD_<commands> here */
259 
267 typedef struct SDSPI_Config_ *SDSPI_Handle;
268 
269 
278 typedef struct SDSPI_Params_ {
279  uint32_t bitRate;
280  void *custom;
281 } SDSPI_Params;
282 
287 typedef void (*SDSPI_InitFxn) (SDSPI_Handle handle);
288 
293 typedef SDSPI_Handle (*SDSPI_OpenFxn) (SDSPI_Handle handle,
294  uint_least8_t drv,
295  SDSPI_Params *params);
296 
301 typedef void (*SDSPI_CloseFxn) (SDSPI_Handle handle);
302 
307 typedef int_fast16_t (*SDSPI_ControlFxn) (SDSPI_Handle handle,
308  uint_fast16_t cmd,
309  void *arg);
310 
316 typedef struct SDSPI_FxnTable_ {
319 
322 
325 
329 
341 typedef struct SDSPI_Config_ {
344 
346  void *object;
347 
349  void const *hwAttrs;
350 } SDSPI_Config;
351 
363 extern void SDSPI_close(SDSPI_Handle handle);
364 
402 extern int_fast16_t SDSPI_control(SDSPI_Handle handle, uint_fast16_t cmd,
403  void *arg);
404 
413 extern void SDSPI_init(void);
414 
437 extern SDSPI_Handle SDSPI_open(uint_least8_t index, uint_least8_t drv,
438  SDSPI_Params *params);
439 
449 extern void SDSPI_Params_init(SDSPI_Params *params);
450 
451 #ifdef __cplusplus
452 }
453 #endif
454 
455 #endif /* ti_drivers_SDSPI__include */
SDSPI Parameters.
Definition: SDSPI.h:278
struct SDSPI_FxnTable_ SDSPI_FxnTable
The definition of a SDSPI function table that contains the required set of functions to control a spe...
struct SDSPI_Config_ SDSPI_Config
SDSPI Global configuration.
void * object
Definition: SDSPI.h:346
SDSPI_InitFxn initFxn
Definition: SDSPI.h:318
struct SDSPI_Params_ SDSPI_Params
SDSPI Parameters.
SDSPI_FxnTable const * fxnTablePtr
Definition: SDSPI.h:343
void(* SDSPI_InitFxn)(SDSPI_Handle handle)
A function pointer to a driver specific implementation of SDSPI_init().
Definition: SDSPI.h:287
SDSPI Global configuration.
Definition: SDSPI.h:341
SDSPI_Handle SDSPI_open(uint_least8_t index, uint_least8_t drv, SDSPI_Params *params)
This function registers the SDSPI driver with BIOS&#39; FatFs module and mounts the FatFs file system...
void * custom
Definition: SDSPI.h:280
struct SDSPI_Config_ * SDSPI_Handle
A handle that is returned from a SDSPI_open() call.
Definition: SDSPI.h:267
SDSPI_CloseFxn closeFxn
Definition: SDSPI.h:324
int_fast16_t SDSPI_control(SDSPI_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given SDSPI_Handle.
void SDSPI_init(void)
This function initializes the SDSPI driver module.
int_fast16_t(* SDSPI_ControlFxn)(SDSPI_Handle handle, uint_fast16_t cmd, void *arg)
A function pointer to a driver specific implementation of SDSPI_control().
Definition: SDSPI.h:307
The definition of a SDSPI function table that contains the required set of functions to control a spe...
Definition: SDSPI.h:316
void const * hwAttrs
Definition: SDSPI.h:349
SDSPI_ControlFxn controlFxn
Definition: SDSPI.h:327
void(* SDSPI_CloseFxn)(SDSPI_Handle handle)
A function pointer to a driver specific implementation of SDSPI_close().
Definition: SDSPI.h:301
void SDSPI_Params_init(SDSPI_Params *params)
Function to initialize the SDSPI_Params struct to its defaults.
uint32_t bitRate
Definition: SDSPI.h:279
SDSPI_OpenFxn openFxn
Definition: SDSPI.h:321
void SDSPI_close(SDSPI_Handle handle)
Function to close a SDSPI peripheral specified by the SDSPI handle. This function unmounts the file s...
SDSPI_Handle(* SDSPI_OpenFxn)(SDSPI_Handle handle, uint_least8_t drv, SDSPI_Params *params)
A function pointer to a driver specific implementation of SDSPI_open().
Definition: SDSPI.h:293
Copyright 2017, Texas Instruments Incorporated