1 /*
2 * Copyright (c) 2016-2017, 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 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36
37 #include <ti/drivers/dpl/HwiP.h>
38 #include <ti/drivers/SD.h>
39
40 extern const SD_Config SD_config[];
41 extern const uint_least8_t SD_count;
42
43 /* Default SD parameters structure */
44 const SD_Params SD_defaultParams = {
45 NULL /* custom */
46 };
47
48 static bool isInitialized = false;
49
50 /*
51 * ======== SD_close ========
52 */
SD_close(SD_Handle handle)53 void SD_close(SD_Handle handle)
54 {
55 handle->fxnTablePtr->closeFxn(handle);
56 }
57
58 /*
59 * ======== SD_control ========
60 */
SD_control(SD_Handle handle,uint_fast16_t cmd,void * arg)61 int_fast16_t SD_control(SD_Handle handle, uint_fast16_t cmd, void *arg)
62 {
63 return (handle->fxnTablePtr->controlFxn(handle, cmd, arg));
64 }
65
66 /*
67 * ======== SD_getNumSectors ========
68 */
SD_getNumSectors(SD_Handle handle)69 uint_fast32_t SD_getNumSectors(SD_Handle handle)
70 {
71 return (handle->fxnTablePtr->getNumSectorsFxn(handle));
72 }
73
74 /*
75 * ======== SD_getSectorSize ========
76 */
SD_getSectorSize(SD_Handle handle)77 uint_fast32_t SD_getSectorSize(SD_Handle handle)
78 {
79 return (handle->fxnTablePtr->getSectorSizeFxn(handle));
80 }
81
82 /*
83 * ======== SD_init ========
84 */
SD_init(void)85 void SD_init(void)
86 {
87 uint_least8_t i;
88 uint_fast32_t key;
89
90 key = HwiP_disable();
91
92 if (!isInitialized) {
93 isInitialized = (bool) true;
94
95 /* Call each driver's init function */
96 for (i = 0; i < SD_count; i++) {
97 SD_config[i].fxnTablePtr->initFxn((SD_Handle)&(SD_config[i]));
98 }
99 }
100
101 HwiP_restore(key);
102 }
103
104 /*
105 * ======== SD_initialize ========
106 */
SD_initialize(SD_Handle handle)107 int_fast16_t SD_initialize(SD_Handle handle)
108 {
109 return (handle->fxnTablePtr->initializeFxn(handle));
110 }
111
112 /*
113 * ======== SD_open ========
114 */
SD_open(uint_least8_t index,SD_Params * params)115 SD_Handle SD_open(uint_least8_t index, SD_Params *params)
116 {
117 SD_Handle handle = NULL;
118
119 /* Verify driver index and state */
120 if (isInitialized && (index < SD_count)) {
121 /* If params are NULL use defaults */
122 if (params == NULL) {
123 params = (SD_Params *) &SD_defaultParams;
124 }
125
126 /* Get handle for this driver instance */
127 handle = (SD_Handle)&(SD_config[index]);
128 handle = handle->fxnTablePtr->openFxn(handle, params);
129 }
130
131 return (handle);
132 }
133
134 /*
135 * ======== SD_Params_init ========
136 */
SD_Params_init(SD_Params * params)137 void SD_Params_init(SD_Params *params)
138 {
139 *params = SD_defaultParams;
140 }
141
142 /*
143 * ======== SD_read ========
144 */
SD_read(SD_Handle handle,void * buf,int_fast32_t sector,uint_fast32_t secCount)145 int_fast16_t SD_read(SD_Handle handle, void *buf,
146 int_fast32_t sector, uint_fast32_t secCount)
147 {
148 return (handle->fxnTablePtr->readFxn(handle, buf, sector, secCount));
149 }
150
151 /*
152 * ======== SD_write ========
153 */
SD_write(SD_Handle handle,const void * buf,int_fast32_t sector,uint_fast32_t secCount)154 int_fast16_t SD_write(SD_Handle handle, const void *buf,
155 int_fast32_t sector, uint_fast32_t secCount)
156 {
157 return (handle->fxnTablePtr->writeFxn(handle, buf, sector, secCount));
158 }
159