Skip to content

Commit 105da03

Browse files
authored
Merge pull request #6117 from u-blox/i2c_read_write
UBLOX C030: I2C avoid any RTOS waits
2 parents acd1895 + 8ebb234 commit 105da03

File tree

5 files changed

+79
-126
lines changed

5 files changed

+79
-126
lines changed

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/battery_charger_i2c.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/battery_charger_i2c.h

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
19+
/** Defining HAL_MspInit strong function
20+
* in user defined file as described in documentation
21+
*/
22+
23+
void HAL_MspInit(void)
24+
{
25+
set_minimum_battery_voltage();
26+
}
27+
28+
void set_minimum_battery_voltage()
29+
{
30+
i2c_t i2c_obj;
31+
int data_read;
32+
i2c_frequency(&i2c_obj, I2C_FREQUENCY);
33+
i2c_init(&i2c_obj, I2C_SDA_B, I2C_SCL_B);
34+
35+
if (read_from_i2c(BQ24295_I2C_ADDRESS, 0, &data_read, i2c_obj)) {
36+
data_read = data_read & MIN_BATTERY_VOLTAGE_MASK;
37+
write_to_i2c(BQ24295_I2C_ADDRESS, 0, data_read, i2c_obj);
38+
//Battery Voltage is set to 3880mV
39+
} else {
40+
// Minimum battery voltage could not be set. This is not a critical error, no need to stop execution
41+
// It simply means that longer cabling or USB ports with lower output voltages may cause problems.
42+
}
43+
}
44+
45+
char write_to_i2c(int slave_addr, int reg_addr, int data_write, i2c_t i2c_obj)
46+
{
47+
char ret_code = 0;
48+
if (!i2c_start(&i2c_obj)) {
49+
if ((i2c_byte_write(&i2c_obj, slave_addr << 1) == 1) &&
50+
(i2c_byte_write(&i2c_obj, reg_addr) == 1) &&
51+
(i2c_byte_write(&i2c_obj,data_write) ==1)) {
52+
ret_code = 1;
53+
}
54+
i2c_stop(&i2c_obj);
55+
}
56+
return ret_code;
57+
}
58+
59+
char read_from_i2c(int slave_addr, int reg_addr, int* data_read, i2c_t i2c_obj)
60+
{
61+
char ret_code = 0;
62+
if (!i2c_start(&i2c_obj)) {
63+
if ((i2c_byte_write(&i2c_obj,(slave_addr << 1))==1) &&
64+
(i2c_byte_write(&i2c_obj, reg_addr)==1) &&
65+
(!i2c_start(&i2c_obj)) &&
66+
(i2c_byte_write(&i2c_obj, ((slave_addr << 1) | 0x01)) == 1)) {
67+
*data_read = i2c_byte_read(&i2c_obj,1);
68+
ret_code = 1;
69+
}
70+
i2c_stop(&i2c_obj);
71+
}
72+
return ret_code;
73+
}

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/min_battery_voltage.cpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/min_battery_voltage.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
#ifndef MIN_BATTERY_VOLTAGE_H
1818
#define MIN_BATTERY_VOLTAGE_H
1919

20+
#include "hal/i2c_api.h"
21+
2022
#ifdef __cplusplus
2123
extern"C"{
2224
#endif
2325

24-
#define BQ24295_I2C_ADDRESS (0x6B << 1)
26+
#define BQ24295_I2C_ADDRESS (0x6B)
27+
#define I2C_FREQUENCY 100000
2528
#define MIN_BATTERY_VOLTAGE_MASK (0x87)
2629

2730
/** Initializes an instance of class BatteryChargerI2c which is using the STM HAL I2C APIs
2831
* This allows longer USB cables or USB ports with lower output voltages to power the board correctly.
2932
*/
3033

3134
void set_minimum_battery_voltage(void);
35+
char read_from_i2c(int slave_addr, int reg_addr, int* data_read, i2c_t i2c_obj);
36+
char write_to_i2c(int slave_addr, int reg_addr,int data_write, i2c_t i2c_obj);
3237

3338
#ifdef __cplusplus
3439
}

0 commit comments

Comments
 (0)