@@ -13,20 +13,23 @@ RGBled nicla::leds;
1313BQ25120A nicla::_pmic;
1414rtos::Mutex nicla::i2c_mutex;
1515bool nicla::started = false ;
16+ uint8_t nicla::_chg_reg = 0 ;
1617
1718void nicla::pingI2CThd () {
1819 while (1 ) {
1920 // already protected by a mutex on Wire operations
20- readLDOreg ();
21+ checkChgReg ();
2122 delay (10000 );
2223 }
2324}
2425
2526bool nicla::begin ()
2627{
28+ pinMode (p25, OUTPUT);
2729 pinMode (P0_10, OUTPUT);
2830 digitalWrite (P0_10, HIGH);
2931 Wire1.begin ();
32+ _chg_reg = _pmic.readByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG);
3033#ifndef NO_NEED_FOR_WATCHDOG_THREAD
3134 static rtos::Thread th (osPriorityHigh, 1024 , nullptr , " ping_thread" );
3235 th.start (&nicla::pingI2CThd);
@@ -98,8 +101,26 @@ bool nicla::enterShipMode()
98101
99102uint8_t nicla::readLDOreg ()
100103{
101- uint8_t ldo_reg = _pmic.readByte (BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
102- return ldo_reg;
104+ return _pmic.readByte (BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
105+ }
106+
107+ bool nicla::enableCharge (uint8_t mA )
108+ {
109+ digitalWrite (p25, LOW);
110+ if (mA < 35 ) {
111+ _chg_reg = ((mA -5 ) << 2 );
112+ } else {
113+ _chg_reg = (((mA -40 )/10 ) << 2 ) | 0x80 ;
114+ }
115+ _pmic.writeByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);
116+ return _pmic.readByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG) == _chg_reg;
117+ }
118+
119+ void nicla::checkChgReg ()
120+ {
121+ if (_chg_reg != _pmic.readByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG)) {
122+ _pmic.writeByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);
123+ }
103124}
104125
105126I2CLed LEDR (red);
0 commit comments