diff options
author | Joel Bodenmann <joel@unormal.org> | 2014-09-29 08:07:43 +0200 |
---|---|---|
committer | inmarket <andrewh@inmarket.com.au> | 2014-09-29 16:22:35 +1000 |
commit | 8efdd2c51419ad7a38a6594ecabe729c6ab824e4 (patch) | |
tree | 38a6dd324e04c4076f62dce2671ae88b10b96bfc /src/gos/gos_freertos.c | |
parent | b7a89b2adaa9ee170d20a0454309ed204a6ffa39 (diff) | |
download | uGFX-8efdd2c51419ad7a38a6594ecabe729c6ab824e4.tar.gz uGFX-8efdd2c51419ad7a38a6594ecabe729c6ab824e4.tar.bz2 uGFX-8efdd2c51419ad7a38a6594ecabe729c6ab824e4.zip |
/src/gos/gfx_* -> /src/gos/gos_*
Diffstat (limited to 'src/gos/gos_freertos.c')
-rw-r--r-- | src/gos/gos_freertos.c | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/gos/gos_freertos.c b/src/gos/gos_freertos.c new file mode 100644 index 00000000..dbdfd22e --- /dev/null +++ b/src/gos/gos_freertos.c @@ -0,0 +1,170 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#include "gfx.h" +#include <string.h> + +#if GFX_USE_OS_FREERTOS + +#if INCLUDE_vTaskDelay != 1 + #error "GOS: INCLUDE_vTaskDelay must be defined in FreeRTOSConfig.h" +#endif + +#if configUSE_MUTEXES != 1 + #error "GOS: configUSE_MUTEXES must be defined in FreeRTOSConfig.h" +#endif + +#if configUSE_COUNTING_SEMAPHORES != 1 + #error "GOS: configUSE_COUNTING_SEMAPHORES must be defined in FreeRTOSConfig.h" +#endif + +void _gosInit(void) +{ + #if !GFX_NO_OS_INIT + #error "GOS: Operating System initialization for FreeRTOS is not yet implemented in uGFX. Please set GFX_NO_OS_INIT to TRUE in your gfxconf.h" + #else + #warning "GOS: Operating System initialization has been turned off. Make sure you call vTaskStartScheduler() before gfxInit() in your application!" + #endif +} + +void _gosDeinit(void) +{ +} + +void* gfxRealloc(void *ptr, size_t oldsz, size_t newsz) +{ + void *np; + + if (newsz <= oldsz) + return ptr; + + np = gfxAlloc(newsz); + if (!np) + return 0; + + if (oldsz) { + memcpy(np, ptr, oldsz); + vPortFree(ptr); + } + + return np; +} + +void gfxSleepMilliseconds(delaytime_t ms) +{ + const portTickType ticks = ms / portTICK_PERIOD_MS; + vTaskDelay(ticks); +} + +void gfxSleepMicroseconds(delaytime_t ms) +{ + const portTickType ticks = (ms / 1000) / portTICK_PERIOD_MS; + + // delay milli seconds + vTaskDelay(ticks); + + // microsecond resolution delay is not supported in FreeRTOS + // vUsDelay(ms%1000); +} + +portTickType MS2ST(portTickType ms) +{ + return (ms / portTICK_PERIOD_MS); +} + +void gfxMutexInit(xSemaphoreHandle *s) +{ + *s = xSemaphoreCreateMutex(); + #if GFX_FREERTOS_USE_TRACE + vTraceSetMutexName(*s,"uGFXMutex"); // for FreeRTOS+Trace debug + #endif +} + +void gfxSemInit(gfxSem* psem, semcount_t val, semcount_t limit) +{ + if (val > limit) + val = limit; + + psem->counter = val; + psem->limit = limit; + psem->sem = xSemaphoreCreateCounting(limit,val); + + #if GFX_FREERTOS_USE_TRACE + vTraceSetSemaphoreName(psem->sem, "uGFXSema"); // for FreeRTOS+Trace debug + #endif +} + +void gfxSemDestroy(gfxSem* psem) +{ + vSemaphoreDelete(psem->sem); +} + +bool_t gfxSemWait(gfxSem* psem, delaytime_t ms) +{ + psem->counter--; + + if (xSemaphoreTake(psem->sem, MS2ST(ms)) == pdPASS) + return TRUE; + + psem->counter++; + + return FALSE; +} + +bool_t gfxSemWaitI(gfxSem* psem) +{ + portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; + + psem->counter--; + + if (xSemaphoreTakeFromISR(psem->sem,&xHigherPriorityTaskWoken) == pdTRUE) + return TRUE; + + psem->counter++; + + return FALSE; +} + +void gfxSemSignal(gfxSem* psem) +{ + taskENTER_CRITICAL(); + + if (psem->counter < psem->limit) { + psem->counter++; + xSemaphoreGive(psem->sem); + } + + taskYIELD(); + taskEXIT_CRITICAL(); +} + +void gfxSemSignalI(gfxSem* psem) +{ + portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; + + if (psem->counter < psem->limit) { + psem->counter++; + xSemaphoreGiveFromISR(psem->sem,&xHigherPriorityTaskWoken); + } +} + +gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) +{ + xTaskHandle task = NULL; + stacksz = (size_t)stackarea; + + if (stacksz < configMINIMAL_STACK_SIZE) + stacksz = configMINIMAL_STACK_SIZE; + + if (xTaskCreate(fn, "uGFX_TASK", stacksz, param, prio, &task )!= pdPASS) { + for (;;); + } + + return task; +} + +#endif /* GFX_USE_OS_FREERTOS */ |