uffs_rtthread.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /*
  10. This file is part of UFFS, the Ultra-low-cost Flash File System.
  11. Copyright (C) 2005-2009 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
  12. UFFS is free software; you can redistribute it and/or modify it under
  13. the GNU Library General Public License as published by the Free Software
  14. Foundation; either version 2 of the License, or (at your option) any
  15. later version.
  16. UFFS is distributed in the hope that it will be useful, but WITHOUT
  17. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19. or GNU Library General Public License, as applicable, for more details.
  20. You should have received a copy of the GNU General Public License
  21. and GNU Library General Public License along with UFFS; if not, write
  22. to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. Boston, MA 02110-1301, USA.
  24. As a special exception, if other files instantiate templates or use
  25. macros or inline functions from this file, or you compile this file
  26. and link it with other works to produce a work based on this file,
  27. this file does not by itself cause the resulting work to be covered
  28. by the GNU General Public License. However the source code for this
  29. file must still be made available in accordance with section (3) of
  30. the GNU General Public License v2.
  31. This exception does not invalidate any other reasons why a work based
  32. on this file might be covered by the GNU General Public License.
  33. */
  34. /**
  35. * \file uffs_os_posix.c
  36. * \brief Emulation on POSIX host. This is just a dumb implementation, does not really create semaphores.
  37. * \author Ricky Zheng
  38. */
  39. #include "uffs_config.h"
  40. #include "uffs/uffs_os.h"
  41. #include "uffs/uffs_public.h"
  42. //#include <time.h>
  43. #define PFX "os : "
  44. int uffs_SemCreate(OSSEM *sem)
  45. {
  46. static int count = 0;
  47. char name [RT_NAME_MAX+1];
  48. struct rt_mutex *mutex = RT_NULL;
  49. rt_snprintf(name, sizeof(name), "usem%d", count++);
  50. mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  51. if (mutex != RT_NULL)
  52. {
  53. *sem = (OSSEM *)mutex;
  54. return 0;
  55. }
  56. uffs_Perror(UFFS_MSG_SERIOUS, "can't get a semphore");
  57. return -1;
  58. }
  59. int uffs_SemWait(OSSEM sem)
  60. {
  61. return rt_mutex_take((struct rt_mutex *)sem, RT_WAITING_FOREVER);
  62. }
  63. int uffs_SemSignal(OSSEM sem)
  64. {
  65. return rt_mutex_release((struct rt_mutex *)sem);
  66. }
  67. int uffs_SemDelete(OSSEM *sem)
  68. {
  69. int ret = -1;
  70. if (sem) {
  71. ret = rt_mutex_delete((struct rt_mutex *)(*sem));
  72. if (ret == RT_EOK) {
  73. *sem = 0;
  74. }
  75. }
  76. return ret;
  77. }
  78. int uffs_OSGetTaskId(void)
  79. {
  80. //TODO: ... return current task ID ...
  81. return 0;
  82. }
  83. unsigned int uffs_GetCurDateTime(void)
  84. {
  85. // FIXME: return system time, please modify this for your platform !
  86. // or just return 0 if you don't care about file time.
  87. #if 0
  88. time_t tvalue;
  89. tvalue = time(NULL);
  90. return (unsigned int)tvalue;
  91. #endif
  92. return 0;
  93. }
  94. #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
  95. static void * sys_malloc(struct uffs_DeviceSt *dev, unsigned int size)
  96. {
  97. dev = dev;
  98. uffs_Perror(UFFS_MSG_NORMAL, "system memory alloc %d bytes", size);
  99. return rt_malloc(size);
  100. }
  101. static URET sys_free(struct uffs_DeviceSt *dev, void *p)
  102. {
  103. dev = dev;
  104. rt_free(p);
  105. return U_SUCC;
  106. }
  107. void uffs_MemSetupSystemAllocator(uffs_MemAllocator *allocator)
  108. {
  109. allocator->malloc = sys_malloc;
  110. allocator->free = sys_free;
  111. }
  112. #endif
  113. #if !defined(RT_THREAD)
  114. /* debug message output throught 'printf' */
  115. static void output_dbg_msg(const char *msg);
  116. static struct uffs_DebugMsgOutputSt m_dbg_ops = {
  117. output_dbg_msg,
  118. NULL,
  119. };
  120. static void output_dbg_msg(const char *msg)
  121. {
  122. rt_kprintf("%s", msg);
  123. }
  124. void uffs_SetupDebugOutput(void)
  125. {
  126. uffs_InitDebugMessageOutput(&m_dbg_ops, UFFS_MSG_NOISY);
  127. }
  128. #else
  129. void uffs_SetupDebugOutput(void)
  130. {
  131. }
  132. #endif