sys_arch.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <[email protected]>
  30. *
  31. */
  32. /* Porting by Michael Vysotsky <[email protected]> August 2011 */
  33. #define SYS_ARCH_GLOBALS
  34. /* lwIP includes. */
  35. #include "sys_arch.h"
  36. #include "lwip/debug.h"
  37. #include "lwip/def.h"
  38. #include "lwip/mem.h"
  39. #include "lwip/sys.h"
  40. #include "string.h"
  41. /*----------------------------------------------------------------------------*/
  42. /* VARIABLES */
  43. /*----------------------------------------------------------------------------*/
  44. static OS_MEM *pQueueMem, *pStackMem;
  45. const void *const pvNullPointer = (mem_ptr_t *)0xffffffff;
  46. #if defined(__GNUC__) /*!< GCC compiler */
  47. __attribute__((aligned(4)))
  48. INT8U pcQueueMemoryPool[MAX_QUEUES * sizeof(TQ_DESCR)];
  49. __attribute__((aligned(4))) OS_STK LwIP_Task_Stk[LWIP_TASK_MAX * LWIP_STK_SIZE];
  50. #elif defined(__CC_ARM) /*!< ARM compiler */
  51. __align(4) INT8U pcQueueMemoryPool[MAX_QUEUES * sizeof(TQ_DESCR)];
  52. __align(4) OS_STK LwIP_Task_Stk[LWIP_TASK_MAX * LWIP_STK_SIZE];
  53. #elif defined(__ICCARM__) /*!< IAR compiler */
  54. #pragma data_alignment = 4
  55. INT8U pcQueueMemoryPool[MAX_QUEUES * sizeof(TQ_DESCR)];
  56. #pragma data_alignment = 4
  57. OS_STK LwIP_Task_Stk[LWIP_TASK_MAX * LWIP_STK_SIZE];
  58. #endif /* __CC_ARM */
  59. INT8U LwIP_task_priopity_stask[LWIP_TASK_MAX];
  60. #define OS_MS_PER_TICK (1000 / OS_TICKS_PER_SEC)
  61. u32_t sys_now(void)
  62. {
  63. return (OSTimeGet() * OS_MS_PER_TICK);
  64. }
  65. /*----------------------------------------------------------------------------*/
  66. /* PROTOTYPES */
  67. /*----------------------------------------------------------------------------*/
  68. /*--------------------Creates an empty mailbox.-------------------------------*/
  69. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  70. {
  71. /* prarmeter "size" can be ignored in your implementation. */
  72. u8_t ucErr;
  73. PQ_DESCR pQDesc;
  74. pQDesc = OSMemGet(pQueueMem, &ucErr);
  75. LWIP_ASSERT("OSMemGet ", ucErr == OS_ERR_NONE);
  76. if (ucErr == OS_ERR_NONE)
  77. {
  78. if (size > MAX_QUEUE_ENTRIES)
  79. {
  80. size = MAX_QUEUE_ENTRIES;
  81. }
  82. pQDesc->pQ = OSQCreate(&(pQDesc->pvQEntries[0]), size);
  83. LWIP_ASSERT("OSQCreate ", pQDesc->pQ != NULL);
  84. if (pQDesc->pQ != NULL)
  85. {
  86. *mbox = pQDesc;
  87. return 0;
  88. }
  89. else
  90. {
  91. ucErr = OSMemPut(pQueueMem, pQDesc);
  92. *mbox = NULL;
  93. return ucErr;
  94. }
  95. }
  96. else
  97. {
  98. return -1;
  99. }
  100. }
  101. /*-----------------------------------------------------------------------------------*/
  102. /*
  103. Deallocates a mailbox. If there are messages still present in the
  104. mailbox when the mailbox is deallocated, it is an indication of a
  105. programming error in lwIP and the developer should be notified.
  106. */
  107. void sys_mbox_free(sys_mbox_t *mbox)
  108. {
  109. u8_t ucErr;
  110. sys_mbox_t m_box = *mbox;
  111. LWIP_ASSERT("sys_mbox_free ", m_box != SYS_MBOX_NULL);
  112. OSQFlush(m_box->pQ);
  113. (void)OSQDel(m_box->pQ, OS_DEL_NO_PEND, &ucErr);
  114. LWIP_ASSERT("OSQDel ", ucErr == OS_ERR_NONE);
  115. ucErr = OSMemPut(pQueueMem, m_box);
  116. LWIP_ASSERT("OSMemPut ", ucErr == OS_ERR_NONE);
  117. *mbox = NULL;
  118. }
  119. /*-----------------------------------------------------------------------------------*/
  120. // Posts the "msg" to the mailbox.
  121. void sys_mbox_post(sys_mbox_t *mbox, void *msg)
  122. {
  123. u8_t i = 0;
  124. sys_mbox_t m_box = *mbox;
  125. if (msg == NULL)
  126. msg = (void *)&pvNullPointer;
  127. /* try 10 times */
  128. while ((i < 10) && ((OSQPost(m_box->pQ, msg)) != OS_ERR_NONE))
  129. {
  130. i++;
  131. OSTimeDly(5);
  132. }
  133. LWIP_ASSERT("sys_mbox_post error!\n", i != 10);
  134. }
  135. /* Try to post the "msg" to the mailbox. */
  136. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  137. {
  138. sys_mbox_t m_box = *mbox;
  139. if (msg == NULL)
  140. msg = (void *)&pvNullPointer;
  141. if ((OSQPost(m_box->pQ, msg)) != OS_ERR_NONE)
  142. {
  143. return ERR_MEM;
  144. }
  145. return ERR_OK;
  146. }
  147. /*-----------------------------------------------------------------------------------*/
  148. /*
  149. Blocks the thread until a message arrives in the mailbox, but does
  150. not block the thread longer than "timeout" milliseconds (similar to
  151. the sys_arch_sem_wait() function). The "msg" argument is a result
  152. parameter that is set by the function (i.e., by doing "*msg =
  153. ptr"). The "msg" parameter maybe NULL to indicate that the message
  154. should be dropped.
  155. The return values are the same as for the sys_arch_sem_wait() function:
  156. Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
  157. timeout.
  158. Note that a function with a similar name, sys_mbox_fetch(), is
  159. implemented by lwIP.
  160. */
  161. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  162. {
  163. u8_t ucErr;
  164. u32_t ucos_timeout, timeout_new;
  165. void *temp;
  166. sys_mbox_t m_box = *mbox;
  167. /* convert to timetick */
  168. if (timeout != 0)
  169. {
  170. ucos_timeout = (timeout * OS_TICKS_PER_SEC) / 1000;
  171. if (ucos_timeout < 1)
  172. {
  173. ucos_timeout = 1;
  174. }
  175. else if (ucos_timeout > 65535) /* ucOS only support u16_t timeout */
  176. {
  177. ucos_timeout = 65535;
  178. }
  179. }
  180. else
  181. ucos_timeout = 0;
  182. timeout = OSTimeGet();
  183. temp = OSQPend(m_box->pQ, (u16_t)ucos_timeout, &ucErr);
  184. if (msg != NULL)
  185. {
  186. if (temp == (void *)&pvNullPointer)
  187. *msg = NULL;
  188. else
  189. *msg = temp;
  190. }
  191. if (ucErr == OS_ERR_TIMEOUT)
  192. timeout = SYS_ARCH_TIMEOUT;
  193. else
  194. {
  195. LWIP_ASSERT("OSQPend ", ucErr == OS_ERR_NONE);
  196. timeout_new = OSTimeGet();
  197. if (timeout_new > timeout)
  198. timeout_new = timeout_new - timeout;
  199. else
  200. timeout_new = 0xffffffff - timeout + timeout_new;
  201. /* convert to millisecond */
  202. timeout = timeout_new * 1000 / OS_TICKS_PER_SEC + 1;
  203. }
  204. return timeout;
  205. }
  206. /**
  207. * Check if an mbox is valid/allocated:
  208. * @param sys_mbox_t *mbox pointer mail box
  209. * @return 1 for valid, 0 for invalid
  210. */
  211. int sys_mbox_valid(sys_mbox_t *mbox)
  212. {
  213. sys_mbox_t m_box = *mbox;
  214. u8_t ucErr;
  215. int ret;
  216. OS_Q_DATA q_data;
  217. memset(&q_data, 0, sizeof(OS_Q_DATA));
  218. ucErr = OSQQuery(m_box->pQ, &q_data);
  219. ret = (ucErr < 2 && (q_data.OSNMsgs < q_data.OSQSize)) ? 1 : 0;
  220. return ret;
  221. }
  222. /**
  223. * Set an mbox invalid so that sys_mbox_valid returns 0
  224. */
  225. void sys_mbox_set_invalid(sys_mbox_t *mbox) {}
  226. /*
  227. * Creates and returns a new semaphore. The "count" argument specifies
  228. * the initial state of the semaphore. TBD finish and test
  229. */
  230. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  231. {
  232. u8_t err;
  233. *sem = OSSemCreate((u16_t)count);
  234. if (*sem == NULL)
  235. {
  236. return -1;
  237. }
  238. OSEventNameSet(*sem, "LWIP Sem", &err);
  239. LWIP_ASSERT("OSSemCreate ", *sem != NULL);
  240. return 0;
  241. }
  242. /*
  243. Blocks the thread while waiting for the semaphore to be
  244. signaled. If the "timeout" argument is non-zero, the thread should
  245. only be blocked for the specified time (measured in
  246. milliseconds).
  247. If the timeout argument is non-zero, the return value is the number of
  248. milliseconds spent waiting for the semaphore to be signaled. If the
  249. semaphore wasn't signaled within the specified time, the return value is
  250. SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
  251. (i.e., it was already signaled), the function may return zero.
  252. Notice that lwIP implements a function with a similar name,
  253. sys_sem_wait(), that uses the sys_arch_sem_wait() function.
  254. */
  255. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  256. {
  257. u8_t ucErr;
  258. u32_t ucos_timeout, timeout_new;
  259. if (timeout != 0)
  260. {
  261. ucos_timeout =
  262. (timeout * OS_TICKS_PER_SEC) / 1000; // convert to timetick
  263. if (ucos_timeout < 1)
  264. {
  265. ucos_timeout = 1;
  266. }
  267. else if (ucos_timeout > 65536) /* uC/OS only support u16_t pend */
  268. {
  269. ucos_timeout = 65535;
  270. }
  271. }
  272. else
  273. ucos_timeout = 0;
  274. timeout = OSTimeGet();
  275. OSSemPend(*sem, (u16_t)ucos_timeout, (u8_t *)&ucErr);
  276. /* only when timeout! */
  277. if (ucErr == OS_ERR_TIMEOUT)
  278. timeout = SYS_ARCH_TIMEOUT;
  279. else
  280. {
  281. /* for pbuf_free, may be called from an ISR */
  282. timeout_new = OSTimeGet();
  283. if (timeout_new >= timeout)
  284. timeout_new = timeout_new - timeout;
  285. else
  286. timeout_new = 0xffffffff - timeout + timeout_new;
  287. /* convert to milisecond */
  288. timeout = (timeout_new * 1000 / OS_TICKS_PER_SEC + 1);
  289. }
  290. return timeout;
  291. }
  292. /*
  293. * Signals a semaphore
  294. */
  295. void sys_sem_signal(sys_sem_t *sem) { OSSemPost(*sem); }
  296. /*
  297. * Deallocates a semaphore
  298. */
  299. void sys_sem_free(sys_sem_t *sem)
  300. {
  301. u8_t ucErr;
  302. (void)OSSemDel(*sem, OS_DEL_ALWAYS, &ucErr);
  303. LWIP_ASSERT("OSSemDel ", ucErr == OS_ERR_NONE);
  304. *sem = NULL;
  305. }
  306. int sys_sem_valid(sys_sem_t *sem)
  307. {
  308. OS_SEM_DATA sem_data;
  309. return (OSSemQuery(*sem, &sem_data) == OS_ERR_NONE) ? 1 : 0;
  310. }
  311. /** Set a semaphore invalid so that sys_sem_valid returns 0 */
  312. void sys_sem_set_invalid(sys_sem_t *sem) {}
  313. /*
  314. * Initialize sys arch
  315. */
  316. void sys_init(void)
  317. {
  318. u8_t ucErr;
  319. memset(LwIP_task_priopity_stask, 0, sizeof(LwIP_task_priopity_stask));
  320. /* init mem used by sys_mbox_t, use ucosII functions */
  321. pQueueMem = OSMemCreate((void *)pcQueueMemoryPool, MAX_QUEUES,
  322. sizeof(TQ_DESCR), &ucErr);
  323. OSMemNameSet(pQueueMem, "LWIP mem", &ucErr);
  324. LWIP_ASSERT("sys_init: failed OSMemCreate Q", ucErr == OS_ERR_NONE);
  325. pStackMem = OSMemCreate((void *)LwIP_Task_Stk, LWIP_TASK_MAX,
  326. LWIP_STK_SIZE * sizeof(OS_STK), &ucErr);
  327. OSMemNameSet(pQueueMem, "LWIP TASK STK", &ucErr);
  328. LWIP_ASSERT("sys_init: failed OSMemCreate STK", ucErr == OS_ERR_NONE);
  329. }
  330. /*-----------------------------------------------------------------------------------*/
  331. /*-----------------------------------------------------------------------------------*/
  332. // TBD
  333. /*-----------------------------------------------------------------------------------*/
  334. /*
  335. Starts a new thread with priority "prio" that will begin its execution in the
  336. function "thread()". The "arg" argument will be passed as an argument to the
  337. thread() function. The id of the new thread is returned. Both the id and
  338. the priority are system dependent.
  339. */
  340. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg,
  341. int stacksize, int prio)
  342. {
  343. u8_t ubPrio = LWIP_TASK_START_PRIO;
  344. u8_t ucErr;
  345. int i;
  346. OS_STK *task_stk;
  347. INT16U task_id;
  348. arg = arg;
  349. if (prio)
  350. {
  351. ubPrio += (prio - 1);
  352. for (i = 0; i < LWIP_TASK_MAX; ++i)
  353. {
  354. if (LwIP_task_priopity_stask[i] == ubPrio)
  355. break;
  356. }
  357. if (i == LWIP_TASK_MAX)
  358. {
  359. for (i = 0; i < LWIP_TASK_MAX; ++i)
  360. {
  361. if (LwIP_task_priopity_stask[i] == 0)
  362. {
  363. LwIP_task_priopity_stask[i] = ubPrio;
  364. break;
  365. }
  366. }
  367. if (i == LWIP_TASK_MAX)
  368. {
  369. LWIP_ASSERT("sys_thread_new: there is no space for priority",
  370. 0);
  371. return (-1);
  372. }
  373. }
  374. else
  375. {
  376. prio = 0;
  377. }
  378. }
  379. else
  380. {
  381. LWIP_ASSERT("priority is invalid \r\n", 0);
  382. return (-1);
  383. }
  384. /* Search for a suitable priority */
  385. if (!prio)
  386. {
  387. ubPrio = LWIP_TASK_START_PRIO;
  388. while (ubPrio < (LWIP_TASK_START_PRIO + LWIP_TASK_MAX))
  389. {
  390. for (i = 0; i < LWIP_TASK_MAX; ++i)
  391. if (LwIP_task_priopity_stask[i] == ubPrio)
  392. {
  393. ++ubPrio;
  394. break;
  395. }
  396. if (i == LWIP_TASK_MAX)
  397. break;
  398. }
  399. if (ubPrio < (LWIP_TASK_START_PRIO + LWIP_TASK_MAX))
  400. for (i = 0; i < LWIP_TASK_MAX; ++i)
  401. if (LwIP_task_priopity_stask[i] == 0)
  402. {
  403. LwIP_task_priopity_stask[i] = ubPrio;
  404. break;
  405. }
  406. if (ubPrio >= (LWIP_TASK_START_PRIO + LWIP_TASK_MAX) ||
  407. i == LWIP_TASK_MAX)
  408. {
  409. LWIP_ASSERT("sys_thread_new: there is no free priority", 0);
  410. return (-1);
  411. }
  412. }
  413. if (stacksize > LWIP_STK_SIZE || !stacksize)
  414. stacksize = LWIP_STK_SIZE;
  415. /* get Stack from pool */
  416. task_stk = OSMemGet(pStackMem, &ucErr);
  417. if (ucErr != OS_ERR_NONE)
  418. {
  419. LWIP_ASSERT("sys_thread_new: impossible to get a stack", 0);
  420. return (-1);
  421. }
  422. task_id = ubPrio - LWIP_TASK_START_PRIO + 4; // LWIP_TSK_ID;
  423. #if (OS_TASK_STAT_EN == 0)
  424. OSTaskCreate(thread, (void *)arg, &task_stk[stacksize - 1], ubPrio);
  425. #else
  426. OSTaskCreateExt(thread, (void *)arg, &task_stk[stacksize - 1], ubPrio,
  427. task_id, &task_stk[0], stacksize, (void *)0,
  428. OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR |
  429. OS_TASK_OPT_SAVE_FP);
  430. #endif
  431. OSTaskNameSet(ubPrio, (u8_t *)name, &ucErr);
  432. return ubPrio;
  433. }
  434. /*
  435. This optional function does a "fast" critical region protection and returns
  436. the previous protection level. This function is only called during very short
  437. critical regions. An embedded system which supports ISR-based drivers might
  438. want to implement this function by disabling interrupts. Task-based systems
  439. might want to implement this by using a mutex or disabling tasking. This
  440. function should support recursive calls from the same task or interrupt. In
  441. other words, sys_arch_protect() could be called while already protected. In
  442. that case the return value indicates that it is already protected.
  443. sys_arch_protect() is only required if your port is supporting an operating
  444. system.
  445. */
  446. sys_prot_t sys_arch_protect(void)
  447. {
  448. sys_prot_t cpu_sr;
  449. cpu_sr = CPU_SR_Save();
  450. return cpu_sr;
  451. }
  452. /*
  453. This optional function does a "fast" set of critical region protection to the
  454. value specified by pval. See the documentation for sys_arch_protect() for
  455. more information. This function is only required if your port is supporting
  456. an operating system.
  457. */
  458. void sys_arch_unprotect(sys_prot_t pval) { CPU_SR_Restore(pval); }