2 * FreeRTOS+FAT FS V1.0.0 (C) 2013 HCC Embedded
\r
4 * The FreeRTOS+FAT SL license terms are different to the FreeRTOS license
\r
7 * FreeRTOS+FAT SL uses a dual license model that allows the software to be used
\r
8 * under a standard GPL open source license, or a commercial license. The
\r
9 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
10 * itself is distributed) requires that all software statically linked with
\r
11 * FreeRTOS+FAT SL is also distributed under the same GPL V2 license terms.
\r
12 * Details of both license options follow:
\r
14 * - Open source licensing -
\r
15 * FreeRTOS+FAT SL is a free download and may be used, modified, evaluated and
\r
16 * distributed without charge provided the user adheres to version two of the
\r
17 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
18 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
19 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
21 * - Commercial licensing -
\r
22 * Businesses and individuals who for commercial or other reasons cannot comply
\r
23 * with the terms of the GPL V2 license must obtain a commercial license before
\r
24 * incorporating FreeRTOS+FAT SL into proprietary software for distribution in
\r
25 * any form. Commercial licenses can be purchased from
\r
26 * http://shop.freertos.org/fat_sl and do not require any source files to be
\r
29 * FreeRTOS+FAT SL is distributed in the hope that it will be useful. You
\r
30 * cannot use FreeRTOS+FAT SL unless you agree that you use the software 'as
\r
31 * is'. FreeRTOS+FAT SL is provided WITHOUT ANY WARRANTY; without even the
\r
32 * implied warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A
\r
33 * PARTICULAR PURPOSE. Real Time Engineers Ltd. and HCC Embedded disclaims all
\r
34 * conditions and terms, be they implied, expressed, or statutory.
\r
36 * http://www.FreeRTOS.org
\r
37 * http://www.FreeRTOS.org/FreeRTOS-Plus
\r
41 #define FS_MUTEX_DEFINED
\r
43 #include "../../api/fat_sl.h"
\r
44 #include "../../version/ver_fat_sl.h"
\r
45 #if VER_FAT_SL_MAJOR != 3 || VER_FAT_SL_MINOR != 2
\r
46 #error Incompatible FAT_SL version number!
\r
49 #if F_FS_THREAD_AWARE == 1
\r
51 xSemaphoreHandle fs_lock_semaphore;
\r
57 ** find first time a file using wildcards
\r
59 ** INPUT : filename - name of the file
\r
60 ** *find - pointer to a pre-define F_FIND structure
\r
61 ** RETURN: F_NOERR - on success
\r
62 ** F_ERR_NOTFOUND - if not found
\r
64 unsigned char fr_findfirst ( const char * filename, F_FIND * find )
\r
68 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
70 rc = fn_findfirst( filename, find );
\r
71 xSemaphoreGive( fs_lock_semaphore );
\r
85 ** find next time a file using wildcards
\r
87 ** INPUT : *find - pointer to a pre-define F_FIND structure
\r
88 ** RETURN: F_NOERR - on success
\r
89 ** F_ERR_NOTFOUND - if not found
\r
91 unsigned char fr_findnext ( F_FIND * find )
\r
95 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
97 rc = fn_findnext( find );
\r
98 xSemaphoreGive( fs_lock_semaphore );
\r
112 ** Get the length of a file
\r
114 ** INPUT : filename - name of the file
\r
115 ** RETURN: size of the file or F_ERR_INVALID if not exists or volume not working
\r
117 long fr_filelength ( const char * filename )
\r
121 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
123 rc = fn_filelength( filename );
\r
124 xSemaphoreGive( fs_lock_semaphore );
\r
140 ** INPUT : filename - file to be opened
\r
141 ** mode - open method (r,w,a,r+,w+,a+)
\r
142 ** RETURN: pointer to a file descriptor or 0 on error
\r
144 F_FILE * fr_open ( const char * filename, const char * mode )
\r
148 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
150 rc = fn_open( filename, mode );
\r
151 xSemaphoreGive( fs_lock_semaphore );
\r
165 ** Close a previously opened file.
\r
167 ** INPUT : *filehandle - pointer to the file descriptor
\r
168 ** RETURN: F_NOERR on success, other if error
\r
170 unsigned char fr_close ( F_FILE * filehandle )
\r
174 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
176 rc = fn_close( filehandle );
\r
177 xSemaphoreGive( fs_lock_semaphore );
\r
191 ** Read from a file.
\r
193 ** INPUT : buf - buffer to read data
\r
194 ** size - number of unique
\r
195 ** size_st - size of unique
\r
196 ** *filehandle - pointer to file descriptor
\r
197 ** OUTPUT: number of read bytes
\r
199 long fr_read ( void * bbuf, long size, long size_st, F_FILE * filehandle )
\r
203 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
205 rc = fn_read( bbuf, size, size_st, filehandle );
\r
206 xSemaphoreGive( fs_lock_semaphore );
\r
220 ** INPUT : bbuf - buffer to write from
\r
221 ** size - number of unique
\r
222 ** size_st - size of unique
\r
223 ** *filehandle - pointer to the file descriptor
\r
224 ** RETURN: number of written bytes
\r
226 long fr_write ( const void * bbuf, long size, long size_st, F_FILE * filehandle )
\r
230 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
232 rc = fn_write( bbuf, size, size_st, filehandle );
\r
233 xSemaphoreGive( fs_lock_semaphore );
\r
248 ** INPUT : *filehandle - pointer to a file descriptor
\r
250 ** whence - F_SEEK_SET: position = offset
\r
251 ** F_SEEK_CUR: position = position + offset
\r
252 ** F_SEEK_END: position = end of file (offset=0)
\r
253 ** RETURN: F_NOERR on succes, other if error.
\r
255 unsigned char fr_seek ( F_FILE * filehandle, long offset, unsigned char whence )
\r
259 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
261 rc = fn_seek( filehandle, offset, whence );
\r
262 xSemaphoreGive( fs_lock_semaphore );
\r
275 ** get current position in the file
\r
277 ** INPUT : *filehandle - pointer to a file descriptor
\r
278 ** RETURN: -1 on error or current position.
\r
280 long fr_tell ( F_FILE * filehandle )
\r
284 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
286 rc = fn_tell( filehandle );
\r
287 xSemaphoreGive( fs_lock_semaphore );
\r
300 ** read one byte from a file
\r
302 ** INPUT : *filehandle - pointer to a file descriptor
\r
303 ** RETURN: -1 if error, otherwise the read character.
\r
305 int fr_getc ( F_FILE * filehandle )
\r
309 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
311 rc = fn_getc( filehandle );
\r
312 xSemaphoreGive( fs_lock_semaphore );
\r
325 ** write one byte to a file
\r
327 ** INPUT : ch - character to write
\r
328 ** *filehandle - pointer to a file handler
\r
329 ** RETURN: ch on success, -1 on error
\r
331 int fr_putc ( int ch, F_FILE * filehandle )
\r
335 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
337 rc = fn_putc( ch, filehandle );
\r
338 xSemaphoreGive( fs_lock_semaphore );
\r
351 ** set current position in the file to the beginning
\r
353 ** INPUT : *filehandle - pointer to a file descriptor
\r
354 ** RETURN: F_NOERR on succes, other if error.
\r
356 unsigned char fr_rewind ( F_FILE * filehandle )
\r
360 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
362 rc = fn_rewind( filehandle );
\r
363 xSemaphoreGive( fs_lock_semaphore );
\r
376 ** check if current position is at the end of the file.
\r
378 ** INPUT : *filehandle - pointer to a file descriptor
\r
379 ** RETURN: F_ERR_EOF - at the end of the file
\r
380 ** F_NOERR - no error, end of the file not reached
\r
381 ** other - on error
\r
383 unsigned char fr_eof ( F_FILE * filehandle )
\r
387 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
389 rc = fn_eof( filehandle );
\r
390 xSemaphoreGive( fs_lock_semaphore );
\r
401 ** Format the device
\r
403 unsigned char fr_hardformat ( unsigned char fattype )
\r
407 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
409 rc = fn_hardformat( fattype );
\r
410 xSemaphoreGive( fs_lock_semaphore );
\r
423 ** Get serial number
\r
425 ** OUTPUT: serial - where to write the serial number
\r
426 ** RETURN: error code
\r
428 unsigned char fr_getserial ( unsigned long * serial )
\r
432 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
434 rc = fn_getserial( serial );
\r
435 xSemaphoreGive( fs_lock_semaphore );
\r
449 ** Delete a file. Removes the chain that belongs to the file and inserts a new descriptor
\r
450 ** to the directory with first_cluster set to 0.
\r
452 ** INPUT : filename - name of the file to delete
\r
453 ** RETURN: F_NOERR on success, other if error.
\r
455 unsigned char fr_delete ( const char * filename )
\r
459 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
461 rc = fn_delete( filename );
\r
462 xSemaphoreGive( fs_lock_semaphore );
\r
475 ** Open a file and set end of file
\r
477 ** INPUT: filename - name of the file
\r
478 ** filesize - required new size
\r
479 ** RETURN: NULL on error, otherwise file pointer
\r
481 F_FILE * fr_truncate ( const char * filename, long filesize )
\r
485 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
487 f = fn_truncate( filename, filesize );
\r
488 xSemaphoreGive( fs_lock_semaphore );
\r
502 ** Get free space on the volume
\r
504 ** OUTPUT: *sp - pre-defined F_SPACE structure, where information will be stored
\r
505 ** RETURN: F_NOERR - on success
\r
506 ** F_ERR_NOTFORMATTED - if volume is not formatted
\r
508 unsigned char fr_getfreespace ( F_SPACE * sp )
\r
512 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
514 rc = fn_getfreespace( sp );
\r
515 xSemaphoreGive( fs_lock_semaphore );
\r
529 ** Change to a directory
\r
531 ** INPUT: path - path to the dircetory
\r
532 ** RETURN: 0 - on success, other if error
\r
534 unsigned char fr_chdir ( const char * path )
\r
538 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
540 rc = fn_chdir( path );
\r
541 xSemaphoreGive( fs_lock_semaphore );
\r
555 ** Create a directory
\r
557 ** INPUT: path - new directory path
\r
558 ** RETURN: 0 - on success, other if error
\r
560 unsigned char fr_mkdir ( const char * path )
\r
564 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
566 rc = fn_mkdir( path );
\r
567 xSemaphoreGive( fs_lock_semaphore );
\r
581 ** Removes a directory
\r
583 ** INPUT: path - path to remove
\r
584 ** RETURN: 0 - on success, other if error
\r
586 unsigned char fr_rmdir ( const char * path )
\r
590 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
592 rc = fn_rmdir( path );
\r
593 xSemaphoreGive( fs_lock_semaphore );
\r
607 ** Get current working directory
\r
609 ** INPUT: maxlen - maximum length allowed
\r
610 ** OUTPUT: path - current working directory
\r
611 ** RETURN: 0 - on success, other if error
\r
613 unsigned char fr_getcwd ( char * path, unsigned char maxlen, char root )
\r
617 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
619 rc = fn_getcwd( path, maxlen, root );
\r
620 xSemaphoreGive( fs_lock_semaphore );
\r
634 ** Initialize FAT_SL OS module
\r
636 ** RETURN: F_NO_ERROR or F_ERR_OS
\r
638 unsigned char fr_init ( void )
\r
643 #endif /* F_FS_THREAD_AWARE */
\r