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
f_lock.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 #define FS_MUTEX_DEFINED
42 
43 #include "fat_sl.h"
44 #include "ver_fat_sl.h"
45 #if VER_FAT_SL_MAJOR != 5 || VER_FAT_SL_MINOR != 2
46  #error Incompatible FAT_SL version number!
47 #endif
48 
49 #if F_FS_THREAD_AWARE == 1
50 
51 xSemaphoreHandle fs_lock_semaphore;
52 
53 
54 /*
55 ** fr_findfirst
56 **
57 ** find first time a file using wildcards
58 **
59 ** INPUT : filename - name of the file
60 ** *find - pointer to a pre-define F_FIND structure
61 ** RETURN: F_NOERR - on success
62 ** F_ERR_NOTFOUND - if not found
63 */
64 unsigned char fr_findfirst ( const char * filename, F_FIND * find )
65 {
66  unsigned char rc;
67 
68  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
69  {
70  rc = fn_findfirst( filename, find );
71  xSemaphoreGive( fs_lock_semaphore );
72  }
73  else
74  {
75  rc = F_ERR_OS;
76  }
77 
78  return rc;
79 }
80 
81 
82 /*
83 ** fr_findnext
84 **
85 ** find next time a file using wildcards
86 **
87 ** INPUT : *find - pointer to a pre-define F_FIND structure
88 ** RETURN: F_NOERR - on success
89 ** F_ERR_NOTFOUND - if not found
90 */
91 unsigned char fr_findnext ( F_FIND * find )
92 {
93  unsigned char rc;
94 
95  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
96  {
97  rc = fn_findnext( find );
98  xSemaphoreGive( fs_lock_semaphore );
99  }
100  else
101  {
102  rc = F_ERR_OS;
103  }
104 
105  return rc;
106 }
107 
108 
109 /*
110 ** fr_filelength
111 **
112 ** Get the length of a file
113 **
114 ** INPUT : filename - name of the file
115 ** RETURN: size of the file or F_ERR_INVALID if not exists or volume not working
116 */
117 long fr_filelength ( const char * filename )
118 {
119  unsigned long rc;
120 
121  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
122  {
123  rc = fn_filelength( filename );
124  xSemaphoreGive( fs_lock_semaphore );
125  }
126  else
127  {
128  rc = 0;
129  }
130 
131  return rc;
132 }
133 
134 
135 /*
136 ** fr_open
137 **
138 ** open a file
139 **
140 ** INPUT : filename - file to be opened
141 ** mode - open method (r,w,a,r+,w+,a+)
142 ** RETURN: pointer to a file descriptor or 0 on error
143 */
144 F_FILE * fr_open ( const char * filename, const char * mode )
145 {
146  F_FILE * rc;
147 
148  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
149  {
150  rc = fn_open( filename, mode );
151  xSemaphoreGive( fs_lock_semaphore );
152  }
153  else
154  {
155  rc = NULL;
156  }
157 
158  return rc;
159 }
160 
161 
162 /*
163 ** fr_close
164 **
165 ** Close a previously opened file.
166 **
167 ** INPUT : *filehandle - pointer to the file descriptor
168 ** RETURN: F_NOERR on success, other if error
169 */
170 unsigned char fr_close ( F_FILE * filehandle )
171 {
172  unsigned char rc;
173 
174  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
175  {
176  rc = fn_close( filehandle );
177  xSemaphoreGive( fs_lock_semaphore );
178  }
179  else
180  {
181  rc = F_ERR_OS;
182  }
183 
184  return rc;
185 }
186 
187 
188 /*
189 ** fr_read
190 **
191 ** Read from a file.
192 **
193 ** INPUT : buf - buffer to read data
194 ** size - number of unique
195 ** size_st - size of unique
196 ** *filehandle - pointer to file descriptor
197 ** OUTPUT: number of read bytes
198 */
199 long fr_read ( void * bbuf, long size, long size_st, F_FILE * filehandle )
200 {
201  long rc;
202 
203  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
204  {
205  rc = fn_read( bbuf, size, size_st, filehandle );
206  xSemaphoreGive( fs_lock_semaphore );
207  }
208  else
209  {
210  rc = 0;
211  }
212 
213  return rc;
214 }
215 
216 
217 /*
218 ** fr_write
219 **
220 ** INPUT : bbuf - buffer to write from
221 ** size - number of unique
222 ** size_st - size of unique
223 ** *filehandle - pointer to the file descriptor
224 ** RETURN: number of written bytes
225 */
226 long fr_write ( const void * bbuf, long size, long size_st, F_FILE * filehandle )
227 {
228  long rc;
229 
230  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
231  {
232  rc = fn_write( bbuf, size, size_st, filehandle );
233  xSemaphoreGive( fs_lock_semaphore );
234  }
235  else
236  {
237  rc = 0;
238  }
239 
240  return rc;
241 }
242 
243 /*
244 ** fr_seek
245 **
246 ** Seek in a file.
247 **
248 ** INPUT : *filehandle - pointer to a file descriptor
249 ** offset - offset
250 ** whence - F_SEEK_SET: position = offset
251 ** F_SEEK_CUR: position = position + offset
252 ** F_SEEK_END: position = end of file (offset=0)
253 ** RETURN: F_NOERR on succes, other if error.
254 */
255 unsigned char fr_seek ( F_FILE * filehandle, long offset, unsigned char whence )
256 {
257  unsigned char rc;
258 
259  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
260  {
261  rc = fn_seek( filehandle, offset, whence );
262  xSemaphoreGive( fs_lock_semaphore );
263  }
264  else
265  {
266  rc = F_ERR_OS;
267  }
268 
269  return rc;
270 }
271 
272 /*
273 ** fr_tell
274 **
275 ** get current position in the file
276 **
277 ** INPUT : *filehandle - pointer to a file descriptor
278 ** RETURN: -1 on error or current position.
279 */
280 long fr_tell ( F_FILE * filehandle )
281 {
282  long rc;
283 
284  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
285  {
286  rc = fn_tell( filehandle );
287  xSemaphoreGive( fs_lock_semaphore );
288  }
289  else
290  {
291  rc = -1;
292  }
293 
294  return rc;
295 }
296 
297 /*
298 ** fr_getc
299 **
300 ** read one byte from a file
301 **
302 ** INPUT : *filehandle - pointer to a file descriptor
303 ** RETURN: -1 if error, otherwise the read character.
304 */
305 int fr_getc ( F_FILE * filehandle )
306 {
307  int rc;
308 
309  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
310  {
311  rc = fn_getc( filehandle );
312  xSemaphoreGive( fs_lock_semaphore );
313  }
314  else
315  {
316  rc = -1;
317  }
318 
319  return rc;
320 }
321 
322 /*
323 ** fr_putc
324 **
325 ** write one byte to a file
326 **
327 ** INPUT : ch - character to write
328 ** *filehandle - pointer to a file handler
329 ** RETURN: ch on success, -1 on error
330 */
331 int fr_putc ( int ch, F_FILE * filehandle )
332 {
333  int rc;
334 
335  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
336  {
337  rc = fn_putc( ch, filehandle );
338  xSemaphoreGive( fs_lock_semaphore );
339  }
340  else
341  {
342  rc = -1;
343  }
344 
345  return rc;
346 }
347 
348 /*
349 ** fr_rewind
350 **
351 ** set current position in the file to the beginning
352 **
353 ** INPUT : *filehandle - pointer to a file descriptor
354 ** RETURN: F_NOERR on succes, other if error.
355 */
356 unsigned char fr_rewind ( F_FILE * filehandle )
357 {
358  unsigned char rc;
359 
360  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
361  {
362  rc = fn_rewind( filehandle );
363  xSemaphoreGive( fs_lock_semaphore );
364  }
365  else
366  {
367  rc = F_ERR_OS;
368  }
369 
370  return rc;
371 }
372 
373 /*
374 ** fr_eof
375 **
376 ** check if current position is at the end of the file.
377 **
378 ** INPUT : *filehandle - pointer to a file descriptor
379 ** RETURN: F_ERR_EOF - at the end of the file
380 ** F_NOERR - no error, end of the file not reached
381 ** other - on error
382 */
383 unsigned char fr_eof ( F_FILE * filehandle )
384 {
385  unsigned char rc;
386 
387  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
388  {
389  rc = fn_eof( filehandle );
390  xSemaphoreGive( fs_lock_semaphore );
391  }
392  else
393  {
394  rc = F_ERR_OS;
395  }
396 
397  return rc;
398 }
399 
400 /*
401 ** Format the device
402 */
403 unsigned char fr_hardformat ( unsigned char fattype )
404 {
405  unsigned char rc;
406 
407  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
408  {
409  rc = fn_hardformat( fattype );
410  xSemaphoreGive( fs_lock_semaphore );
411  }
412  else
413  {
414  rc = F_ERR_OS;
415  }
416 
417  return rc;
418 }
419 
420 /*
421 ** fr_get_serial
422 **
423 ** Get serial number
424 **
425 ** OUTPUT: serial - where to write the serial number
426 ** RETURN: error code
427 */
428 unsigned char fr_getserial ( unsigned long * serial )
429 {
430  unsigned char rc;
431 
432  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
433  {
434  rc = fn_getserial( serial );
435  xSemaphoreGive( fs_lock_semaphore );
436  }
437  else
438  {
439  rc = F_ERR_OS;
440  }
441 
442  return rc;
443 }
444 
445 
446 /*
447 ** fr_delete
448 **
449 ** Delete a file. Removes the chain that belongs to the file and inserts a new descriptor
450 ** to the directory with first_cluster set to 0.
451 **
452 ** INPUT : filename - name of the file to delete
453 ** RETURN: F_NOERR on success, other if error.
454 */
455 unsigned char fr_delete ( const char * filename )
456 {
457  unsigned char rc;
458 
459  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
460  {
461  rc = fn_delete( filename );
462  xSemaphoreGive( fs_lock_semaphore );
463  }
464  else
465  {
466  rc = F_ERR_OS;
467  }
468 
469  return rc;
470 }
471 
472 /*
473 ** fr_truncate
474 **
475 ** Open a file and set end of file
476 **
477 ** INPUT: filename - name of the file
478 ** filesize - required new size
479 ** RETURN: NULL on error, otherwise file pointer
480 */
481 F_FILE * fr_truncate ( const char * filename, long filesize )
482 {
483  F_FILE * f;
484 
485  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
486  {
487  f = fn_truncate( filename, filesize );
488  xSemaphoreGive( fs_lock_semaphore );
489  }
490  else
491  {
492  f = NULL;
493  }
494 
495  return f;
496 }
497 
498 
499 /*
500 ** fr_getfreespace
501 **
502 ** Get free space on the volume
503 **
504 ** OUTPUT: *sp - pre-defined F_SPACE structure, where information will be stored
505 ** RETURN: F_NOERR - on success
506 ** F_ERR_NOTFORMATTED - if volume is not formatted
507 */
508 unsigned char fr_getfreespace ( F_SPACE * sp )
509 {
510  unsigned char rc;
511 
512  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
513  {
514  rc = fn_getfreespace( sp );
515  xSemaphoreGive( fs_lock_semaphore );
516  }
517  else
518  {
519  rc = F_ERR_OS;
520  }
521 
522  return rc;
523 }
524 
525 
526 /*
527 ** fr_chdir
528 **
529 ** Change to a directory
530 **
531 ** INPUT: path - path to the dircetory
532 ** RETURN: 0 - on success, other if error
533 */
534 unsigned char fr_chdir ( const char * path )
535 {
536  unsigned char rc;
537 
538  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
539  {
540  rc = fn_chdir( path );
541  xSemaphoreGive( fs_lock_semaphore );
542  }
543  else
544  {
545  rc = F_ERR_OS;
546  }
547 
548  return rc;
549 }
550 
551 
552 /*
553 ** fr_mkdir
554 **
555 ** Create a directory
556 **
557 ** INPUT: path - new directory path
558 ** RETURN: 0 - on success, other if error
559 */
560 unsigned char fr_mkdir ( const char * path )
561 {
562  unsigned char rc;
563 
564  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
565  {
566  rc = fn_mkdir( path );
567  xSemaphoreGive( fs_lock_semaphore );
568  }
569  else
570  {
571  rc = F_ERR_OS;
572  }
573 
574  return rc;
575 }
576 
577 
578 /*
579 ** fr_rmdir
580 **
581 ** Removes a directory
582 **
583 ** INPUT: path - path to remove
584 ** RETURN: 0 - on success, other if error
585 */
586 unsigned char fr_rmdir ( const char * path )
587 {
588  unsigned char rc;
589 
590  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
591  {
592  rc = fn_rmdir( path );
593  xSemaphoreGive( fs_lock_semaphore );
594  }
595  else
596  {
597  rc = F_ERR_OS;
598  }
599 
600  return rc;
601 }
602 
603 
604 /*
605 ** fr_getcwd
606 **
607 ** Get current working directory
608 **
609 ** INPUT: maxlen - maximum length allowed
610 ** OUTPUT: path - current working directory
611 ** RETURN: 0 - on success, other if error
612 */
613 unsigned char fr_getcwd ( char * path, unsigned char maxlen, char root )
614 {
615  unsigned char rc;
616 
617  if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
618  {
619  rc = fn_getcwd( path, maxlen, root );
620  xSemaphoreGive( fs_lock_semaphore );
621  }
622  else
623  {
624  rc = F_ERR_OS;
625  }
626 
627  return rc;
628 }
629 
630 
631 /*
632 ** fr_init
633 **
634 ** Initialize FAT_SL OS module
635 **
636 ** RETURN: F_NO_ERROR or F_ERR_OS
637 */
638 unsigned char fr_init ( void )
639 {
640  return F_NO_ERROR;
641 }
642 
643 #endif /* F_FS_THREAD_AWARE */
644 
long fn_read(void *buf, long size, long _size_t, F_FILE *filehandle)
Definition: file.c:851
unsigned char fn_chdir(const char *dirname)
Definition: dir.c:665
long fn_tell(F_FILE *filehandle)
Definition: file.c:1184
unsigned char fn_getcwd(char *buffer, unsigned char maxlen, char root)
Definition: dir.c:513
unsigned char fn_hardformat(unsigned char fattype)
Definition: volume.c:530
unsigned char fn_mkdir(const char *dirname)
Definition: dir.c:925
#define xSemaphoreHandle
Definition: FreeRTOS.h:731
unsigned char fn_eof(F_FILE *filehandle)
Definition: file.c:1218
#define F_MAX_LOCK_WAIT_TICKS
Definition: config_fat_sl.h:60
unsigned char fn_seek(F_FILE *filehandle, long offset, unsigned char whence)
Definition: file.c:1130
unsigned char fn_findnext(F_FIND *find)
Definition: dir.c:608
unsigned char fn_close(F_FILE *filehandle)
Definition: file.c:704
F_FILE * fn_open(const char *filename, const char *mode)
Definition: file.c:421
unsigned char fn_findfirst(const char *filename, F_FIND *find)
Definition: dir.c:562
unsigned char fn_delete(const char *filename)
Definition: file.c:1336
int fn_putc(int ch, F_FILE *filehandle)
Definition: file.c:1276
int fn_getc(F_FILE *filehandle)
Definition: file.c:1307
Definition: fat_sl.h:73
unsigned char fn_rmdir(const char *dirname)
Definition: dir.c:1088
#define pdPASS
Definition: projdefs.h:78
#define xSemaphoreGive(xSemaphore)
Definition: semphr.h:419
#define xSemaphoreTake(xSemaphore, xBlockTime)
Definition: semphr.h:248
long fn_filelength(const char *filename)
Definition: file.c:75
F_FILE * fn_truncate(const char *, long)
Definition: file.c:1518
long fn_write(const void *buf, long size, long _size_t, F_FILE *filehandle)
Definition: file.c:969
Definition: fat_sl.h:107
unsigned char fn_rewind(F_FILE *filehandle)
Definition: file.c:1252
unsigned char fn_getfreespace(F_SPACE *pspace)
Definition: volume.c:787
unsigned char fn_getserial(unsigned long *)
Definition: volume.c:859