SAM4S-EK_FreeRTOS+FAT-SL  1.0
An example project to test the functionality of FreeRTOS+FAT-SL using SD card as data storage medium
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
drv.c
Go to the documentation of this file.
1 /*
2  * FreeRTOS+FAT SL V1.0.1 (C) 2014 HCC Embedded
3  *
4  * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license
5  * terms.
6  *
7  * FreeRTOS+FAT SL uses a dual license model that allows the software to be used
8  * under a standard GPL open source license, or a commercial license. The
9  * standard GPL license (unlike the modified GPL license under which FreeRTOS
10  * itself is distributed) requires that all software statically linked with
11  * FreeRTOS+FAT SL is also distributed under the same GPL V2 license terms.
12  * Details of both license options follow:
13  *
14  * - Open source licensing -
15  * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and
16  * distributed without charge provided the user adheres to version two of the
17  * GNU General Public License (GPL) and does not remove the copyright notice or
18  * this text. The GPL V2 text is available on the gnu.org web site, and on the
19  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
20  *
21  * - Commercial licensing -
22  * Businesses and individuals who for commercial or other reasons cannot comply
23  * with the terms of the GPL V2 license must obtain a commercial license before
24  * incorporating FreeRTOS+FAT SL into proprietary software for distribution in
25  * any form. Commercial licenses can be purchased from
26  * http://shop.freertos.org/fat_sl and do not require any source files to be
27  * changed.
28  *
29  * FreeRTOS+FAT SL is distributed in the hope that it will be useful. You
30  * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as
31  * is'. FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the
32  * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A
33  * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all
34  * conditions and terms, be they implied, expressed, or statutory.
35  *
36  * http://www.FreeRTOS.org
37  * http://www.FreeRTOS.org/FreeRTOS-Plus
38  *
39  */
40 
41 #include "fat_sl.h"
42 #include "psp_string.h"
43 
44 #include "drv.h"
45 #include "util.h"
46 #include "volume.h"
47 
48 #include "ver_fat_sl.h"
49 #if VER_FAT_SL_MAJOR != 5 || VER_FAT_SL_MINOR != 2
50  #error Incompatible FAT_SL version number!
51 #endif
52 
53 F_DRIVER * mdrv = NULL; /* driver structure */
54 
55 
56 /****************************************************************************
57  *
58  * _f_checkstatus
59  *
60  * checking a volume driver status, if media is removed or has been changed
61  *
62  * RETURNS
63  *
64  * error code or zero if successful
65  *
66  ***************************************************************************/
67 unsigned char _f_checkstatus ( void )
68 {
69  if ( mdrv->getstatus != NULL )
70  {
71  if ( mdrv->getstatus( mdrv ) & ( F_ST_MISSING | F_ST_CHANGED ) )
72  {
73  gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/
74  return F_ERR_CARDREMOVED;
75  }
76  }
77 
78  return F_NO_ERROR;
79 }
80 
81 
82 /****************************************************************************
83  *
84  * _f_writesector
85  *
86  * write sector data on a volume, it calls low level driver function, it
87  * writes a complete sector
88  *
89  * INPUTS
90  * sector - which physical sector
91  *
92  * RETURNS
93  * error code or zero if successful
94  *
95  ***************************************************************************/
96 unsigned char _f_writeglsector ( unsigned long sector )
97 {
98  unsigned char retry;
99 
100  if ( mdrv->writesector == NULL )
101  {
102  gl_volume.state = F_STATE_NEEDMOUNT; /*no write function*/
103  return F_ERR_ACCESSDENIED;
104  }
105 
106  if ( sector == (unsigned long)-1 )
107  {
108  if ( gl_file.modified )
109  {
110  sector = gl_file.pos.sector;
111  }
112  else
113  {
114  sector = gl_volume.actsector;
115  }
116  }
117 
118  if ( sector != (unsigned long)-1 )
119  {
120  if ( mdrv->getstatus != NULL )
121  {
122  unsigned int status;
123 
124  status = mdrv->getstatus( mdrv );
125 
126  if ( status & ( F_ST_MISSING | F_ST_CHANGED ) )
127  {
128  gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/
129  return F_ERR_CARDREMOVED;
130  }
131 
132  if ( status & ( F_ST_WRPROTECT ) )
133  {
134  gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/
135  return F_ERR_WRITEPROTECT;
136  }
137  }
138 
139  gl_volume.modified = 0;
140  gl_file.modified = 0;
141  gl_volume.actsector = sector;
142  for ( retry = 3 ; retry ; retry-- )
143  {
144  int mdrv_ret;
145  mdrv_ret = mdrv->writesector( mdrv, (unsigned char *)gl_sector, sector );
146  if ( !mdrv_ret )
147  {
148  return F_NO_ERROR;
149  }
150 
151  if ( mdrv_ret == -1 )
152  {
153  gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/
154  return F_ERR_CARDREMOVED;
155  }
156  }
157  }
158 
159 
160  return F_ERR_ONDRIVE;
161 } /* _f_writeglsector */
162 
163 
164 /****************************************************************************
165  *
166  * _f_readsector
167  *
168  * read sector data from a volume, it calls low level driver function, it
169  * reads a complete sector
170  *
171  * INPUTS
172  * sector - which physical sector is read
173  *
174  * RETURNS
175  * error code or zero if successful
176  *
177  ***************************************************************************/
178 unsigned char _f_readglsector ( unsigned long sector )
179 {
180  unsigned char retry;
181  unsigned char ret;
182 
183  if ( sector == gl_volume.actsector )
184  {
185  return F_NO_ERROR;
186  }
187 
189  {
190  ret = _f_writeglsector( (unsigned long)-1 );
191  if ( ret )
192  {
193  return ret;
194  }
195  }
196 
197 
198  for ( retry = 3 ; retry ; retry-- )
199  {
200  int mdrv_ret;
201  mdrv_ret = mdrv->readsector( mdrv, (unsigned char *)gl_sector, sector );
202  if ( !mdrv_ret )
203  {
204  gl_volume.actsector = sector;
205  return F_NO_ERROR;
206  }
207 
208  if ( mdrv_ret == -1 )
209  {
210  gl_volume.state = F_STATE_NEEDMOUNT; /*card has been removed;*/
211  return F_ERR_CARDREMOVED;
212  }
213  }
214 
215  gl_volume.actsector = (unsigned long)-1;
216  return F_ERR_ONDRIVE;
217 } /* _f_readglsector */
218 
unsigned char modified
Definition: volume.h:87
F_FILE gl_file
Definition: volume.c:61
unsigned long actsector
Definition: volume.h:83
#define F_ST_CHANGED
Definition: api_mdriver.h:71
unsigned char _f_writeglsector(unsigned long sector)
Definition: drv.c:96
unsigned char modified
Definition: fat_sl.h:113
F_DRIVER * mdrv
Definition: drv.c:53
unsigned long sector
Definition: fat_sl.h:68
F_POS pos
Definition: fat_sl.h:116
F_READSECTOR readsector
Definition: api_mdriver.h:90
F_VOLUME gl_volume
Definition: volume.c:60
unsigned char _f_readglsector(unsigned long sector)
Definition: drv.c:178
#define F_ST_WRPROTECT
Definition: api_mdriver.h:72
F_WRITESECTOR writesector
Definition: api_mdriver.h:89
char gl_sector[F_SECTOR_SIZE]
Definition: volume.c:62
#define F_ST_MISSING
Definition: api_mdriver.h:70
unsigned char state
Definition: volume.h:77
unsigned char _f_checkstatus(void)
Definition: drv.c:67
F_GETSTATUS getstatus
Definition: api_mdriver.h:92