types.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
  21. * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. * -
  33. * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  34. *
  35. * This file is distributed in the hope that it will be useful, but WITHOUT
  36. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  37. * FITNESS FOR A PARTICULAR PURPOSE.
  38. *
  39. * You can redistribute this file and/or modify it under the terms of the GNU
  40. * General Public License (GPL) as published by the Free Software Foundation;
  41. * either version 2 of the License, or (at your discretion) any later version.
  42. * See the accompanying file "copying-gpl.txt" for more details.
  43. *
  44. * As a special exception to the GPL, permission is granted for additional
  45. * uses of the text contained in this file. See the accompanying file
  46. * "copying-liquorice.txt" for details.
  47. * -
  48. * Portions Copyright (c) 1983, 1993 by
  49. * The Regents of the University of California. All rights reserved.
  50. *
  51. * Redistribution and use in source and binary forms, with or without
  52. * modification, are permitted provided that the following conditions
  53. * are met:
  54. * 1. Redistributions of source code must retain the above copyright
  55. * notice, this list of conditions and the following disclaimer.
  56. * 2. Redistributions in binary form must reproduce the above copyright
  57. * notice, this list of conditions and the following disclaimer in the
  58. * documentation and/or other materials provided with the distribution.
  59. * 3. Neither the name of the University nor the names of its contributors
  60. * may be used to endorse or promote products derived from this software
  61. * without specific prior written permission.
  62. *
  63. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  64. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  67. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  68. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  69. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  70. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  71. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  73. * SUCH DAMAGE.
  74. */
  75. /*
  76. * $Log: types.h,v $
  77. * Revision 1.12 2006/10/08 16:48:22 haraldkipp
  78. * Documentation fixed
  79. *
  80. * Revision 1.11 2006/03/27 09:34:05 freckle
  81. * added u_longlong and longlong for emulation
  82. *
  83. * Revision 1.10 2006/03/02 20:04:11 haraldkipp
  84. * ICCARM doesn't know __PTRDIFF_TYPE__.
  85. *
  86. * Revision 1.9 2005/07/26 15:49:59 haraldkipp
  87. * Cygwin support added.
  88. *
  89. * Revision 1.8 2005/04/08 12:46:46 freckle
  90. * removed htons, htonl, ntohs, ntohs, ntohl, from unix emulation as
  91. * provided by system headers somewhere
  92. *
  93. * Revision 1.7 2005/02/10 07:06:48 hwmaier
  94. * Changes to incorporate support for AT90CAN128 CPU
  95. *
  96. * Revision 1.6 2004/06/08 15:04:24 freckle
  97. * changed #include "types_orig.h" to <sys/types_orig.h>
  98. * to allow this file to be in $MODDIR. Relevant only for *nix emulation
  99. *
  100. * Revision 1.5 2004/04/20 07:58:32 drsung
  101. * Use __GNUC__ instead of __GCC__
  102. *
  103. * Revision 1.4 2004/04/07 12:13:57 haraldkipp
  104. * Matthias Ringwald's *nix emulation added
  105. *
  106. * Revision 1.3 2004/03/18 15:40:55 haraldkipp
  107. * ICCAVR failed to compile
  108. *
  109. * Revision 1.2 2004/03/16 16:48:44 haraldkipp
  110. * Added Jan Dubiec's H8/300 port.
  111. *
  112. * Revision 1.1.1.1 2003/05/09 14:41:23 haraldkipp
  113. * Initial using 3.2.1
  114. *
  115. * Revision 1.8 2003/02/04 18:00:54 harald
  116. * Version 3 released
  117. *
  118. * Revision 1.7 2002/08/08 17:25:12 harald
  119. * *** empty log message ***
  120. *
  121. * Revision 1.6 2002/06/26 17:29:30 harald
  122. * First pre-release with 2.4 stack
  123. *
  124. */
  125. #ifndef _SYS_TYPES_H_
  126. #ifndef _SYS_VIRTUAL_TYPES_H_
  127. #define _SYS_VIRTUAL_TYPES_H_
  128. #if defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
  129. // on an emulation platform, we need to have both
  130. // a) the native types headers and libs and
  131. #include <sys/types_orig.h>
  132. // b) the additional nut os header and implementation
  133. /*! \brief 64-bit values */
  134. typedef unsigned long long u_longlong;
  135. typedef long long longlong;
  136. #endif
  137. #ifndef _SYS_TYPES_H
  138. #define _SYS_TYPES_H_
  139. #endif
  140. /*!
  141. * \file sys/types.h
  142. * \brief Nut/OS type declarations.
  143. */
  144. #ifdef __cplusplus
  145. extern "C" {
  146. #endif
  147. #include <compiler.h>
  148. /*!
  149. * \weakgroup xgNutOS
  150. */
  151. /*@{*/
  152. #if !defined(__linux__) && !defined(__APPLE__) && !defined(__CYGWIN__)
  153. /*! \brief Unsigned 8-bit value.
  154. *
  155. * \todo We should switch to stdint.h as suggested by Dirk Kaufmann. See
  156. * feature request #1282721.
  157. */
  158. typedef unsigned char u_char;
  159. /*! \brief Unsigned 16-bit value. */
  160. typedef unsigned short u_short;
  161. /*! \brief Unsigned int value. */
  162. /* Warning: size is highly architecture/compiler dependant! */
  163. typedef unsigned int u_int;
  164. /*! \brief Unsigned 32-bit value */
  165. typedef unsigned long u_long;
  166. /*! \brief Unsigned 64-bit value */
  167. typedef unsigned long long u_longlong;
  168. #endif /* unix emulation */
  169. /*! \brief Void pointer */
  170. typedef void *HANDLE;
  171. /*!
  172. * \brief Unsigned register type.
  173. *
  174. * The size of this type is equal to the size of a register,
  175. * the hardware datapath or whatever might fit to give optimum
  176. * performance for values from 0 to 255.
  177. *
  178. * Typically 8 bit CPUs will use unsigned characters, 16 bit
  179. * CPUs will use unsigned shorts etc.
  180. */
  181. #if defined(__AVR__)
  182. typedef unsigned char ureg_t;
  183. #elif defined(__arm__)
  184. typedef unsigned short ureg_t;
  185. #elif defined(__H8300__) || defined(__H8300H__) || defined(__H8300S__)
  186. typedef unsigned short ureg_t;
  187. #elif defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
  188. typedef unsigned short ureg_t;
  189. #elif defined(__m68k__)
  190. typedef unsigned short ureg_t;
  191. #endif
  192. /*!
  193. * \brief Signed register type.
  194. *
  195. * Similar to ureg_t, but for signed values from -128 to +127.
  196. */
  197. #if defined(__AVR__)
  198. typedef unsigned char reg_t;
  199. #elif defined(__arm__)
  200. typedef unsigned short reg_t;
  201. #elif defined(__H8300__) || defined(__H8300H__) || defined(__H8300S__)
  202. typedef unsigned short reg_t;
  203. #elif defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
  204. typedef unsigned short reg_t;
  205. #elif defined(__m68k__)
  206. typedef unsigned short reg_t;
  207. #endif
  208. /*!
  209. * \brief Unsigned pointer value type.
  210. *
  211. * The size of this type is at least the size of a memory pointer.
  212. * For CPUs with 16 address bits this will be an unsigned short.
  213. */
  214. #if defined(__AVR__)
  215. typedef unsigned short uptr_t;
  216. #elif defined(__GNUC__)
  217. /*
  218. * For remaining MCUs GCC is assumed where __PTRDIFF_TYPE__ macro is defined
  219. */
  220. typedef unsigned __PTRDIFF_TYPE__ uptr_t;
  221. #else
  222. typedef unsigned int uptr_t;
  223. #endif
  224. #define __byte_swap2(val) \
  225. ((((val) & 0xff) << 8) | \
  226. (((val) & 0xff00) >> 8))
  227. #define __byte_swap4(val) \
  228. ((((val) & 0xff) << 24) | \
  229. (((val) & 0xff00) << 8) | \
  230. (((val) & 0xff0000) >> 8) | \
  231. (((val) & 0xff000000) >> 24))
  232. #if defined(__GNUC__) && defined(__AVR__)
  233. /*
  234. * Conversion of 16 bit value to network order.
  235. */
  236. #undef __byte_swap2
  237. static inline u_short __byte_swap2(u_short val) {
  238. asm volatile ("mov __tmp_reg__, %A0\n\t" "mov %A0, %B0\n\t" "mov %B0, __tmp_reg__\n\t":"=r" (val)
  239. :"0"(val)
  240. );
  241. return val;
  242. }
  243. /*
  244. * Conversion of 32 bit value to network order.
  245. */
  246. #undef __byte_swap4
  247. static inline u_long __byte_swap4(u_long val) {
  248. asm volatile ("mov __tmp_reg__, %A0\n\t"
  249. "mov %A0, %D0\n\t"
  250. "mov %D0, __tmp_reg__\n\t" "mov __tmp_reg__, %B0\n\t" "mov %B0, %C0\n\t" "mov %C0, __tmp_reg__\n\t":"=r" (val)
  251. :"0"(val)
  252. );
  253. return val;
  254. }
  255. #endif /* #if defined(__GCC__) && defined(__AVR__) */
  256. #if !defined(__linux__) && !defined(__APPLE__) && !defined(__CYGWIN__) /* provided by system libraries */
  257. /*!
  258. * \brief Convert short value from host to network byte order.
  259. */
  260. #ifndef __BIG_ENDIAN__
  261. #define htons(x) __byte_swap2(x)
  262. #else
  263. #define htons(x) (x)
  264. #endif
  265. /*!
  266. * \brief Convert long value from host to network byte order.
  267. */
  268. #ifndef __BIG_ENDIAN__
  269. #define htonl(x) __byte_swap4(x)
  270. #else
  271. #define htonl(x) (x)
  272. #endif
  273. /*!
  274. * \brief Convert short value from network to host byte order.
  275. */
  276. #ifndef __BIG_ENDIAN__
  277. #define ntohs(x) __byte_swap2(x)
  278. #else
  279. #define ntohs(x) (x)
  280. #endif
  281. /*!
  282. * \brief Convert long value from network to host byte order.
  283. */
  284. #ifndef __BIG_ENDIAN__
  285. #define ntohl(x) __byte_swap4(x)
  286. #else
  287. #define ntohl(x) (x)
  288. #endif
  289. #endif /* network to host byte conversion */
  290. /*@}*/
  291. #ifdef __cplusplus
  292. }
  293. #endif
  294. #endif /* #ifndef _SYS_VIRTUAL_TYPES_H_ */
  295. #endif /* #ifndef _SYS_TYPES_H_ */