sys_arch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. #if SYS_STATS
  51. ++lwip_stats.sys.mbox.used;
  52. if(lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used){
  53. lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
  54. }
  55. #endif /* SYS_STATS */
  56. if (*mbox == NULL){
  57. return ERR_MEM;
  58. }
  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. /* Line for breakpoint. Should never break here! */
  71. portNOP();
  72. #if SYS_STATS
  73. lwip_stats.sys.mbox.err++;
  74. #endif /* SYS_STATS */
  75. // TODO notify the user of failure.
  76. }
  77. vQueueDelete( *mbox );
  78. #if SYS_STATS
  79. --lwip_stats.sys.mbox.used;
  80. #endif /* SYS_STATS */
  81. }
  82. /*-----------------------------------------------------------------------------------*/
  83. // Posts the "msg" to the mailbox.
  84. void sys_mbox_post(sys_mbox_t *mbox, void *data)
  85. {
  86. while( xQueueSendToBack(*mbox, &data, portMAX_DELAY ) != pdTRUE ){
  87. }
  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. result = ERR_OK;
  96. }
  97. else{
  98. // could not post, queue must be full
  99. result = ERR_MEM;
  100. #if SYS_STATS
  101. lwip_stats.sys.mbox.err++;
  102. #endif /* SYS_STATS */
  103. }
  104. return result;
  105. }
  106. /*-----------------------------------------------------------------------------------*/
  107. /*
  108. Blocks the thread until a message arrives in the mailbox, but does
  109. not block the thread longer than "timeout" milliseconds (similar to
  110. the sys_arch_sem_wait() function). The "msg" argument is a result
  111. parameter that is set by the function (i.e., by doing "*msg =
  112. ptr"). The "msg" parameter maybe NULL to indicate that the message
  113. should be dropped.
  114. The return values are the same as for the sys_arch_sem_wait() function:
  115. Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
  116. timeout.
  117. Note that a function with a similar name, sys_mbox_fetch(), is
  118. implemented by lwIP.
  119. */
  120. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  121. {
  122. void *dummyptr;
  123. portTickType StartTime, EndTime, Elapsed;
  124. StartTime = xTaskGetTickCount();
  125. if( msg == NULL ){
  126. msg = &dummyptr;
  127. }
  128. if( timeout != 0 ){
  129. if ( pdTRUE == xQueueReceive( *mbox, &(*msg), timeout / portTICK_RATE_MS ) ){
  130. EndTime = xTaskGetTickCount();
  131. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  132. return ( Elapsed );
  133. }
  134. // timed out blocking for message
  135. else{
  136. *msg = NULL;
  137. return SYS_ARCH_TIMEOUT;
  138. }
  139. }
  140. // block forever for a message.
  141. else{
  142. while( pdTRUE != xQueueReceive( *mbox, &(*msg), portMAX_DELAY ) ){} // time is arbitrary
  143. EndTime = xTaskGetTickCount();
  144. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  145. return ( Elapsed ); // return time blocked TODO test
  146. }
  147. }
  148. /*-----------------------------------------------------------------------------------*/
  149. /*
  150. Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll
  151. return with SYS_MBOX_EMPTY. On success, 0 is returned.
  152. */
  153. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  154. {
  155. void *dummyptr;
  156. if( msg == NULL ){
  157. msg = &dummyptr;
  158. }
  159. if( pdTRUE == xQueueReceive( *mbox, &(*msg), 0 ) ){
  160. return ERR_OK;
  161. }
  162. else{
  163. return SYS_MBOX_EMPTY;
  164. }
  165. }
  166. /*----------------------------------------------------------------------------------*/
  167. int sys_mbox_valid(sys_mbox_t *mbox)
  168. {
  169. if (*mbox == SYS_MBOX_NULL){
  170. return 0;
  171. }
  172. else{
  173. return 1;
  174. }
  175. }
  176. /*-----------------------------------------------------------------------------------*/
  177. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  178. {
  179. *mbox = SYS_MBOX_NULL;
  180. }
  181. /*-----------------------------------------------------------------------------------*/
  182. // Creates a new semaphore. The "count" argument specifies
  183. // the initial state of the semaphore.
  184. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  185. {
  186. vSemaphoreCreateBinary(*sem );
  187. if(*sem == NULL){
  188. #if SYS_STATS
  189. ++lwip_stats.sys.sem.err;
  190. #endif /* SYS_STATS */
  191. return ERR_MEM;
  192. }
  193. // Means it can't be taken
  194. if(count == 0){
  195. xSemaphoreTake(*sem,1);
  196. }
  197. #if SYS_STATS
  198. ++lwip_stats.sys.sem.used;
  199. if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used){
  200. lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
  201. }
  202. #endif /* SYS_STATS */
  203. return ERR_OK;
  204. }
  205. /*-----------------------------------------------------------------------------------*/
  206. /*
  207. Blocks the thread while waiting for the semaphore to be
  208. signaled. If the "timeout" argument is non-zero, the thread should
  209. only be blocked for the specified time (measured in
  210. milliseconds).
  211. If the timeout argument is non-zero, the return value is the number of
  212. milliseconds spent waiting for the semaphore to be signaled. If the
  213. semaphore wasn't signaled within the specified time, the return value is
  214. SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
  215. (i.e., it was already signaled), the function may return zero.
  216. Notice that lwIP implements a function with a similar name,
  217. sys_sem_wait(), that uses the sys_arch_sem_wait() function.
  218. */
  219. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  220. {
  221. portTickType StartTime, EndTime, Elapsed;
  222. StartTime = xTaskGetTickCount();
  223. if(timeout != 0){
  224. if( xSemaphoreTake( *sem, timeout / portTICK_RATE_MS ) == pdTRUE ){
  225. EndTime = xTaskGetTickCount();
  226. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  227. return (Elapsed); // return time blocked TODO test
  228. }
  229. else{
  230. return SYS_ARCH_TIMEOUT;
  231. }
  232. }
  233. // must block without a timeout
  234. else{
  235. while( xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE){
  236. }
  237. EndTime = xTaskGetTickCount();
  238. Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
  239. return ( Elapsed ); // return time blocked
  240. }
  241. }
  242. /*-----------------------------------------------------------------------------------*/
  243. // Signals a semaphore
  244. void sys_sem_signal(sys_sem_t *sem)
  245. {
  246. xSemaphoreGive(*sem);
  247. }
  248. /*-----------------------------------------------------------------------------------*/
  249. // Deallocates a semaphore
  250. void sys_sem_free(sys_sem_t *sem)
  251. {
  252. #if SYS_STATS
  253. --lwip_stats.sys.sem.used;
  254. #endif /* SYS_STATS */
  255. vQueueDelete(*sem);
  256. }
  257. /*-----------------------------------------------------------------------------------*/
  258. int sys_sem_valid(sys_sem_t *sem)
  259. {
  260. if (*sem == SYS_SEM_NULL){
  261. return 0;
  262. }
  263. else{
  264. return 1;
  265. }
  266. }
  267. /*-----------------------------------------------------------------------------------*/
  268. void sys_sem_set_invalid(sys_sem_t *sem)
  269. {
  270. *sem = SYS_SEM_NULL;
  271. }
  272. /*-----------------------------------------------------------------------------------*/
  273. // Initialize sys arch
  274. void sys_init(void)
  275. {
  276. // keep track of how many threads have been created
  277. s_nextthread = 0;
  278. }
  279. /*-----------------------------------------------------------------------------------*/
  280. /* Mutexes*/
  281. /*-----------------------------------------------------------------------------------*/
  282. /*-----------------------------------------------------------------------------------*/
  283. #if LWIP_COMPAT_MUTEX == 0
  284. /* Create a new mutex*/
  285. err_t sys_mutex_new(sys_mutex_t *mutex)
  286. {
  287. *mutex = xSemaphoreCreateMutex();
  288. if(*mutex == NULL){
  289. #if SYS_STATS
  290. ++lwip_stats.sys.mutex.err;
  291. #endif /* SYS_STATS */
  292. return ERR_MEM;
  293. }
  294. #if SYS_STATS
  295. ++lwip_stats.sys.mutex.used;
  296. if(lwip_stats.sys.mutex.max < lwip_stats.sys.mutex.used){
  297. lwip_stats.sys.mutex.max = lwip_stats.sys.mutex.used;
  298. }
  299. #endif /* SYS_STATS */
  300. return ERR_OK;
  301. }
  302. /*-----------------------------------------------------------------------------------*/
  303. /* Deallocate a mutex*/
  304. void sys_mutex_free(sys_mutex_t *mutex)
  305. {
  306. #if SYS_STATS
  307. --lwip_stats.sys.mutex.used;
  308. #endif /* SYS_STATS */
  309. vQueueDelete(*mutex);
  310. }
  311. /*-----------------------------------------------------------------------------------*/
  312. /* Lock a mutex*/
  313. void sys_mutex_lock(sys_mutex_t *mutex)
  314. {
  315. sys_arch_sem_wait(*mutex, 0);
  316. }
  317. /*-----------------------------------------------------------------------------------*/
  318. /* Unlock a mutex*/
  319. void sys_mutex_unlock(sys_mutex_t *mutex)
  320. {
  321. xSemaphoreGive(*mutex);
  322. }
  323. #endif /*LWIP_COMPAT_MUTEX*/
  324. /*-----------------------------------------------------------------------------------*/
  325. // TODO
  326. /*-----------------------------------------------------------------------------------*/
  327. /*
  328. Starts a new thread with priority "prio" that will begin its execution in the
  329. function "thread()". The "arg" argument will be passed as an argument to the
  330. thread() function. The id of the new thread is returned. Both the id and
  331. the priority are system dependent.
  332. */
  333. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread , void *arg, int stacksize, int prio)
  334. {
  335. xTaskHandle CreatedTask;
  336. int result;
  337. if ( s_nextthread < SYS_THREAD_MAX ){
  338. result = xTaskCreate( thread, name, stacksize, arg, prio, &CreatedTask );
  339. // For each task created, store the task handle (pid) in the timers array.
  340. // This scheme doesn't allow for threads to be deleted
  341. //s_timeoutlist[s_nextthread++].pid = CreatedTask;
  342. if(result == pdPASS){
  343. return CreatedTask;
  344. }
  345. else{
  346. return NULL;
  347. }
  348. }
  349. else{
  350. return NULL;
  351. }
  352. }
  353. /*
  354. This optional function does a "fast" critical region protection and returns
  355. the previous protection level. This function is only called during very short
  356. critical regions. An embedded system which supports ISR-based drivers might
  357. want to implement this function by disabling interrupts. Task-based systems
  358. might want to implement this by using a mutex or disabling tasking. This
  359. function should support recursive calls from the same task or interrupt. In
  360. other words, sys_arch_protect() could be called while already protected. In
  361. that case the return value indicates that it is already protected.
  362. sys_arch_protect() is only required if your port is supporting an operating
  363. system.
  364. */
  365. sys_prot_t sys_arch_protect(void)
  366. {
  367. vPortEnterCritical();
  368. return 1;
  369. }
  370. /*
  371. This optional function does a "fast" set of critical region protection to the
  372. value specified by pval. See the documentation for sys_arch_protect() for
  373. more information. This function is only required if your port is supporting
  374. an operating system.
  375. */
  376. void sys_arch_unprotect(sys_prot_t pval)
  377. {
  378. ( void ) pval;
  379. vPortExitCritical();
  380. }
  381. /*
  382. * Prints an assertion messages and aborts execution.
  383. */
  384. void sys_assert( const char *msg )
  385. {
  386. ( void ) msg;
  387. /*FSL:only needed for debugging
  388. printf(msg);
  389. printf("\n\r");
  390. */
  391. vPortEnterCritical( );
  392. for(;;);
  393. }