sys_arch.c 15 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 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. #if !NO_SYS
  39. #include "cmsis_os.h"
  40. #if defined(LWIP_PROVIDE_ERRNO)
  41. int errno;
  42. #endif
  43. /*-----------------------------------------------------------------------------------*/
  44. // Creates an empty mailbox.
  45. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  46. {
  47. #if (osCMSIS < 0x20000U)
  48. osMessageQDef(QUEUE, size, void *);
  49. *mbox = osMessageCreate(osMessageQ(QUEUE), NULL);
  50. #else
  51. *mbox = osMessageQueueNew(size, sizeof(void *), NULL);
  52. #endif
  53. #if SYS_STATS
  54. ++lwip_stats.sys.mbox.used;
  55. if(lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used)
  56. {
  57. lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
  58. }
  59. #endif /* SYS_STATS */
  60. if(*mbox == NULL)
  61. return ERR_MEM;
  62. return ERR_OK;
  63. }
  64. /*-----------------------------------------------------------------------------------*/
  65. /*
  66. Deallocates a mailbox. If there are messages still present in the
  67. mailbox when the mailbox is deallocated, it is an indication of a
  68. programming error in lwIP and the developer should be notified.
  69. */
  70. void sys_mbox_free(sys_mbox_t *mbox)
  71. {
  72. #if (osCMSIS < 0x20000U)
  73. if(osMessageWaiting(*mbox))
  74. #else
  75. if(osMessageQueueGetCount(*mbox))
  76. #endif
  77. {
  78. /* Line for breakpoint. Should never break here! */
  79. portNOP();
  80. #if SYS_STATS
  81. lwip_stats.sys.mbox.err++;
  82. #endif /* SYS_STATS */
  83. }
  84. #if (osCMSIS < 0x20000U)
  85. osMessageDelete(*mbox);
  86. #else
  87. osMessageQueueDelete(*mbox);
  88. #endif
  89. #if SYS_STATS
  90. --lwip_stats.sys.mbox.used;
  91. #endif /* SYS_STATS */
  92. }
  93. /*-----------------------------------------------------------------------------------*/
  94. // Posts the "msg" to the mailbox.
  95. void sys_mbox_post(sys_mbox_t *mbox, void *data)
  96. {
  97. #if (osCMSIS < 0x20000U)
  98. while(osMessagePut(*mbox, (uint32_t)data, osWaitForever) != osOK);
  99. #else
  100. while(osMessageQueuePut(*mbox, &data, 0, osWaitForever) != osOK);
  101. #endif
  102. }
  103. /*-----------------------------------------------------------------------------------*/
  104. // Try to post the "msg" to the mailbox.
  105. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  106. {
  107. err_t result;
  108. #if (osCMSIS < 0x20000U)
  109. if(osMessagePut(*mbox, (uint32_t)msg, 0) == osOK)
  110. #else
  111. if(osMessageQueuePut(*mbox, &msg, 0, 0) == osOK)
  112. #endif
  113. {
  114. result = ERR_OK;
  115. }
  116. else
  117. {
  118. // could not post, queue must be full
  119. result = ERR_MEM;
  120. #if SYS_STATS
  121. lwip_stats.sys.mbox.err++;
  122. #endif /* SYS_STATS */
  123. }
  124. return result;
  125. }
  126. /*-----------------------------------------------------------------------------------*/
  127. // Try to post the "msg" to the mailbox.
  128. err_t sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg)
  129. {
  130. return sys_mbox_trypost(mbox, msg);
  131. }
  132. /*-----------------------------------------------------------------------------------*/
  133. /*
  134. Blocks the thread until a message arrives in the mailbox, but does
  135. not block the thread longer than "timeout" milliseconds (similar to
  136. the sys_arch_sem_wait() function). The "msg" argument is a result
  137. parameter that is set by the function (i.e., by doing "*msg =
  138. ptr"). The "msg" parameter maybe NULL to indicate that the message
  139. should be dropped.
  140. The return values are the same as for the sys_arch_sem_wait() function:
  141. Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
  142. timeout.
  143. Note that a function with a similar name, sys_mbox_fetch(), is
  144. implemented by lwIP.
  145. */
  146. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  147. {
  148. #if (osCMSIS < 0x20000U)
  149. osEvent event;
  150. uint32_t starttime = osKernelSysTick();
  151. #else
  152. osStatus_t status;
  153. uint32_t starttime = osKernelGetTickCount();
  154. #endif
  155. if(timeout != 0)
  156. {
  157. #if (osCMSIS < 0x20000U)
  158. event = osMessageGet (*mbox, timeout);
  159. if(event.status == osEventMessage)
  160. {
  161. *msg = (void *)event.value.v;
  162. return (osKernelSysTick() - starttime);
  163. }
  164. #else
  165. status = osMessageQueueGet(*mbox, msg, 0, timeout);
  166. if (status == osOK)
  167. {
  168. return (osKernelGetTickCount() - starttime);
  169. }
  170. #endif
  171. else
  172. {
  173. return SYS_ARCH_TIMEOUT;
  174. }
  175. }
  176. else
  177. {
  178. #if (osCMSIS < 0x20000U)
  179. event = osMessageGet (*mbox, osWaitForever);
  180. *msg = (void *)event.value.v;
  181. return (osKernelSysTick() - starttime);
  182. #else
  183. osMessageQueueGet(*mbox, msg, 0, osWaitForever );
  184. return (osKernelGetTickCount() - starttime);
  185. #endif
  186. }
  187. }
  188. /*-----------------------------------------------------------------------------------*/
  189. /*
  190. Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll
  191. return with SYS_MBOX_EMPTY. On success, 0 is returned.
  192. */
  193. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  194. {
  195. #if (osCMSIS < 0x20000U)
  196. osEvent event;
  197. event = osMessageGet (*mbox, 0);
  198. if(event.status == osEventMessage)
  199. {
  200. *msg = (void *)event.value.v;
  201. #else
  202. if (osMessageQueueGet(*mbox, msg, 0, 0) == osOK)
  203. {
  204. #endif
  205. return ERR_OK;
  206. }
  207. else
  208. {
  209. return SYS_MBOX_EMPTY;
  210. }
  211. }
  212. /*----------------------------------------------------------------------------------*/
  213. int sys_mbox_valid(sys_mbox_t *mbox)
  214. {
  215. if (*mbox == SYS_MBOX_NULL)
  216. return 0;
  217. else
  218. return 1;
  219. }
  220. /*-----------------------------------------------------------------------------------*/
  221. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  222. {
  223. *mbox = SYS_MBOX_NULL;
  224. }
  225. /*-----------------------------------------------------------------------------------*/
  226. // Creates a new semaphore. The "count" argument specifies
  227. // the initial state of the semaphore.
  228. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  229. {
  230. #if (osCMSIS < 0x20000U)
  231. osSemaphoreDef(SEM);
  232. *sem = osSemaphoreCreate (osSemaphore(SEM), 1);
  233. #else
  234. *sem = osSemaphoreNew(UINT16_MAX, count, NULL);
  235. #endif
  236. if(*sem == NULL)
  237. {
  238. #if SYS_STATS
  239. ++lwip_stats.sys.sem.err;
  240. #endif /* SYS_STATS */
  241. return ERR_MEM;
  242. }
  243. if(count == 0) // Means it can't be taken
  244. {
  245. #if (osCMSIS < 0x20000U)
  246. osSemaphoreWait(*sem, 0);
  247. #else
  248. osSemaphoreAcquire(*sem, 0);
  249. #endif
  250. }
  251. #if SYS_STATS
  252. ++lwip_stats.sys.sem.used;
  253. if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) {
  254. lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
  255. }
  256. #endif /* SYS_STATS */
  257. return ERR_OK;
  258. }
  259. /*-----------------------------------------------------------------------------------*/
  260. /*
  261. Blocks the thread while waiting for the semaphore to be
  262. signaled. If the "timeout" argument is non-zero, the thread should
  263. only be blocked for the specified time (measured in
  264. milliseconds).
  265. If the timeout argument is non-zero, the return value is the number of
  266. milliseconds spent waiting for the semaphore to be signaled. If the
  267. semaphore wasn't signaled within the specified time, the return value is
  268. SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
  269. (i.e., it was already signaled), the function may return zero.
  270. Notice that lwIP implements a function with a similar name,
  271. sys_sem_wait(), that uses the sys_arch_sem_wait() function.
  272. */
  273. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  274. {
  275. #if (osCMSIS < 0x20000U)
  276. uint32_t starttime = osKernelSysTick();
  277. #else
  278. uint32_t starttime = osKernelGetTickCount();
  279. #endif
  280. if(timeout != 0)
  281. {
  282. #if (osCMSIS < 0x20000U)
  283. if(osSemaphoreWait (*sem, timeout) == osOK)
  284. {
  285. return (osKernelSysTick() - starttime);
  286. #else
  287. if(osSemaphoreAcquire(*sem, timeout) == osOK)
  288. {
  289. return (osKernelGetTickCount() - starttime);
  290. #endif
  291. }
  292. else
  293. {
  294. return SYS_ARCH_TIMEOUT;
  295. }
  296. }
  297. else
  298. {
  299. #if (osCMSIS < 0x20000U)
  300. while(osSemaphoreWait (*sem, osWaitForever) != osOK);
  301. return (osKernelSysTick() - starttime);
  302. #else
  303. while(osSemaphoreAcquire(*sem, osWaitForever) != osOK);
  304. return (osKernelGetTickCount() - starttime);
  305. #endif
  306. }
  307. }
  308. /*-----------------------------------------------------------------------------------*/
  309. // Signals a semaphore
  310. void sys_sem_signal(sys_sem_t *sem)
  311. {
  312. osSemaphoreRelease(*sem);
  313. }
  314. /*-----------------------------------------------------------------------------------*/
  315. // Deallocates a semaphore
  316. void sys_sem_free(sys_sem_t *sem)
  317. {
  318. #if SYS_STATS
  319. --lwip_stats.sys.sem.used;
  320. #endif /* SYS_STATS */
  321. osSemaphoreDelete(*sem);
  322. }
  323. /*-----------------------------------------------------------------------------------*/
  324. int sys_sem_valid(sys_sem_t *sem)
  325. {
  326. if (*sem == SYS_SEM_NULL)
  327. return 0;
  328. else
  329. return 1;
  330. }
  331. /*-----------------------------------------------------------------------------------*/
  332. void sys_sem_set_invalid(sys_sem_t *sem)
  333. {
  334. *sem = SYS_SEM_NULL;
  335. }
  336. /*-----------------------------------------------------------------------------------*/
  337. #if (osCMSIS < 0x20000U)
  338. osMutexId lwip_sys_mutex;
  339. osMutexDef(lwip_sys_mutex);
  340. #else
  341. osMutexId_t lwip_sys_mutex;
  342. #endif
  343. // Initialize sys arch
  344. void sys_init(void)
  345. {
  346. #if (osCMSIS < 0x20000U)
  347. lwip_sys_mutex = osMutexCreate(osMutex(lwip_sys_mutex));
  348. #else
  349. lwip_sys_mutex = osMutexNew(NULL);
  350. #endif
  351. }
  352. /*-----------------------------------------------------------------------------------*/
  353. /* Mutexes*/
  354. /*-----------------------------------------------------------------------------------*/
  355. /*-----------------------------------------------------------------------------------*/
  356. #if LWIP_COMPAT_MUTEX == 0
  357. /* Create a new mutex*/
  358. err_t sys_mutex_new(sys_mutex_t *mutex) {
  359. #if (osCMSIS < 0x20000U)
  360. osMutexDef(MUTEX);
  361. *mutex = osMutexCreate(osMutex(MUTEX));
  362. #else
  363. *mutex = osMutexNew(NULL);
  364. #endif
  365. if(*mutex == NULL)
  366. {
  367. #if SYS_STATS
  368. ++lwip_stats.sys.mutex.err;
  369. #endif /* SYS_STATS */
  370. return ERR_MEM;
  371. }
  372. #if SYS_STATS
  373. ++lwip_stats.sys.mutex.used;
  374. if (lwip_stats.sys.mutex.max < lwip_stats.sys.mutex.used) {
  375. lwip_stats.sys.mutex.max = lwip_stats.sys.mutex.used;
  376. }
  377. #endif /* SYS_STATS */
  378. return ERR_OK;
  379. }
  380. /*-----------------------------------------------------------------------------------*/
  381. /* Deallocate a mutex*/
  382. void sys_mutex_free(sys_mutex_t *mutex)
  383. {
  384. #if SYS_STATS
  385. --lwip_stats.sys.mutex.used;
  386. #endif /* SYS_STATS */
  387. osMutexDelete(*mutex);
  388. }
  389. /*-----------------------------------------------------------------------------------*/
  390. /* Lock a mutex*/
  391. void sys_mutex_lock(sys_mutex_t *mutex)
  392. {
  393. #if (osCMSIS < 0x20000U)
  394. osMutexWait(*mutex, osWaitForever);
  395. #else
  396. osMutexAcquire(*mutex, osWaitForever);
  397. #endif
  398. }
  399. /*-----------------------------------------------------------------------------------*/
  400. /* Unlock a mutex*/
  401. void sys_mutex_unlock(sys_mutex_t *mutex)
  402. {
  403. osMutexRelease(*mutex);
  404. }
  405. #endif /*LWIP_COMPAT_MUTEX*/
  406. /*-----------------------------------------------------------------------------------*/
  407. // TODO
  408. /*-----------------------------------------------------------------------------------*/
  409. /*
  410. Starts a new thread with priority "prio" that will begin its execution in the
  411. function "thread()". The "arg" argument will be passed as an argument to the
  412. thread() function. The id of the new thread is returned. Both the id and
  413. the priority are system dependent.
  414. */
  415. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread , void *arg, int stacksize, int prio)
  416. {
  417. #if (osCMSIS < 0x20000U)
  418. const osThreadDef_t os_thread_def = { (char *)name, (os_pthread)thread, (osPriority)prio, 0, stacksize};
  419. return osThreadCreate(&os_thread_def, arg);
  420. #else
  421. const osThreadAttr_t attributes = {
  422. .name = name,
  423. .stack_size = stacksize,
  424. .priority = (osPriority_t)prio,
  425. };
  426. return osThreadNew(thread, arg, &attributes);
  427. #endif
  428. }
  429. /*
  430. This optional function does a "fast" critical region protection and returns
  431. the previous protection level. This function is only called during very short
  432. critical regions. An embedded system which supports ISR-based drivers might
  433. want to implement this function by disabling interrupts. Task-based systems
  434. might want to implement this by using a mutex or disabling tasking. This
  435. function should support recursive calls from the same task or interrupt. In
  436. other words, sys_arch_protect() could be called while already protected. In
  437. that case the return value indicates that it is already protected.
  438. sys_arch_protect() is only required if your port is supporting an operating
  439. system.
  440. Note: This function is based on FreeRTOS API, because no equivalent CMSIS-RTOS
  441. API is available
  442. */
  443. sys_prot_t sys_arch_protect(void)
  444. {
  445. #if (osCMSIS < 0x20000U)
  446. osMutexWait(lwip_sys_mutex, osWaitForever);
  447. #else
  448. osMutexAcquire(lwip_sys_mutex, osWaitForever);
  449. #endif
  450. return (sys_prot_t)1;
  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. Note: This function is based on FreeRTOS API, because no equivalent CMSIS-RTOS
  458. API is available
  459. */
  460. void sys_arch_unprotect(sys_prot_t pval)
  461. {
  462. ( void ) pval;
  463. osMutexRelease(lwip_sys_mutex);
  464. }
  465. #endif /* !NO_SYS */