aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/os/various/lwip_bindings/arch/sys_arch.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/os/various/lwip_bindings/arch/sys_arch.c')
-rw-r--r--lib/chibios/os/various/lwip_bindings/arch/sys_arch.c239
1 files changed, 239 insertions, 0 deletions
diff --git a/lib/chibios/os/various/lwip_bindings/arch/sys_arch.c b/lib/chibios/os/various/lwip_bindings/arch/sys_arch.c
new file mode 100644
index 000000000..12e4fba13
--- /dev/null
+++ b/lib/chibios/os/various/lwip_bindings/arch/sys_arch.c
@@ -0,0 +1,239 @@
1/*
2 ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16/*
17 * **** This file incorporates work covered by the following copyright and ****
18 * **** permission notice: ****
19 *
20 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without modification,
24 * are permitted provided that the following conditions are met:
25 *
26 * 1. Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright notice,
29 * this list of conditions and the following disclaimer in the documentation
30 * and/or other materials provided with the distribution.
31 * 3. The name of the author may not be used to endorse or promote products
32 * derived from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
37 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
39 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
43 * OF SUCH DAMAGE.
44 *
45 * This file is part of the lwIP TCP/IP stack.
46 *
47 * Author: Adam Dunkels <[email protected]>
48 *
49 */
50
51/*
52 * See http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions.
53 */
54
55#include "hal.h"
56
57#include "lwip/opt.h"
58#include "lwip/mem.h"
59#include "lwip/sys.h"
60#include "lwip/stats.h"
61
62#include "arch/cc.h"
63#include "arch/sys_arch.h"
64
65void sys_init(void) {
66
67}
68
69err_t sys_sem_new(sys_sem_t *sem, u8_t count) {
70
71 *sem = chHeapAlloc(NULL, sizeof(semaphore_t));
72 if (*sem == 0) {
73 SYS_STATS_INC(sem.err);
74 return ERR_MEM;
75 }
76 else {
77 chSemObjectInit(*sem, (cnt_t)count);
78 SYS_STATS_INC_USED(sem);
79 return ERR_OK;
80 }
81}
82
83void sys_sem_free(sys_sem_t *sem) {
84
85 chHeapFree(*sem);
86 *sem = SYS_SEM_NULL;
87 SYS_STATS_DEC(sem.used);
88}
89
90void sys_sem_signal(sys_sem_t *sem) {
91
92 chSemSignal(*sem);
93}
94
95/* CHIBIOS FIX: specific variant of this call to be called from within
96 a lock.*/
97void sys_sem_signal_S(sys_sem_t *sem) {
98
99 chSemSignalI(*sem);
100 chSchRescheduleS();
101}
102
103u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) {
104 systime_t start;
105 sysinterval_t tmo, remaining;
106
107 chSysLock();
108 tmo = timeout > 0 ? TIME_MS2I((time_msecs_t)timeout) : TIME_INFINITE;
109 start = chVTGetSystemTimeX();
110 if (chSemWaitTimeoutS(*sem, tmo) != MSG_OK) {
111 chSysUnlock();
112 return SYS_ARCH_TIMEOUT;
113 }
114 remaining = chTimeDiffX(start, chVTGetSystemTimeX());
115 chSysUnlock();
116 return (u32_t)TIME_I2MS(remaining);
117}
118
119int sys_sem_valid(sys_sem_t *sem) {
120 return *sem != SYS_SEM_NULL;
121}
122
123// typically called within lwIP after freeing a semaphore
124// to make sure the pointer is not left pointing to invalid data
125void sys_sem_set_invalid(sys_sem_t *sem) {
126 *sem = SYS_SEM_NULL;
127}
128
129err_t sys_mbox_new(sys_mbox_t *mbox, int size) {
130
131 *mbox = chHeapAlloc(NULL, sizeof(mailbox_t) + sizeof(msg_t) * size);
132 if (*mbox == 0) {
133 SYS_STATS_INC(mbox.err);
134 return ERR_MEM;
135 }
136 else {
137 chMBObjectInit(*mbox, (void *)(((uint8_t *)*mbox) + sizeof(mailbox_t)), size);
138 SYS_STATS_INC(mbox.used);
139 return ERR_OK;
140 }
141}
142
143void sys_mbox_free(sys_mbox_t *mbox) {
144 cnt_t tmpcnt;
145
146 chSysLock();
147 tmpcnt = chMBGetUsedCountI(*mbox);
148 chSysUnlock();
149
150 if (tmpcnt != 0) {
151 // If there are messages still present in the mailbox when the mailbox
152 // is deallocated, it is an indication of a programming error in lwIP
153 // and the developer should be notified.
154 SYS_STATS_INC(mbox.err);
155 chMBReset(*mbox);
156 }
157 chHeapFree(*mbox);
158 *mbox = SYS_MBOX_NULL;
159 SYS_STATS_DEC(mbox.used);
160}
161
162void sys_mbox_post(sys_mbox_t *mbox, void *msg) {
163
164 chMBPostTimeout(*mbox, (msg_t)msg, TIME_INFINITE);
165}
166
167err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) {
168
169 if (chMBPostTimeout(*mbox, (msg_t)msg, TIME_IMMEDIATE) == MSG_TIMEOUT) {
170 SYS_STATS_INC(mbox.err);
171 return ERR_MEM;
172 }
173 return ERR_OK;
174}
175
176u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) {
177 systime_t start;
178 sysinterval_t tmo, remaining;
179
180 chSysLock();
181 tmo = timeout > 0 ? TIME_MS2I((time_msecs_t)timeout) : TIME_INFINITE;
182 start = chVTGetSystemTimeX();
183 if (chMBFetchTimeoutS(*mbox, (msg_t *)msg, tmo) != MSG_OK) {
184 chSysUnlock();
185 return SYS_ARCH_TIMEOUT;
186 }
187 remaining = chTimeDiffX(start, chVTGetSystemTimeX());
188 chSysUnlock();
189 return (u32_t)TIME_I2MS(remaining);
190}
191
192u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) {
193
194 if (chMBFetchTimeout(*mbox, (msg_t *)msg, TIME_IMMEDIATE) == MSG_TIMEOUT)
195 return SYS_MBOX_EMPTY;
196 return 0;
197}
198
199int sys_mbox_valid(sys_mbox_t *mbox) {
200 return *mbox != SYS_MBOX_NULL;
201}
202
203// typically called within lwIP after freeing an mbox
204// to make sure the pointer is not left pointing to invalid data
205void sys_mbox_set_invalid(sys_mbox_t *mbox) {
206 *mbox = SYS_MBOX_NULL;
207}
208
209sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread,
210 void *arg, int stacksize, int prio) {
211 thread_t *tp;
212
213 tp = chThdCreateFromHeap(NULL, THD_WORKING_AREA_SIZE(stacksize),
214 name, prio, (tfunc_t)thread, arg);
215 return (sys_thread_t)tp;
216}
217
218sys_prot_t sys_arch_protect(void) {
219
220 return chSysGetStatusAndLockX();
221}
222
223void sys_arch_unprotect(sys_prot_t pval) {
224
225 chSysRestoreStatusX((syssts_t)pval);
226}
227
228u32_t sys_now(void) {
229
230#if OSAL_ST_FREQUENCY == 1000
231 return (u32_t)chVTGetSystemTimeX();
232#elif (OSAL_ST_FREQUENCY / 1000) >= 1 && (OSAL_ST_FREQUENCY % 1000) == 0
233 return ((u32_t)chVTGetSystemTimeX() - 1) / (OSAL_ST_FREQUENCY / 1000) + 1;
234#elif (1000 / OSAL_ST_FREQUENCY) >= 1 && (1000 % OSAL_ST_FREQUENCY) == 0
235 return ((u32_t)chVTGetSystemTimeX() - 1) * (1000 / OSAL_ST_FREQUENCY) + 1;
236#else
237 return (u32_t)(((u64_t)(chVTGetSystemTimeX() - 1) * 1000) / OSAL_ST_FREQUENCY) + 1;
238#endif
239}