charge_demo.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. @file
  3. charger_demo.c
  4. @brief
  5. quectel charger_demo.
  6. */
  7. /*================================================================
  8. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  9. Quectel Wireless Solution Proprietary and Confidential.
  10. =================================================================*/
  11. /*=================================================================
  12. EDIT HISTORY FOR MODULE
  13. This section contains comments describing changes made to the module.
  14. Notice that changes are listed in reverse chronological order.
  15. WHEN WHO WHAT, WHERE, WHY
  16. ------------ ------- -------------------------------------------------------------------------------
  17. 15/01/2021 Neo Init version
  18. =================================================================*/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include "ql_api_osi.h"
  23. #include "ql_log.h"
  24. #include "ql_power.h"
  25. #include "ql_adc.h"
  26. #include "charge_demo.h"
  27. /*===========================================================================
  28. * Macro Definition
  29. ===========================================================================*/
  30. #define QL_CHARGERDEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  31. #define QL_CHARGERDEMO_LOG(msg, ...) QL_LOG(QL_CHARGERDEMO_LOG_LEVEL, "ql_CHARGER", msg, ##__VA_ARGS__)
  32. #define QL_CHARGERDEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("qlCHARGER", msg, ##__VA_ARGS__)
  33. /*
  34. ** CONFIG_CHARGER_POWERUP宏支持关机充电和充电自动开机功能,可以添加到对应项目的target.config开启这项功能
  35. ** CONFIG_SRV_POWER_ON_VOLTAGE 为开机电压,默认为3300mV
  36. ** CONFIG_SRV_POWER_OFF_VOLTAGE为关机电压,默认为3000mV
  37. */
  38. #define QL_ENABLE_BATTERY_DETECT 0 //只有外接ADC检测电池,才可以打开这个功能
  39. #define BATTERY_CONNECT_THRESHOLD 1720 //电池在位阈值,需要根据根据NTC电阻温度对照表确定在位阈值电压!!!!!!
  40. #define BATTERY_NO_CONNECT 0 //电池未连接
  41. #define BATTERY_CONNECT 1 //电池已连接
  42. /*===========================================================================
  43. * Functions
  44. ===========================================================================*/
  45. #if QL_ENABLE_BATTERY_DETECT
  46. /*
  47. ** Description: 获取电池在位状态。根据读取的NTC电压判断是否处于在位状态,需要根据根据NTC电阻温度对照表确定在位阈值电压!!!!!!
  48. */
  49. void ql_get_battery_state(int ql_adc_channel_id, uint8_t* bat_state)
  50. {
  51. int ntc_vol = 0;
  52. ql_adc_get_volt(ql_adc_channel_id, &ntc_vol);
  53. QL_CHARGERDEMO_LOG("ntc_vol=%d", ntc_vol);
  54. if(ntc_vol < BATTERY_CONNECT_THRESHOLD)
  55. {
  56. *bat_state = BATTERY_CONNECT;
  57. }
  58. else
  59. {
  60. *bat_state = BATTERY_NO_CONNECT;
  61. }
  62. }
  63. /*
  64. ** Description: 获取电池温度,根据电池电压与温度对应关系计算,映射关系需要参考NTC电阻温度对照表!!!!!!
  65. */
  66. void ql_get_battery_temperature(int ql_adc_channel_id, int *bat_temperature)
  67. {
  68. int ntc_vol = 0, i = 0;
  69. uint8_t bat_state = 0;
  70. uint32_t table_size = 0;
  71. ql_get_battery_state(ql_adc_channel_id, &bat_state);
  72. if(BATTERY_NO_CONNECT == bat_state)
  73. {
  74. return;
  75. }
  76. table_size = sizeof(ql_vbat_temp_table) / sizeof(ql_vbat_temp_table[0]);
  77. ql_adc_get_volt(ql_adc_channel_id, &ntc_vol);
  78. for (i = 0; i < table_size; i++)
  79. {
  80. if (ntc_vol >= ql_vbat_temp_table[i][0])
  81. {
  82. break;
  83. }
  84. }
  85. if (i < table_size)
  86. {
  87. *bat_temperature = ql_vbat_temp_table[i][1];
  88. }
  89. else
  90. {
  91. *bat_temperature = ql_vbat_temp_table[table_size - 1][1];
  92. }
  93. QL_CHARGERDEMO_LOG("bat_temperature=%d, ntc_vol=%d", *bat_temperature, ntc_vol);
  94. }
  95. #endif
  96. static void ql_charge_demo_thread(void *param)
  97. {
  98. QL_CHARGERDEMO_LOG("charger demo thread enter, param 0x%x", param);
  99. uint32_t chg_vol = 0;
  100. uint32_t vbus_state = 0;
  101. uint32_t battery_vol = 0;
  102. uint32_t battery_level = 0;
  103. uint32_t charger_cur = 0;
  104. ql_charge_status_e charge_status;
  105. ql_charge_config_t charge_config =
  106. {
  107. 4150, //rechg_vol
  108. 4210, //chg_end_vol
  109. QL_CHARGER_CURRENT_700MA, //standard_chg_current
  110. 18000, //chg_timeout, unit:s
  111. 9000, //chg_ontime_ms
  112. 1000, //chg_offtime_ms
  113. }; //the above is the original default value of unisoc
  114. ql_charge_config_t charge_config_obtain = {0};
  115. #if QL_ENABLE_BATTERY_DETECT
  116. int bat_temperature = 0;
  117. uint8_t bat_state = 0;
  118. #endif
  119. ql_charge_set_config(&charge_config);
  120. ql_charge_get_config(&charge_config_obtain);
  121. QL_CHARGERDEMO_LOG("rechg_vol=%d, chg_end_vol=%d, standard_chg_current=%d, chg_timeout=%d\r\n",
  122. charge_config_obtain.rechg_vol, charge_config_obtain.chg_end_vol,
  123. charge_config_obtain.standard_chg_current, charge_config_obtain.chg_timeout);
  124. QL_CHARGERDEMO_LOG("chg_ontime_ms=%d,chg_offtime_ms=%d\r\n", charge_config_obtain.chg_ontime_ms,charge_config_obtain.chg_offtime_ms);
  125. while(1)
  126. {
  127. ql_get_vchg_vol(&chg_vol);
  128. ql_get_vbus_state(&vbus_state);
  129. ql_get_battery_vol(&battery_vol);
  130. ql_get_battery_level(&battery_level);
  131. ql_get_charge_cur(&charger_cur);
  132. ql_get_charge_status(&charge_status);
  133. QL_CHARGERDEMO_LOG("battery_vol:%d, battery_level:%d\r\n", battery_vol, battery_level);
  134. QL_CHARGERDEMO_LOG("charger_status:%d, charger_cur:%d\r\n",charge_status,charger_cur);
  135. QL_CHARGERDEMO_LOG("chg_vol:%d, vbus_state:%d\r\n",chg_vol, vbus_state);
  136. #if QL_ENABLE_BATTERY_DETECT
  137. bat_state = BATTERY_NO_CONNECT;
  138. bat_temperature = 0;
  139. ql_get_battery_state(QL_ADC0_CHANNEL, &bat_state); //此处使用ADC0检测,客户可以替换为其它ADC通道
  140. ql_get_battery_temperature(QL_ADC0_CHANNEL, &bat_temperature); //此处使用ADC0检测,客户可以替换为其它ADC通道
  141. QL_CHARGERDEMO_LOG("battery_state:%d, battery_temperature:%d\r\n",bat_state,bat_temperature);
  142. if(bat_temperature > 45 || bat_temperature < -5)
  143. {
  144. if(QL_CHG_CHARGING == charge_status)
  145. {
  146. ql_charge_set_over_temp(true); //温度过高/过低保护,停止充电
  147. }
  148. }
  149. else
  150. {
  151. if(QL_CHG_IDLE == charge_status)
  152. {
  153. ql_charge_set_over_temp(false); //取消过高/过低保护,恢复充电
  154. }
  155. }
  156. #endif
  157. if(charge_status==QL_CHG_IDLE)
  158. {
  159. QL_CHARGERDEMO_LOG("battery idle");
  160. }
  161. else
  162. {
  163. QL_CHARGERDEMO_LOG("battery charging");
  164. }
  165. ql_rtos_task_sleep_s(5);
  166. }
  167. ql_rtos_task_delete(NULL);
  168. }
  169. void ql_charge_app_init(void)
  170. {
  171. QlOSStatus err = QL_SUCCESS;
  172. ql_task_t charge_task = NULL;
  173. err = ql_rtos_task_create(&charge_task, 1024, APP_PRIORITY_NORMAL, "ql_chargedemo", ql_charge_demo_thread, NULL, 1);
  174. if( err != QL_SUCCESS )
  175. {
  176. QL_CHARGERDEMO_LOG("charger demo task created failed");
  177. }
  178. }