µGFX  2.9
version 2.9
gos_cmsis2.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_CMSIS2
12 
13 #if !GFX_OS_NO_INIT && !GFX_OS_CALL_UGFXMAIN
14  #error "GOS: Either GFX_OS_NO_INIT or GFX_OS_CALL_UGFXMAIN must be defined for CMSIS V2"
15 #endif
16 
17 void _gosHeapInit(void);
18 
19 void _gosInit(void)
20 {
21  #if GFX_OS_NO_INIT && !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()."
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().")
26  #endif
27  #endif
28 
29  // Set up the heap allocator
30  _gosHeapInit();
31 }
32 
33 #if !GFX_OS_NO_INIT && GFX_OS_CALL_UGFXMAIN
34  static GFX_THREAD_FUNCTION(startUGFX_CMSIS2, p) {
35  (void) p;
36  uGFXMain();
37  }
38 #endif
39 void _gosPostInit(void)
40 {
41  #if !GFX_OS_NO_INIT && GFX_OS_CALL_UGFXMAIN
42  switch(osKernelGetState()) {
43  case osKernelInactive:
44  osKernelInitialize();
45  /* Fall Through */
46  case osKernelReady:
47  gfxThreadCreate(0, GFX_OS_UGFXMAIN_STACKSIZE, gThreadpriorityNormal, startUGFX_CMSIS2, 0);
48  osKernelStart();
49  gfxHalt("Unable to start CMSIS V2 scheduler. Out of memory?");
50  break;
51  default:
52  gfxThreadCreate(0, GFX_OS_UGFXMAIN_STACKSIZE, gThreadpriorityNormal, startUGFX_CMSIS2, 0);
53  break;
54  }
55  #endif
56 }
57 
58 void _gosDeinit(void)
59 {
60 }
61 
62 void gfxMutexInit(gMutex* pmutex)
63 {
64  *pmutex = osMutexNew(NULL);
65 }
66 
67 void gfxSemInit(gSem* psem, gSemcount val, gSemcount limit)
68 {
69  *psem = osSemaphoreNew(limit, val, NULL);
70 }
71 
72 gBool gfxSemWait(gSem* psem, gDelay ms)
73 {
74  if (osSemaphoreAcquire(*psem, gfxMillisecondsToTicks(ms)) == osOK)
75  return gTrue;
76  return gFalse;
77 }
78 
79 gThread gfxThreadCreate(void* stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void* param)
80 {
81  osThreadAttr_t def;
82 
83  (void)stackarea;
84 
85  memset(&def, 0, sizeof(def));
86  def.name = "uGFX";
87  def.attr_bits = osThreadDetached; // osThreadJoinable
88  def.stack_mem = 0;
89  def.stack_size = stacksz;
90  def.priority = prio;
91  //def.tz_module = ????;
92 
93  return osThreadNew((osThreadFunc_t)fn, param, &def);
94 }
95 
96 gThreadreturn gfxThreadWait(gThread thread) {
97  while(1) {
98  switch(osThreadGetState(thread)) {
99  case osThreadReady:
100  case osThreadRunning:
101  case osThreadBlocked:
102  gfxYield();
103  break;
104  default:
105  return;
106  }
107  }
108 }
109 
110 #endif /* GFX_USE_OS_CMSIS2 */
void GFXUSERFN uGFXMain(void)
The user supplied function containing all the user uGFX application code.
gThreadreturn gfxThreadWait(gThread thread)
Wait for a thread to finish.
#define GFX_OS_UGFXMAIN_STACKSIZE
When uGFXMain() is started as a thread, what stack size should be used.
Definition: gos_options.h:227
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
gTicks gfxMillisecondsToTicks(gDelay ms)
Convert a given number of millseconds to a number of operating system ticks.
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.
void gfxHalt(const char *msg)
Halt the GFX application due to an error.
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
Initialise a Counted Semaphore.
A mutex.
Definition: gos.h:110
A semaphore.
Definition: gos.h:104