fatfs.c 3.64 KB
Newer Older
swym's avatar
swym committed
1 2 3 4
/*
 * support routines for FatFs
 */

5 6
#include <errno.h>
#include <stddef.h>
swym's avatar
swym committed
7 8
#include <stdio.h>
#include <stdbool.h>
9 10
#include <stdlib.h>

swym's avatar
swym committed
11 12 13 14 15
#include <ff.h>

#include <FreeRTOS.h>
#include <semphr.h>

16
#include "modules.h"
17
#include "epicardium.h"
18 19 20 21 22

#ifndef EPIC_FAT_STATIC_SEMAPHORE
#define EPIC_FAT_STATIC_SEMAPHORE 0
#endif

23
static const TCHAR *rcstrings =
Rahix's avatar
Rahix committed
24 25 26 27
	_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
	_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
	_T("NOT_ENABLED\0NO_FILESYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
	_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0INVALID_PARAMETER\0");
28

swym's avatar
swym committed
29 30 31 32
static bool mount(void);

DIR dir;
FATFS FatFs;
33 34 35 36

#if (EPIC_FAT_STATIC_SEMAPHORE == 1)
StaticSemaphore_t xSemaphoreBuffer;
#endif
swym's avatar
swym committed
37

38
static volatile bool s_fatfs_initiaized = false;
swym's avatar
swym committed
39

40 41
void fatfs_init()
{
Rahix's avatar
Rahix committed
42
	if (mount()) {
43
		s_fatfs_initiaized = true;
Rahix's avatar
Rahix committed
44 45
		printf("FatFs mounted\n");
	}
46 47 48 49
}

const char *f_get_rc_string(FRESULT rc)
{
Rahix's avatar
Rahix committed
50 51 52 53 54 55 56 57
	FRESULT i;
	const char *p = rcstrings;

	for (i = 0; i != rc && *p; i++) {
		while (*p++)
			;
	}
	return p;
58 59 60
}

static bool mount()
swym's avatar
swym committed
61
{
Rahix's avatar
Rahix committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
	FRESULT res;
	res = f_mount(&FatFs, "/", 0);
	if (res != FR_OK) {
		printf("f_mount error %s\n", f_get_rc_string(res));
		return false;
	}

	res = f_opendir(&dir, "0:");
	if (res != FR_OK) {
		printf("f_opendir error %s\n", f_get_rc_string(res));
		return false;
	}

	return true;
swym's avatar
swym committed
76
}
77

78 79 80 81 82 83 84
/*------------------------------------------------------------------------*/
/* Create a Synchronization Object */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to create a new
/  synchronization object for the volume, such as semaphore and mutex.
/  When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
*/
swym's avatar
swym committed
85

Rahix's avatar
Rahix committed
86 87 88 89 90 91
/*
 * Return value:
 *   - 1: Function succeeded
 *   - 0: Could not create the sync object
 */
int ff_cre_syncobj(BYTE vol, FF_SYNC_t *sobj)
swym's avatar
swym committed
92
{
93
#if (EPIC_FAT_STATIC_SEMAPHORE == 1)
Rahix's avatar
Rahix committed
94
	*sobj = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer);
95
#else
Rahix's avatar
Rahix committed
96
	*sobj = xSemaphoreCreateMutex();
97 98
#endif //EPIC_FAT_STATIC_SEMAPHORE

Rahix's avatar
Rahix committed
99
	return (int)(*sobj != NULL);
swym's avatar
swym committed
100 101 102 103 104 105 106 107 108 109
}

/*------------------------------------------------------------------------*/
/* Delete a Synchronization Object                                        */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization
/  object that created with ff_cre_syncobj() function. When a 0 is returned,
/  the f_mount() function fails with FR_INT_ERR.
*/

Rahix's avatar
Rahix committed
110 111 112 113 114 115
/*
 * Return value:
 *   - 1: Function succeeded
 *   - 0: Could not delete due to an error
 */
int ff_del_syncobj(FF_SYNC_t sobj)
swym's avatar
swym committed
116
{
Rahix's avatar
Rahix committed
117 118 119
	/* FreeRTOS */
	vSemaphoreDelete(sobj);
	return 1;
swym's avatar
swym committed
120 121 122 123 124 125 126 127 128
}

/*------------------------------------------------------------------------*/
/* Request Grant to Access the Volume                                     */
/*------------------------------------------------------------------------*/
/* This function is called on entering file functions to lock the volume.
/  When a 0 is returned, the file function fails with FR_TIMEOUT.
*/

Rahix's avatar
Rahix committed
129 130 131 132 133 134
/*
 * Return value:
 *   - 1: Got a grant to access the volume
 *   - 0: Could not get a grant
 */
int ff_req_grant(FF_SYNC_t sobj)
swym's avatar
swym committed
135
{
Rahix's avatar
Rahix committed
136 137
	/* FreeRTOS */
	return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
swym's avatar
swym committed
138 139 140 141 142 143 144 145
}

/*------------------------------------------------------------------------*/
/* Release Grant to Access the Volume                                     */
/*------------------------------------------------------------------------*/
/* This function is called on leaving file functions to unlock the volume.
*/

Rahix's avatar
Rahix committed
146
void ff_rel_grant(FF_SYNC_t sobj)
swym's avatar
swym committed
147
{
Rahix's avatar
Rahix committed
148 149
	/* FreeRTOS */
	xSemaphoreGive(sobj);
150
}