sys_arch.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 modification,
  6. * 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 EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. /* lwIP includes. */
  33. #include "lwip/debug.h"
  34. #include "lwip/def.h"
  35. #include "lwip/sys.h"
  36. #include "lwip/mem.h"
  37. #include "lwip/stats.h"
  38. #include "FreeRTOS.h"
  39. #include "task.h"
  40. xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION;
  41. /* This is the number of threads that can be started with sys_thread_new() */
  42. #define SYS_THREAD_MAX 6
  43. static u16_t s_nextthread = 0;
  44. /*-----------------------------------------------------------------------------------*/
  45. // Creates an empty mailbox.
  46. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  47. {
  48. //(void ) size;
  49. //*mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );
  50. *mbox = xQueueCreate( size, sizeof( void * ) );
  51. #if SYS_STATS
  52. ++lwip_stats.sys.mbox.used;
  53. if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) {
  54. lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
  55. }
  56. #endif /* SYS_STATS */
  57. if (*mbox == NULL)
  58. return ERR_MEM;
  59. return ERR_OK;
  60. }
  61. /*-----------------------------------------------------------------------------------*/
  62. /*
  63. Deallocates a mailbox. If there are messages still present in the
  64. mailbox when the mailbox is deallocated, it is an indication of a
  65. programming error in lwIP and the developer should be notified.
  66. */
  67. void sys_mbox_free(sys_mbox_t *mbox)
  68. {
  69. if( uxQueueMessagesWaiting( *mbox ) )
  70. {
  71. /* Line for breakpoint. Should never break here! */
  72. portNOP();
  73. #if SYS_STATS
  74. lwip_stats.sys.mbox.err++;
  75. #endif /* SYS_STATS */
  76. // TODO notify the user of failure.
  77. }
  78. vQueueDelete( *mbox );
  79. #if SYS_STATS
  80. --lwip_stats.sys.mbox.used;
  81. #endif /* SYS_STATS */
  82. }
  83. /*-----------------------------------------------------------------------------------*/
  84. // Posts the "msg" to the mailbox.
  85. void sys_mbox_post(sys_mbox_t *mbox, void *data)
  86. {
  87. while ( xQueueSendToBack(*mbox, &data, portMAX_DELAY ) != pdTRUE ){}
  88. }
  89. /*-----------------------------------------------------------------------------------*/
  90. // Try to post the "msg" to the mailbox.
  91. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  92. {
  93. err_t result;
  94. if ( xQueueSend( *mbox, &msg, 0 ) == pdPASS )
  95. {
  96. result = ERR_OK;
  97. }
  98. else {
  99. // could not post, queue must be full
  100. result = ERR_MEM;
  101. #if SYS_STATS
  102. lwip_stats.sys.mbox.err++;
  103. #endif /* SYS_STATS */
  104. }
  105. return result;
  106. }
  107. /*-----------------------------------------------------------------------------------*/
  108. /*
  109. Blocks the thread until a message arrives in the mailbox, but does
  110. not block the thread longer than "timeout" milliseconds (similar to
  111. the sys_arch_sem_wait() function). The "msg" argument is a result
  112. parameter that is set by the function (i.e., by doing "*msg =
  113. ptr"). The "msg" parameter maybe NULL to indicate that the message
  114. should be dropped.
  115. The return values are the same as for the sys_arch_sem_wait() function:
  116. Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
  117. timeout.
  118. Note that a function with a similar name, sys_mbox_fetch(), is
  119. implemented by lwIP.
  120. */
  121. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  122. {
  123. void *dummyptr;
  124. portTickType StartTime, EndTime, Elapsed;
  125. StartTime = xTaskGetTickCount();
  126. if ( msg == NULL )
  127. {
  128. msg = &dummyptr;
  129. }
  130. if ( timeout != 0 )
  131. {
  132. if ( pdTRUE == xQueueReceive( *mbox, &(*msg), timeout / portTICK_RATE_MS ) )
  133. {
  134. EndTime = xTaskGetTickCount();
  135. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  136. return ( Elapsed );
  137. }
  138. else // timed out blocking for message
  139. {
  140. *msg = NULL;
  141. return SYS_ARCH_TIMEOUT;
  142. }
  143. }
  144. else // block forever for a message.
  145. {
  146. while( pdTRUE != xQueueReceive( *mbox, &(*msg), portMAX_DELAY ) ){} // time is arbitrary
  147. EndTime = xTaskGetTickCount();
  148. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  149. return ( Elapsed ); // return time blocked TODO test
  150. }
  151. }
  152. /*-----------------------------------------------------------------------------------*/
  153. /*
  154. Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll
  155. return with SYS_MBOX_EMPTY. On success, 0 is returned.
  156. */
  157. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  158. {
  159. void *dummyptr;
  160. if ( msg == NULL )
  161. {
  162. msg = &dummyptr;
  163. }
  164. if ( pdTRUE == xQueueReceive( *mbox, &(*msg), 0 ) )
  165. {
  166. return ERR_OK;
  167. }
  168. else
  169. {
  170. return SYS_MBOX_EMPTY;
  171. }
  172. }
  173. /*----------------------------------------------------------------------------------*/
  174. int sys_mbox_valid(sys_mbox_t *mbox)
  175. {
  176. if (*mbox == SYS_MBOX_NULL)
  177. return 0;
  178. else
  179. return 1;
  180. }
  181. /*-----------------------------------------------------------------------------------*/
  182. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  183. {
  184. *mbox = SYS_MBOX_NULL;
  185. }
  186. /*-----------------------------------------------------------------------------------*/
  187. // Creates a new semaphore. The "count" argument specifies
  188. // the initial state of the semaphore.
  189. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  190. {
  191. vSemaphoreCreateBinary(*sem );
  192. if(*sem == NULL)
  193. {
  194. #if SYS_STATS
  195. ++lwip_stats.sys.sem.err;
  196. #endif /* SYS_STATS */
  197. return ERR_MEM;
  198. }
  199. if(count == 0) // Means it can't be taken
  200. {
  201. xSemaphoreTake(*sem,1);
  202. }
  203. #if SYS_STATS
  204. ++lwip_stats.sys.sem.used;
  205. if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) {
  206. lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
  207. }
  208. #endif /* SYS_STATS */
  209. return ERR_OK;
  210. }
  211. /*-----------------------------------------------------------------------------------*/
  212. /*
  213. Blocks the thread while waiting for the semaphore to be
  214. signaled. If the "timeout" argument is non-zero, the thread should
  215. only be blocked for the specified time (measured in
  216. milliseconds).
  217. If the timeout argument is non-zero, the return value is the number of
  218. milliseconds spent waiting for the semaphore to be signaled. If the
  219. semaphore wasn't signaled within the specified time, the return value is
  220. SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
  221. (i.e., it was already signaled), the function may return zero.
  222. Notice that lwIP implements a function with a similar name,
  223. sys_sem_wait(), that uses the sys_arch_sem_wait() function.
  224. */
  225. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  226. {
  227. portTickType StartTime, EndTime, Elapsed;
  228. StartTime = xTaskGetTickCount();
  229. if( timeout != 0)
  230. {
  231. if( xSemaphoreTake( *sem, timeout / portTICK_RATE_MS ) == pdTRUE )
  232. {
  233. EndTime = xTaskGetTickCount();
  234. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  235. return (Elapsed); // return time blocked TODO test
  236. }
  237. else
  238. {
  239. return SYS_ARCH_TIMEOUT;
  240. }
  241. }
  242. else // must block without a timeout
  243. {
  244. while( xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE){}
  245. EndTime = xTaskGetTickCount();
  246. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  247. return ( Elapsed ); // return time blocked
  248. }
  249. }
  250. /*-----------------------------------------------------------------------------------*/
  251. // Signals a semaphore
  252. void sys_sem_signal(sys_sem_t *sem)
  253. {
  254. xSemaphoreGive(*sem);
  255. }
  256. /*-----------------------------------------------------------------------------------*/
  257. // Deallocates a semaphore
  258. void sys_sem_free(sys_sem_t *sem)
  259. {
  260. #if SYS_STATS
  261. --lwip_stats.sys.sem.used;
  262. #endif /* SYS_STATS */
  263. vQueueDelete(*sem);
  264. }
  265. /*-----------------------------------------------------------------------------------*/
  266. int sys_sem_valid(sys_sem_t *sem)
  267. {
  268. if (*sem == SYS_SEM_NULL)
  269. return 0;
  270. else
  271. return 1;
  272. }
  273. /*-----------------------------------------------------------------------------------*/
  274. void sys_sem_set_invalid(sys_sem_t *sem)
  275. {
  276. *sem = SYS_SEM_NULL;
  277. }
  278. /*-----------------------------------------------------------------------------------*/
  279. // Initialize sys arch
  280. void sys_init(void)
  281. {
  282. // keep track of how many threads have been created
  283. s_nextthread = 0;
  284. }
  285. /*-----------------------------------------------------------------------------------*/
  286. /* Mutexes*/
  287. /*-----------------------------------------------------------------------------------*/
  288. /*-----------------------------------------------------------------------------------*/
  289. #if LWIP_COMPAT_MUTEX == 0
  290. /* Create a new mutex*/
  291. err_t sys_mutex_new(sys_mutex_t *mutex) {
  292. *mutex = xSemaphoreCreateMutex();
  293. if(*mutex == NULL)
  294. {
  295. #if SYS_STATS
  296. ++lwip_stats.sys.mutex.err;
  297. #endif /* SYS_STATS */
  298. return ERR_MEM;
  299. }
  300. #if SYS_STATS
  301. ++lwip_stats.sys.mutex.used;
  302. if (lwip_stats.sys.mutex.max < lwip_stats.sys.mutex.used) {
  303. lwip_stats.sys.mutex.max = lwip_stats.sys.mutex.used;
  304. }
  305. #endif /* SYS_STATS */
  306. return ERR_OK;
  307. }
  308. /*-----------------------------------------------------------------------------------*/
  309. /* Deallocate a mutex*/
  310. void sys_mutex_free(sys_mutex_t *mutex)
  311. {
  312. #if SYS_STATS
  313. --lwip_stats.sys.mutex.used;
  314. #endif /* SYS_STATS */
  315. vQueueDelete(*mutex);
  316. }
  317. /*-----------------------------------------------------------------------------------*/
  318. /* Lock a mutex*/
  319. void sys_mutex_lock(sys_mutex_t *mutex)
  320. {
  321. sys_arch_sem_wait(mutex, 0);
  322. }
  323. /*-----------------------------------------------------------------------------------*/
  324. /* Unlock a mutex*/
  325. void sys_mutex_unlock(sys_mutex_t *mutex)
  326. {
  327. xSemaphoreGive(*mutex);
  328. }
  329. #endif /*LWIP_COMPAT_MUTEX*/
  330. /*-----------------------------------------------------------------------------------*/
  331. // TODO
  332. /*-----------------------------------------------------------------------------------*/
  333. /*
  334. Starts a new thread with priority "prio" that will begin its execution in the
  335. function "thread()". The "arg" argument will be passed as an argument to the
  336. thread() function. The id of the new thread is returned. Both the id and
  337. the priority are system dependent.
  338. */
  339. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread , void *arg, int stacksize, int prio)
  340. {
  341. xTaskHandle CreatedTask;
  342. int result;
  343. if ( s_nextthread < SYS_THREAD_MAX )
  344. {
  345. result = xTaskCreate( thread, name, stacksize, arg, prio, &CreatedTask );
  346. // For each task created, store the task handle (pid) in the timers array.
  347. // This scheme doesn't allow for threads to be deleted
  348. //s_timeoutlist[s_nextthread++].pid = CreatedTask;
  349. if(result == pdPASS)
  350. {
  351. return CreatedTask;
  352. }
  353. else
  354. {
  355. return NULL;
  356. }
  357. }
  358. else
  359. {
  360. return NULL;
  361. }
  362. }
  363. /*
  364. This optional function does a "fast" critical region protection and returns
  365. the previous protection level. This function is only called during very short
  366. critical regions. An embedded system which supports ISR-based drivers might
  367. want to implement this function by disabling interrupts. Task-based systems
  368. might want to implement this by using a mutex or disabling tasking. This
  369. function should support recursive calls from the same task or interrupt. In
  370. other words, sys_arch_protect() could be called while already protected. In
  371. that case the return value indicates that it is already protected.
  372. sys_arch_protect() is only required if your port is supporting an operating
  373. system.
  374. */
  375. sys_prot_t sys_arch_protect(void)
  376. {
  377. vPortEnterCritical();
  378. return 1;
  379. }
  380. /*
  381. This optional function does a "fast" set of critical region protection to the
  382. value specified by pval. See the documentation for sys_arch_protect() for
  383. more information. This function is only required if your port is supporting
  384. an operating system.
  385. */
  386. void sys_arch_unprotect(sys_prot_t pval)
  387. {
  388. ( void ) pval;
  389. vPortExitCritical();
  390. }
  391. /*
  392. * Prints an assertion messages and aborts execution.
  393. */
  394. void sys_assert( const char *msg )
  395. {
  396. ( void ) msg;
  397. /*FSL:only needed for debugging
  398. printf(msg);
  399. printf("\n\r");
  400. */
  401. vPortEnterCritical( );
  402. for(;;)
  403. ;
  404. }
  405. u32_t sys_jiffies(void)
  406. {
  407. return (u32_t)(xTaskGetTickCount()*portTICK_RATE_MS);
  408. }
  409. u32_t sys_now(void)
  410. {
  411. return xTaskGetTickCount();
  412. }
  413. #if 0
  414. /*
  415. * Copyright (c) 2017 Simon Goldschmidt
  416. * All rights reserved.
  417. *
  418. * Redistribution and use in source and binary forms, with or without modification,
  419. * are permitted provided that the following conditions are met:
  420. *
  421. * 1. Redistributions of source code must retain the above copyright notice,
  422. * this list of conditions and the following disclaimer.
  423. * 2. Redistributions in binary form must reproduce the above copyright notice,
  424. * this list of conditions and the following disclaimer in the documentation
  425. * and/or other materials provided with the distribution.
  426. * 3. The name of the author may not be used to endorse or promote products
  427. * derived from this software without specific prior written permission.
  428. *
  429. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  430. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  431. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  432. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  433. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  434. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  435. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  436. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  437. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  438. * OF SUCH DAMAGE.
  439. *
  440. * This file is part of the lwIP TCP/IP stack.
  441. *
  442. * Author: Simon Goldschmidt
  443. *
  444. */
  445. #include <lwip/opt.h>
  446. #include <lwip/arch.h>
  447. #if !NO_SYS
  448. #include "sys_arch.h"
  449. #endif
  450. #include <lwip/stats.h>
  451. #include <lwip/debug.h>
  452. #include <lwip/sys.h>
  453. #include <string.h>
  454. #define SYS_THREAD_MAX 6
  455. static u16_t s_nextthread = 0;
  456. u32_t lwip_sys_now;
  457. u32_t
  458. sys_jiffies(void)
  459. {
  460. return lwip_sys_now;
  461. }
  462. u32_t
  463. sys_now(void)
  464. {
  465. return lwip_sys_now;
  466. }
  467. void
  468. sys_init(void)
  469. {
  470. }
  471. #if !NO_SYS
  472. test_sys_arch_waiting_fn the_waiting_fn;
  473. void
  474. test_sys_arch_wait_callback(test_sys_arch_waiting_fn waiting_fn)
  475. {
  476. the_waiting_fn = waiting_fn;
  477. }
  478. err_t
  479. sys_sem_new(sys_sem_t *sem, u8_t count)
  480. {
  481. // LWIP_ASSERT("sem != NULL", sem != NULL);
  482. // *sem = count + 1;
  483. *sem = xSemaphoreCreateBinary();/* 创建二值信号量 */
  484. if(*sem == NULL)
  485. {
  486. return ERR_BUF;
  487. }
  488. if(count == 0)
  489. {
  490. xSemaphoreTake(*sem,0);/* 获取信号量 */
  491. }
  492. else
  493. {
  494. xSemaphoreGive(*sem);/* 释放信号量 */
  495. }
  496. return ERR_OK;
  497. }
  498. void
  499. sys_sem_free(sys_sem_t *sem)
  500. {
  501. // LWIP_ASSERT("sem != NULL", sem != NULL);
  502. // *sem = 0;
  503. vSemaphoreDelete(*sem);/* 删除信号量 */
  504. }
  505. void
  506. sys_sem_set_invalid(sys_sem_t *sem)
  507. {
  508. LWIP_ASSERT("sem != NULL", sem != NULL);
  509. *sem = 0;
  510. }
  511. /* semaphores are 1-based because RAM is initialized as 0, which would be valid */
  512. u32_t
  513. sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  514. {
  515. portTickType StartTime,Endtime,Elapsed;
  516. StartTime = xTaskGetTickCount();
  517. if(timeout != 0 )/* 有超时时间设定 */
  518. {
  519. if(xSemaphoreTake(*sem,timeout/portTICK_RATE_MS) == pdTRUE)
  520. {
  521. Endtime = xTaskGetTickCount();
  522. Elapsed = (Endtime - StartTime) * portTICK_RATE_MS;
  523. return Elapsed;
  524. }
  525. else
  526. {
  527. return SYS_ARCH_TIMEOUT;
  528. }
  529. }
  530. else/* 无超时时间,死等 */
  531. {
  532. xSemaphoreTake(*sem,portMAX_DELAY);
  533. Endtime = xTaskGetTickCount();
  534. Elapsed = (Endtime - StartTime) * portTICK_RATE_MS;
  535. return Elapsed;
  536. }
  537. #if 0
  538. u32_t ret = 0;
  539. LWIP_ASSERT("sem != NULL", sem != NULL);
  540. LWIP_ASSERT("*sem > 0", *sem > 0);
  541. if (*sem == 1) {
  542. /* need to wait */
  543. if(!timeout)
  544. {
  545. /* wait infinite */
  546. LWIP_ASSERT("cannot wait without waiting callback", the_waiting_fn != NULL);
  547. do {
  548. int expectSomething = the_waiting_fn(sem, NULL);
  549. LWIP_ASSERT("*sem > 0", *sem > 0);
  550. LWIP_ASSERT("expecting a semaphore count but it's 0", !expectSomething || (*sem > 1));
  551. ret++;
  552. if (ret == SYS_ARCH_TIMEOUT) {
  553. ret--;
  554. }
  555. } while(*sem == 1);
  556. }
  557. else
  558. {
  559. if (the_waiting_fn) {
  560. int expectSomething = the_waiting_fn(sem, NULL);
  561. LWIP_ASSERT("expecting a semaphore count but it's 0", !expectSomething || (*sem > 1));
  562. }
  563. LWIP_ASSERT("*sem > 0", *sem > 0);
  564. if (*sem == 1) {
  565. return SYS_ARCH_TIMEOUT;
  566. }
  567. ret = 1;
  568. }
  569. }
  570. LWIP_ASSERT("*sem > 0", *sem > 0);
  571. (*sem)--;
  572. LWIP_ASSERT("*sem > 0", *sem > 0);
  573. /* return the time we waited for the sem */
  574. return ret;
  575. #endif
  576. }
  577. void
  578. sys_sem_signal(sys_sem_t *sem)
  579. {
  580. // LWIP_ASSERT("sem != NULL", sem != NULL);
  581. // LWIP_ASSERT("*sem > 0", *sem > 0);
  582. // (*sem)++;
  583. // LWIP_ASSERT("*sem > 0", *sem > 0);
  584. xSemaphoreGive(*sem);/* 释放信号量 */
  585. }
  586. int sys_sem_valid(sys_sem_t *sem)
  587. {
  588. return ERR_OK;
  589. }
  590. err_t
  591. sys_mutex_new(sys_mutex_t *mutex)
  592. {
  593. // LWIP_ASSERT("mutex != NULL", mutex != NULL);
  594. // *mutex = 1; /* 1 allocated */
  595. return ERR_OK;
  596. }
  597. void
  598. sys_mutex_free(sys_mutex_t *mutex)
  599. {
  600. /* parameter check */
  601. // LWIP_ASSERT("mutex != NULL", mutex != NULL);
  602. // LWIP_ASSERT("*mutex >= 1", *mutex >= 1);
  603. // *mutex = 0;
  604. }
  605. void
  606. sys_mutex_set_invalid(sys_mutex_t *mutex)
  607. {
  608. // LWIP_ASSERT("mutex != NULL", mutex != NULL);
  609. // *mutex = 0;
  610. }
  611. void
  612. sys_mutex_lock(sys_mutex_t *mutex)
  613. {
  614. /* nothing to do, no multithreading supported */
  615. #if 0
  616. LWIP_ASSERT("mutex != NULL", mutex != NULL);
  617. /* check that the mutext is valid and unlocked (no nested locking) */
  618. LWIP_ASSERT("*mutex >= 1", *mutex == 1);
  619. /* we count up just to check the correct pairing of lock/unlock */
  620. (*mutex)++;
  621. LWIP_ASSERT("*mutex >= 1", *mutex >= 1);
  622. #endif
  623. }
  624. void
  625. sys_mutex_unlock(sys_mutex_t *mutex)
  626. {
  627. /* nothing to do, no multithreading supported */
  628. #if 0
  629. LWIP_ASSERT("mutex != NULL", mutex != NULL);
  630. LWIP_ASSERT("*mutex >= 1", *mutex >= 1);
  631. /* we count down just to check the correct pairing of lock/unlock */
  632. (*mutex)--;
  633. LWIP_ASSERT("*mutex >= 1", *mutex >= 1);
  634. #endif
  635. }
  636. sys_thread_t
  637. sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksize, int prio)
  638. {
  639. xTaskHandle network_task;
  640. int result;
  641. if(s_nextthread < SYS_THREAD_MAX)
  642. result = xTaskCreate(function,(portCHAR *)name,stacksize,arg,prio,&network_task);
  643. if(result == pdPASS)
  644. return network_task;
  645. else
  646. return NULL;
  647. #if 0
  648. LWIP_UNUSED_ARG(name);
  649. LWIP_UNUSED_ARG(function);
  650. LWIP_UNUSED_ARG(arg);
  651. LWIP_UNUSED_ARG(stacksize);
  652. LWIP_UNUSED_ARG(prio);
  653. /* threads not supported */
  654. return 0;
  655. #endif
  656. }
  657. err_t
  658. sys_mbox_new(sys_mbox_t *mbox, int size)
  659. {
  660. *mbox = xQueueCreate(size,sizeof(void *));
  661. #if 0
  662. int mboxsize = size;
  663. LWIP_ASSERT("mbox != NULL", mbox != NULL);
  664. LWIP_ASSERT("size >= 0", size >= 0);
  665. if (size == 0) {
  666. mboxsize = 1024;
  667. }
  668. mbox->head = mbox->tail = 0;
  669. mbox->sem = mbox; /* just point to something for sys_mbox_valid() */
  670. mbox->q_mem = (void**)malloc(sizeof(void*)*mboxsize);
  671. mbox->size = mboxsize;
  672. mbox->used = 0;
  673. memset(mbox->q_mem, 0, sizeof(void*)*mboxsize);
  674. #endif
  675. return ERR_OK;
  676. }
  677. void
  678. sys_mbox_free(sys_mbox_t *mbox)
  679. {
  680. if(uxQueueMessagesWaiting(*mbox))
  681. {
  682. portNOP();
  683. }
  684. vQueueDelete(*mbox);
  685. /* parameter check */
  686. #if 0
  687. LWIP_ASSERT("mbox != NULL", mbox != NULL);
  688. LWIP_ASSERT("mbox->sem != NULL", mbox->sem != NULL);
  689. LWIP_ASSERT("mbox->sem == mbox", mbox->sem == mbox);
  690. LWIP_ASSERT("mbox->q_mem != NULL", mbox->q_mem != NULL);
  691. mbox->sem = NULL;
  692. free(mbox->q_mem);
  693. mbox->q_mem = NULL;
  694. #endif
  695. }
  696. void
  697. sys_mbox_set_invalid(sys_mbox_t *mbox)
  698. {
  699. #if 0
  700. LWIP_ASSERT("mbox != NULL", mbox != NULL);
  701. LWIP_ASSERT("mbox->q_mem == NULL", mbox->q_mem == NULL);
  702. mbox->sem = NULL;
  703. mbox->q_mem = NULL;
  704. #endif
  705. }
  706. void
  707. sys_mbox_post(sys_mbox_t *q, void *msg)
  708. {
  709. while(xQueueSendToBack(*q,&msg,portMAX_DELAY) != pdTRUE);
  710. #if 0
  711. LWIP_ASSERT("q != SYS_MBOX_NULL", q != SYS_MBOX_NULL);
  712. LWIP_ASSERT("q->sem == q", q->sem == q);
  713. LWIP_ASSERT("q->q_mem != NULL", q->q_mem != NULL);
  714. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  715. LWIP_ASSERT("q->size > 0", q->size > 0);
  716. LWIP_ASSERT("mbox already full", q->used < q->size);
  717. q->q_mem[q->head] = msg;
  718. q->head++;
  719. if (q->head >= (unsigned int)q->size) {
  720. q->head = 0;
  721. }
  722. LWIP_ASSERT("mbox is full!", q->head != q->tail);
  723. q->used++;
  724. #endif
  725. }
  726. err_t
  727. sys_mbox_trypost(sys_mbox_t *q, void *msg)
  728. {
  729. if(xQueueSend(*q,&msg,0) == pdPASS)
  730. return ERR_OK;
  731. else
  732. return ERR_MEM;
  733. #if 0
  734. LWIP_ASSERT("q != SYS_MBOX_NULL", q != SYS_MBOX_NULL);
  735. LWIP_ASSERT("q->sem == q", q->sem == q);
  736. LWIP_ASSERT("q->q_mem != NULL", q->q_mem != NULL);
  737. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  738. LWIP_ASSERT("q->size > 0", q->size > 0);
  739. LWIP_ASSERT("q->used <= q->size", q->used <= q->size);
  740. if (q->used == q->size) {
  741. return ERR_MEM;
  742. }
  743. sys_mbox_post(q, msg);
  744. return ERR_OK;
  745. #endif
  746. }
  747. err_t
  748. sys_mbox_trypost_fromisr(sys_mbox_t *q, void *msg)
  749. {
  750. return sys_mbox_trypost(q, msg);
  751. }
  752. u32_t
  753. sys_arch_mbox_fetch(sys_mbox_t *q, void **msg, u32_t timeout)
  754. {
  755. portTickType StartTime,Endtime,Elapsed;
  756. void *dummuyptr;
  757. StartTime = xTaskGetTickCount();
  758. if(msg == NULL)
  759. msg = &dummuyptr;
  760. if(timeout != 0)
  761. {
  762. if(pdTRUE == xQueueReceive(*q,&(*msg),timeout/portTICK_RATE_MS))
  763. {
  764. Endtime = xTaskGetTickCount();
  765. Elapsed = (Endtime - StartTime)*portTICK_RATE_MS;
  766. return Elapsed;
  767. }
  768. else
  769. {
  770. *msg = NULL;
  771. return SYS_ARCH_TIMEOUT;
  772. }
  773. }
  774. else
  775. {
  776. while(pdTRUE != xQueueReceive(*q,&(*msg),portMAX_DELAY));
  777. Endtime = xTaskGetTickCount();
  778. Elapsed = (Endtime - StartTime)*portTICK_RATE_MS;
  779. return Elapsed;
  780. }
  781. #if 0
  782. u32_t ret = 0;
  783. u32_t ret2;
  784. LWIP_ASSERT("q != SYS_MBOX_NULL", q != SYS_MBOX_NULL);
  785. LWIP_ASSERT("q->sem == q", q->sem == q);
  786. LWIP_ASSERT("q->q_mem != NULL", q->q_mem != NULL);
  787. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  788. LWIP_ASSERT("q->size > 0", q->size > 0);
  789. if (q->used == 0) {
  790. /* need to wait */
  791. /* need to wait */
  792. if(!timeout)
  793. {
  794. /* wait infinite */
  795. LWIP_ASSERT("cannot wait without waiting callback", the_waiting_fn != NULL);
  796. do {
  797. int expectSomething = the_waiting_fn(NULL, q);
  798. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  799. LWIP_ASSERT("expecting item available but it's 0", !expectSomething || (q->used > 0));
  800. ret++;
  801. if (ret == SYS_ARCH_TIMEOUT) {
  802. ret--;
  803. }
  804. } while(q->used == 0);
  805. }
  806. else
  807. {
  808. if (the_waiting_fn) {
  809. int expectSomething = the_waiting_fn(NULL, q);
  810. LWIP_ASSERT("expecting item available count but it's 0", !expectSomething || (q->used > 0));
  811. }
  812. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  813. if (q->used == 0) {
  814. if(msg) {
  815. *msg = NULL;
  816. }
  817. return SYS_ARCH_TIMEOUT;
  818. }
  819. ret = 1;
  820. }
  821. }
  822. LWIP_ASSERT("q->used > 0", q->used > 0);
  823. ret2 = sys_arch_mbox_tryfetch(q, msg);
  824. LWIP_ASSERT("got no message", ret2 == 0);
  825. return ret;
  826. #endif
  827. }
  828. u32_t
  829. sys_arch_mbox_tryfetch(sys_mbox_t *q, void **msg)
  830. {
  831. void *dummuyptr;
  832. if(msg == NULL)
  833. msg = &dummuyptr;
  834. if(pdTRUE == xQueueReceive(*q,&(*msg),0))
  835. {
  836. return ERR_OK;
  837. }
  838. else
  839. {
  840. return SYS_MBOX_EMPTY;
  841. }
  842. #if 0
  843. LWIP_ASSERT("q != SYS_MBOX_NULL", q != SYS_MBOX_NULL);
  844. LWIP_ASSERT("q->sem == q", q->sem == q);
  845. LWIP_ASSERT("q->q_mem != NULL", q->q_mem != NULL);
  846. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  847. LWIP_ASSERT("q->size > 0", q->size > 0);
  848. if (!q->used) {
  849. return SYS_ARCH_TIMEOUT;
  850. }
  851. if(msg) {
  852. *msg = q->q_mem[q->tail];
  853. }
  854. q->tail++;
  855. if (q->tail >= (unsigned int)q->size) {
  856. q->tail = 0;
  857. }
  858. q->used--;
  859. LWIP_ASSERT("q->used >= 0", q->used >= 0);
  860. return 0;
  861. #endif
  862. }
  863. sys_prot_t sys_arch_protect(void)
  864. {
  865. vPortEnterCritical();
  866. return 1;
  867. }
  868. void sys_arch_unprotect(sys_prot_t pval)
  869. {
  870. (void) pval;
  871. vPortExitCritical();
  872. }
  873. void sys_assert(const char *msg)
  874. {
  875. (void)msg;
  876. vPortEnterCritical();
  877. for(;;);
  878. }
  879. int sys_mbox_valid(sys_mbox_t *mbox)
  880. {
  881. //return ERR_OK;
  882. if(mbox == NULL) {
  883. return 0;
  884. }
  885. else {
  886. return 1;
  887. }
  888. }
  889. #if LWIP_NETCONN_SEM_PER_THREAD
  890. #error LWIP_NETCONN_SEM_PER_THREAD==1 not supported
  891. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  892. #endif /* !NO_SYS */
  893. #endif