version 2.8
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.org/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(gfxMutex* pmutex)
42 {
43  osMutexDef_t def;
44  def.mutex = pmutex->mutex;
45 
46  pmutex->id = osMutexCreate(&def);
47 }
48 
49 void gfxSemInit(gfxSem* psem, semcount_t val, semcount_t 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(gfxSem* psem)
60 {
61  osSemaphoreDelete(psem->id);
62 }
63 
64 bool_t gfxSemWait(gfxSem* psem, delaytime_t ms)
65 {
66  if (osSemaphoreWait(psem->id, ms) > 0) {
67  psem->available++;
68  return TRUE;
69  }
70  return FALSE;
71 }
72 
73 bool_t gfxSemWaitI(gfxSem* psem)
74 {
75  return gfxSemWait(psem, 0);
76 }
77 
78 void gfxSemSignal(gfxSem* psem)
79 {
80  gfxSemSignalI(psem);
81 }
82 
83 void gfxSemSignalI(gfxSem* psem)
84 {
85  if (psem->available) {
86  psem->available--;
87  osSemaphoreRelease(psem->id);
88  }
89 }
90 
91 gfxThreadHandle gfxThreadCreate(void* stackarea, size_t stacksz, threadpriority_t prio, DECLARE_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 threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
106  while(osThreadGetPriority(thread) == osPriorityError)
107  gfxYield();
108 }
109 
110 #endif /* GFX_USE_OS_CMSIS */
void * gfxThreadHandle
A thread handle.
Definition: gos.h:117
void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit)
Initialise a Counted Semaphore.
#define FALSE
Generic &#39;false&#39; boolean constant.
Definition: gfx.h:31
void gfxSemDestroy(gfxSem *psem)
Destroy a Counted Semaphore.
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms)
Wait on a semaphore.
void gfxSemSignalI(gfxSem *psem)
Signal a semaphore.
A semaphore.
Definition: gos.h:105
#define DECLARE_THREAD_FUNCTION(fnName, param)
Declare a thread function.
Definition: gos.h:62
void gfxSemSignal(gfxSem *psem)
Signal a semaphore.
gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn), p), void *param)
Start a new thread.
A mutex.
Definition: gos.h:111
threadreturn_t gfxThreadWait(gfxThreadHandle thread)
Wait for a thread to finish.
void gfxMutexInit(gfxMutex *pmutex)
Initialise a mutex to protect a region of code from other threads.
void gfxYield(void)
Yield the current thread.
#define TRUE
Generic &#39;true&#39; boolean constant.
Definition: gfx.h:38
bool_t gfxSemWaitI(gfxSem *psem)
Test if a wait on a semaphore can be satisfied immediately.