Skip to content

Commit

Permalink
Added acquisition of battery voltage, charge and discharge current lo…
Browse files Browse the repository at this point in the history
…gic, increased limit current to 450ma, modified flash adjustment method (#8)

* new flash  led driver added

* Added flash brightness adjustment features and maximum current limiting features

* Define the command sent by Bluetooth to stm flash led

* Add charging type identification logic (usb charging and wireless charging)

* Adjust back to get_charge_type logic

* Added acquisition of battery voltage, charge and discharge current logic, increased limit current to 450ma, modified flash adjustment method
  • Loading branch information
guowei0105 authored Jan 18, 2024
1 parent 5594e1e commit c24f283
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/driver_3rdparty/axp/axp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
AXP:300~2550,100/step
*/
/*充电电流,uA*/
#define STACHGCUR 750*1000
#define STACHGCUR 450*1000


/*目标充电电压,mV*/
Expand Down
10 changes: 8 additions & 2 deletions app/driver_3rdparty/axp/axp_supply.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@
#define AXP_ADC_GPIO2_ENABLE (1 << 1)
#define AXP_ADC_GPIO3_ENABLE (1 << 0)
#define AXP_ADC_CONTROL3 (0x84)
#define AXP_VBATH_RES (0x78)
#define AXP_VBATH_RES (0x78) //电池电压高位寄存器
#define AXP_VTS_RES (0x58)
#define AXP_VBATL_RES (0x79)
#define AXP_VBATL_RES (0x79) //电池电压低位寄存器
#define AXP_OCVBATH_RES (0xBC)
#define AXP_OCVBATL_RES (0xBD)
#define AXP_INTTEMP (0x56)

#define AXP_CCBATH_RES (0X7A) //电池充电电流高位寄存器
#define AXP_CCBATL_RES (0X7B) //电池充电电流低位寄存器
#define AXP_DCBATH_RES (0X7C) //电池放电电流高位寄存器
#define AXP_DCBATL_RES (0X7D) //电池放电电流低位寄存器

#define AXP_DATA_BUFFER0 AXP_BUFFER1
#define AXP_DATA_BUFFER1 AXP_BUFFER2
#define AXP_DATA_BUFFER2 AXP_BUFFER3
Expand Down
1 change: 1 addition & 0 deletions app/driver_3rdparty/lm/lm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define LM36011_LED_BRIGHTNESS (0x04) //control light brightness
#define LM36011_LED_RESET (0x06) //control light reset
#define LM36011_LED_MAX_SSC (0x20) //max ssc 79ma
#define LM36011_LED_MAX_PRECENT 100
#define LED_FLASHLIGHT_MODE 2
#define LED_FLASHLIGHT_OFF 0
#define LED_BRIGHTNESS_VALUE 0x15 // LED brightness value
Expand Down
16 changes: 12 additions & 4 deletions app/flashled_manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@
//开启灯及设置亮度值
ret_code_t set_led_brightness(uint8_t brightness)
{
uint8_t brightness_regist_value = 0;
ret_code_t ret;
//关闭闪光灯
if (brightness == 0)
{
ret = lm36011_write(LM36011_LED_STATUS, LED_FLASHLIGHT_OFF);
return ret;
}
// 判断是否超过最大允许电流,如果是,设置为最大电流
if (brightness >= LM36011_LED_MAX_SSC)
// 判断是否超过最大设置比例
if (brightness >= LM36011_LED_MAX_PRECENT)
{
brightness = LM36011_LED_MAX_SSC;
brightness = LM36011_LED_MAX_PRECENT;
}
// 计算对应亮度比例对应的值
brightness_regist_value = ((uint32_t)brightness * LM36011_LED_MAX_SSC) / LM36011_LED_MAX_PRECENT;
// 判断是否超过最大允许电流,如果是,设置为最大电流(防止后期亮度比例允许超过100)
if (brightness_regist_value >= LM36011_LED_MAX_SSC)
{
brightness_regist_value = LM36011_LED_MAX_SSC;
}
//开启闪光灯手电筒模式
ret = lm36011_write(LM36011_LED_STATUS, LED_FLASHLIGHT_MODE);
if (ret != LED_CONTROL_SUCCESS) {
return ret;
}
nrf_delay_ms(100);
ret = lm36011_write(LM36011_LED_BRIGHTNESS, brightness);
ret = lm36011_write(LM36011_LED_BRIGHTNESS, brightness_regist_value);
//NRF_LOG_INFO("setting status = %d", ret);
return ret;
}
Expand Down
66 changes: 64 additions & 2 deletions app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
#define LED_GET_BRIHTNESS 2


#define BAT_DEF 0
#define SEND_BAT_VOL 1
#define SEND_BAT_CHARGE_CUR 2
#define SEND_BAT_DISCHARGE_CUR 3


#define NO_CHARGE 0
#define USB_CHARGE 1
#define ERROR_STA 2
Expand Down Expand Up @@ -252,6 +258,7 @@
#define BLE_PWR_PERCENT 0X04

#define BLE_CMD_FLASH_LED_STA 0x0C
#define BLE_CMD_BAT_CV_MSG 0x0D
//end BLE send CMD
//
#define UART_CMD_BLE_CON_STA 0x01
Expand Down Expand Up @@ -290,6 +297,12 @@
#define ST_CMD_LED 0X85
#define ST_SEND_SET_LED_BRIGHTNESS 0X01
#define ST_SEND_GET_LED_BRIGHTNESS 0X02
//
#define STM_CMD_BAT 0X86
#define STM_SEND_BAT_VOL 0X01 //Send battery voltage
#define STM_SEND_BAT_CHARGE_CUR 0X02 //Send battery charge current
#define STM_SEND_BAT_DISCHARGE_CUR 0X03 //Send battery discharge current




Expand Down Expand Up @@ -361,6 +374,7 @@ static volatile uint8_t ble_conn_nopair_flag = BLE_DEF;
static volatile uint8_t pwr_status_flag = PWR_DEF;
static volatile uint8_t trans_info_flag = UART_DEF;
static volatile uint8_t led_brightness_flag = LED_DEF;
static volatile uint8_t bat_msg_flag = BAT_DEF;
static volatile uint8_t ble_trans_timer_flag=TIMER_INIT_FLAG;
static uint8_t mac_ascii[12];
static uint8_t mac[6]={0x42,0x13,0xc7,0x98,0x95,0x1a}; //Device MAC address
Expand Down Expand Up @@ -1813,13 +1827,31 @@ void uart_event_handle(app_uart_evt_t * p_event)
{
led_brightness_flag = LED_GET_BRIHTNESS;
}
else if (ST_SEND_SET_LED_BRIGHTNESS == data_array[5])
else if (ST_SEND_SET_LED_BRIGHTNESS == data_array[5])
{
led_brightness_flag = LED_SET_BRIHTNESS;
led_brightness_value = data_array[6];
}
break;

case STM_CMD_BAT:
switch (data_array[5])
{
case STM_SEND_BAT_VOL:
bat_msg_flag = SEND_BAT_VOL;
break;
case STM_SEND_BAT_CHARGE_CUR:
bat_msg_flag = SEND_BAT_CHARGE_CUR;
break;
case STM_SEND_BAT_DISCHARGE_CUR:
bat_msg_flag = SEND_BAT_DISCHARGE_CUR;
break;
default:
bat_msg_flag = BAT_DEF;
break;
}
break;

default:
break;
}
Expand Down Expand Up @@ -2535,6 +2567,34 @@ static void led_ctl_process(void *p_event_data,uint16_t event_size)
}
}


static void bat_msg_report_process(void *p_event_data,uint16_t event_size)
{

if(bat_msg_flag == SEND_BAT_VOL ||
bat_msg_flag == SEND_BAT_CHARGE_CUR ||
bat_msg_flag == SEND_BAT_DISCHARGE_CUR)
{
uint8_t bat_values[2] = {0};
// 根据 bat_msg_flag 选择相应的寄存器地址
uint8_t axp_reg = (bat_msg_flag == SEND_BAT_VOL) ? AXP_VBATH_RES :
(bat_msg_flag == SEND_BAT_CHARGE_CUR) ? AXP_CCBATH_RES :
AXP_DCBATH_RES;
//获取电池对应信息
get_battery_cv_msg(axp_reg, bat_values);

#ifdef UART_TRANS
bak_buff[0] = BLE_CMD_BAT_CV_MSG;
bak_buff[1] = bat_msg_flag;
bak_buff[2] = bat_values[0];
bak_buff[3] = bat_values[1];
send_stm_data(bak_buff, 4);
#endif
bat_msg_flag = BAT_DEF;
}
}


static void scheduler_init(void)
{
APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
Expand All @@ -2547,6 +2607,8 @@ static void main_loop(void)
// app_sched_event_put(NULL,NULL,nfc_poll);
app_sched_event_put(NULL,0,ble_resp_data);
app_sched_event_put(NULL,0,led_ctl_process);
app_sched_event_put(NULL,0,bat_msg_report_process);

}

static void watch_dog_init(void){
Expand Down Expand Up @@ -2603,7 +2665,7 @@ int main(void)
{
main_loop();
app_sched_execute();
idle_state_handle();
idle_state_handle();
}
}

Expand Down
12 changes: 12 additions & 0 deletions app/power_manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ uint8_t get_irq_status(void)
}
}


//获取电池相关信息
void get_battery_cv_msg(uint8_t bat_reg_addr,uint8_t bat_value[2])
{
uint8_t val[2] = {0};
axp216_read(bat_reg_addr,2,val);
val[1] &= 0x0F; //不用的位置零
bat_value[0] = val[0];
bat_value[1] = val[1];
}


void set_wakeup_irq(uint8_t set_value)
{
uint8_t reg_val;
Expand Down
2 changes: 2 additions & 0 deletions app/power_manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ extern uint8_t get_charge_status(void);

extern uint8_t get_charge_type(void); //get the charging type when charging

extern void get_battery_cv_msg(uint8_t bat_reg_addr,uint8_t bat_value[2]);

extern uint8_t get_irq_vbus_status(void);

extern uint8_t get_irq_charge_status(void);
Expand Down

0 comments on commit c24f283

Please sign in to comment.