2 #include "Driver_MCI.h"
5 /* Usage example: ARM_MCI_Initialize ----------------------------------------*/
7 // ARM_MCI_SignalEvent callback function prototype
8 void MCI_SignalEvent_Callback (uint32_t event);
10 void init_driver (ARM_DRIVER_MCI *drv) {
13 status = drv->Initialize (&MCI_SignalEvent_Callback);
15 if (status != ARM_DRIVER_OK) {
16 // Initialization and event callback registration failed
20 /* Usage example: ARM_MCI_Uninitialize --------------------------------------*/
22 void uninit_driver (ARM_DRIVER_MCI *drv) {
25 status = drv->Uninitialize ();
27 if (status == ARM_DRIVER_OK) {
28 // Driver successfully uninitialized
32 /* Usage example: ARM_MCI_PowerControl --------------------------------------*/
34 void control_driver_power (ARM_DRIVER_MCI *drv, bool enable) {
38 status = drv->PowerControl (ARM_POWER_FULL);
41 status = drv->PowerControl (ARM_POWER_OFF);
44 if (status == ARM_DRIVER_OK) {
45 // Driver power enabled/disabled
49 /* Usage example: ARM_MCI_CardPower -----------------------------------------*/
51 ARM_MCI_CAPABILITIES drv_capabilities;
53 void set_card_vdd_3v3 (ARM_DRIVER_MCI *drv) {
56 if (drv_capabilities.vdd == 1U) {
57 // Power switching to 3.3V supported
58 status = drv->CardPower (ARM_MCI_POWER_VDD_3V3);
60 if (status == ARM_DRIVER_OK) {
61 // Card power set to 3.3V
66 /* Usage example: ARM_MCI_ReadCD --------------------------------------------*/
68 void read_card_detect_state (ARM_DRIVER_MCI *drv) {
71 status = drv->ReadCD();
74 // Memory card is detected
78 // Memory card is not detected
81 // Error reading card detect pin state
86 /* Usage example: ARM_MCI_ReadWP --------------------------------------------*/
88 void read_write_protect_state (ARM_DRIVER_MCI *drv) {
91 status = drv->ReadWP();
94 // Memory card write protection is enabled
98 // Memory card write protection is disabled
101 // Error reading write protect pin state
106 /* Usage example: ARM_MCI_SendCommand ---------------------------------------*/
108 volatile uint32_t MCI_Events;
110 void MCI_SignalEvent_Callback (uint32_t event) {
111 // Save current event
115 void send_CMD0 (ARM_DRIVER_MCI *drv) {
119 MCI_Events = 0U; //Clear MCI driver event flags
120 cmd = 0U; // Set GO_IDLE_STATE command code
122 status = drv->SendCommand (cmd, 0U, ARM_MCI_CARD_INITIALIZE | ARM_MCI_RESPONSE_NONE, NULL);
124 if (status == ARM_DRIVER_OK) {
126 while ((MCI_Events & ARM_MCI_EVENT_COMMAND_COMPLETE) == 0U);
127 // Command was successfully sent to memory card
135 /* Usage example: ARM_MCI_SetupTransfer -------------------------------------*/
137 volatile uint32_t MCI_Events;
139 void MCI_SignalEvent_Callback (uint32_t event) {
140 MCI_Events |= event; // Save current event
143 void read_sector (ARM_DRIVER_MCI *drv, uint8_t *buf, uint32_t sz) {
149 // Invalid buffer size, sector consists of 512 bytes
153 status = drv->SetupTransfer (buf, 1U, 512U, ARM_MCI_TRANSFER_READ | ARM_MCI_TRANSFER_BLOCK);
155 if (status == ARM_DRIVER_OK) {
156 MCI_Events = 0U; //Clear MCI driver event flags
158 cmd = 17U; // Set READ_SINGLE_BLOCK command
159 arg = 0U; // Set sector number
161 status = drv->SendCommand (cmd, arg, ARM_MCI_RESPONSE_SHORT | ARM_MCI_RESPONSE_CRC | ARM_MCI_TRANSFER_DATA, &resp);
163 if (status == ARM_DRIVER_OK) {
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
177 /* Usage example: ARM_MCI_AbortTransfer -------------------------------------*/
179 void abort_data_transfer (ARM_DRIVER_MCI *drv) {
180 ARM_MCI_STATUS drv_status;
182 drv_status = drv->GetStatus();
184 if (drv_status.transfer_active == 1U) {
185 // Data transfer is active, abort the transfer
186 if (drv->AbortTransfer() == ARM_DRIVER_OK) {
193 /* Usage example: ARM_MCI_GetStatus -----------------------------------------*/
195 void check_transfer_status (ARM_DRIVER_MCI *drv) {
196 ARM_MCI_STATUS drv_status;
198 drv_status = drv->GetStatus();
200 if (drv_status.transfer_active == 1U) {
201 // Data transfer is active
204 if (drv_status.transfer_timeout == 1U) {
205 // Data not received, timeout expired
208 if (drv_status.transfer_error == 1U) {
209 // Data transfer ended with error
213 /* Usage example: ARM_MCI_SignalEvent ---------------------------------------*/
215 void MCI_SignalEvent_Callback (uint32_t event) {
216 if ((event & ARM_MCI_EVENT_CARD_INSERTED) != 0U) {
217 // Memory card was inserted into socket
219 if ((event & ARM_MCI_EVENT_CARD_REMOVED) != 0U) {
220 // Memory card was removed from socket
223 if ((event & ARM_MCI_EVENT_COMMAND_COMPLETE) != 0U) {
224 // Command was successfully sent to memory card
226 if ((event & ARM_MCI_EVENT_COMMAND_TIMEOUT) != 0U) {
227 // Command response was not received in time
229 if ((event & ARM_MCI_EVENT_COMMAND_ERROR) != 0U) {
230 // Command response was invalid
233 if ((event & ARM_MCI_EVENT_TRANSFER_COMPLETE) != 0U) {
234 // Data successfully transferred from/to memory card
236 if ((event & ARM_MCI_EVENT_TRANSFER_TIMEOUT) != 0U) {
237 // Data not transferred from/to memory card, timeout expired
239 if ((event & ARM_MCI_EVENT_TRANSFER_ERROR) != 0U) {
240 // Data transfer ended with errors
243 if ((event & ARM_MCI_EVENT_SDIO_INTERRUPT) != 0U) {
244 // SD I/O card sent interrupt request
247 if ((event & ARM_MCI_EVENT_CCS) != 0U) {
248 // CE-ATA command completion signal received
250 if ((event & ARM_MCI_EVENT_CCS_TIMEOUT) != 0U) {
251 // CE-ATA command completion signal wait timeout expired