Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
BQ25713_mock.h
Go to the documentation of this file.
1
5#ifndef BQ25713_MOCK_H
6#define BQ25713_MOCK_H
7
8#include "i_ic.h"
9
10// use depend of real component (to have the registers)
12
13// hardwaree influencer simulator
15
17
18#include "src/system/hal/gpio.h"
19
21
22#include <map>
23
24namespace simulator {
25
26// independant of enabl/disable
27float targetOTGVoltage = 0.0;
28namespace __private {
29::lampda::hal::gpio::DigitalPin enableOTG(::lampda::hal::gpio::DigitalPin::GPIO::Output_EnableOnTheGo);
30}
31
33{
34public:
36 {
37 // fill register map
38
39 // ids
40 _registerMap[bq25713::MANUFACTURER_ID_ADDR] = std::make_unique<ManufacturerId>();
41 _registerMap[bq25713::DEVICE_ID_ADDR] = std::make_unique<DeviceId>();
42
43 // simple read bit
44 _registerMap[bq25713::CHARGE_OPTION_0_ADDR] = std::make_unique<Register>();
45 _registerMap[bq25713::CHARGE_OPTION_1_ADDR] = std::make_unique<Register>();
46 _registerMap[bq25713::CHARGE_OPTION_2_ADDR] = std::make_unique<Register>();
47 _registerMap[bq25713::CHARGE_OPTION_3_ADDR] = std::make_unique<Register>();
48
49 _registerMap[bq25713::ADC_OPTION_ADDR] = std::make_unique<AdcOption_Register>();
50 _registerMap[bq25713::PROCHOT_STATUS_ADDR] = std::make_unique<Register>();
51
52 _registerMap[bq25713::PROCHOT_OPTION_0_ADDR] = std::make_unique<Register>();
53 _registerMap[bq25713::PROCHOT_OPTION_1_ADDR] = std::make_unique<Register>();
54
55 // status
56 _registerMap[bq25713::CHARGE_STATUS_ADDR] = std::make_unique<ChargerStatus_Register>();
57 //
58 _registerMap[bq25713::VBAT_ADC_ADDR] = std::make_unique<VBAT_ADC_Register>();
59 _registerMap[bq25713::CHARGE_CURRENT_ADDR] = std::make_unique<Register>();
60 _registerMap[bq25713::MAX_CHARGE_VOLTAGE_ADDR] = std::make_unique<Register>();
61 _registerMap[bq25713::MINIMUM_SYSTEM_VOLTAGE_ADDR] = std::make_unique<Register>();
62 _registerMap[bq25713::OTG_VOLTAGE_ADDR] = std::make_unique<OTG_Register>();
63 _registerMap[bq25713::OTG_CURRENT_ADDR] = std::make_unique<Register>();
64 _registerMap[bq25713::INPUT_VOLTAGE_ADDR] = std::make_unique<Register>();
65 _registerMap[bq25713::IIN_HOST_ADDR] = std::make_unique<Register>();
66 _registerMap[bq25713::IIN_DPM_ADDR] = std::make_unique<Register>();
67
68 _registerMap[bq25713::ADC_VBUS_PSYS_ADC_ADDR] = std::make_unique<AdcVbusPsys_Register>();
69 _registerMap[bq25713::ADC_IBAT_ADDR] = std::make_unique<Register>();
70 _registerMap[bq25713::CMPIN_ADC_ADDR] = std::make_unique<Register>();
71 }
72
73 void run_electrical_update() override
74 {
75 // TODO
76
77 // check OTG enable
78 const uint16_t chargeOption3 = _registerMap[bq25713::CHARGE_OPTION_3_ADDR]->read();
79 if (chargeOption3)
80 {
81 const uint8_t val0 = chargeOption3 & 0xff;
82 const uint8_t val1 = (chargeOption3 >> 8) & 0xff;
83 // EN_OTG
84 if ((val1 & (1 << 0x04)) != 0 && __private::enableOTG.is_high())
85 {
86 mock_electrical::chargeOtgOutput = targetOTGVoltage;
87 }
88 else
89 {
90 mock_electrical::chargeOtgOutput = 0;
91 }
92 }
93 }
94
95 uint8_t get_i2c_address() const override { return bq25713::BQ25713::BQ25713addr; }
96
97protected:
98private:
99 // Create instance of registers data structure
100 inline static bq25713::BQ25713::Regt IcRegisters;
101
102 // interface with base registers
103 static uint16_t encode_to_base_read_register(const uint16_t val, const bq25713::BQ25713::IBaseReadRegister* reg)
104 {
105 // constraint
106 uint16_t valC =
107 (::lampda::lmpd_constrain<uint16_t>(val, reg->minVal(), reg->maxVal()) - reg->minVal()) / reg->resolution();
108 // rectified
109 uint16_t valR = (valC & reg->mask()) << reg->offset();
110 return valR;
111 }
112 static uint16_t decode_base_register_value(const uint16_t val, const bq25713::BQ25713::IBaseReadRegister* reg)
113 {
114 // unpack
115 uint16_t res = val;
116 res >>= reg->offset();
117 res &= reg->mask();
118 // convert to real units
119 return res * reg->resolution() + reg->minVal();
120 }
121
122 static uint16_t encode_to_double_register(const uint16_t val0,
123 const uint16_t val1,
125 {
126 // rectified vals
127 uint8_t val0R = (std::max<uint16_t>(val0, reg->minVal0()) - reg->minVal0()) / reg->resolutionVal0();
128 uint8_t val1R = (std::max<uint16_t>(val1, reg->minVal1()) - reg->minVal1()) / reg->resolutionVal1();
129
130 uint16_t result = (val1R & reg->maskVal1()) << 8 | (val0R & reg->maskVal0());
131 return result;
132 }
133
134 struct ChargerStatus_Register : public Register
135 {
136 uint16_t read() override
137 {
138 uint8_t val0 = 0;
139
140 uint8_t val1 = 0;
141 // val1 |= 1 << 0x07; // AC stat
142 val1 |= 1 << 0x06; // ICO done
143 val1 |= 1 << 0x00; // in OTG
144
145 return val1 << 8 | val0;
146 }
147 };
148
149 struct AdcOption_Register : public Register
150 {
151 uint16_t read() override
152 {
153 uint8_t val0 = 0;
154
155 uint8_t val1 = 0;
156 val1 |= 0 << 0x06; // ADC_START
157
158 return val1 << 8 | val0;
159 }
160 };
161
162 struct OTG_Register : public Register
163 {
164 int write(uint16_t data) override
165 {
166 const uint16_t decoded = decode_base_register_value(data, &IcRegisters.oTGVoltage);
167
168 // adjust for low range
169 targetOTGVoltage = (decoded + (IcRegisters.chargeOption3.OTG_RANGE_LOW() ? 0 : 1280) + 154) / 1000.0;
170
171 _data = data;
172 return 0;
173 }
174 };
175
176 struct AdcVbusPsys_Register : public Register
177 {
178 uint16_t read() override
179 {
180 return encode_to_double_register(
181 mock_battery::voltage * 1000.0, mock_electrical::powerRailVoltage * 1000.0, &IcRegisters.aDCVBUSPSYS);
182 }
183 };
184
185 // Battery and Vsys register
186 struct VBAT_ADC_Register : public Register
187 {
188 uint16_t read() override
189 {
190 return encode_to_double_register(
191 mock_battery::voltage * 1000.0, mock_battery::voltage * 1000.0, &IcRegisters.aDCVSYSVBAT);
192 }
193 };
194
195 struct ManufacturerId : public Register
196 {
197 uint16_t read() override { return bq25713::MANUFACTURER_ID; }
198 };
199
200 struct DeviceId : public Register
201 {
202 uint16_t read() override { return bq25713::DEVICE_ID; }
203 };
204};
205
206} // namespace simulator
207
208#endif
Definition: gpio.h:20
Definition: BQ25713_mock.h:33
Interface for the platform specific GeneralPurposeInputOutputs.
Handle the physical simulation paremeters of a real lamp.
Electrical simulation interface with the electrical simulator.
Simulator dedicated namespace.
Definition: BQ25713_mock.h:24
Definition: BQ25713.h:121
Definition: BQ25713.h:195
Definition: BQ25713.h:242
Convertion header to use the text out functions from C code.
Define useful functions.