]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-FAT-SL/media-drv/ram/ramdrv_f.c
5d1d49fa1d05376cb0d3fb43dbd6de4563816b7f
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-FAT-SL / media-drv / ram / ramdrv_f.c
1 /*\r
2  * FreeRTOS+FAT FS V1.0.0 (C) 2013 HCC Embedded\r
3  *\r
4  * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license \r
5  * terms.\r
6  * \r
7  * FreeRTOS+FAT SL uses a dual license model that allows the software to be used\r
8  * under a pure GPL open source license (as opposed to the modified GPL licence\r
9  * under which FreeRTOS is distributed) or a commercial license.  Details of \r
10  * both license options follow:\r
11  * \r
12  * - Open source licensing -\r
13  * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and\r
14  * distributed without charge provided the user adheres to version two of the \r
15  * GNU General Public License (GPL) and does not remove the copyright notice or \r
16  * this text.  The GPL V2 text is available on the gnu.org web site, and on the\r
17  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.\r
18  * \r
19  * - Commercial licensing -\r
20  * Businesses and individuals who for commercial or other reasons cannot comply\r
21  * with the terms of the GPL V2 license must obtain a commercial license before \r
22  * incorporating FreeRTOS+FAT SL into proprietary software for distribution in \r
23  * any form.  Commercial licenses can be purchased from \r
24  * http://shop.freertos.org/fat_sl and do not require any source files to be \r
25  * changed.\r
26  *\r
27  * FreeRTOS+FAT SL is distributed in the hope that it will be useful.  You\r
28  * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as\r
29  * is'.  FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the\r
30  * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A\r
31  * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all\r
32  * conditions and terms, be they implied, expressed, or statutory.\r
33  *\r
34  * http://www.FreeRTOS.org\r
35  * http://www.FreeRTOS.org/FreeRTOS-Plus\r
36  *\r
37  */\r
38 \r
39 #include "../../api/api_mdriver_ram.h"\r
40 #include "config_mdriver_ram.h"\r
41 #include "../../psp/include/psp_string.h"\r
42 \r
43 #include "../../version/ver_mdriver_ram.h"\r
44 #if VER_MDRIVER_RAM_MAJOR != 1 || VER_MDRIVER_RAM_MINOR != 2\r
45  #error Incompatible MDRIVER_RAM version number!\r
46 #endif\r
47 \r
48 \r
49 char  ramdrv0[MDRIVER_RAM_VOLUME0_SIZE];\r
50 \r
51 typedef struct\r
52 {\r
53   char         * ramdrv;\r
54   unsigned long  maxsector;\r
55   int            use;\r
56   F_DRIVER     * driver;\r
57 } t_RamDrv;\r
58 \r
59 static F_DRIVER  t_drivers[1];\r
60 \r
61 static t_RamDrv  RamDrv[1] =\r
62 {\r
63   { ramdrv0, ( MDRIVER_RAM_VOLUME0_SIZE / MDRIVER_RAM_SECTOR_SIZE ), 0, &t_drivers[0] }\r
64 };\r
65 \r
66 \r
67 /****************************************************************************\r
68  * Read one sector\r
69  ***************************************************************************/\r
70 static int ram_readsector ( F_DRIVER * driver, void * data, unsigned long sector )\r
71 {\r
72   long       len;\r
73   char     * d = (char *)data;\r
74   char     * s;\r
75   t_RamDrv * p = (t_RamDrv *)( driver->user_ptr );\r
76 \r
77   if ( sector >= p->maxsector )\r
78   {\r
79     return MDRIVER_RAM_ERR_SECTOR;\r
80   }\r
81 \r
82   s = p->ramdrv;\r
83   s += sector * MDRIVER_RAM_SECTOR_SIZE;\r
84   len = MDRIVER_RAM_SECTOR_SIZE;\r
85 \r
86 #if MDRIVER_MEM_LONG_ACCESS\r
87   if ( ( !( len & 3 ) ) && ( !( ( (long)d ) & 3 ) ) && ( !( ( (long)s ) & 3 ) ) )\r
88   {\r
89     long * dd = (long *)d;\r
90     long * ss = (long *)s;\r
91     len >>= 2;\r
92     while ( len-- )\r
93     {\r
94       *dd++ = *ss++;\r
95     }\r
96 \r
97     return MDRIVER_RAM_NO_ERROR;\r
98   }\r
99 \r
100 #endif /* if MDRIVER_MEM_LONG_ACCESS */\r
101 \r
102   while ( len-- )\r
103   {\r
104     *d++ = *s++;\r
105   }\r
106 \r
107   return MDRIVER_RAM_NO_ERROR;\r
108 }\r
109 \r
110 /****************************************************************************\r
111  * Write one sector\r
112  ***************************************************************************/\r
113 static int ram_writesector ( F_DRIVER * driver, void * data, unsigned long sector )\r
114 {\r
115   long       len;\r
116   char     * s = (char *)data;\r
117   char     * d;\r
118   t_RamDrv * p = (t_RamDrv *)( driver->user_ptr );\r
119 \r
120   if ( sector >= p->maxsector )\r
121   {\r
122     return MDRIVER_RAM_ERR_SECTOR;\r
123   }\r
124 \r
125   d = p->ramdrv;\r
126   d += sector * MDRIVER_RAM_SECTOR_SIZE;\r
127   len = MDRIVER_RAM_SECTOR_SIZE;\r
128 \r
129 #if MDRIVER_MEM_LONG_ACCESS\r
130   if ( ( !( len & 3 ) ) && ( !( ( (long)d ) & 3 ) ) && ( !( ( (long)s ) & 3 ) ) )\r
131   {\r
132     long * dd = (long *)d;\r
133     long * ss = (long *)s;\r
134     len >>= 2;\r
135     while ( len-- )\r
136     {\r
137       *dd++ = *ss++;\r
138     }\r
139 \r
140     return MDRIVER_RAM_NO_ERROR;\r
141   }\r
142 \r
143 #endif /* if MDRIVER_MEM_LONG_ACCESS */\r
144 \r
145   while ( len-- )\r
146   {\r
147     *d++ = *s++;\r
148   }\r
149 \r
150   return MDRIVER_RAM_NO_ERROR;\r
151 }\r
152 \r
153 \r
154 /****************************************************************************\r
155  *\r
156  * ram_getphy\r
157  *\r
158  * determinate ramdrive physicals\r
159  *\r
160  * INPUTS\r
161  *\r
162  * driver - driver structure\r
163  * phy - this structure has to be filled with physical information\r
164  *\r
165  * RETURNS\r
166  *\r
167  * error code or zero if successful\r
168  *\r
169  ***************************************************************************/\r
170 static int ram_getphy ( F_DRIVER * driver, F_PHY * phy )\r
171 {\r
172   t_RamDrv * p = (t_RamDrv *)( driver->user_ptr );\r
173 \r
174   phy->number_of_sectors = p->maxsector;\r
175   phy->bytes_per_sector = MDRIVER_RAM_SECTOR_SIZE;\r
176 \r
177   return MDRIVER_RAM_NO_ERROR;\r
178 }\r
179 \r
180 \r
181 /****************************************************************************\r
182  *\r
183  * ram_release\r
184  *\r
185  * Releases a drive\r
186  *\r
187  * INPUTS\r
188  *\r
189  * driver_param - driver parameter\r
190  *\r
191  ***************************************************************************/\r
192 static void ram_release ( F_DRIVER * driver )\r
193 {\r
194   t_RamDrv * p = (t_RamDrv *)( driver->user_ptr );\r
195 \r
196   if ( p == RamDrv )\r
197   {\r
198     p->use = 0;\r
199   }\r
200 }\r
201 \r
202 \r
203 /****************************************************************************\r
204  *\r
205  * ram_initfunc\r
206  *\r
207  * this init function has to be passed for highlevel to initiate the\r
208  * driver functions\r
209  *\r
210  * INPUTS\r
211  *\r
212  * driver_param - driver parameter\r
213  *\r
214  * RETURNS\r
215  *\r
216  * driver structure pointer\r
217  *\r
218  ***************************************************************************/\r
219 F_DRIVER * ram_initfunc ( unsigned long driver_param )\r
220 {\r
221   t_RamDrv    * p;\r
222 \r
223   p = RamDrv + driver_param;\r
224 \r
225   if ( p != RamDrv )\r
226   {\r
227     return 0;\r
228   }\r
229 \r
230   if ( p->use )\r
231   {\r
232     return 0;\r
233   }\r
234 \r
235   (void)psp_memset( p->driver, 0, sizeof( F_DRIVER ) );\r
236 \r
237   p->driver->readsector = ram_readsector;\r
238   p->driver->writesector = ram_writesector;\r
239   p->driver->getphy = ram_getphy;\r
240   p->driver->release = ram_release;\r
241   p->driver->user_ptr = p;\r
242 \r
243   p->use = 1;\r
244 \r
245   return p->driver;\r
246 } /* ram_initfunc */\r
247 \r