os_port.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @file os_port.h
  3. * @brief RTOS abstraction layer
  4. *
  5. * @section License
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * Copyright (C) 2010-2023 Oryx Embedded SARL. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. *
  25. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  26. * @version 2.2.4
  27. **/
  28. #ifndef _OS_PORT_H
  29. #define _OS_PORT_H
  30. //Dependencies
  31. #include "os_port_config.h"
  32. #include "compiler_port.h"
  33. //Compilation flags used to enable/disable features
  34. #define ENABLED 1
  35. #define DISABLED 0
  36. #define PTR_OFFSET(addr, offset) ((void *) ((uint8_t *) (addr) + (offset)))
  37. #define timeCompare(t1, t2) ((int32_t) ((t1) - (t2)))
  38. //Miscellaneous macros
  39. #if !defined(__AT32F403A_407_LIBRARY_VERSION) && \
  40. !defined(__AT32F435_437_LIBRARY_VERSION)
  41. #ifndef FALSE
  42. #define FALSE 0
  43. #endif
  44. #ifndef TRUE
  45. #define TRUE 1
  46. #endif
  47. #endif
  48. #ifndef LSB
  49. #define LSB(x) ((x) & 0xFF)
  50. #endif
  51. #ifndef MSB
  52. #define MSB(x) (((x) >> 8) & 0xFF)
  53. #endif
  54. #ifndef MIN
  55. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  56. #endif
  57. #ifndef MAX
  58. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  59. #endif
  60. #ifndef arraysize
  61. #define arraysize(a) (sizeof(a) / sizeof(a[0]))
  62. #endif
  63. //Infinite delay
  64. #define INFINITE_DELAY ((uint_t) -1)
  65. //Maximum delay
  66. #define MAX_DELAY (INFINITE_DELAY / 2)
  67. //No RTOS?
  68. #if defined(USE_NO_RTOS)
  69. #include "os_port_none.h"
  70. //ChibiOS/RT port?
  71. #elif defined(USE_CHIBIOS)
  72. #include "os_port_chibios.h"
  73. //CMX-RTX port?
  74. #elif defined(USE_CMX_RTX)
  75. #include "os_port_cmx_rtx.h"
  76. //CMSIS-RTOS port?
  77. #elif defined(USE_CMSIS_RTOS)
  78. #include "os_port_cmsis_rtos.h"
  79. //CMSIS-RTOS2 port?
  80. #elif defined(USE_CMSIS_RTOS2)
  81. #include "os_port_cmsis_rtos2.h"
  82. //FreeRTOS port?
  83. #elif defined(USE_FREERTOS)
  84. #include "os_port_freertos.h"
  85. //SafeRTOS port?
  86. #elif defined(USE_SAFERTOS)
  87. #include "os_port_safertos.h"
  88. //Azure RTOS ThreadX port?
  89. #elif defined(USE_THREADX)
  90. #include "os_port_threadx.h"
  91. //Keil RTX port?
  92. #elif defined(USE_RTX)
  93. #include "os_port_rtx.h"
  94. //Micrium uC/OS-II port?
  95. #elif defined(USE_UCOS2)
  96. #include "os_port_ucos2.h"
  97. //Micrium uC/OS-III port?
  98. #elif defined(USE_UCOS3)
  99. #include "os_port_ucos3.h"
  100. //Segger embOS port?
  101. #elif defined(USE_EMBOS)
  102. #include "os_port_embos.h"
  103. //TI SYS/BIOS port?
  104. #elif defined(USE_SYS_BIOS)
  105. #include "os_port_sys_bios.h"
  106. //Zephyr port?
  107. #elif defined(USE_ZEPHYR)
  108. #include "os_port_zephyr.h"
  109. //Windows port?
  110. #elif defined(_WIN32)
  111. #include "os_port_windows.h"
  112. //POSIX Threads port?
  113. #elif defined(__linux__) || defined(__FreeBSD__)
  114. // #include "os_port_posix.h"
  115. #endif
  116. #ifndef osAllocMem
  117. #include <stdlib.h>
  118. #define osAllocMem(size) malloc(size)
  119. #endif
  120. #ifndef osFreeMem
  121. #include <stdlib.h>
  122. #define osFreeMem(p) free(p)
  123. #endif
  124. //Fill block of memory
  125. #ifndef osMemset
  126. #include <string.h>
  127. #define osMemset(p, value, length) (void) memset(p, value, length)
  128. #endif
  129. //Copy block of memory
  130. #ifndef osMemcpy
  131. #include <string.h>
  132. #define osMemcpy(dest, src, length) (void) memcpy(dest, src, length)
  133. #endif
  134. //Move block of memory
  135. #ifndef osMemmove
  136. #include <string.h>
  137. #define osMemmove(dest, src, length) (void) memmove(dest, src, length)
  138. #endif
  139. //Compare two blocks of memory
  140. #ifndef osMemcmp
  141. #include <string.h>
  142. #define osMemcmp(p1, p2, length) memcmp(p1, p2, length)
  143. #endif
  144. //Search for the first occurrence of a given character
  145. #ifndef osMemchr
  146. #include <string.h>
  147. #define osMemchr(p, c, length) memchr(p, c, length)
  148. #endif
  149. //Get string length
  150. #ifndef osStrlen
  151. #include <string.h>
  152. #define osStrlen(s) strlen(s)
  153. #endif
  154. //Compare strings
  155. #ifndef osStrcmp
  156. #include <string.h>
  157. #define osStrcmp(s1, s2) strcmp(s1, s2)
  158. #endif
  159. //Compare substrings
  160. #ifndef osStrncmp
  161. #include <string.h>
  162. #define osStrncmp(s1, s2, length) strncmp(s1, s2, length)
  163. #endif
  164. //Compare strings without case
  165. #ifndef osStrcasecmp
  166. #include <string.h>
  167. #define osStrcasecmp(s1, s2) strcasecmp(s1, s2)
  168. #endif
  169. //Compare substrings without case
  170. #ifndef osStrncasecmp
  171. #include <string.h>
  172. #define osStrncasecmp(s1, s2, length) strncasecmp(s1, s2, length)
  173. #endif
  174. //Search for the first occurrence of a given character
  175. #ifndef osStrchr
  176. #include <string.h>
  177. #define osStrchr(s, c) strchr(s, c)
  178. #endif
  179. //Search for the first occurrence of a substring
  180. #ifndef osStrstr
  181. #include <string.h>
  182. #define osStrstr(s1, s2) strstr(s1, s2)
  183. #endif
  184. //Copy string
  185. #ifndef osStrcpy
  186. #include <string.h>
  187. #define osStrcpy(s1, s2) (void) strcpy(s1, s2)
  188. #endif
  189. //Copy characters from string
  190. #ifndef osStrncpy
  191. #include <string.h>
  192. #define osStrncpy(s1, s2, length) (void) strncpy(s1, s2, length)
  193. #endif
  194. //Concatenate strings
  195. #ifndef osStrcat
  196. #include <string.h>
  197. #define osStrcat(s1, s2) (void) strcat(s1, s2)
  198. #endif
  199. //Extract tokens from string
  200. #ifndef osStrtok_r
  201. // #include <string.h>
  202. // #define osStrtok_r(s, delim, last) strtok_r(s, delim, last)
  203. #endif
  204. //Format string
  205. #ifndef osSprintf
  206. // #include <stdio.h>
  207. // #define osSprintf(dest, ...) sprintf(dest, __VA_ARGS__)
  208. #endif
  209. //Format string
  210. #ifndef osSnprintf
  211. // #include <stdio.h>
  212. // #define osSnprintf(dest, size, ...) snprintf(dest, size, __VA_ARGS__)
  213. #endif
  214. //Format string
  215. #ifndef osVsnprintf
  216. // #include <stdio.h>
  217. // #define osVsnprintf(dest, size, format, ap) vsnprintf(dest, size, format, ap)
  218. #endif
  219. //Convert string to unsigned long integer
  220. #ifndef osStrtoul
  221. // #include <stdlib.h>
  222. // #define osStrtoul(s, endptr, base) strtoul(s, endptr, base)
  223. #endif
  224. //Convert string to unsigned long long integer
  225. #ifndef osStrtoull
  226. // #include <stdlib.h>
  227. // #define osStrtoull(s, endptr, base) strtoull(s, endptr, base)
  228. #endif
  229. //Convert a character to lowercase
  230. #ifndef osTolower
  231. #include <ctype.h>
  232. #define osTolower(c) tolower((uint8_t) (c))
  233. #endif
  234. //Convert a character to uppercase
  235. #ifndef osToupper
  236. #include <ctype.h>
  237. #define osToupper(c) toupper((uint8_t) (c))
  238. #endif
  239. //Check if a character is an uppercase letter
  240. #ifndef osIsupper
  241. #include <ctype.h>
  242. #define osIsupper(c) isupper((uint8_t) (c))
  243. #endif
  244. //Check if a character is a decimal digit
  245. #ifndef osIsdigit
  246. #include <ctype.h>
  247. #define osIsdigit(c) isdigit((uint8_t) (c))
  248. #endif
  249. //Check if a character is a whitespace character
  250. #ifndef osIsspace
  251. #include <ctype.h>
  252. #define osIsspace(c) isspace((uint8_t) (c))
  253. #endif
  254. //Check if a character is a blank character
  255. #ifndef osIsblank
  256. #define osIsblank(c) ((c) == ' ' || (c) == '\t')
  257. #endif
  258. #if !defined(__linux__) && !defined(__FreeBSD__)
  259. //Delay routines
  260. #ifndef usleep
  261. #define usleep(delay) {volatile uint32_t n = delay * 4; while(n > 0) n--;}
  262. #endif
  263. #ifndef sleep
  264. #define sleep(delay) {volatile uint32_t n = delay * 4000; while(n > 0) n--;}
  265. #endif
  266. #endif
  267. //Task object (deprecated)
  268. #define OsTask void
  269. //Invalid handle value (deprecated)
  270. #define OS_INVALID_HANDLE OS_INVALID_TASK_ID
  271. #endif