aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ch.cpp
blob: ad4be5f4d493624dfc1bcca939f55fd385e9c20f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <ch.hpp>

namespace chibios_rt {

  /*------------------------------------------------------------------------*
   * chibios_rt::System                                                     *
   *------------------------------------------------------------------------*/
  void System::Init(void) {

    chSysInit();
  }

  void Lock(void) {

    chSysLock();
  }

  void Unlock(void) {

    chSysUnlock();
  }

#ifdef CH_USE_SYSTEMTIME
  systime_t System::GetTime(void) {

    return chSysGetTime();
  }
#endif /* CH_USE_SYSTEMTIME */

#ifdef CH_USE_VIRTUAL_TIMERS
  /*------------------------------------------------------------------------*
   * chibios_rt::Timer                                                      *
   *------------------------------------------------------------------------*/
  void Timer::Set(systime_t time, vtfunc_t vtfunc, void *par) {

    chVTSetI(&timer, time, vtfunc, par);
  }

  void Timer::Reset() {

    chVTResetI(&timer);
  }

  bool Timer::IsArmed(void) {

    return chVTIsArmedI(&timer);
  }
#endif /* CH_USE_VIRTUAL_TIMER */

  /*------------------------------------------------------------------------*
   * chibios_rt::BaseThread                                                 *
   *------------------------------------------------------------------------*/
  static msg_t thdstart(void *arg) {
    BaseThread *btp = (BaseThread *)arg;

    return btp->Main();
  }

  BaseThread::BaseThread(tprio_t prio, tmode_t mode, void *workspace, size_t wsize) {
    msg_t thdstart(void *arg);

    thread_ref = chThdCreate(prio, mode, workspace, wsize, thdstart, this);
  }

  void BaseThread::Exit(msg_t msg) {

    chThdExit(msg);
  }

#ifdef CH_USE_WAITEXIT
  msg_t BaseThread::Wait(void) {

    return chThdWait(thread_ref);
  }
#endif /* CH_USE_WAITEXIT */

  void BaseThread::SetPriority(tprio_t newprio) {

    chThdSetPriority(newprio);
  }

#ifdef CH_USE_RESUME
  void BaseThread::Resume(void) {

    chThdResume(thread_ref);
  }
#endif /* CH_USE_RESUME */

#ifdef CH_USE_TERMINATE
  void BaseThread::Terminate(void) {

    chThdTerminate(thread_ref);
  }
#endif /* CH_USE_TERMINATE */

#ifdef CH_USE_SLEEP
  void BaseThread::Sleep(systime_t n) {

    chThdSleep(n);
  }

#ifdef CH_USE_SYSTEMTIME
  void BaseThread::SleepUntil(systime_t time) {

    chThdSleepUntil(time);
  }
#endif /* CH_USE_SYSTEMTIME */
#endif /* CH_USE_SLEEP */

#ifdef CH_USE_MESSAGES
  msg_t BaseThread::SendMessage(msg_t msg) {

    return chMsgSend(thread_ref, msg);
  }

  msg_t BaseThread::WaitMessage(void) {

    return chMsgWait();
  }

  msg_t BaseThread::GetMessage(void) {

    return chMsgGet();
  }

  void BaseThread::ReleaseMessage(msg_t msg) {

    chMsgRelease(msg);
  }

  bool BaseThread::IsPendingMessage(void) {

    return chMsgIsPendingI(thread_ref);
  }
#endif /* CH_USE_MESSAGES */

  msg_t BaseThread::Main(void) {

    return 0;
  }

#ifdef CH_USE_SEMAPHORES
  /*------------------------------------------------------------------------*
   * chibios_rt::Semaphore                                                  *
   *------------------------------------------------------------------------*/
  Semaphore::Semaphore(cnt_t n) {

    chSemInit(&sem, n);
  }

  void Semaphore::Reset(cnt_t n) {

    chSemReset(&sem, n);
  }

  msg_t Semaphore::Wait(void) {

    return chSemWait(&sem);
  }

#ifdef CH_USE_SEMAPHORES_TIMEOUT
  msg_t Semaphore::WaitTimeout(systime_t time) {

    return chSemWaitTimeout(&sem, time);
  }
#endif /* CH_USE_SEMAPHORES_TIMEOUT */

  void Semaphore::Signal(void) {

    chSemSignal(&sem);
  }
#endif /* CH_USE_SEMAPHORES */

#ifdef CH_USE_MUTEXES
  /*------------------------------------------------------------------------*
   * chibios_rt::Mutex                                                      *
   *------------------------------------------------------------------------*/
  Mutex::Mutex(void) {

    chMtxInit(&mutex);
  }

  bool Mutex::TryLock(void) {

    return chMtxTryLock(&mutex);
  }

  void Mutex::Lock(void) {

    chMtxLock(&mutex);
  }

  void Mutex::Unlock(void) {

    chMtxUnlock();
  }

  void UnlockAll(void) {

    chMtxUnlockAll();
  }
#endif /* CH_USE_MUTEXES */

#ifdef CH_USE_EVENTS
  /*------------------------------------------------------------------------*
   * chibios_rt::Event                                                      *
   *------------------------------------------------------------------------*/
  Event::Event(void) {

    chEvtInit(&event);
  }

  void Event::Register(EventListener *elp, eventid_t eid) {

    chEvtRegister(&event,elp, eid);
  }

  void Event::Unregister(EventListener *elp) {

    chEvtUnregister(&event, elp);
  }

  void Event::Send(void) {

    chEvtSend(&event);
  }

  void Event::Clear(eventmask_t mask) {

    chEvtClear(mask);
  }

  eventid_t Event::Wait(eventmask_t ewmask, const evhandler_t handlers[]) {

    return chEvtWait(ewmask, handlers);
  }

#ifdef CH_USE_EVENTS_TIMEOUT
  eventid_t Event::WaitTimeout(eventmask_t ewmask,
                               const evhandler_t handlers[],
                               systime_t time) {

    return chEvtWaitTimeout(ewmask, handlers, time);
  }
#endif /* CH_USE_EVENTS_TIMEOUT */
#endif /* CH_USE_EVENTS */
}