sys_arch.c 15 KB

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