Statistics
| Branch: | Revision:

amiro-apps / middleware / apps_urtosal.c @ 7e5fca2f

History | View | Annotate | Download (7.824 KB)

1 6d4ba740 Thomas Schöpping
/*
2
AMiRo-Apps is a collection of applications for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2018..2020  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <urtware.h>
20
21
/*============================================================================*/
22
/* DEPENDENCIES                                                               */
23
/*============================================================================*/
24
25
/*============================================================================*/
26
/* DEBUG                                                                      */
27
/*============================================================================*/
28
29
/*============================================================================*/
30
/* MUTEX                                                                      */
31
/*============================================================================*/
32
33
/*============================================================================*/
34
/* CONDITION VARIABLE                                                         */
35
/*============================================================================*/
36
37 880a8dde Thomas Schöpping
#if (URT_CFG_OSAL_CONDVAR_TIMEOUT == true) || defined(__DOXYGEN__)
38 6d4ba740 Thomas Schöpping
/**
39
 * @details Due to limitations of ChibiOS, the 'mutex' argument is ignored in
40
 *          this implementation. Instead, the mutex, which was locked by the
41
 *          calling thread most recently is used.
42
 *          In debug builds, however, the argument is still checked to be
43
 *          identical to implicitely selected mutex.
44
 */
45
urt_osCondvarWaitStatus_t urtCondvarWait(urt_osCondvar_t* condvar, urt_osMutex_t* mutex, urt_delay_t timeout)
46
{
47
  aosDbgCheck(condvar != NULL);
48
  aosDbgCheck(mutex != NULL);
49
  aosDbgCheck(chMtxGetNextMutexX() == mutex); // due to limitation of ChibiOS
50
51
  (void)mutex;
52
53
  switch (chCondWaitTimeout(condvar, timeout)) {
54
    case MSG_OK:
55
      return URT_CONDVAR_WAITSTATUS_SIGNAL;
56
    case MSG_RESET:
57
      return URT_CONDVAR_WAITSTATUS_BROADCAST;
58
    case MSG_TIMEOUT:
59
      return URT_CONDVAR_WAITSTATUS_TIMEOUT;
60
    default:
61
      chSysHalt(__func__);
62
      return URT_CONDVAR_WAITSTATUS_TIMEOUT;
63
  }
64
}
65 880a8dde Thomas Schöpping
#else
66
urt_osCondvarWaitStatus_t urtCondvarWait(urt_osCondvar_t* condvar, urt_osMutex_t* mutex)
67
{
68
  aosDbgCheck(condvar != NULL);
69
  aosDbgCheck(mutex != NULL);
70
  aosDbgCheck(chMtxGetNextMutexX() == mutex); // due to limitation of ChibiOS
71
72
  (void)mutex;
73
74
  switch (chCondWait(condvar)) {
75
    case MSG_OK:
76
      return URT_CONDVAR_WAITSTATUS_SIGNAL;
77
    case MSG_RESET:
78
      return URT_CONDVAR_WAITSTATUS_BROADCAST;
79
    default:
80
      chSysHalt(__func__);
81
      return URT_CONDVAR_WAITSTATUS_TIMEOUT;
82
  }
83
}
84
#endif
85 6d4ba740 Thomas Schöpping
86
/*============================================================================*/
87
/* EVENTS                                                                     */
88
/*============================================================================*/
89
90
urt_osEventMask_t urtEventWait(urt_osEventMask_t mask, urt_osEventWait_t type, urt_delay_t timeout)
91
{
92
  switch (type) {
93
    case URT_EVENT_WAIT_ONE:
94
      return chEvtWaitOneTimeout(mask, timeout);
95
    case URT_EVENT_WAIT_ANY:
96
      return chEvtWaitAnyTimeout(mask, timeout);
97
    case URT_EVENT_WAIT_ALL:
98
      return chEvtWaitAllTimeout(mask, timeout);
99
    default:
100
      return 0;
101
  }
102
}
103
104
/*============================================================================*/
105
/* STREAMS                                                                    */
106
/*============================================================================*/
107
108
/*============================================================================*/
109
/* TIME                                                                       */
110
/*============================================================================*/
111
112
urt_osTime_t urtTimeNow(void)
113
{
114
  urt_osTime_t time;
115
  aosSysGetUptime(&time);
116
  return time;
117
}
118
119
/*============================================================================*/
120
/* THREAD                                                                     */
121
/*============================================================================*/
122
123
urt_osThread_t* urtThreadInit(void* memory, size_t size, urt_osThreadPrio_t prio, urt_osThreadFunction_t func, void* arg)
124
{
125
  aosDbgCheck(memory != NULL);
126
  aosDbgCheck(size != 0);
127
  aosDbgCheck(prio >= URT_THREAD_PRIO_LOW_MIN && prio <= URT_THREAD_PRIO_RT_MAX);
128
  aosDbgCheck(func != NULL);
129
130
  const thread_descriptor_t descriptor = {
131
    /* name                         */ "",
132
    /* pointer to working area base */ (stkalign_t*)memory,
133 557c9c87 Svenja
    /* end of the working area      */ &((stkalign_t*)memory)[/*size*/ 512 / sizeof(stkalign_t)],
134 6d4ba740 Thomas Schöpping
    /* thread priority              */ prio,
135
    /* thread function pointer      */ func,
136
    /* thread argument              */ arg,
137
    /* pointer to the parent thread */ chThdGetSelfX(),
138
  };
139
140 a33cec74 skenneweg
 /* urtPrintf("memory: 0x%08X\n", memory);
141 557c9c87 Svenja
  urtPrintf("size: %u\n", size);
142
  urtPrintf("prio: %u\n", prio);
143
  urtPrintf("func: 0x%08X\n", func);
144
  urtPrintf("arg: 0x%08X\n", arg);
145
  urtPrintf("parent: 0x%08X\n", chThdGetSelfX());
146 a33cec74 skenneweg
  urtThreadMSleep(10); */
147 6479c731 skenneweg
148 a33cec74 skenneweg
  return chThdCreateSuspended(&descriptor);
149 6d4ba740 Thomas Schöpping
}
150
151
void urtThreadTerminate(urt_osThread_t* thread, urt_osThreadTerminateSignal_t sig)
152
{
153
  aosDbgCheck(thread != NULL);
154
155
  switch (sig) {
156
    case URT_THREAD_TERMINATE_REQUEST:
157
      chThdTerminate(thread);
158
      return;
159
    case URT_THREAD_TERMINATE_KILL:
160
      /*
161
       * TODO: implement kill functionality
162
       */
163
      chThdTerminate(thread);
164
      return;
165
  }
166
}
167
168
urt_osThreadState_t urtThreadGetState(urt_osThread_t* thread)
169
{
170
  aosDbgCheck(thread != NULL);
171
172
  switch (thread->state) {
173
    case CH_STATE_CURRENT:
174
      return URT_THREAD_STATE_RUNNING;
175
    case CH_STATE_READY:
176
      return URT_THREAD_STATE_READY;
177
    case CH_STATE_SLEEPING:
178
      return URT_THREAD_STATE_SLEEPING;
179
    case CH_STATE_WTSTART:
180
    case CH_STATE_SUSPENDED:
181
      return URT_THREAD_STATE_SUSPENDED;
182
    case CH_STATE_FINAL:
183
      return URT_THREAD_STATE_TERMINATED;
184
    case CH_STATE_QUEUED:
185
    case CH_STATE_WTSEM:
186
    case CH_STATE_WTMTX:
187
    case CH_STATE_WTCOND:
188
    case CH_STATE_WTEXIT:
189
    case CH_STATE_WTOREVT:
190
    case CH_STATE_WTANDEVT:
191
    case CH_STATE_SNDMSGQ:
192
    case CH_STATE_SNDMSG:
193
    case CH_STATE_WTMSG:
194
    default:
195
      return URT_THREAD_STATE_WAITING;
196
  }
197
}
198
199
/*============================================================================*/
200
/* TIMER                                                                      */
201
/*============================================================================*/
202
203
void urtTimerSet(urt_osTimer_t* timer, urt_delay_t delay, urt_osTimerCallback_t callback, void* cbparams)
204
{
205
  aosDbgCheck(timer != NULL);
206
  aosDbgCheck(callback != NULL);
207
208
  if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) {
209
    aosTimerSetLongInterval(timer, (aos_longinterval_t)delay, callback, cbparams);
210
  } else {
211
    aosTimerSetInterval(timer, delay, callback, cbparams);
212
  }
213
  return;
214
}
215
216
void urtTimerSetPeriodic(urt_osTimer_t* timer, urt_delay_t period, urt_osTimerCallback_t callback, void* cbparams)
217
{
218
  aosDbgCheck(timer != NULL);
219
  aosDbgCheck(callback != NULL);
220
221
  if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) {
222
    aosTimerPeriodicLongInterval(timer, (aos_longinterval_t)period, callback, cbparams);
223
  } else {
224
    aosTimerPeriodicInterval(timer, period, callback, cbparams);
225
  }
226
  return;
227
}