sys_arch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * File : sys_arch.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-12-8 Bernard add file header
  13. * export bsd socket symbol for RT-Thread Application Module
  14. */
  15. #include <rtthread.h>
  16. #include "lwip/sys.h"
  17. #include "lwip/opt.h"
  18. #include "lwip/stats.h"
  19. #include "lwip/err.h"
  20. #include "arch/sys_arch.h"
  21. #include "lwip/debug.h"
  22. #include "lwip/netif.h"
  23. #include "lwip/tcpip.h"
  24. #include "netif/ethernetif.h"
  25. #include "lwip/sio.h"
  26. #include <string.h>
  27. static err_t netif_device_init(struct netif *netif)
  28. {
  29. struct eth_device *ethif;
  30. ethif = (struct eth_device *)netif->state;
  31. if (ethif != RT_NULL)
  32. {
  33. rt_device_t device;
  34. /* get device object */
  35. device = (rt_device_t) ethif;
  36. if (rt_device_init(device) != RT_EOK)
  37. {
  38. return ERR_IF;
  39. }
  40. /* copy device flags to netif flags */
  41. netif->flags = ethif->flags;
  42. return ERR_OK;
  43. }
  44. return ERR_IF;
  45. }
  46. static void tcpip_init_done_callback(void *arg)
  47. {
  48. rt_device_t device;
  49. struct eth_device *ethif;
  50. struct ip_addr ipaddr, netmask, gw;
  51. struct rt_list_node* node;
  52. struct rt_object* object;
  53. struct rt_object_information *information;
  54. extern struct rt_object_information rt_object_container[];
  55. LWIP_ASSERT("invalid arg.\n",arg);
  56. IP4_ADDR(&gw, 0,0,0,0);
  57. IP4_ADDR(&ipaddr, 0,0,0,0);
  58. IP4_ADDR(&netmask, 0,0,0,0);
  59. /* enter critical */
  60. rt_enter_critical();
  61. /* for each network interfaces */
  62. information = &rt_object_container[RT_Object_Class_Device];
  63. for (node = information->object_list.next;
  64. node != &(information->object_list);
  65. node = node->next)
  66. {
  67. object = rt_list_entry(node, struct rt_object, list);
  68. device = (rt_device_t)object;
  69. if (device->type == RT_Device_Class_NetIf)
  70. {
  71. ethif = (struct eth_device *)device;
  72. /* leave critical */
  73. rt_exit_critical();
  74. netif_add(ethif->netif, &ipaddr, &netmask, &gw,
  75. ethif, netif_device_init, tcpip_input);
  76. if (netif_default == RT_NULL)
  77. netif_set_default(ethif->netif);
  78. #if LWIP_DHCP
  79. if (ethif->flags & NETIF_FLAG_DHCP)
  80. {
  81. /* if this interface uses DHCP, start the DHCP client */
  82. dhcp_start(ethif->netif);
  83. }
  84. else
  85. #endif
  86. {
  87. /* set interface up */
  88. netif_set_up(ethif->netif);
  89. }
  90. #ifdef LWIP_NETIF_LINK_CALLBACK
  91. netif_set_link_up(ethif->netif);
  92. #endif
  93. /* enter critical */
  94. rt_enter_critical();
  95. }
  96. }
  97. /* leave critical */
  98. rt_exit_critical();
  99. rt_sem_release((rt_sem_t)arg);
  100. }
  101. /**
  102. * LwIP system initialization
  103. */
  104. int lwip_system_init(void)
  105. {
  106. rt_err_t rc;
  107. struct rt_semaphore done_sem;
  108. /* set default netif to NULL */
  109. netif_default = RT_NULL;
  110. rc = rt_sem_init(&done_sem, "done", 0, RT_IPC_FLAG_FIFO);
  111. if (rc != RT_EOK)
  112. {
  113. LWIP_ASSERT("Failed to create semaphore", 0);
  114. return -1;
  115. }
  116. tcpip_init(tcpip_init_done_callback, (void *)&done_sem);
  117. /* waiting for initialization done */
  118. if (rt_sem_take(&done_sem, RT_WAITING_FOREVER) != RT_EOK)
  119. {
  120. rt_sem_detach(&done_sem);
  121. return -1;
  122. }
  123. rt_sem_detach(&done_sem);
  124. /* set default ip address */
  125. #if !LWIP_DHCP
  126. if (netif_default != RT_NULL)
  127. {
  128. struct ip_addr ipaddr, netmask, gw;
  129. IP4_ADDR(&ipaddr, RT_LWIP_IPADDR0, RT_LWIP_IPADDR1, RT_LWIP_IPADDR2, RT_LWIP_IPADDR3);
  130. IP4_ADDR(&gw, RT_LWIP_GWADDR0, RT_LWIP_GWADDR1, RT_LWIP_GWADDR2, RT_LWIP_GWADDR3);
  131. IP4_ADDR(&netmask, RT_LWIP_MSKADDR0, RT_LWIP_MSKADDR1, RT_LWIP_MSKADDR2, RT_LWIP_MSKADDR3);
  132. netifapi_netif_set_addr(netif_default, &ipaddr, &netmask, &gw);
  133. }
  134. #endif
  135. return 0;
  136. }
  137. INIT_COMPONENT_EXPORT(lwip_system_init);
  138. void sys_init(void)
  139. {
  140. /* nothing on RT-Thread porting */
  141. }
  142. void lwip_sys_init(void)
  143. {
  144. lwip_system_init();
  145. }
  146. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  147. {
  148. static unsigned short counter = 0;
  149. char tname[RT_NAME_MAX];
  150. sys_sem_t tmpsem;
  151. RT_DEBUG_NOT_IN_INTERRUPT;
  152. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_SEM_NAME, counter);
  153. counter ++;
  154. tmpsem = rt_sem_create(tname, count, RT_IPC_FLAG_FIFO);
  155. if (tmpsem == RT_NULL)
  156. return ERR_MEM;
  157. else
  158. {
  159. *sem = tmpsem;
  160. return ERR_OK;
  161. }
  162. }
  163. void sys_sem_free(sys_sem_t *sem)
  164. {
  165. RT_DEBUG_NOT_IN_INTERRUPT;
  166. rt_sem_delete(*sem);
  167. }
  168. void sys_sem_signal(sys_sem_t *sem)
  169. {
  170. rt_sem_release(*sem);
  171. }
  172. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  173. {
  174. rt_err_t ret;
  175. s32_t t;
  176. u32_t tick;
  177. RT_DEBUG_NOT_IN_INTERRUPT;
  178. /* get the begin tick */
  179. tick = rt_tick_get();
  180. if (timeout == 0)
  181. t = RT_WAITING_FOREVER;
  182. else
  183. {
  184. /* convert msecond to os tick */
  185. if (timeout < (1000/RT_TICK_PER_SECOND))
  186. t = 1;
  187. else
  188. t = timeout / (1000/RT_TICK_PER_SECOND);
  189. }
  190. ret = rt_sem_take(*sem, t);
  191. if (ret == -RT_ETIMEOUT)
  192. return SYS_ARCH_TIMEOUT;
  193. else
  194. {
  195. if (ret == RT_EOK)
  196. ret = 1;
  197. }
  198. /* get elapse msecond */
  199. tick = rt_tick_get() - tick;
  200. /* convert tick to msecond */
  201. tick = tick * (1000 / RT_TICK_PER_SECOND);
  202. if (tick == 0)
  203. tick = 1;
  204. return tick;
  205. }
  206. #ifndef sys_sem_valid
  207. /** Check if a semaphore is valid/allocated:
  208. * return 1 for valid, 0 for invalid
  209. */
  210. int sys_sem_valid(sys_sem_t *sem)
  211. {
  212. return (int)(*sem);
  213. }
  214. #endif
  215. #ifndef sys_sem_set_invalid
  216. /** Set a semaphore invalid so that sys_sem_valid returns 0
  217. */
  218. void sys_sem_set_invalid(sys_sem_t *sem)
  219. {
  220. *sem = RT_NULL;
  221. }
  222. #endif
  223. /* ====================== Mutex ====================== */
  224. /** Create a new mutex
  225. * @param mutex pointer to the mutex to create
  226. * @return a new mutex
  227. */
  228. err_t sys_mutex_new(sys_mutex_t *mutex)
  229. {
  230. static unsigned short counter = 0;
  231. char tname[RT_NAME_MAX];
  232. sys_mutex_t tmpmutex;
  233. RT_DEBUG_NOT_IN_INTERRUPT;
  234. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MUTEX_NAME, counter);
  235. counter ++;
  236. tmpmutex = rt_mutex_create(tname, RT_IPC_FLAG_FIFO);
  237. if (tmpmutex == RT_NULL)
  238. return ERR_MEM;
  239. else
  240. {
  241. *mutex = tmpmutex;
  242. return ERR_OK;
  243. }
  244. }
  245. /** Lock a mutex
  246. * @param mutex the mutex to lock
  247. */
  248. void sys_mutex_lock(sys_mutex_t *mutex)
  249. {
  250. RT_DEBUG_NOT_IN_INTERRUPT;
  251. rt_mutex_take(*mutex, RT_WAITING_FOREVER);
  252. return;
  253. }
  254. /** Unlock a mutex
  255. * @param mutex the mutex to unlock
  256. */
  257. void sys_mutex_unlock(sys_mutex_t *mutex)
  258. {
  259. rt_mutex_release(*mutex);
  260. }
  261. /** Delete a semaphore
  262. * @param mutex the mutex to delete
  263. */
  264. void sys_mutex_free(sys_mutex_t *mutex)
  265. {
  266. RT_DEBUG_NOT_IN_INTERRUPT;
  267. rt_mutex_delete(*mutex);
  268. }
  269. #ifndef sys_mutex_valid
  270. /** Check if a mutex is valid/allocated:
  271. * return 1 for valid, 0 for invalid
  272. */
  273. int sys_mutex_valid(sys_mutex_t *mutex)
  274. {
  275. return (int)(*mutex);
  276. }
  277. #endif
  278. #ifndef sys_mutex_set_invalid
  279. /** Set a mutex invalid so that sys_mutex_valid returns 0
  280. */
  281. void sys_mutex_set_invalid(sys_mutex_t *mutex)
  282. {
  283. *mutex = RT_NULL;
  284. }
  285. #endif
  286. /* ====================== Mailbox ====================== */
  287. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  288. {
  289. static unsigned short counter = 0;
  290. char tname[RT_NAME_MAX];
  291. sys_mbox_t tmpmbox;
  292. RT_DEBUG_NOT_IN_INTERRUPT;
  293. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MBOX_NAME, counter);
  294. counter ++;
  295. tmpmbox = rt_mb_create(tname, size, RT_IPC_FLAG_FIFO);
  296. if (tmpmbox != RT_NULL)
  297. {
  298. *mbox = tmpmbox;
  299. return ERR_OK;
  300. }
  301. return ERR_MEM;
  302. }
  303. void sys_mbox_free(sys_mbox_t *mbox)
  304. {
  305. RT_DEBUG_NOT_IN_INTERRUPT;
  306. rt_mb_delete(*mbox);
  307. return;
  308. }
  309. /** Post a message to an mbox - may not fail
  310. * -> blocks if full, only used from tasks not from ISR
  311. * @param mbox mbox to posts the message
  312. * @param msg message to post (ATTENTION: can be NULL)
  313. */
  314. void sys_mbox_post(sys_mbox_t *mbox, void *msg)
  315. {
  316. RT_DEBUG_NOT_IN_INTERRUPT;
  317. rt_mb_send_wait(*mbox, (rt_uint32_t)msg, RT_WAITING_FOREVER);
  318. return;
  319. }
  320. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  321. {
  322. if (rt_mb_send(*mbox, (rt_uint32_t)msg) == RT_EOK)
  323. return ERR_OK;
  324. return ERR_MEM;
  325. }
  326. /** Wait for a new message to arrive in the mbox
  327. * @param mbox mbox to get a message from
  328. * @param msg pointer where the message is stored
  329. * @param timeout maximum time (in milliseconds) to wait for a message
  330. * @return time (in milliseconds) waited for a message, may be 0 if not waited
  331. or SYS_ARCH_TIMEOUT on timeout
  332. * The returned time has to be accurate to prevent timer jitter!
  333. */
  334. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  335. {
  336. rt_err_t ret;
  337. s32_t t;
  338. u32_t tick;
  339. RT_DEBUG_NOT_IN_INTERRUPT;
  340. /* get the begin tick */
  341. tick = rt_tick_get();
  342. if(timeout == 0)
  343. t = RT_WAITING_FOREVER;
  344. else
  345. {
  346. /* convirt msecond to os tick */
  347. if (timeout < (1000/RT_TICK_PER_SECOND))
  348. t = 1;
  349. else
  350. t = timeout / (1000/RT_TICK_PER_SECOND);
  351. }
  352. ret = rt_mb_recv(*mbox, (rt_uint32_t *)msg, t);
  353. if(ret == -RT_ETIMEOUT)
  354. return SYS_ARCH_TIMEOUT;
  355. else
  356. {
  357. LWIP_ASSERT("rt_mb_recv returned with error!", ret == RT_EOK);
  358. }
  359. /* get elapse msecond */
  360. tick = rt_tick_get() - tick;
  361. /* convert tick to msecond */
  362. tick = tick * (1000 / RT_TICK_PER_SECOND);
  363. if (tick == 0)
  364. tick = 1;
  365. return tick;
  366. }
  367. /** Wait for a new message to arrive in the mbox
  368. * @param mbox mbox to get a message from
  369. * @param msg pointer where the message is stored
  370. * @param timeout maximum time (in milliseconds) to wait for a message
  371. * @return 0 (milliseconds) if a message has been received
  372. * or SYS_MBOX_EMPTY if the mailbox is empty
  373. */
  374. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  375. {
  376. int ret;
  377. ret = rt_mb_recv(*mbox, (rt_uint32_t *)msg, 0);
  378. if(ret == -RT_ETIMEOUT)
  379. return SYS_ARCH_TIMEOUT;
  380. else
  381. {
  382. if (ret == RT_EOK)
  383. ret = 1;
  384. }
  385. return ret;
  386. }
  387. #ifndef sys_mbox_valid
  388. /** Check if an mbox is valid/allocated:
  389. * return 1 for valid, 0 for invalid
  390. */
  391. int sys_mbox_valid(sys_mbox_t *mbox)
  392. {
  393. return (int)(*mbox);
  394. }
  395. #endif
  396. #ifndef sys_mbox_set_invalid
  397. /** Set an mbox invalid so that sys_mbox_valid returns 0
  398. */
  399. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  400. {
  401. *mbox = RT_NULL;
  402. }
  403. #endif
  404. /* ====================== System ====================== */
  405. sys_thread_t sys_thread_new(const char *name,
  406. lwip_thread_fn thread,
  407. void *arg,
  408. int stacksize,
  409. int prio)
  410. {
  411. rt_thread_t t;
  412. RT_DEBUG_NOT_IN_INTERRUPT;
  413. /* create thread */
  414. t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
  415. RT_ASSERT(t != RT_NULL);
  416. /* startup thread */
  417. rt_thread_startup(t);
  418. return t;
  419. }
  420. sys_prot_t sys_arch_protect(void)
  421. {
  422. rt_base_t level;
  423. /* disable interrupt */
  424. level = rt_hw_interrupt_disable();
  425. return level;
  426. }
  427. void sys_arch_unprotect(sys_prot_t pval)
  428. {
  429. /* enable interrupt */
  430. rt_hw_interrupt_enable(pval);
  431. return;
  432. }
  433. void sys_arch_assert(const char *file, int line)
  434. {
  435. rt_kprintf("\nAssertion: %d in %s, thread %s\n",
  436. line, file, rt_thread_self()->name);
  437. RT_ASSERT(0);
  438. }
  439. u32_t sys_jiffies(void)
  440. {
  441. return rt_tick_get();
  442. }
  443. #ifdef RT_LWIP_PPP
  444. u32_t sio_read(sio_fd_t fd, u8_t *buf, u32_t size)
  445. {
  446. u32_t len;
  447. RT_ASSERT(fd != RT_NULL);
  448. len = rt_device_read((rt_device_t)fd, 0, buf, size);
  449. if (len <= 0)
  450. return 0;
  451. return len;
  452. }
  453. u32_t sio_write(sio_fd_t fd, u8_t *buf, u32_t size)
  454. {
  455. RT_ASSERT(fd != RT_NULL);
  456. return rt_device_write((rt_device_t)fd, 0, buf, size);
  457. }
  458. void sio_read_abort(sio_fd_t fd)
  459. {
  460. rt_kprintf("read_abort\n");
  461. }
  462. void ppp_trace(int level, const char *format, ...)
  463. {
  464. va_list args;
  465. rt_size_t length;
  466. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  467. va_start(args, format);
  468. length = rt_vsprintf(rt_log_buf, format, args);
  469. rt_device_write((rt_device_t)rt_console_get_device(), 0, rt_log_buf, length);
  470. va_end(args);
  471. }
  472. #endif
  473. /*
  474. * export bsd socket symbol for RT-Thread Application Module
  475. */
  476. #if LWIP_SOCKET
  477. #include <lwip/sockets.h>
  478. RTM_EXPORT(lwip_accept);
  479. RTM_EXPORT(lwip_bind);
  480. RTM_EXPORT(lwip_shutdown);
  481. RTM_EXPORT(lwip_getpeername);
  482. RTM_EXPORT(lwip_getsockname);
  483. RTM_EXPORT(lwip_getsockopt);
  484. RTM_EXPORT(lwip_setsockopt);
  485. RTM_EXPORT(lwip_close);
  486. RTM_EXPORT(lwip_connect);
  487. RTM_EXPORT(lwip_listen);
  488. RTM_EXPORT(lwip_recv);
  489. RTM_EXPORT(lwip_read);
  490. RTM_EXPORT(lwip_recvfrom);
  491. RTM_EXPORT(lwip_send);
  492. RTM_EXPORT(lwip_sendto);
  493. RTM_EXPORT(lwip_socket);
  494. RTM_EXPORT(lwip_write);
  495. RTM_EXPORT(lwip_select);
  496. RTM_EXPORT(lwip_ioctl);
  497. RTM_EXPORT(lwip_fcntl);
  498. #if LWIP_DNS
  499. #include <lwip/netdb.h>
  500. RTM_EXPORT(lwip_gethostbyname);
  501. RTM_EXPORT(lwip_gethostbyname_r);
  502. RTM_EXPORT(lwip_freeaddrinfo);
  503. RTM_EXPORT(lwip_getaddrinfo);
  504. #endif
  505. #endif
  506. #if LWIP_DHCP
  507. #include <lwip/dhcp.h>
  508. RTM_EXPORT(dhcp_start);
  509. RTM_EXPORT(dhcp_renew);
  510. RTM_EXPORT(dhcp_stop);
  511. #endif
  512. #if LWIP_NETIF_API
  513. #include <lwip/netifapi.h>
  514. RTM_EXPORT(netifapi_netif_set_addr);
  515. #endif