|
| 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 | +} |
0 commit comments