µGFX  2.9
version 2.9
gos_chibios.h
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 #ifndef _GOS_CHIBIOS_H
9 #define _GOS_CHIBIOS_H
10 
11 #if GFX_USE_OS_CHIBIOS
12 
13 #if GFX_COMPAT_V2
14  // This shouldn't be needed but some people are complaining
15  // about TRUE/FALSE redefinition so we fix it here.
16  // ChibiOS will define them in its own header files.
17  #undef TRUE
18  #undef FALSE
19 #endif
20 
21 #include "ch.h"
22 #include "hal.h"
23 
24 /*===========================================================================*/
25 /* Type definitions */
26 /*===========================================================================*/
27 
28 #define gDelayNone TIME_IMMEDIATE
29 #define gDelayForever TIME_INFINITE
30 
31 #if CH_KERNEL_MAJOR <= 4
32  typedef systime_t gDelay;
33 #else
34  typedef sysinterval_t gDelay;
35 #endif
36 typedef systime_t gTicks;
37 typedef cnt_t gSemcount;
38 #if CH_KERNEL_MAJOR >= 6
39  typedef void gThreadreturn;
40 #else
41  typedef msg_t gThreadreturn;
42 #endif
43 typedef tprio_t gThreadpriority;
44 
45 #define gSemMaxCount ((gSemcount)(((unsigned long)((gSemcount)(-1))) >> 1))
46 #define gThreadpriorityLow LOWPRIO
47 #define gThreadpriorityNormal NORMALPRIO
48 #define gThreadpriorityHigh HIGHPRIO
49 
50 #define GFX_THREAD_STACK(name, sz) WORKING_AREA(name, sz)
51 #define GFX_THREAD_FUNCTION(fnName, param) gThreadreturn fnName(void *param)
52 #define gfxThreadReturn(retval) return retval
53 
54 #if CH_KERNEL_MAJOR <= 2
55  typedef struct {
56  Semaphore sem;
57  gSemcount limit;
58  } gSem;
59 
60  typedef Mutex gMutex;
61  typedef Thread* gThread;
62 #else
63  #undef GFX_THREAD_STACK
64  #define GFX_THREAD_STACK(a, b) THD_WORKING_AREA(a, b)
65 
66  typedef struct {
67  semaphore_t sem;
68  gSemcount limit;
69  } gSem;
70 
71  typedef mutex_t gMutex;
72  typedef thread_t* gThread;
73 #endif
74 
75 
76 /*===========================================================================*/
77 /* Function declarations. */
78 /*===========================================================================*/
79 
80 // First the kernel version specific ones
81 #if CH_KERNEL_MAJOR <= 2
82  #define gfxSystemTicks() chTimeNow()
83  #define gfxMutexInit(pmutex) chMtxInit(pmutex)
84  #define gfxMutexExit(pmutex) chMtxUnlock()
85  #define gfxExit() chSysHalt()
86  #define gfxHalt(msg) { chDbgPanic(msg); chSysHalt(); }
87 #else
88  #define gfxSystemTicks() chVTGetSystemTimeX()
89  #define gfxMutexInit(pmutex) chMtxObjectInit(pmutex)
90  #define gfxMutexExit(pmutex) chMtxUnlock(pmutex)
91  #define gfxExit() osalSysHalt("gfx_exit")
92  #define gfxHalt(msg) { chSysHalt(msg); }
93 #endif
94 
95 #if CH_KERNEL_MAJOR <= 4
96  #define gfxMillisecondsToTicks(ms) MS2ST(ms)
97 #else
98  #define gfxMillisecondsToTicks(ms) TIME_MS2I(ms)
99 #endif
100 
101 #define gfxAlloc(sz) chHeapAlloc(0, sz)
102 #define gfxFree(ptr) chHeapFree(ptr)
103 #define gfxYield() chThdYield()
104 #define gfxSystemLock() chSysLock()
105 #define gfxSystemUnlock() chSysUnlock()
106 #define gfxMutexDestroy(pmutex) (void)pmutex
107 #define gfxMutexEnter(pmutex) chMtxLock(pmutex)
108 void *gfxRealloc(void *ptr, gMemSize oldsz, gMemSize newsz);
109 void gfxSleepMilliseconds(gDelay ms);
110 void gfxSleepMicroseconds(gDelay ms);
111 void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit);
112 void gfxSemDestroy(gSem *psem);
113 gBool gfxSemWait(gSem *psem, gDelay ms);
114 gBool gfxSemWaitI(gSem *psem);
115 void gfxSemSignal(gSem *psem);
116 void gfxSemSignalI(gSem *psem);
117 gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn),p), void *param);
118 #define gfxThreadWait(thread) chThdWait(thread)
119 #define gfxThreadMe() chThdSelf()
120 #define gfxThreadClose(thread) (void)thread
121 
122 #endif /* GFX_USE_OS_CHIBIOS */
123 #endif /* _GOS_CHIBIOS_H */
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
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.
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 mutex.
Definition: gos.h:110
A semaphore.
Definition: gos.h:104