Statistics
| Branch: | Revision:

amiro-apps / middleware / apps_urtosal.c @ 358e7de5

History | View | Annotate | Download (7.05 KB)

1
/*
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
/**
38
 * @details Due to limitations of ChibiOS, the 'mutex' argument is ignored in
39
 *          this implementation. Instead, the mutex, which was locked by the
40
 *          calling thread most recently is used.
41
 *          In debug builds, however, the argument is still checked to be
42
 *          identical to implicitely selected mutex.
43
 */
44
urt_osCondvarWaitStatus_t urtCondvarWait(urt_osCondvar_t* condvar, urt_osMutex_t* mutex, urt_delay_t timeout)
45
{
46
  aosDbgCheck(condvar != NULL);
47
  aosDbgCheck(mutex != NULL);
48
  aosDbgCheck(chMtxGetNextMutexX() == mutex); // due to limitation of ChibiOS
49

    
50
  (void)mutex;
51

    
52
  switch (chCondWaitTimeout(condvar, timeout)) {
53
    case MSG_OK:
54
      return URT_CONDVAR_WAITSTATUS_SIGNAL;
55
    case MSG_RESET:
56
      return URT_CONDVAR_WAITSTATUS_BROADCAST;
57
    case MSG_TIMEOUT:
58
      return URT_CONDVAR_WAITSTATUS_TIMEOUT;
59
    default:
60
      chSysHalt(__func__);
61
      return URT_CONDVAR_WAITSTATUS_TIMEOUT;
62
  }
63
}
64

    
65
/*============================================================================*/
66
/* EVENTS                                                                     */
67
/*============================================================================*/
68

    
69
urt_osEventMask_t urtEventWait(urt_osEventMask_t mask, urt_osEventWait_t type, urt_delay_t timeout)
70
{
71
  switch (type) {
72
    case URT_EVENT_WAIT_ONE:
73
      return chEvtWaitOneTimeout(mask, timeout);
74
    case URT_EVENT_WAIT_ANY:
75
      return chEvtWaitAnyTimeout(mask, timeout);
76
    case URT_EVENT_WAIT_ALL:
77
      return chEvtWaitAllTimeout(mask, timeout);
78
    default:
79
      return 0;
80
  }
81
}
82

    
83
/*============================================================================*/
84
/* STREAMS                                                                    */
85
/*============================================================================*/
86

    
87
/*============================================================================*/
88
/* TIME                                                                       */
89
/*============================================================================*/
90

    
91
urt_osTime_t urtTimeNow(void)
92
{
93
  urt_osTime_t time;
94
  aosSysGetUptime(&time);
95
  return time;
96
}
97

    
98
/*============================================================================*/
99
/* THREAD                                                                     */
100
/*============================================================================*/
101

    
102
urt_osThread_t* urtThreadInit(void* memory, size_t size, urt_osThreadPrio_t prio, urt_osThreadFunction_t func, void* arg)
103
{
104
  aosDbgCheck(memory != NULL);
105
  aosDbgCheck(size != 0);
106
  aosDbgCheck(prio >= URT_THREAD_PRIO_LOW_MIN && prio <= URT_THREAD_PRIO_RT_MAX);
107
  aosDbgCheck(func != NULL);
108

    
109
  const thread_descriptor_t descriptor = {
110
    /* name                         */ "",
111
    /* pointer to working area base */ (stkalign_t*)memory,
112
    /* end of the working area      */ &((stkalign_t*)memory)[size / sizeof(stkalign_t)],
113
    /* thread priority              */ prio,
114
    /* thread function pointer      */ func,
115
    /* thread argument              */ arg,
116
    /* pointer to the parent thread */ chThdGetSelfX(),
117
  };
118

    
119
  return chThdCreateSuspended(&descriptor);
120
}
121

    
122
void urtThreadTerminate(urt_osThread_t* thread, urt_osThreadTerminateSignal_t sig)
123
{
124
  aosDbgCheck(thread != NULL);
125

    
126
  switch (sig) {
127
    case URT_THREAD_TERMINATE_REQUEST:
128
      chThdTerminate(thread);
129
      return;
130
    case URT_THREAD_TERMINATE_KILL:
131
      /*
132
       * TODO: implement kill functionality
133
       */
134
      chThdTerminate(thread);
135
      return;
136
  }
137
}
138

    
139
bool urtThreadShouldTerminate(void)
140
{
141
    return true;
142
}
143

    
144
urt_osThreadState_t urtThreadGetState(urt_osThread_t* thread)
145
{
146
  aosDbgCheck(thread != NULL);
147

    
148
  switch (thread->state) {
149
    case CH_STATE_CURRENT:
150
      return URT_THREAD_STATE_RUNNING;
151
    case CH_STATE_READY:
152
      return URT_THREAD_STATE_READY;
153
    case CH_STATE_SLEEPING:
154
      return URT_THREAD_STATE_SLEEPING;
155
    case CH_STATE_WTSTART:
156
    case CH_STATE_SUSPENDED:
157
      return URT_THREAD_STATE_SUSPENDED;
158
    case CH_STATE_FINAL:
159
      return URT_THREAD_STATE_TERMINATED;
160
    case CH_STATE_QUEUED:
161
    case CH_STATE_WTSEM:
162
    case CH_STATE_WTMTX:
163
    case CH_STATE_WTCOND:
164
    case CH_STATE_WTEXIT:
165
    case CH_STATE_WTOREVT:
166
    case CH_STATE_WTANDEVT:
167
    case CH_STATE_SNDMSGQ:
168
    case CH_STATE_SNDMSG:
169
    case CH_STATE_WTMSG:
170
    default:
171
      return URT_THREAD_STATE_WAITING;
172
  }
173
}
174

    
175
/*============================================================================*/
176
/* TIMER                                                                      */
177
/*============================================================================*/
178

    
179
void urtTimerSet(urt_osTimer_t* timer, urt_delay_t delay, urt_osTimerCallback_t callback, void* cbparams)
180
{
181
  aosDbgCheck(timer != NULL);
182
  aosDbgCheck(callback != NULL);
183

    
184
  if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) {
185
    aosTimerSetLongInterval(timer, (aos_longinterval_t)delay, callback, cbparams);
186
  } else {
187
    aosTimerSetInterval(timer, delay, callback, cbparams);
188
  }
189
  return;
190
}
191

    
192
void urtTimerSetPeriodic(urt_osTimer_t* timer, urt_delay_t period, urt_osTimerCallback_t callback, void* cbparams)
193
{
194
  aosDbgCheck(timer != NULL);
195
  aosDbgCheck(callback != NULL);
196

    
197
  if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) {
198
    aosTimerPeriodicLongInterval(timer, (aos_longinterval_t)period, callback, cbparams);
199
  } else {
200
    aosTimerPeriodicInterval(timer, period, callback, cbparams);
201
  }
202
  return;
203
}