2 * FreeRTOS+FAT build 191128 - Note: FreeRTOS+FAT is still in the lab!
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
4 * Authors include James Walmsley, Hein Tibosch and Richard Barry
\r
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
7 * this software and associated documentation files (the "Software"), to deal in
\r
8 * the Software without restriction, including without limitation the rights to
\r
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
10 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
11 * subject to the following conditions:
\r
13 * The above copyright notice and this permission notice shall be included in all
\r
14 * copies or substantial portions of the Software.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * https://www.FreeRTOS.org
\r
31 /* Scheduler include files. */
\r
32 #include "FreeRTOS.h"
\r
35 #include "ff_headers.h"
\r
36 #include "event_groups.h"
\r
38 #ifndef configUSE_RECURSIVE_MUTEXES
\r
39 #error configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h
\r
41 #if( configUSE_RECURSIVE_MUTEXES != 1 )
\r
42 #error configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h
\r
44 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
46 #if ( INCLUDE_vTaskDelay != 1 )
\r
47 #error Missing some FreeRTOS define
\r
50 /* There are two areas which are protected with a semaphore:
\r
51 Directories and the FAT area.
\r
52 The masks below are used when calling Group Event functions. */
\r
53 #define FF_FAT_LOCK_EVENT_BITS ( ( const EventBits_t ) FF_FAT_LOCK )
\r
54 #define FF_DIR_LOCK_EVENT_BITS ( ( const EventBits_t ) FF_DIR_LOCK )
\r
56 /* This is not a real lock: it is a bit (or semaphore) will will be given
\r
57 each time when a sector buffer is released. */
\r
58 #define FF_BUF_LOCK_EVENT_BITS ( ( const EventBits_t ) FF_BUF_LOCK )
\r
60 /*-----------------------------------------------------------*/
\r
62 BaseType_t FF_TrySemaphore( void *pxSemaphore, uint32_t ulTime_ms )
\r
66 configASSERT( pxSemaphore );
\r
67 xReturn = xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) pxSemaphore, pdMS_TO_TICKS( ulTime_ms ) );
\r
70 /*-----------------------------------------------------------*/
\r
72 void FF_PendSemaphore( void *pxSemaphore )
\r
74 configASSERT( pxSemaphore );
\r
75 xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) pxSemaphore, portMAX_DELAY );
\r
77 /*-----------------------------------------------------------*/
\r
79 void FF_ReleaseSemaphore( void *pxSemaphore )
\r
81 configASSERT( pxSemaphore );
\r
82 xSemaphoreGiveRecursive( ( SemaphoreHandle_t ) pxSemaphore );
\r
84 /*-----------------------------------------------------------*/
\r
86 void FF_Sleep( uint32_t ulTime_ms )
\r
88 vTaskDelay( pdMS_TO_TICKS( ulTime_ms ) );
\r
90 /*-----------------------------------------------------------*/
\r
92 void FF_DeleteEvents( FF_IOManager_t *pxIOManager )
\r
94 if( pxIOManager->xEventGroup != NULL )
\r
96 vEventGroupDelete( pxIOManager->xEventGroup );
\r
99 /*-----------------------------------------------------------*/
\r
101 BaseType_t FF_CreateEvents( FF_IOManager_t *pxIOManager )
\r
103 BaseType_t xResult;
\r
105 pxIOManager->xEventGroup = xEventGroupCreate();
\r
106 if( pxIOManager->xEventGroup != NULL )
\r
108 xEventGroupSetBits( pxIOManager->xEventGroup,
\r
109 FF_FAT_LOCK_EVENT_BITS | FF_DIR_LOCK_EVENT_BITS | FF_BUF_LOCK_EVENT_BITS );
\r
119 /*-----------------------------------------------------------*/
\r
121 void FF_LockDirectory( FF_IOManager_t *pxIOManager )
\r
127 /* Called when a task want to make changes to a directory.
\r
128 First it waits for the desired bit to come high. */
\r
129 xEventGroupWaitBits( pxIOManager->xEventGroup,
\r
130 FF_DIR_LOCK_EVENT_BITS, /* uxBitsToWaitFor */
\r
131 ( EventBits_t )0, /* xClearOnExit */
\r
132 pdFALSE, /* xWaitForAllBits n.a. */
\r
133 pdMS_TO_TICKS( 10000UL ) );
\r
135 /* The next operation will only succeed for 1 task at a time,
\r
136 because it is an atomary test & set operation: */
\r
137 xBits = xEventGroupClearBits( pxIOManager->xEventGroup, FF_DIR_LOCK_EVENT_BITS );
\r
139 if( ( xBits & FF_DIR_LOCK_EVENT_BITS ) != 0 )
\r
141 /* This task has cleared the desired bit.
\r
142 It now 'owns' the resource. */
\r
147 /*-----------------------------------------------------------*/
\r
149 void FF_UnlockDirectory( FF_IOManager_t *pxIOManager )
\r
151 configASSERT( ( xEventGroupGetBits( pxIOManager->xEventGroup ) & FF_DIR_LOCK_EVENT_BITS ) == 0 );
\r
152 xEventGroupSetBits( pxIOManager->xEventGroup, FF_DIR_LOCK_EVENT_BITS );
\r
154 /*-----------------------------------------------------------*/
\r
156 int FF_Has_Lock( FF_IOManager_t *pxIOManager, uint32_t aBits )
\r
160 void *handle = xTaskGetCurrentTaskHandle();
\r
161 if( ( aBits & FF_FAT_LOCK_EVENT_BITS ) != 0 )
\r
163 if( ( pxIOManager->pvFATLockHandle != NULL ) && ( pxIOManager->pvFATLockHandle == handle ) )
\r
179 void FF_Assert_Lock( FF_IOManager_t *pxIOManager, uint32_t aBits )
\r
181 void *handle = xTaskGetCurrentTaskHandle();
\r
183 if( ( aBits & FF_FAT_LOCK_EVENT_BITS ) != 0 )
\r
185 configASSERT( ( pxIOManager->pvFATLockHandle != NULL ) && ( pxIOManager->pvFATLockHandle == handle ) );
\r
187 /* In case configASSERT() is not defined. */
\r
188 ( void ) pxIOManager;
\r
193 void FF_LockFAT( FF_IOManager_t *pxIOManager )
\r
197 configASSERT( FF_Has_Lock( pxIOManager, FF_FAT_LOCK ) == pdFALSE );
\r
201 /* Called when a task want to make changes to the FAT area.
\r
202 First it waits for the desired bit to come high. */
\r
203 xEventGroupWaitBits( pxIOManager->xEventGroup,
\r
204 FF_FAT_LOCK_EVENT_BITS, /* uxBitsToWaitFor */
\r
205 ( EventBits_t )0, /* xClearOnExit */
\r
206 pdFALSE, /* xWaitForAllBits n.a. */
\r
207 pdMS_TO_TICKS( 10000UL ) );
\r
209 /* The next operation will only succeed for 1 task at a time,
\r
210 because it is an atomary test & set operation: */
\r
211 xBits = xEventGroupClearBits( pxIOManager->xEventGroup, FF_FAT_LOCK_EVENT_BITS );
\r
213 if( ( xBits & FF_FAT_LOCK_EVENT_BITS ) != 0 )
\r
215 /* This task has cleared the desired bit.
\r
216 It now 'owns' the resource. */
\r
217 pxIOManager->pvFATLockHandle = xTaskGetCurrentTaskHandle();
\r
222 /*-----------------------------------------------------------*/
\r
224 void FF_UnlockFAT( FF_IOManager_t *pxIOManager )
\r
226 configASSERT( ( xEventGroupGetBits( pxIOManager->xEventGroup ) & FF_FAT_LOCK_EVENT_BITS ) == 0 );
\r
227 pxIOManager->pvFATLockHandle = NULL;
\r
228 xEventGroupSetBits( pxIOManager->xEventGroup, FF_FAT_LOCK_EVENT_BITS );
\r
230 /*-----------------------------------------------------------*/
\r
232 BaseType_t FF_BufferWait( FF_IOManager_t *pxIOManager, uint32_t xWaitMS )
\r
235 BaseType_t xReturn;
\r
237 /* This function is called when a task is waiting for a sector buffer
\r
238 to become available. */
\r
239 xBits = xEventGroupWaitBits( pxIOManager->xEventGroup,
\r
240 FF_BUF_LOCK_EVENT_BITS, /* uxBitsToWaitFor */
\r
241 FF_BUF_LOCK_EVENT_BITS, /* xClearOnExit */
\r
242 pdFALSE, /* xWaitForAllBits n.a. */
\r
243 pdMS_TO_TICKS( xWaitMS ) );
\r
244 if( ( xBits & FF_BUF_LOCK_EVENT_BITS ) != 0 )
\r
255 /*-----------------------------------------------------------*/
\r
257 void FF_BufferProceed( FF_IOManager_t *pxIOManager )
\r
259 /* Wake-up all tasks that are waiting for a sector buffer to become available. */
\r
260 xEventGroupSetBits( pxIOManager->xEventGroup, FF_BUF_LOCK_EVENT_BITS );
\r
262 /*-----------------------------------------------------------*/
\r