utils.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Copyright 2018 Canaan Inc.
  2. *
  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. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _DRIVER_UTILS_H
  16. #define _DRIVER_UTILS_H
  17. #ifdef __cplusplus
  18. #include <cstdbool>
  19. #include <cstddef>
  20. #include <cstdint>
  21. #else /* __cplusplus */
  22. #include <stdbool.h>
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #endif /* __cplusplus */
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /* __cplusplus */
  30. #define KENDRYTE_MIN(a, b) ((a) > (b) ? (b) : (a))
  31. #define KENDRYTE_MAX(a, b) ((a) > (b) ? (a) : (b))
  32. #define FIX_CACHE 1
  33. #ifdef __ASSEMBLY__
  34. #define KENDRYTE_CAST(type, ptr) ptr
  35. #else /* __ASSEMBLY__ */
  36. /**
  37. * @brief Cast the pointer to specified pointer type.
  38. *
  39. * @param[in] type The pointer type to cast to
  40. * @param[in] ptr The pointer to apply the type cast to
  41. */
  42. #define KENDRYTE_CAST(type, ptr) ((type)(ptr))
  43. #endif /* __ASSEMBLY__ */
  44. /**
  45. * @addtogroup UTIL_RW_FUNC Memory Read/Write Utilities
  46. *
  47. * This section implements read and write functionality for various
  48. * memory untis. The memory unit terms used for these functions are
  49. * consistent with those used in the ARM Architecture Reference Manual
  50. * ARMv7-A and ARMv7-R edition manual. The terms used for units of memory are:
  51. *
  52. * Unit of Memory | Abbreviation | Size in Bits
  53. * :---------------|:-------------|:------------:
  54. * Byte | byte | 8
  55. * Half Word | hword | 16
  56. * Word | word | 32
  57. * Double Word | dword | 64
  58. *
  59. */
  60. /**
  61. * @brief Write the 8 bit byte to the destination address in device memory.
  62. *
  63. * @param[in] dest Write destination pointer address
  64. * @param[in] src 8 bit data byte to write to memory
  65. */
  66. #define kendryte_write_byte(dest, src) \
  67. (*KENDRYTE_CAST(volatile uint8_t *, (dest)) = (src))
  68. /**
  69. * @brief Read and return the 8 bit byte from the source address in device memory.
  70. *
  71. * @param[in] src Read source pointer address
  72. *
  73. * @return 8 bit data byte value
  74. */
  75. #define kendryte_read_byte(src) (*KENDRYTE_CAST(volatile uint8_t *, (src)))
  76. /**
  77. * @brief Write the 16 bit half word to the destination address in device memory.
  78. *
  79. * @param[in] dest Write destination pointer address
  80. * @param[in] src 16 bit data half word to write to memory
  81. */
  82. #define kendryte_write_hword(dest, src) \
  83. (*KENDRYTE_CAST(volatile uint16_t *, (dest)) = (src))
  84. /**
  85. * @brief Read and return the 16 bit half word from the source address in device
  86. *
  87. * @param[in] src Read source pointer address
  88. *
  89. * @return 16 bit data half word value
  90. */
  91. #define kendryte_read_hword(src) (*KENDRYTE_CAST(volatile uint16_t *, (src)))
  92. /**
  93. * @brief Write the 32 bit word to the destination address in device memory.
  94. *
  95. * @param[in] dest Write destination pointer address
  96. * @param[in] src 32 bit data word to write to memory
  97. */
  98. #define kendryte_write_word(dest, src) \
  99. (*KENDRYTE_CAST(volatile uint32_t *, (dest)) = (src))
  100. /**
  101. * @brief Read and return the 32 bit word from the source address in device memory.
  102. *
  103. * @param[in] src Read source pointer address
  104. *
  105. * @return 32 bit data half word value
  106. */
  107. #define kendryte_read_word(src) (*KENDRYTE_CAST(volatile uint32_t *, (src)))
  108. /**
  109. * @brief Write the 64 bit double word to the destination address in device memory.
  110. *
  111. * @param[in] dest Write destination pointer address
  112. * @param[in] src 64 bit data word to write to memory
  113. */
  114. #define kendryte_write_dword(dest, src) \
  115. (*KENDRYTE_CAST(volatile uint64_t *, (dest)) = (src))
  116. /**
  117. * @brief Read and return the 64 bit double word from the source address in device
  118. *
  119. * @param[in] src Read source pointer address
  120. *
  121. * @return 64 bit data half word value
  122. */
  123. #define kendryte_read_dword(src) (*KENDRYTE_CAST(volatile uint64_t *, (src)))
  124. /**
  125. * @brief Set selected bits in the 8 bit byte at the destination address in device
  126. *
  127. * @param[in] dest Destination pointer address
  128. * @param[in] bits Bits to set in destination byte
  129. */
  130. #define kendryte_setbits_byte(dest, bits) \
  131. (kendryte_write_byte(dest, kendryte_read_byte(dest) | (bits)))
  132. /**
  133. * @brief Clear selected bits in the 8 bit byte at the destination address in device
  134. *
  135. * @param[in] dest Destination pointer address
  136. * @param[in] bits Bits to clear in destination byte
  137. */
  138. #define kendryte_clrbits_byte(dest, bits) \
  139. (kendryte_write_byte(dest, kendryte_read_byte(dest) & ~(bits)))
  140. /**
  141. * @brief Change or toggle selected bits in the 8 bit byte at the destination address
  142. *
  143. * @param[in] dest Destination pointer address
  144. * @param[in] bits Bits to change in destination byte
  145. */
  146. #define kendryte_xorbits_byte(dest, bits) \
  147. (kendryte_write_byte(dest, kendryte_read_byte(dest) ^ (bits)))
  148. /**
  149. * @brief Replace selected bits in the 8 bit byte at the destination address in device
  150. *
  151. * @param[in] dest Destination pointer address
  152. * @param[in] msk Bits to replace in destination byte
  153. * @param[in] src Source bits to write to cleared bits in destination byte
  154. */
  155. #define kendryte_replbits_byte(dest, msk, src) \
  156. (kendryte_write_byte(dest, (kendryte_read_byte(dest) & ~(msk)) | ((src) & (msk))))
  157. /**
  158. * @brief Set selected bits in the 16 bit halfword at the destination address in
  159. *
  160. * @param[in] dest Destination pointer address
  161. * @param[in] bits Bits to set in destination halfword
  162. */
  163. #define kendryte_setbits_hword(dest, bits) \
  164. (kendryte_write_hword(dest, kendryte_read_hword(dest) | (bits)))
  165. /**
  166. * @brief Clear selected bits in the 16 bit halfword at the destination address in
  167. *
  168. * @param[in] dest Destination pointer address
  169. * @param[in] bits Bits to clear in destination halfword
  170. */
  171. #define kendryte_clrbits_hword(dest, bits) \
  172. (kendryte_write_hword(dest, kendryte_read_hword(dest) & ~(bits)))
  173. /**
  174. * @brief Change or toggle selected bits in the 16 bit halfword at the destination
  175. *
  176. * @param[in] dest Destination pointer address
  177. * @param[in] bits Bits to change in destination halfword
  178. */
  179. #define kendryte_xorbits_hword(dest, bits) \
  180. (kendryte_write_hword(dest, kendryte_read_hword(dest) ^ (bits)))
  181. /**
  182. * @brief Replace selected bits in the 16 bit halfword at the destination address in
  183. *
  184. * @param[in] dest Destination pointer address
  185. * @param[in] msk Bits to replace in destination byte
  186. * @param[in] src Source bits to write to cleared bits in destination halfword
  187. */
  188. #define kendryte_replbits_hword(dest, msk, src) \
  189. (kendryte_write_hword(dest, (kendryte_read_hword(dest) & ~(msk)) | ((src) & (msk))))
  190. /**
  191. * @brief Set selected bits in the 32 bit word at the destination address in device
  192. *
  193. * @param[in] dest Destination pointer address
  194. * @param[in] bits Bits to set in destination word
  195. */
  196. #define kendryte_setbits_word(dest, bits) \
  197. (kendryte_write_word(dest, kendryte_read_word(dest) | (bits)))
  198. /**
  199. * @brief Clear selected bits in the 32 bit word at the destination address in device
  200. *
  201. * @param[in] dest Destination pointer address
  202. * @param[in] bits Bits to clear in destination word
  203. */
  204. #define kendryte_clrbits_word(dest, bits) \
  205. (kendryte_write_word(dest, kendryte_read_word(dest) & ~(bits)))
  206. /**
  207. * @brief Change or toggle selected bits in the 32 bit word at the destination address
  208. *
  209. * @param[in] dest Destination pointer address
  210. * @param[in] bits Bits to change in destination word
  211. */
  212. #define kendryte_xorbits_word(dest, bits) \
  213. (kendryte_write_word(dest, kendryte_read_word(dest) ^ (bits)))
  214. /**
  215. * @brief Replace selected bits in the 32 bit word at the destination address in
  216. *
  217. * @param[in] dest Destination pointer address
  218. * @param[in] msk Bits to replace in destination word
  219. * @param[in] src Source bits to write to cleared bits in destination word
  220. */
  221. #define kendryte_replbits_word(dest, msk, src) \
  222. (kendryte_write_word(dest, (kendryte_read_word(dest) & ~(msk)) | ((src) & (msk))))
  223. /**
  224. * @brief Set selected bits in the 64 bit doubleword at the destination address in
  225. *
  226. * @param[in] dest Destination pointer address
  227. * @param[in] bits Bits to set in destination doubleword
  228. */
  229. #define kendryte_setbits_dword(dest, bits) \
  230. (kendryte_write_dword(dest, kendryte_read_dword(dest) | (bits)))
  231. /**
  232. * @brief Clear selected bits in the 64 bit doubleword at the destination address in
  233. *
  234. * @param[in] dest Destination pointer address
  235. * @param[in] bits Bits to clear in destination doubleword
  236. */
  237. #define kendryte_clrbits_dword(dest, bits) \
  238. (kendryte_write_dword(dest, kendryte_read_dword(dest) & ~(bits)))
  239. /**
  240. * @brief Change or toggle selected bits in the 64 bit doubleword at the destination
  241. *
  242. * @param[in] dest Destination pointer address
  243. * @param[in] bits Bits to change in destination doubleword
  244. */
  245. #define kendryte_xorbits_dword(dest, bits) \
  246. (kendryte_write_dword(dest, kendryte_read_dword(dest) ^ (bits)))
  247. /**
  248. * @brief Replace selected bits in the 64 bit doubleword at the destination address in
  249. *
  250. * @param[in] dest Destination pointer address
  251. * @param[in] msk its to replace in destination doubleword
  252. * @param[in] src Source bits to write to cleared bits in destination word
  253. */
  254. #define kendryte_replbits_dword(dest, msk, src) \
  255. (kendryte_write_dword(dest, (kendryte_read_dword(dest) & ~(msk)) | ((src) & (msk))))
  256. #define configASSERT(x) \
  257. if((x) == 0) \
  258. { \
  259. printf("(%s:%d) %s\r\n", __FILE__, __LINE__, #x); \
  260. for(;;) \
  261. ; \
  262. }
  263. /**
  264. * @brief Set value by mask
  265. *
  266. * @param[in] bits The one be set
  267. * @param[in] mask mask value
  268. * @param[in] value The value to set
  269. */
  270. void set_bit(volatile uint32_t *bits, uint32_t mask, uint32_t value);
  271. /**
  272. * @brief Set value by mask
  273. *
  274. * @param[in] bits The one be set
  275. * @param[in] mask Mask value
  276. * @param[in] offset Mask's offset
  277. * @param[in] value The value to set
  278. */
  279. void set_bit_offset(volatile uint32_t *bits, uint32_t mask, size_t offset, uint32_t value);
  280. /**
  281. * @brief Set bit for gpio, only set one bit
  282. *
  283. * @param[in] bits The one be set
  284. * @param[in] idx Offset value
  285. * @param[in] value The value to set
  286. */
  287. void set_gpio_bit(volatile uint32_t *bits, size_t idx, uint32_t value);
  288. /**
  289. * @brief Get bits value of mask
  290. *
  291. * @param[in] bits The source data
  292. * @param[in] mask Mask value
  293. * @param[in] offset Mask's offset
  294. *
  295. * @return The bits value of mask
  296. */
  297. uint32_t get_bit(volatile uint32_t *bits, uint32_t mask, size_t offset);
  298. /**
  299. * @brief Get a bit value by offset
  300. *
  301. * @param[in] bits The source data
  302. * @param[in] offset Bit's offset
  303. *
  304. *
  305. * @return The bit value
  306. */
  307. uint32_t get_gpio_bit(volatile uint32_t *bits, size_t offset);
  308. uint32_t is_memory_cache(uintptr_t address);
  309. #ifdef __cplusplus
  310. }
  311. #endif /* __cplusplus */
  312. #endif /* _DRIVER_COMMON_H */