sys_arch.c
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include "arch/cc.h"
#include "arch/sys_arch.h"
#include "lwip/err.h"
#include "lwip/mem.h"
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "msg.h"
#include "sema.h"
#include "thread.h"
#include "xtimer.h"
#define _MSG_SUCCESS (0x5cac)
#define _MSG_TIMEOUT (0x5cad)
void sys_init(void)
{
return;
}
err_t sys_mutex_new(sys_mutex_t *mutex)
{
mutex_init((mutex_t *)mutex);
return ERR_OK;
}
void sys_mutex_lock(sys_mutex_t *mutex)
{
mutex_lock((mutex_t *)mutex);
}
void sys_mutex_unlock(sys_mutex_t *mutex)
{
mutex_unlock((mutex_t *)mutex);
}
void sys_mutex_free(sys_mutex_t *mutex)
{
mem_free(mutex);
}
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
{
if (sema_create((sema_t *)sem, (unsigned int)count) < 0) {
return ERR_VAL;
}
return ERR_OK;
}
void sys_sem_free(sys_sem_t *sem)
{
sema_destroy((sema_t *)sem);
}
void sys_sem_signal(sys_sem_t *sem)
{
LWIP_ASSERT("invalid semaphor", sys_sem_valid(sem));
sema_post((sema_t *)sem);
}
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t count)
{
LWIP_ASSERT("invalid semaphor", sys_sem_valid(sem));
if (count != 0) {
uint64_t stop, start;
start = xtimer_now_usec64();
int res = sema_wait_timed((sema_t *)sem, count * MS_IN_USEC);
stop = xtimer_now_usec64() - start;
if (res == -ETIMEDOUT) {
return SYS_ARCH_TIMEOUT;
}
return (u32_t)(stop / MS_IN_USEC);
}
else {
sema_wait_timed((sema_t *)sem, 0);
return 0;
}
}
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
{
(void)size;
mbox_init(&mbox->mbox, mbox->msgs, SYS_MBOX_SIZE);
return ERR_OK;
}
void sys_mbox_free(sys_mbox_t *mbox)
{
(void)mbox;
return;
}
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
{
msg_t m = { .content = { .ptr = msg }, .type = _MSG_SUCCESS };
LWIP_ASSERT("invalid mbox", sys_mbox_valid(mbox));
mbox_put(&mbox->mbox, &m);
}
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
{
msg_t m = { .content = { .ptr = msg }, .type = _MSG_SUCCESS };
if (mbox_try_put(&mbox->mbox, &m)) {
return ERR_OK;
}
else {
return ERR_MEM;
}
}
static void _mbox_timeout(void *arg)
{
msg_t m = { .type = _MSG_TIMEOUT };
mbox_put(arg, &m);
}
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
{
msg_t m;
xtimer_t timer = { .callback = _mbox_timeout, .arg = &mbox->mbox };
uint64_t start, stop;
start = xtimer_now_usec64();
if (timeout > 0) {
uint64_t u_timeout = (timeout * MS_IN_USEC);
_xtimer_set64(&timer, (uint32_t)u_timeout, (uint32_t)(u_timeout >> 32));
}
mbox_get(&mbox->mbox, &m);
stop = xtimer_now_usec64();
xtimer_remove(&timer); /* in case timer did not time out */
switch (m.type) {
case _MSG_SUCCESS:
*msg = m.content.ptr;
return (u32_t)((stop - start) / MS_IN_USEC);
case _MSG_TIMEOUT:
break;
default: /* should not happen */
LWIP_ASSERT("invalid message received", false);
break;
}
return SYS_ARCH_TIMEOUT;
}
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
{
msg_t m;
if (mbox_try_get(&mbox->mbox, &m)) {
LWIP_ASSERT("invalid message received", (m.type == _MSG_SUCCESS));
*msg = m.content.ptr;
return ERR_OK;
}
else {
return SYS_MBOX_EMPTY;
}
}
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg,
int stacksize, int prio)
{
kernel_pid_t res;
char *stack = mem_malloc((size_t)stacksize);
if (stack == NULL) {
return ERR_MEM;
}
if ((res = thread_create(stack, stacksize, prio, THREAD_CREATE_STACKTEST,
(thread_task_func_t)thread, arg, name)) <= KERNEL_PID_UNDEF) {
abort();
}
sched_switch((char)prio);
return res;
}
/** @} */