十二、powerManager

PowerManger模块主要负责电池工作状态,电量监测,充放电管理。

1.1     初始化

在PowerInit()接口中完成了powerManager模块的初始化,在初始化的末端,进行了多个ADC请求,分别读取vref,vthm,vchg,vbat。当这些ADC请求返回时,置位相应的标志位,并获得vref,vthm,vchg,vbat等初始值,当四个变量的初次请求结果都成功返回后,powerinit的四个步骤都已经完成,之后调用PowerInitComplete()通知模块初始化完成。

InitEarlyUserFeatures()à

         configManagerInit(TRUE)à

                  configManagerPower()à

                          powerManagerConfig()à

                                   PowerInit(&theSink.task, config)à

                                            powerMonitorInit();

                                            PowerChargerMonitor();

powerMonitorInit()à

        

         power->task.handler = powerMonitorHandler;

         powerMonitorStart()à

                  powerMonitorReadAdc(&power->task, POWER_INTERNAL_VREF_REQ, 0);

             powerMonitorReadAdc(&power->task, POWER_INTERNAL_VTHM_SETUP, 0);

             powerMonitorReadAdc(&power->task, POWER_INTERNAL_VCHG_REQ, 0);

             powerMonitorReadAdc(&power->task, POWER_INTERNAL_VBAT_REQ, 0);à

                          powerMonitorReadAdc()à

                                   AdcRequest(); /*#MESSAGE_ADC_RESULT message will be sent*/

                                   powerMonitorReadAdc(task, id, period); /* Successful, queue next monitor */

powerMonitor模块handler在接收到ADC请求返回的结果,即MESSAGE_ADC_RESULT时,根据message携带的附加信息,获取当前ADC请求的结果是Vref,temperaure,battery还是charger来进行不同分支处理。如果模块初始化已经完成,且power_init_cfm_sent标志未置位,则调用PowerInitComplete,通知clientTask模块初始化完成。

powerMonitorHandler()àcase MESSAGE_ADC_RESULTà

         powerBatteryHandleTemperatureReading()

         ||powerBatteryHandleVoltageReading()

         || powerChargerHandleVoltageReading()

         PowerInitComplete(power->clientTask, TRUE);

 

PowerInitComplete()à

         POWER_INIT_SET(power_init_cfm_sent);

         MessageSend(clientTask, POWER_INIT_CFM)à

handlePowerMessage()àPOWER_INIT_CFMà

         powerManagerHandleVbat(battery_level_initial_reading

                  event = setting.event;

                  MessageSend(&theSink.task, event, NULL);

                  powerManagerHandleVbatCritical();

                  ||powerManagerHandleVbatLow();

                  || powerManagerHandleVbatNormal();

 

powerBatteryHandleVoltageReading()à

         powerBatterySmoothVoltageReading();

         if(new_level != old_level)

            powerBatterySendVoltageInd();à

                                   MessageSend(power->clientTask, POWER_BATTERY_VOLTAGE_IND)à

handlePowerMessage()àPOWER_BATTERY_VOLTAGE_INDà

powerManagerHandleVbat(ind->vbat, battery_level_automatic);

最终由powerManagerHandleVbat()负责将电量值反馈给上层客户,当然是通过message的方式进行传递的。当检测到电量低或者进入critical状态时,进行紧急处理。

1.2     持续监测

如何保证电量监测工作连续持久地进行下去呢?在ADC请求之后,立即进行下一次检测请求。这也是用信号机制的好处,连续持久地进行某项操作变得非常简单。

powerMonitorReadAdc()à

         AdcRequest(); /*#MESSAGE_ADC_RESULT message will be sent*/

         powerMonitorReadAdc(task, id, period); /* Successful, queue next monitor */

1.3     GATT客户通知

当GATT客户开启了电量通知时,GATT battery server又是如何通知客户的呢?在客户连接后,gattClientAdd()中,如果发现通知开启,则向ble_task发送#BATTERY_READ_TIMER消息,msgHandler在收到该消息后,通过powerManagerBatteryLevelAsPercentage()获取当前的电量值,注意该接口不会进行ADC请求获取电压值,而是直接使用上一次获取的电压值。在该接口中完成电压值到电量值的映射,如果你希望修改这种映射机制,可以在该接口中完成。

另外,当修改battery server的battery level notification属性,即从失能到使能时,也会发送BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER给bletask。

sinkGattManagerMsgHandlerà GATT_MANAGER_REMOTE_CLIENT_CONNECT_CFMà

         handleGattManagerRemoteClientConnectCfm()à

                  gattClientAdd(cfm->cid, &cfm->taddr, ble_gap_role_peripheral)à

                          MessageSend(BleTask, BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER, 0 );à

sinkGattManagerMsgHandlerà GATT_BATTERY_SERVER_WRITE_CLIENT_CONFIG_INDà

         handleWriteBatteryLevelClientConfig()à

                  MessageSend(BleTask, BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER, 0 );

 

bleInternalMsgHandler()àBLE_INTERNAL_MESSAGE_BATTERY_READ_TIMERà

         sinkBleBatteryLevelReadSendAndRepeat()à

                  GattBatteryServerSendLevelNotification(powerManagerBatteryLevelAsPercentage

                          GattManagerRemoteClientNotify(&battery_level);

                  MessageSendLater(BleTask, BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER, 30000 );

 

posted on 2019-01-29 13:54  arduino  阅读(541)  评论(0编辑  收藏  举报

导航