sys_arch.c 9.7 KB
Newer Older
1 2 3 4 5 6 7
#include <rtthread.h>

#include "lwip/sys.h"
#include "lwip/opt.h"
#include "lwip/stats.h"
#include "lwip/err.h"
#include "arch/sys_arch.h"
8
#include "lwip/debug.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

#include <string.h>

#define LWIP_THREAD_MAGIC	0x1919


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

	return;
}

/* ====================== Semaphore ====================== */

err_t sys_sem_new(sys_sem_t *sem, u8_t count)
{
	static unsigned short counter = 0;
	char tname[RT_NAME_MAX];
	sys_sem_t tmpsem;

M
mbbill@gmail.com 已提交
30
	RT_DEBUG_NOT_IN_INTERRUPT;
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
	rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_SEM_NAME, counter);

#if SYS_DEBUG
	{
		struct rt_thread *thread;

		thread = rt_thread_self();
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Create sem: %s \n",thread->name, tname));
	}
#endif

	counter++;

	tmpsem = rt_sem_create(tname, count, RT_IPC_FLAG_FIFO);
	if( tmpsem == RT_NULL )
		return ERR_MEM;
	else
	{
		*sem = tmpsem;
		return ERR_OK;
	}
}


void sys_sem_free(sys_sem_t *sem)
{
M
mbbill@gmail.com 已提交
58
	RT_DEBUG_NOT_IN_INTERRUPT;
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
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete sem: %s \n",thread->name,
			(*sem)->parent.parent.name));
	}
#endif

	rt_sem_delete(*sem);

}


void sys_sem_signal(sys_sem_t *sem)
{
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Release signal: %s , %d\n",thread->name,
			(*sem)->parent.parent.name, (*sem)->value));
	}
#endif

	rt_sem_release(*sem);

}

u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
{
	rt_err_t ret;
	s32_t t;
	u32_t tick;

M
mbbill@gmail.com 已提交
97
	RT_DEBUG_NOT_IN_INTERRUPT;
98
	
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	/* get the begin tick */
	tick = rt_tick_get();
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Wait sem: %s , %d\n",thread->name,
			(*sem)->parent.parent.name, (*sem)->value));
	}
#endif

	if(timeout == 0)
		t = RT_WAITING_FOREVER;
	else
	{
		/* convirt msecond to os tick */
M
mbbill@gmail.com 已提交
116 117 118 119
		if (timeout < (1000/RT_TICK_PER_SECOND))
			t = 1;
		else
			t = timeout / (1000/RT_TICK_PER_SECOND);
120 121 122 123
	}

	ret = rt_sem_take(*sem, t);

M
mbbill@gmail.com 已提交
124 125 126 127 128 129 130
	if (ret == -RT_ETIMEOUT)
		return SYS_ARCH_TIMEOUT;
	else
	{
		if (ret == RT_EOK)
			ret = 1;
	}
131 132 133 134 135 136

	/* get elapse msecond */
	tick = rt_tick_get() - tick;

	/* convert tick to msecond */
	tick = tick * (1000/RT_TICK_PER_SECOND);
M
mbbill@gmail.com 已提交
137 138
	if (tick == 0)
		tick = 1;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

	return tick;
}

#ifndef sys_sem_valid
/** Check if a sempahore is valid/allocated: return 1 for valid, 0 for invalid */
int sys_sem_valid(sys_sem_t *sem)
{
	return (int)(*sem);
}
#endif
#ifndef sys_sem_set_invalid
/** Set a semaphore invalid so that sys_sem_valid returns 0 */
void sys_sem_set_invalid(sys_sem_t *sem)
{
	*sem = RT_NULL;
}
#endif


M
mbbill@gmail.com 已提交
159
/* ====================== Mutex ====================== */
160

M
mbbill@gmail.com 已提交
161 162 163 164 165 166 167 168 169
/** Create a new mutex
 * @param mutex pointer to the mutex to create
 * @return a new mutex */
err_t sys_mutex_new(sys_mutex_t *mutex)
{
	static unsigned short counter = 0;
	char tname[RT_NAME_MAX];
	sys_mutex_t tmpmutex;

M
mbbill@gmail.com 已提交
170
	RT_DEBUG_NOT_IN_INTERRUPT;
171

M
mbbill@gmail.com 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MUTEX_NAME, counter);

#if SYS_DEBUG
	{
		struct rt_thread *thread;

		thread = rt_thread_self();
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Create mutex: %s \n",thread->name, tname));
	}
#endif

	counter++;

	tmpmutex = rt_mutex_create(tname, RT_IPC_FLAG_FIFO);
	if( tmpmutex == RT_NULL )
		return ERR_MEM;
	else
	{
		*mutex = tmpmutex;
		return ERR_OK;
	}
}

/** Lock a mutex
 * @param mutex the mutex to lock */
void sys_mutex_lock(sys_mutex_t *mutex)
{

M
mbbill@gmail.com 已提交
200
	RT_DEBUG_NOT_IN_INTERRUPT;
201

M
mbbill@gmail.com 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Wait mutex: %s , %d\n",thread->name,
			(*mutex)->parent.parent.name, (*mutex)->value));
	}
#endif

	rt_mutex_take(*mutex, RT_WAITING_FOREVER);

	return;
}


/** Unlock a mutex
 * @param mutex the mutex to unlock */
void sys_mutex_unlock(sys_mutex_t *mutex)
{
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Release signal: %s , %d\n",thread->name,
			(*mutex)->parent.parent.name, (*mutex)->value));
	}
#endif

	rt_mutex_release(*mutex);
}

/** Delete a semaphore
 * @param mutex the mutex to delete */
void sys_mutex_free(sys_mutex_t *mutex)
{
M
mbbill@gmail.com 已提交
239
	RT_DEBUG_NOT_IN_INTERRUPT;
240

M
mbbill@gmail.com 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete sem: %s \n",thread->name,
			(*mutex)->parent.parent.name));
	}
#endif

	rt_mutex_delete(*mutex);
}

#ifndef sys_mutex_valid
/** Check if a mutex is valid/allocated: return 1 for valid, 0 for invalid */
int sys_mutex_valid(sys_mutex_t *mutex)
{
	return (int)(*mutex);
}
#endif

#ifndef sys_mutex_set_invalid
/** Set a mutex invalid so that sys_mutex_valid returns 0 */
void sys_mutex_set_invalid(sys_mutex_t *mutex)
{
	*mutex = RT_NULL;
}
#endif
269 270 271 272 273 274 275 276 277

/* ====================== Mailbox ====================== */

err_t sys_mbox_new(sys_mbox_t *mbox, int size)
{
	static unsigned short counter = 0;
	char tname[RT_NAME_MAX];
	sys_mbox_t tmpmbox;

M
mbbill@gmail.com 已提交
278
	RT_DEBUG_NOT_IN_INTERRUPT;
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
	rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MBOX_NAME, counter);

#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();
		LWIP_DEBUGF(SYS_DEBUG, ("%s, Create mbox: %s \n",thread->name, tname));
	}
#endif

	counter++;

	tmpmbox = rt_mb_create(tname, size, RT_IPC_FLAG_FIFO);
	if( tmpmbox != RT_NULL )
	{
		*mbox = tmpmbox;
		return ERR_OK;
	}
	else
		return ERR_MEM;

}

void sys_mbox_free(sys_mbox_t *mbox)
{
M
mbbill@gmail.com 已提交
305
	RT_DEBUG_NOT_IN_INTERRUPT;
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete mbox: %s\n",thread->name,
			(*mbox)->parent.parent.name));
	}
#endif

	rt_mb_delete(*mbox);

	return;
}

M
mbbill@gmail.com 已提交
322 323 324 325
/** Post a message to an mbox - may not fail
 * -> blocks if full, only used from tasks not from ISR
 * @param mbox mbox to posts the message
 * @param msg message to post (ATTENTION: can be NULL) */
326 327
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
{
M
mbbill@gmail.com 已提交
328
	RT_DEBUG_NOT_IN_INTERRUPT;
329

330 331 332 333 334 335
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Post mail: %s ,0x%x\n",thread->name,
336
			(*mbox)->parent.parent.name, (rt_uint32_t)msg));
337 338 339
	}
#endif

340 341
	rt_mb_send_wait(*mbox, (rt_uint32_t)msg,RT_WAITING_FOREVER);

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	return;
}

err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
{
#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Post mail: %s ,0x%x\n",thread->name,
			(*mbox)->parent.parent.name, (rt_uint32_t)msg));
	}
#endif

M
mbbill@gmail.com 已提交
357 358
	if (rt_mb_send(*mbox, (rt_uint32_t)msg) == RT_EOK)
		return ERR_OK;
359 360 361 362

	return ERR_MEM;
}

M
mbbill@gmail.com 已提交
363 364 365 366 367 368 369
/** Wait for a new message to arrive in the mbox
 * @param mbox mbox to get a message from
 * @param msg pointer where the message is stored
 * @param timeout maximum time (in milliseconds) to wait for a message
 * @return time (in milliseconds) waited for a message, may be 0 if not waited
           or SYS_ARCH_TIMEOUT on timeout
 *         The returned time has to be accurate to prevent timer jitter! */
370 371 372 373 374 375
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
{
	rt_err_t ret;
	s32_t t;
	u32_t tick;

M
mbbill@gmail.com 已提交
376
	RT_DEBUG_NOT_IN_INTERRUPT;
377

378 379 380 381 382 383 384 385
	/* get the begin tick */
	tick = rt_tick_get();

	if(timeout == 0)
		t = RT_WAITING_FOREVER;
	else
	{
		/* convirt msecond to os tick */
M
mbbill@gmail.com 已提交
386 387 388 389
		if (timeout < (1000/RT_TICK_PER_SECOND))
			t = 1;
		else
			t = timeout / (1000/RT_TICK_PER_SECOND);
390 391 392 393
	}

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

M
mbbill@gmail.com 已提交
394 395 396 397
	if(ret == -RT_ETIMEOUT)
		return SYS_ARCH_TIMEOUT;
	else
	{
M
mbbill@gmail.com 已提交
398
		LWIP_ASSERT("rt_mb_recv returned with error!", ret == RT_EOK);
M
mbbill@gmail.com 已提交
399
	}
400 401 402 403 404 405 406

#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Fetch mail: %s , 0x%x\n",thread->name,
407
			(*mbox)->parent.parent.name, *(rt_uint32_t **)msg));
408 409 410 411 412 413 414 415
	}
#endif

	/* get elapse msecond */
	tick = rt_tick_get() - tick;

	/* convert tick to msecond */
	tick = tick * (1000/RT_TICK_PER_SECOND);
M
mbbill@gmail.com 已提交
416 417
	if (tick == 0)
		tick = 1;
418 419 420 421

	return tick;
}

M
mbbill@gmail.com 已提交
422 423 424 425 426 427
/** Wait for a new message to arrive in the mbox
 * @param mbox mbox to get a message from
 * @param msg pointer where the message is stored
 * @param timeout maximum time (in milliseconds) to wait for a message
 * @return 0 (milliseconds) if a message has been received
 *         or SYS_MBOX_EMPTY if the mailbox is empty */
428 429 430 431 432 433
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
{
	int ret;

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

M
mbbill@gmail.com 已提交
434 435 436 437 438 439 440
	if(ret == -RT_ETIMEOUT)
		return SYS_ARCH_TIMEOUT;
	else
	{
		if (ret == RT_EOK) 
			ret = 1;
	}
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

#if SYS_DEBUG
	{
		struct rt_thread *thread;
		thread = rt_thread_self();

		LWIP_DEBUGF(SYS_DEBUG, ("%s, Fetch mail: %s , 0x%x\n",thread->name,
			(*mbox)->parent.parent.name, *(rt_uint32_t **)msg));
	}
#endif

	return ret;
}

#ifndef sys_mbox_valid
/** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */
int sys_mbox_valid(sys_mbox_t *mbox)
{
	return (int)(*mbox);
}
#endif
#ifndef sys_mbox_set_invalid
/** Set an mbox invalid so that sys_mbox_valid returns 0 */
void sys_mbox_set_invalid(sys_mbox_t *mbox)
{
	*mbox = RT_NULL;
}
#endif




/* ====================== System ====================== */

sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
{
	rt_thread_t t;

M
mbbill@gmail.com 已提交
479
	RT_DEBUG_NOT_IN_INTERRUPT;
480

481 482 483 484 485 486 487 488 489 490 491 492
	/* create thread */
	t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
	RT_ASSERT(t != RT_NULL);

	/* startup thread */
	rt_thread_startup(t);

	return t;
}

sys_prot_t sys_arch_protect(void)
{
493 494
	rt_base_t level;

495
	/* disable interrupt */
496 497 498 499 500 501
	level = rt_hw_interrupt_disable();

	/* must also lock scheduler */
	rt_enter_critical();

	return level;
502 503 504 505
}

void sys_arch_unprotect(sys_prot_t pval)
{
506 507 508
	/* unlock scheduler */
	rt_exit_critical();

509 510 511 512 513 514 515 516 517 518 519 520 521
	/* enable interrupt */
	rt_hw_interrupt_enable(pval);

	return;
}

void sys_arch_assert(const char* file, int line)
{
	rt_kprintf("\nAssertion: %d in %s, thread %s\n", line, file,
        rt_thread_self()->name);
	RT_ASSERT(0);
}