comparison src/cs/drivers/drv_app/pwr/pwr_disch.c @ 0:92470e5d0b9e

src: partial import from FC Selenite
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 15 May 2020 01:28:16 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:92470e5d0b9e
1 /*******************************************************************************
2 *
3 * pwr_disch.c
4 *
5 * Purpose: This file contains functions for battery discharge management.
6 *
7 * (C) Texas Instruments 2001
8 *
9 ******************************************************************************/
10
11 #include "rv/rv_defined_swe.h" // for RVM_PWR_SWE
12
13 #ifdef RVM_PWR_SWE
14
15 #include "abb/abb.h"
16 #include "rvm/rvm_use_id_list.h"
17 #include "pwr/pwr_disch.h"
18 #include "power/power.h"
19 #include "spi/spi_task.h"
20 #include "pwr/pwr_cust.h"
21 #include "pwr/pwr_messages.h"
22 #include "spi/spi_env.h"
23 #include "pwr/pwr_env.h"
24
25 /* Define a pointer to the PWR Environment control block. */
26 extern T_PWR_ENV_CTRL_BLK *pwr_env_ctrl_blk;
27
28
29
30
31 /*******************************************************************************
32 ** Function pwr_discharge_timer_process
33 **
34 ** Description
35 **
36 *******************************************************************************/
37 void pwr_discharge_timer_process(void)
38 {
39 rvf_send_trace("TIMER3", 6, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
40 if (SPI_GBL_INFO_PTR->is_gsm_on == TRUE)
41 {
42 pwr_handle_discharge(); /* battery discharge management */
43 }
44
45 }
46
47
48
49 /*******************************************************************************
50 ** Function pwr_handle_discharge
51 **
52 ** Description Compares the battery voltage with some thresholds and, if a
53 ** threshold is passed, sends event(s) to the upper layer.
54 ** Re-start the timer with a value depending on the discharge
55 ** level.
56 **
57 *******************************************************************************/
58 void pwr_handle_discharge(void)
59 {
60 UINT16 timer_value;
61 UINT16 status;
62 UINT16 bat_madc_voltage, bat_voltage;
63 static T_PWR_PERCENT remain_capacity = 100; /* since this variable is declared as static */
64 /* it will keep its value from one function call to the other */
65 T_PWR_PERCENT current_capacity;
66
67 if (SPI_GBL_INFO_PTR->is_adc_on == FALSE)
68 {
69 /* Start VBAT channel conversion by writing in the result register */
70 ABB_Write_Register_on_page(PAGE0, VBATREG, 0x0000);
71 rvf_delay(RVF_MS_TO_TICKS(5));
72 bat_madc_voltage = ABB_Read_Register_on_page(PAGE0, VBATREG);
73 rvf_send_trace("battery voltage (MADC code) ", 28, bat_madc_voltage, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
74 }
75 else /* The L1 asks for ADC conversions */
76 {
77 #ifndef _WINDOWS
78 bat_madc_voltage = SPI_GBL_INFO_PTR->adc_result[0];
79 #else
80 bat_madc_voltage = ABB_Read_Register_on_page(PAGE0, VBATREG);
81 #endif
82 }
83
84 /* Find the remaining capacity in the battery corresponding to this new voltage */
85 bat_voltage = pwr_adc_to_mvolt(bat_madc_voltage);
86 rvf_send_trace("battery voltage (mV) ", 21, bat_voltage, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
87 current_capacity = pwr_get_capacity_vs_voltage(bat_voltage);
88 rvf_send_trace("current capacity (%) ", 21, current_capacity, RV_TRACE_LEVEL_DEBUG_LOW, PWR_USE_ID);
89
90 status = ABB_Read_Status();
91
92 /* Determine if a threshold has been passed */
93 if (current_capacity != remain_capacity)
94 {
95 /* a new threshold has been passed */
96 remain_capacity = current_capacity;
97
98 /* informs the upper layer */
99 pwr_send_bat_discharge_event(remain_capacity);
100
101 if (status & CHGPRES) /* charger plugged */
102 {
103 if (remain_capacity == CHARGE_START_AGAIN_CAPACITY)
104 {
105 PWR_Charger_Plug();
106 }
107 }
108
109 else /* charger not plugged */
110 {
111 if(remain_capacity <= pwr_env_ctrl_blk->power_alert.remain_capa_threshold)
112 {
113 /* informs the upper layer that the battery is low */
114 pwr_send_low_bat_event(remain_capacity);
115 timer_value = SPI_TIMER3_INTERVAL_BIS; /* 10 s */
116 }
117 else
118 {
119 #ifndef _WINDOWS
120 timer_value = SPI_TIMER3_INTERVAL; /* 1 minute */
121 #else
122 timer_value = SPI_TIMER3_INTERVAL_BIS; /* 10 s */
123 #endif
124 }
125
126 /* Start timer with a value depending on the remaining capacity in the battery */
127 rvf_start_timer (SPI_TIMER3,
128 RVF_MS_TO_TICKS (timer_value),
129 FALSE);
130 }
131 }
132
133 else /* the capacity has not changed */
134 {
135 #ifndef _WINDOWS
136 timer_value = SPI_TIMER3_INTERVAL; /* 1 minute */
137 #else
138 timer_value = SPI_TIMER3_INTERVAL_BIS; /* 10 s */
139 #endif
140
141 /* Start timer with a value depending on the remaining capacity in the battery */
142 rvf_start_timer (SPI_TIMER3,
143 RVF_MS_TO_TICKS (timer_value),
144 FALSE);
145
146 }
147 }
148
149 #endif /* #ifdef RVM_PWR_SWE */