Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 * Copyright (c) 2006-2017 ARM Limited
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 5 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 6 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 7 *
lypinator 0:bb348c97df44 8 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 9 *
lypinator 0:bb348c97df44 10 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 11 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 13 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 14 * limitations under the License.
lypinator 0:bb348c97df44 15 */
lypinator 0:bb348c97df44 16
lypinator 0:bb348c97df44 17 #include "min_battery_voltage.h"
lypinator 0:bb348c97df44 18
lypinator 0:bb348c97df44 19 /** Defining HAL_MspInit strong function
lypinator 0:bb348c97df44 20 * in user defined file as described in documentation
lypinator 0:bb348c97df44 21 */
lypinator 0:bb348c97df44 22
lypinator 0:bb348c97df44 23 void HAL_MspInit(void)
lypinator 0:bb348c97df44 24 {
lypinator 0:bb348c97df44 25 set_minimum_battery_voltage();
lypinator 0:bb348c97df44 26 }
lypinator 0:bb348c97df44 27
lypinator 0:bb348c97df44 28 void set_minimum_battery_voltage()
lypinator 0:bb348c97df44 29 {
lypinator 0:bb348c97df44 30 i2c_t i2c_obj = {0};
lypinator 0:bb348c97df44 31 int data_read;
lypinator 0:bb348c97df44 32 i2c_init(&i2c_obj, I2C_SDA_B, I2C_SCL_B);
lypinator 0:bb348c97df44 33 i2c_frequency(&i2c_obj, I2C_FREQUENCY);
lypinator 0:bb348c97df44 34
lypinator 0:bb348c97df44 35 if (read_from_i2c(BQ24295_I2C_ADDRESS, 0, &data_read, i2c_obj)) {
lypinator 0:bb348c97df44 36 data_read = data_read & MIN_BATTERY_VOLTAGE_MASK;
lypinator 0:bb348c97df44 37 write_to_i2c(BQ24295_I2C_ADDRESS, 0, data_read, i2c_obj);
lypinator 0:bb348c97df44 38 //Battery Voltage is set to 3880mV
lypinator 0:bb348c97df44 39 } else {
lypinator 0:bb348c97df44 40 // Minimum battery voltage could not be set. This is not a critical error, no need to stop execution
lypinator 0:bb348c97df44 41 // It simply means that longer cabling or USB ports with lower output voltages may cause problems.
lypinator 0:bb348c97df44 42 }
lypinator 0:bb348c97df44 43 }
lypinator 0:bb348c97df44 44
lypinator 0:bb348c97df44 45 char write_to_i2c(int slave_addr, int reg_addr, int data_write, i2c_t i2c_obj)
lypinator 0:bb348c97df44 46 {
lypinator 0:bb348c97df44 47 char ret_code = 0;
lypinator 0:bb348c97df44 48 if (!i2c_start(&i2c_obj)) {
lypinator 0:bb348c97df44 49 if ((i2c_byte_write(&i2c_obj, slave_addr << 1) == 1) &&
lypinator 0:bb348c97df44 50 (i2c_byte_write(&i2c_obj, reg_addr) == 1) &&
lypinator 0:bb348c97df44 51 (i2c_byte_write(&i2c_obj,data_write) ==1)) {
lypinator 0:bb348c97df44 52 ret_code = 1;
lypinator 0:bb348c97df44 53 }
lypinator 0:bb348c97df44 54 i2c_stop(&i2c_obj);
lypinator 0:bb348c97df44 55 }
lypinator 0:bb348c97df44 56 return ret_code;
lypinator 0:bb348c97df44 57 }
lypinator 0:bb348c97df44 58
lypinator 0:bb348c97df44 59 char read_from_i2c(int slave_addr, int reg_addr, int* data_read, i2c_t i2c_obj)
lypinator 0:bb348c97df44 60 {
lypinator 0:bb348c97df44 61 char ret_code = 0;
lypinator 0:bb348c97df44 62 if (!i2c_start(&i2c_obj)) {
lypinator 0:bb348c97df44 63 if ((i2c_byte_write(&i2c_obj,(slave_addr << 1))==1) &&
lypinator 0:bb348c97df44 64 (i2c_byte_write(&i2c_obj, reg_addr)==1) &&
lypinator 0:bb348c97df44 65 (!i2c_start(&i2c_obj)) &&
lypinator 0:bb348c97df44 66 (i2c_byte_write(&i2c_obj, ((slave_addr << 1) | 0x01)) == 1)) {
lypinator 0:bb348c97df44 67 *data_read = i2c_byte_read(&i2c_obj,1);
lypinator 0:bb348c97df44 68 ret_code = 1;
lypinator 0:bb348c97df44 69 }
lypinator 0:bb348c97df44 70 i2c_stop(&i2c_obj);
lypinator 0:bb348c97df44 71 }
lypinator 0:bb348c97df44 72 return ret_code;
lypinator 0:bb348c97df44 73 }