LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fs_mci.c
Go to the documentation of this file.
1 /*
2  * @brief SDMMC Chan FATFS simple abstraction layer
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "fsmci_cfg.h"
33 #include "board.h"
34 #include "chip.h"
35 
36 /*****************************************************************************
37  * Private types/enumerations/variables
38  ****************************************************************************/
39 
40 /* Disk Status */
41 static volatile DSTATUS Stat = STA_NOINIT;
42 
43 /* 100Hz decrement timer stopped at zero (disk_timerproc()) */
44 static volatile WORD Timer2;
45 
47 
48 /*****************************************************************************
49  * Public types/enumerations/variables
50  ****************************************************************************/
51 
52 /*****************************************************************************
53  * Private functions
54  ****************************************************************************/
55 
56 /*****************************************************************************
57  * Public functions
58  ****************************************************************************/
59 
60 /* Initialize Disk Drive */
62 {
63  if (drv) {
64  return STA_NOINIT; /* Supports only single drive */
65  }
66  /* if (Stat & STA_NODISK) return Stat; *//* No card in the socket */
67 
68  if (Stat != STA_NOINIT) {
69  return Stat; /* card is already enumerated */
70 
71  }
72 
73  #if !_FS_READONLY
75  #endif
76 
77  /* Initialize the Card Data Strucutre */
78  hCard = FSMCI_CardInit();
79 
80  /* Reset */
81  Stat = STA_NOINIT;
82 
83  FSMCI_CardInsertWait(hCard); /* Wait for card to be inserted */
84 
85  /* Enumerate the card once detected. Note this function may block for a little while. */
86  if (!FSMCI_CardAcquire(hCard)) {
87  DEBUGOUT("Card Acquire failed...\r\n");
88  return Stat;
89  }
90 
91  Stat &= ~STA_NOINIT;
92  return Stat;
93 
94 }
95 
96 /* Disk Drive miscellaneous Functions */
97 DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
98 {
99  DRESULT res;
100  BYTE *ptr = buff;
101 
102  if (drv) {
103  return RES_PARERR;
104  }
105  if (Stat & STA_NOINIT) {
106  return RES_NOTRDY;
107  }
108 
109  res = RES_ERROR;
110 
111  switch (ctrl) {
112  case CTRL_SYNC: /* Make sure that no pending write process */
113  if (FSMCI_CardReadyWait(hCard, 50)) {
114  res = RES_OK;
115  }
116  break;
117 
118  case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
119  *(DWORD *) buff = FSMCI_CardGetSectorCnt(hCard);
120  res = RES_OK;
121  break;
122 
123  case GET_SECTOR_SIZE: /* Get R/W sector size (WORD) */
124  *(WORD *) buff = FSMCI_CardGetSectorSz(hCard);
125  res = RES_OK;
126  break;
127 
128  case GET_BLOCK_SIZE:/* Get erase block size in unit of sector (DWORD) */
129  *(DWORD *) buff = FSMCI_CardGetBlockSz(hCard);
130  res = RES_OK;
131  break;
132 
133  case MMC_GET_TYPE: /* Get card type flags (1 byte) */
134  *ptr = FSMCI_CardGetType(hCard);
135  res = RES_OK;
136  break;
137 
138  case MMC_GET_CSD: /* Receive CSD as a data block (16 bytes) */
139  *((uint32_t *) buff + 0) = FSMCI_CardGetCSD(hCard, 0);
140  *((uint32_t *) buff + 1) = FSMCI_CardGetCSD(hCard, 1);
141  *((uint32_t *) buff + 2) = FSMCI_CardGetCSD(hCard, 2);
142  *((uint32_t *) buff + 3) = FSMCI_CardGetCSD(hCard, 3);
143  res = RES_OK;
144  break;
145 
146  case MMC_GET_CID: /* Receive CID as a data block (16 bytes) */
147  *((uint32_t *) buff + 0) = FSMCI_CardGetCID(hCard, 0);
148  *((uint32_t *) buff + 1) = FSMCI_CardGetCID(hCard, 1);
149  *((uint32_t *) buff + 2) = FSMCI_CardGetCID(hCard, 2);
150  *((uint32_t *) buff + 3) = FSMCI_CardGetCID(hCard, 3);
151  res = RES_OK;
152  break;
153 
154  case MMC_GET_SDSTAT:/* Receive SD status as a data block (64 bytes) */
155  if (FSMCI_CardGetState(hCard, (uint8_t *) buff)) {
156  res = RES_OK;
157  }
158  break;
159 
160  default:
161  res = RES_PARERR;
162  break;
163  }
164 
165  return res;
166 }
167 
168 /* Read Sector(s) */
169 DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
170 {
171  if (drv || !count) {
172  return RES_PARERR;
173  }
174  if (Stat & STA_NOINIT) {
175  return RES_NOTRDY;
176  }
177 
178  if (FSMCI_CardReadSectors(hCard, buff, sector, count)) {
179  return RES_OK;
180  }
181 
182  return RES_ERROR;
183 }
184 
185 /* Get Disk Status */
187 {
188  if (drv) {
189  return STA_NOINIT; /* Supports only single drive */
190 
191  }
192  return Stat;
193 }
194 
195 /* Write Sector(s) */
196 DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
197 {
198 
199  if (drv || !count) {
200  return RES_PARERR;
201  }
202  if (Stat & STA_NOINIT) {
203  return RES_NOTRDY;
204  }
205 
206  if ( FSMCI_CardWriteSectors(hCard, (void *) buff, sector, count)) {
207  return RES_OK;
208  }
209 
210  return RES_ERROR;
211 }