Statistics
| Branch: | Revision:

amiro-apps / middleware / apps_urtosal.c @ 557c9c87

History | View | Annotate | Download (8.022 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 6479c731 skenneweg
  urtPrintf("Test before chThdCreateSuspended in threadInit\n");
141
  urtThreadMSleep(10);
142
143 557c9c87 Svenja
  urtPrintf("memory: 0x%08X\n", memory);
144
  urtPrintf("size: %u\n", size);
145
  urtPrintf("prio: %u\n", prio);
146
  urtPrintf("func: 0x%08X\n", func);
147
  urtPrintf("arg: 0x%08X\n", arg);
148
  urtPrintf("parent: 0x%08X\n", chThdGetSelfX());
149
  urtThreadMSleep(10);
150
151 6479c731 skenneweg
  urt_osThread_t* temp= chThdCreateSuspended(&descriptor);
152
153
  urtPrintf("Test after chThdCreateSuspended in threadInit\n");
154
  urtThreadMSleep(10);
155
156
  return temp;
157 6d4ba740 Thomas Schöpping
}
158
159
void urtThreadTerminate(urt_osThread_t* thread, urt_osThreadTerminateSignal_t sig)
160
{
161
  aosDbgCheck(thread != NULL);
162
163
  switch (sig) {
164
    case URT_THREAD_TERMINATE_REQUEST:
165
      chThdTerminate(thread);
166
      return;
167
    case URT_THREAD_TERMINATE_KILL:
168
      /*
169
       * TODO: implement kill functionality
170
       */
171
      chThdTerminate(thread);
172
      return;
173
  }
174
}
175
176
urt_osThreadState_t urtThreadGetState(urt_osThread_t* thread)
177
{
178
  aosDbgCheck(thread != NULL);
179
180
  switch (thread->state) {
181
    case CH_STATE_CURRENT:
182
      return URT_THREAD_STATE_RUNNING;
183
    case CH_STATE_READY:
184
      return URT_THREAD_STATE_READY;
185
    case CH_STATE_SLEEPING:
186
      return URT_THREAD_STATE_SLEEPING;
187
    case CH_STATE_WTSTART:
188
    case CH_STATE_SUSPENDED:
189
      return URT_THREAD_STATE_SUSPENDED;
190
    case CH_STATE_FINAL:
191
      return URT_THREAD_STATE_TERMINATED;
192
    case CH_STATE_QUEUED:
193
    case CH_STATE_WTSEM:
194
    case CH_STATE_WTMTX:
195
    case CH_STATE_WTCOND:
196
    case CH_STATE_WTEXIT:
197
    case CH_STATE_WTOREVT:
198
    case CH_STATE_WTANDEVT:
199
    case CH_STATE_SNDMSGQ:
200
    case CH_STATE_SNDMSG:
201
    case CH_STATE_WTMSG:
202
    default:
203
      return URT_THREAD_STATE_WAITING;
204
  }
205
}
206
207
/*============================================================================*/
208
/* TIMER                                                                      */
209
/*============================================================================*/
210
211
void urtTimerSet(urt_osTimer_t* timer, urt_delay_t delay, urt_osTimerCallback_t callback, void* cbparams)
212
{
213
  aosDbgCheck(timer != NULL);
214
  aosDbgCheck(callback != NULL);
215
216
  if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) {
217
    aosTimerSetLongInterval(timer, (aos_longinterval_t)delay, callback, cbparams);
218
  } else {
219
    aosTimerSetInterval(timer, delay, callback, cbparams);
220
  }
221
  return;
222
}
223
224
void urtTimerSetPeriodic(urt_osTimer_t* timer, urt_delay_t period, urt_osTimerCallback_t callback, void* cbparams)
225
{
226
  aosDbgCheck(timer != NULL);
227
  aosDbgCheck(callback != NULL);
228
229
  if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) {
230
    aosTimerPeriodicLongInterval(timer, (aos_longinterval_t)period, callback, cbparams);
231
  } else {
232
    aosTimerPeriodicInterval(timer, period, callback, cbparams);
233
  }
234
  return;
235
}