µGFX library fork

gos_chibios.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. #include "../../gfx.h"
  8. #if GFX_USE_OS_CHIBIOS
  9. #include <string.h>
  10. #if CH_KERNEL_MAJOR == 2
  11. #if !CH_USE_MUTEXES
  12. #error "GOS: CH_USE_MUTEXES must be defined in chconf.h"
  13. #endif
  14. #if !CH_USE_SEMAPHORES
  15. #error "GOS: CH_USE_SEMAPHORES must be defined in chconf.h"
  16. #endif
  17. #elif (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
  18. #if !CH_CFG_USE_MUTEXES
  19. #error "GOS: CH_CFG_USE_MUTEXES must be defined in chconf.h"
  20. #endif
  21. #if !CH_CFG_USE_SEMAPHORES
  22. #error "GOS: CH_CFG_USE_SEMAPHORES must be defined in chconf.h"
  23. #endif
  24. #else
  25. #error "GOS: Unsupported version of ChibiOS"
  26. #endif
  27. void _gosInit(void)
  28. {
  29. #if !GFX_OS_NO_INIT
  30. /* Don't Initialize if the user already has */
  31. #if CH_KERNEL_MAJOR == 2
  32. if (!chThdSelf()) {
  33. halInit();
  34. chSysInit();
  35. }
  36. #elif (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
  37. if (!chThdGetSelfX()) {
  38. halInit();
  39. chSysInit();
  40. }
  41. #endif
  42. #elif !GFX_OS_INIT_NO_WARNING
  43. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  44. #warning "GOS: Operating System initialization has been turned off. Make sure you call halInit() and chSysInit() before gfxInit() in your application!"
  45. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  46. COMPILER_WARNING("GOS: Operating System initialization has been turned off. Make sure you call halInit() and chSysInit() before gfxInit() in your application!")
  47. #endif
  48. #endif
  49. }
  50. void _gosPostInit(void)
  51. {
  52. }
  53. void _gosDeinit(void)
  54. {
  55. /* ToDo */
  56. }
  57. void *gfxRealloc(void *ptr, size_t oldsz, size_t newsz)
  58. {
  59. void *np;
  60. if (newsz <= oldsz)
  61. return ptr;
  62. np = gfxAlloc(newsz);
  63. if (!np)
  64. return 0;
  65. if (oldsz)
  66. memcpy(np, ptr, oldsz);
  67. return np;
  68. }
  69. void gfxSleepMilliseconds(delaytime_t ms)
  70. {
  71. switch(ms) {
  72. case TIME_IMMEDIATE: chThdYield(); return;
  73. case TIME_INFINITE: chThdSleep(TIME_INFINITE); return;
  74. default: chThdSleepMilliseconds(ms); return;
  75. }
  76. }
  77. void gfxSleepMicroseconds(delaytime_t ms)
  78. {
  79. switch(ms) {
  80. case TIME_IMMEDIATE: return;
  81. case TIME_INFINITE: chThdSleep(TIME_INFINITE); return;
  82. default: chThdSleepMicroseconds(ms); return;
  83. }
  84. }
  85. void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit)
  86. {
  87. if (val > limit)
  88. val = limit;
  89. psem->limit = limit;
  90. #if CH_KERNEL_MAJOR == 2
  91. chSemInit(&psem->sem, val);
  92. #elif (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
  93. chSemObjectInit(&psem->sem, val);
  94. #endif
  95. }
  96. void gfxSemDestroy(gfxSem *psem)
  97. {
  98. chSemReset(&psem->sem, 1);
  99. }
  100. bool_t gfxSemWait(gfxSem *psem, delaytime_t ms)
  101. {
  102. #if CH_KERNEL_MAJOR == 2
  103. switch(ms) {
  104. case TIME_IMMEDIATE: return chSemWaitTimeout(&psem->sem, TIME_IMMEDIATE) != RDY_TIMEOUT;
  105. case TIME_INFINITE: chSemWait(&psem->sem); return TRUE;
  106. default: return chSemWaitTimeout(&psem->sem, MS2ST(ms)) != RDY_TIMEOUT;
  107. }
  108. #elif (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
  109. switch(ms) {
  110. case TIME_IMMEDIATE: return chSemWaitTimeout(&psem->sem, TIME_IMMEDIATE) != MSG_TIMEOUT;
  111. case TIME_INFINITE: chSemWait(&psem->sem); return TRUE;
  112. default: return chSemWaitTimeout(&psem->sem, MS2ST(ms)) != MSG_TIMEOUT;
  113. }
  114. #endif
  115. }
  116. bool_t gfxSemWaitI(gfxSem *psem)
  117. {
  118. if (chSemGetCounterI(&psem->sem) <= 0)
  119. return FALSE;
  120. chSemFastWaitI(&psem->sem);
  121. return TRUE;
  122. }
  123. void gfxSemSignal(gfxSem *psem)
  124. {
  125. chSysLock();
  126. if (gfxSemCounterI(psem) < psem->limit)
  127. chSemSignalI(&psem->sem);
  128. chSchRescheduleS();
  129. chSysUnlock();
  130. }
  131. void gfxSemSignalI(gfxSem *psem)
  132. {
  133. if (gfxSemCounterI(psem) < psem->limit)
  134. chSemSignalI(&psem->sem);
  135. }
  136. gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param)
  137. {
  138. if (!stackarea) {
  139. if (!stacksz) stacksz = 256;
  140. #if (CH_KERNEL_MAJOR == 2) || (CH_KERNEL_MAJOR == 3)
  141. return chThdCreateFromHeap(0, stacksz, prio, (tfunc_t)fn, param);
  142. #elif CH_KERNEL_MAJOR == 4
  143. return chThdCreateFromHeap(0, stacksz, "ugfx", prio, (tfunc_t)fn, param);
  144. #endif
  145. }
  146. if (!stacksz)
  147. return 0;
  148. return chThdCreateStatic(stackarea, stacksz, prio, (tfunc_t)fn, param);
  149. }
  150. #endif /* GFX_USE_OS_CHIBIOS */