amiro-apps / middleware / apps_urtosal.c @ 21f9c863
History | View | Annotate | Download (6.998 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 | /**
|
||
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 | urt_osThreadState_t urtThreadGetState(urt_osThread_t* thread) |
||
140 | { |
||
141 | aosDbgCheck(thread != NULL);
|
||
142 | |||
143 | switch (thread->state) {
|
||
144 | case CH_STATE_CURRENT:
|
||
145 | return URT_THREAD_STATE_RUNNING;
|
||
146 | case CH_STATE_READY:
|
||
147 | return URT_THREAD_STATE_READY;
|
||
148 | case CH_STATE_SLEEPING:
|
||
149 | return URT_THREAD_STATE_SLEEPING;
|
||
150 | case CH_STATE_WTSTART:
|
||
151 | case CH_STATE_SUSPENDED:
|
||
152 | return URT_THREAD_STATE_SUSPENDED;
|
||
153 | case CH_STATE_FINAL:
|
||
154 | return URT_THREAD_STATE_TERMINATED;
|
||
155 | case CH_STATE_QUEUED:
|
||
156 | case CH_STATE_WTSEM:
|
||
157 | case CH_STATE_WTMTX:
|
||
158 | case CH_STATE_WTCOND:
|
||
159 | case CH_STATE_WTEXIT:
|
||
160 | case CH_STATE_WTOREVT:
|
||
161 | case CH_STATE_WTANDEVT:
|
||
162 | case CH_STATE_SNDMSGQ:
|
||
163 | case CH_STATE_SNDMSG:
|
||
164 | case CH_STATE_WTMSG:
|
||
165 | default:
|
||
166 | return URT_THREAD_STATE_WAITING;
|
||
167 | } |
||
168 | } |
||
169 | |||
170 | /*============================================================================*/
|
||
171 | /* TIMER */
|
||
172 | /*============================================================================*/
|
||
173 | |||
174 | void urtTimerSet(urt_osTimer_t* timer, urt_delay_t delay, urt_osTimerCallback_t callback, void* cbparams) |
||
175 | { |
||
176 | aosDbgCheck(timer != NULL);
|
||
177 | aosDbgCheck(callback != NULL);
|
||
178 | |||
179 | if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) { |
||
180 | aosTimerSetLongInterval(timer, (aos_longinterval_t)delay, callback, cbparams); |
||
181 | } else {
|
||
182 | aosTimerSetInterval(timer, delay, callback, cbparams); |
||
183 | } |
||
184 | return;
|
||
185 | } |
||
186 | |||
187 | void urtTimerSetPeriodic(urt_osTimer_t* timer, urt_delay_t period, urt_osTimerCallback_t callback, void* cbparams) |
||
188 | { |
||
189 | aosDbgCheck(timer != NULL);
|
||
190 | aosDbgCheck(callback != NULL);
|
||
191 | |||
192 | if (sizeof(urt_delay_t) > sizeof(aos_interval_t)) { |
||
193 | aosTimerPeriodicLongInterval(timer, (aos_longinterval_t)period, callback, cbparams); |
||
194 | } else {
|
||
195 | aosTimerPeriodicInterval(timer, period, callback, cbparams); |
||
196 | } |
||
197 | return;
|
||
198 | } |