Skip to content

Commit a40469e

Browse files
committed
C030 Battery Charger Voltage Fix
1 parent 2e1c2a1 commit a40469e

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "battery_charger_i2c.h"
18+
19+
BatteryChargerI2c::BatteryChargerI2c(PinName sda, PinName scl):_i2c(), _hz(100000) {
20+
i2c_init(&_i2c, sda, scl);
21+
}
22+
23+
bool BatteryChargerI2c::read_from_i2c(int i2c_address, char* data_read, int length)
24+
{
25+
int bytes_read = i2c_read(&_i2c, i2c_address, data_read, length, 1);
26+
return (length == bytes_read);
27+
}
28+
29+
bool BatteryChargerI2c::write_to_i2c(int i2c_address, const char* data_write, int length)
30+
{
31+
int bytes_written = i2c_write(&_i2c, i2c_address, data_write, length, 1);
32+
return (length == bytes_written);
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef BATTERY_CHARGER_I2C
2+
#define BATTERY_CHARGER_I2C
3+
4+
#include "hal/i2c_api.h"
5+
6+
#ifdef __cplusplus
7+
extern"C"{
8+
#endif
9+
10+
class BatteryChargerI2c{
11+
12+
public:
13+
BatteryChargerI2c(PinName sda, PinName scl);
14+
bool read_from_i2c(int i2c_address, char *data_read, int length);
15+
bool write_to_i2c(int i2c_address, const char *data_write, int length);
16+
virtual ~BatteryChargerI2c() {}
17+
18+
private:
19+
i2c_t _i2c;
20+
int _hz;
21+
};
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
26+
27+
#endif //BATTERY_CHARGER_I2C
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "min_battery_voltage.h"
18+
#include "battery_charger_i2c.h"
19+
20+
/** Defining HAL_MspInit strong function
21+
* in user defined file as described in documentation
22+
*/
23+
24+
void HAL_MspInit(void)
25+
{
26+
set_minimum_battery_voltage();
27+
}
28+
29+
void set_minimum_battery_voltage()
30+
{
31+
char data_write[2] = {0};
32+
char data_read;
33+
BatteryChargerI2c i2c_object(I2C_SDA_B, I2C_SCL_B);
34+
35+
if (i2c_object.write_to_i2c(BQ24295_I2C_ADDRESS,&data_write[0] , 1)){
36+
i2c_object.read_from_i2c(BQ24295_I2C_ADDRESS, &data_read, 1);
37+
data_read = data_read & MIN_BATTERY_VOLTAGE_MASK;
38+
data_write[0] = 0x0;
39+
data_write[1] = data_read;
40+
if (i2c_object.write_to_i2c(BQ24295_I2C_ADDRESS,&data_write[0] , 2)){
41+
//Battery Voltage is set to 3880mV
42+
}
43+
}
44+
else{
45+
// Minimum battery voltage could not be set. This is not a critical error, no need to stop execution
46+
// It simply means that longer cabling or USB ports with lower output voltages may cause problems.
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef MIN_BATTERY_VOLTAGE_H
2+
#define MIN_BATTERY_VOLTAGE_H
3+
4+
#ifdef __cplusplus
5+
extern"C"{
6+
#endif
7+
8+
#define BQ24295_I2C_ADDRESS (0x6B << 1)
9+
#define MIN_BATTERY_VOLTAGE_MASK (0x87)
10+
11+
/** Initializes an instance of class BatteryChargerI2c which is using the STM HAL I2C APIs
12+
* This allows longer USB cables or USB ports with lower output voltages to power the board correctly.
13+
*/
14+
15+
void set_minimum_battery_voltage(void);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif
20+
21+
#endif // MIN_BATTERY_VOLTAGE_H

0 commit comments

Comments
 (0)