forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBq2579x.cs
1188 lines (925 loc) · 42.7 KB
/
Bq2579x.cs
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
////
using System;
using System.Device.I2c;
using System.Device.Model;
using UnitsNet;
namespace Iot.Device.Bq2579x
{
/// <summary>
/// Buck-Boost Battery Charger Bq2579x.
/// </summary>
[Interface("Buck-Boost Battery Charger Bq2579x")]
public class Bq2579x : IDisposable
{
/// <summary>
/// Bq25792/8 Default I2C Address.
/// </summary>
public const byte DefaultI2cAddress = 0x6B;
private const byte DevicePartNumberMask = 0b00111000;
private const byte PrechargeCurrentLimitMask = 0b00111111;
private const byte ChargerStatus0PowerGoodStatusMask = 0b0000_1000;
private const byte ChargerStatus1ChargeStatusMask = 0b1110_0000;
private const byte ChargerStatus1VbusStatusMask = 0b0001_1110;
private const byte ChargerStatus1Bc12DetectionMask = 0b0000_0001;
private const byte ChargerControl1WatchdogMask = 0b0000_0111;
private const byte AdcControlEnableMask = 0b1000_0000;
private const byte AdcControlConversionRateMask = 0b0100_0000;
private const byte AdcControlResolutionMask = 0b0011_0000;
private const byte AdcControlAverageControlMask = 0b0000_1000;
private const byte AdcControlInitialAverageMask = 0b0000_0100;
private const byte ThermalRegulationThresholdMask = 0b0000_0011;
private const byte ThermalShutdownThresholdMask = 0b0000_1100;
private const byte ChargerStatus3IcoStatusMask = 0b1100_0000;
private const byte NtcControl0ChargerVoltageMask = 0b1110_0000;
private const byte NtcControl0ChargeCurrentMask = 0b0001_1000;
private const byte NtcControl1Vt2ComparatorVoltageMask = 0b1100_0000;
private const byte NtcControl1Vt3ComparatorVoltageMask = 0b0011_0000;
private const byte NtcControl1OtgHotTemperatureMask = 0b0000_1100;
private const byte NtcControl1OtgColdTemperatureMask = 0b0000_0010;
private const byte NtcControl1IgnoreTSMask = 0b0000_0001;
private const int FixedOffsetMinimalSystemVoltage = 2500;
private const int MaxValueMinimalSystemVoltage = 16000;
private const int StepMinimalSystemVoltage = 250;
private const int PrechargeCurrentLimitMinValue = 40;
private const int StepPrechargeCurrentLimit = 40;
private const int PrechargeCurrentLimitMaxValue = 2000;
private const int FixedOffsetMinimalChargeVoltageLimit = 3000;
private const int MaxValueChargeVoltageLimit = 18800;
private const int FixedOffsetMinimalChargeCurrentLimit = 50;
private const int MaxValueChargeCurrentLimit = 5000;
private const int MaxValueInputVoltageLimit = 22000;
private const int MinValueInputVoltageLimit = 3600;
private const int FixedOffsetMinimalInputCurrentLimit = 100;
private const int MaxValueInputCurrentLimit = 3300;
private const int VbusAdcStep = 1;
private const int ChargeVoltageStep = 10;
private const int ChargeInputCurrentStep = 10;
private const int InputVoltageStep = 100;
private const float TdieStemp = 0.5f;
private I2cDevice _i2cDevice;
private Model _deviceModel;
/// <summary>
/// Gets de model of the Bq2579x device connected.
/// </summary>
public Model Model => _deviceModel;
/// <summary>
/// Gets the charge status.
/// </summary>
[Property]
public ChargeStatus ChargeStatus => GetChargeStatus();
/// <summary>
/// Gets the VBUS status.
/// </summary>
[Property]
public VbusStatus VbusStatus => GetVbusStatus();
/// <summary>
/// Gets BC1.2 or non-standard detection.
/// </summary>
/// <value><see langword="true"/> if BC1.2 or non-standard detection is complete, otherwise <see langword="false"/>.</value>
[Property]
public bool Bc12Detection => GetBc12Detection();
/// <summary>
/// Gets or sets a value indicating whether the ADC is enable.
/// </summary>
/// <value><see langword="true"/> to enable ADC, otherwise <see langword="false"/>.</value>
[Property]
public bool AdcEnable { get => GetAdcEnable(); set => SetAdcEnable(value); }
/// <summary>
/// Gets or sets ADC conversion rate.
/// </summary>
[Property]
public AdcConversioRate AdcConversionRate { get => GetAdcConversionRate(); set => SetAdcConversionRate(value); }
/// <summary>
/// Gets or sets ADC resolution.
/// </summary>
[Property]
public AdcResolution AdcResolution { get => GetAdcResolution(); set => SetAdcResolution(value); }
/// <summary>
/// Gets or sets ADC resolution.
/// </summary>
[Property]
public AdcAveraging AdcAveraging { get => GetAdcAveraging(); set => SetAdcAveraging(value); }
/// <summary>
/// Gets or sets ADC resolution.
/// </summary>
[Property]
public AdcInitialAverage AdcInitialAverage { get => GetAdcInitialAverage(); set => SetAdcInitialAverage(value); }
/// <summary>
/// Gets or sets minimal system voltage.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If value is out of range (2500mV-16000mV).</exception>
/// <remarks>
/// <para>
/// During POR, the device reads the resistance tie to PROG pin, to identify the default battery cell count and determine the default power.
/// </para>
/// <para>
/// Range : 2500mV-16000mV
/// </para>
/// </remarks>
[Property]
public ElectricPotentialDc MinimalSystemVoltage { get => GetMinimalSystemVoltage(); set => SetMinimalSystemVoltage(value); }
/// <summary>
/// Gets or sets Battery Voltage Limit.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If value is out of range (3000mV-18800mV).</exception>
/// <remarks>
/// <para>
/// During POR, the device reads the resistance tie to PROG pin, to identify the default battery cell count and determine the default power-on battery voltage regulation limit.
/// </para>
/// <para>
/// Range: 3000mV-18800mV.
/// </para>
/// </remarks>
public ElectricPotentialDc ChargeVoltageLimit { get => GetChargeVoltageLimit(); set => SetChargeVoltageLimit(value); }
/// <summary>
/// Gets or sets Charge Current Limit.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If value is out of range (50mA-5000mA).</exception>
/// <remarks>
/// <para>
/// During POR, the device reads the resistance tie to PROG pin, to identify the default battery cell count and determine the default power-on battery charging current: 1A.
/// </para>
/// <para>
/// Range: 50mA-5000mA.
/// </para>
/// </remarks>
public ElectricCurrent ChargeCurrentLimit { get => GetChargeCurrentLimit(); set => SetChargeCurrentLimit(value); }
/// <summary>
/// Gets or sets Input Voltage Limit.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If value is out of range (3600mV-22000mV).</exception>
/// <remarks>
/// <para>
/// VINDPM register is reset to 3600mV upon adapter unplugged and it is set to the value based on the VBUS measurement when the adapter plugs in. It is not reset by the REG_RST and the WATCHDOG.
/// </para>
/// <para>
/// Range: 3600mV-22000mV.
/// </para>
/// </remarks>
public ElectricPotentialDc InputVoltageLimit { get => GetInputVoltageLimit(); set => SetInputVoltageLimit(value); }
/// <summary>
/// Gets or sets Input Current Limit.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If value is out of range (100mA-3300mA).</exception>
/// <remarks>
/// <para>
/// Based on D+/D- detection results:
/// USB SDP = 500mA
/// USB CDP = 1.5A
/// USB DCP = 3.25A
/// Adjustable High Voltage DCP = 1.5A
/// Unknown Adapter = 3A
/// Non-Standard Adapter = 1A/2A/2.1A/2.4A
/// </para>
/// <para>
/// Range: 100mA-3300mA.
/// </para>
/// </remarks>
public ElectricCurrent InputCurrentLimit { get => GetInputCurrentLimit(); set => SetInputCurrentLimit(value); }
/// <summary>
/// Gets or sets battery voltage thresholds for the transition from precharge to fast charge.
/// </summary>
/// <remarks>Defined as a ratio of battery regulation limit(VREG).</remarks>
[Property]
public ThresholdFastCharge FastChargeTransitionVoltage { get => GetThresholdFastCharge(); set => SetThresholdFastCharge(value); }
/// <summary>
/// Gets or sets precharge current limit (in steps of 40mA).
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If value is out of range (40mA-2000mA).</exception>
/// <remarks>Equivalent range: 40mA-2000mA.</remarks>
[Property]
public ElectricCurrent PrechargeCurrentLimit { get => GetPrechargeCurrentLimit(); set => SetPrechargeCurrentLimit(value); }
/// <summary>
/// Gets VBUS.
/// </summary>
/// <remarks>Range: 0mV-30000mV.</remarks>
public ElectricPotentialDc Vbus => GetAdcVoltage(Register.REG35_VBUS_ADC);
/// <summary>
/// Gets VAC1.
/// </summary>
/// <remarks>Range: 0mV-30000mV.</remarks>
public ElectricPotentialDc Vac1 => GetAdcVoltage(Register.REG37_VAC1_ADC);
/// <summary>
/// Gets VAC2.
/// </summary>
/// <remarks>Range: 0mV-30000mV.</remarks>
public ElectricPotentialDc Vac2 => GetAdcVoltage(Register.REG39_VAC2_ADC);
/// <summary>
/// Gets VBAT.
/// </summary>
/// <remarks>Range: 0mV-20000mV.</remarks>
public ElectricPotentialDc Vbat => GetAdcVoltage(Register.REG3B_VBAT_ADC);
/// <summary>
/// Gets VSYS.
/// </summary>
/// <remarks>Range: 0mV-24000mV.</remarks>
public ElectricPotentialDc Vsys => GetAdcVoltage(Register.REG3D_VSYS_ADC);
/// <summary>
/// Gets die temperature.
/// </summary>
/// <remarks>Range: -40°C -150°C.</remarks>
public Temperature DieTemperature => GetDieTemperature();
/// <summary>
/// Gets or sets the watchdog timer setting (in milliseconds).
/// </summary>
public WatchdogSetting WatchdogTimerSetting { get => GetWatchdogTimerSetting(); set => SetWatchdogTimerSetting(value); }
/// <summary>
/// Gets or sets Thermal regulation threshold.
/// </summary>
[Property]
public ThermalRegulationThreshold ThermalRegulationThreshold { get => GetThermalRegulationThreshold(); set => SetThermalRegulationThreshold(value); }
/// <summary>
/// Gets or sets Thermal shutdown threshold.
/// </summary>
[Property]
public ThermalShutdownThreshold ThermalShutdownThreshold { get => GetThermalShutdownThreshold(); set => SetThermalShutdownThreshold(value); }
/// <summary>
/// Gets or sets charge voltage for JEITA high temperature range (TWARN - THOT).
/// </summary>
[Property]
public ChargeVoltage ChargeVoltageHighTempRange { get => GetChargeVoltage(); set => SetChargeVoltage(value); }
/// <summary>
/// Gets or sets charge current for JEITA high temperature range (TWARN - THOT).
/// </summary>
[Property]
public ChargeCurrent ChargeCurrentHighTempRange { get => GetChargeCurrentHighTempRange(); set => SetChargeCurrentHighTempRange(value); }
/// <summary>
/// Gets or sets charge current for JEITA low temperature range (TCOLD - TCOOL).
/// </summary>
[Property]
public ChargeCurrent ChargeCurrentLowTempRange { get => GetChargeCurrentLowTempRange(); set => SetChargeCurrentLowTempRange(value); }
/// <summary>
/// Gets or sets VT2 comparator voltage rising thresholds as a percentage of REGN.
/// </summary>
[Property]
public CompVoltageRisingThreshold Vt2ComparatorRisingThreshold { get => GetVt2ComparatorVoltage(); set => SetVt2ComparatorVoltage(value); }
/// <summary>
/// Gets or sets VT3 comparator voltage falling thresholds as a percentage of REGN.
/// </summary>
[Property]
public CompVoltageFallingThreshold Vt3ComparatorFallingThreshold { get => GetVt3ComparatorVoltage(); set => SetVt3ComparatorVoltage(value); }
/// <summary>
/// Gets or sets OTG mode TS HOT temperature threshold.
/// </summary>
[Property]
public OtgHotTempThreshold OtgHotTempThreshold { get => GetOtgHotTempThreshold(); set => SetOtgHotTempThreshold(value); }
/// <summary>
/// Gets or sets OTG mode TS COLD temperature threshold.
/// </summary>
[Property]
public OtgColdTempThreshold OtgColdTempThreshold { get => GetOtgColdTempThreshold(); set => SetOtgColdTempThreshold(value); }
/// <summary>
/// Gets or sets a value indicating whether the temperature sensor feedback should be ignored.
/// </summary>
/// <value><see langword="true"/> if ignoring temperature sensor feedback, otherwise <see langword="false"/>.</value>
/// <remarks>
/// Ignore the temperature sensor (TS) feedback, the charger considers the TS is always good to allow the charging and OTG modes. <see cref="Vt2ComparatorRisingThreshold"/> and <see cref="Vt3ComparatorFallingThreshold"/> always read 0 to report the normal condition.
/// </remarks>
[Property]
public bool IgnoreTempSensor { get => GetIgnoreTempSensor(); set => SetIgnoreTempSensor(value); }
/// <summary>
/// Gets Power Good Status.
/// </summary>
/// <value><see langword="true"/> if power is good, otherwise <see langword="false"/>.</value>
[Property]
public bool IsPowerGood => GetChargerStatus0().HasFlag(ChargerStatus0.PowerGood);
/// <summary>
/// Gets state of VAC2 inserted.
/// </summary>
/// <value><see langword="true"/> if VAC2 is inserted, otherwise <see langword="false"/>.</value>
[Property]
public bool IsVac2Present => GetChargerStatus0().HasFlag(ChargerStatus0.Vac2Inserted);
/// <summary>
/// Gets state of VBUS inserted.
/// </summary>
/// <value><see langword="true"/> if VBUS is inserted, otherwise <see langword="false"/>.</value>
[Property]
public bool IsVbusPresent => GetChargerStatus0().HasFlag(ChargerStatus0.VbusPresent);
/// <summary>
/// Gets state of VAC1 inserted.
/// </summary>
/// <value><see langword="true"/> if VAC1 is inserted, otherwise <see langword="false"/>.</value>
[Property]
public bool IsVac1Present => GetChargerStatus0().HasFlag(ChargerStatus0.Vac1Inserted);
/// <summary>
/// Gets state IC thermal regulation.
/// </summary>
/// <value><see langword="true"/> if device is in thermal regulation, <see langword="false"/> for normal operation.</value>
[Property]
public bool IsInThermalRegulation => GetChargerStatus2().HasFlag(ChargerStatus2.ThermalRegulation);
/// <summary>
/// Gets D+/D- detection status bits.
/// </summary>
/// <value><see langword="true"/> if D+/D- detection is ongoing, <see langword="false"/> D+/D- detection is NOT started yet, or the detection is done.</value>
[Property]
public bool DpDmDetectionOngoing => GetChargerStatus2().HasFlag(ChargerStatus2.DpDmStatus);
/// <summary>
/// Gets battery present status.
/// </summary>
/// <value><see langword="true"/> if VBAT present, otherwise <see langword="false"/>.</value>
[Property]
public bool IsVbatPresent => GetChargerStatus2().HasFlag(ChargerStatus2.VbatPresent);
/// <summary>
/// Gets Input Current Optimizer (ICO) status.
/// </summary>
[Property]
public IcoStatus IcoStatus => GetIcoStatus();
/// <summary>
/// Initializes a new instance of the <see cref="Bq2579x" /> class.
/// </summary>
/// <param name="i2cDevice"><see cref="I2cDevice"/> to communicate with Si7021 device.</param>
/// <exception cref="InvalidOperationException">When failing to read part information.</exception>
/// <exception cref="NotSupportedException">If the part information returned is invalid, thus the connected part is not one of the supported BQ2579x devices.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="i2cDevice"/> is null.</exception>
public Bq2579x(I2cDevice i2cDevice)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException();
// read part information
ReadPartInformation();
}
/// <summary>
/// Read part information to perform a sanity check for the correct part.
/// </summary>
private void ReadPartInformation()
{
byte[] buffer = ReadFromRegister(Register.REG48_Part_Information, 1);
_deviceModel = (Model)(buffer[0] & DevicePartNumberMask);
// sanity check for valid part
if (_deviceModel != Model.Bq25792
&& _deviceModel != Model.Bq25798)
{
throw new NotSupportedException();
}
}
/// <summary>
/// Reset I2C device watchdog.
/// </summary>
/// <exception cref="InvalidOperationException">If the I2C operation to write to the device to reset the watchdog fails.</exception>
public void ResetWatchdog()
{
WriteToRegister(Register.REG10_Charger_Control_1, 0b0000_1000);
}
/// <inheritdoc/>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null;
}
#region REG1B_Charger_Status_0
// REG1B_Charger_Status_0 Register
// | 7 6 5 4 3 2 1 0 |
// | IINDPM_STAT | VINDPM_STAT | WD_STAT | RESERVED | PG_STAT | AC2_PRESENT_STAT | AC1_PRESENT_STAT | VBUS_PRESENT_STAT |
////
private ChargerStatus0 GetChargerStatus0()
{
byte[] buffer = ReadFromRegister(Register.REG1C_Charger_Status_1, 1);
return (ChargerStatus0)buffer[0];
}
#endregion
#region REG1C_Charger_Status_1
// REG1C_Charger_Status_1 Register
// | 7 6 5 | 4 3 2 1 | 0 |
// | CHG_STAT_2:0 | VBUS_STAT_3:0 | BC1.2_DONE_STAT |
////
private ChargeStatus GetChargeStatus()
{
byte[] buffer = ReadFromRegister(Register.REG1C_Charger_Status_1, 1);
return (ChargeStatus)(buffer[0] & ChargerStatus1ChargeStatusMask);
}
private VbusStatus GetVbusStatus()
{
byte[] buffer = ReadFromRegister(Register.REG1C_Charger_Status_1, 1);
return (VbusStatus)(buffer[0] & ChargerStatus1VbusStatusMask);
}
private bool GetBc12Detection()
{
byte[] buffer = ReadFromRegister(Register.REG1C_Charger_Status_1, 1);
return (buffer[0] & ChargerStatus1Bc12DetectionMask) == 1;
}
#endregion
#region REG1D_Charger_Status_2
// REG1D_Charger_Status_2 Register
// | 7 6 | 5 4 3 | 2 | 1 | 0 |
// | ICO_STAT_1:0 | RESERVED | TREG_STAT | DPDM_STAT | VBAT_PRESENT_STAT |
////
private ChargerStatus2 GetChargerStatus2()
{
byte[] buffer = ReadFromRegister(Register.REG1D_Charger_Status_2, 1);
return (ChargerStatus2)buffer[0];
}
private IcoStatus GetIcoStatus()
{
byte[] buffer = ReadFromRegister(Register.REG1D_Charger_Status_2, 1);
return (IcoStatus)(buffer[0] & ChargerStatus3IcoStatusMask);
}
#endregion
#region REG17_NTC_Control_0
// REG17_NTC_Control_0 Register
// | 7 6 5 | 4 3 | 2 1 | 0 |
// | JEITA_VSET_2:0 | JEITA_ISETH_1:0 | JEITA_ISETC_1:0 | RESERVED |
////
private ChargeVoltage GetChargeVoltage()
{
byte[] buffer = ReadFromRegister(Register.REG17_NTC_Control_0, 1);
return (ChargeVoltage)(buffer[0] & NtcControl0ChargerVoltageMask);
}
private void SetChargeVoltage(ChargeVoltage value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG17_NTC_Control_0, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl0ChargerVoltageMask);
// set value
buffer[0] |= (byte)value;
WriteToRegister(Register.REG17_NTC_Control_0, buffer[0]);
}
private ChargeCurrent GetChargeCurrentHighTempRange()
{
byte[] buffer = ReadFromRegister(Register.REG17_NTC_Control_0, 1);
// need to shift 3 positions to the right to get the value
return (ChargeCurrent)((buffer[0] & NtcControl0ChargeCurrentMask) >> 3);
}
private void SetChargeCurrentHighTempRange(ChargeCurrent value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG17_NTC_Control_0, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl0ChargeCurrentMask);
// set value
// need to shift 3 positions to the left to get the value into the correct position
buffer[0] |= (byte)((byte)value << 3);
WriteToRegister(Register.REG17_NTC_Control_0, buffer[0]);
}
private ChargeCurrent GetChargeCurrentLowTempRange()
{
byte[] buffer = ReadFromRegister(Register.REG17_NTC_Control_0, 1);
// need to shift 1 position to the right to get the value
return (ChargeCurrent)((buffer[0] & NtcControl0ChargeCurrentMask) >> 1);
}
private void SetChargeCurrentLowTempRange(ChargeCurrent value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG17_NTC_Control_0, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl0ChargeCurrentMask);
// set value
// need to shift 1 position to the left to get the value into the correct position
buffer[0] |= (byte)((byte)value << 1);
WriteToRegister(Register.REG17_NTC_Control_0, buffer[0]);
}
#endregion
#region REG18_NTC_Control_1
// REG18_NTC_Control_1 Register
// | 7 6 | 5 4 | 3 2 | 1 | 0 |
// | TS_COOL_1:0 | TS_WARM_1:0 | BHOT_1:0 | BCOLD | TS_IGNORE |
////
private CompVoltageRisingThreshold GetVt2ComparatorVoltage()
{
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
return (CompVoltageRisingThreshold)(buffer[0] & NtcControl1Vt2ComparatorVoltageMask);
}
private void SetVt2ComparatorVoltage(CompVoltageRisingThreshold value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl1Vt2ComparatorVoltageMask);
// set value
buffer[0] |= (byte)value;
WriteToRegister(Register.REG18_NTC_Control_1, buffer[0]);
}
private CompVoltageFallingThreshold GetVt3ComparatorVoltage()
{
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
return (CompVoltageFallingThreshold)(buffer[0] & NtcControl1Vt3ComparatorVoltageMask);
}
private void SetVt3ComparatorVoltage(CompVoltageFallingThreshold value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl1Vt3ComparatorVoltageMask);
// set value
buffer[0] |= (byte)value;
WriteToRegister(Register.REG18_NTC_Control_1, buffer[0]);
}
private OtgHotTempThreshold GetOtgHotTempThreshold()
{
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
return (OtgHotTempThreshold)(buffer[0] & NtcControl1OtgHotTemperatureMask);
}
private void SetOtgHotTempThreshold(OtgHotTempThreshold value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl1OtgHotTemperatureMask);
// set value
buffer[0] |= (byte)value;
WriteToRegister(Register.REG18_NTC_Control_1, buffer[0]);
}
private OtgColdTempThreshold GetOtgColdTempThreshold()
{
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
return (OtgColdTempThreshold)(buffer[0] & NtcControl1OtgColdTemperatureMask);
}
private void SetOtgColdTempThreshold(OtgColdTempThreshold value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl1OtgColdTemperatureMask);
// set value
buffer[0] |= (byte)value;
WriteToRegister(Register.REG18_NTC_Control_1, buffer[0]);
}
private bool GetIgnoreTempSensor()
{
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
return (buffer[0] & NtcControl1IgnoreTSMask) == 1;
}
private void SetIgnoreTempSensor(bool value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG18_NTC_Control_1, 1);
// clear bits
buffer[0] = (byte)(buffer[0] & ~NtcControl1IgnoreTSMask);
// set value
buffer[0] |= (byte)(value ? 0b0001 : 0b0000);
WriteToRegister(Register.REG18_NTC_Control_1, buffer[0]);
}
#endregion
#region REG00_Minimal_System_Voltage
// REG00_Minimal_System_Voltage
// | 7 6 | 5 4 3 2 1 0 |
// | RESERVED | VSYSMIN_5:0 |
////
private ElectricPotentialDc GetMinimalSystemVoltage()
{
byte[] buffer = ReadFromRegister(Register.REG00_Minimal_System_Voltage, 1);
return new ElectricPotentialDc(
(buffer[0] * StepMinimalSystemVoltage) + FixedOffsetMinimalSystemVoltage,
UnitsNet.Units.ElectricPotentialDcUnit.MillivoltDc);
}
private void SetMinimalSystemVoltage(ElectricPotentialDc value)
{
// sanity check
if (value.MillivoltsDc < FixedOffsetMinimalSystemVoltage
|| value.MillivoltsDc > MaxValueMinimalSystemVoltage)
{
throw new ArgumentOutOfRangeException();
}
// read existing content
byte[] buffer = ReadFromRegister(Register.REG00_Minimal_System_Voltage, 1);
// divide by step value, as the register takes the value as 240mV steps
var newValue = value.MillivoltsDc / StepMinimalSystemVoltage;
// process value to replace VSYSMIN_5:0
// no need to mask as the value has to be already 6 bits wide
buffer[0] |= (byte)newValue;
WriteToRegister(Register.REG00_Minimal_System_Voltage, buffer);
}
#endregion
#region REG01_Charge_Voltage_Limit
// REG01_Charge_Voltage_Limit
// | 15 14 13 12 11 | 10 9 8 | 7 6 5 4 3 2 1 0 |
// | RESERVED | VREG_10:0 |
////
private ElectricPotentialDc GetChargeVoltageLimit()
{
byte[] buffer = ReadFromRegister(Register.REG01_Charge_Voltage_Limit, 2);
var vbus = (buffer[0] << 8) | buffer[1];
return new ElectricPotentialDc(
vbus * ChargeVoltageStep,
UnitsNet.Units.ElectricPotentialDcUnit.MillivoltDc);
}
private void SetChargeVoltageLimit(ElectricPotentialDc value)
{
// sanity check
if (value.MillivoltsDc < FixedOffsetMinimalChargeVoltageLimit
|| value.MillivoltsDc > MaxValueChargeVoltageLimit)
{
throw new ArgumentOutOfRangeException();
}
// divide by step value, as the register takes the value as 10mV steps
var newValue = value.MillivoltsDc / ChargeVoltageStep;
byte[] buffer = new byte[2];
// process value
buffer[0] |= (byte)newValue;
buffer[1] |= (byte)((int)newValue >> 8);
WriteToRegister(Register.REG01_Charge_Voltage_Limit, buffer);
}
#endregion
#region REG03_Charge_Current_Limit
// REG03_Charge_Current_Limit
// | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 |
// | RESERVED | ICHG_8:0 |
////
private ElectricCurrent GetChargeCurrentLimit()
{
byte[] buffer = ReadFromRegister(Register.REG03_Charge_Current_Limit, 2);
var vbus = (buffer[0] << 8) | buffer[1];
return new ElectricCurrent(
vbus * ChargeInputCurrentStep,
UnitsNet.Units.ElectricCurrentUnit.Milliampere);
}
private void SetChargeCurrentLimit(ElectricCurrent value)
{
// sanity check
if (value.Milliamperes < FixedOffsetMinimalChargeCurrentLimit
|| value.Milliamperes > MaxValueChargeCurrentLimit)
{
throw new ArgumentOutOfRangeException();
}
// divide by step value, as the register takes the value as 10mA steps
var newValue = value.Milliamperes / ChargeInputCurrentStep;
byte[] buffer = new byte[2];
// process value
buffer[0] |= (byte)newValue;
buffer[1] |= (byte)((int)newValue >> 8);
WriteToRegister(Register.REG03_Charge_Current_Limit, buffer);
}
#endregion
#region REG05_Input_Voltage_Limit
// REG05_INput_Voltage_Limit
// | 7 6 5 4 3 2 1 0 |
// | VINDPM_7:0 |
////
private ElectricPotentialDc GetInputVoltageLimit()
{
byte[] buffer = ReadFromRegister(Register.REG05_Input_Voltage_Limit, 1);
return new ElectricPotentialDc(
buffer[0] * InputVoltageStep,
UnitsNet.Units.ElectricPotentialDcUnit.MillivoltDc);
}
private void SetInputVoltageLimit(ElectricPotentialDc value)
{
// sanity check
if (value.MillivoltsDc < MinValueInputVoltageLimit
|| value.MillivoltsDc > MaxValueInputVoltageLimit)
{
throw new ArgumentOutOfRangeException();
}
// divide by step value, as the register takes the value as 100mV steps
var newValue = value.MillivoltsDc / InputVoltageStep;
WriteToRegister(Register.REG05_Input_Voltage_Limit, (byte)newValue);
}
#endregion
#region REG06_Input_Current_Limit
// REG06_Charge_Current_Limit
// | 15 14 13 12 11 10 9 | 8 7 6 5 4 3 2 1 0 |
// | RESERVED | IINDPM_8:0 |
////
private ElectricCurrent GetInputCurrentLimit()
{
byte[] buffer = ReadFromRegister(Register.REG06_Input_Current_Limit, 2);
var vbus = (buffer[0] << 8) | buffer[1];
return new ElectricCurrent(
vbus * ChargeInputCurrentStep,
UnitsNet.Units.ElectricCurrentUnit.Milliampere);
}
private void SetInputCurrentLimit(ElectricCurrent value)
{
// sanity check
if (value.Milliamperes < FixedOffsetMinimalInputCurrentLimit
|| value.Milliamperes > MaxValueInputCurrentLimit)
{
throw new ArgumentOutOfRangeException();
}
// divide by step value, as the register takes the value as 10mA steps
var newValue = value.Milliamperes / ChargeInputCurrentStep;
byte[] buffer = new byte[2];
// process value
buffer[0] |= (byte)newValue;
buffer[1] |= (byte)((int)newValue >> 8);
WriteToRegister(Register.REG06_Input_Current_Limit, buffer);
}
#endregion
#region REG08_Precharge_Control
// REG08_Precharge_Control Register
// | 7 6 | 5 4 3 2 1 0 |
// | VBAT_LOWV_1:0 | IPRECHG_5:0 |
////
private ThresholdFastCharge GetThresholdFastCharge()
{
byte[] buffer = ReadFromRegister(Register.REG08_Precharge_Control, 1);
return (ThresholdFastCharge)(buffer[0] >> 6);
}
private void SetThresholdFastCharge(ThresholdFastCharge value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG08_Precharge_Control, 1);
// process value to replace VBAT_LOWV_1:0
buffer[0] = (byte)(((byte)value << 6) | (byte)(buffer[0] & 0b0011_1111));
}
private ElectricCurrent GetPrechargeCurrentLimit()
{
byte[] buffer = ReadFromRegister(Register.REG08_Precharge_Control, 1);
return new ElectricCurrent(
buffer[0] & PrechargeCurrentLimitMask * StepPrechargeCurrentLimit,
UnitsNet.Units.ElectricCurrentUnit.Milliampere);
}
private void SetPrechargeCurrentLimit(ElectricCurrent value)
{
// sanity check
if (value.Milliamperes < PrechargeCurrentLimitMinValue
|| value.Milliamperes > PrechargeCurrentLimitMaxValue)
{
throw new ArgumentOutOfRangeException();
}
// read existing content
byte[] buffer = ReadFromRegister(Register.REG08_Precharge_Control, 1);
// divide by 40 as the register takes the value as 40mA steps
var newValue = value.Milliamperes / StepPrechargeCurrentLimit;
// process value to replace IPRECHG_5:0
// no need to mask as the value has to be already 6 bits wide
buffer[0] |= (byte)newValue;
}
#endregion
#region REG10_Charger_Control_1
private WatchdogSetting GetWatchdogTimerSetting()
{
byte[] buffer = ReadFromRegister(Register.REG10_Charger_Control_1, 1);
return (WatchdogSetting)(buffer[0] & ChargerControl1WatchdogMask);
}
private void SetWatchdogTimerSetting(WatchdogSetting value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG10_Charger_Control_1, 1);
buffer[0] = (byte)(buffer[0] & ~ChargerControl1WatchdogMask);
buffer[0] |= (byte)value;
WriteToRegister(Register.REG10_Charger_Control_1, buffer[0]);
}
#endregion
#region REG2E_ADC_Control
// REG08_Precharge_Control Register
// | 7 | 6 | 5 4 | 3 | 2 | 1 0 |
// | ADC_EN | ADC_RATE | ADC_SAMPLE_1:0 | ADC_AVG | ADC_AVG_INIT | RESERVED |
////
private bool GetAdcEnable()
{
byte[] buffer = ReadFromRegister(Register.REG2E_ADC_Control, 1);
return (buffer[0] & AdcControlEnableMask) != 0;
}
private void SetAdcEnable(bool value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG2E_ADC_Control, 1);
// clear bit
buffer[0] = (byte)(buffer[0] & ~AdcControlEnableMask);
// set bit if needed
if (value)
{
buffer[0] |= AdcControlEnableMask;
}
WriteToRegister(Register.REG2E_ADC_Control, buffer[0]);
}
private AdcConversioRate GetAdcConversionRate()
{
byte[] buffer = ReadFromRegister(Register.REG2E_ADC_Control, 1);
return (AdcConversioRate)(buffer[0] & AdcControlConversionRateMask);
}
private void SetAdcConversionRate(AdcConversioRate value)
{
// read existing content
byte[] buffer = ReadFromRegister(Register.REG2E_ADC_Control, 1);
// clear bit