hal_defs.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /**************************************************************************************************
  29. Filename: hal_defs.h
  30. Revised:
  31. Revision:
  32. Description: This file contains useful macros and data types
  33. **************************************************************************************************/
  34. #ifndef HAL_DEFS_H
  35. #define HAL_DEFS_H
  36. /* ------------------------------------------------------------------------------------------------
  37. * Macros
  38. * ------------------------------------------------------------------------------------------------
  39. */
  40. #ifndef BV
  41. #define BV(n) (1 << (n))
  42. #endif
  43. #ifndef BF
  44. #define BF(x,b,s) (((x) & (b)) >> (s))
  45. #endif
  46. #ifndef MIN
  47. #define MIN(n,m) (((n) < (m)) ? (n) : (m))
  48. #endif
  49. #ifndef MAX
  50. #define MAX(n,m) (((n) < (m)) ? (m) : (n))
  51. #endif
  52. #ifndef ABS
  53. #define ABS(n) (((n) < 0) ? -(n) : (n))
  54. #endif
  55. /* takes a byte out of a uint32 : var - uint32, ByteNum - byte to take out (0 - 3) */
  56. #define BREAK_UINT32( var, ByteNum ) \
  57. (uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF))
  58. #define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
  59. ((uint32)((uint32)((Byte0) & 0x00FF) \
  60. + ((uint32)((Byte1) & 0x00FF) << 8) \
  61. + ((uint32)((Byte2) & 0x00FF) << 16) \
  62. + ((uint32)((Byte3) & 0x00FF) << 24)))
  63. #define BUILD_UINT16(loByte, hiByte) \
  64. ((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
  65. #define HI_UINT16(a) (((a) >> 8) & 0xFF)
  66. #define LO_UINT16(a) ((a) & 0xFF)
  67. #define BUILD_UINT8(hiByte, loByte) \
  68. ((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))
  69. #define HI_UINT8(a) (((a) >> 4) & 0x0F)
  70. #define LO_UINT8(a) ((a) & 0x0F)
  71. // Write the 32bit value of 'val' in little endian format to the buffer pointed
  72. // to by pBuf, and increment pBuf by 4
  73. #define UINT32_TO_BUF_LITTLE_ENDIAN(pBuf,val) \
  74. do { \
  75. *(pBuf)++ = (((val) >> 0) & 0xFF); \
  76. *(pBuf)++ = (((val) >> 8) & 0xFF); \
  77. *(pBuf)++ = (((val) >> 16) & 0xFF); \
  78. *(pBuf)++ = (((val) >> 24) & 0xFF); \
  79. } while (0)
  80. // Return the 32bit little-endian formatted value pointed to by pBuf, and increment pBuf by 4
  81. #define BUF_TO_UINT32_LITTLE_ENDIAN(pBuf) (((pBuf) += 4), BUILD_UINT32((pBuf)[-4], (pBuf)[-3], (pBuf)[-2], (pBuf)[-1]))
  82. #ifndef GET_BIT
  83. #define GET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] & BV((IDX) % 8)) ? TRUE : FALSE)
  84. #endif
  85. #ifndef SET_BIT
  86. #define SET_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] |= BV((IDX) % 8)))
  87. #endif
  88. #ifndef CLR_BIT
  89. #define CLR_BIT(DISCS, IDX) (((DISCS)[((IDX) / 8)] &= (BV((IDX) % 8) ^ 0xFF)))
  90. #endif
  91. /*
  92. * This macro is for use by other macros to form a fully valid C statement.
  93. * Without this, the if/else conditionals could show unexpected behavior.
  94. *
  95. * For example, use...
  96. * #define SET_REGS() st( ioreg1 = 0; ioreg2 = 0; )
  97. * instead of ...
  98. * #define SET_REGS() { ioreg1 = 0; ioreg2 = 0; }
  99. * or
  100. * #define SET_REGS() ioreg1 = 0; ioreg2 = 0;
  101. * The last macro would not behave as expected in the if/else construct.
  102. * The second to last macro will cause a compiler error in certain uses
  103. * of if/else construct
  104. *
  105. * It is not necessary, or recommended, to use this macro where there is
  106. * already a valid C statement. For example, the following is redundant...
  107. * #define CALL_FUNC() st( func(); )
  108. * This should simply be...
  109. * #define CALL_FUNC() func()
  110. *
  111. * (The while condition below evaluates false without generating a
  112. * constant-controlling-loop type of warning on most compilers.)
  113. */
  114. #define st(x) do { x } while (__LINE__ == -1)
  115. #define HAL_WAIT_CONDITION(condition) {while(!(condition)){}}
  116. #define HAL_WAIT_CONDITION_TIMEOUT(condition, timeout) {\
  117. volatile int val = 0;\
  118. while(!(condition)){\
  119. if(val ++ > timeout)\
  120. return PPlus_ERR_TIMEOUT;\
  121. }\
  122. }
  123. #define HAL_WAIT_CONDITION_TIMEOUT_WO_RETURN(condition, timeout) {\
  124. volatile int val = 0;\
  125. while(!(condition)){\
  126. if(val ++ > timeout)\
  127. break;\
  128. }\
  129. }
  130. /**************************************************************************************************
  131. */
  132. #endif