µGFX  2.9
version 2.9
gos_chibios.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 
10 #if GFX_USE_OS_CHIBIOS
11 
12 #include <string.h>
13 
14 #if CH_KERNEL_MAJOR < 2 || CH_KERNEL_MAJOR > 6
15  #error "GOS: Unsupported version of ChibiOS"
16 #endif
17 
18 #if CH_KERNEL_MAJOR <= 2
19  #if !CH_USE_MUTEXES
20  #error "GOS: CH_USE_MUTEXES must be defined in chconf.h"
21  #endif
22  #if !CH_USE_SEMAPHORES
23  #error "GOS: CH_USE_SEMAPHORES must be defined in chconf.h"
24  #endif
25 #else
26  #if !CH_CFG_USE_MUTEXES
27  #error "GOS: CH_CFG_USE_MUTEXES must be defined in chconf.h"
28  #endif
29  #if !CH_CFG_USE_SEMAPHORES
30  #error "GOS: CH_CFG_USE_SEMAPHORES must be defined in chconf.h"
31  #endif
32 #endif
33 
34 void _gosInit(void)
35 {
36  #if !GFX_OS_NO_INIT
37  /* Don't Initialize if the user already has */
38  #if CH_KERNEL_MAJOR <= 2
39  if (!chThdSelf()) {
40  halInit();
41  chSysInit();
42  }
43  #else
44  if (!chThdGetSelfX()) {
45  halInit();
46  chSysInit();
47  }
48  #endif
49  #elif !GFX_OS_INIT_NO_WARNING
50  #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
51  #warning "GOS: Operating System initialization has been turned off. Make sure you call halInit() and chSysInit() before gfxInit() in your application!"
52  #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
53  COMPILER_WARNING("GOS: Operating System initialization has been turned off. Make sure you call halInit() and chSysInit() before gfxInit() in your application!")
54  #endif
55  #endif
56 }
57 
58 void _gosPostInit(void)
59 {
60 }
61 
62 void _gosDeinit(void)
63 {
64  /* ToDo */
65 }
66 
67 void *gfxRealloc(void *ptr, gMemSize oldsz, gMemSize newsz)
68 {
69  void *np;
70 
71  if (newsz <= oldsz)
72  return ptr;
73 
74  np = gfxAlloc(newsz);
75  if (!np)
76  return 0;
77 
78  if (oldsz)
79  memcpy(np, ptr, oldsz);
80 
81  return np;
82 }
83 
84 void gfxSleepMilliseconds(gDelay ms)
85 {
86  switch(ms) {
87  case gDelayNone: chThdYield(); return;
88  case gDelayForever: chThdSleep(TIME_INFINITE); return;
89  default: chThdSleepMilliseconds(ms); return;
90  }
91 }
92 
93 void gfxSleepMicroseconds(gDelay ms)
94 {
95  switch(ms) {
96  case gDelayNone: return;
97  case gDelayForever: chThdSleep(TIME_INFINITE); return;
98  default: chThdSleepMicroseconds(ms); return;
99  }
100 }
101 
102 void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
103 {
104  if (val > limit)
105  val = limit;
106 
107  psem->limit = limit;
108 
109  #if CH_KERNEL_MAJOR <= 2
110  chSemInit(&psem->sem, val);
111  #else
112  chSemObjectInit(&psem->sem, val);
113  #endif
114 }
115 
116 void gfxSemDestroy(gSem *psem)
117 {
118  chSemReset(&psem->sem, 1);
119 }
120 
121 gBool gfxSemWait(gSem *psem, gDelay ms)
122 {
123  #if CH_KERNEL_MAJOR <= 2
124  switch(ms) {
125  case gDelayNone: return chSemWaitTimeout(&psem->sem, TIME_IMMEDIATE) != RDY_TIMEOUT;
126  case gDelayForever: chSemWait(&psem->sem); return gTrue;
127  default: return chSemWaitTimeout(&psem->sem, gfxMillisecondsToTicks(ms)) != RDY_TIMEOUT;
128  }
129  #else
130  switch(ms) {
131  case gDelayNone: return chSemWaitTimeout(&psem->sem, TIME_IMMEDIATE) != MSG_TIMEOUT;
132  case gDelayForever: chSemWait(&psem->sem); return gTrue;
133  default: return chSemWaitTimeout(&psem->sem, gfxMillisecondsToTicks(ms)) != MSG_TIMEOUT;
134  }
135  #endif
136 }
137 
138 gBool gfxSemWaitI(gSem *psem)
139 {
140  #if CH_KERNEL_MAJOR <= 3
141  if (psem->sem.s_cnt <= 0)
142  return gFalse;
143  #else
144  if (psem->sem.cnt <= 0)
145  return gFalse;
146  #endif
147  chSemFastWaitI(&psem->sem);
148  return gTrue;
149 }
150 
151 void gfxSemSignal(gSem *psem)
152 {
153  chSysLock();
154 
155  #if CH_KERNEL_MAJOR <= 3
156  if (psem->sem.s_cnt < psem->limit)
157  chSemSignalI(&psem->sem);
158  #else
159  if (psem->sem.cnt < psem->limit)
160  chSemSignalI(&psem->sem);
161  #endif
162 
163  chSchRescheduleS();
164  chSysUnlock();
165 }
166 
167 void gfxSemSignalI(gSem *psem)
168 {
169  #if CH_KERNEL_MAJOR <= 3
170  if (psem->sem.s_cnt < psem->limit)
171  chSemSignalI(&psem->sem);
172  #else
173  if (psem->sem.cnt < psem->limit)
174  chSemSignalI(&psem->sem);
175  #endif
176 }
177 
178 gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param)
179 {
180  if (!stackarea) {
181  if (!stacksz) stacksz = 256;
182  #if CH_KERNEL_MAJOR <= 3
183  return chThdCreateFromHeap(0, stacksz, prio, (tfunc_t)fn, param);
184  #else
185  return chThdCreateFromHeap(0, stacksz, "ugfx", prio, (tfunc_t)fn, param);
186  #endif
187  }
188 
189  if (!stacksz)
190  return 0;
191 
192  return chThdCreateStatic(stackarea, stacksz, prio, (tfunc_t)fn, param);
193 }
194 
195 #endif /* GFX_USE_OS_CHIBIOS */
void gfxSemSignal(gSem *psem)
Signal a semaphore.
void * gThread
A thread handle.
Definition: gos.h:116
#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 gfxSleepMicroseconds(gDelay us)
Put the current thread to sleep for the specified period in microseconds.
void * gfxAlloc(gMemSize sz)
Allocate memory.
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 * gfxRealloc(void *ptr, gMemSize oldsz, gMemSize newsz)
Re-allocate memory.
void gfxSemSignalI(gSem *psem)
Signal a semaphore.
void gfxSleepMilliseconds(gDelay ms)
Put the current thread to sleep for the specified period in milliseconds.
A semaphore.
Definition: gos.h:104