AD7124_ATY.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * @file AD7124_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 functions of AD7124 for C platform
  20. *
  21. * @version
  22. * - 1_01_230828 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_231229 > ATY
  25. * -# add multy addr and channel
  26. * - 1_03_240111 > ATY
  27. * -# add lock
  28. * - 1_04_251124 > ATY
  29. * -# change log and test basely
  30. ********************************************************************************
  31. */
  32. #ifndef __AD7124_ATY_H
  33. #define __AD7124_ATY_H
  34. #include "INCLUDE_ATY.h"
  35. /******************************* For user *************************************/
  36. /******************************************************************************/
  37. struct AD7124_ATY_Cfg
  38. {
  39. uint32_t AD7124_STATUS_REG_t; // r
  40. uint32_t AD7124_ADC_CTRL_REG_t;
  41. uint32_t AD7124_DATA_REG_t; // r
  42. uint32_t AD7124_IO_CTRL1_REG_t;
  43. uint32_t AD7124_IO_CTRL2_REG_t;
  44. uint32_t AD7124_ID_REG_t; // r
  45. uint32_t AD7124_ERR_REG_t; // r
  46. uint32_t AD7124_ERREN_REG_t;
  47. uint32_t AD7124_MCLK_COUNT_REG_t; // r
  48. uint32_t AD7124_CH0_MAP_REG_t;
  49. uint32_t AD7124_CH1_MAP_REG_t;
  50. uint32_t AD7124_CH2_MAP_REG_t;
  51. uint32_t AD7124_CH3_MAP_REG_t;
  52. uint32_t AD7124_CH4_MAP_REG_t;
  53. uint32_t AD7124_CH5_MAP_REG_t;
  54. uint32_t AD7124_CH6_MAP_REG_t;
  55. uint32_t AD7124_CH7_MAP_REG_t;
  56. uint32_t AD7124_CH8_MAP_REG_t;
  57. uint32_t AD7124_CH9_MAP_REG_t;
  58. uint32_t AD7124_CH10_MAP_REG_t;
  59. uint32_t AD7124_CH11_MAP_REG_t;
  60. uint32_t AD7124_CH12_MAP_REG_t;
  61. uint32_t AD7124_CH13_MAP_REG_t;
  62. uint32_t AD7124_CH14_MAP_REG_t;
  63. uint32_t AD7124_CH15_MAP_REG_t;
  64. uint32_t AD7124_CFG0_REG_t;
  65. uint32_t AD7124_CFG1_REG_t;
  66. uint32_t AD7124_CFG2_REG_t;
  67. uint32_t AD7124_CFG3_REG_t;
  68. uint32_t AD7124_CFG4_REG_t;
  69. uint32_t AD7124_CFG5_REG_t;
  70. uint32_t AD7124_CFG6_REG_t;
  71. uint32_t AD7124_CFG7_REG_t;
  72. uint32_t AD7124_FILT0_REG_t;
  73. uint32_t AD7124_FILT1_REG_t;
  74. uint32_t AD7124_FILT2_REG_t;
  75. uint32_t AD7124_FILT3_REG_t;
  76. uint32_t AD7124_FILT4_REG_t;
  77. uint32_t AD7124_FILT5_REG_t;
  78. uint32_t AD7124_FILT6_REG_t;
  79. uint32_t AD7124_FILT7_REG_t;
  80. uint32_t AD7124_OFFS0_REG_t;
  81. uint32_t AD7124_OFFS1_REG_t;
  82. uint32_t AD7124_OFFS2_REG_t;
  83. uint32_t AD7124_OFFS3_REG_t;
  84. uint32_t AD7124_OFFS4_REG_t;
  85. uint32_t AD7124_OFFS5_REG_t;
  86. uint32_t AD7124_OFFS6_REG_t;
  87. uint32_t AD7124_OFFS7_REG_t;
  88. uint32_t AD7124_GAIN0_REG_t;
  89. uint32_t AD7124_GAIN1_REG_t;
  90. uint32_t AD7124_GAIN2_REG_t;
  91. uint32_t AD7124_GAIN3_REG_t;
  92. uint32_t AD7124_GAIN4_REG_t;
  93. uint32_t AD7124_GAIN5_REG_t;
  94. uint32_t AD7124_GAIN6_REG_t;
  95. uint32_t AD7124_GAIN7_REG_t;
  96. };
  97. struct AD7124_ATY_Dev
  98. {
  99. uint8_t addr;
  100. uint8_t id;
  101. void (*nssSet)(uint8_t level);
  102. void (*enSet)(uint8_t level);
  103. void (*syncSet)(uint8_t level);
  104. uint8_t(*spiProcess)(uint8_t* data_t, uint16_t len, uint8_t rw);
  105. void (*delay)(uint32_t t);
  106. uint8_t lock;
  107. };
  108. uint8_t AD7124_WriteRead(uint8_t* data, uint8_t len, struct AD7124_ATY_Dev* dev);
  109. uint8_t AD7124_Reset(struct AD7124_ATY_Dev* dev);
  110. uint8_t AD7124_ReadId(uint8_t* id, struct AD7124_ATY_Dev* dev);
  111. uint8_t AD7124_WriteReg(uint8_t regAddr, uint32_t data, uint8_t len, struct AD7124_ATY_Dev* dev);
  112. uint8_t AD7124_ReadReg(uint8_t regAddr, uint32_t* data, uint8_t len, struct AD7124_ATY_Dev* dev);
  113. uint8_t AD7124_ReadData(uint8_t* data, struct AD7124_ATY_Dev* dev);
  114. uint8_t AD7124_ReadStatus(uint8_t* status, struct AD7124_ATY_Dev* dev);
  115. float AD7124_DataToRes(uint32_t data, uint8_t resolution, float refRes, uint8_t gain);
  116. float AD7124_DataToResDefault(uint32_t data);
  117. uint8_t AD7124_Init(struct AD7124_ATY_Cfg* cfg, struct AD7124_ATY_Dev* dev);
  118. uint8_t AD7124_Config(struct AD7124_ATY_Cfg* cfg, struct AD7124_ATY_Dev* dev);
  119. uint8_t AD7124_ReadAllReg(struct AD7124_ATY_Cfg* cfg, struct AD7124_ATY_Dev* dev);
  120. uint8_t AD7124_Calibrate(uint8_t calibrateType, struct AD7124_ATY_Cfg* cfg, struct AD7124_ATY_Dev* dev);
  121. #define AD7124_CALIBRATE_INT_FULL 0x01
  122. #define AD7124_CALIBRATE_INT_ZERO 0x02
  123. #define AD7124_CALIBRATE_SYS_FULL 0x04
  124. #define AD7124_CALIBRATE_SYS_ZERO 0x08
  125. /******************* Register map and register definitions ********************/
  126. #define AD7124_RW 1 /* Read and Write */
  127. #define AD7124_R 2 /* Read only */
  128. #define AD7124_W 3 /* Write only */
  129. /* AD7124 Register Map */
  130. #define AD7124_COMM_REG 0x00
  131. #define AD7124_STATUS_REG 0x00
  132. #define AD7124_ADC_CTRL_REG 0x01
  133. #define AD7124_DATA_REG 0x02
  134. #define AD7124_IO_CTRL1_REG 0x03
  135. #define AD7124_IO_CTRL2_REG 0x04
  136. #define AD7124_ID_REG 0x05
  137. #define AD7124_ERR_REG 0x06
  138. #define AD7124_ERREN_REG 0x07
  139. #define AD7124_MCLK_COUNT_REG 0x08
  140. #define AD7124_CH0_MAP_REG 0x09
  141. #define AD7124_CH1_MAP_REG 0x0A
  142. #define AD7124_CH2_MAP_REG 0x0B
  143. #define AD7124_CH3_MAP_REG 0x0C
  144. #define AD7124_CH4_MAP_REG 0x0D
  145. #define AD7124_CH5_MAP_REG 0x0E
  146. #define AD7124_CH6_MAP_REG 0x0F
  147. #define AD7124_CH7_MAP_REG 0x10
  148. #define AD7124_CH8_MAP_REG 0x11
  149. #define AD7124_CH9_MAP_REG 0x12
  150. #define AD7124_CH10_MAP_REG 0x13
  151. #define AD7124_CH11_MAP_REG 0x14
  152. #define AD7124_CH12_MAP_REG 0x15
  153. #define AD7124_CH13_MAP_REG 0x16
  154. #define AD7124_CH14_MAP_REG 0x17
  155. #define AD7124_CH15_MAP_REG 0x18
  156. #define AD7124_CFG0_REG 0x19
  157. #define AD7124_CFG1_REG 0x1A
  158. #define AD7124_CFG2_REG 0x1B
  159. #define AD7124_CFG3_REG 0x1C
  160. #define AD7124_CFG4_REG 0x1D
  161. #define AD7124_CFG5_REG 0x1E
  162. #define AD7124_CFG6_REG 0x1F
  163. #define AD7124_CFG7_REG 0x20
  164. #define AD7124_FILT0_REG 0x21
  165. #define AD7124_FILT1_REG 0x22
  166. #define AD7124_FILT2_REG 0x23
  167. #define AD7124_FILT3_REG 0x24
  168. #define AD7124_FILT4_REG 0x25
  169. #define AD7124_FILT5_REG 0x26
  170. #define AD7124_FILT6_REG 0x27
  171. #define AD7124_FILT7_REG 0x28
  172. #define AD7124_OFFS0_REG 0x29
  173. #define AD7124_OFFS1_REG 0x2A
  174. #define AD7124_OFFS2_REG 0x2B
  175. #define AD7124_OFFS3_REG 0x2C
  176. #define AD7124_OFFS4_REG 0x2D
  177. #define AD7124_OFFS5_REG 0x2E
  178. #define AD7124_OFFS6_REG 0x2F
  179. #define AD7124_OFFS7_REG 0x30
  180. #define AD7124_GAIN0_REG 0x31
  181. #define AD7124_GAIN1_REG 0x32
  182. #define AD7124_GAIN2_REG 0x33
  183. #define AD7124_GAIN3_REG 0x34
  184. #define AD7124_GAIN4_REG 0x35
  185. #define AD7124_GAIN5_REG 0x36
  186. #define AD7124_GAIN6_REG 0x37
  187. #define AD7124_GAIN7_REG 0x38
  188. /* Communication Register bits */
  189. #define AD7124_COMM_REG_WEN (0 << 7)
  190. #define AD7124_COMM_REG_WR (0 << 6)
  191. #define AD7124_COMM_REG_RD (1 << 6)
  192. #define AD7124_COMM_REG_RA(x) ((x) | 0x40) // read reg at address (x)
  193. #define AD7124_COMM_REG_WA(x) ((x) & 0x3F) // write reg at address (x)
  194. /* Status Register bits */
  195. #define AD7124_STATUS_REG_RDY (1 << 7)
  196. #define AD7124_STATUS_REG_ERROR_FLAG (1 << 6)
  197. #define AD7124_STATUS_REG_POR_FLAG (1 << 4)
  198. #define AD7124_STATUS_REG_CH_ACTIVE(x) ((x) & 0xF)
  199. /* ADC_Control Register bits */
  200. #define AD7124_ADC_CTRL_REG_DOUT_RDY_DEL (1 << 12)
  201. #define AD7124_ADC_CTRL_REG_CONT_READ (1 << 11)
  202. #define AD7124_ADC_CTRL_REG_DATA_STATUS (1 << 10)
  203. #define AD7124_ADC_CTRL_REG_CS_EN (1 << 9)
  204. #define AD7124_ADC_CTRL_REG_REF_EN (1 << 8)
  205. #define AD7124_ADC_CTRL_REG_POWER_MODE(x) (((x) & 0x3) << 6)
  206. #define AD7124_ADC_CTRL_REG_MODE(x) (((x) & 0xF) << 2)
  207. #define AD7124_ADC_CTRL_REG_CLK_SEL(x) (((x) & 0x3) << 0)
  208. /* IO_Control_1 Register bits */
  209. #define AD7124_IO_CTRL1_REG_GPIO_DAT2 (1 << 23)
  210. #define AD7124_IO_CTRL1_REG_GPIO_DAT1 (1 << 22)
  211. #define AD7124_IO_CTRL1_REG_GPIO_CTRL2 (1 << 19)
  212. #define AD7124_IO_CTRL1_REG_GPIO_CTRL1 (1 << 18)
  213. #define AD7124_IO_CTRL1_REG_PDSW (1 << 15)
  214. #define AD7124_IO_CTRL1_REG_IOUT1(x) (((x) & 0x7) << 11)
  215. #define AD7124_IO_CTRL1_REG_IOUT0(x) (((x) & 0x7) << 8)
  216. #define AD7124_IO_CTRL1_REG_IOUT_CH1(x) (((x) & 0xF) << 4)
  217. #define AD7124_IO_CTRL1_REG_IOUT_CH0(x) (((x) & 0xF) << 0)
  218. /* IO_Control_1 AD7124-8 specific bits */
  219. #define AD7124_8_IO_CTRL1_REG_GPIO_DAT4 (1 << 23)
  220. #define AD7124_8_IO_CTRL1_REG_GPIO_DAT3 (1 << 22)
  221. #define AD7124_8_IO_CTRL1_REG_GPIO_DAT2 (1 << 21)
  222. #define AD7124_8_IO_CTRL1_REG_GPIO_DAT1 (1 << 20)
  223. #define AD7124_8_IO_CTRL1_REG_GPIO_CTRL4 (1 << 19)
  224. #define AD7124_8_IO_CTRL1_REG_GPIO_CTRL3 (1 << 18)
  225. #define AD7124_8_IO_CTRL1_REG_GPIO_CTRL2 (1 << 17)
  226. #define AD7124_8_IO_CTRL1_REG_GPIO_CTRL1 (1 << 16)
  227. /* IO_Control_2 Register bits */
  228. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS7 (1 << 15)
  229. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS6 (1 << 14)
  230. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS5 (1 << 11)
  231. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS4 (1 << 10)
  232. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS3 (1 << 5)
  233. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS2 (1 << 4)
  234. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS1 (1 << 1)
  235. #define AD7124_IO_CTRL2_REG_GPIO_VBIAS0 (1 << 0)
  236. /* IO_Control_2 AD7124-8 specific bits */
  237. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS15 (1 << 15)
  238. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS14 (1 << 14)
  239. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS13 (1 << 13)
  240. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS12 (1 << 12)
  241. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS11 (1 << 11)
  242. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS10 (1 << 10)
  243. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS9 (1 << 9)
  244. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS8 (1 << 8)
  245. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS7 (1 << 7)
  246. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS6 (1 << 6)
  247. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS5 (1 << 5)
  248. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS4 (1 << 4)
  249. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS3 (1 << 3)
  250. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS2 (1 << 2)
  251. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS1 (1 << 1)
  252. #define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS0 (1 << 0)
  253. /* ID Register bits */
  254. #define AD7124_ID_REG_DEVICE_ID(x) (((x) & 0xF) << 4)
  255. #define AD7124_ID_REG_SILICON_REV(x) (((x) & 0xF) << 0)
  256. /* Error Register bits */
  257. #define AD7124_ERR_REG_LDO_CAP_ERR (1 << 19)
  258. #define AD7124_ERR_REG_ADC_CAL_ERR (1 << 18)
  259. #define AD7124_ERR_REG_ADC_CONV_ERR (1 << 17)
  260. #define AD7124_ERR_REG_ADC_SAT_ERR (1 << 16)
  261. #define AD7124_ERR_REG_AINP_OV_ERR (1 << 15)
  262. #define AD7124_ERR_REG_AINP_UV_ERR (1 << 14)
  263. #define AD7124_ERR_REG_AINM_OV_ERR (1 << 13)
  264. #define AD7124_ERR_REG_AINM_UV_ERR (1 << 12)
  265. #define AD7124_ERR_REG_REF_DET_ERR (1 << 11)
  266. #define AD7124_ERR_REG_DLDO_PSM_ERR (1 << 9)
  267. #define AD7124_ERR_REG_ALDO_PSM_ERR (1 << 7)
  268. #define AD7124_ERR_REG_SPI_IGNORE_ERR (1 << 6)
  269. #define AD7124_ERR_REG_SPI_SLCK_CNT_ERR (1 << 5)
  270. #define AD7124_ERR_REG_SPI_READ_ERR (1 << 4)
  271. #define AD7124_ERR_REG_SPI_WRITE_ERR (1 << 3)
  272. #define AD7124_ERR_REG_SPI_CRC_ERR (1 << 2)
  273. #define AD7124_ERR_REG_MM_CRC_ERR (1 << 1)
  274. #define AD7124_ERR_REG_ROM_CRC_ERR (1 << 0)
  275. /* Error_En Register bits */
  276. #define AD7124_ERREN_REG_MCLK_CNT_EN (1 << 22)
  277. #define AD7124_ERREN_REG_LDO_CAP_CHK_TEST_EN (1 << 21)
  278. #define AD7124_ERREN_REG_LDO_CAP_CHK(x) (((x) & 0x3) << 19)
  279. #define AD7124_ERREN_REG_ADC_CAL_ERR_EN (1 << 18)
  280. #define AD7124_ERREN_REG_ADC_CONV_ERR_EN (1 << 17)
  281. #define AD7124_ERREN_REG_ADC_SAT_ERR_EN (1 << 16)
  282. #define AD7124_ERREN_REG_AINP_OV_ERR_EN (1 << 15)
  283. #define AD7124_ERREN_REG_AINP_UV_ERR_EN (1 << 14)
  284. #define AD7124_ERREN_REG_AINM_OV_ERR_EN (1 << 13)
  285. #define AD7124_ERREN_REG_AINM_UV_ERR_EN (1 << 12)
  286. #define AD7124_ERREN_REG_REF_DET_ERR_EN (1 << 11)
  287. #define AD7124_ERREN_REG_DLDO_PSM_TRIP_TEST_EN (1 << 10)
  288. #define AD7124_ERREN_REG_DLDO_PSM_ERR_ERR (1 << 9)
  289. #define AD7124_ERREN_REG_ALDO_PSM_TRIP_TEST_EN (1 << 8)
  290. #define AD7124_ERREN_REG_ALDO_PSM_ERR_EN (1 << 7)
  291. #define AD7124_ERREN_REG_SPI_IGNORE_ERR_EN (1 << 6)
  292. #define AD7124_ERREN_REG_SPI_SCLK_CNT_ERR_EN (1 << 5)
  293. #define AD7124_ERREN_REG_SPI_READ_ERR_EN (1 << 4)
  294. #define AD7124_ERREN_REG_SPI_WRITE_ERR_EN (1 << 3)
  295. #define AD7124_ERREN_REG_SPI_CRC_ERR_EN (1 << 2)
  296. #define AD7124_ERREN_REG_MM_CRC_ERR_EN (1 << 1)
  297. #define AD7124_ERREN_REG_ROM_CRC_ERR_EN (1 << 0)
  298. /* Channel Registers 0-15 bits */
  299. #define AD7124_CH_MAP_REG_CH_ENABLE (1 << 15)
  300. #define AD7124_CH_MAP_REG_SETUP(x) (((x) & 0x7) << 12)
  301. #define AD7124_CH_MAP_REG_AINP(x) (((x) & 0x1F) << 5)
  302. #define AD7124_CH_MAP_REG_AINM(x) (((x) & 0x1F) << 0)
  303. /* Configuration Registers 0-7 bits */
  304. #define AD7124_CFG_REG_BIPOLAR (1 << 11)
  305. #define AD7124_CFG_REG_BURNOUT(x) (((x) & 0x3) << 9)
  306. #define AD7124_CFG_REG_REF_BUFP (1 << 8)
  307. #define AD7124_CFG_REG_REF_BUFM (1 << 7)
  308. #define AD7124_CFG_REG_AIN_BUFP (1 << 6)
  309. #define AD7124_CFG_REG_AINN_BUFM (1 << 5)
  310. #define AD7124_CFG_REG_REF_SEL(x) ((x) & 0x3) << 3
  311. #define AD7124_CFG_REG_PGA(x) (((x) & 0x7) << 0)
  312. /* Filter Register 0-7 bits */
  313. #define AD7124_FILT_REG_FILTER(x) (((x) & 0x7) << 21)
  314. #define AD7124_FILT_REG_REJ60 (1 << 20)
  315. #define AD7124_FILT_REG_POST_FILTER(x) (((x) & 0x7) << 17)
  316. #define AD7124_FILT_REG_SINGLE_CYCLE (1 << 16)
  317. #define AD7124_FILT_REG_FS(x) (((x) & 0x7FF) << 0)
  318. /******************* AD7124 Constants *****************************************/
  319. #define AD7124_CRC8_POLYNOMIAL_REPRESENTATION 0x07 /* x8 + x2 + x + 1 */
  320. #define AD7124_DISABLE_CRC 0
  321. #define AD7124_USE_CRC 1
  322. #endif /* __AD7124_ATY_H */
  323. /******************************** End Of File *********************************/