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 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
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
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
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
34 * http://www.FreeRTOS.org
\r
35 * http://www.FreeRTOS.org/FreeRTOS-Plus
\r
39 #define FS_MUTEX_DEFINED
\r
41 #include "../../api/fat_sl.h"
\r
42 #include "../../version/ver_fat_sl.h"
\r
43 #if VER_FAT_SL_MAJOR != 3 || VER_FAT_SL_MINOR != 2
\r
44 #error Incompatible FAT_SL version number!
\r
47 #if F_FS_THREAD_AWARE == 1
\r
49 xSemaphoreHandle fs_lock_semaphore;
\r
55 ** find first time a file using wildcards
\r
57 ** INPUT : filename - name of the file
\r
58 ** *find - pointer to a pre-define F_FIND structure
\r
59 ** RETURN: F_NOERR - on success
\r
60 ** F_ERR_NOTFOUND - if not found
\r
62 unsigned char fr_findfirst ( const char * filename, F_FIND * find )
\r
66 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
68 rc = fn_findfirst( filename, find );
\r
69 xSemaphoreGive( fs_lock_semaphore );
\r
83 ** find next time a file using wildcards
\r
85 ** INPUT : *find - pointer to a pre-define F_FIND structure
\r
86 ** RETURN: F_NOERR - on success
\r
87 ** F_ERR_NOTFOUND - if not found
\r
89 unsigned char fr_findnext ( F_FIND * find )
\r
93 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
95 rc = fn_findnext( find );
\r
96 xSemaphoreGive( fs_lock_semaphore );
\r
110 ** Get the length of a file
\r
112 ** INPUT : filename - name of the file
\r
113 ** RETURN: size of the file or F_ERR_INVALID if not exists or volume not working
\r
115 long fr_filelength ( const char * filename )
\r
119 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
121 rc = fn_filelength( filename );
\r
122 xSemaphoreGive( fs_lock_semaphore );
\r
138 ** INPUT : filename - file to be opened
\r
139 ** mode - open method (r,w,a,r+,w+,a+)
\r
140 ** RETURN: pointer to a file descriptor or 0 on error
\r
142 F_FILE * fr_open ( const char * filename, const char * mode )
\r
146 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
148 rc = fn_open( filename, mode );
\r
149 xSemaphoreGive( fs_lock_semaphore );
\r
163 ** Close a previously opened file.
\r
165 ** INPUT : *filehandle - pointer to the file descriptor
\r
166 ** RETURN: F_NOERR on success, other if error
\r
168 unsigned char fr_close ( F_FILE * filehandle )
\r
172 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
174 rc = fn_close( filehandle );
\r
175 xSemaphoreGive( fs_lock_semaphore );
\r
189 ** Read from a file.
\r
191 ** INPUT : buf - buffer to read data
\r
192 ** size - number of unique
\r
193 ** size_st - size of unique
\r
194 ** *filehandle - pointer to file descriptor
\r
195 ** OUTPUT: number of read bytes
\r
197 long fr_read ( void * bbuf, long size, long size_st, F_FILE * filehandle )
\r
201 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
203 rc = fn_read( bbuf, size, size_st, filehandle );
\r
204 xSemaphoreGive( fs_lock_semaphore );
\r
218 ** INPUT : bbuf - buffer to write from
\r
219 ** size - number of unique
\r
220 ** size_st - size of unique
\r
221 ** *filehandle - pointer to the file descriptor
\r
222 ** RETURN: number of written bytes
\r
224 long fr_write ( const void * bbuf, long size, long size_st, F_FILE * filehandle )
\r
228 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
230 rc = fn_write( bbuf, size, size_st, filehandle );
\r
231 xSemaphoreGive( fs_lock_semaphore );
\r
246 ** INPUT : *filehandle - pointer to a file descriptor
\r
248 ** whence - F_SEEK_SET: position = offset
\r
249 ** F_SEEK_CUR: position = position + offset
\r
250 ** F_SEEK_END: position = end of file (offset=0)
\r
251 ** RETURN: F_NOERR on succes, other if error.
\r
253 unsigned char fr_seek ( F_FILE * filehandle, long offset, unsigned char whence )
\r
257 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
259 rc = fn_seek( filehandle, offset, whence );
\r
260 xSemaphoreGive( fs_lock_semaphore );
\r
273 ** get current position in the file
\r
275 ** INPUT : *filehandle - pointer to a file descriptor
\r
276 ** RETURN: -1 on error or current position.
\r
278 long fr_tell ( F_FILE * filehandle )
\r
282 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
284 rc = fn_tell( filehandle );
\r
285 xSemaphoreGive( fs_lock_semaphore );
\r
298 ** read one byte from a file
\r
300 ** INPUT : *filehandle - pointer to a file descriptor
\r
301 ** RETURN: -1 if error, otherwise the read character.
\r
303 int fr_getc ( F_FILE * filehandle )
\r
307 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
309 rc = fn_getc( filehandle );
\r
310 xSemaphoreGive( fs_lock_semaphore );
\r
323 ** write one byte to a file
\r
325 ** INPUT : ch - character to write
\r
326 ** *filehandle - pointer to a file handler
\r
327 ** RETURN: ch on success, -1 on error
\r
329 int fr_putc ( int ch, F_FILE * filehandle )
\r
333 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
335 rc = fn_putc( ch, filehandle );
\r
336 xSemaphoreGive( fs_lock_semaphore );
\r
349 ** set current position in the file to the beginning
\r
351 ** INPUT : *filehandle - pointer to a file descriptor
\r
352 ** RETURN: F_NOERR on succes, other if error.
\r
354 unsigned char fr_rewind ( F_FILE * filehandle )
\r
358 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
360 rc = fn_rewind( filehandle );
\r
361 xSemaphoreGive( fs_lock_semaphore );
\r
374 ** check if current position is at the end of the file.
\r
376 ** INPUT : *filehandle - pointer to a file descriptor
\r
377 ** RETURN: F_ERR_EOF - at the end of the file
\r
378 ** F_NOERR - no error, end of the file not reached
\r
379 ** other - on error
\r
381 unsigned char fr_eof ( F_FILE * filehandle )
\r
385 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
387 rc = fn_eof( filehandle );
\r
388 xSemaphoreGive( fs_lock_semaphore );
\r
399 ** Format the device
\r
401 unsigned char fr_hardformat ( unsigned char fattype )
\r
405 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
407 rc = fn_hardformat( fattype );
\r
408 xSemaphoreGive( fs_lock_semaphore );
\r
421 ** Get serial number
\r
423 ** OUTPUT: serial - where to write the serial number
\r
424 ** RETURN: error code
\r
426 unsigned char fr_getserial ( unsigned long * serial )
\r
430 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
432 rc = fn_getserial( serial );
\r
433 xSemaphoreGive( fs_lock_semaphore );
\r
447 ** Delete a file. Removes the chain that belongs to the file and inserts a new descriptor
\r
448 ** to the directory with first_cluster set to 0.
\r
450 ** INPUT : filename - name of the file to delete
\r
451 ** RETURN: F_NOERR on success, other if error.
\r
453 unsigned char fr_delete ( const char * filename )
\r
457 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
459 rc = fn_delete( filename );
\r
460 xSemaphoreGive( fs_lock_semaphore );
\r
473 ** Open a file and set end of file
\r
475 ** INPUT: filename - name of the file
\r
476 ** filesize - required new size
\r
477 ** RETURN: NULL on error, otherwise file pointer
\r
479 F_FILE * fr_truncate ( const char * filename, long filesize )
\r
483 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
485 f = fn_truncate( filename, filesize );
\r
486 xSemaphoreGive( fs_lock_semaphore );
\r
500 ** Get free space on the volume
\r
502 ** OUTPUT: *sp - pre-defined F_SPACE structure, where information will be stored
\r
503 ** RETURN: F_NOERR - on success
\r
504 ** F_ERR_NOTFORMATTED - if volume is not formatted
\r
506 unsigned char fr_getfreespace ( F_SPACE * sp )
\r
510 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
512 rc = fn_getfreespace( sp );
\r
513 xSemaphoreGive( fs_lock_semaphore );
\r
527 ** Change to a directory
\r
529 ** INPUT: path - path to the dircetory
\r
530 ** RETURN: 0 - on success, other if error
\r
532 unsigned char fr_chdir ( const char * path )
\r
536 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
538 rc = fn_chdir( path );
\r
539 xSemaphoreGive( fs_lock_semaphore );
\r
553 ** Create a directory
\r
555 ** INPUT: path - new directory path
\r
556 ** RETURN: 0 - on success, other if error
\r
558 unsigned char fr_mkdir ( const char * path )
\r
562 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
564 rc = fn_mkdir( path );
\r
565 xSemaphoreGive( fs_lock_semaphore );
\r
579 ** Removes a directory
\r
581 ** INPUT: path - path to remove
\r
582 ** RETURN: 0 - on success, other if error
\r
584 unsigned char fr_rmdir ( const char * path )
\r
588 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
590 rc = fn_rmdir( path );
\r
591 xSemaphoreGive( fs_lock_semaphore );
\r
605 ** Get current working directory
\r
607 ** INPUT: maxlen - maximum length allowed
\r
608 ** OUTPUT: path - current working directory
\r
609 ** RETURN: 0 - on success, other if error
\r
611 unsigned char fr_getcwd ( char * path, unsigned char maxlen, char root )
\r
615 if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS )
\r
617 rc = fn_getcwd( path, maxlen, root );
\r
618 xSemaphoreGive( fs_lock_semaphore );
\r
632 ** Initialize FAT_SL OS module
\r
634 ** RETURN: F_NO_ERROR or F_ERR_OS
\r
636 unsigned char fr_init ( void )
\r
641 #endif /* F_FS_THREAD_AWARE */
\r