LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fs_mem.c
Go to the documentation of this file.
1 /*
2  * @brief SDMMC Chan FATFS simple abstraction layer (for RAM disk)
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 <string.h>
33 #include "diskio.h"
34 #include "fs_mem.h"
35 /*****************************************************************************
36  * Private types/enumerations/variables
37  ****************************************************************************/
38 
39 /* Disk Status */
40 static volatile DSTATUS Stat = STA_NOINIT;
41 static uint8_t *buff_ptr;
43 
44 /*****************************************************************************
45  * Public functions
46  ****************************************************************************/
47 
48 /* Initialize Disk Drive */
50 {
51 
52  if (drv) {
53  return STA_NOINIT; /* Supports only single drive */
54  }
55 
56  if (Stat != STA_NOINIT) {
57  return Stat; /* Mem variables already set */
58 
59  }
60 
61  /* Reset */
62  Stat = STA_NOINIT;
63 
65 
66  /* Check if we got a valid, atleast 4K sized buffer */
67  if (!buff_ptr || buff_sz < 4096) {
68  return Stat;
69  }
70 
71  Stat &= ~STA_NOINIT;
72  return Stat;
73 
74 }
75 
76 
77 /* Read Sector(s) */
78 DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
79 {
80  if (drv || !count) {
81  return RES_PARERR;
82  }
83  if (Stat & STA_NOINIT) {
84  return RES_NOTRDY;
85  }
86 
87  memcpy(buff, buff_ptr + (sector * SECTOR_SZ), SECTOR_SZ * count);
88 
89  return RES_OK;
90 }
91 
92 /* Get Disk Status */
94 {
95  if (drv) {
96  return STA_NOINIT; /* Supports only single drive */
97 
98  }
99  return Stat;
100 }
101 
102 /* Write Sector(s) */
103 DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
104 {
105 
106  if (drv || !count) {
107  return RES_PARERR;
108  }
109  if (Stat & STA_NOINIT) {
110  return RES_NOTRDY;
111  }
112 
113  memcpy(buff_ptr + (sector * SECTOR_SZ), buff, SECTOR_SZ * count);
114  return RES_OK;
115 }
116 
117 /* Disk Drive miscellaneous Functions */
118 DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
119 {
120  DRESULT res;
121 
122  if (drv) {
123  return RES_PARERR;
124  }
125  if (Stat & STA_NOINIT) {
126  return RES_NOTRDY;
127  }
128 
129  res = RES_ERROR;
130 
131  switch (ctrl) {
132  case CTRL_SYNC: /* Make sure that no pending write process */
133  res = RES_OK;
134  break;
135 
136  case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
137  *(DWORD *) buff = buff_sz / SECTOR_SZ;
138  res = RES_OK;
139  break;
140 
141  case GET_SECTOR_SIZE: /* Get R/W sector size (WORD) */
142  *(WORD *) buff = SECTOR_SZ; // 512;
143  res = RES_OK;
144  break;
145 
146  case GET_BLOCK_SIZE:/* Get erase block size in unit of sector (DWORD) */
147  *(DWORD *) buff = SECTOR_SZ; // FIXED:
148  res = RES_OK;
149  break;
150 
151  default:
152  res = RES_PARERR;
153  break;
154  }
155 
156  return res;
157 }