changeset 145:246f4a7dd92b

src/cs/drivers/drv_app/pwr: import from MV100 source
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 09 Oct 2016 05:59:14 +0000
parents fd8227e3047d
children fb95e3f83dc5
files src/cs/drivers/drv_app/pwr/pwr_analog_dev.h src/cs/drivers/drv_app/pwr/pwr_api.c src/cs/drivers/drv_app/pwr/pwr_api.h src/cs/drivers/drv_app/pwr/pwr_cust.c src/cs/drivers/drv_app/pwr/pwr_cust.h src/cs/drivers/drv_app/pwr/pwr_disch.c src/cs/drivers/drv_app/pwr/pwr_disch.h src/cs/drivers/drv_app/pwr/pwr_env.c src/cs/drivers/drv_app/pwr/pwr_env.h src/cs/drivers/drv_app/pwr/pwr_liion_cha.c src/cs/drivers/drv_app/pwr/pwr_liion_cha.h src/cs/drivers/drv_app/pwr/pwr_messages.c src/cs/drivers/drv_app/pwr/pwr_messages.h src/cs/drivers/drv_app/pwr/pwr_messages_i.c src/cs/drivers/drv_app/pwr/pwr_messages_i.h src/cs/drivers/drv_app/pwr/pwr_process.c src/cs/drivers/drv_app/pwr/pwr_process.h
diffstat 17 files changed, 2965 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_analog_dev.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ *
+ * pwr_analog_dev.h
+ *
+ * Purpose: This file contains definitions that can be used by any SWE
+ *          for power management services.
+ *
+ * Author    Candice Bazanegue (c-brille@ti.com)
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_ANALOG_DEV_H__
+#define __PWR_ANALOG_DEV_H__
+
+#ifndef _WINDOWS
+   #include "chipset.cfg"
+#endif
+
+/* Constants relative to the analog device */
+
+#if (ANALOG == 1) 
+
+#define VOLTAGE_LOOP_OFFSET   400   /* (mV) measured */
+#define MADC_OFFSET           160   /* (mV) measured */
+
+#define DAC_THRESHOLD         240   /* (mV) spec */
+#define DAC_VOLTAGE_STEP      1875  /* (uV) spec */
+#define DAC_CURRENT_STEP      937   /* (uA) */
+#define MADC_VOLTAGE_STEP     1710  /* (uV) spec */
+#define MADC_CURRENT_STEP     855   /* (uA) spec */
+
+#elif (ANALOG == 2)
+
+#define VOLTAGE_LOOP_OFFSET   200   /* (mV) measured */
+#define MADC_OFFSET           160   /* (mV) measured */
+
+#define DAC_THRESHOLD         0     /* (mV) spec */
+#define DAC_VOLTAGE_STEP      1710  /* (uV) spec */
+#define DAC_CURRENT_STEP      855   /* (uA) */
+#define MADC_VOLTAGE_STEP     1710  /* (uV) spec */
+#define MADC_CURRENT_STEP     855   /* (uA) spec */
+
+#endif	// ANALOG
+
+#endif /* __PWR_ANALOG_DEV_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_api.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,240 @@
+/*******************************************************************************
+ *
+ * pwr_api.c
+ *
+ * Purpose: This file contains functions that can be accessed by any SWE
+ *          to use power management services.
+ *
+ * Author    Candice Bazanegue (c-brille@ti.com)
+ *
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "rvm_use_id_list.h"
+#include "spi_api.h"
+#include "spi_env.h"
+#include "pwr_env.h"
+#include "pwr_cust.h"
+#include "pwr_messages.h"
+#include "pwr_messages_i.h"
+
+/* Define a pointer to the PWR Environment control block.   */
+extern T_PWR_ENV_CTRL_BLK *pwr_env_ctrl_blk;
+
+
+/*******************************************************************************
+**
+** Function         power_info_register
+**
+** Description      Function called by the MMI to enable (or disable) the receipt
+**                  of several events related to the charging process or the 
+**                  discharge levels.
+**
+**
+*******************************************************************************/
+T_POWER_RET power_info_register(BOOLEAN info_enable, T_RV_RETURN return_path)
+{
+   if (((pwr_env_ctrl_blk->power_info.return_path.callback_func)!= NULL) || 
+      (pwr_env_ctrl_blk->power_info.return_path.addr_id != RVF_INVALID_ADDR_ID))
+   {
+      rvf_send_trace("power_info_register : info already registered, performs another registration",
+                     76,
+                     NULL_PARAM,
+                     RV_TRACE_LEVEL_DEBUG_HIGH,
+                     PWR_USE_ID);
+   }
+   pwr_env_ctrl_blk->power_info.info_enable                 = info_enable;
+   pwr_env_ctrl_blk->power_info.return_path.addr_id         = return_path.addr_id;
+   pwr_env_ctrl_blk->power_info.return_path.callback_func   = return_path.callback_func;
+
+   return (POWER_OK);
+
+}
+
+
+
+/*******************************************************************************
+**
+** Function         power_alert_register
+**
+** Description      Function called by the MMI to configure the threshold of 
+**                  remaining energy in the battery, under which the MMI will
+**                  receive low battery alerts.
+**
+**
+*******************************************************************************/
+T_POWER_RET power_alert_register(T_PWR_PERCENT remain_capa_threshold,
+                                 T_RV_RETURN return_path)
+{
+
+   if (((pwr_env_ctrl_blk->power_alert.return_path.callback_func)!= NULL) || 
+      (pwr_env_ctrl_blk->power_alert.return_path.addr_id != RVF_INVALID_ADDR_ID))
+   {
+      rvf_send_trace("power_alert_register : info already registered, performs another registration",
+                     77,
+                     NULL_PARAM,
+                     RV_TRACE_LEVEL_DEBUG_HIGH,
+                     PWR_USE_ID);
+   }
+   pwr_env_ctrl_blk->power_alert.remain_capa_threshold      = remain_capa_threshold;
+   pwr_env_ctrl_blk->power_alert.return_path.addr_id        = return_path.addr_id;
+   pwr_env_ctrl_blk->power_alert.return_path.callback_func  = return_path.callback_func;
+
+   return (POWER_OK);
+
+}
+
+
+/*******************************************************************************
+**
+** Function         power_emergency_register
+**
+** Description      Function called by the MMI to define the call-back function 
+**                  which will be called in case of emergency switch OFF.
+**
+**
+*******************************************************************************/
+T_POWER_RET power_emergency_register(T_RV_RETURN return_path)
+{
+
+   if (((pwr_env_ctrl_blk->power_emergency.callback_func)!= NULL) || 
+      (pwr_env_ctrl_blk->power_emergency.addr_id != RVF_INVALID_ADDR_ID))
+   {
+      rvf_send_trace("power_emergency_register : info already registered, performs another registration",
+                     80,
+                     NULL_PARAM,
+                     RV_TRACE_LEVEL_DEBUG_HIGH,
+                     PWR_USE_ID);
+   }
+   pwr_env_ctrl_blk->power_emergency.addr_id       = return_path.addr_id;
+   pwr_env_ctrl_blk->power_emergency.callback_func = return_path.callback_func;
+
+   return (POWER_OK);
+
+}
+
+
+
+/*******************************************************************************
+**
+** Function         pwr_callback_func
+**
+** Description      
+**
+**
+**
+*******************************************************************************/
+void pwr_callback_func(void *ptr)
+{
+   T_PWR_CHARGING_STATE charge_started;
+
+
+   /* Charge started? */
+   charge_started = pwr_env_ctrl_blk->charging_state;
+
+   if (charge_started == CHARGE_STOPPED)
+   {
+      /* Ask for battery temperature measurement */
+      pwr_get_battery_temperature();
+   }
+
+   else if ((charge_started == CI_CHARGE_STARTED) ||
+            (charge_started == CV_CHARGE_STARTED))
+   {
+      /* The battery temperature has been measured during the charge */
+      pwr_get_bat_info();
+   }
+
+}
+
+
+
+
+/*******************************************************************************
+**
+** Function         pwr_get_bat_info
+**
+** Description      
+**
+**
+*******************************************************************************/
+void pwr_get_bat_info(void)
+{
+   UINT16 bat_voltage, bat_madc_voltage;
+   UINT16 charger_current, bat_madc_current;
+   UINT16 status;
+   BOOLEAN charger_plugged;
+   T_PWR_CHARGING_STATE charge_started;
+   T_PWR_PERCENT remain_capacity;
+   INT16 bat_temperature;
+
+
+   /* Voltage and current */
+   /* Read the conversion results in the analog device registers */
+   bat_madc_voltage = ABB_Read_Register_on_page(PAGE0, VBATREG);
+   bat_madc_current = ABB_Read_Register_on_page(PAGE0, ICHGREG);
+
+   bat_voltage = pwr_adc_to_mvolt(bat_madc_voltage);
+   charger_current = pwr_adc_to_mA(bat_madc_current);
+
+   /* Remaining capacity in the battery */
+   remain_capacity = pwr_get_capacity_vs_voltage(bat_voltage);
+
+   /* Charger plugged? */
+   status = ABB_Read_Status();
+   if (status & CHGPRES)
+   {
+      charger_plugged = TRUE;
+   }
+   else
+   {
+      charger_plugged = FALSE;
+   }
+
+   /* Charge started? */
+   charge_started = pwr_env_ctrl_blk->charging_state;
+
+   /* The battery temperature is already measured during the charge */
+   /* Use the last result */
+   bat_temperature = pwr_env_ctrl_blk->bat_celsius_temp;
+
+   /* Send the results to the upper layer */
+   pwr_send_battery_info(bat_voltage, bat_temperature, charger_current,
+                         charger_plugged, charge_started,
+                         remain_capacity);
+}
+
+
+
+
+/*******************************************************************************
+**
+** Function         power_battery_information
+**
+** Description      Function called by the MMI to get information concerning
+**                  the battery.
+**
+**
+*******************************************************************************/
+T_POWER_RET power_battery_information(void)
+{
+   T_RV_RET spi_return;
+
+   spi_return = pwr_spi_battery_info(pwr_callback_func);
+   if (spi_return != RV_OK)
+   {
+      return (POWER_ERROR);
+   }
+
+   return (POWER_OK);
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_api.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ *
+ * pwr_api.h
+ *
+ * Purpose: This file contains definitions that can be used by any SWE
+ *          for power management services.
+ *
+ * Author    Candice Bazanegue (c-brille@ti.com)
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_API_H__
+#define __PWR_API_H__
+
+#include "rv_general.h"
+
+typedef T_RV_RET T_POWER_RET;
+
+typedef enum
+{
+    POWER_OK      =  0,    /* OK */
+    POWER_ERROR   = -1     /* ERROR */
+};
+
+
+/* Define the different charging process states */
+typedef enum
+{
+   CHARGE_STOPPED,
+   TESTING_BATTERY,
+   CI_CHARGE_STARTED,   /* constant current charge */
+   CV_CHARGE_STARTED    /* constant voltage charge */
+}T_PWR_CHARGING_STATE;
+
+
+/* Define the different states for TIMER0 use */
+typedef enum
+{
+   BATTERY_TYPE_TEST,
+   BATTERY_SHORT_TEST,
+   BATTERY_OPEN_TEST,
+   BATTERY_50UA_TEMP_TEST,
+   BATTERY_10UA_TEMP_TEST,
+   BATTERY_CALIBRATION
+}T_PWR_TIMER0_STATE;
+
+
+typedef struct
+{
+   BOOLEAN        info_enable;
+   T_RV_RETURN    return_path;
+}T_POWER_INFO;
+
+typedef UINT8 T_PWR_PERCENT;
+
+typedef struct
+{
+   T_PWR_PERCENT  remain_capa_threshold;
+   T_RV_RETURN    return_path;
+}T_POWER_ALERT;
+
+typedef enum
+{
+   BAT_TEMP_OUTSIDE_LIMITS,
+   BAT_OPEN_TEST_FAILED,
+   BAT_SHORT_TEST_FAILED,
+   BAT_TYPE_UNKNOWN
+} T_BAT_PROBLEM;
+
+
+
+
+/* Prototypes */
+
+T_POWER_RET power_info_register(BOOLEAN info_enable, T_RV_RETURN return_path);
+T_POWER_RET power_alert_register(T_PWR_PERCENT remain_capa_threshold, T_RV_RETURN return_path);
+T_POWER_RET power_emergency_register(T_RV_RETURN return_path);
+T_POWER_RET power_battery_information(void);
+void pwr_get_bat_info(void);
+
+#endif /* __PWR_API_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_cust.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,551 @@
+/*******************************************************************************
+ *
+ * pwr_cust.c
+ *
+ * Purpose: This file contains functions for battery management.
+ *          These functions can be modified by the customer.
+ *
+ * Author   Candice Bazanegue (c-brille@ti.com)
+ *
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "abb.h"
+#include "rvm_use_id_list.h"
+#include "pwr_messages.h"
+#include "rvf_api.h"
+#include "pwr_cust.h"
+#include "pwr_env.h"
+#include "pwr_analog_dev.h"
+#include "spi_env.h"
+#include "spi_task.h"
+#include "pwr_liion_cha.h"
+#include "pwr_disch.h"
+
+/* Caution: keep ascending order in the temperature arrays !!! */
+
+const INT16 BAT_Celsius_temp_10uA[4]=
+{
+   -10, -5, 0, 5
+};
+const UINT16 BAT_MADC_temp_10uA[4]=
+{
+   0x13F, 0x103, 0xCB, 0x9C
+};
+const INT16 BAT_Celsius_temp_50uA[13]=
+{
+   0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
+};
+const UINT16 BAT_MADC_temp_50uA[13]=
+{
+   0x351, 0x2AD, 0x22E, 0x1C4, 0x169, 0x128, 0xF9, 0xCB, 0x96, 0x83, 0x68, 0x58, 0x4A
+};
+
+
+/* Correpondence between the battery voltage and the remaining capacity in the battery */
+/* The voltages have to be expressed in mV units */
+/* The capacities are percentages of the total capacity */
+/* Caution: keep this order in the array !!!! (in voltage (or capacity) descending order) */
+
+const T_PWR_THRESHOLDS a_pwr_thresholds[NB_THRESHOLDS]= 
+{{4200,100}, {4000,75}, {3970,50}, {3950,25}, {3900,15}, {3870,10}};
+
+
+
+/* Global variable */
+extern T_SPI_GBL_INFO *SPI_GBL_INFO_PTR;
+
+
+
+/*******************************************************************************
+** Function         pwr_adc_to_mvolt
+**
+** Description      Converts the MADC voltage reading into voltage in mVolt
+**
+** Warning:         The offsets are not taken into account !!!
+**
+*******************************************************************************/
+UINT16 pwr_adc_to_mvolt(UINT16 voltage_madc)
+{
+   UINT16 voltage_mvolt;
+
+   /* Note: /1000 because MADC_VOLTAGE_STEP is expressed in uV */
+   voltage_mvolt = (voltage_madc * MADC_VOLTAGE_STEP * 4) / 1000;
+
+   /* return voltage in mVolt */
+   return (voltage_mvolt);
+}
+
+
+/*******************************************************************************
+** Function         pwr_adc_to_mA
+**
+** Description      Converts the MADC current reading into current in mA
+**
+** Warning:         The offsets are not taken into account !!!
+** 
+*******************************************************************************/
+UINT16 pwr_adc_to_mA(UINT16 current_madc)
+{
+UINT16 current_mA;
+
+   /* Note: /1000 because MADC_VOLTAGE_STEP is expressed in uA */
+   current_mA = (current_madc * MADC_CURRENT_STEP) / 1000;
+
+   /* return current in mA */
+   return (current_mA);
+}
+
+
+
+
+
+/*******************************************************************************
+** Function       pwr_bat_temp_within_limits
+**
+** Description    Check if the battery temperature is within limits
+**
+** Parameter      battery_temperature : battery temperature un Celsius degrees
+**
+*******************************************************************************/
+UINT8 pwr_bat_temp_within_limits(INT16 battery_temperature)
+{
+   rvf_send_trace("Battery temperature (Celsius degrees) ", 38, battery_temperature, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+   if ((battery_temperature < BAT_TEMPERATURE_MAX) &&
+      (battery_temperature > BAT_TEMPERATURE_MIN))
+   {
+      rvf_send_trace("Battery temperature within limits",33, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+      return (TRUE);
+   }
+
+   rvf_send_trace("Battery temperature outside limits",34, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_MEDIUM, PWR_USE_ID); 
+   return (FALSE);
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_madc_to_Celius_conv
+**
+** Description      Find the temperature in Celsius degrees corresponding 
+**                  to the value given by the MADC
+**
+*******************************************************************************/
+UINT8 pwr_madc_to_Celsius_conv(UINT8 bias_current, UINT16 madc_temp, INT16 *celsius_temp)
+{
+   UINT8 last_index, i;
+   const UINT16 *MADC_temp_array; 
+   const INT16 *celsius_temp_array;
+   UINT16 madc_diff;
+   UINT16  madc_inc;
+   UINT16  celsius_inc;
+
+   /* choose the table */
+   switch(bias_current)
+   {
+      case THEN_10uA:
+         MADC_temp_array = BAT_MADC_temp_10uA;
+         celsius_temp_array = BAT_Celsius_temp_10uA;
+         /* get last index of the lookup table array(s) */
+         last_index = sizeof(BAT_MADC_temp_10uA)/sizeof(BAT_MADC_temp_10uA[0]);
+         break;
+
+      case THEN_50uA:
+         MADC_temp_array = BAT_MADC_temp_50uA;
+         celsius_temp_array = BAT_Celsius_temp_50uA;
+         /* get last index of the lookup table array(s) */
+         last_index = sizeof(BAT_MADC_temp_50uA)/sizeof(BAT_MADC_temp_50uA[0]);
+         break;
+
+      default: return (FALSE);
+   }
+
+   /* check the limits */
+   if((madc_temp > MADC_temp_array[0]) || (madc_temp < MADC_temp_array[last_index-1]))
+   {
+      return (FALSE);
+   }
+
+
+   /* find the two points between which the given point lies */
+   for(i=0; i<last_index; i++)
+   {
+      if(madc_temp >= MADC_temp_array[i])
+      {
+         if(i==0)
+         {
+            *celsius_temp = celsius_temp_array[0];
+            return (TRUE);
+         }
+
+         /* the value is between MADC_temp_array[i] and MADC_temp_array[i-1] */
+         /* interpolate to get a more precise value */
+ 
+         madc_inc = MADC_temp_array[i-1] - MADC_temp_array[i];
+         celsius_inc = celsius_temp_array[1] - celsius_temp_array[0];   /* positive value */
+ 
+         /* difference between the given point and the first madc value below this point */
+         madc_diff = madc_temp - MADC_temp_array[i];
+ 
+         *celsius_temp = celsius_temp_array[i] - (madc_diff*celsius_inc)/madc_inc;
+
+         return (TRUE);
+      }
+
+      /* else, try a smaller value */
+   }
+
+   return (FALSE);
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_get_battery_temperature
+**
+** Description      Start MADC temperature reading
+**
+** Note  If the used battery does not allow to make a temperature 
+**       measurement, the body of this function can be replaced 
+**       by just a "return" with a temperature (in Celsius degrees) 
+**       between BAT_TEMPERATURE_MIN and BAT_TEMPERATURE_MAX.
+*******************************************************************************/
+void pwr_get_battery_temperature(void)
+{
+
+   /* set the bias current to 50uA */
+   ABB_Write_Register_on_page(PAGE0, BCICTL1, THEN_50uA);
+   rvf_delay(RVF_MS_TO_TICKS(5));
+
+   pwr_env_ctrl_blk->timer0_state = BATTERY_50UA_TEMP_TEST;
+
+   if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+   {
+      /* start ADIN2REG channel conversion by writing in the result register */
+      ABB_Write_Register_on_page(PAGE0, ADIN2REG, 0x0000);
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_1),
+                       FALSE);
+   }
+   else /* The L1 asks for ADC conversions */
+   {
+      /* Let time for the L1 to ask for new AD conversions */
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_2),
+                       FALSE);
+   }
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_bat_50uA_temp_test_timer_process
+**
+** Description      
+**
+** 
+*******************************************************************************/
+void pwr_bat_50uA_temp_test_timer_process(void)
+{
+   UINT16 bat_madc_temp;
+
+   rvf_send_trace("TIMER0: Battery 50uA temp test",30, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   pwr_env_ctrl_blk->bat_celsius_temp = (INT16)(0xFFFF);
+
+   /* Read ADC result */
+   bat_madc_temp = ABB_Read_Register_on_page(PAGE0, ADIN2REG);
+
+   /* MADC value to Celsius degrees conversion */
+   if (!pwr_madc_to_Celsius_conv(THEN_50uA, bat_madc_temp, &(pwr_env_ctrl_blk->bat_celsius_temp)))
+   {
+      /* outside the 50uA temp range */
+      /* set the bias current to 10uA */
+
+      pwr_env_ctrl_blk->timer0_state = BATTERY_10UA_TEMP_TEST;
+
+      ABB_Write_Register_on_page(PAGE0, BCICTL1, THEN_10uA);
+      rvf_delay(RVF_MS_TO_TICKS(5));
+
+      if (SPI_GBL_INFO_PTR->is_adc_on == FALSE) 
+      {
+         /* start ADIN2REG channel conversion by writing in the result register */
+         ABB_Write_Register_on_page(PAGE0, ADIN2REG, 0x0000);
+         rvf_start_timer (SPI_TIMER0,
+                          RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_1),
+                          FALSE);
+      }
+      else /* The L1 asks for ADC conversions */
+      {
+         /* Let time for the L1 to ask for new AD conversions */
+         rvf_start_timer (SPI_TIMER0,
+                          RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_2),
+                          FALSE);
+      }
+   }
+
+   else
+   {
+      /* The battery temp is inside the 50uA temp range */
+      /* Turn OFF the current source */
+      ABB_Write_Register_on_page(PAGE0, BCICTL1, MESBAT);
+
+      /* Go ahead */
+      if (pwr_env_ctrl_blk->charging_state == CI_CHARGE_STARTED)
+      {
+         /* temperature measurement during CI charge */
+         pwr_CI_charge_process();
+      }
+      else if (pwr_env_ctrl_blk->charging_state == CV_CHARGE_STARTED)
+      {
+         /* temperature measurement during CV charge */
+         pwr_CV_charge_process();
+      }
+      else if (pwr_env_ctrl_blk->charging_state == TESTING_BATTERY)
+      {
+         /* temperature measurement before calibration */
+         pwr_calibration_process();
+      }
+      else
+      {
+         /* temperature measurement for battery information */
+         pwr_get_bat_info();
+      }
+   }
+}
+
+
+/*******************************************************************************
+** Function         pwr_bat_10uA_temp_test_timer_process
+**
+** Description      
+**
+** 
+*******************************************************************************/
+void pwr_bat_10uA_temp_test_timer_process(void)
+{
+   UINT16 bat_madc_temp;
+
+   rvf_send_trace("TIMER0: Battery 10uA temp test",30, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   pwr_env_ctrl_blk->bat_celsius_temp = (INT16)(0xFFFF);
+
+   bat_madc_temp = ABB_Read_Register_on_page(PAGE0, ADIN2REG);
+
+      /* MADC value to Celsius degrees conversion */
+   if (!pwr_madc_to_Celsius_conv(THEN_10uA, bat_madc_temp, &(pwr_env_ctrl_blk->bat_celsius_temp)))
+      {
+         /* ERROR: Reading out of limits */
+         rvf_send_trace("Battery temperature reading out of limits", 41, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+      /* turn OFF the current source */
+      ABB_Write_Register_on_page(PAGE0, BCICTL1, MESBAT);
+
+      pwr_stop_charging();
+
+      /* informs the upper layer that the battery temperature is not correct */
+      pwr_send_charge_not_possible_event(BAT_TEMP_OUTSIDE_LIMITS);
+
+      if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+      {
+#if (ANALOG == 1) 
+         ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+         ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+      }
+      else
+      {
+         pwr_handle_discharge();
+      }
+   }
+
+   else
+   {
+      /* The battery temperature is inside the 10uA temp range */
+   /* turn OFF the current source */
+   ABB_Write_Register_on_page(PAGE0, BCICTL1, MESBAT);
+
+      /* Go ahead */
+      if (pwr_env_ctrl_blk->charging_state == CI_CHARGE_STARTED)
+      {
+         /* temperature measurement during CI charge */
+         pwr_CI_charge_process();
+      }
+      else if (pwr_env_ctrl_blk->charging_state == CV_CHARGE_STARTED)
+      {
+         /* temperature measurement during CV charge */
+         pwr_CV_charge_process();
+      }
+      else if (pwr_env_ctrl_blk->charging_state == TESTING_BATTERY)
+      {
+         /* temperature measurement before calibration */
+         pwr_calibration_process();
+      }
+      else
+      {
+         /* temperature measurement for battery information */
+         pwr_get_bat_info();
+      }
+   }
+}
+
+
+
+
+/*******************************************************************************
+** Function         pwr_get_battery_type
+**
+** Description      Return the type of the battery
+**
+** Note        If the used battery does not allow to make a battery type 
+**             SW detection, the body of this function can be replaced 
+**             by the last 2 lines
+**
+*******************************************************************************/
+void pwr_get_battery_type(void)
+{
+
+   /* enable bias current for main battery type reading 
+   ABB_Write_Register_on_page(PAGE0, BCICTL1, 0x0081);
+   rvf_delay(RVF_MS_TO_TICKS(5));
+
+   pwr_env_ctrl_blk->timer0_state = BATTERY_TYPE_TEST;
+
+   if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+   {
+      /* start ADIN1REG channel conversion by writing in the result register 
+      ABB_Write_Register_on_page(PAGE0, ADIN1REG, 0x0000);
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_1),
+                       FALSE);
+   }
+   else /* The L1 asks for ADC conversions  
+   {
+      /* Let time for the L1 to ask for new AD conversions 
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_2),
+                       FALSE);
+   }
+   */
+
+
+
+   /****************************************/
+   /* If the battery can not be detected,  */
+   /* the code above has to be replaced by */
+   /* those 2 lines (with the type of the  */
+   /* used battery).                       */
+   /****************************************/
+
+   pwr_env_ctrl_blk->bat_type = LI_ION;
+
+   /* Check if the battery is OK */
+   pwr_battery_qualification();
+
+
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_type_test_timer_process
+**
+** Description     
+**
+**
+*******************************************************************************/
+void pwr_type_test_timer_process(void)
+{
+   UINT16 bat_type_voltage;
+
+   rvf_send_trace("TIMER0: Battery type test",25, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   bat_type_voltage = ABB_Read_Register_on_page(PAGE0, ADIN1REG);
+
+   /* turn OFF the current source */
+   ABB_Write_Register_on_page(PAGE0, BCICTL1, MESBAT);
+
+   if ((bat_type_voltage >= BAT_TYPE_LI_ION_MIN) &&
+      (bat_type_voltage <= BAT_TYPE_LI_ION_MAX))   /* Li-ion batteries */
+   {
+      pwr_env_ctrl_blk->bat_type = LI_ION;
+
+      /* Check if the battery is OK */
+      pwr_battery_qualification();
+
+   }
+
+   else /* battery type unknown */
+   {
+      pwr_env_ctrl_blk->bat_type = UNKNOWN;
+
+      /* informs the upper layer that the battery type is unknown */
+      pwr_send_charge_not_possible_event(BAT_TYPE_UNKNOWN);
+      if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+      {
+#if (ANALOG == 1) 
+         ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+         ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+      }
+      else
+      {
+         pwr_handle_discharge();
+      }
+   }
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_get_capacity_vs_voltage
+**
+** Description    Compares the battery voltage with the thresholds given in the
+**                a_pwr_thresholds array and returns the remaining capacity
+**                corresponding to the threshold above this voltage.
+**
+** Parameters:    UINT16 bat_voltage: battery voltage in mV !!!
+**
+*******************************************************************************/
+T_PWR_PERCENT pwr_get_capacity_vs_voltage(UINT16 bat_voltage)
+{
+   volatile UINT16 i;
+   T_PWR_PERCENT remaining_capacity;
+
+   for(i=0; i<NB_THRESHOLDS; i++)
+   {
+      if(bat_voltage > a_pwr_thresholds[i].bat_voltage)
+      {
+         if(i==0)
+         {
+            remaining_capacity = a_pwr_thresholds[0].remain_capa;
+         }
+         else
+         {
+            remaining_capacity = a_pwr_thresholds[i-1].remain_capa;
+         }
+
+         return (remaining_capacity);
+      }
+      /* else, try the next threshold */
+   }
+
+   if(i==NB_THRESHOLDS)
+   {
+      /* battery voltage below the last threshold */
+      remaining_capacity = a_pwr_thresholds[i-1].remain_capa;
+      return (remaining_capacity);
+   }
+
+   return 0; /* dummy return */
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_cust.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,100 @@
+/*******************************************************************************
+ *
+ * pwr_cust.h
+ *
+ * Purpose: This file contains definitions for battery management.
+ *          These definitions can be modified by the customer.
+ *
+ * Author   Candice Bazanegue (c-brille@ti.com)
+ *
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_CUST_H__
+#define __PWR_CUST_H__
+
+#include "pwr_api.h"
+
+
+/* Constants relative to the battery type */
+
+#define BAT_TYPE_LI_ION_MIN      0x400
+#define BAT_TYPE_LI_ION_MAX      0x450
+
+
+/* Constants relative to the charging process */
+
+#define CONSTANT_VOLTAGE_VALUE   4200     // (mV)  /* for CV charge */
+#define CONSTANT_CURRENT_VALUE   500      // (mA)  /* for CI charge */
+#define END_OF_CHARGE_I          150      // (mA)  /* for CV charge */
+#define END_OF_CHARGE_V          4200     // (mV)  /* for CI charge */
+
+#define CHARGE_START_AGAIN_CAPACITY 75 /* capacity threshold under which */
+                                       /* the charge is started again if */
+                                       /* the charger has been left plugged. */
+                                       /* the chosen value must be part of the */
+                                       /* a_pwr_thresholds array. */
+
+
+/* Constants relative to the battery temperature */
+
+#define BAT_TEMPERATURE_MAX_LI_ION  (50) /* Celsius degrees */
+#define BAT_TEMPERATURE_MIN_LI_ION  (0)  /* Celsius degrees */
+
+#define BAT_TEMPERATURE_MAX         BAT_TEMPERATURE_MAX_LI_ION 
+#define BAT_TEMPERATURE_MIN         BAT_TEMPERATURE_MIN_LI_ION
+
+
+#define THEN_10uA 0x0041 /* THSENS0 , THSENS1, THSENS2 = 0, 0, 0  MESBAT = 1 */
+#define THEN_30uA 0x0051 /* THSENS0 , THSENS1, THSENS2 = 0, 1, 0  MESBAT = 1 */
+#define THEN_50uA 0x0061 /* THSENS0 , THSENS1, THSENS2 = 0, 0, 1  MESBAT = 1 */
+#define THEN_80uA 0x0079 /* THSENS0 , THSENS1, THSENS2 = 1, 1, 1  MESBAT = 1 */
+#define MESBAT    0x0001 /* Resistive divider connected to main battery */
+
+
+/* Constants relative to the timers duration */
+#define PWR_BAT_TEST_TIME_1            (10)   /* 10 ms */
+#define PWR_BAT_TEST_TIME_2            (5000) /* 5s */
+#define PWR_CALIBRATION_TIME_1         (10)   /* 10 ms */
+#define PWR_CALIBRATION_TIME_2         (5000) /* 5s */
+#define PWR_CI_CHECKING_TIME           (4000) /* 4s */
+#define PWR_CV_CHECKING_TIME           (4000) /* 4s */
+#define PWR_DISCHARGE_CHECKING_TIME_1  (60000) /* 1 minute */
+#define PWR_DISCHARGE_CHECKING_TIME_2  (10000) /* 10s */
+
+
+typedef struct { 
+   UINT16 bat_voltage;
+   T_PWR_PERCENT remain_capa;
+}T_PWR_THRESHOLDS; 
+
+
+typedef enum {
+   LI_ION,
+   UNKNOWN
+} T_PWR_BATTERY_TYPE;
+
+
+#define NB_THRESHOLDS   (6)      /* number of elements in the a_pwr_thresholds array */	
+extern const T_PWR_THRESHOLDS a_pwr_thresholds[NB_THRESHOLDS];
+
+
+
+
+
+/* Prototypes */
+
+UINT16 pwr_adc_to_mvolt(UINT16 bat_voltage_madc);
+UINT16 pwr_adc_to_mA(UINT16 current_madc);
+UINT8 pwr_bat_temp_within_limits(INT16 battery_temperature);
+UINT8 pwr_madc_to_Celsius_conv(UINT8 bias_current, UINT16 madc_temp, INT16 *celsius_temp);
+void pwr_get_battery_type(void);
+void pwr_get_battery_temperature(void);
+T_PWR_PERCENT pwr_get_capacity_vs_voltage(UINT16 bat_voltage);
+void pwr_bat_50uA_temp_test_timer_process(void);
+void pwr_bat_10uA_temp_test_timer_process(void);
+void pwr_type_test_timer_process(void);
+
+#endif /* __PWR_CUST_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_disch.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ *
+ * pwr_disch.c
+ *
+ * Purpose: This file contains functions for battery discharge management.
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "abb.h"
+#include "rvm_use_id_list.h"
+#include "pwr_disch.h"
+#include "power.h"
+#include "spi_task.h"
+#include "pwr_cust.h"
+#include "pwr_messages.h"
+#include "spi_env.h"
+#include "pwr_env.h"
+
+/* Define a pointer to the PWR Environment control block.  */
+extern T_PWR_ENV_CTRL_BLK *pwr_env_ctrl_blk;
+
+
+
+
+/*******************************************************************************
+** Function         pwr_discharge_timer_process
+**
+** Description      
+**
+*******************************************************************************/
+void pwr_discharge_timer_process(void)
+{
+   rvf_send_trace("TIMER3", 6, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+   if (SPI_GBL_INFO_PTR->is_gsm_on == TRUE)
+   {
+      pwr_handle_discharge(); /* battery discharge management */
+   } 
+
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_handle_discharge
+**
+** Description      Compares the battery voltage with some thresholds and, if a 
+**                  threshold is passed, sends event(s) to the upper layer.
+**                  Re-start the timer with a value depending on the discharge 
+**                  level.
+**
+*******************************************************************************/
+void pwr_handle_discharge(void)
+{
+   UINT16 timer_value;
+   UINT16 status;
+   UINT16 bat_madc_voltage, bat_voltage;
+   static T_PWR_PERCENT remain_capacity = 100;  /* since this variable is declared as static */
+                                                /* it will keep its value from one function call to the other */
+   T_PWR_PERCENT current_capacity;
+  
+   if (SPI_GBL_INFO_PTR->is_adc_on == FALSE) 
+   {
+      /* Start VBAT channel conversion by writing in the result register */
+      ABB_Write_Register_on_page(PAGE0, VBATREG, 0x0000);
+      rvf_delay(RVF_MS_TO_TICKS(5));
+      bat_madc_voltage = ABB_Read_Register_on_page(PAGE0, VBATREG);
+      rvf_send_trace("battery voltage (MADC code) ", 28, bat_madc_voltage, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+   }
+   else /* The L1 asks for ADC conversions */
+   {
+#ifndef _WINDOWS
+      bat_madc_voltage = SPI_GBL_INFO_PTR->adc_result[0];
+#else
+      bat_madc_voltage = ABB_Read_Register_on_page(PAGE0, VBATREG);
+#endif
+   }
+
+   /* Find the remaining capacity in the battery corresponding to this new voltage */
+   bat_voltage = pwr_adc_to_mvolt(bat_madc_voltage);
+   rvf_send_trace("battery voltage (mV) ", 21, bat_voltage, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+   current_capacity = pwr_get_capacity_vs_voltage(bat_voltage);
+   rvf_send_trace("current capacity (%) ", 21, current_capacity, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+
+   status = ABB_Read_Status();
+
+   /* Determine if a threshold has been passed */
+   if (current_capacity != remain_capacity) 
+   {
+      /* a new threshold has been passed */
+      remain_capacity = current_capacity;
+
+      /* informs the upper layer */
+      pwr_send_bat_discharge_event(remain_capacity);
+
+      if (status & CHGPRES)   /* charger plugged */
+      {
+         if (remain_capacity == CHARGE_START_AGAIN_CAPACITY)
+         {
+            PWR_Charger_Plug();
+         }
+      }
+
+      else  /* charger not plugged */
+      {
+         if(remain_capacity <= pwr_env_ctrl_blk->power_alert.remain_capa_threshold)
+         {
+            /* informs the upper layer that the battery is low */
+            pwr_send_low_bat_event(remain_capacity);
+            timer_value = SPI_TIMER3_INTERVAL_BIS; /* 10 s */
+         }
+         else  
+         {
+#ifndef _WINDOWS
+            timer_value = SPI_TIMER3_INTERVAL; /* 1 minute */
+#else
+            timer_value = SPI_TIMER3_INTERVAL_BIS; /* 10 s */
+#endif
+         }
+
+         /* Start timer with a value depending on the remaining capacity in the battery */
+         rvf_start_timer (SPI_TIMER3,
+                          RVF_MS_TO_TICKS (timer_value),
+                          FALSE);
+      }
+   }
+
+   else /* the capacity has not changed */
+   {
+#ifndef _WINDOWS
+      timer_value = SPI_TIMER3_INTERVAL; /* 1 minute */
+#else
+      timer_value = SPI_TIMER3_INTERVAL_BIS; /* 10 s */
+#endif
+
+      /* Start timer with a value depending on the remaining capacity in the battery */
+      rvf_start_timer (SPI_TIMER3,
+                       RVF_MS_TO_TICKS (timer_value),
+                       FALSE);
+
+   }
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_disch.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,18 @@
+/*******************************************************************************
+ *
+ * pwr_disch.h
+ *
+ * Purpose: This file contains prototypes for battery discharge management.
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_DISCH_H__
+#define __PWR_DISCH_H__
+
+/* Prototypes */
+void pwr_discharge_timer_process(void);
+void pwr_handle_discharge(void);
+
+#endif /* __PWR_DISCH_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_env.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,253 @@
+/*****************************************************************************/
+/*                                                                           */
+/*  File Name: pwr_env.c                                                     */
+/*                                                                           */
+/*  Purpose: This file contains definitions for RV manager related           */
+/*           functions used to get info, start and stop the PWR SWE.         */
+/*                                                                           */
+/*  Version    0.1                                                           */
+/*                                                                           */
+/*  Date       Modification                                                  */
+/*  ------------------------------------                                     */
+/*  20/08/2000 Create                                                        */
+/*                                                                           */
+/* Author      Candice Bazanegue (c-brille@ti.com)                           */
+/*                                                                           */
+/* (C) Copyright 2000 by Texas Instruments Incorporated, All Rights Reserved */
+/*****************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "rvm_priorities.h"
+#include "rvm_api.h"
+#include "pwr_env.h"
+#include "spi_env.h"
+#include "pwr_cust.h"
+#include "pwr_analog_dev.h"
+#include "rvm_use_id_list.h"
+#include <string.h>
+
+/* Define a pointer to the PWR environment control block */
+T_PWR_ENV_CTRL_BLK *pwr_env_ctrl_blk = NULL;
+
+/* Define global pointer to the error function */
+static T_RVM_RETURN (*pwr_error_ft) (T_RVM_NAME    swe_name,
+                                     T_RVM_RETURN  error_cause,
+                                     T_RVM_ERROR_TYPE  error_type,
+                                     T_RVM_STRING   error_msg);
+
+
+/******************************************************************************
+* Function name: pwr_get_info
+*
+* Description : Generic PWR get info  function : provide general information 
+*               regarding the PWR SWE to the Riviera Manager
+*               For more details, see Riviera Manager documentation
+*
+* Parameters :  Pointer on the info structure
+*
+* Return     :  Always OK 
+*
+* History    :
+*
+******************************************************************************/
+T_RVM_RETURN pwr_get_info (T_RVM_INFO_SWE  *infoSWE)
+{
+   /* SWE info */
+   infoSWE->swe_type = RVM_SWE_TYPE_1;
+   infoSWE->type_info.type1.swe_use_id = PWR_USE_ID;
+
+   memcpy( infoSWE->type_info.type1.swe_name, "PWR", sizeof("PWR") );
+
+   /* Set the return path */
+   infoSWE->type_info.type1.return_path.callback_func = NULL;
+   infoSWE->type_info.type1.return_path.addr_id       = 0;
+
+
+   /* memory bank info */
+   infoSWE->type_info.type1.nb_mem_bank = 1;
+
+   memcpy ((UINT8 *) infoSWE->type_info.type1.mem_bank[0].bank_name, "PWR_PRIM", 9);
+   infoSWE->type_info.type1.mem_bank[0].initial_params.size          = PWR_MB_PRIM_SIZE;
+   infoSWE->type_info.type1.mem_bank[0].initial_params.watermark     = PWR_MB_PRIM_WATERMARK;
+
+
+   /* Linked SW entities : PWR needs SPI */ 
+   infoSWE->type_info.type1.nb_linked_swe = 1;
+   infoSWE->type_info.type1.linked_swe_id[0] = SPI_USE_ID;
+
+   /* generic functions */
+   infoSWE->type_info.type1.set_info = pwr_set_info;
+   infoSWE->type_info.type1.init     = pwr_init;
+   infoSWE->type_info.type1.start    = pwr_start;
+   infoSWE->type_info.type1.stop     = pwr_stop;
+   infoSWE->type_info.type1.kill     = pwr_kill;
+
+   return RV_OK;
+}
+
+
+/******************************************************************************
+* Function    : pwr_set_info
+*
+* Description : This function is called by the RV manager to inform  
+*               the pwr SWE about task_id, mb_id and error function.
+*
+* Parameters  : - T_RVM_TASK_ID  taskId[]: array of task_id.
+*                 taskId[0] contains pwr task_id.
+*               - T_RVF_MB_ID mbId[]: array of memory bank ids.
+*               - callback function to call in case of unrecoverable error.
+*
+* Return      : T_RVM_RETURN
+* 
+* History     : 
+*
+*
+******************************************************************************/
+T_RVM_RETURN pwr_set_info(T_RVF_ADDR_ID   addr_id,
+                          T_RV_RETURN     return_path[],
+                          T_RVF_MB_ID     mbId[],
+                          T_RVM_RETURN  (*callBackFct) (T_RVM_NAME SWEntName,
+                                                        T_RVM_RETURN errorCause,
+                                                        T_RVM_ERROR_TYPE errorType,
+                                                        T_RVM_STRING errorMsg))
+{
+   T_RVF_MB_STATUS   mb_status;
+
+
+   rvf_send_trace("PWR: pwr_set_info: try to init GLOBAL INFO PWR structure ...",60,
+                  NULL_PARAM,
+                  RV_TRACE_LEVEL_DEBUG_HIGH,
+                  PWR_USE_ID);
+
+   mb_status = rvf_get_buf(mbId[0],sizeof(T_PWR_ENV_CTRL_BLK),(void **) &pwr_env_ctrl_blk);
+
+   if (mb_status == RVF_RED) 
+   {
+      rvf_send_trace("PWR : pwr_set_info: Not enough memory to initiate GLOBAL INFO PWR structure ... ",86,
+                     NULL_PARAM,
+                     RV_TRACE_LEVEL_ERROR,
+                     PWR_USE_ID);
+
+      return (RVM_MEMORY_ERR);
+   }
+
+
+   /* store the pointer to the error function */
+   pwr_error_ft = callBackFct ;
+
+   /* Store the addr id */
+   /* Same task id than SPI but different mailbox */
+   pwr_env_ctrl_blk->addr_id = SPI_GBL_INFO_PTR->addr_id;
+
+   pwr_env_ctrl_blk->prim_id = mbId[0];
+
+   pwr_env_ctrl_blk->charging_state = CHARGE_STOPPED;
+   pwr_env_ctrl_blk->madc_eoc_current_code = (UINT16)(1000 * END_OF_CHARGE_I / MADC_CURRENT_STEP); /* modified after i2v calibration */
+   pwr_env_ctrl_blk->max_voltage_code = (UINT16)(1000 * ((END_OF_CHARGE_V + MADC_OFFSET)/4) / MADC_VOLTAGE_STEP);
+
+   pwr_env_ctrl_blk->power_info.info_enable = FALSE;
+   /* initalize the return path */
+   pwr_env_ctrl_blk->power_info.return_path.addr_id = RVF_INVALID_ADDR_ID;
+   pwr_env_ctrl_blk->power_info.return_path.callback_func = NULL;
+   pwr_env_ctrl_blk->power_alert.remain_capa_threshold =0x0A;
+   /* initalize the return path */
+   pwr_env_ctrl_blk->power_alert.return_path.addr_id = RVF_INVALID_ADDR_ID;
+   pwr_env_ctrl_blk->power_alert.return_path.callback_func = NULL;
+   /* initalize the return path */
+   pwr_env_ctrl_blk->power_emergency.addr_id = RVF_INVALID_ADDR_ID;
+   pwr_env_ctrl_blk->power_emergency.callback_func = NULL;
+
+   pwr_env_ctrl_blk->bat_celsius_temp = 0;
+   pwr_env_ctrl_blk->timer0_state = BATTERY_SHORT_TEST;
+
+   return RV_OK;
+}
+
+
+/******************************************************************************
+* Function    : pwr_init
+*
+* Description : This function is called by the RV manager to initialize the 
+* pwr SWE before creating the task and calling pwr_start. 
+*
+* Parameters  : None
+*
+* Return      : T_RVM_RETURN
+* 
+* History     : 
+*
+*
+******************************************************************************/
+T_RVM_RETURN pwr_init(void)
+{
+   return RV_OK;
+}
+
+
+
+/******************************************************************************
+* Function  :   pwr_start
+*
+* Description : This function is called by the RV manager to start the pwr
+*               SWE, it is the body of the task.
+*
+* Parameters  : None
+*
+* Return      : T_RVM_RETURN
+* 
+* History     : 
+*
+*
+******************************************************************************/
+T_RVM_RETURN pwr_start(void)
+{
+   return RV_OK;
+}
+
+
+/******************************************************************************
+* Function    : pwr_stop
+*
+* Description : This function is called by the RV manager to stop the pwr SWE.
+*
+* Parameters  : None
+*
+* Return      : T_RVM_RETURN
+* 
+* History     : 
+*
+*
+******************************************************************************/
+T_RVM_RETURN pwr_stop(void)
+{
+   return RV_OK;
+}
+
+
+/******************************************************************************
+* Function	  : pwr_kill
+*
+* Description : This function is called by the RV manager to kill the pwr
+*               SWE, after the pwr_stop function has been called.
+*
+* Parameters  : None
+*
+* Return      : T_RVM_RETURN
+* 
+* History     : 
+*
+*
+******************************************************************************/
+T_RVM_RETURN pwr_kill (void)
+{
+   /* free all memory buffer previously allocated */
+   rvf_free_buf ((void *) pwr_env_ctrl_blk);
+
+   return RV_OK;
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_env.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,77 @@
+/*****************************************************************************/
+/*                                                                           */
+/* File Name:  pwr_env.h                                                     */
+/*                                                                           */
+/* Purpose: This file contains prototypes for RV manager related             */
+/*          functions used to get info, start and stop the pwr block         */
+/*                                                                           */
+/* Version     0.1                                                           */
+/*                                                                           */
+/* Date        Modification                                                  */
+/*  ------------------------------------                                     */
+/* 20/08/2000  Create                                                        */
+/*                                                                           */
+/* Author      Candice Bazanegue (c-brille@ti.com)                           */
+/*                                                                           */
+/* (C) Copyright 2000 by Texas Instruments Incorporated, All Rights Reserved */
+/*****************************************************************************/
+
+#ifndef __PWR_ENV_H__
+#define __PWR_ENV_H__
+
+#include "rvm_gen.h"
+#include "pwr_api.h"
+
+#include "pwr_cust.h"
+
+#include "rvf_pool_size.h"	/* Stack & Memory Bank sizes definitions */
+
+#define PWR_MAILBOX_USED   RVF_TASK_MBOX_1
+
+/* memory bank size and watermark */
+#define PWR_MB_PRIM_SIZE            RVF_PWR_MB1_SIZE
+
+#define PWR_MB_PRIM_WATERMARK     (PWR_MB_PRIM_SIZE - 200)
+
+#define PWR_MB_PRIM_INC_SIZE        0
+#define PWR_MB_PRIM_INC_WATERMARK   0
+
+typedef struct
+{
+   T_RVF_MB_ID          prim_id;
+   T_RVF_ADDR_ID        addr_id;
+   T_PWR_CHARGING_STATE charging_state;
+   UINT16               i2v_madc_offset;
+   UINT16               madc_eoc_current_code;
+   UINT16               max_voltage_code;
+   T_POWER_INFO         power_info;
+   T_POWER_ALERT        power_alert;
+   T_RV_RETURN          power_emergency;
+   T_PWR_TIMER0_STATE   timer0_state;
+   T_PWR_BATTERY_TYPE   bat_type;
+   INT16                bat_celsius_temp; /* temperature in Celsius degrees */
+} T_PWR_ENV_CTRL_BLK;
+
+
+extern T_PWR_ENV_CTRL_BLK *pwr_env_ctrl_blk;
+
+
+/* Prototypes */
+
+T_RVM_RETURN pwr_get_info (T_RVM_INFO_SWE  *infoSWE);
+
+T_RVM_RETURN pwr_set_info( T_RVF_ADDR_ID  addr_id,
+                           T_RV_RETURN    return_path[],
+                           T_RVF_MB_ID    mbId[],
+                           T_RVM_RETURN   (*callBackFct) (T_RVM_NAME SWEntName,
+                           T_RVM_RETURN errorCause,
+                           T_RVM_ERROR_TYPE errorType,
+                           T_RVM_STRING errorMsg));
+
+T_RVM_RETURN pwr_init (void);
+T_RVM_RETURN pwr_start (void);
+T_RVM_RETURN pwr_stop (void);
+T_RVM_RETURN pwr_kill (void);
+
+
+#endif /* __PWR_ENV_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_liion_cha.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,698 @@
+/*******************************************************************************
+ *
+ *  pwr_liion_cha.c
+ *
+ * Purpose:    This file contains functions for managing the Li-ion batteries 
+ *             charging process.
+ *
+ * Author      Candice Bazanegue (c-brille@ti.com)
+ *
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "rvm_use_id_list.h"
+#include "spi_task.h"
+#include "spi_api.h"
+#include "pwr_cust.h"
+#include "pwr_liion_cha.h"
+#include "pwr_disch.h"
+#include "spi_env.h"
+#include "rvf_api.h"
+#include "pwr_analog_dev.h"
+#include "pwr_messages.h"
+
+/* Global variable */
+extern T_SPI_GBL_INFO *SPI_GBL_INFO_PTR;
+
+
+/*******************************************************************************
+** Function         pwr_start_CI_charging
+**
+** Description      This function is used to start the constant current 
+**                  battery charging.   
+**
+*******************************************************************************/
+void pwr_start_CI_charging(unsigned short charging_current)
+{
+   volatile unsigned short dac_current_code;
+   
+
+   rvf_send_trace("Start CI charging",17, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+   dac_current_code = (unsigned int)((1000 * charging_current)/DAC_CURRENT_STEP);
+
+   /* Select constant current charging. The charger is disabled */
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0002);
+
+   /* Program the DAC with the constant current value */
+   ABB_Write_Register_on_page(PAGE0, CHGREG, dac_current_code);
+
+   /* Enable the charger  */
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0003);
+
+   pwr_env_ctrl_blk->charging_state = CI_CHARGE_STARTED;
+
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_start_CV_charging
+**
+** Description      This function is used to start the constant voltage 
+**                  battery charging.   
+**
+*******************************************************************************/
+void pwr_start_CV_charging(unsigned short charging_voltage)
+{
+   volatile unsigned short dac_voltage_code;
+   unsigned short dac_voltage;
+
+   rvf_send_trace("Start CV charging",17, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+   dac_voltage = charging_voltage + VOLTAGE_LOOP_OFFSET;
+
+   dac_voltage_code = (unsigned int)((1000 * ((dac_voltage /4) - DAC_THRESHOLD))/DAC_VOLTAGE_STEP);
+
+   rvf_send_trace("Voltage (DAC code) ", 19, dac_voltage_code, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+   /* Select constant voltage charging. The charger is disabled */
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
+
+   /* Program the DAC with the constant voltage value */
+   ABB_Write_Register_on_page(PAGE0, CHGREG, dac_voltage_code);
+
+   /* Enable the charger */
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0001);
+
+   pwr_env_ctrl_blk->charging_state = CV_CHARGE_STARTED;
+
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_stop_charging
+**
+** Description      This function is used to stop the battery charging process.
+**
+*******************************************************************************/
+void pwr_stop_charging(void)
+{
+
+   rvf_send_trace("Stop charging process",21, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
+   pwr_env_ctrl_blk->charging_state = CHARGE_STOPPED;
+}
+
+
+
+
+/*******************************************************************************
+** Function         pwr_current_loop_cal
+**
+** Description    This function is used to evaluate the offset introduced
+**                by the current to voltage converter in the current loop.
+**                   
+**
+*******************************************************************************/
+void pwr_current_loop_cal(void)
+{
+   rvf_send_trace("Current loop calibration",24, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+   /* Set the CHDISPA bit */
+   /* And start the zero calibration routine of the I to V converter */
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0010);
+   ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0019);
+
+   if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+   {
+      /* start ICHG channel conversion by writing in the result register */
+      ABB_Write_Register_on_page(PAGE0, ICHGREG, 0x0000);
+
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_3),
+                       FALSE);
+   }
+   else /* The L1 asks for ADC conversions */
+   {
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_4),
+                       FALSE);
+   }
+
+}
+
+
+
+/*******************************************************************************
+** Function         pwr_calibration_process
+**
+** Description      
+** 
+*******************************************************************************/
+void pwr_calibration_process(void)
+{
+   if (pwr_bat_temp_within_limits(pwr_env_ctrl_blk->bat_celsius_temp))
+   {
+      pwr_env_ctrl_blk->timer0_state = BATTERY_CALIBRATION;
+      pwr_current_loop_cal();
+   }
+   else
+   {
+      /* informs the upper layer that the battery temperature is not correct */
+      pwr_send_charge_not_possible_event(BAT_TEMP_OUTSIDE_LIMITS);
+      if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+      {
+#if (ANALOG == 1) 
+         ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+         ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+      }
+      else
+      {
+         pwr_handle_discharge();
+      }
+   }
+}
+
+
+
+
+/*******************************************************************************
+** Function         pwr_battery_qualification
+**
+** Description      Battery open and short tests 
+** 
+*******************************************************************************/
+void pwr_battery_qualification(void)
+{
+   rvf_send_trace("Battery qualification",21, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+   pwr_env_ctrl_blk->timer0_state = BATTERY_SHORT_TEST;
+
+   /* Short test */
+   pwr_start_CI_charging(CONSTANT_CURRENT_VALUE);
+   if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+   {
+      /* Start VBAT channel conversion by writing in the result register */
+      ABB_Write_Register_on_page(PAGE0, VBATREG, 0x0000);
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_1),
+                       FALSE);
+   }
+   else
+   {
+      /* Let time for the L1 to ask for new AD conversions */
+      rvf_start_timer (SPI_TIMER0,
+                       RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_2),
+                       FALSE);
+   }
+
+}
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_start_fast_charge                                      */
+/*                                                                              */
+/*    Purpose: Starts the fast charging process for Li-ion batteries            */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_start_fast_charge(void)
+{
+   UINT16 i2v_dac_offset_mA;
+
+   /* Informs the upper layer that the charging process has started */
+   pwr_send_CI_charge_start_event();
+
+   /* Connect resistive bridge to main battery */
+   ABB_Write_Register_on_page(PAGE0, BCICTL1, MESBAT);
+
+   /* Start the constant current charging */
+   i2v_dac_offset_mA = (UINT16)((MADC_CURRENT_STEP*pwr_env_ctrl_blk->i2v_madc_offset)/1000);
+
+   pwr_start_CI_charging((unsigned short)(CONSTANT_CURRENT_VALUE + i2v_dac_offset_mA));
+
+   rvf_start_timer (SPI_TIMER1,
+                    RVF_MS_TO_TICKS (SPI_TIMER1_INTERVAL),
+                    FALSE);
+}
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_short_test_timer_process                               */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_short_test_timer_process(void)
+{
+   UINT16 Vbat_test;
+
+   rvf_send_trace("TIMER0: Battery short test",26, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   /* Read ADC result */
+   Vbat_test = ABB_Read_Register_on_page(PAGE0, VBATREG);
+   rvf_send_trace("Vbat (MADC code) ",17, Vbat_test, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   pwr_stop_charging();
+
+   if (Vbat_test > 0)
+   {
+      pwr_env_ctrl_blk->timer0_state = BATTERY_OPEN_TEST;
+
+      /* Start open test */
+      pwr_start_CV_charging(CONSTANT_VOLTAGE_VALUE);
+      rvf_delay(RVF_MS_TO_TICKS(5));
+
+      if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+      {
+         /* start ICHG channel conversion by writing in the result register */
+         ABB_Write_Register_on_page(PAGE0, ICHGREG, 0x0000);
+         rvf_start_timer (SPI_TIMER0,
+                          RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_1),
+                          FALSE);
+      }
+      else
+      {
+         /* Let time for the L1 to ask for new AD conversions */
+         rvf_start_timer (SPI_TIMER0,
+                          RVF_MS_TO_TICKS (SPI_TIMER0_INTERVAL_2),
+                          FALSE);
+      }
+   }
+   else
+   {
+      rvf_send_trace("Short battery",13, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID); 
+         
+      /* informs the upper layer that the short test has failed */
+      pwr_send_charge_not_possible_event(BAT_SHORT_TEST_FAILED);
+      if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+      {
+#if (ANALOG == 1) 
+         ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+         ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+      }
+      else
+      {
+         pwr_handle_discharge();
+      }
+   }
+}
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_open_test_timer_process                                */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_open_test_timer_process(void)
+{
+   UINT16 Ichg_test;
+
+   rvf_send_trace("TIMER0: Battery open test",25, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   /* Read ADC result */
+   Ichg_test = ABB_Read_Register_on_page(PAGE0, ICHGREG);
+   rvf_send_trace("Charge current (MADC code) ",27, Ichg_test, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+
+   pwr_stop_charging();
+      
+   if (Ichg_test > 0) /* Battery OK */
+   {
+      rvf_send_trace("Battery OK",10, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+   
+      /* Check if the initial battery temperature is correct */
+      pwr_env_ctrl_blk->charging_state = TESTING_BATTERY;
+      pwr_get_battery_temperature();
+   }
+
+   else /* Open battery */
+   {
+      rvf_send_trace("Open battery",12, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID); 
+      
+      /* informs the upper layer that the open test has failed */
+      pwr_send_charge_not_possible_event(BAT_OPEN_TEST_FAILED);
+      if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+      {
+#if (ANALOG == 1) 
+         ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+         ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+      }
+      else
+      {
+         pwr_handle_discharge();
+      }
+   }
+}
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_cal_timer_process                                      */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_cal_timer_process(void)
+{
+   rvf_send_trace("TIMER0: Battery calibration",27, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+
+   pwr_env_ctrl_blk->i2v_madc_offset = ABB_Read_Register_on_page(PAGE0, ICHGREG);
+
+   rvf_send_trace("i2v offset (MADC code) ", 23, pwr_env_ctrl_blk->i2v_madc_offset, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+   pwr_stop_charging();
+
+#if (ANALOG == 2)
+   if (pwr_env_ctrl_blk->i2v_madc_offset == 0)
+   {
+   /* IOTA: the offset can be made positive and minimized programming */
+   /* the OFFEN and OFFSN bits in the BCICONF register */
+      ABB_Write_Register_on_page(PAGE1, BCICONF, 0x001F);
+      pwr_current_loop_cal();
+   }
+   else
+   {
+      /* the i2v calibration must be done before this initialization */
+      pwr_env_ctrl_blk->madc_eoc_current_code = (UINT16)(1000 * END_OF_CHARGE_I / MADC_CURRENT_STEP) + pwr_env_ctrl_blk->i2v_madc_offset;
+
+      rvf_send_trace("End of charge current (MADC code) ", 34, pwr_env_ctrl_blk->madc_eoc_current_code, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+      /* Start the fast charging cycle */
+      pwr_start_fast_charge();
+   }
+
+#elif (ANALOG == 1)
+   /* the i2v calibration must be done before this initialization */
+   pwr_env_ctrl_blk->madc_eoc_current_code = (UINT16)(1000 * END_OF_CHARGE_I / MADC_CURRENT_STEP) + pwr_env_ctrl_blk->i2v_madc_offset;
+
+   rvf_send_trace("End of charge current (MADC code) ", 34, pwr_env_ctrl_blk->madc_eoc_current_code, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+   /* Start the fast charging cycle */
+   pwr_start_fast_charge();
+
+#endif /* #if (ANALOG == 2) */
+}
+
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_CI_charge_process                                      */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_CI_charge_process(void)
+{
+   UINT16 vbat;
+   UINT16 ichg;
+
+   /* Check if the battery temperature is still correct */
+   if (pwr_bat_temp_within_limits(pwr_env_ctrl_blk->bat_celsius_temp))
+   {
+      if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+      {
+         /* start VBAT channel conversion by writing in the result register */
+         ABB_Write_Register_on_page(PAGE0, VBATREG, 0x0000);
+         rvf_delay(RVF_MS_TO_TICKS(5));
+         vbat = ABB_Read_Register_on_page(PAGE0, VBATREG);
+      }
+      else
+      {
+         /* Use the ADC conversions results from the L1 */
+         vbat = SPI_GBL_INFO_PTR->adc_result[0];
+      }
+
+      rvf_send_trace("TIMER1", 6, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+      rvf_send_trace("Vbat (MADC code) ", 17, vbat, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+      if (vbat < pwr_env_ctrl_blk->max_voltage_code)
+      {
+         rvf_start_timer (SPI_TIMER1, RVF_MS_TO_TICKS (SPI_TIMER1_INTERVAL), FALSE);
+      }
+      else
+      {
+         pwr_stop_charging();
+         pwr_send_CV_charge_start_event();
+         pwr_start_CV_charging(CONSTANT_VOLTAGE_VALUE);
+         rvf_start_timer (SPI_TIMER2, RVF_MS_TO_TICKS (SPI_TIMER2_INTERVAL), FALSE);
+      }
+
+      if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+      {
+         /* start ICHG channel conversion  by writing in the result register */
+         ABB_Write_Register_on_page(PAGE0, ICHGREG, 0x0000);
+         rvf_delay(RVF_MS_TO_TICKS(5));
+         ichg = ABB_Read_Register_on_page(PAGE0, ICHGREG);
+      }
+      else
+      {
+         ichg = SPI_GBL_INFO_PTR->adc_result[2];
+      }
+
+      rvf_send_trace("Ichg (MADC code) ", 17, ichg, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+   }
+
+   else /* battery temperature not correct !!! */
+      {
+         pwr_stop_charging();
+         rvf_send_trace("Battery temperature not correct! ",33, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID); 
+
+         /* informs the upper layer that the battery temperature is not correct */
+         pwr_send_charge_not_possible_event(BAT_TEMP_OUTSIDE_LIMITS);
+
+         if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+         {
+#if (ANALOG == 1) 
+            ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+            ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+         }
+         else
+         {
+            pwr_handle_discharge();
+         }
+      }
+   }
+
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_CI_charge_timer_process                                */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_CI_charge_timer_process(void)
+{
+   UINT16 status_value;
+
+   status_value = ABB_Read_Status();
+   if (status_value & CHGPRES)   /* if the charger is still plugged */
+   {
+      pwr_get_battery_temperature();
+   }
+   else
+   {
+      /* informs the upper layer that the charging process has stopped */
+      pwr_send_charge_stop_event();
+   }
+}
+
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_CV_charge_process                                      */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_CV_charge_process(void)
+{
+   UINT16 vbat;
+   UINT16 ichg;
+
+   /* Check if the battery temperature is still correct */
+   if (pwr_bat_temp_within_limits(pwr_env_ctrl_blk->bat_celsius_temp))
+   {
+      if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+      {
+         /* start ICHG channel conversion by writing in the result register */
+         ABB_Write_Register_on_page(PAGE0, ICHGREG, 0x0000);
+         rvf_delay(RVF_MS_TO_TICKS(5));
+         ichg = ABB_Read_Register_on_page(PAGE0, ICHGREG);
+      }
+      else
+      {
+         ichg = SPI_GBL_INFO_PTR->adc_result[2];
+      }
+
+      rvf_send_trace("TIMER2",6, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID);
+      rvf_send_trace("Ichg (MADC code) ", 17, ichg, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID); 
+
+      if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+      {
+         /* start VBAT channel conversion by writing in the result register */
+         ABB_Write_Register_on_page(PAGE0, VBATREG, 0x0000);
+         rvf_delay(RVF_MS_TO_TICKS(5));
+         vbat = ABB_Read_Register_on_page(PAGE0, VBATREG);
+      }
+      else
+      {
+         /* Use the ADC results asked for by the layer 1 */
+         vbat = SPI_GBL_INFO_PTR->adc_result[0];
+      }
+
+      rvf_send_trace("Vbat (MADC code) ", 17, vbat, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+         if (ichg > pwr_env_ctrl_blk->madc_eoc_current_code)
+         {
+            rvf_start_timer (SPI_TIMER2, RVF_MS_TO_TICKS (SPI_TIMER2_INTERVAL), FALSE);
+         }
+         else
+         {
+            pwr_stop_charging();
+            /* informs the upper layer that the charging process has stopped */
+            pwr_send_charge_stop_event();
+            rvf_send_trace("Fast charge termination criterion",33, NULL_PARAM, 
+                           RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
+
+            if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+            {
+#if (ANALOG == 1) 
+               ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+               ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+            }
+            else
+            {
+               pwr_handle_discharge();
+            }
+         }
+      }
+   
+   else /* Battery temperature not correct !!! */
+   {
+      pwr_stop_charging();
+      rvf_send_trace("Battery temperature not correct! ",33, NULL_PARAM, RV_TRACE_LEVEL_WARNING, PWR_USE_ID); 
+
+      /* informs the upper layer that the battery temperature is not correct */
+      pwr_send_charge_not_possible_event(BAT_TEMP_OUTSIDE_LIMITS);
+
+      if (SPI_GBL_INFO_PTR->is_gsm_on == FALSE) /* GSM OFF */
+      {
+#if (ANALOG == 1) 
+         ABB_Write_Register_on_page(PAGE0, VRPCCTL2, 0x00EE);
+#elif (ANALOG == 2)
+         ABB_Write_Register_on_page(PAGE0, VRPCDEV, 0x0001);
+#endif
+      }
+      else
+      {
+         pwr_handle_discharge();
+      }
+   }
+}
+
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_CV_charge_timer_process                                */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_CV_charge_timer_process(void)
+{
+   UINT16 status_value;
+
+   status_value = ABB_Read_Status();
+   if (status_value & CHGPRES)   /* if the charger is still plugged */
+   {
+      /* Control the battery temperature */
+      pwr_get_battery_temperature();
+   }
+   else
+   {
+      /* informs the upper layer that the charging process has stopped */
+      pwr_send_charge_stop_event();
+   }
+}
+
+
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_bat_test_timer_process                                */
+/*                                                                              */
+/*    Purpose:                                                                  */ 
+/*                                                                              */
+/*                                                                              */
+/********************************************************************************/
+void pwr_bat_test_timer_process(void)
+{
+   if (pwr_env_ctrl_blk->timer0_state == BATTERY_TYPE_TEST)
+   {
+      pwr_type_test_timer_process();
+   }
+   else if (pwr_env_ctrl_blk->timer0_state == BATTERY_SHORT_TEST)
+   {
+      pwr_short_test_timer_process();
+   }
+   else if (pwr_env_ctrl_blk->timer0_state == BATTERY_OPEN_TEST)
+   {
+      pwr_open_test_timer_process();
+   }
+   else if (pwr_env_ctrl_blk->timer0_state == BATTERY_50UA_TEMP_TEST)
+   {
+      pwr_bat_50uA_temp_test_timer_process();
+   }
+   else if (pwr_env_ctrl_blk->timer0_state == BATTERY_10UA_TEMP_TEST)
+   {
+      pwr_bat_10uA_temp_test_timer_process();
+   }
+   else if (pwr_env_ctrl_blk->timer0_state == BATTERY_CALIBRATION)
+   {
+      /* end of current loop calibration */
+      pwr_cal_timer_process();
+   }
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_liion_cha.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ *
+ *  pwr_liion_cha.h
+ *
+ * Purpose:    This file contains definitions for managing the Li-ion batteries 
+ *             charging process.
+ *
+ * Author      Candice Bazanegue (c-brille@ti.com)
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_LIION_CHA_H__
+#define __PWR_LIION_CHA_H__
+
+/* Define for battery qualification */
+
+#define BATTERY_OK      (0)
+#define OPEN_BATTERY    (1)
+#define SHORT_BATTERY   (2)
+
+
+/* Prototypes */
+
+void pwr_start_CI_charging(unsigned short DAC_current);
+void pwr_start_CV_charging(unsigned short charging_voltage);
+void pwr_stop_charging(void);
+void pwr_current_loop_cal(void);
+void pwr_calibration_process(void);
+void pwr_battery_qualification(void);
+void pwr_start_fast_charge(void);
+void pwr_CI_charge_process(void);
+void pwr_CV_charge_process(void);
+void pwr_bat_test_timer_process(void);
+void pwr_short_test_timer_process(void);
+void pwr_open_test_timer_process(void);
+void pwr_cal_timer_process(void);
+void pwr_CI_charge_timer_process(void);
+void pwr_CV_charge_timer_process(void);
+
+#endif /* __PWR_LIION_CHA_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_messages.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,364 @@
+/*******************************************************************************
+ *
+ *  pwr_messages.c
+ *
+ * Purpose: Contains functions used by PWR to send events
+ *          to the upper layer (MMI)
+ *
+ * Author   Candice Bazanegue (c-brille@ti.com)
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "rvm_use_id_list.h"
+#include "rvf_api.h"
+#include "pwr_messages.h"
+#include "rv_general.h"
+#include "spi_env.h"
+
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_charger_plug_event
+**
+** Description      This function is used to send a 'POWER_CHARGER_PLUG_IND' 
+**                  event to the upper layer.
+**
+*******************************************************************************/
+T_RV_RET pwr_send_charger_plug_event(void)
+{
+   T_RV_HDR *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+     if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_RV_HDR),(void **) &msg_p) == RVF_RED)
+     {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+     }
+      
+     msg_p->msg_id        = POWER_CHARGER_PLUG_IND;
+     msg_p->callback_func = NULL;
+
+     PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_charger_unplug_event
+**
+** Description      This function is used to send a 'POWER_CHARGER_UNPLUG_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_charger_unplug_event(void)
+{
+   T_RV_HDR *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+      if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_RV_HDR),(void **) &msg_p) == RVF_RED)
+      {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+      }
+      
+      msg_p->msg_id        = POWER_CHARGER_UNPLUG_IND;
+      msg_p->callback_func = NULL;
+
+      PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_CI_charge_start_event
+**
+** Description      This function is used to send a 'POWER_BAT_CI_CHARGE_START_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_CI_charge_start_event(void)
+{
+   T_RV_HDR *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+      if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_RV_HDR),(void **) &msg_p) == RVF_RED)
+      {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+      }
+
+      msg_p->msg_id        = POWER_BAT_CI_CHARGE_START_IND;
+      msg_p->callback_func = NULL;
+
+      PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_CV_charge_start_event
+**
+** Description      This function is used to send a 'POWER_BAT_CV_CHARGE_START_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_CV_charge_start_event(void)
+{
+   T_RV_HDR *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+      if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_RV_HDR),(void **) &msg_p) == RVF_RED)
+      {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+      }
+
+      msg_p->msg_id        = POWER_BAT_CV_CHARGE_START_IND;
+      msg_p->callback_func = NULL;
+
+      PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_charge_stop_event
+**
+** Description      This function is used to send a 'POWER_BAT_CHARGE_STOP_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_charge_stop_event(void)
+{
+   T_RV_HDR *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+      if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_RV_HDR),(void **) &msg_p) == RVF_RED)
+      {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+      }
+
+      msg_p->msg_id        = POWER_BAT_CHARGE_STOP_IND;
+      msg_p->callback_func = NULL;
+
+      PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_charge_not_possible_event
+**
+** Description      This function is used to send a 'POWER_BAT_CHARGE_NOT_POSSIBLE_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_charge_not_possible_event(T_BAT_PROBLEM charge_problem)
+{
+   T_PWR_BAT_CHARGE_NOT_POSSIBLE *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+      if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_PWR_BAT_CHARGE_NOT_POSSIBLE),(void **) &msg_p) == RVF_RED)
+      {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+      }
+
+      (msg_p->hdr).msg_id= POWER_BAT_CHARGE_NOT_POSSIBLE_IND;
+      (msg_p->hdr).callback_func= NULL;
+      msg_p->problem= charge_problem;
+
+      PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_bat_discharge_event
+**
+** Description      This function is used to send a 'POWER_BAT_DISCHARGE_LEVEL_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_bat_discharge_event(T_PWR_PERCENT remaining_capacity)
+{
+   T_PWR_BAT_DISCHARGE_LEVEL *msg_p;
+
+   if (pwr_env_ctrl_blk->power_info.info_enable == TRUE)
+   {
+      if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_PWR_BAT_DISCHARGE_LEVEL),(void **) &msg_p) == RVF_RED)
+      {
+         rvf_send_trace ("PWR ERROR: Not enough memory",
+                         28,
+                         NULL_PARAM,
+                         RV_TRACE_LEVEL_ERROR,
+                         PWR_USE_ID);
+
+         return (RV_MEMORY_ERR);
+      }
+
+      (msg_p->hdr).msg_id= POWER_BAT_DISCHARGE_LEVEL_IND;
+      (msg_p->hdr).callback_func= NULL;
+      msg_p->remaining_capacity= remaining_capacity;
+
+      PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   }
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_low_bat_event
+**
+** Description      This function is used to send a 'POWER_LOW_BAT_ALERT_IND' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_low_bat_event(T_PWR_PERCENT remaining_capacity)
+{
+   T_PWR_LOW_BAT_ALERT *msg_p;
+
+   if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_PWR_LOW_BAT_ALERT),(void **) &msg_p) == RVF_RED)
+   {
+      rvf_send_trace ("PWR ERROR: Not enough memory",
+                      28,
+                      NULL_PARAM,
+                      RV_TRACE_LEVEL_ERROR,
+                      PWR_USE_ID);
+
+      return (RV_MEMORY_ERR);
+   }
+
+   (msg_p->hdr).msg_id= POWER_LOW_BAT_ALERT_IND;
+   (msg_p->hdr).callback_func = NULL;
+   msg_p->remaining_capacity= remaining_capacity;
+
+   PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_alert.return_path, msg_p);
+
+   return (RV_OK);
+}
+
+
+/*******************************************************************************
+**
+** Function         pwr_send_battery_info
+**
+** Description      This function is used to send a 'POWER_BAT_INFO_STATUS' 
+**                  event to the upper layer.
+**
+**
+*******************************************************************************/
+T_RV_RET pwr_send_battery_info(UINT16 battery_voltage, UINT16 battery_temperature,
+                               UINT16 battery_charger_current,
+                               BOOLEAN is_charger_plugged,
+                               T_PWR_CHARGING_STATE is_charge_started,
+                               T_PWR_PERCENT remaining_capacity)
+{
+   T_PWR_BAT_INFO_STATUS *msg_p;
+
+   if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_PWR_BAT_INFO_STATUS),(void **) &msg_p) == RVF_RED)
+   {
+      rvf_send_trace ("PWR ERROR: Not enough memory",
+                      28,
+                      NULL_PARAM,
+                      RV_TRACE_LEVEL_ERROR,
+                      PWR_USE_ID);
+
+      return (RV_MEMORY_ERR);
+   }
+
+   (msg_p->hdr).msg_id= POWER_BAT_INFO_STATUS;
+   (msg_p->hdr).callback_func= NULL;
+   msg_p->bat_info.battery_voltage= battery_voltage;
+   msg_p->bat_info.battery_temperature= battery_temperature;
+   msg_p->bat_info.battery_charger_current= battery_charger_current;
+   msg_p->bat_info.charger_plugged= is_charger_plugged;
+   msg_p->bat_info.charge_started= is_charge_started;
+   msg_p->bat_info.remaining_capacity= remaining_capacity;
+
+
+
+   PWR_SEND_MESSAGE(pwr_env_ctrl_blk->power_info.return_path, msg_p);
+
+   return (RV_OK);
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_messages.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ *
+ *  pwr_messages.h
+ *
+ * Purpose: Contains definitions used by PWR SWE to send events
+ *          to the upper layer (MMI)
+ *
+ * Author   Candice Bazanegue (c-brille@ti.com)
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_MESSAGES_H__
+#define __PWR_MESSAGES_H__
+
+#include "rv_general.h"
+#include "pwr_api.h"
+#include "pwr_env.h"
+#include "spi_api.h"
+
+typedef struct
+{
+   UINT16               battery_voltage;
+   INT16                battery_temperature;
+   UINT16               battery_charger_current;
+   BOOLEAN              charger_plugged;
+   T_PWR_CHARGING_STATE charge_started;
+   T_PWR_PERCENT        remaining_capacity;
+} T_PWR_BAT_INFO;
+
+
+#define PWR_SEND_MESSAGE(mmi_return_path, \
+                         message_p) \
+      { \
+         if ((mmi_return_path).callback_func != NULL) \
+         { \
+            ((mmi_return_path).callback_func) (message_p); \
+         } \
+         else \
+         { \
+            if ((mmi_return_path).addr_id != RVF_INVALID_ADDR_ID) \
+            { \
+               (void) rvf_send_msg ((mmi_return_path).addr_id, \
+                                    (message_p)); \
+            } \
+            else \
+            { \
+               rvf_free_buf((T_RVF_BUFFER *) message_p); \
+            } \
+         } \
+      }
+
+
+/* Events that can be sent by the power SWE API to the MMI */
+
+#define POWER_CHARGER_PLUG_IND            1
+#define POWER_CHARGER_UNPLUG_IND          2
+#define POWER_BAT_CI_CHARGE_START_IND     3
+#define POWER_BAT_CV_CHARGE_START_IND     4
+#define POWER_BAT_CHARGE_STOP_IND         5
+#define POWER_BAT_CHARGE_NOT_POSSIBLE_IND 6
+#define POWER_BAT_DISCHARGE_LEVEL_IND     7
+#define POWER_LOW_BAT_ALERT_IND           8
+#define POWER_BAT_INFO_STATUS             9
+
+
+
+/* Structures of messages */
+
+typedef struct
+{   T_RV_HDR   hdr;
+   INT8        status;
+} T_PWR_BAT_CHARGE_STOP;
+
+typedef struct
+{  T_RV_HDR       hdr;
+   T_BAT_PROBLEM  problem;
+} T_PWR_BAT_CHARGE_NOT_POSSIBLE;
+
+typedef struct
+{  T_RV_HDR       hdr;
+   T_PWR_PERCENT  remaining_capacity;
+} T_PWR_BAT_DISCHARGE_LEVEL;
+
+typedef struct
+{  T_RV_HDR       hdr;
+   T_PWR_PERCENT  remaining_capacity;
+} T_PWR_LOW_BAT_ALERT;
+
+
+typedef struct
+{  T_RV_HDR          hdr;
+   T_PWR_BAT_INFO    bat_info;
+} T_PWR_BAT_INFO_STATUS;
+
+
+typedef struct
+{  T_RV_HDR       hdr;
+   BOOLEAN        info_enable;
+   T_RV_RETURN    return_path;
+} T_PWR_SPI_INFO_REG;
+
+
+typedef struct
+{  T_RV_HDR       hdr;
+   T_PWR_PERCENT  remain_capa_threshold;
+   T_RV_RETURN    return_path;
+} T_PWR_SPI_ALERT_REG;
+
+
+typedef struct
+{  T_RV_HDR    hdr;
+   T_RV_RETURN return_path;
+} T_PWR_SPI_EMERGENCY_REG;
+
+
+
+
+
+/* Prototypes */
+
+/* Functions used to send messages to the upper layer */
+T_RV_RET pwr_send_charger_plug_event(void);
+T_RV_RET pwr_send_charger_unplug_event(void);
+T_RV_RET pwr_send_CI_charge_start_event(void);
+T_RV_RET pwr_send_CV_charge_start_event(void);
+T_RV_RET pwr_send_charge_stop_event(void);
+T_RV_RET pwr_send_charge_not_possible_event(T_BAT_PROBLEM charge_problem);
+T_RV_RET pwr_send_bat_discharge_event(T_PWR_PERCENT remaining_capacity);
+T_RV_RET pwr_send_low_bat_event(T_PWR_PERCENT remaining_capacity);
+T_RV_RET pwr_send_battery_info(UINT16 battery_voltage, UINT16 battery_temperature,
+                               UINT16 battery_charger_current,
+                               BOOLEAN is_charger_plugged,
+                               T_PWR_CHARGING_STATE is_charge_started,
+                               T_PWR_PERCENT remaining_capacity);
+
+#endif /* __PWR_MESSAGES_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_messages_i.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ *
+ *  pwr_messages_i.c
+ *
+ * Purpose: Contains functions used by PWR SWE to send events to the SPI task.
+ *
+ *
+ * Author   Candice Bazanegue (c-brille@ti.com)
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "rvm_use_id_list.h"
+#include "rvf_api.h"
+#include "pwr_messages_i.h"
+#include "pwr_env.h"
+#include "spi_env.h"
+
+
+/********************************************************************************/
+/*                                                                              */
+/*    Function Name: pwr_spi_battery_info                                       */
+/*                                                                              */
+/*    Purpose:    This function is used to send a PWR_SPI_INFO_BATTERY_EVT      */
+/*                message to the SPI task.                                      */
+/*                                                                              */
+/********************************************************************************/
+T_RV_RET pwr_spi_battery_info(CALLBACK_FUNC CallBack)
+{
+   T_RV_HDR *msgPtr;
+
+   /* check if the driver has been started */
+   if (pwr_env_ctrl_blk == NULL)
+      return (RVM_NOT_READY);
+   
+   if(SPI_GBL_INFO_PTR->SpiTaskReady == FALSE)
+      return(RVM_NOT_READY);
+
+   if (rvf_get_buf (pwr_env_ctrl_blk->prim_id, sizeof (T_RV_HDR),(void **) &msgPtr) == RVF_RED)
+   {
+      rvf_send_trace ("PWR ERROR: Not enough memory",
+                      28,
+                      NULL_PARAM,
+                      RV_TRACE_LEVEL_ERROR,
+                      PWR_USE_ID);
+
+      return (RV_MEMORY_ERR);
+   }
+
+   msgPtr->msg_id          = PWR_SPI_INFO_BATTERY_EVT;
+   msgPtr->dest_addr_id    = pwr_env_ctrl_blk->addr_id;
+   msgPtr->callback_func   = (CALLBACK_FUNC) CallBack;
+
+   rvf_send_msg (pwr_env_ctrl_blk->addr_id, msgPtr);
+
+   return (RV_OK);
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_messages_i.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ *
+ *  pwr_messages_i.h
+ *
+ * Purpose: Contains internal definitions used by PWR SWE. 
+ * Author   Candice Bazanegue (c-brille@ti.com)
+ *
+ *
+ * (C) Texas Instruments 2001
+ *
+ ******************************************************************************/
+
+#ifndef __PWR_MESSAGES_I_H__
+#define __PWR_MESSAGES_I_H__
+
+#include "rv_general.h"
+#include "pwr_api.h"
+#include "spi_api.h"
+
+
+
+/* Events that can be sent by the power SWE API to the SPI task */
+#define PWR_SPI_INFO_BATTERY_EVT    (1)
+
+
+
+/* Prototypes */
+
+/* Functions used to send messages to the SPI task */
+T_RV_RET pwr_spi_battery_info(CALLBACK_FUNC CallBack);
+
+#endif /* __PWR_MESSAGES_I_H__ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_process.c	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,83 @@
+/*****************************************************************************/
+/*                                                                           */
+/*  Name       pwr_process.c                                                 */
+/*                                                                           */
+/*  Function   this file contains the pwr_process function, used to          */
+/*			   handle messages received in the SPI task mailbox.             */
+/*                                                                           */
+/*  Version    0.1                                                           */
+/*                                                                           */
+/*  Date       Modification                                                  */
+/*  ------------------------------------                                     */
+/*                                                                           */
+/* Author      Candice Bazanegue                                             */
+/*                                                                           */
+/* (C) Copyright 2000 by Texas Instruments Incorporated, All Rights Reserved */
+/*****************************************************************************/
+
+#include "rv_defined_swe.h"	   // for RVM_PWR_SWE
+
+#ifdef RVM_PWR_SWE
+
+#include "abb.h"
+#include "rvm_use_id_list.h"
+#include "rvf_api.h"
+#include "pwr_messages_i.h"
+#include "spi_env.h"
+
+
+/*******************************************************************************
+** Function         pwr_process
+**
+** Description     It is called by the spi task core.                                        
+**                 Messages can be related to ABB interrupt handling, or        
+**                 battery charge management. 
+**
+*******************************************************************************/
+void pwr_process(T_RV_HDR * msg_ptr)
+{
+   void *ptr = NULL;
+
+   if (msg_ptr != NULL)
+   {
+      switch (msg_ptr->msg_id)
+      {
+         case PWR_SPI_INFO_BATTERY_EVT:
+		 {
+            /* Battery voltage and charger current */
+            if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
+            {
+               /* start the channels conversion */
+               ABB_Write_Register_on_page(PAGE0, VBATREG, 0x0000);
+            }
+
+            /* Callback function */
+            if(((T_RV_HDR *)msg_ptr)->callback_func != NULL)
+            {
+               ((T_RV_HDR *)msg_ptr)->callback_func(ptr);
+            }
+
+            rvf_free_buf ((void *) msg_ptr);
+
+            break;
+		 }
+
+         default:
+	     {
+            /* Unknow message has been received */
+            rvf_send_trace("PWR_task : Received an unknown message",38, NULL_PARAM ,
+                           RV_TRACE_LEVEL_DEBUG_HIGH, PWR_USE_ID);
+
+            rvf_free_buf ((void *) msg_ptr);
+
+            break;
+         } 
+      }   // end of switch
+   }   // end of if
+   return;
+}
+
+#endif /* #ifdef RVM_PWR_SWE */
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/pwr/pwr_process.h	Sun Oct 09 05:59:14 2016 +0000
@@ -0,0 +1,25 @@
+/*****************************************************************************/
+/*                                                                           */
+/*  Name       pwr_process.h                                                 */
+/*                                                                           */
+/*  Function    this file contains the pwr_process function prototype,       */
+/*				used to handle messages received in the SPI task mailbox.    */
+/*                                                                           */
+/*  Version    0.1                                                           */
+/*                                                                           */
+/*  Date       Modification                                                  */
+/*  ------------------------------------                                     */
+/*                                                                           */
+/* Author      Candice Bazanegue                                             */
+/*                                                                           */
+/* (C) Copyright 2000 by Texas Instruments Incorporated, All Rights Reserved */
+/*****************************************************************************/
+
+#ifndef __PWR_PROCESS_H__
+#define __PWR_PROCESS_H__
+
+
+/* Prototypes */
+void pwr_process(T_RV_HDR * msg_ptr);
+
+#endif /* __PWR_PROCESS_H__ */
\ No newline at end of file