123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- #include <rtthread.h>
- #include "lwip/debug.h"
- #include "lwip/sys.h"
- #include "lwip/opt.h"
- #include "lwip/stats.h"
- #include "lwip/err.h"
- #include "arch/sys_arch.h"
- #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;
- 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)
- {
- #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;
- /* 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 */
- 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;
- }
- /* 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;
- }
- #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
- /* ====================== Mutex ====================== */
- /** 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;
- 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)
- {
- #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)
- {
- #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
- /* ====================== 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;
- 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)
- {
- #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;
- }
- /** 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) */
- void sys_mbox_post(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,
- (*mbo)x->parent.parent.name, (rt_uint32_t)msg));
- }
- #endif
- if (rt_mb_send(*mbox, (rt_uint32_t)msg) != RT_EOK)
- rt_kprintf("TODO: FIX THIS!! mbox overflow");
- 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
- if (rt_mb_send(*mbox, (rt_uint32_t)msg) == RT_EOK)
- return ERR_OK;
- return ERR_MEM;
- }
- /** 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! */
- 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;
- /* get the begin tick */
- tick = rt_tick_get();
- 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;
- }
- #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
- /* 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;
- }
- /** 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 */
- 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;
- }
- #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;
- /* 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)
- {
- /* disable interrupt */
- return rt_hw_interrupt_disable();
- }
- void sys_arch_unprotect(sys_prot_t pval)
- {
- /* 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);
- }
|