forked from arduino-libraries/Arduino_PF1550
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPF1550_Control.cpp
302 lines (241 loc) · 8.37 KB
/
PF1550_Control.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Copyright (c) 2019 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
INCLUDE
******************************************************************************/
#include "PF1550_Control.h"
#include "util/PF1550_Util.h"
#include "PF1550.h"
/******************************************************************************
CTOR/DTOR
******************************************************************************/
PF1550_Control::PF1550_Control(PF1550_IO & io)
: _io(io)
{
}
/******************************************************************************
PUBLIC MEMBER FUNCTIONS
******************************************************************************/
void PF1550_Control::begin()
{
_io.begin();
}
void PF1550_Control::debug(Stream& stream)
{
_debug = &stream;
_io.debug(stream);
}
void PF1550_Control::setBit(Register const reg, uint8_t const bit_pos)
{
_io.setBit(reg, bit_pos);
}
void PF1550_Control::clrBit(Register const reg, uint8_t const bit_pos)
{
_io.clrBit(reg, bit_pos);
}
void PF1550_Control::writeReg(Register const reg_addr, uint8_t const val)
{
if (_debug) {
_debug->print("PF1550_Control::writeReg at address = ");
_debug->print(static_cast<uint8_t>(reg_addr), HEX);
_debug->print(" data=");
_debug->println(val, HEX);
}
_io.writeRegister(reg_addr, val);
}
void PF1550_Control::readReg(Register const reg_addr, uint8_t *data)
{
_io.readRegister(reg_addr, data);
}
void PF1550_Control::setLDO1Voltage(Ldo1Voltage const ldo1_volt)
{
writeReg(Register::PMIC_LDO1_VOLT, static_cast<uint8_t>(ldo1_volt));
}
void PF1550_Control::turnLDO1On(Ldo1Mode const mode)
{
_io.setBit(Register::PMIC_LDO1_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::turnLDO1Off(Ldo1Mode const mode)
{
_io.clrBit(Register::PMIC_LDO1_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::setLDO2Voltage(Ldo2Voltage const ldo2_volt)
{
writeReg(Register::PMIC_LDO2_VOLT, static_cast<uint8_t>(ldo2_volt));
}
void PF1550_Control::turnLDO2On(Ldo2Mode const mode)
{
_io.setBit(Register::PMIC_LDO2_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::turnLDO2Off(Ldo2Mode const mode)
{
_io.clrBit(Register::PMIC_LDO2_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::setLDO3Voltage(Ldo3Voltage const ldo3_volt)
{
writeReg(Register::PMIC_LDO3_VOLT, static_cast<uint8_t>(ldo3_volt));
}
void PF1550_Control::turnLDO3On(Ldo3Mode const mode)
{
_io.setBit(Register::PMIC_LDO3_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::turnLDO3Off(Ldo3Mode const mode)
{
_io.clrBit(Register::PMIC_LDO3_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::setSw1Voltage(Sw1Voltage const sw1_volt)
{
writeReg(Register::PMIC_SW1_VOLT, static_cast<uint8_t>(sw1_volt));
}
void PF1550_Control::setSw1VoltageStandby(Sw1Voltage const sw1_volt_standby)
{
writeReg(Register::PMIC_SW1_STBY_VOLT, static_cast<uint8_t>(sw1_volt_standby));
}
void PF1550_Control::setSw1VoltageSleep(Sw1Voltage const sw1_volt_sleep)
{
writeReg(Register::PMIC_SW1_SLP_VOLT, static_cast<uint8_t>(sw1_volt_sleep));
}
void PF1550_Control::setSw1CurrentLimit(Sw1CurrentLimit const sw1_current_limit)
{
uint8_t sw1_ctrl1_reg;
_io.readRegister(Register::PMIC_SW1_CTRL1, &sw1_ctrl1_reg);
sw1_ctrl1_reg &= ~REG_SW1_CTRL1_SW1_ILIM_mask;
sw1_ctrl1_reg |= static_cast<uint8_t>(sw1_current_limit);
writeReg(Register::PMIC_SW1_CTRL1, sw1_ctrl1_reg);
}
void PF1550_Control::turnSw1On(Sw1Mode const mode)
{
_io.setBit(Register::PMIC_SW1_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::turnSw1Off(Sw1Mode const mode)
{
_io.clrBit(Register::PMIC_SW1_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::setSw2Voltage(Sw2Voltage const sw2_volt)
{
writeReg(Register::PMIC_SW2_VOLT, static_cast<uint8_t>(sw2_volt));
}
void PF1550_Control::setSw2VoltageStandby(Sw2Voltage const sw2_volt_standby)
{
writeReg(Register::PMIC_SW2_STBY_VOLT, static_cast<uint8_t>(sw2_volt_standby));
}
void PF1550_Control::setSw2VoltageSleep(Sw2Voltage const sw2_volt_sleep)
{
writeReg(Register::PMIC_SW2_SLP_VOLT, static_cast<uint8_t>(sw2_volt_sleep));
}
void PF1550_Control::setSw2CurrentLimit(Sw2CurrentLimit const sw2_current_limit)
{
uint8_t sw2_ctrl1_reg;
_io.readRegister(Register::PMIC_SW2_CTRL1, &sw2_ctrl1_reg);
sw2_ctrl1_reg &= ~REG_SW2_CTRL1_SW2_ILIM_mask;
sw2_ctrl1_reg |= static_cast<uint8_t>(sw2_current_limit);
writeReg(Register::PMIC_SW2_CTRL1, sw2_ctrl1_reg);
}
void PF1550_Control::turnSw2On(Sw2Mode const mode)
{
_io.setBit(Register::PMIC_SW2_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::turnSw2Off(Sw2Mode const mode)
{
_io.clrBit(Register::PMIC_SW2_CTRL, static_cast<uint8_t>(mode));
}
void PF1550_Control::setFastChargeCurrent(IFastCharge const i_fast_charge)
{
uint8_t chg_curr_reg;
_io.readRegister(Register::CHARGER_CHG_CURR_CFG, &chg_curr_reg);
chg_curr_reg &= ~REG_CHG_CURR_CFG_CHG_CC_mask;
chg_curr_reg |= static_cast<uint8_t>(i_fast_charge);
writeReg(Register::CHARGER_CHG_CURR_CFG, chg_curr_reg);
}
void PF1550_Control::setFastChargeVoltage(VFastCharge const v_fast_charge)
{
uint8_t batt_reg;
_io.readRegister(Register::CHARGER_BATT_REG, &batt_reg);
batt_reg &= ~REG_BATT_REG_CHCCV_mask;
batt_reg |= static_cast<uint8_t>(v_fast_charge);
writeReg(Register::CHARGER_BATT_REG, batt_reg);
}
void PF1550_Control::setEndOfChargeCurrent(IEndOfCharge const i_end_of_charge)
{
uint8_t chg_eoc_cnfg;
_io.readRegister(Register::CHARGER_CHG_EOC_CNFG, &chg_eoc_cnfg);
chg_eoc_cnfg &= ~REG_CHG_EOC_CNFG_IEOC_mask;
chg_eoc_cnfg |= static_cast<uint8_t>(i_end_of_charge);
writeReg(Register::CHARGER_CHG_EOC_CNFG, chg_eoc_cnfg);
}
void PF1550_Control::setInputCurrentLimit(IInputCurrentLimit const i_input_current_limit)
{
uint8_t vbus_inlim_cnfg;
_io.readRegister(Register::CHARGER_VBUS_INLIM_CNFG, &vbus_inlim_cnfg);
vbus_inlim_cnfg &= ~REG_VBUS_INLIM_CNFG_VBUS_LIN_INLIM_mask;
vbus_inlim_cnfg |= static_cast<uint8_t>(i_input_current_limit);
writeReg(Register::CHARGER_VBUS_INLIM_CNFG, vbus_inlim_cnfg);
}
uint8_t PF1550_Control::getDeviceId()
{
uint8_t deviceId;
_io.readRegister(Register::PMIC_DEVICE_ID, &deviceId);
return deviceId;
}
void PF1550_Control::onPMICEvent()
{
/* Retrieve the source of the interrupt */
uint8_t int_category;
_io.readRegister(Register::PMIC_INT_CATEGORY, &int_category);
/* Call the appropriate event handler */
if(isBitSet(int_category, REG_INT_CATEGORY_CHG_INT_bp )) onChargerEvent ();
if(isBitSet(int_category, REG_INT_CATEGORY_SW1_INT_bp )) onSwitch1Event ();
if(isBitSet(int_category, REG_INT_CATEGORY_SW2_INT_bp )) onSwitch2Event ();
if(isBitSet(int_category, REG_INT_CATEGORY_SW3_INT_bp )) onSwitch3Event ();
if(isBitSet(int_category, REG_INT_CATEGORY_LDO_INT_bp )) onLdoEvent ();
if(isBitSet(int_category, REG_INT_CATEGORY_ONKEY_INT_bp)) onOnKeyEvent ();
if(isBitSet(int_category, REG_INT_CATEGORY_TEMP_INT_bp )) onTemperatureMonitorEvent();
if(isBitSet(int_category, REG_INT_CATEGORY_MISC_INT_bp )) onMiscellaneousEvent ();
}
/******************************************************************************
PRIVATE MEMBER FUNCTIONS
******************************************************************************/
void PF1550_Control::onChargerEvent()
{
/* TODO */
}
void PF1550_Control::onSwitch1Event()
{
/* TODO */
}
void PF1550_Control::onSwitch2Event()
{
/* TODO */
}
void PF1550_Control::onSwitch3Event()
{
/* TODO */
}
void PF1550_Control::onLdoEvent()
{
/* TODO */
}
void PF1550_Control::onOnKeyEvent()
{
/* TODO */
}
void PF1550_Control::onTemperatureMonitorEvent()
{
/* TODO */
}
void PF1550_Control::onMiscellaneousEvent()
{
/* TODO */
}