hal_sy6970.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #ifndef __HAL_SY6970_H__
  2. #define __HAL_SY6970_H__
  3. #include "hc32_ll_gpio.h"
  4. #include "hc32_ll_fcg.h"
  5. #include "hc32_ll_interrupts.h"
  6. #include "hc32_ll_tmr0.h"
  7. #include "ring_buf.h"
  8. #include "hc32_ll_aos.h"
  9. #if 0
  10. #define SY6970_ADDR 0x6A
  11. void sy6970_init(void);
  12. uint8_t sy6970_charge_status(void);
  13. #endif
  14. #define uint8 uint8_t
  15. #define uint16 uint16_t
  16. #define ENABLE (1)
  17. #define DISABLE (0)
  18. #define REG_INPUT_CURRENT_CTR (0X00)
  19. #define REG_ADC_START_CTR (0X02)
  20. #define REG_CHARGE_CTR (0X03)
  21. #define REG_CURRENT_CHARGE_CTR (0X04)
  22. #define REG_VOLTAGE_CHARGE_CTR (0X06)
  23. #define REG_CHARGE_STATUS_READ (0X0B)
  24. #define REG_NTC_STATUS_READ (0X0C)
  25. #define REG_VOLTAGE_READ (0X0E)
  26. #define REG_SYS_VOLTAGE_READ (0X0F)
  27. #define REG_CHARGE_CURRENT_READ (0X12)
  28. #define REG_CURRENT_STATUS_READ (0X13)
  29. #define SY6970_ADDR (0x6A << 1)
  30. #pragma pack(1)
  31. typedef struct
  32. {
  33. uint8 IINLIM:6;
  34. uint8 EN_ILIM:1;
  35. uint8 EN_HIZ:1;
  36. /*
  37. * Input current limit:
  38. (Actual input current limit is the lower of I2C or ILIM pin)
  39. IINLIM=100mA+50mA*[IINLIM]
  40. Range:100mA(000000)-3.25A(111111)
  41. 000000=100mA
  42. 000001=150mA
  43. 001000=500mA(Default)
  44. 111111=3.25A
  45. IINLIM will be changed according to the adapter type after input
  46. DP/DM detection is done.
  47. USB Host SDP=500mA
  48. USB CDP=1.5A
  49. USB DCP=3.25A
  50. */
  51. }REG00_CONFIG_CON;
  52. typedef struct
  53. {
  54. uint8 auto_dpdm_en:1;
  55. uint8 force_dpdm:1;
  56. uint8 hv_type:1;
  57. uint8 hvdcp_en:1;
  58. uint8 aicl_en:1;
  59. uint8 boost_freq:1;
  60. uint8 conv_rate:1;
  61. uint8 conv_start:1;
  62. }REG02_CONFIG_CON;
  63. typedef union
  64. {
  65. uint8 data;
  66. REG02_CONFIG_CON config;
  67. }REG02_CONFIG;
  68. typedef struct
  69. {
  70. uint8 reserved:1;
  71. uint8 sys_min:3;
  72. uint8 chg_config:1;
  73. uint8 otg_config:1;
  74. uint8 wd_rst:1;
  75. uint8 bat_load_en:1;
  76. }REG03_CHARGE_CON;
  77. typedef union
  78. {
  79. uint8 data;
  80. REG03_CHARGE_CON config;
  81. }REG03_CHARGE;
  82. typedef struct
  83. {
  84. uint8 ICHG:7; //Fast Charge Current Limit: ICHG=[ICHG]*64mA
  85. uint8 EN_PUMPX:1;
  86. }REG04_CURRENT_CON;
  87. typedef union
  88. {
  89. uint8 data;
  90. REG04_CURRENT_CON config;
  91. }REG04_CURRENT;
  92. typedef struct
  93. {
  94. uint8 ITERM:4; // Termination Current Limit: ITERM=64mA+[ITERM]*64mA
  95. uint8 IPRECHG:4;// Precharge Current Limit:IPRECHG=64mA+[IPRECHG]*64mA
  96. }REG05_CURRENT_CON;
  97. typedef struct
  98. {
  99. uint8 VRECHG:1; // Battery Recharge Threshold Offset: 0-100mV 1-200mV
  100. uint8 BATLOWV:1;// Battery Precharge to Fast Charge Threshold: 0-2.8V 1-3.0V
  101. uint8 VREG:1; // Charge Voltage Limit: VREG=3.840V+[VREG]*16mV
  102. }REG06_VREG_CON;
  103. typedef struct
  104. {
  105. uint8 JEITA_ISET:1; // JEITA Low Temperature Current Setting:
  106. //Percentage with respect to ICHG register REG04[6:0]
  107. //0-50% 1-20%
  108. uint8 CHG_TIMER:2; // Fast Charge Timer Setting: 00-5 hrs 01-8 hrs 10-12 hrs 11-20 hrs
  109. uint8 EN_TIMER:1;
  110. uint8 WATCHDOG:2;//
  111. uint8 STAT_DIS:1; //
  112. uint8 EN_TERM:1;
  113. }REG07_VREG_CON;
  114. typedef struct
  115. {
  116. uint8 TREG:2;
  117. uint8 VCLAMP:3;
  118. uint8 BAT_COMP:3;
  119. }REG08_BAT_CON;
  120. typedef struct
  121. {
  122. uint8 PUMPX_DN:1; // Current pulse control to request lower voltage:
  123. uint8 PUMPX_UP:1; // Current pulse control to request higher voltage:
  124. uint8 BATFET_RST_EN:1; //BATFET Reset Enable:
  125. uint8 BATFET_DLY:1; // BATFET turn off delay control:
  126. uint8 JEITA_VSET:1; // JEITA High Temperature Voltage Setting:
  127. uint8 BATFET_DIS:1; //Force BATFET Off:
  128. uint8 TMR2X_EN:1; //Safety Timer Setting during Input DPM and Thermal
  129. uint8 FORCE_AICL:1;//Force Start Adaptive Input Current Limit:
  130. }REG09_CURRENT_CON;
  131. typedef struct
  132. {
  133. uint8 BOOST_LIM:3; // Boost Mode Current Limit:
  134. uint8 Reserved:1; // Reserved
  135. uint8 BOOSTV:4; //Boost Mode Voltage Regulation:VBOOST=4.55V+[BOOSTV]*64mV:
  136. }REG0A_BOOSTV_CON;
  137. typedef struct
  138. {
  139. uint8 VSYS_STAT:1; //VSYS Regulation Status:0-Not in SYSMIN regulation (VBAT>VSYSMIN) 1-In SYSMIN regulation (VBAT<VSYSMIN)
  140. uint8 SDP_STAT:1; // Charging status: 00-Not Charging 001-Pre-charge (VBAT <VBATLOWV)
  141. //10-Fast Charging 11-Charge Termination Done
  142. uint8 PG_STAT:1; //Power Good Status: 0-Not Power Good 1-Power Good
  143. uint8 CHRG_STAT:2; // BATFET turn off delay control:
  144. /*00-Not Charging
  145. 01-Pre-charge (VBAT <VBATLOWV)
  146. 10-Fast Charging
  147. 11-Charge Termination Done
  148. uint8 BUS_STAT:2;*/
  149. uint8 BUS_STAT:2; //BUS Status register:
  150. /*000:No input
  151. 001:USB Host SDP
  152. 010:USB CDP
  153. 011:USB DCP
  154. 100:HVDCP
  155. 101:Unknown Adapter
  156. 110:Non-Standard Adapter
  157. 111:OTG*/
  158. }REG0B_CURRENT_CON;
  159. typedef struct
  160. {
  161. uint8 bsys_stat:1;
  162. uint8 sdp_stat:1;
  163. uint8 pg_stat:1;
  164. uint8 charg_stat:2;
  165. /*
  166. * Charging status:
  167. * 00-Not Charging
  168. * 01-Pre-charge (VBAT <VBATLOWV)
  169. * 10-Fast Charging
  170. * 11-Charge Termination Done
  171. */
  172. uint8 bus_stat:3;
  173. /*
  174. * BUS Status register:
  175. * 000:No input
  176. * 001:USB Host SDP
  177. * 010:USB CDP
  178. * 011:USB DCP
  179. * 100:HVDCP
  180. * 101:Unknown Adapter
  181. * 110:Non-Standard Adapter
  182. * 111:OTG
  183. */
  184. }REG0B_STATUS;
  185. typedef struct
  186. {
  187. uint8 NTC_FAULT:2;
  188. uint8 BAT_FAULT:1;
  189. uint8 CHRG_FAULT:2;
  190. uint8 BOOST_FAULT:1;
  191. uint8 WATCHDOG_FAULT:1;
  192. }REG0C_TEM_INFO;
  193. typedef struct
  194. {
  195. uint8 VINDPM:7;
  196. uint8 VINDPM_MODE:1;
  197. }REG0D_VINDPM_CON;
  198. typedef union
  199. {
  200. uint8 data;
  201. REG0D_VINDPM_CON config;
  202. }REG0D_VINDPM;
  203. typedef struct
  204. {
  205. uint8 BATV:7;
  206. uint8 THERM_STAT:1;
  207. }REG0E_BATTV_INFO;
  208. typedef struct
  209. {
  210. uint8 SYSV:7;
  211. uint8 Reserved:1;
  212. }REG0F_BATTV_INFO;
  213. typedef struct
  214. {
  215. uint8 busv:7;
  216. uint8 bus_gd:1;
  217. }REG11_BUS;
  218. typedef union
  219. {
  220. uint8 data;
  221. REG11_BUS reg_read;
  222. }REG11_BUS_STATUS;
  223. typedef struct
  224. {
  225. uint8 ICHGR:7;
  226. uint8 RE:1;
  227. }REG12_CHARGE_CURRENT;
  228. typedef struct
  229. {
  230. uint8 CurrentInputLimit:6;
  231. uint8 IDPM_STAT:1;
  232. uint8 VDPM_STAT:1;
  233. }REG13_CURRENT_STATUS;
  234. typedef union
  235. {
  236. uint8 data_uint8;
  237. REG00_CONFIG_CON re00_config;
  238. REG02_CONFIG_CON reg02_config;
  239. REG03_CHARGE_CON reg03_config;
  240. REG04_CURRENT_CON reg04_config;
  241. REG06_VREG_CON reg06_config;
  242. REG0B_STATUS reg0b_onre_status;
  243. REG0C_TEM_INFO reg0c_tem_info;
  244. REG0D_VINDPM_CON reg0D_config;
  245. REG0E_BATTV_INFO reg0e_batt_info;
  246. REG0F_BATTV_INFO reg0f_sysv_info;
  247. REG11_BUS reg11_bus_info;
  248. REG12_CHARGE_CURRENT reg12_charge_info;
  249. REG13_CURRENT_STATUS reg13_current_input_info;
  250. }REG_SYS_CONFIG;
  251. #pragma pack()
  252. REG_SYS_CONFIG sy6970_get_reg_config(uint8_t cmd);
  253. void sy6970_init(void);
  254. void sy6970_read_byte(uint8_t dev_addr, uint8_t RAddr, uint8_t *RData);
  255. uint16_t sy6970_get_batv(void);
  256. uint16_t sy6970_get_sysv(void);
  257. uint16_t sy6970_set_reg(uint8 cmd, uint16 cmd_data);
  258. void sy6970_set_reg03_charge(void);
  259. void sy6970_reg0d_vindpm_config(void);
  260. REG_SYS_CONFIG sy6970_get_reg02_config(void);
  261. REG_SYS_CONFIG sy6970_get_reg03_config(void);
  262. REG_SYS_CONFIG sy6970_get_reg0b_charge_status(void);
  263. REG_SYS_CONFIG sy6970_get_reg0d_vindpm_config(void);
  264. REG_SYS_CONFIG sy6970_get_reg11_bus_status(void);
  265. void sy6970_task(void *argv);
  266. #endif