LPCOpen Platform
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 "fs_mci.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 
46 /* Card Configuration */
47 // CARDCONFIG CardConfig; /* Card configuration */
48 
49 /*****************************************************************************
50  * Public types/enumerations/variables
51  ****************************************************************************/
52 
54 
55 /*****************************************************************************
56  * Private functions
57  ****************************************************************************/
58 
59 /* Get bits from an array */
61 {
62  uint32_t byte_idx_stx;
63  uint8_t bit_idx_stx, bit_idx;
64  uint32_t ret, byte_idx;
65 
66  byte_idx_stx = start / 8;
67  bit_idx_stx = start - byte_idx_stx * 8;
68 
69  if (size < (8 - bit_idx_stx)) {
70  return (resp[byte_idx_stx] >> bit_idx_stx) & ((1 << size) - 1);
71  }
72 
73  ret = 0;
74 
75  ret = (resp[byte_idx_stx] >> bit_idx_stx) & ((1 << (8 - bit_idx_stx)) - 1);
76  bit_idx = 8 - bit_idx_stx;
77  size -= bit_idx;
78 
79  byte_idx = 1;
80  while (size > 8) {
81  ret |= resp[byte_idx_stx + byte_idx] << (bit_idx);
82  size -= 8;
83  bit_idx += 8;
84  byte_idx++;
85  }
86 
87  if (size > 0) {
88  ret |= (resp[byte_idx_stx + byte_idx] & ((1 << size) - 1)) << bit_idx;
89  }
90 
91  return ret;
92 }
93 
94 /* Wait for device to complete */
96 {
97  /* Poll card for program completion */
98  Timer2 = 50;// 500 ms
99  while (Timer2) {
100  if (Chip_SDMMC_GetState() != -1) {
101  return true;
102  }
103  }
104 
105  return false;
106 }
107 
108 /*****************************************************************************
109  * Public functions
110  ****************************************************************************/
111 
112 /* Initialize Disk Drive */
114 {
115  if (drv) {
116  return STA_NOINIT; /* Supports only single drive */
117  }
118  // if (Stat & STA_NODISK) return Stat; /* No card in the socket */
119 
120  if (Stat != STA_NOINIT) {
121  return Stat; // card is already enumerated
122 
123  }
124  /* Reset */
125  Stat = STA_NOINIT;
126 
127 #ifndef BOARD_NGX_XPLORER_18304330
128  /* NGX board ignored SD_CD pin */
129  /* Wait for a card to be inserted (note CD is not on the SDMMC power rail and can be polled without enabling SD slot power */
130  while (Chip_SDMMC_CardNDetect() == 0) {}
131 #endif
132 
133  /* Enumerate the card once detected. Note this function may block for a little while. */
134 
135  if (!Chip_SDMMC_Acquire(&sdcardinfo)) {
136  DEBUGOUT("Card Acquire failed...\r\n");
137  return Stat;
138  }
139 
140  Stat &= ~STA_NOINIT;
141  return Stat;
142 
143 }
144 
145 /* Disk Drive miscellaneous Functions */
146 DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
147 {
148  DRESULT res;
149  BYTE n, *ptr = buff;
150  ULONG *ptr32 = (ULONG *) buff;
151 
152  if (drv) {
153  return RES_PARERR;
154  }
155  if (Stat & STA_NOINIT) {
156  return RES_NOTRDY;
157  }
158 
159  res = RES_ERROR;
160 
161  switch (ctrl) {
162  case CTRL_SYNC: /* Make sure that no pending write process */
163  if (mci_wait_for_ready() == true) {
164  res = RES_OK;
165  }
166 
167  break;
168 
169  case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
170  *(DWORD *) buff = sdcardinfo.blocknr;
171  res = RES_OK;
172  break;
173 
174  case GET_SECTOR_SIZE: /* Get R/W sector size (WORD) */
175  *(WORD *) buff = sdcardinfo.block_len; // 512;
176  res = RES_OK;
177  break;
178 
179  case GET_BLOCK_SIZE:/* Get erase block size in unit of sector (DWORD) */
180  *(DWORD *) buff = 4096; // FIXED:
181  res = RES_OK;
182  break;
183 
184  case MMC_GET_TYPE: /* Get card type flags (1 byte) */
185  *ptr = sdcardinfo.card_type;
186  res = RES_OK;
187  break;
188 
189  case MMC_GET_CSD: /* Receive CSD as a data block (16 bytes) */
190  for (n = 0; n < 4; n++) {
191  *(ptr32 + n) = sdcardinfo.csd[n];
192  }
193  res = RES_OK;
194  break;
195 
196  case MMC_GET_CID: /* Receive CID as a data block (16 bytes) */
197  for (n = 0; n < 4; n++) {
198  *(ptr32 + n) = sdcardinfo.cid[n];
199  }
200  res = RES_OK;
201  break;
202 
203  case MMC_GET_SDSTAT:/* Receive SD status as a data block (64 bytes) */
204  {
205  int32_t cardStatus;
206 
207  cardStatus = Chip_SDMMC_GetState();
208  if (cardStatus != -1) {
209  uint8_t *status = (uint8_t *) &cardStatus;
210  for (n = 0; n < 2; n++) {
211  *(ptr + n) = ((uint8_t *) status)[n];
212  }
213  res = RES_OK;
214  }
215  }
216  break;
217 
218  default:
219  res = RES_PARERR;
220  break;
221  }
222 
223  return res;
224 }
225 
226 /* Read Sector(s) */
227 DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
228 {
229  if (drv || !count) {
230  return RES_PARERR;
231  }
232  if (Stat & STA_NOINIT) {
233  return RES_NOTRDY;
234  }
235 
236  if (Chip_SDMMC_ReadBlocks(buff, sector, count)) {
237  return RES_OK;
238  }
239 
240  return RES_ERROR;
241 }
242 
243 /* Get Disk Status */
245 {
246  if (drv) {
247  return STA_NOINIT; /* Supports only single drive */
248 
249  }
250  return Stat;
251 }
252 
253 /* Write Sector(s) */
254 DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
255 {
256 
257  if (drv || !count) {
258  return RES_PARERR;
259  }
260  if (Stat & STA_NOINIT) {
261  return RES_NOTRDY;
262  }
263 
264  if ( Chip_SDMMC_WriteBlocks((void *) buff, sector, count)) {
265  return RES_OK;
266  }
267 
268  return RES_ERROR;
269 }
270 
271 /* Device timer function */
272 void disk_timerproc(void)
273 {
274  WORD n;
275 
276  n = Timer2;
277  if (n) {
278  Timer2 = --n;
279  }
280 }