µGFX  2.9
version 2.9
gos_cmsis.c
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.io/license.html
6  */
7 
8 #include "../../gfx.h"
9 #include <string.h>
10 
11 #if GFX_USE_OS_CMSIS
12 
13 void _gosHeapInit(void);
14 
15 void _gosInit(void)
16 {
17  #if !GFX_OS_NO_INIT
18  osKernelInitialize();
19  if (!osKernelRunning())
20  osKernelStart();
21  #elif !GFX_OS_INIT_NO_WARNING
22  #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
23  #warning "GOS: Operating System initialization has been turned off. Make sure you call osKernelInitialize() and osKernelStart() before gfxInit() in your application!"
24  #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
25  COMPILER_WARNING("GOS: Operating System initialization has been turned off. Make sure you call osKernelInitialize() and osKernelStart() before gfxInit() in your application!")
26  #endif
27  #endif
28 
29  // Set up the heap allocator
30  _gosHeapInit();
31 }
32 
33 void _gosPostInit(void)
34 {
35 }
36 
37 void _gosDeinit(void)
38 {
39 }
40 
41 void gfxMutexInit(gMutex* pmutex)
42 {
43  osMutexDef_t def;
44  def.mutex = pmutex->mutex;
45 
46  pmutex->id = osMutexCreate(&def);
47 }
48 
49 void gfxSemInit(gSem* psem, gSemcount val, gSemcount limit)
50 {
51  osSemaphoreDef_t def;
52  def.semaphore = psem->semaphore;
53 
54  if (val > limit) val = limit;
55  psem->available = limit - val;
56  psem->id = osSemaphoreCreate(&def, val);
57 }
58 
59 void gfxSemDestroy(gSem* psem)
60 {
61  osSemaphoreDelete(psem->id);
62 }
63 
64 gBool gfxSemWait(gSem* psem, gDelay ms)
65 {
66  if (osSemaphoreWait(psem->id, ms) > 0) {
67  psem->available++;
68  return gTrue;
69  }
70  return gFalse;
71 }
72 
73 gBool gfxSemWaitI(gSem* psem)
74 {
75  return gfxSemWait(psem, 0);
76 }
77 
78 void gfxSemSignal(gSem* psem)
79 {
80  gfxSemSignalI(psem);
81 }
82 
83 void gfxSemSignalI(gSem* psem)
84 {
85  if (psem->available) {
86  psem->available--;
87  osSemaphoreRelease(psem->id);
88  }
89 }
90 
91 gThread gfxThreadCreate(void* stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void* param)
92 {
93  osThreadDef_t def;
94 
95  (void)stackarea;
96 
97  def.pthread = (os_pthread)fn;
98  def.tpriority = prio;
99  def.instances = 1;
100  def.stacksize = stacksz;
101 
102  return osThreadCreate(&def, param);
103 }
104 
105 gThreadreturn gfxThreadWait(gThread thread) {
106  while(osThreadGetPriority(thread) == osPriorityError)
107  gfxYield();
108 }
109 
110 #endif /* GFX_USE_OS_CMSIS */
gThreadreturn gfxThreadWait(gThread thread)
Wait for a thread to finish.
void gfxSemSignal(gSem *psem)
Signal a semaphore.
void * gThread
A thread handle.
Definition: gos.h:116
void gfxYield(void)
Yield the current thread.
#define GFX_THREAD_FUNCTION(fnName, param)
Declare a thread function.
Definition: gos.h:73
gBool gfxSemWait(gSem *psem, gDelay ms)
Wait on a semaphore.
void gfxMutexInit(gMutex *pmutex)
Initialise a mutex to protect a region of code from other threads.
gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn), p), void *param)
Start a new thread.
gBool gfxSemWaitI(gSem *psem)
Test if a wait on a semaphore can be satisfied immediately.
void gfxSemDestroy(gSem *psem)
Destroy a Counted Semaphore.
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
Initialise a Counted Semaphore.
void gfxSemSignalI(gSem *psem)
Signal a semaphore.
A mutex.
Definition: gos.h:110
A semaphore.
Definition: gos.h:104