sys_arch.c 6.1 KB
Newer Older
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
#include <rtthread.h>

#include "lwip/debug.h"
#include "lwip/sys.h"
#include "lwip/opt.h"
#include "lwip/stats.h"
#include "arch/sys_arch.h"

#include <string.h>

#define LWIP_THREAD_MAGIC	0x1919
struct lwip_thread
{
	rt_uint32_t magic;
	struct sys_timeouts timeouts;

	rt_thread_t tid;
};

void sys_init(void)
{
	/* nothing to do in RT-Thread */

	return;
}

sys_sem_t sys_sem_new(u8_t count)
{
	static int counter = 0;
	char tname[RT_NAME_MAX];

	rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_SEM_NAME, counter);

B
bernard.xiong 已提交
34
#if SYS_DEBUG
35 36
	{
		struct rt_thread *thread;
37

38
		thread = rt_thread_self();
B
bernard.xiong 已提交
39
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Create sem: %s \n",thread->name, tname));
40 41 42 43 44 45 46 47 48 49
	}
#endif

	counter++;

	return rt_sem_create(tname, count, RT_IPC_FLAG_FIFO);
}

void sys_sem_free(sys_sem_t sem)
{
B
bernard.xiong 已提交
50
#if SYS_DEBUG
51 52 53
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
54

B
bernard.xiong 已提交
55
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete sem: %s \n",thread->name,
56 57 58 59 60 61 62 63 64 65 66
			sem->parent.parent.name));
	}
#endif

	rt_sem_delete(sem);

	return;
}

void sys_sem_signal(sys_sem_t sem)
{
B
bernard.xiong 已提交
67
#if SYS_DEBUG
68 69 70
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
71

B
bernard.xiong 已提交
72
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Release signal: %s , %d\n",thread->name,
73 74 75 76 77 78 79 80 81 82 83 84 85
			sem->parent.parent.name, sem->value));
	}
#endif

	rt_sem_release(sem);

	return;
}

u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
{
	rt_err_t ret;
	s32_t t;
B
bernard.xiong 已提交
86
	u32_t tick;
87

B
bernard.xiong 已提交
88 89
	/* get the begin tick */
	tick = rt_tick_get();
B
bernard.xiong 已提交
90
#if SYS_DEBUG
91 92 93
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
94

B
bernard.xiong 已提交
95
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Wait sem: %s , %d\n",thread->name,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
			sem->parent.parent.name, sem->value));
	}
#endif

	if(timeout == 0)
		t = RT_WAITING_FOREVER;
	else
	{
		/* convirt msecond to os tick */
		if (timeout < (1000/RT_TICK_PER_SECOND)) t = 1;
		else t = timeout / (1000/RT_TICK_PER_SECOND);
	}

	ret = rt_sem_take(sem, t);

	if (ret == -RT_ETIMEOUT) return SYS_ARCH_TIMEOUT;
	else if (ret == RT_EOK) ret = 1;

B
bernard.xiong 已提交
114 115 116 117 118 119 120 121
	/* get elapse msecond */
	tick = rt_tick_get() - tick;

	/* convert tick to msecond */
	tick = tick * (1000/RT_TICK_PER_SECOND);
	if (tick == 0) tick = 1;

	return tick;
122 123 124 125 126 127 128 129 130
}

sys_mbox_t sys_mbox_new(int size)
{
	static int counter = 0;
	char tname[RT_NAME_MAX];

	rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MBOX_NAME, counter);

B
bernard.xiong 已提交
131
#if SYS_DEBUG
132 133 134
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
135

B
bernard.xiong 已提交
136
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Create mbox: %s \n",thread->name, tname));
137 138 139 140 141 142 143 144 145 146
	}
#endif

	counter++;

	return rt_mb_create(tname, size, RT_IPC_FLAG_FIFO);
}

void sys_mbox_free(sys_mbox_t mbox)
{
B
bernard.xiong 已提交
147
#if SYS_DEBUG
148 149 150
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
151

B
bernard.xiong 已提交
152
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete mbox: %s\n",thread->name,
153 154 155 156 157 158 159 160 161 162 163
			mbox->parent.parent.name));
	}
#endif

	rt_mb_delete(mbox);

	return;
}

void sys_mbox_post(sys_mbox_t mbox, void *msg)
{
B
bernard.xiong 已提交
164
#if SYS_DEBUG
165 166 167
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
168

B
bernard.xiong 已提交
169
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Post mail: %s ,0x%x\n",thread->name,
170 171 172 173 174 175 176 177 178 179 180
			mbox->parent.parent.name, (rt_uint32_t)msg));
	}
#endif

	rt_mb_send(mbox, (rt_uint32_t)msg);

	return;
}

err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg)
{
B
bernard.xiong 已提交
181
#if SYS_DEBUG
182 183 184
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
185

B
bernard.xiong 已提交
186
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Post mail: %s ,0x%x\n",thread->name,
187 188 189 190 191 192 193 194 195 196 197 198 199
			mbox->parent.parent.name, (rt_uint32_t)msg));
	}
#endif

	if (rt_mb_send(mbox, (rt_uint32_t)msg) == RT_EOK) return ERR_OK;

	return ERR_MEM;
}

u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)
{
	rt_err_t ret;
	s32_t t;
B
bernard.xiong 已提交
200 201 202 203
	u32_t tick;

	/* get the begin tick */
	tick = rt_tick_get();
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	if(timeout == 0)
		t = RT_WAITING_FOREVER;
	else
	{
		/* convirt msecond to os tick */
		if (timeout < (1000/RT_TICK_PER_SECOND)) t = 1;
		else t = timeout / (1000/RT_TICK_PER_SECOND);
	}

	ret = rt_mb_recv(mbox, (rt_uint32_t *)msg, t);

	if(ret == -RT_ETIMEOUT) return SYS_ARCH_TIMEOUT;
	else if (ret == RT_EOK) ret = 1;

B
bernard.xiong 已提交
219
#if SYS_DEBUG
220 221 222
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
223

B
bernard.xiong 已提交
224
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Fetch mail: %s , 0x%x\n",thread->name,
225 226 227 228
			mbox->parent.parent.name, *(rt_uint32_t **)msg));
	}
#endif

B
bernard.xiong 已提交
229 230 231 232 233 234 235 236
	/* get elapse msecond */
	tick = rt_tick_get() - tick;

	/* convert tick to msecond */
	tick = tick * (1000/RT_TICK_PER_SECOND);
	if (tick == 0) tick = 1;

	return tick;
237 238 239 240 241 242 243 244 245 246 247
}

u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg)
{
	int ret;

	ret = rt_mb_recv(mbox, (rt_uint32_t *)msg, 0);

	if(ret == -RT_ETIMEOUT) return SYS_ARCH_TIMEOUT;
	else if (ret == RT_EOK) ret = 1;

B
bernard.xiong 已提交
248
#if SYS_DEBUG
249 250 251
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
252

B
bernard.xiong 已提交
253
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Fetch mail: %s , 0x%x\n",thread->name,
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
			mbox->parent.parent.name, *(rt_uint32_t **)msg));
	}
#endif

	return ret;
}

struct sys_timeouts *sys_arch_timeouts(void)
{
	rt_thread_t self = rt_thread_self();
	struct lwip_thread* lwip_th = (struct lwip_thread*)self->user_data;

	if (lwip_th != RT_NULL)
	{
		RT_ASSERT(lwip_th->magic == LWIP_THREAD_MAGIC);

		return &(lwip_th->timeouts);
	}

	return RT_NULL;
}

sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)
{
	rt_thread_t t;
	struct lwip_thread* lwip_th;

	/* create lwip thread */
	lwip_th = (struct lwip_thread*) rt_malloc (sizeof(struct lwip_thread));
	RT_ASSERT(lwip_th != RT_NULL);

	/* create thread */
	t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
	RT_ASSERT(t != RT_NULL);
	t->user_data = (rt_uint32_t)lwip_th;

	/* init lwip_thread */
	lwip_th->magic = LWIP_THREAD_MAGIC;
	lwip_th->timeouts.next = RT_NULL;
	lwip_th->tid = t;

	/* startup thread */
	rt_thread_startup(t);

	return t;
}

sys_prot_t sys_arch_protect(void)
{
	/* disable interrupt */
	return rt_hw_interrupt_disable();
}

void sys_arch_unprotect(sys_prot_t pval)
{
	/* enable interrupt */
	rt_hw_interrupt_enable(pval);

	return;
}
B
bernard.xiong 已提交
314 315 316

void sys_arch_assert(const char* file, int line)
{
317 318
	rt_kprintf("\nAssertion: %d in %s, thread %s\n", line, file,
        rt_thread_self()->name);
B
bernard.xiong 已提交
319 320
	RT_ASSERT(0);
}