uffs_rtthread.c 3.8 KB

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