Skip to content

Commit 806b762

Browse files
authored
Merge pull request #5677 from u-blox/c030_bq24295_voltage_fix
C030 Battery Charger Voltage Fix
2 parents 897324e + 0469dfb commit 806b762

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
{
21+
i2c_init(&_i2c, sda, scl);
22+
}
23+
24+
bool BatteryChargerI2c::read_from_i2c(int i2c_address, char* data_read, int length)
25+
{
26+
int bytes_read = i2c_read(&_i2c, i2c_address, data_read, length, 1);
27+
return (length == bytes_read);
28+
}
29+
30+
bool BatteryChargerI2c::write_to_i2c(int i2c_address, const char* data_write, int length)
31+
{
32+
int bytes_written = i2c_write(&_i2c, i2c_address, data_write, length, 1);
33+
return (length == bytes_written);
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
#ifndef BATTERY_CHARGER_I2C
18+
#define BATTERY_CHARGER_I2C
19+
20+
#include "hal/i2c_api.h"
21+
22+
#ifdef __cplusplus
23+
extern"C"{
24+
#endif
25+
26+
class BatteryChargerI2c{
27+
28+
public:
29+
BatteryChargerI2c(PinName sda, PinName scl);
30+
bool read_from_i2c(int i2c_address, char *data_read, int length);
31+
bool write_to_i2c(int i2c_address, const char *data_write, int length);
32+
virtual ~BatteryChargerI2c() {}
33+
34+
private:
35+
i2c_t _i2c;
36+
int _hz;
37+
};
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
#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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#ifndef MIN_BATTERY_VOLTAGE_H
18+
#define MIN_BATTERY_VOLTAGE_H
19+
20+
#ifdef __cplusplus
21+
extern"C"{
22+
#endif
23+
24+
#define BQ24295_I2C_ADDRESS (0x6B << 1)
25+
#define MIN_BATTERY_VOLTAGE_MASK (0x87)
26+
27+
/** Initializes an instance of class BatteryChargerI2c which is using the STM HAL I2C APIs
28+
* This allows longer USB cables or USB ports with lower output voltages to power the board correctly.
29+
*/
30+
31+
void set_minimum_battery_voltage(void);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
37+
#endif // MIN_BATTERY_VOLTAGE_H

0 commit comments

Comments
 (0)