version 2.8
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.org/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 DECLARE_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, NORMAL_PRIORITY, 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, NORMAL_PRIORITY, startUGFX_CMSIS2, 0);
53  break;
54  }
55  #endif
56 }
57 
58 void _gosDeinit(void)
59 {
60 }
61 
62 void gfxMutexInit(gfxMutex* pmutex)
63 {
64  *pmutex = osMutexNew(NULL);
65 }
66 
67 void gfxSemInit(gfxSem* psem, semcount_t val, semcount_t limit)
68 {
69  *psem = osSemaphoreNew(limit, val, NULL);
70 }
71 
72 bool_t gfxSemWait(gfxSem* psem, delaytime_t ms)
73 {
74  if (osSemaphoreAcquire(*psem, gfxMillisecondsToTicks(ms)) == osOK)
75  return TRUE;
76  return FALSE;
77 }
78 
79 gfxThreadHandle gfxThreadCreate(void* stackarea, size_t stacksz, threadpriority_t prio, DECLARE_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 threadreturn_t gfxThreadWait(gfxThreadHandle 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 * gfxThreadHandle
A thread handle.
Definition: gos.h:117
#define GFX_OS_UGFXMAIN_STACKSIZE
When uGFXMain() is started as a thread, what stack size should be used.
Definition: gos_options.h:227
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
systemticks_t gfxMillisecondsToTicks(delaytime_t ms)
Convert a given number of millseconds to a number of operating system ticks.
bool_t gfxSemWait(gfxSem *psem, delaytime_t ms)
Wait on a semaphore.
A semaphore.
Definition: gos.h:105
#define DECLARE_THREAD_FUNCTION(fnName, param)
Declare a thread function.
Definition: gos.h:62
void gfxHalt(const char *msg)
Halt the GFX application due to an error.
void uGFXMain(void)
The function containing all the user uGFX application code.
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