hv_vos_Conn.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * @file hv_vos_Conn.h
  3. * @brief Header file of connect.
  4. *
  5. * @verbatim
  6. * ==============================================================================
  7. * ##### How to use #####
  8. * ==============================================================================
  9. * (+) Use ()
  10. *
  11. * @endverbatim
  12. * @author HiView SoC Software Team
  13. * @version 1.0.0
  14. * @date 2023-03-01
  15. */
  16. /** @file vos_conn.h
  17. * @brief VOS board connection functions
  18. */
  19. #ifndef _HV_VOS_CONN_H
  20. #define _HV_VOS_CONN_H
  21. #include "hv_vos_Types.h"
  22. #include "hv_vos_Macro.h"
  23. #include "hv_vos_Endian.h"
  24. #include "hv_vos_Log.h"
  25. VOS_EXTERN_C_BEGIN
  26. /** @brief forward declaration of connection type */
  27. typedef struct _HV_VOS_CONN_S Hv_Vos_CONN_S;
  28. /** @defgroup VOS_CONN VOS_CONN
  29. * VOS board connection functions
  30. * @{
  31. */
  32. /** @brief board connection type */
  33. typedef enum _HV_VOS_CONN_TYPE_E {
  34. HV_VOS_CONN_SIM, //!< simulate connection
  35. HV_VOS_CONN_ETHERNET, //!< connect via ETHERNET
  36. HV_VOS_CONN_I2C, //!< connect via I2C, need unlock
  37. HV_VOS_CONN_DSUI2C, //!< connect via normal I2C
  38. HV_VOS_CONN_AUTO, //<! got connection from ChipDebugger.ini
  39. HV_VOS_CONN_DSUSPI, //!< connect via debug spi
  40. } Hv_Vos_CONN_TYPE_E;
  41. /// debug flag
  42. typedef enum _HV_VOS_CONN_DEBUG_FLAG {
  43. HV_VOS_CONN_DEBUG_NONE = 0,
  44. HV_VOS_CONN_DEBUG_READ = 1,
  45. HV_VOS_CONN_DEBUG_WRITE = 2,
  46. HV_VOS_CONN_DEBUG_READ_WRITE = 3,
  47. } HV_VOS_CONN_DEBUG_FLAG;
  48. #define Hv_Vos_CONN_Open(type, connectString) (NULL)
  49. #define Hv_Vos_CONN_Close(self)
  50. #define Hv_Vos_CONN_IsConnected(self) (HV_TRUE)
  51. #define Hv_Vos_CONN_SetDebug(self, flag)
  52. #ifndef HV_PHY_ADDR
  53. #define HV_PHY_ADDR(addr) ((intptr_t)(addr))
  54. #endif
  55. /* little endian functions */
  56. #define HV_R8(addr) (*(volatile UCHAR8*)(HV_PHY_ADDR(addr)))
  57. #define HV_W8(addr, value) (*(volatile UCHAR8*)(HV_PHY_ADDR(addr)) = (value))
  58. INLINE void HV_W8_MASK(UINT32 addr, UCHAR8 mask, UCHAR8 value){
  59. UCHAR8 val = HV_R8(addr);
  60. val = HV_MASK_ASSIGN(val, mask, value);
  61. HV_W8(addr, val);
  62. }
  63. /* big endian functions */
  64. #define HV_R8_BE(addr) HV_R8(addr)
  65. #define HV_W8_BE(addr, value) HV_W8_BE(addr, value)
  66. #define HV_W8_BE_MASK(addr, mask, value) HV_W8_MASK(addr, mask, value)
  67. #ifdef HV_CONFIG_ENDIAN_LITTLE
  68. /* Access LE registers on LE CPU */
  69. #define HV_R16(addr) (*(volatile USHORT16*)(HV_PHY_ADDR(addr)))
  70. #define HV_W16(addr, value) (*(volatile USHORT16*)(HV_PHY_ADDR(addr)) = (value))
  71. /* Access BE registers on LE CPU */
  72. #define HV_R16_BE(addr) HV_SWAP16(*(volatile USHORT16*)(HV_PHY_ADDR(addr)))
  73. #define HV_W16_BE(addr, value) (*(volatile USHORT16*)(HV_PHY_ADDR(addr)) = HV_SWAP16(value))
  74. #else
  75. /* Access LE registers on BE CPU */
  76. #define HV_R16(addr) HV_SWAP16((*(volatile USHORT16*)(HV_PHY_ADDR(addr))))
  77. #define HV_W16(addr, value) (*(volatile USHORT16*)(HV_PHY_ADDR(addr)) = HV_SWAP16(value))
  78. /* Access BE registers on BE CPU */
  79. #define HV_R16_BE(addr) (*(volatile USHORT16*)(HV_PHY_ADDR(addr)))
  80. #define HV_W16_BE(addr, value) (*(volatile USHORT16*)(HV_PHY_ADDR(addr)) = (value))
  81. #endif
  82. INLINE void HV_W16_MASK(USHORT16 addr, USHORT16 mask, USHORT16 value){
  83. USHORT16 val = HV_R16(addr);
  84. val = HV_MASK_ASSIGN(val, mask, value);
  85. HV_W16(addr, val);
  86. }
  87. INLINE void HV_W16_BE_MASK(USHORT16 addr, USHORT16 mask, USHORT16 value){
  88. USHORT16 val = HV_R16_BE(addr);
  89. val = HV_MASK_ASSIGN(val, mask, value);
  90. HV_W16_BE(addr, val);
  91. }
  92. #ifdef HV_CONFIG_ENDIAN_LITTLE
  93. /* Access LE registers on LE CPU */
  94. #define HV_R32(addr) (*(volatile UINT32*)(HV_PHY_ADDR(addr)))
  95. #define HV_W32(addr, value) (*(volatile UINT32*)(HV_PHY_ADDR(addr)) = (value))
  96. /* Access BE registers on LE CPU */
  97. #define HV_R32_BE(addr) HV_SWAP32(*(volatile UINT32*)(HV_PHY_ADDR(addr)))
  98. #define HV_W32_BE(addr, value) (*(volatile UINT32*)(HV_PHY_ADDR(addr)) = HV_SWAP32(value))
  99. #else
  100. /* Access LE registers on BE CPU */
  101. #define HV_R32(addr) (HV_SWAP32(*(volatile UINT32*)(addr)))
  102. #define HV_W32(addr, value) (*(volatile UINT32*)(addr)) = HV_SWAP32(value)
  103. /* Access BE registers on BE CPU */
  104. #define HV_R32_BE(addr) (*(volatile UINT32*)(HV_PHY_ADDR(addr)))
  105. #define HV_W32_BE(addr, value) (*(volatile UINT32*)(HV_PHY_ADDR(addr)) = (value))
  106. #endif
  107. INLINE void HV_W32_MASK(UINT32 addr, UINT32 mask, UINT32 value){
  108. UINT32 val = HV_R32(addr);
  109. val = HV_MASK_ASSIGN(val, mask, value);
  110. HV_W32(addr, val);
  111. }
  112. INLINE void HV_W32_BE_MASK(UINT32 addr, UINT32 mask, UINT32 value){
  113. UINT32 val = HV_R32_BE(addr);
  114. val = HV_MASK_ASSIGN(val, mask, value);
  115. HV_W32_BE(addr, val);
  116. }
  117. void HV_WRITE_MEM(UINT32 addr, const UCHAR8 *data, UINT32 size);
  118. void HV_READ_MEM(UINT32 addr, UCHAR8 *data, UINT32 size);
  119. /// define a variable for HV_R8_FIELD
  120. #define HV_R8_DECL_VAR() UCHAR8 _reg_val
  121. /// read register field, need HV_R8_DECL_VAR first
  122. #define HV_R8_FIELD(addr, field) \
  123. (_reg_val = HV_R8(addr), ((addr##_struct*)(void*)&(_reg_val))->field)
  124. /// read register field
  125. #define HV_R8_FIELD_EX(addr, base, field) \
  126. (_reg_val = HV_R8(addr + base), ((addr##_struct*)&(_reg_val))->field)
  127. #define HV_W8_FIELD(addr, field, value) do { \
  128. UCHAR8 _val = HV_R8(addr); \
  129. ((addr##_struct*)&(_val))->field = value; \
  130. HV_W8(addr, _val); \
  131. } while (0)
  132. #define HV_W8_FIELD_EX(addr, base, field, value) do { \
  133. UCHAR8 _val = HV_R8(addr + base); \
  134. ((addr##_struct*)&(_val))->field = value; \
  135. HV_W8(addr + base, _val); \
  136. } while (0)
  137. /// read register field, need HV_R8_DECL_VAR first
  138. #define HV_R8_BE_FIELD(addr, field) \
  139. (_reg_val = HV_R8_BE(addr), ((addr##_struct*)(void*)&(_reg_val))->field)
  140. /// read register field
  141. #define HV_R8_BE_FIELD_EX(addr, base, field) \
  142. (_reg_val = HV_R8_BE(addr + base), ((addr##_struct*)&(_reg_val))->field)
  143. #define HV_W8_BE_FIELD(addr, field, value) do { \
  144. UCHAR8 _val = HV_R8_BE(addr); \
  145. ((addr##_struct*)&(_val))->field = value; \
  146. HV_W8_BE(addr, _val); \
  147. } while (0)
  148. #define HV_W8_FIELD_EX(addr, base, field, value) do { \
  149. UCHAR8 _val = HV_R8(addr + base); \
  150. ((addr##_struct*)&(_val))->field = value; \
  151. HV_W8(addr + base, _val); \
  152. } while (0)
  153. /// define a variable for HV_R16_FIELD
  154. #define HV_R16_DECL_VAR() USHORT16 _reg_val
  155. /// read register field, need HV_R16_DECL_VAR first
  156. #define HV_R16_FIELD(addr, field) \
  157. (_reg_val = HV_R16(addr), ((addr##_struct*)(void*)&(_reg_val))->field)
  158. /// read register field
  159. #define HV_R16_FIELD_EX(addr, base, field) \
  160. (_reg_val = HV_R16(addr + base), ((addr##_struct*)&(_reg_val))->field)
  161. #define HV_W16_FIELD(addr, field, value) do { \
  162. UCHAR8 _val = HV_R16(addr); \
  163. ((addr##_struct*)&(_val))->field = value; \
  164. HV_W16(addr, _val); \
  165. } while (0)
  166. #define HV_W16_FIELD_EX(addr, base, field, value) do { \
  167. USHORT16 _val = HV_R16(addr + base); \
  168. ((addr##_struct*)&(_val))->field = value; \
  169. HV_W16(addr + base, _val); \
  170. } while (0)
  171. /// read register field, need HV_R16_DECL_VAR first
  172. #define HV_R16_BE_FIELD(addr, field) \
  173. (_reg_val = HV_R16_BE(addr), ((addr##_struct*)(void*)&(_reg_val))->field)
  174. /// read register field
  175. #define HV_R16_BE_FIELD_EX(addr, base, field) \
  176. (_reg_val = HV_R16_BE(addr + base), ((addr##_struct*)&(_reg_val))->field)
  177. #define HV_W16_BE_FIELD(addr, field, value) do { \
  178. UCHAR8 _val = HV_R16_BE(addr); \
  179. ((addr##_struct*)&(_val))->field = value; \
  180. HV_W16_BE(addr, _val); \
  181. } while (0)
  182. #define HV_W16_BE_FIELD_EX(addr, base, field, value) do { \
  183. USHORT16 _val = HV_R16_BE(addr + base); \
  184. ((addr##_struct*)&(_val))->field = value; \
  185. HV_W16_BE(addr + base, _val); \
  186. } while (0)
  187. /// define a variable for HV_R32_FIELD
  188. #define HV_R32_DECL_VAR() UINT32 _reg_val
  189. /// read register field, need HV_R32_DECL_VAR first
  190. #define HV_R32_FIELD(addr, field) \
  191. (_reg_val = HV_R32(addr), ((addr##_struct*)(void*)&(_reg_val))->field)
  192. /// read register field
  193. #define HV_R32_FIELD_EX(addr, base, field) \
  194. (_reg_val = HV_R32(addr + base), ((addr##_struct*)&(_reg_val))->field)
  195. #define HV_W32_FIELD(addr, field, value) do { \
  196. UINT32 _val = HV_R32(addr); \
  197. ((addr##_struct*)&(_val))->field = value; \
  198. HV_W32(addr, _val); \
  199. } while (0)
  200. #define HV_W32_FIELD_EX(addr, base, field, value) do { \
  201. UINT32 _val = HV_R32(addr + base); \
  202. ((addr##_struct*)&(_val))->field = value; \
  203. HV_W32(addr + base, _val); \
  204. } while (0)
  205. /// read register field, need HV_R32_DECL_VAR first
  206. #define HV_R32_BE_FIELD(addr, field) \
  207. (_reg_val = HV_R32_BE(addr), ((addr##_struct*)(void*)&(_reg_val))->field)
  208. /// read register field
  209. #define HV_R32_BE_FIELD_EX(addr, base, field) \
  210. (_reg_val = HV_R32_BE(addr + base), ((addr##_struct*)&(_reg_val))->field)
  211. #define HV_W32_BE_FIELD(addr, field, value) do { \
  212. UINT32 _val = HV_R32_BE(addr); \
  213. ((addr##_struct*)&(_val))->field = value; \
  214. HV_W32_BE(addr, _val); \
  215. } while (0)
  216. #define HV_W32_BE_FIELD_EX(addr, base, field, value) do { \
  217. UINT32 _val = HV_R32_BE(addr + base); \
  218. ((addr##_struct*)&(_val))->field = value; \
  219. HV_W32_BE(addr + base, _val); \
  220. } while (0)
  221. /// convert register structure to UINT32
  222. #define HV_REG_VAL(reg) (*(UINT32*)&(reg))
  223. /// express way to write a register struct data value
  224. #define HV_W32_REG(addr, reg) HV_W32(addr, HV_REG_VAL(reg))
  225. #define HV_W32_BE_REG(addr, reg) HV_W32_BE(addr, HV_REG_VAL(reg))
  226. /// express way to read register to struct data value
  227. #define HV_R32_REG(addr, reg) HV_REG_VAL(reg) = HV_R32(addr)
  228. #define HV_R32_BE_REG(addr, reg) HV_REG_VAL(reg) = HV_R32_BE(addr)
  229. #define HV_W32_SET_BIT(addr, bit) HV_W32_MASK(addr, 1 << (bit), 1 << (bit))
  230. #define HV_W32_CLR_BIT(addr, bit) HV_W32_MASK(addr, 1 << (bit), 0x00000000)
  231. #define HV_R32_GET_BIT(addr, bit) ((HV_R32(addr) >> bit) & 1)
  232. #define HV_W_FEILD_REG(addr, startbit,endbit, reg) do{ \
  233. UINT32 _val = HV_R32(addr); \
  234. if((endbit-startbit) != 31) \
  235. { \
  236. UINT32 _mask = ~(0xFFFFFFFF << (endbit - startbit + 1)) << startbit;\
  237. _val &= ~_mask; \
  238. _val |= (reg<<startbit)&_mask; \
  239. HV_W32(addr,_val); \
  240. } \
  241. else \
  242. { \
  243. HV_W32(addr,reg); \
  244. } \
  245. } while (0)
  246. /** @} */
  247. VOS_EXTERN_C_END
  248. #endif