version 2.8
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.org/license.html
6  */
7 
8 #ifndef _GOS_CHIBIOS_H
9 #define _GOS_CHIBIOS_H
10 
11 #if GFX_USE_OS_CHIBIOS
12 
13 // This shouldn't be needed but some people are complaining
14 // about TRUE/FALSE redefinition so we fix it here.
15 #undef TRUE
16 #undef FALSE
17 
18 #include "ch.h"
19 #include "hal.h"
20 
21 /*===========================================================================*/
22 /* Type definitions */
23 /*===========================================================================*/
24 
25 /**
26  * bool_t,
27  * int8_t, uint8_t,
28  * int16_t, uint16_t,
29  * int32_t, uint32_t,
30  * size_t
31  * TRUE, FALSE
32  * TIME_IMMEDIATE, TIME_INFINITE
33  * are already defined by ChibiOS
34  */
35 
36 #if !defined(FALSE)
37  #define FALSE 0
38 #endif
39 #if !defined(TRUE)
40  #define TRUE -1
41 #endif
42 #if (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
43  typedef char bool_t;
44 #endif
45 
46 typedef systime_t delaytime_t;
47 typedef systime_t systemticks_t;
48 typedef cnt_t semcount_t;
49 typedef msg_t threadreturn_t;
50 typedef tprio_t threadpriority_t;
51 
52 #define MAX_SEMAPHORE_COUNT ((semcount_t)(((unsigned long)((semcount_t)(-1))) >> 1))
53 #define LOW_PRIORITY LOWPRIO
54 #define NORMAL_PRIORITY NORMALPRIO
55 #define HIGH_PRIORITY HIGHPRIO
56 
57 #define DECLARE_THREAD_STACK(name, sz) WORKING_AREA(name, sz)
58 #define DECLARE_THREAD_FUNCTION(fnName, param) threadreturn_t fnName(void *param)
59 #define THREAD_RETURN(retval) return retval
60 
61 #if CH_KERNEL_MAJOR == 2
62  typedef struct {
63  Semaphore sem;
64  semcount_t limit;
65  } gfxSem;
66 
67  typedef Mutex gfxMutex;
68  typedef Thread* gfxThreadHandle;
69 #elif (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
70  #undef DECLARE_THREAD_STACK
71  #define DECLARE_THREAD_STACK(a, b) THD_WORKING_AREA(a, b)
72 
73  typedef struct {
74  semaphore_t sem;
75  semcount_t limit;
76  } gfxSem;
77 
78  typedef mutex_t gfxMutex;
79  typedef thread_t* gfxThreadHandle;
80 #endif
81 
82 
83 /*===========================================================================*/
84 /* Function declarations. */
85 /*===========================================================================*/
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 // First the kernel version specific ones
92 #if CH_KERNEL_MAJOR == 2
93  #define gfxSystemTicks() chTimeNow()
94  #define gfxMutexInit(pmutex) chMtxInit(pmutex)
95  #define gfxMutexExit(pmutex) chMtxUnlock()
96  #define gfxExit() chSysHalt()
97  #define gfxHalt(msg) { chDbgPanic(msg); chSysHalt(); }
98 #elif (CH_KERNEL_MAJOR == 3) || (CH_KERNEL_MAJOR == 4)
99  #define gfxSystemTicks() chVTGetSystemTimeX()
100  #define gfxMutexInit(pmutex) chMtxObjectInit(pmutex)
101  #define gfxMutexExit(pmutex) chMtxUnlock(pmutex)
102  #define gfxExit() osalSysHalt("gfx_exit")
103 #define gfxHalt(msg) { chSysHalt(msg); }
104 #endif
105 
106 #define gfxAlloc(sz) chHeapAlloc(0, sz)
107 #define gfxFree(ptr) chHeapFree(ptr)
108 #define gfxYield() chThdYield()
109 #define gfxMillisecondsToTicks(ms) MS2ST(ms)
110 #define gfxSystemLock() chSysLock()
111 #define gfxSystemUnlock() chSysUnlock()
112 #define gfxMutexDestroy(pmutex) (void)pmutex
113 #define gfxMutexEnter(pmutex) chMtxLock(pmutex)
114 void *gfxRealloc(void *ptr, size_t oldsz, size_t newsz);
115 void gfxSleepMilliseconds(delaytime_t ms);
116 void gfxSleepMicroseconds(delaytime_t ms);
117 void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit);
118 void gfxSemDestroy(gfxSem *psem);
119 bool_t gfxSemWait(gfxSem *psem, delaytime_t ms);
120 bool_t gfxSemWaitI(gfxSem *psem);
121 void gfxSemSignal(gfxSem *psem);
122 void gfxSemSignalI(gfxSem *psem);
123 gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param);
124 #define gfxThreadWait(thread) chThdWait(thread)
125 #define gfxThreadMe() chThdSelf()
126 #define gfxThreadClose(thread) (void)thread
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* GFX_USE_OS_CHIBIOS */
133 #endif /* _GOS_CHIBIOS_H */
void * gfxThreadHandle
A thread handle.
Definition: gos.h:117
void gfxSemInit(gfxSem *psem, semcount_t val, semcount_t limit)
Initialise a Counted Semaphore.
void gfxSemDestroy(gfxSem *psem)
Destroy a Counted Semaphore.
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.
A mutex.
Definition: gos.h:111
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.