set(COMPONENT_NAME "batterry_service")
-set(PRIVATE_HEADER
-${CMAKE_CURRENT_SOURCE_DIR}/include
+set(PUBLIC_HEADER
+${CMAKE_CURRENT_SOURCE_DIR}
)
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/battery.c
+${CMAKE_CURRENT_SOURCE_DIR}/battery_at_cmd.c
)
set(COMPONENT_PUBLIC_CCFLAGS
set(PUBLIC_DEFINES
SUPPORT_BATTERY_SERVICE
+SUPPORT_AT_BATTERY
)
set(PRIVATE_DEFINES
#include "errcode.h"
#include "common_def.h"
+#include "chip_core_irq.h"
+#include "chip_io.h"
+#include "gpio.h"
+#include "pm_veto.h"
+
#include "battery.h"
#define HAL_ADC_SAMPLE_NUMBER_CONFIG_0 10 /* Channel with buffer manual sample average number. */
}
+
+
+/*****
+ * AGPIO_R9 输出低停止充电,AGPIO_L29 输入低充电中,AGPIO_L20输出高 0.2C充电
+*/
+#include "hal_gpio.h"
+#include "gpio.h"
+#define GPIO_PIN_ENABLE_CHARGE S_AGPIO_R9
+#define GPIO_PIN_ENABLE_CHARGE_0_2C S_AGPIO_L20 //低电流充电
+#define GPIO_PIN_CHARGE_STAT S_AGPIO_L29
+void disable_charge(void)
+{
+ PRINT("disable_charge\r\n");
+ uapi_pin_set_mode(GPIO_PIN_ENABLE_CHARGE, (pin_mode_t)HAL_PIO_FUNC_GPIO);
+ uapi_gpio_set_dir(GPIO_PIN_ENABLE_CHARGE, GPIO_DIRECTION_OUTPUT);
+ uapi_tcxo_delay_ms(20);
+ uapi_gpio_set_val(GPIO_PIN_ENABLE_CHARGE, GPIO_LEVEL_HIGH);
+ uint8_t level = uapi_gpio_get_output_val(GPIO_PIN_ENABLE_CHARGE);
+ PRINT("GPIO_PIN_ENABLE_CHARGE uapi_gpio_get_output_val %d\r\n",level);
+}
+
+void enable_charge(void)
+{
+ PRINT("enable_charge\r\n");
+ uapi_pin_set_mode(GPIO_PIN_ENABLE_CHARGE, (pin_mode_t)HAL_PIO_FUNC_GPIO);
+ uapi_gpio_set_dir(GPIO_PIN_ENABLE_CHARGE, GPIO_DIRECTION_OUTPUT);
+ uapi_tcxo_delay_ms(20);
+ uapi_gpio_set_val(GPIO_PIN_ENABLE_CHARGE, GPIO_LEVEL_LOW);
+ uint8_t level = uapi_gpio_get_output_val(GPIO_PIN_ENABLE_CHARGE);
+ PRINT("GPIO_PIN_ENABLE_CHARGE uapi_gpio_get_output_val %d\r\n",level);
+
+}
+
+
+void enable_02c(void)
+{
+ PRINT("enable_charge_02c\r\n");
+ uapi_pin_set_mode(GPIO_PIN_ENABLE_CHARGE_0_2C, (pin_mode_t)HAL_PIO_FUNC_GPIO);
+ uapi_gpio_set_dir(GPIO_PIN_ENABLE_CHARGE_0_2C, GPIO_DIRECTION_OUTPUT);
+ uapi_tcxo_delay_ms(20);
+ uapi_gpio_set_val(GPIO_PIN_ENABLE_CHARGE_0_2C, GPIO_LEVEL_LOW); //拉低低电流充电
+ uint8_t level = uapi_gpio_get_output_val(GPIO_PIN_ENABLE_CHARGE_0_2C);
+ PRINT("GPIO_PIN_ENABLE_CHARGE_0_2C uapi_gpio_get_output_val %d\r\n",level);
+}
+
+
+void disable_02c(void)
+{
+ PRINT("disable_charge_02c\r\n");
+ uapi_pin_set_mode(GPIO_PIN_ENABLE_CHARGE_0_2C, (pin_mode_t)HAL_PIO_FUNC_GPIO);
+ uapi_gpio_set_dir(GPIO_PIN_ENABLE_CHARGE_0_2C, GPIO_DIRECTION_OUTPUT);
+ uapi_tcxo_delay_ms(20);
+ uapi_gpio_set_val(GPIO_PIN_ENABLE_CHARGE_0_2C, GPIO_LEVEL_HIGH); //拉高正常电流充电
+ uint8_t level = uapi_gpio_get_output_val(GPIO_PIN_ENABLE_CHARGE_0_2C);
+ PRINT("GPIO_PIN_ENABLE_CHARGE_0_2C uapi_gpio_get_output_val %d\r\n",level);
+
+}
+/**
+ * return 0 not charged, 1 charging
+*/
+// uint8_t get_charge_stat(void)
+// {
+// PRINT("get_charge_stat\r\n");
+// uapi_pin_set_mode(GPIO_PIN_CHARGE_STAT, (pin_mode_t)HAL_PIO_FUNC_GPIO);
+// uapi_gpio_set_dir(GPIO_PIN_CHARGE_STAT, GPIO_DIRECTION_INPUT);
+// uapi_pin_set_ie(GPIO_PIN_CHARGE_STAT,HAL_PIO_IE_ENBALE);
+// uapi_tcxo_delay_ms(20);
+// uapi_gpio_clear_interrupt(GPIO_PIN_CHARGE_STAT);
+// uint8_t level = uapi_gpio_get_output_val(GPIO_PIN_CHARGE_STAT);
+// PRINT("GPIO_PIN_CHARGE_STAT uapi_gpio_get_output_val %d\r\n",level);
+// level = uapi_gpio_get_val(GPIO_PIN_CHARGE_STAT);
+// PRINT("GPIO_PIN_CHARGE_STAT uapi_gpio_get_val %d\r\n",level);
+// if (GPIO_LEVEL_HIGH == level)
+// return 0;
+// else
+// return 1;
+// }
+
+#define ULP_WKUP_INT_STS 0x5702C1E0
+
+/**
+ * return 0 not charged, 1 charging
+*/
+uint8_t get_charge_stat(void)
+{
+ uint16_t vbus_status = reg32_getbit(ULP_WKUP_INT_STS, 0x4);
+ uint16_t g_ulp_wkup_flag = readw(ULP_WKUP_INT_STS);
+ PRINT("get_charge_stat %02X %02X\r\n",vbus_status,g_ulp_wkup_flag);
+ if (vbus_status == 1) {
+ return 1;
+ }
+
+ return 0;
+}
+#include "tcxo.h"
uint8_t battery_get_voltage(float *voltage)
{
+
+ disable_charge();
+ uapi_tcxo_delay_ms(100);
battery_get_voltage_by_channel(voltage,2);
uint8_t percent = 0;
-
+ enable_charge();
if (*voltage <= BATTERY_VOLTAGE_MIN)
return 0;
if (*voltage >= BATTERY_VOLTAGE_MAX)
return 100;
percent = (*voltage - BATTERY_VOLTAGE_MIN)*100/(BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN) ;
return percent;
-}
\ No newline at end of file
+}
+
*/
uint8_t battery_get_voltage(float *voltage);
+/**
+ * return 0 not charged, 1 charging
+*/
+uint8_t get_charge_stat(void);
+
#ifdef __cplusplus
#if __cplusplus
}
--- /dev/null
+#include "at_cmd_api.h"
+#include "pm_control.h"
+#include "debug_print.h"
+#include "battery.h"
+#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
+#define BATTERY_MAX_ARGC 8
+#define BATTERY_MAX_ARGV_LEN 64
+
+#define BATTERY_SUCCESS 0
+#define BATTERY_FAILED (-1)
+static int ConvertCmdToParam(uint8_t *para, uint32_t paraLen,
+ uint8_t argv[BATTERY_MAX_ARGC][BATTERY_MAX_ARGV_LEN], uint32_t* argc)
+{
+ uint32_t pos = 0;
+ *argc = 0;
+
+ for (uint32_t i = 0; i < paraLen; i++) {
+ if ((para[i] == ' ' || para[i] == '\n') && pos != 0) {
+ argv[*argc][pos] = '\0';
+ *argc += 1;
+ pos = 0;
+ if (*argc >= BATTERY_MAX_ARGC) {
+ return BATTERY_FAILED;
+ }
+ } else {
+ argv[*argc][pos] = para[i];
+ pos++;
+ if (pos >= BATTERY_MAX_ARGV_LEN) {
+ return BATTERY_FAILED;
+ }
+ }
+ }
+ if (pos != 0) {
+ *argc += 1;
+ }
+
+ return BATTERY_SUCCESS;
+}
+
+static uint32_t battery_do_test(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+ UNUSED(strCommonCmd);
+ uint8_t argv[BATTERY_MAX_ARGC][BATTERY_MAX_ARGV_LEN] = { 0 };
+ uint32_t argc = 0;
+
+ int ret = ConvertCmdToParam(para, paraLen, argv, &argc);
+ if (ret != BATTERY_SUCCESS) {
+ PRINT("BATTERYTEST ATCMD Convert Command Failed\n");
+ return ERRCODE_FAIL;
+ }
+
+ if (argc != 1) {
+ PRINT("BATTERYTEST ATCMD Set Parameter Failed\n");
+ return ERRCODE_FAIL;
+ }
+ char *mode = (char*)argv[0];
+ if (strcmp(mode, "enable") == 0) {
+ enable_charge();
+ } else if (strcmp(mode, "disable") == 0) {
+ disable_charge();
+ } else if (strcmp(mode, "en02c") == 0) {
+ enable_02c();
+ } else if (strcmp(mode, "dis02c") == 0) {
+ disable_02c();
+ }
+ return ERRCODE_SUCC;
+}
+
+static const at_cmd_table_t g_battery_cmd[] = {
+ { (char *)"AT^CHARGETEST", battery_do_test, (char *)"enable" },
+};
+
+at_cmd_table_t *get_battery_cmd_table(uint32_t *cmd_count)
+{
+ *cmd_count = ARRAY_COUNT(g_battery_cmd);
+ return (at_cmd_table_t *)g_battery_cmd;
+}
--- /dev/null
+/**
+ * Copyright (c) @CompanyNameMagicTag 2022-2022. All rights reserved. \n
+ *
+ * Description: Provides at cmd register func \n
+ * Author: @CompanyNameTag \n
+ * History: \n
+ * 2022-07-20, Create file. \n
+ */
+#ifndef BATTERY_AT_PROCESS_H
+#define BATTERY_AT_PROCESS_H
+
+#include <stdint.h>
+#include <stdio.h>
+#include "errcode.h"
+#include "soc_osal.h"
+#include "at_cmd_api.h"
+
+#ifdef __cplusplus
+#if __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+#endif /* __cplusplus */
+
+at_cmd_table_t *get_battery_cmd_table(uint32_t *cmd_count);
+
+#ifdef __cplusplus
+#if __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* __cplusplus */
+
+#endif
#include "hx3695_at_cmd.h"
#endif
+#ifdef SUPPORT_AT_BATTERY
+#include "battery_at_cmd.h"
+#endif
+
#ifdef SUPPORT_AT_LIS2DOC
#include "lis2doc_at_cmd.h"
#endif
at_cmd_table_t *hx3695_table = get_hx3695_cmd_table(&hx3695_cmd_count);
at_cmd_add_moudle(hx3695_table, hx3695_cmd_count);
#endif
+#ifdef SUPPORT_AT_BATTERY
+ uint32_t battery_cmd_count;
+ at_cmd_table_t *battery_table = get_battery_cmd_table(&battery_cmd_count);
+ at_cmd_add_moudle(battery_table, battery_cmd_count);
+#endif
#ifdef SUPPORT_AT_LIS2DOC
uint32_t lis2doc_cmd_count;
return(uapi_adc_auto_scan_ch_disable(channel));
}
#endif
-extern bool battery_get_voltage_by_channel(float *voltage, uint8_t channel);
+// extern bool battery_get_voltage_by_channel(float *voltage, uint8_t channel);
extern uint8_t battery_get_voltage(float *voltage);
+extern uint8_t get_charge_stat(void);
static uint32_t adc_manu_sample_adc(uint8_t *para, uint32_t para_len)
{
char *ptr = (char *)para;
uint32_t ret = 0;
float voltage = 0.0F ;
- uint32_t channel = strtoul(ptr, NULL, 10); // 10: 十进制
+ // uint32_t channel = strtoul(ptr, NULL, 10); // 10: 十进制
// battery_get_voltage_by_channel(&voltage,channel);
+ uint8_t charged = get_charge_stat();
uint8_t percent = battery_get_voltage(&voltage);
- wstp_print("manu adc sample , channel = %x , voltage = %.03f , percent = %u\r\n", channel,voltage,percent);
+ wstp_print("manu adc sample , charged = %x , voltage = %.03f , percent = %u\r\n", charged,voltage,percent);
return 0;
}
static uint32_t cat1_power_on(uint8_t *para, uint32_t para_len)
//S_AGPIO_L21 NTC_VDD_ON 给低打开 NTC上拉电源
{ S_AGPIO_L21, HAL_GPIO_DIRECTION_OUTPUT, HAL_GPIO_LEVEL_LOW, 0, NULL },
-
+// #define GPIO_PIN_ENABLE_CHARGE S_AGPIO_R9
+// #define GPIO_PIN_ENABLE_CHARGE_0_2C S_AGPIO_L20 //低电流充电
+ { S_AGPIO_R9, HAL_GPIO_DIRECTION_OUTPUT, HAL_GPIO_LEVEL_LOW, 0, NULL },
+ { S_AGPIO_L20, HAL_GPIO_DIRECTION_OUTPUT, HAL_GPIO_LEVEL_HIGH, 0, NULL },
+
};
/**
* @}