sys_arch.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * **** This file incorporates work covered by the following copyright and ****
  15. * **** permission notice: ****
  16. *
  17. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without modification,
  21. * are permitted provided that the following conditions are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright notice,
  26. * this list of conditions and the following disclaimer in the documentation
  27. * and/or other materials provided with the distribution.
  28. * 3. The name of the author may not be used to endorse or promote products
  29. * derived from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  32. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  34. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  36. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  40. * OF SUCH DAMAGE.
  41. *
  42. * This file is part of the lwIP TCP/IP stack.
  43. *
  44. * Author: Adam Dunkels <adam@sics.se>
  45. *
  46. */
  47. /*
  48. * See http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions.
  49. */
  50. #include "hal.h"
  51. #include "lwip/opt.h"
  52. #include "lwip/mem.h"
  53. #include "lwip/sys.h"
  54. #include "lwip/stats.h"
  55. #include "arch/cc.h"
  56. #include "arch/sys_arch.h"
  57. void sys_init(void) {
  58. }
  59. err_t sys_sem_new(sys_sem_t *sem, u8_t count) {
  60. *sem = chHeapAlloc(NULL, sizeof(semaphore_t));
  61. if (*sem == 0) {
  62. SYS_STATS_INC(sem.err);
  63. return ERR_MEM;
  64. }
  65. else {
  66. chSemObjectInit(*sem, (cnt_t)count);
  67. SYS_STATS_INC_USED(sem);
  68. return ERR_OK;
  69. }
  70. }
  71. void sys_sem_free(sys_sem_t *sem) {
  72. chHeapFree(*sem);
  73. *sem = SYS_SEM_NULL;
  74. SYS_STATS_DEC(sem.used);
  75. }
  76. void sys_sem_signal(sys_sem_t *sem) {
  77. chSemSignal(*sem);
  78. }
  79. /* CHIBIOS FIX: specific variant of this call to be called from within
  80. a lock.*/
  81. void sys_sem_signal_S(sys_sem_t *sem) {
  82. chSemSignalI(*sem);
  83. chSchRescheduleS();
  84. }
  85. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) {
  86. systime_t start;
  87. sysinterval_t tmo, remaining;
  88. chSysLock();
  89. tmo = timeout > 0 ? TIME_MS2I((time_msecs_t)timeout) : TIME_INFINITE;
  90. start = chVTGetSystemTimeX();
  91. if (chSemWaitTimeoutS(*sem, tmo) != MSG_OK) {
  92. chSysUnlock();
  93. return SYS_ARCH_TIMEOUT;
  94. }
  95. remaining = chTimeDiffX(start, chVTGetSystemTimeX());
  96. chSysUnlock();
  97. return (u32_t)TIME_I2MS(remaining);
  98. }
  99. int sys_sem_valid(sys_sem_t *sem) {
  100. return *sem != SYS_SEM_NULL;
  101. }
  102. // typically called within lwIP after freeing a semaphore
  103. // to make sure the pointer is not left pointing to invalid data
  104. void sys_sem_set_invalid(sys_sem_t *sem) {
  105. *sem = SYS_SEM_NULL;
  106. }
  107. err_t sys_mbox_new(sys_mbox_t *mbox, int size) {
  108. *mbox = chHeapAlloc(NULL, sizeof(mailbox_t) + sizeof(msg_t) * size);
  109. if (*mbox == 0) {
  110. SYS_STATS_INC(mbox.err);
  111. return ERR_MEM;
  112. }
  113. else {
  114. chMBObjectInit(*mbox, (void *)(((uint8_t *)*mbox) + sizeof(mailbox_t)), size);
  115. SYS_STATS_INC(mbox.used);
  116. return ERR_OK;
  117. }
  118. }
  119. void sys_mbox_free(sys_mbox_t *mbox) {
  120. cnt_t tmpcnt;
  121. chSysLock();
  122. tmpcnt = chMBGetUsedCountI(*mbox);
  123. chSysUnlock();
  124. if (tmpcnt != 0) {
  125. // If there are messages still present in the mailbox when the mailbox
  126. // is deallocated, it is an indication of a programming error in lwIP
  127. // and the developer should be notified.
  128. SYS_STATS_INC(mbox.err);
  129. chMBReset(*mbox);
  130. }
  131. chHeapFree(*mbox);
  132. *mbox = SYS_MBOX_NULL;
  133. SYS_STATS_DEC(mbox.used);
  134. }
  135. void sys_mbox_post(sys_mbox_t *mbox, void *msg) {
  136. chMBPostTimeout(*mbox, (msg_t)msg, TIME_INFINITE);
  137. }
  138. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) {
  139. if (chMBPostTimeout(*mbox, (msg_t)msg, TIME_IMMEDIATE) == MSG_TIMEOUT) {
  140. SYS_STATS_INC(mbox.err);
  141. return ERR_MEM;
  142. }
  143. return ERR_OK;
  144. }
  145. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout) {
  146. systime_t start;
  147. sysinterval_t tmo, remaining;
  148. chSysLock();
  149. tmo = timeout > 0 ? TIME_MS2I((time_msecs_t)timeout) : TIME_INFINITE;
  150. start = chVTGetSystemTimeX();
  151. if (chMBFetchTimeoutS(*mbox, (msg_t *)msg, tmo) != MSG_OK) {
  152. chSysUnlock();
  153. return SYS_ARCH_TIMEOUT;
  154. }
  155. remaining = chTimeDiffX(start, chVTGetSystemTimeX());
  156. chSysUnlock();
  157. return (u32_t)TIME_I2MS(remaining);
  158. }
  159. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) {
  160. if (chMBFetchTimeout(*mbox, (msg_t *)msg, TIME_IMMEDIATE) == MSG_TIMEOUT)
  161. return SYS_MBOX_EMPTY;
  162. return 0;
  163. }
  164. int sys_mbox_valid(sys_mbox_t *mbox) {
  165. return *mbox != SYS_MBOX_NULL;
  166. }
  167. // typically called within lwIP after freeing an mbox
  168. // to make sure the pointer is not left pointing to invalid data
  169. void sys_mbox_set_invalid(sys_mbox_t *mbox) {
  170. *mbox = SYS_MBOX_NULL;
  171. }
  172. sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread,
  173. void *arg, int stacksize, int prio) {
  174. thread_t *tp;
  175. tp = chThdCreateFromHeap(NULL, THD_WORKING_AREA_SIZE(stacksize),
  176. name, prio, (tfunc_t)thread, arg);
  177. return (sys_thread_t)tp;
  178. }
  179. sys_prot_t sys_arch_protect(void) {
  180. return chSysGetStatusAndLockX();
  181. }
  182. void sys_arch_unprotect(sys_prot_t pval) {
  183. chSysRestoreStatusX((syssts_t)pval);
  184. }
  185. u32_t sys_now(void) {
  186. #if OSAL_ST_FREQUENCY == 1000
  187. return (u32_t)chVTGetSystemTimeX();
  188. #elif (OSAL_ST_FREQUENCY / 1000) >= 1 && (OSAL_ST_FREQUENCY % 1000) == 0
  189. return ((u32_t)chVTGetSystemTimeX() - 1) / (OSAL_ST_FREQUENCY / 1000) + 1;
  190. #elif (1000 / OSAL_ST_FREQUENCY) >= 1 && (1000 % OSAL_ST_FREQUENCY) == 0
  191. return ((u32_t)chVTGetSystemTimeX() - 1) * (1000 / OSAL_ST_FREQUENCY) + 1;
  192. #else
  193. return (u32_t)(((u64_t)(chVTGetSystemTimeX() - 1) * 1000) / OSAL_ST_FREQUENCY) + 1;
  194. #endif
  195. }