1
2 #include "Driver_MCI.h"
3
4
5 /* Usage example: ARM_MCI_Initialize ----------------------------------------*/
6
7 // ARM_MCI_SignalEvent callback function prototype
8 void MCI_SignalEvent_Callback (uint32_t event);
9
init_driver(ARM_DRIVER_MCI * drv)10 void init_driver (ARM_DRIVER_MCI *drv) {
11 int32_t status;
12
13 status = drv->Initialize (&MCI_SignalEvent_Callback);
14
15 if (status != ARM_DRIVER_OK) {
16 // Initialization and event callback registration failed
17 }
18 }
19
20 /* Usage example: ARM_MCI_Uninitialize --------------------------------------*/
21
uninit_driver(ARM_DRIVER_MCI * drv)22 void uninit_driver (ARM_DRIVER_MCI *drv) {
23 int32_t status;
24
25 status = drv->Uninitialize ();
26
27 if (status == ARM_DRIVER_OK) {
28 // Driver successfully uninitialized
29 }
30 }
31
32 /* Usage example: ARM_MCI_PowerControl --------------------------------------*/
33
control_driver_power(ARM_DRIVER_MCI * drv,bool enable)34 void control_driver_power (ARM_DRIVER_MCI *drv, bool enable) {
35 int32_t status;
36
37 if (enable == true) {
38 status = drv->PowerControl (ARM_POWER_FULL);
39 }
40 else {
41 status = drv->PowerControl (ARM_POWER_OFF);
42 }
43
44 if (status == ARM_DRIVER_OK) {
45 // Driver power enabled/disabled
46 }
47 }
48
49 /* Usage example: ARM_MCI_CardPower -----------------------------------------*/
50
51 ARM_MCI_CAPABILITIES drv_capabilities;
52
set_card_vdd_3v3(ARM_DRIVER_MCI * drv)53 void set_card_vdd_3v3 (ARM_DRIVER_MCI *drv) {
54 int32_t status;
55
56 if (drv_capabilities.vdd == 1U) {
57 // Power switching to 3.3V supported
58 status = drv->CardPower (ARM_MCI_POWER_VDD_3V3);
59
60 if (status == ARM_DRIVER_OK) {
61 // Card power set to 3.3V
62 }
63 }
64 }
65
66 /* Usage example: ARM_MCI_ReadCD --------------------------------------------*/
67
read_card_detect_state(ARM_DRIVER_MCI * drv)68 void read_card_detect_state (ARM_DRIVER_MCI *drv) {
69 int32_t status;
70
71 status = drv->ReadCD();
72
73 if (status == 1) {
74 // Memory card is detected
75 }
76 else {
77 if (status == 0) {
78 // Memory card is not detected
79 }
80 else {
81 // Error reading card detect pin state
82 }
83 }
84 }
85
86 /* Usage example: ARM_MCI_ReadWP --------------------------------------------*/
87
read_write_protect_state(ARM_DRIVER_MCI * drv)88 void read_write_protect_state (ARM_DRIVER_MCI *drv) {
89 int32_t status;
90
91 status = drv->ReadWP();
92
93 if (status == 1) {
94 // Memory card write protection is enabled
95 }
96 else {
97 if (status == 0) {
98 // Memory card write protection is disabled
99 }
100 else {
101 // Error reading write protect pin state
102 }
103 }
104 }
105
106 /* Usage example: ARM_MCI_SendCommand ---------------------------------------*/
107
108 volatile uint32_t MCI_Events;
109
MCI_SignalEvent_Callback(uint32_t event)110 void MCI_SignalEvent_Callback (uint32_t event) {
111 // Save current event
112 MCI_Events |= event;
113 }
114
send_CMD0(ARM_DRIVER_MCI * drv)115 void send_CMD0 (ARM_DRIVER_MCI *drv) {
116 int32_t status;
117 uint32_t cmd;
118
119 MCI_Events = 0U; //Clear MCI driver event flags
120 cmd = 0U; // Set GO_IDLE_STATE command code
121
122 status = drv->SendCommand (cmd, 0U, ARM_MCI_CARD_INITIALIZE | ARM_MCI_RESPONSE_NONE, NULL);
123
124 if (status == ARM_DRIVER_OK) {
125 /* Wait for event */
126 while ((MCI_Events & ARM_MCI_EVENT_COMMAND_COMPLETE) == 0U);
127 // Command was successfully sent to memory card
128 // ..
129 }
130 else {
131 // Error
132 }
133 }
134
135 /* Usage example: ARM_MCI_SetupTransfer -------------------------------------*/
136
137 volatile uint32_t MCI_Events;
138
MCI_SignalEvent_Callback(uint32_t event)139 void MCI_SignalEvent_Callback (uint32_t event) {
140 MCI_Events |= event; // Save current event
141 }
142
read_sector(ARM_DRIVER_MCI * drv,uint8_t * buf,uint32_t sz)143 void read_sector (ARM_DRIVER_MCI *drv, uint8_t *buf, uint32_t sz) {
144 int32_t status;
145 uint32_t cmd, arg;
146 uint32_t resp;
147
148 if (sz < 512U) {
149 // Invalid buffer size, sector consists of 512 bytes
150 //...
151 }
152
153 status = drv->SetupTransfer (buf, 1U, 512U, ARM_MCI_TRANSFER_READ | ARM_MCI_TRANSFER_BLOCK);
154
155 if (status == ARM_DRIVER_OK) {
156 MCI_Events = 0U; //Clear MCI driver event flags
157
158 cmd = 17U; // Set READ_SINGLE_BLOCK command
159 arg = 0U; // Set sector number
160
161 status = drv->SendCommand (cmd, arg, ARM_MCI_RESPONSE_SHORT | ARM_MCI_RESPONSE_CRC | ARM_MCI_TRANSFER_DATA, &resp);
162
163 if (status == ARM_DRIVER_OK) {
164 /* Wait for event */
165 while ((MCI_Events & ARM_MCI_EVENT_COMMAND_COMPLETE) == 0U);
166 // Command was successfully sent to memory card
167 if ((resp & 0x03U) == 0U) {
168 // Sector number is valid, wait until data transfer completes
169 while ((MCI_Events & ARM_MCI_EVENT_TRANSFER_COMPLETE) == 0U);
170 // Data was successfully read from memory card
171 // ...
172 }
173 }
174 }
175 }
176
177 /* Usage example: ARM_MCI_AbortTransfer -------------------------------------*/
178
abort_data_transfer(ARM_DRIVER_MCI * drv)179 void abort_data_transfer (ARM_DRIVER_MCI *drv) {
180 ARM_MCI_STATUS drv_status;
181
182 drv_status = drv->GetStatus();
183
184 if (drv_status.transfer_active == 1U) {
185 // Data transfer is active, abort the transfer
186 if (drv->AbortTransfer() == ARM_DRIVER_OK) {
187 // Transfer aborted
188 // ...
189 }
190 }
191 }
192
193 /* Usage example: ARM_MCI_GetStatus -----------------------------------------*/
194
check_transfer_status(ARM_DRIVER_MCI * drv)195 void check_transfer_status (ARM_DRIVER_MCI *drv) {
196 ARM_MCI_STATUS drv_status;
197
198 drv_status = drv->GetStatus();
199
200 if (drv_status.transfer_active == 1U) {
201 // Data transfer is active
202 }
203
204 if (drv_status.transfer_timeout == 1U) {
205 // Data not received, timeout expired
206 }
207
208 if (drv_status.transfer_error == 1U) {
209 // Data transfer ended with error
210 }
211 }
212
213 /* Usage example: ARM_MCI_SignalEvent ---------------------------------------*/
214
MCI_SignalEvent_Callback(uint32_t event)215 void MCI_SignalEvent_Callback (uint32_t event) {
216 if ((event & ARM_MCI_EVENT_CARD_INSERTED) != 0U) {
217 // Memory card was inserted into socket
218 }
219 if ((event & ARM_MCI_EVENT_CARD_REMOVED) != 0U) {
220 // Memory card was removed from socket
221 }
222
223 if ((event & ARM_MCI_EVENT_COMMAND_COMPLETE) != 0U) {
224 // Command was successfully sent to memory card
225 }
226 if ((event & ARM_MCI_EVENT_COMMAND_TIMEOUT) != 0U) {
227 // Command response was not received in time
228 }
229 if ((event & ARM_MCI_EVENT_COMMAND_ERROR) != 0U) {
230 // Command response was invalid
231 }
232
233 if ((event & ARM_MCI_EVENT_TRANSFER_COMPLETE) != 0U) {
234 // Data successfully transferred from/to memory card
235 }
236 if ((event & ARM_MCI_EVENT_TRANSFER_TIMEOUT) != 0U) {
237 // Data not transferred from/to memory card, timeout expired
238 }
239 if ((event & ARM_MCI_EVENT_TRANSFER_ERROR) != 0U) {
240 // Data transfer ended with errors
241 }
242
243 if ((event & ARM_MCI_EVENT_SDIO_INTERRUPT) != 0U) {
244 // SD I/O card sent interrupt request
245 }
246
247 if ((event & ARM_MCI_EVENT_CCS) != 0U) {
248 // CE-ATA command completion signal received
249 }
250 if ((event & ARM_MCI_EVENT_CCS_TIMEOUT) != 0U) {
251 // CE-ATA command completion signal wait timeout expired
252 }
253 }
254