WXK
2023-09-08 00fc23c0b2c7b7dfd6df3e9fb9e385ec949cd8cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*******************************************************************************
 * File Name         : Battery.c
 * Description       :
 * Author            : hido.ltd
 *******************************************************************************/
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "battery.h"
#include "adc.h"
#include "gpio.h"
 
/*******************************************************************************
 *                                  Macro                                      *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Local Variable                                  *
 *******************************************************************************/
static ST_GPIO l_astBatteryPin[BATTERY_PIN_MAX];
extern ADC_HandleTypeDef hadc;
 
/*******************************************************************************
 *                        Local Function Declaration                           *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Local Function                                  *
 *******************************************************************************/
 
/*******************************************************************************
 * Function Name     : Battery_MearsEnable
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : hido.ltd
 *******************************************************************************/
void Battery_MearsEnable(void)
{
    GPIO_RESET(&l_astBatteryPin[BATTERY_PIN_MEARS_CTRL]);
}
 
/*******************************************************************************
 * Function Name     : Battery_PinRegister
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : hido.ltd
 *******************************************************************************/
void Battery_MearsDisable(void)
{
    GPIO_SET(&l_astBatteryPin[BATTERY_PIN_MEARS_CTRL]);
}
 
/*******************************************************************************
 *                             Global Function                                 *
 *******************************************************************************/
 
/*******************************************************************************
 * Function Name     : Battery_GetADC
 * Description       : »ñÈ¡µç³ØADCÖµ
 * Input             : None
 * Output            : None
 * Return            : µç³ØADCÖµ
 * Author            : hido.ltd
 *******************************************************************************/
void Battery_ConfigureAdcChannel(void)
{
    ;
}
 
/*******************************************************************************
 * Function Name     : Battery_GetADC
 * Description       : »ñÈ¡µç³ØADCÖµ
 * Input             : None
 * Output            : None
 * Return            : µç³ØADCÖµ
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_UINT32 Battery_GetADC(void)
{
    HIDO_UINT32 adc_value = 0;
 
    Battery_ConfigureAdcChannel();
 
    // Æô¶¯Ò»´Î ADC ×ª»»
    HAL_ADC_Start(&hadc);
 
    // µÈ´ýת»»Íê³É
    if (HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) == HAL_OK)
    {
        // ¶Áȡת»»½á¹û
        adc_value = HAL_ADC_GetValue(&hadc);
    }
 
    // ¹Ø±Õ ADC
    HAL_ADC_Stop(&hadc);
 
    return adc_value;
}
 
/*******************************************************************************
 * Function Name     : Battery_VoltageConvert
 * Description       : µç³Øµçѹת»»
 * Input             : None
 * Output            : None
 * Return            : µç³ØADCÖµ
 * Author            : hido.ltd
 *******************************************************************************/
float Battery_VoltageConvert(HIDO_UINT32 _u32ADC)
{
    float fVoltage = (_u32ADC / 4095.0) * 3.3;
 
    fVoltage = fVoltage * 2;
 
    return fVoltage;
}
 
/*******************************************************************************
 * Function Name     : Battery_GetVoltage
 * Description       : »ñÈ¡µç³Øµçѹ
 * Input             : None
 * Output            : None
 * Return            : µç³Øµçѹ
 * Author            : hido.ltd
 *******************************************************************************/
float Battery_GetVoltage(void)
{
    float fVoltage;
 
    Battery_MearsEnable();
    HAL_Delay(100);
    fVoltage = Battery_VoltageConvert(Battery_GetADC());
    Battery_MearsDisable();
 
    return fVoltage;
}
 
/*******************************************************************************
 * Function Name     : Battery_GetPercentage
 * Description       : »ñÈ¡µç³ØÈÝÁ¿°Ù·Ö±È
 * Input             : None
 * Output            : None
 * Return            : µç³ØÈÝÁ¿°Ù·Ö±È
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_UINT32 Battery_GetPercentage(void)
{
    float fVoltage = 0;
    HIDO_UINT32 u32Percentage = 0;
 
    fVoltage = Battery_GetVoltage();
    if(fVoltage < (float)3.3)
    {
        u32Percentage = 0;
    }
    else if(u32Percentage > 4.2)
    {
        u32Percentage = 100;
    }
    else
    {
        u32Percentage = (fVoltage - (float)3.3) / (float)(4.2 - 3.3)*100;
    }
 
    return u32Percentage;
}
 
/*******************************************************************************
 * Function Name     : Battery_PinRegister
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_INT32 Battery_PinRegister(E_BatteryPin _ePin, HIDO_VOID* _pstGPIOx, HIDO_UINT16 _u16GPIOPin)
{
    if(_ePin >= BATTERY_PIN_MAX)
    {
        return HIDO_ERR;
    }
 
    l_astBatteryPin[_ePin].m_pstGPIOx = (GPIO_TypeDef *)_pstGPIOx;
    l_astBatteryPin[_ePin].m_u16GPIOPin = _u16GPIOPin;
 
    return HIDO_OK;
}