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