µGFX library fork

gos_freertos.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <string.h>
  9. #if GFX_USE_OS_FREERTOS
  10. #if INCLUDE_vTaskDelay != 1
  11. #error "GOS: INCLUDE_vTaskDelay must be defined in FreeRTOSConfig.h"
  12. #endif
  13. #if configUSE_MUTEXES != 1
  14. #error "GOS: configUSE_MUTEXES must be defined in FreeRTOSConfig.h"
  15. #endif
  16. #if configUSE_COUNTING_SEMAPHORES != 1
  17. #error "GOS: configUSE_COUNTING_SEMAPHORES must be defined in FreeRTOSConfig.h"
  18. #endif
  19. #if !GFX_OS_NO_INIT && INCLUDE_xTaskGetSchedulerState != 1 && configUSE_TIMERS != 1
  20. #error "GOS: Either INCLUDE_xTaskGetSchedulerState or configUSE_TIMERS must be defined in FreeRTOSConfig.h"
  21. #endif
  22. #if !GFX_OS_NO_INIT && !GFX_OS_CALL_UGFXMAIN
  23. #error "GOS: Either GFX_OS_NO_INIT or GFX_OS_CALL_UGFXMAIN must be defined for FreeRTOS"
  24. #endif
  25. void _gosInit(void)
  26. {
  27. #if GFX_OS_NO_INIT && !GFX_OS_INIT_NO_WARNING
  28. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  29. #warning "GOS: Operating System initialization has been turned off. Make sure you call vTaskStartScheduler()."
  30. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  31. COMPILER_WARNING("GOS: Operating System initialization has been turned off. Make sure you call vTaskStartScheduler().")
  32. #endif
  33. #endif
  34. }
  35. #if !GFX_OS_NO_INIT && GFX_OS_CALL_UGFXMAIN
  36. static DECLARE_THREAD_FUNCTION(startUGFX_FreeRTOS, p) {
  37. (void) p;
  38. uGFXMain();
  39. }
  40. #endif
  41. void _gosPostInit(void)
  42. {
  43. #if !GFX_OS_NO_INIT && GFX_OS_CALL_UGFXMAIN
  44. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  45. gfxThreadCreate(0, GFX_OS_UGFXMAIN_STACKSIZE, NORMAL_PRIORITY, startUGFX_FreeRTOS, 0);
  46. vTaskStartScheduler();
  47. gfxHalt("Unable to start FreeRTOS scheduler. Out of memory?");
  48. }
  49. #endif
  50. }
  51. void _gosDeinit(void)
  52. {
  53. #if !GFX_OS_NO_INIT
  54. vTaskDelete(0);
  55. #endif
  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. gfxFree(ptr);
  68. }
  69. return np;
  70. }
  71. void gfxSleepMilliseconds(delaytime_t ms)
  72. {
  73. vTaskDelay(gfxMillisecondsToTicks(ms));
  74. }
  75. void gfxSleepMicroseconds(delaytime_t ms)
  76. {
  77. // delay milli seconds - microsecond resolution delay is not supported in FreeRTOS
  78. vTaskDelay(gfxMillisecondsToTicks(ms/1000));
  79. // vUsDelay(ms%1000);
  80. }
  81. void gfxMutexInit(gfxMutex *pmutex)
  82. {
  83. *pmutex = xSemaphoreCreateMutex();
  84. #if GFX_FREERTOS_USE_TRACE
  85. vTraceSetMutexName(*pmutex,"uGFXMutex");
  86. #endif
  87. }
  88. void gfxSemInit(gfxSem* psem, semcount_t val, semcount_t limit)
  89. {
  90. if (val > limit)
  91. val = limit;
  92. *psem = xSemaphoreCreateCounting(limit,val);
  93. #if GFX_FREERTOS_USE_TRACE
  94. vTraceSetSemaphoreName(*psem, "uGFXSema");
  95. #endif
  96. }
  97. bool_t gfxSemWait(gfxSem* psem, delaytime_t ms)
  98. {
  99. if (xSemaphoreTake(*psem, gfxMillisecondsToTicks(ms)) == pdPASS)
  100. return TRUE;
  101. return FALSE;
  102. }
  103. bool_t gfxSemWaitI(gfxSem* psem)
  104. {
  105. portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  106. if (xSemaphoreTakeFromISR(*psem, &xHigherPriorityTaskWoken) == pdTRUE)
  107. return TRUE;
  108. return FALSE;
  109. }
  110. void gfxSemSignal(gfxSem* psem)
  111. {
  112. xSemaphoreGive(*psem);
  113. taskYIELD();
  114. }
  115. void gfxSemSignalI(gfxSem* psem)
  116. {
  117. portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  118. xSemaphoreGiveFromISR(*psem,&xHigherPriorityTaskWoken);
  119. }
  120. gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param)
  121. {
  122. gfxThreadHandle task;
  123. (void) stackarea;
  124. // uGFX expresses stack size in bytes - FreeRTOS in "Stack Words"
  125. stacksz /= sizeof(StackType_t);
  126. if (stacksz < configMINIMAL_STACK_SIZE)
  127. stacksz = configMINIMAL_STACK_SIZE;
  128. task = 0;
  129. if (xTaskCreate(fn, "uGFX_TASK", stacksz, param, prio, &task) != pdPASS)
  130. return 0;
  131. return task;
  132. }
  133. #if INCLUDE_eTaskGetState == 1
  134. threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
  135. while (eTaskGetState(thread) != eDeleted)
  136. gfxYield();
  137. }
  138. #endif
  139. #endif /* GFX_USE_OS_FREERTOS */