forked from arduino/ArduinoCore-mbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNicla_System.cpp
151 lines (133 loc) · 3.1 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
#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;
void nicla::pingI2CThd() {
while(1) {
// already protected by a mutex on Wire operations
readLDOreg();
delay(10000);
}
}
bool nicla::begin()
{
pinMode(P0_10, OUTPUT);
digitalWrite(P0_10, HIGH);
Wire1.begin();
#ifndef NO_NEED_FOR_WATCHDOG_THREAD
static rtos::Thread th(osPriorityHigh, 1024, nullptr, "ping_thread");
th.start(&nicla::pingI2CThd);
#endif
started = true;
return true;
}
void nicla::enableCD()
{
pinMode(p25, OUTPUT);
digitalWrite(p25, HIGH);
}
void nicla::disableCD()
{
pinMode(p25, OUTPUT);
digitalWrite(p25, LOW);
}
/*
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()
{
enableCD();
uint8_t ldo_reg = 0xE4;
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL, ldo_reg);
if (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL) != ldo_reg) {
disableCD();
return false;
}
disableCD();
return true;
}
bool nicla::enable1V8LDO()
{
enableCD();
uint8_t ldo_reg = 0xA8;
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL, ldo_reg);
if (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL) != ldo_reg) {
disableCD();
return false;
}
disableCD();
return true;
}
bool nicla::disableLDO()
{
enableCD();
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) {
disableCD();
return false;
}
disableCD();
return true;
}
uint8_t nicla::readLDOreg()
{
enableCD();
uint8_t ldo_reg = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
disableCD();
return ldo_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;
}
}