The official µGFX library repository.

gos_freertos.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. void _gosInit(void)
  20. {
  21. #if !GFX_OS_NO_INIT
  22. #error "GOS: Operating System initialization for FreeRTOS is not yet implemented in uGFX. Please set GFX_OS_NO_INIT to TRUE in your gfxconf.h"
  23. #endif
  24. #if !GFX_OS_INIT_NO_WARNING
  25. #warning "GOS: Operating System initialization has been turned off. Make sure you call vTaskStartScheduler() before gfxInit() in your application!"
  26. #endif
  27. }
  28. void _gosDeinit(void)
  29. {
  30. }
  31. void* gfxRealloc(void *ptr, size_t oldsz, size_t newsz)
  32. {
  33. void *np;
  34. if (newsz <= oldsz)
  35. return ptr;
  36. np = gfxAlloc(newsz);
  37. if (!np)
  38. return 0;
  39. if (oldsz) {
  40. memcpy(np, ptr, oldsz);
  41. vPortFree(ptr);
  42. }
  43. return np;
  44. }
  45. void gfxSleepMilliseconds(delaytime_t ms)
  46. {
  47. const portTickType ticks = ms / portTICK_PERIOD_MS;
  48. vTaskDelay(ticks);
  49. }
  50. void gfxSleepMicroseconds(delaytime_t ms)
  51. {
  52. const portTickType ticks = (ms / 1000) / portTICK_PERIOD_MS;
  53. // delay milli seconds
  54. vTaskDelay(ticks);
  55. // microsecond resolution delay is not supported in FreeRTOS
  56. // vUsDelay(ms%1000);
  57. }
  58. portTickType MS2ST(portTickType ms)
  59. {
  60. return (ms / portTICK_PERIOD_MS);
  61. }
  62. void gfxMutexInit(xSemaphoreHandle *s)
  63. {
  64. *s = xSemaphoreCreateMutex();
  65. #if GFX_FREERTOS_USE_TRACE
  66. vTraceSetMutexName(*s,"uGFXMutex"); // for FreeRTOS+Trace debug
  67. #endif
  68. }
  69. void gfxSemInit(gfxSem* psem, semcount_t val, semcount_t limit)
  70. {
  71. if (val > limit)
  72. val = limit;
  73. psem->counter = val;
  74. psem->limit = limit;
  75. psem->sem = xSemaphoreCreateCounting(limit,val);
  76. #if GFX_FREERTOS_USE_TRACE
  77. vTraceSetSemaphoreName(psem->sem, "uGFXSema"); // for FreeRTOS+Trace debug
  78. #endif
  79. }
  80. void gfxSemDestroy(gfxSem* psem)
  81. {
  82. vSemaphoreDelete(psem->sem);
  83. }
  84. bool_t gfxSemWait(gfxSem* psem, delaytime_t ms)
  85. {
  86. psem->counter--;
  87. if (xSemaphoreTake(psem->sem, MS2ST(ms)) == pdPASS)
  88. return TRUE;
  89. psem->counter++;
  90. return FALSE;
  91. }
  92. bool_t gfxSemWaitI(gfxSem* psem)
  93. {
  94. portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  95. psem->counter--;
  96. if (xSemaphoreTakeFromISR(psem->sem,&xHigherPriorityTaskWoken) == pdTRUE)
  97. return TRUE;
  98. psem->counter++;
  99. return FALSE;
  100. }
  101. void gfxSemSignal(gfxSem* psem)
  102. {
  103. taskENTER_CRITICAL();
  104. if (psem->counter < psem->limit) {
  105. psem->counter++;
  106. xSemaphoreGive(psem->sem);
  107. }
  108. taskYIELD();
  109. taskEXIT_CRITICAL();
  110. }
  111. void gfxSemSignalI(gfxSem* psem)
  112. {
  113. portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  114. if (psem->counter < psem->limit) {
  115. psem->counter++;
  116. xSemaphoreGiveFromISR(psem->sem,&xHigherPriorityTaskWoken);
  117. }
  118. }
  119. gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param)
  120. {
  121. xTaskHandle task = NULL;
  122. stacksz = (size_t)stackarea;
  123. if (stacksz < configMINIMAL_STACK_SIZE)
  124. stacksz = configMINIMAL_STACK_SIZE;
  125. if (xTaskCreate(fn, "uGFX_TASK", stacksz, param, prio, &task )!= pdPASS) {
  126. for (;;);
  127. }
  128. return task;
  129. }
  130. #endif /* GFX_USE_OS_FREERTOS */