Files
2026-01-12 18:46:01 +08:00

125 lines
3.4 KiB
C

#include <FreeRTOS.h>
#include <portable.h>
#include <semphr.h>
#include <stdint.h>
#include <stdlib.h>
#include <task.h>
#include "ff.h"
/* Dynamic memory allocation */
#if FF_USE_LFN == 3
/**
* @brief Allocate a memory block with 512-byte alignment.
*
* This function is used to allocate a memory block with a specified size and
* ensure that the allocated memory is aligned to a 512-byte boundary. It is
* designed to work with the FreeRTOS operating system.
*
* @param msize The size of the memory block to allocate, in bytes.
* @return A pointer to the allocated memory block, or NULL if the allocation
* fails.
*/
void *ff_memalloc(UINT msize)
{
/* Use osAllocAlign alloc 512 bytes aligned buf */
/* FreeRTOS */
return pvPortMallocAligned(msize, 512);
}
/**
* @brief Free a previously allocated memory block.
*
* This function is used to free a memory block that was previously allocated
* using the `ff_memalloc` function. It uses the FreeRTOS API `vPortFree` to
* release the memory.
*
* @param mblock A pointer to the memory block to be freed.
*/
void ff_memfree(void *mblock)
{
/* Free the memory block with POSIX API */
/* FreeRTOS */
vPortFree(mblock);
}
#endif
/* Mutal exclusion */
#if FF_FS_REENTRANT
/**
* @brief Create a synchronization object for a specific volume.
*
* This function is used to create a synchronization object (mutex semaphore)
* for a given volume. It is designed to work with the FreeRTOS operating
* system.
*
* @param vol The volume number for which the synchronization object is created.
* @param sobj A pointer to the synchronization object handle.
* @return Returns 1 if the synchronization object is created successfully, 0
* otherwise.
*/
int ff_cre_syncobj(BYTE vol, FF_SYNC_t *sobj)
{
/* FreeRTOS */
*sobj = (FF_SYNC_t)xSemaphoreCreateMutex();
return (int)(*sobj != NULL);
}
/**
* @brief Delete a synchronization object.
*
* This function is called in the `f_mount()` function to delete a
* synchronization object that was previously created with the
* `ff_cre_syncobj()` function.
*
* @param sobj A pointer to the synchronization object to be deleted.
* @return Returns 1 to indicate successful deletion. If 0 is returned, the
* `f_mount()` function fails with `FR_INT_ERR`.
*/
int ff_del_syncobj(FF_SYNC_t sobj)
{
/* FreeRTOS */
vSemaphoreDelete(sobj);
return 1;
}
/**
* @brief Request a grant to access the volume.
*
* This function is called on entering file functions to lock the volume.
* It attempts to take the semaphore associated with the volume within the
* specified timeout. If the semaphore is successfully taken within the timeout,
* it means the access to the volume is granted.
*
* @param sobj A pointer to the synchronization object (semaphore) for the
* volume.
* @return Returns 1 if the semaphore is successfully taken within the timeout,
* 0 otherwise. When a 0 is returned, the file function fails with FR_TIMEOUT.
*/
int ff_req_grant(FF_SYNC_t sobj)
{
/* FreeRTOS */
return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
}
/**
* @brief Release the grant to access the volume.
*
* This function is called on leaving file functions to unlock the volume.
* It releases the semaphore associated with the volume, allowing other tasks to
* access it.
*
* @param sobj A pointer to the synchronization object (semaphore) for the
* volume.
*/
void ff_rel_grant(FF_SYNC_t sobj)
{
/* FreeRTOS */
xSemaphoreGive(sobj);
}
#endif