INCLUDE_ATY.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /**
  2. * @file INCLUDE_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Headers include or defines for C platform
  20. *
  21. * @version
  22. * - D+: Design, T+: Test, A+: Archive
  23. * - 1_01_220605 > ATY
  24. * -# Preliminary version, first Release
  25. * - 1_02_251114 > ATY
  26. * -# refact and reorganize
  27. * @todo Not support 64 platfrom, do or never(and double)
  28. ********************************************************************************
  29. */
  30. // fromelf.exe --bin -o ./Pro/Pro.bin ./Pro/Pro.axf
  31. /* Keyword *********************************************************************
  32. | ANSI C (A) | ANSI C (B) | C99 | C11 | C51(C89) |
  33. |------------|------------|------------|-----------------|------------|
  34. | auto | break | inline | _Alignas | _at_ |
  35. | case | char | restrict | _Alignof | bdata |
  36. | const | continue | _Bool | _Atomic | bit |
  37. | default | do | _Complex | _Static_assert | code |
  38. | double | else | _Imaginary | _Noreturn | data |
  39. | enum | extern | | _Thread_local | idata |
  40. | float | for | | _Generic | xdata |
  41. | goto | if | | | pdata |
  42. | int | long | | | sbit |
  43. | register | return | | | sfr |
  44. | short | signed | | | sfr16 |
  45. | sizeof | static | | | interrupt |
  46. | struct | switch | | | using |
  47. | typedef | union | | | reentrant |
  48. | unsigned | void | | | |
  49. | volatile | while | | | |
  50. ********************************************************************************
  51. Removed keywords (C51 or compiler-model terms):
  52. - alien, compact, large, small, _priority_, _task_
  53. */
  54. #ifndef __INCLUDE_ATY_H
  55. #define __INCLUDE_ATY_H
  56. /******************************* Platform *************************************/
  57. /********************************* Words **************************************/
  58. #if defined (__SDCC_SYNTAX_FIX)
  59. # warning SDCC unrecognized compiler
  60. #define __BIT
  61. #define __DATA
  62. #define __IDATA
  63. #define __PDATA
  64. #define __XDATA
  65. #define __CODE
  66. #define __REENTRANT
  67. #define __ATY_NOP
  68. #define __ATY_WEAK
  69. #elif defined(SDCC) || defined(__SDCC)
  70. #define __BIT __bit
  71. #define __DATA __data
  72. #define __IDATA __idata
  73. #define __PDATA __pdata
  74. #define __XDATA __xdata
  75. #define __CODE __code
  76. #define __REENTRANT __reentrant
  77. #define __ATY_NOP __asm NOP __endasm
  78. #define __ATY_WEAK
  79. #elif defined(__CX51__) || defined(__C51__)
  80. #define __BIT bit
  81. #define __DATA data
  82. #define __IDATA idata
  83. #define __PDATA pdata
  84. #define __XDATA xdata
  85. #define __CODE code
  86. #define __REENTRANT reentrant
  87. #include <intrins.h> // _nop_()
  88. extern void _nop_(void);
  89. #define __ATY_NOP _nop_()
  90. #define __ATY_WEAK
  91. #elif defined(__ICCARM__)
  92. #define __BIT bit
  93. #define __DATA
  94. #define __IDATA
  95. #define __PDATA
  96. #define __XDATA
  97. #define __REENTRANT
  98. #define __CODE
  99. #define __ATY_NOP __nop()
  100. #define __ATY_WEAK __WEAK __ATTRIBUTES
  101. #elif defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__ARMCOMPILER_VERSION)
  102. #define __BIT bit
  103. #define __DATA
  104. #define __IDATA
  105. #define __PDATA
  106. #define __XDATA
  107. #define __REENTRANT
  108. #define __CODE
  109. #define __ATY_NOP __nop()
  110. #define __ATY_WEAK __weak
  111. #else
  112. #warning Unsupported compiler!
  113. #endif /* Words */
  114. /******************************* Compiler *************************************/
  115. #if defined(_MSC_VER)
  116. #warning __ATY_COMPILER_MSVC 1
  117. #define __ATY_COMPILER_MSVC 1
  118. #else
  119. #define __ATY_COMPILER_MSVC 0
  120. #endif
  121. #if defined(__clang__)
  122. #warning __ATY_COMPILER_CLANG 1
  123. #define __ATY_COMPILER_CLANG 1
  124. #else
  125. #define __ATY_COMPILER_CLANG 0
  126. #endif
  127. #if defined(__GNUC__) && !defined(__clang__)
  128. // ESP-IDF,
  129. #warning __ATY_COMPILER_GCC 1
  130. #define __ATY_COMPILER_GCC 1
  131. #else
  132. #define __ATY_COMPILER_GCC 0
  133. #endif
  134. #if defined(__ICCARM__)
  135. // IAR, STM8,
  136. #warning __ATY_COMPILER_IAR 1
  137. #define __ATY_COMPILER_IAR 1
  138. #else
  139. #define __ATY_COMPILER_IAR 0
  140. #endif
  141. #if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__ARMCOMPILER_VERSION)
  142. // Keil MDK ARM, like STM32\GD32\ HX32...,
  143. #pragma diag_suppress 1215 // user #warning
  144. #warning __ATY_COMPILER_ARMCC 1
  145. #define __ATY_COMPILER_ARMCC 1
  146. #else
  147. #define __ATY_COMPILER_ARMCC 0
  148. #endif
  149. #if defined(SDCC) || defined(__SDCC)
  150. // 51,
  151. #warning __ATY_COMPILER_SDCC 1
  152. #define __ATY_COMPILER_SDCC 1
  153. #else
  154. #define __ATY_COMPILER_SDCC 0
  155. #endif
  156. #if defined(__C51__) || defined(__CX51__)
  157. // 51,
  158. #warning __ATY_COMPILER_KEIL_C51 1
  159. #define __ATY_COMPILER_KEIL_C51 1
  160. #else
  161. #define __ATY_COMPILER_KEIL_C51 0
  162. #endif
  163. #if __ATY_COMPILER_SDCC || __ATY_COMPILER_KEIL_C51
  164. #warning __ATY_IS_8051_COMPILER 1
  165. #define __ATY_IS_8051_COMPILER 1
  166. #else
  167. #define __ATY_IS_8051_COMPILER 0
  168. #endif
  169. #if __ATY_IS_8051_COMPILER
  170. #define __ATY_HAS_64BIT 0
  171. #else
  172. #define __ATY_HAS_64BIT 1
  173. #endif
  174. // USE_HAL_DRIVER
  175. // USE_FULL_LL_DRIVER
  176. // USE_STDPERIPH_DRIVER
  177. /******************************** Endian **************************************/
  178. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
  179. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  180. #define __ATY_ENDIAN_LE 1
  181. #define __ATY_ENDIAN_BE 0
  182. #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  183. #define __ATY_ENDIAN_LE 0
  184. #define __ATY_ENDIAN_BE 1
  185. #else
  186. #define __ATY_ENDIAN_LE 1
  187. #define __ATY_ENDIAN_BE 0
  188. #endif
  189. #elif defined(__ARMEL__)
  190. #define __ATY_ENDIAN_LE 1
  191. #define __ATY_ENDIAN_BE 0
  192. #elif defined(__ARMEB__)
  193. #define __ATY_ENDIAN_LE 0
  194. #define __ATY_ENDIAN_BE 1
  195. #elif __ATY_IS_8051_COMPILER
  196. #define __ATY_ENDIAN_LE 0
  197. #define __ATY_ENDIAN_BE 1
  198. #elif defined(_WIN32) || defined(_WIN64)
  199. #define __ATY_ENDIAN_LE 1
  200. #define __ATY_ENDIAN_BE 0
  201. #else
  202. #define __ATY_ENDIAN_LE 1
  203. #define __ATY_ENDIAN_BE 0
  204. #endif
  205. #define __ATY_BSWAP16(x) (uint16_t)( \
  206. (((uint16_t)(x) & 0x00FFu) << 8) | \
  207. (((uint16_t)(x) & 0xFF00u) >> 8))
  208. #define __ATY_BSWAP32(x) (uint32_t)(( \
  209. ((uint32_t)(x) & 0x000000FFu) << 24) | \
  210. (((uint32_t)(x) & 0x0000FF00u) << 8) | \
  211. (((uint32_t)(x) & 0x00FF0000u) >> 8) | \
  212. (((uint32_t)(x) & 0xFF000000u) >> 24))
  213. #if __ATY_HAS_64BIT
  214. #define __ATY_BSWAP64(x) (uint64_t)( \
  215. ((((uint64_t)(x)) & 0x00000000000000FFULL) << 56) | \
  216. ((((uint64_t)(x)) & 0x000000000000FF00ULL) << 40) | \
  217. ((((uint64_t)(x)) & 0x0000000000FF0000ULL) << 24) | \
  218. ((((uint64_t)(x)) & 0x00000000FF000000ULL) << 8 ) | \
  219. ((((uint64_t)(x)) & 0x000000FF00000000ULL) >> 8 ) | \
  220. ((((uint64_t)(x)) & 0x0000FF0000000000ULL) >> 24) | \
  221. ((((uint64_t)(x)) & 0x00FF000000000000ULL) >> 40) | \
  222. ((((uint64_t)(x)) & 0xFF00000000000000ULL) >> 56))
  223. #endif
  224. #if __ATY_ENDIAN_LE
  225. #define __ATY_HTOLE16(x) (uint16_t)(x)
  226. #define __ATY_LETOH16(x) (uint16_t)(x)
  227. #define __ATY_HTOBE16(x) __ATY_BSWAP16(x)
  228. #define __ATY_BETOH16(x) __ATY_BSWAP16(x)
  229. #define __ATY_HTOLE32(x) (uint32_t)(x)
  230. #define __ATY_LETOH32(x) (uint32_t)(x)
  231. #define __ATY_HTOBE32(x) __ATY_BSWAP32(x)
  232. #define __ATY_BETOH32(x) __ATY_BSWAP32(x)
  233. #if __ATY_HAS_64BIT
  234. #define __ATY_HTOLE64(x) (uint64_t)(x)
  235. #define __ATY_LETOH64(x) (uint64_t)(x)
  236. #define __ATY_HTOBE64(x) __ATY_BSWAP64(x)
  237. #define __ATY_BETOH64(x) __ATY_BSWAP64(x)
  238. #endif
  239. #else
  240. #define __ATY_HTOLE16(x) __ATY_BSWAP16(x)
  241. #define __ATY_LETOH16(x) __ATY_BSWAP16(x)
  242. #define __ATY_HTOBE16(x) (uint16_t)(x)
  243. #define __ATY_BETOH16(x) (uint16_t)(x)
  244. #define __ATY_HTOLE32(x) __ATY_BSWAP32(x)
  245. #define __ATY_LETOH32(x) __ATY_BSWAP32(x)
  246. #define __ATY_HTOBE32(x) (uint32_t)(x)
  247. #define __ATY_BETOH32(x) (uint32_t)(x)
  248. #if __ATY_HAS_64BIT
  249. #define __ATY_HTOLE64(x) __ATY_BSWAP64(x)
  250. #define __ATY_LETOH64(x) __ATY_BSWAP64(x)
  251. #define __ATY_HTOBE64(x) (uint64_t)(x)
  252. #define __ATY_BETOH64(x) (uint64_t)(x)
  253. #endif
  254. #endif
  255. /******************************* Data Type ************************************/
  256. // #define __UNUSED(x) ((void)(x))
  257. #define __UNUSED(x) (x = x) // not suit for const, used for function formal parameter uaually
  258. #ifndef __stdint_h
  259. #define __stdint_h
  260. #ifndef bool
  261. #define bool uint8_t
  262. #endif /* bool */
  263. #ifndef true
  264. #define true 1
  265. #endif /* true */
  266. #ifndef false
  267. #define false 0
  268. #endif /* false */
  269. #ifndef bit
  270. #define bit uint8_t
  271. #endif /* bit */
  272. #ifndef _UINT8_T
  273. #define _UINT8_T
  274. typedef unsigned char uint8_t;
  275. #endif /* _UINT8_T */
  276. #ifndef _UINT16_T
  277. #define _UINT16_T
  278. typedef unsigned short int uint16_t;
  279. #endif /* _UINT16_T */
  280. #ifndef _UINT32_T
  281. #define _UINT32_T
  282. typedef unsigned long uint32_t;
  283. #endif /* _UINT32_T */
  284. #ifndef _UINT64_T
  285. #define _UINT64_T
  286. #if __ATY_HAS_64BIT
  287. typedef unsigned long long int uint64_t;
  288. #else
  289. typedef unsigned long int uint64_t; // 64 = 32, not 64 bit platform
  290. #endif /* uint64_t */
  291. #endif /* _UINT64_T */
  292. #ifndef _INT8_T
  293. #define _INT8_T
  294. typedef signed char int8_t;
  295. #endif /* _INT8_T */
  296. #ifndef _INT16_T
  297. #define _INT16_T
  298. typedef signed short int int16_t;
  299. #endif /* _INT16_T */
  300. #ifndef _INT32_T
  301. #define _INT32_T
  302. typedef signed int int32_t;
  303. #endif /* _INT32_T */
  304. #ifndef _INT64_T
  305. #define _INT64_T
  306. #if __ATY_HAS_64BIT
  307. typedef signed long long int int64_t;
  308. #else
  309. typedef signed long int int64_t; // 64 = 32, not 64 bit platform
  310. #endif /* int64_t */
  311. #endif /* _INT64_T */
  312. #endif /* __stdint_h */
  313. /********************************** ATY ***************************************/
  314. #define __ATY_IO_I 0
  315. #define __ATY_IO_O 1
  316. #define __ATY_HL_L 0
  317. #define __ATY_HL_H 1
  318. #define __ATY_RW_W 0
  319. #define __ATY_RW_R 1
  320. #define __ATY_RW_RW 2
  321. #define __ATY_BUSY 99
  322. #define __ATY_LOCKED 101
  323. #define __ATY_UNLOCKED 102
  324. #if (USE_RTOS == 1U)
  325. #define __ATY_LOCK(__HANDLE__)
  326. #define __ATY_UNLOCK(__HANDLE__)
  327. #else
  328. #define __ATY_LOCK(__HANDLE__) \
  329. do{ \
  330. if((__HANDLE__)->lock == __ATY_LOCKED) { \
  331. return __ATY_BUSY; \
  332. } \
  333. else { \
  334. (__HANDLE__)->lock = __ATY_LOCKED; \
  335. } \
  336. }while (0U)
  337. #define __ATY_UNLOCK(__HANDLE__) \
  338. do{ \
  339. (__HANDLE__)->lock = __ATY_UNLOCKED; \
  340. }while (0U)
  341. #endif /* USE_RTOS */
  342. /******************************* For user *************************************/
  343. #include "main.h"
  344. /* Diagnostic Suppress (hide warnings) *****************************************
  345. | Platform | Assigned but not used |
  346. |-----------------|------------------------------------------------------------|
  347. | ArmCC5(KeilARM) | #pragma diag_suppress 550 |
  348. | IAR (ICCARM) | #pragma diag_suppress=Pe550 |
  349. | GCC | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
  350. | Clang/ArmClang | #pragma clang diagnostic ignored "-Wunused..." |
  351. | MSVC | #pragma warning(disable: 4189) |
  352. | Keil C51 | Not supported unified pragma diag |
  353. | SDCC (8051) | Not supported unified pragma diag |
  354. *******************************************************************************/
  355. #if defined (__ATY_COMPILER_ARMCC)
  356. #pragma diag_suppress 1215 // user #warning (before #warning)
  357. #pragma diag_suppress 550 // variable "" was set but never used
  358. #pragma diag_suppress 177 // function "" was declared but never referenced
  359. #pragma diag_suppress 186 // pointless comparison of unsigned integer with zero
  360. #pragma diag_suppress 1035 // single-precision operand implicitly converted to double-precision
  361. // #pragma diag_suppress 223 // function "xxx" declared implicitly
  362. #endif
  363. // stdarg ----------------------------------------------------------------------
  364. #define __ATY_USE_STDARG_LIB
  365. #ifdef __ATY_USE_STDARG_LIB
  366. #include <stdarg.h>
  367. #else
  368. #ifndef __stdarg_h
  369. #define __stdarg_h
  370. #if __ATY_COMPILER_CLANG || __ATY_COMPILER_GCC
  371. typedef __builtin_va_list va_list;
  372. #define va_start(ap, param) __builtin_va_start(ap, param)
  373. #define va_end(ap) __builtin_va_end(ap)
  374. #define va_arg(ap, type) __builtin_va_arg(ap, type)
  375. #define va_copy(dest, src) __builtin_va_copy(dest, src)
  376. #elif __ATY_COMPILER_ARMCC
  377. typedef struct __va_list{ void* __ap; } va_list;
  378. #define va_start(ap, parmN) __va_start(ap, parmN)
  379. #define va_arg(ap, type) __va_arg(ap, type)
  380. #define va_end(ap) __va_end(ap)
  381. #define va_copy(dest, src) ((void)((dest) = (src)))
  382. #elif __ATY_COMPILER_IAR
  383. typedef char* va_list;
  384. #define __ATY_VA_ALIGN (sizeof(int))
  385. #define va_start(ap, last) ((ap) = (char*)&(last) + ((sizeof(last)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1)))
  386. #define va_arg(ap, type) (*(type*)((ap += ((sizeof(type)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1))) - ((sizeof(type)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1))))
  387. #define va_end(ap) ((ap) = (va_list)0)
  388. #define va_copy(dest, src) ((dest) = (src))
  389. #elif __ATY_IS_8051_COMPILER
  390. typedef unsigned char* va_list;
  391. #define va_start(ap, last) ((ap) = (unsigned char*)&(last) + sizeof(last))
  392. #define va_arg(ap, type) (*(type*)((ap += sizeof(type)) - sizeof(type)))
  393. #define va_end(ap) ((ap) = (va_list)0)
  394. #define va_copy(dest, src) ((dest) = (src))
  395. #elif __ATY_COMPILER_MSVC
  396. typedef char* va_list;
  397. #define __ATY_VA_ALIGN (sizeof(int))
  398. #define va_start(ap, last) ((ap) = (char*)&(last) + ((sizeof(last)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1)))
  399. #define va_arg(ap, type) (*(type*)((ap += ((sizeof(type)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1))) - ((sizeof(type)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1))))
  400. #define va_end(ap) ((ap) = (va_list)0)
  401. #define va_copy(dest, src) ((dest) = (src))
  402. #else
  403. typedef char* va_list;
  404. #define __ATY_VA_ALIGN (sizeof(int))
  405. #define va_start(ap, last) ((ap) = (char*)&(last) + ((sizeof(last)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1)))
  406. #define va_arg(ap, type) (*(type*)((ap += ((sizeof(type)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1))) - ((sizeof(type)+__ATY_VA_ALIGN-1)&~(__ATY_VA_ALIGN-1))))
  407. #define va_end(ap) ((ap) = (va_list)0)
  408. #define va_copy(dest, src) ((dest) = (src))
  409. #endif
  410. #endif /* __stdarg_h */
  411. #endif /* __ATY_USE_STDARG_LIB */
  412. #define __ATY_PRINT_ENABLE 1
  413. #define __ATY_DEBUG_ENABLE 1
  414. #if __ATY_PRINT_ENABLE
  415. #include "HW_UART_ATY.h"
  416. extern struct HW_UART_ATY_Dev HW_UART_ATY_Dev_1;
  417. #define printf_ATY(...) UartPrintf(&HW_UART_ATY_Dev_1, __VA_ARGS__)
  418. #if __ATY_DEBUG_ENABLE
  419. #define printf_ATY_D(...) UartPrintf(&HW_UART_ATY_Dev_1, __VA_ARGS__)
  420. #else
  421. #define printf_ATY_D(...)
  422. #endif
  423. #else
  424. #define printf_ATY(...)
  425. #define printf_ATY_D(...)
  426. #endif
  427. // Soft Reset ------------------------------------------------------------------
  428. #ifndef __ATY_SOFT_RESET
  429. #if __ATY_IS_8051_COMPILER
  430. #if __ATY_COMPILER_SDCC
  431. #define __ATY_SOFT_RESET() \
  432. do { \
  433. IAP_CONTR |= 0x60; \
  434. void (* __code __aty_reset)(void) = (void (__code *)(void))0x0000; \
  435. __aty_reset(); \
  436. } while (0U)
  437. #elif __ATY_COMPILER_KEIL_C51
  438. #define __ATY_SOFT_RESET() \
  439. do { \
  440. IAP_CONTR |= 0x60; \
  441. void (* __aty_reset)(void) = (void (*)(void))0x0000; \
  442. __aty_reset(); \
  443. } while (0U)
  444. #else
  445. #define __ATY_SOFT_RESET() do { } while (0U)
  446. #endif
  447. #elif __ATY_COMPILER_ARMCC || __ATY_COMPILER_IAR || __ATY_COMPILER_GCC || __ATY_COMPILER_CLANG
  448. #define __ATY_SOFT_RESET() \
  449. do { \
  450. __disable_irq(); \
  451. __NVIC_SystemReset(); \
  452. } while (0U)
  453. #elif __ATY_COMPILER_MSVC
  454. #include <stdlib.h>
  455. #define __ATY_SOFT_RESET() \
  456. do { \
  457. abort(); \
  458. } while (0U)
  459. #else
  460. #define __ATY_SOFT_RESET() do { } while (0U)
  461. #endif
  462. #endif
  463. // DBG IO ----------------------------------------------------------------------
  464. // todo: move to HW_GPIO
  465. #define __ATY_DBG_DH 64u
  466. #define __ATY_DBG_DL 64u
  467. // #if defined(USE_HAL_DRIVER) && defined(ATY_DBG_GPIO_Port) && defined(ATY_DBG_GPIO_Pin)
  468. // #define __ATY_DEBUG_GPIO_WRITE(v) HAL_GPIO_WritePin(ATY_DBG_GPIO_Port, ATY_DBG_GPIO_Pin, ((v)?GPIO_PIN_SET:GPIO_PIN_RESET))
  469. // #define __ATY_DEBUG_GPIO_READ() (HAL_GPIO_ReadPin(ATY_DBG_GPIO_Port, ATY_DBG_GPIO_Pin)?1:0)
  470. // #elif __ATY_IS_8051_COMPILER && defined(ATY_DBG_BIT)
  471. // #define __ATY_DEBUG_GPIO_WRITE(v) do{ ATY_DBG_BIT = ((v)?1:0); }while(0)
  472. // #define __ATY_DEBUG_GPIO_READ() ((ATY_DBG_BIT)?1:0)
  473. // #elif defined(__ESP32_IDF_ATY) && defined(ATY_DBG_GPIO_NUM)
  474. // #define __ATY_DEBUG_GPIO_WRITE(v) gpio_set_level((gpio_num_t)(ATY_DBG_GPIO_NUM), ((v)?1:0))
  475. // #define __ATY_DEBUG_GPIO_READ() ((int)gpio_get_level((gpio_num_t)(ATY_DBG_GPIO_NUM)))
  476. // #endif
  477. #ifndef __ATY_DEBUG_GPIO_WRITE
  478. #define __ATY_DEBUG_GPIO_WRITE(v) do{ (void)(v); }while(0)
  479. #endif
  480. #ifndef __ATY_DEBUG_GPIO_READ
  481. #define __ATY_DEBUG_GPIO_READ() (0)
  482. #endif
  483. #define __ATY_DELAY_LOOP(n) \
  484. do { \
  485. volatile uint32_t __aty_d = (uint32_t)(n); \
  486. while (__aty_d--) { \
  487. __NOP_ATY; \
  488. } \
  489. } while (0U)
  490. #define __ATY_DBG_WAIT_RISING(timeout, okflag) \
  491. do { \
  492. uint32_t __aty_to = (uint32_t)(timeout); \
  493. (okflag) = 0; \
  494. while ((__ATY_DEBUG_GPIO_READ() == 0) && (__aty_to--)) { \
  495. __NOP_ATY; \
  496. } \
  497. if (__ATY_DEBUG_GPIO_READ() != 0) { \
  498. (okflag) = 1; \
  499. } \
  500. } while (0U)
  501. #define __ATY_DBG_WAIT_FALLING(timeout, okflag) \
  502. do { \
  503. uint32_t __aty_to = (uint32_t)(timeout); \
  504. (okflag) = 0; \
  505. while ((__ATY_DEBUG_GPIO_READ() != 0) && (__aty_to--)) { \
  506. __NOP_ATY; \
  507. } \
  508. if (__ATY_DEBUG_GPIO_READ() == 0) { \
  509. (okflag) = 1; \
  510. } \
  511. } while (0U)
  512. #define __ATY_DBG_MEASURE_HIGH(maxcount, count_out) \
  513. do { \
  514. uint32_t __aty_cnt = 0u; \
  515. while ((__ATY_DEBUG_GPIO_READ() != 0) && (__aty_cnt < (uint32_t)(maxcount))) { \
  516. __aty_cnt++; \
  517. __NOP_ATY; \
  518. } \
  519. (count_out) = __aty_cnt; \
  520. } while (0U)
  521. #define __ATY_DBG_RECV8(dst) \
  522. do { \
  523. uint8_t __aty_v = 0u; \
  524. for (uint8_t __aty_i = 0u; __aty_i < 8u; ++__aty_i) { \
  525. uint8_t __aty_ok = 0u; \
  526. __ATY_DBG_WAIT_RISING((__ATY_DBG_DH * 8u + __ATY_DBG_DL * 8u), __aty_ok); \
  527. if (!__aty_ok) { \
  528. break; \
  529. } \
  530. uint32_t __aty_len = 0u; \
  531. __ATY_DBG_MEASURE_HIGH((__ATY_DBG_DH * 3u), __aty_len); \
  532. if (__aty_len <= (__ATY_DBG_DH + (__ATY_DBG_DH / 2u))) { \
  533. __aty_v |= (uint8_t)(1u << __aty_i); \
  534. } \
  535. __ATY_DBG_WAIT_FALLING((__ATY_DBG_DL * 8u), __aty_ok); \
  536. __ATY_DELAY_LOOP(__ATY_DBG_DL); \
  537. } \
  538. (dst) = __aty_v; \
  539. __ATY_DELAY_LOOP(__ATY_DBG_DH * 4u); \
  540. } while (0U)
  541. #define __ATY_DBG_PULSE(hi, lo) \
  542. do { \
  543. __ATY_DEBUG_GPIO_WRITE(1); \
  544. __ATY_DELAY_LOOP((hi)); \
  545. __ATY_DEBUG_GPIO_WRITE(0); \
  546. __ATY_DELAY_LOOP((lo)); \
  547. } while (0U)
  548. #define __ATY_DBG_MARK(cnt, hi, lo) \
  549. do { \
  550. uint32_t __aty_c = (uint32_t)(cnt); \
  551. while (__aty_c--) { \
  552. __ATY_DBG_PULSE((hi), (lo)); \
  553. } \
  554. __ATY_DELAY_LOOP((hi) * 4u); \
  555. } while (0U)
  556. #define __ATY_DBG_SEND8(v) \
  557. do { \
  558. uint8_t __aty_b = (uint8_t)(v); \
  559. for (uint8_t __aty_i = 0u; __aty_i < 8u; ++__aty_i) { \
  560. if (((__aty_b & 0x01u) != 0u)) { \
  561. __ATY_DEBUG_GPIO_WRITE(1); \
  562. __ATY_DELAY_LOOP(__ATY_DBG_DH); \
  563. __ATY_DEBUG_GPIO_WRITE(0); \
  564. __ATY_DELAY_LOOP(__ATY_DBG_DL); \
  565. } else { \
  566. __ATY_DEBUG_GPIO_WRITE(1); \
  567. __ATY_DELAY_LOOP(__ATY_DBG_DH * 2u); \
  568. __ATY_DEBUG_GPIO_WRITE(0); \
  569. __ATY_DELAY_LOOP(__ATY_DBG_DL); \
  570. } \
  571. __aty_b >>= 1; \
  572. } \
  573. __ATY_DELAY_LOOP(__ATY_DBG_DH * 4u); \
  574. } while (0U)
  575. #define __ATY_DBG_R(v) __ATY_DBG_RECV8(v)
  576. #define __ATY_DBG_W(v) __ATY_DBG_SEND8(v)
  577. // remember to define __ATY_DEBUG_GPIO_WRITE(v) and __ATY_DEBUG_GPIO_READ()
  578. // for example:
  579. // #define __ATY_DEBUG_GPIO_WRITE(v) HAL_GPIO_WritePin(ATY_DBG_GPIO_Port, ATY_DBG_GPIO_Pin, ((v)?GPIO_PIN_SET:GPIO_PIN_RESET))
  580. // #define __ATY_DEBUG_GPIO_READ() (HAL_GPIO_ReadPin(ATY_DBG_GPIO_Port, ATY_DBG_GPIO_Pin)?1:0)
  581. /******************************************************************************/
  582. #endif /* __INCLUDE_ATY_H */
  583. /************************************ etc *************************************/
  584. /* init */
  585. /* use */
  586. /******************************************************************************/
  587. /******************************** End Of File *********************************/