-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathNicla_System.cpp
179 lines (153 loc) · 4.25 KB
/
Nicla_System.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
#include "Nicla_System.h"
#if defined __has_include
# if __has_include (<Arduino_BHY2.h>)
# define NO_NEED_FOR_WATCHDOG_THREAD
# else
# include "rtos.h"
# endif
#endif
RGBled nicla::leds;
BQ25120A nicla::_pmic;
rtos::Mutex nicla::i2c_mutex;
bool nicla::started = false;
uint8_t nicla::_chg_reg = 0;
void nicla::pingI2CThd() {
while(1) {
// already protected by a mutex on Wire operations
checkChgReg();
delay(10000);
}
}
bool nicla::begin()
{
pinMode(p25, OUTPUT);
pinMode(P0_10, OUTPUT);
digitalWrite(P0_10, HIGH);
Wire1.begin();
_chg_reg = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG);
#ifndef NO_NEED_FOR_WATCHDOG_THREAD
static rtos::Thread th(osPriorityHigh, 1024, nullptr, "ping_thread");
th.start(&nicla::pingI2CThd);
#endif
started = true;
return true;
}
/*
LDO reg:
| B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
| EN_LDO | LDO_4 | LDO_3 | LDO_2 | LDO_1 | LDO_0 | X | X |
Conversion function:
LDO = 0.8V + LDO_CODE * 100mV
- for LDO = 3.3V:
- set LCO_CODE = 25 (0x19)
- shift to lef by 2 positions: (0x19 << 2) = 0x64
- set EN_LDO: 0xE4
- for LDO = 1.8V:
- set LCO_CODE = 10 (0x0A)
- shift to lef by 2 positions: (0x0A << 2) = 0x28
- set EN_LDO: 0xA8
*/
bool nicla::enable3V3LDO()
{
uint8_t ldo_reg = 0xE4;
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL, ldo_reg);
if (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL) != ldo_reg) {
return false;
}
return true;
}
bool nicla::enable1V8LDO()
{
uint8_t ldo_reg = 0xA8;
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL, ldo_reg);
if (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL) != ldo_reg) {
return false;
}
return true;
}
bool nicla::disableLDO()
{
uint8_t ldo_reg = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
ldo_reg &= 0x7F;
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL, ldo_reg);
if (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL) != ldo_reg) {
return false;
}
return true;
}
bool nicla::enterShipMode()
{
// STATUS reg:
// | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
// | RO | RO | EN_SHIPMODE | RO | RO | RO | RO | RO |
uint8_t status_reg = _pmic.getStatus();
status_reg |= 0x20;
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_STATUS, status_reg);
}
uint8_t nicla::readLDOreg()
{
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
}
bool nicla::enableCharge(uint8_t mA)
{
if (mA < 35) {
_chg_reg = ((mA-5) << 2);
} else {
_chg_reg = (((mA-40)/10) << 2) | 0x80;
}
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);
// For very depleted batteries, set ULVO at the vary minimum to re-enable charging
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F);
// also set max battery voltage to 4.2V (VBREG)
// _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG) == _chg_reg;
}
uint8_t nicla::getFault() {
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAULTS);
}
float nicla::getBetteryStatus() {
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON, 1);
delay(2);
uint8_t data = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON);
float res = 0.6f + (data >> 5) * 0.1f + (data >> 2) * 0.02f - 0.01f;
return res;
}
void nicla::checkChgReg()
{
if (_chg_reg != _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG)) {
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);
}
}
I2CLed LEDR(red);
I2CLed LEDG(green);
I2CLed LEDB(blue);
I2CLed LED_BUILTIN(white);
void pinMode(I2CLed pin, PinMode mode)
{
if (!nicla::started) {
nicla::begin();
nicla::leds.begin();
nicla::leds.setColor(off);
}
}
void digitalWrite(I2CLed pin, PinStatus value)
{
switch (pin.get()) {
case red:
nicla::leds.setColorRed(value == LOW ? 0 : 0xFF);
break;
case blue:
nicla::leds.setColorBlue(value == LOW ? 0 : 0xFF);
break;
case green:
nicla::leds.setColorGreen(value == LOW ? 0 : 0xFF);
break;
case white:
if (value == LOW) {
nicla::leds.setColor(0, 0, 0);
} else {
nicla::leds.setColor(0xFF, 0xFF, 0xFF);
}
break;
}
}