ff_locking.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * FreeRTOS+FAT V2.3.3
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. /* Scheduler include files. */
  30. #include "FreeRTOS.h"
  31. #include "task.h"
  32. #include "semphr.h"
  33. #include "ff_headers.h"
  34. #include "event_groups.h"
  35. #ifndef configUSE_RECURSIVE_MUTEXES
  36. #error configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h
  37. #else
  38. #if ( configUSE_RECURSIVE_MUTEXES != 1 )
  39. #error configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h
  40. #endif
  41. #endif /* configUSE_RECURSIVE_MUTEXES */
  42. #if ( INCLUDE_vTaskDelay != 1 )
  43. #error Missing some FreeRTOS define
  44. #endif
  45. /* There are two areas which are protected with a semaphore:
  46. * Directories and the FAT area.
  47. * The masks below are used when calling Group Event functions. */
  48. #define FF_FAT_LOCK_EVENT_BITS ( ( const EventBits_t ) FF_FAT_LOCK )
  49. #define FF_DIR_LOCK_EVENT_BITS ( ( const EventBits_t ) FF_DIR_LOCK )
  50. /* This is not a real lock: it is a bit (or semaphore) will will be given
  51. * each time when a sector buffer is released. */
  52. #define FF_BUF_LOCK_EVENT_BITS ( ( const EventBits_t ) FF_BUF_LOCK )
  53. #ifndef FF_TIME_TO_WAIT_FOR_EVENT_TICKS
  54. /* The maximum time to wait for a event group bit to come high,
  55. * which gives access to a "critical section": either directories,
  56. * or the FAT. */
  57. #define FF_TIME_TO_WAIT_FOR_EVENT_TICKS pdMS_TO_TICKS( 10000UL )
  58. #endif
  59. /*-----------------------------------------------------------*/
  60. BaseType_t FF_TrySemaphore( void * pxSemaphore,
  61. uint32_t ulTime_ms )
  62. {
  63. BaseType_t xReturn;
  64. /* HT: Actually FF_TrySemaphore is never used. */
  65. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  66. {
  67. return 0;
  68. }
  69. configASSERT( pxSemaphore );
  70. xReturn = xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) pxSemaphore, pdMS_TO_TICKS( ulTime_ms ) );
  71. return xReturn;
  72. }
  73. /*-----------------------------------------------------------*/
  74. void FF_PendSemaphore( void * pxSemaphore )
  75. {
  76. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  77. {
  78. /* No need to take the semaphore. */
  79. return;
  80. }
  81. configASSERT( pxSemaphore );
  82. xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) pxSemaphore, portMAX_DELAY );
  83. }
  84. /*-----------------------------------------------------------*/
  85. void FF_ReleaseSemaphore( void * pxSemaphore )
  86. {
  87. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  88. {
  89. /* Scheduler not yet active. */
  90. return;
  91. }
  92. configASSERT( pxSemaphore );
  93. xSemaphoreGiveRecursive( ( SemaphoreHandle_t ) pxSemaphore );
  94. }
  95. /*-----------------------------------------------------------*/
  96. void FF_Sleep( uint32_t ulTime_ms )
  97. {
  98. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  99. {
  100. /* This sleep is used as a kind of yield.
  101. * Not necessary while the Scheduler does not run. */
  102. return;
  103. }
  104. vTaskDelay( pdMS_TO_TICKS( ulTime_ms ) );
  105. }
  106. /*-----------------------------------------------------------*/
  107. void FF_DeleteEvents( FF_IOManager_t * pxIOManager )
  108. {
  109. if( pxIOManager->xEventGroup != NULL )
  110. {
  111. vEventGroupDelete( pxIOManager->xEventGroup );
  112. }
  113. }
  114. /*-----------------------------------------------------------*/
  115. BaseType_t FF_CreateEvents( FF_IOManager_t * pxIOManager )
  116. {
  117. BaseType_t xResult;
  118. pxIOManager->xEventGroup = xEventGroupCreate();
  119. if( pxIOManager->xEventGroup != NULL )
  120. {
  121. xEventGroupSetBits( pxIOManager->xEventGroup,
  122. FF_FAT_LOCK_EVENT_BITS | FF_DIR_LOCK_EVENT_BITS | FF_BUF_LOCK_EVENT_BITS );
  123. xResult = pdTRUE;
  124. }
  125. else
  126. {
  127. xResult = pdFALSE;
  128. }
  129. return xResult;
  130. }
  131. /*-----------------------------------------------------------*/
  132. void FF_LockDirectory( FF_IOManager_t * pxIOManager )
  133. {
  134. EventBits_t xBits;
  135. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  136. {
  137. /* Scheduler not yet active. */
  138. return;
  139. }
  140. for( ; ; )
  141. {
  142. /* Called when a task want to make changes to a directory.
  143. * It waits for the desired bit to come high, and clears the
  144. * bit so that other tasks can not take it. */
  145. xBits = xEventGroupWaitBits( pxIOManager->xEventGroup,
  146. FF_DIR_LOCK_EVENT_BITS, /* uxBitsToWaitFor */
  147. pdTRUE, /* xClearOnExit */
  148. pdFALSE, /* xWaitForAllBits n.a. */
  149. FF_TIME_TO_WAIT_FOR_EVENT_TICKS );
  150. if( ( xBits & FF_DIR_LOCK_EVENT_BITS ) != 0 )
  151. {
  152. /* This task has cleared the desired bit.
  153. * It now 'owns' the resource. */
  154. break;
  155. }
  156. }
  157. }
  158. /*-----------------------------------------------------------*/
  159. void FF_UnlockDirectory( FF_IOManager_t * pxIOManager )
  160. {
  161. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  162. {
  163. /* Scheduler not yet active. */
  164. return;
  165. }
  166. configASSERT( ( xEventGroupGetBits( pxIOManager->xEventGroup ) & FF_DIR_LOCK_EVENT_BITS ) == 0 );
  167. xEventGroupSetBits( pxIOManager->xEventGroup, FF_DIR_LOCK_EVENT_BITS );
  168. }
  169. /*-----------------------------------------------------------*/
  170. int FF_Has_Lock( FF_IOManager_t * pxIOManager,
  171. uint32_t aBits )
  172. {
  173. int iReturn;
  174. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  175. {
  176. /* Scheduler not yet active. */
  177. return 0;
  178. }
  179. void * handle = xTaskGetCurrentTaskHandle();
  180. if( ( aBits & FF_FAT_LOCK_EVENT_BITS ) != 0 )
  181. {
  182. if( ( pxIOManager->pvFATLockHandle != NULL ) && ( pxIOManager->pvFATLockHandle == handle ) )
  183. {
  184. iReturn = pdTRUE;
  185. }
  186. else
  187. {
  188. iReturn = pdFALSE;
  189. }
  190. }
  191. else
  192. {
  193. iReturn = pdFALSE;
  194. }
  195. return iReturn;
  196. }
  197. void FF_Assert_Lock( FF_IOManager_t * pxIOManager,
  198. uint32_t aBits )
  199. {
  200. void * handle;
  201. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  202. {
  203. /* Scheduler not yet active. */
  204. return;
  205. }
  206. handle = xTaskGetCurrentTaskHandle();
  207. if( ( aBits & FF_FAT_LOCK_EVENT_BITS ) != 0 )
  208. {
  209. configASSERT( ( pxIOManager->pvFATLockHandle != NULL ) && ( pxIOManager->pvFATLockHandle == handle ) );
  210. /* In case configASSERT() is not defined. */
  211. ( void ) pxIOManager;
  212. ( void ) handle;
  213. }
  214. }
  215. void FF_LockFAT( FF_IOManager_t * pxIOManager )
  216. {
  217. EventBits_t xBits;
  218. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  219. {
  220. /* Scheduler not yet active. */
  221. return;
  222. }
  223. configASSERT( FF_Has_Lock( pxIOManager, FF_FAT_LOCK ) == pdFALSE );
  224. for( ; ; )
  225. {
  226. /* Called when a task want to make changes to the FAT area.
  227. * It waits for the desired bit to come high, and clears the
  228. * bit so that other tasks can not take it. */
  229. xBits = xEventGroupWaitBits( pxIOManager->xEventGroup,
  230. FF_FAT_LOCK_EVENT_BITS, /* uxBitsToWaitFor */
  231. pdTRUE, /* xClearOnExit */
  232. pdFALSE, /* xWaitForAllBits n.a. */
  233. FF_TIME_TO_WAIT_FOR_EVENT_TICKS );
  234. if( ( xBits & FF_FAT_LOCK_EVENT_BITS ) != 0 )
  235. {
  236. /* This task has cleared the desired bit.
  237. * It now 'owns' the resource. */
  238. pxIOManager->pvFATLockHandle = xTaskGetCurrentTaskHandle();
  239. break;
  240. }
  241. }
  242. }
  243. /*-----------------------------------------------------------*/
  244. void FF_UnlockFAT( FF_IOManager_t * pxIOManager )
  245. {
  246. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  247. {
  248. /* Scheduler not yet active. */
  249. return;
  250. }
  251. configASSERT( ( xEventGroupGetBits( pxIOManager->xEventGroup ) & FF_FAT_LOCK_EVENT_BITS ) == 0 );
  252. pxIOManager->pvFATLockHandle = NULL;
  253. xEventGroupSetBits( pxIOManager->xEventGroup, FF_FAT_LOCK_EVENT_BITS );
  254. }
  255. /*-----------------------------------------------------------*/
  256. BaseType_t FF_BufferWait( FF_IOManager_t * pxIOManager,
  257. uint32_t xWaitMS )
  258. {
  259. EventBits_t xBits;
  260. BaseType_t xReturn;
  261. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  262. {
  263. /* Scheduler not yet active. */
  264. return pdTRUE;
  265. }
  266. /* This function is called when a task is waiting for a sector buffer
  267. * to become available. Each time when a sector buffer becomes available,
  268. * the bit will be set ( see FF_BufferProceed() here below ). */
  269. xBits = xEventGroupWaitBits( pxIOManager->xEventGroup,
  270. FF_BUF_LOCK_EVENT_BITS, /* uxBitsToWaitFor */
  271. pdTRUE, /* xClearOnExit */
  272. pdFALSE, /* xWaitForAllBits n.a. */
  273. pdMS_TO_TICKS( xWaitMS ) );
  274. if( ( xBits & FF_BUF_LOCK_EVENT_BITS ) != 0 )
  275. {
  276. xReturn = pdTRUE;
  277. }
  278. else
  279. {
  280. xReturn = pdFALSE;
  281. }
  282. return xReturn;
  283. }
  284. /*-----------------------------------------------------------*/
  285. void FF_BufferProceed( FF_IOManager_t * pxIOManager )
  286. {
  287. if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
  288. {
  289. /* Scheduler not yet active. */
  290. return;
  291. }
  292. /* Wake-up a task that is waiting for a sector buffer to become available. */
  293. xEventGroupSetBits( pxIOManager->xEventGroup, FF_BUF_LOCK_EVENT_BITS );
  294. }
  295. /*-----------------------------------------------------------*/