]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-FAT-SL/fat_sl/common/drv.c
Add FAT SL code and demo project.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-FAT-SL / fat_sl / common / drv.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/fat_sl.h"\r
40 #include "../../psp/include/psp_string.h"\r
41 \r
42 #include "drv.h"\r
43 #include "util.h"\r
44 #include "volume.h"\r
45 \r
46 #include "../../version/ver_fat_sl.h"\r
47 #if VER_FAT_SL_MAJOR != 3 || VER_FAT_SL_MINOR != 2\r
48  #error Incompatible FAT_SL version number!\r
49 #endif\r
50 \r
51 F_DRIVER * mdrv = NULL;  /* driver structure */\r
52 \r
53 \r
54 /****************************************************************************\r
55  *\r
56  * _f_checkstatus\r
57  *\r
58  * checking a volume driver status, if media is removed or has been changed\r
59  *\r
60  * RETURNS\r
61  *\r
62  * error code or zero if successful\r
63  *\r
64  ***************************************************************************/\r
65 unsigned char _f_checkstatus ( void )\r
66 {\r
67   if ( mdrv->getstatus != NULL )\r
68   {\r
69     if ( mdrv->getstatus( mdrv ) & ( F_ST_MISSING | F_ST_CHANGED ) )\r
70     {\r
71       gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/\r
72       return F_ERR_CARDREMOVED;\r
73     }\r
74   }\r
75 \r
76   return F_NO_ERROR;\r
77 }\r
78 \r
79 \r
80 /****************************************************************************\r
81  *\r
82  * _f_writesector\r
83  *\r
84  * write sector data on a volume, it calls low level driver function, it\r
85  * writes a complete sector\r
86  *\r
87  * INPUTS\r
88  * sector - which physical sector\r
89  *\r
90  * RETURNS\r
91  * error code or zero if successful\r
92  *\r
93  ***************************************************************************/\r
94 unsigned char _f_writeglsector ( unsigned long sector )\r
95 {\r
96   unsigned char  retry;\r
97 \r
98   if ( mdrv->writesector == NULL )\r
99   {\r
100     gl_volume.state = F_STATE_NEEDMOUNT; /*no write function*/\r
101     return F_ERR_ACCESSDENIED;\r
102   }\r
103 \r
104   if ( sector == (unsigned long)-1 )\r
105   {\r
106     if ( gl_file.modified )\r
107     {\r
108       sector = gl_file.pos.sector;\r
109     }\r
110     else\r
111     {\r
112       sector = gl_volume.actsector;\r
113     }\r
114   }\r
115 \r
116   if ( sector != (unsigned long)-1 )\r
117   {\r
118     if ( mdrv->getstatus != NULL )\r
119     {\r
120       unsigned int  status;\r
121 \r
122       status = mdrv->getstatus( mdrv );\r
123 \r
124       if ( status & ( F_ST_MISSING | F_ST_CHANGED ) )\r
125       {\r
126         gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/\r
127         return F_ERR_CARDREMOVED;\r
128       }\r
129 \r
130       if ( status & ( F_ST_WRPROTECT ) )\r
131       {\r
132         gl_volume.state = F_STATE_NEEDMOUNT;  /*card has been removed;*/\r
133         return F_ERR_WRITEPROTECT;\r
134       }\r
135     }\r
136 \r
137     gl_volume.modified = 0;\r
138     gl_file.modified = 0;\r
139     gl_volume.actsector = sector;\r
140     for ( retry = 3 ; retry ; retry-- )\r
141     {\r
142       int mdrv_ret;\r
143       mdrv_ret = mdrv->writesector( mdrv, (unsigned char *)gl_sector, sector );\r
144       if ( !mdrv_ret )\r
145       {\r
146         return F_NO_ERROR;\r
147       }\r
148 \r
149       if ( mdrv_ret == -1 )\r
150       {\r
151         gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/\r
152         return F_ERR_CARDREMOVED;\r
153       }\r
154     }\r
155   }\r
156 \r
157 \r
158   return F_ERR_ONDRIVE;\r
159 } /* _f_writeglsector */\r
160 \r
161 \r
162 /****************************************************************************\r
163  *\r
164  * _f_readsector\r
165  *\r
166  * read sector data from a volume, it calls low level driver function, it\r
167  * reads a complete sector\r
168  *\r
169  * INPUTS\r
170  * sector - which physical sector is read\r
171  *\r
172  * RETURNS\r
173  * error code or zero if successful\r
174  *\r
175  ***************************************************************************/\r
176 unsigned char _f_readglsector ( unsigned long sector )\r
177 {\r
178   unsigned char  retry;\r
179   unsigned char  ret;\r
180 \r
181   if ( sector == gl_volume.actsector )\r
182   {\r
183     return F_NO_ERROR;\r
184   }\r
185 \r
186   if ( gl_volume.modified || gl_file.modified )\r
187   {\r
188     ret = _f_writeglsector( (unsigned long)-1 );\r
189     if ( ret )\r
190     {\r
191       return ret;\r
192     }\r
193   }\r
194 \r
195 \r
196   for ( retry = 3 ; retry ; retry-- )\r
197   {\r
198     int mdrv_ret;\r
199     mdrv_ret = mdrv->readsector( mdrv, (unsigned char *)gl_sector, sector );\r
200     if ( !mdrv_ret )\r
201     {\r
202       gl_volume.actsector = sector;\r
203       return F_NO_ERROR;\r
204     }\r
205 \r
206     if ( mdrv_ret == -1 )\r
207     {\r
208       gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/\r
209       return F_ERR_CARDREMOVED;\r
210     }\r
211   }\r
212 \r
213   gl_volume.actsector = (unsigned long)-1;\r
214   return F_ERR_ONDRIVE;\r
215 } /* _f_readglsector */\r
216 \r