Maxim Integrated / max77650_charger_sample

Dependents:   Low_Power_Long_Distance_IR_Vision_Robot Low_Power_Long_Distance_IR_Vision_Robot

Fork of max77650_charger_sample by Maxim Integrated

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers max77650.cpp Source File

max77650.cpp

00001 /*******************************************************************************
00002 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************
00032 */
00033 
00034 #include "max77650.h"
00035 
00036 /* LIBRARY FUNCTION SUCCESS*/
00037 #define F_SUCCESS_0  0
00038 
00039 /* LIBRARY FUNCTION ERROR CODES */
00040 #define F_ERROR_1 -1    //-1 if read/write errors exist         
00041 
00042 
00043 MAX77650::MAX77650(I2C &i2c) :
00044     m_i2cBus(i2c)
00045 {
00046 
00047 }
00048 
00049 MAX77650::~MAX77650()
00050 {
00051 
00052 }
00053 
00054 
00055 int MAX77650::writeReg(registers_t reg_addr, uint8_t reg_data)
00056 {
00057 
00058     char addr_plus_data[2] = {reg_addr, reg_data};
00059 
00060     if ( m_i2cBus.write(I2C_W_ADRS, addr_plus_data, 2, false) == F_SUCCESS_0)
00061         return F_SUCCESS_0;
00062     else
00063         return F_ERROR_1;
00064 
00065 }
00066 
00067 
00068 int MAX77650::readReg(registers_t reg_addr, uint8_t &value)
00069 {
00070     int result;
00071     char local_data[1];
00072     local_data[0] = reg_addr;
00073     char read_data[1];
00074 
00075     result = m_i2cBus.write(I2C_W_ADRS, local_data, 1);
00076     if(result == F_SUCCESS_0) {
00077         result = m_i2cBus.read(I2C_R_ADRS, read_data , 1, false);
00078         if (result == F_SUCCESS_0) {
00079             value =  read_data[0];
00080             result = F_SUCCESS_0;
00081         }
00082     }
00083 
00084     return result;
00085 
00086 }
00087 
00088 
00089 /**
00090  * @brief      Initialization of Chager for the MAX77650 PMIC
00091  * @details    Sets the parameters for the charger.
00092  * @return     status
00093  */
00094 int MAX77650::initCharger() //Only for Mobility Demo BU
00095 {
00096 // Set the charge current to 52.5mA:
00097 // i2c write -> write 0x19 to register 0x1C
00098     int status;
00099 
00100     status = writeReg(CNFG_CHG_E, 0X19);
00101 
00102     if (status == F_SUCCESS_0) {
00103         // Set the charge voltage to 4.2V:
00104         // i2c write -> write 0x60 to register 0x1E
00105         status = writeReg(CNFG_CHG_G, 0X60);
00106         if (status == F_SUCCESS_0) {
00107             status = writeReg(INT_M_CHG, 0XFB);
00108             if (status == F_SUCCESS_0) {
00109                 return F_SUCCESS_0;
00110             }
00111         }
00112     } else
00113         return F_ERROR_1;
00114 
00115     return status;
00116 
00117 }
00118 
00119 
00120 int MAX77650::enCharger() //Only for Mobility Demo BU
00121 {
00122 //You can set the charge current and charge voltage as soon
00123 //as MAX77650 gets up and running.
00124 //Then, you can just disable/enable the charger whenever you like to have it charge the battery.
00125 // Enable or disable the charger:
00126 // i2c write -> write 0x01 to register 0x19 (enables the charger)
00127     int status;
00128     //Incorporated some of the LEDS driven by the PMIC to indicate chargering status
00129     uint8_t reg, temp, mMASK = 3, pmicLED2_ON = 0x44, pmicLED2_OFF = 0x04, p1sDS50 = 0x17;
00130     
00131     status = readReg(STAT_CHG_B, reg);
00132     if (status == F_ERROR_1)
00133         return F_ERROR_1;
00134     temp = reg>>2;
00135     temp = temp & mMASK;
00136 
00137     if (temp == 3) {
00138         status = writeReg(CNFG_CHG_B, 0X01);
00139         readReg(CNFG_CHG_B, reg);
00140         readReg(STAT_CHG_B, reg);
00141         if (status == F_SUCCESS_0) {
00142             writeReg(CNFG_LED2_B,p1sDS50);
00143             writeReg(CNFG_LED2_A,pmicLED2_ON);
00144             return F_SUCCESS_0;
00145         }
00146     } else if (temp == 0) {
00147 
00148         status = writeReg(CNFG_CHG_B, 0X00);
00149         if (status == F_SUCCESS_0) {
00150             writeReg(CNFG_LED2_A,pmicLED2_OFF);
00151             return F_SUCCESS_0;
00152         }
00153 
00154     }
00155     return F_ERROR_1;
00156 
00157 }
00158 
00159 /**
00160  * @brief       Disable Charger Function
00161  * @details     Sets the parameters for the charger.
00162  *
00163  * @parameters  reg_addr Register to read
00164  */
00165 int MAX77650::disblCharger() //Only for Mobility Demo BU
00166 {
00167 //You can set the charge current and charge voltage as soon
00168 //as MAX77650 gets up and running.
00169 //Then, you can just disable/enable the charger whenever you like to have it charge the battery.
00170 // Enable or disable the charger:
00171 // i2c write -> write 0x00 to register 0x19 (disables the charger)
00172 
00173     int status;
00174     uint8_t reg, temp, mMASK = 3;
00175     status = readReg(STAT_CHG_B, reg);
00176     if (status == F_ERROR_1)
00177         return F_ERROR_1;
00178     temp = reg>>2;
00179     temp = temp & mMASK;
00180 
00181     if (temp == 0) {
00182 
00183         status = writeReg(CNFG_CHG_B, 0X00);
00184         if (status == F_ERROR_1) {
00185             return F_ERROR_1;
00186         }
00187         return F_SUCCESS_0;
00188     }
00189     return F_ERROR_1;
00190 
00191 }
00192 
00193