This is a sample library for the MAX77650 incorporated with the MAX32620FTHR development board.
Dependents: Low_Power_Long_Distance_IR_Vision_Robot Low_Power_Long_Distance_IR_Vision_Robot
Fork of max77650_charger_sample by
max77650.cpp@1:324b6205cdd8, 2018-08-14 (annotated)
- Committer:
- fneirab
- Date:
- Tue Aug 14 16:57:16 2018 +0000
- Revision:
- 1:324b6205cdd8
- Parent:
- 0:0bb9daf44568
changes to made public with Charger functions with LED indicator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fneirab | 0:0bb9daf44568 | 1 | /******************************************************************************* |
fneirab | 0:0bb9daf44568 | 2 | * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved. |
fneirab | 0:0bb9daf44568 | 3 | * |
fneirab | 0:0bb9daf44568 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
fneirab | 0:0bb9daf44568 | 5 | * copy of this software and associated documentation files (the "Software"), |
fneirab | 0:0bb9daf44568 | 6 | * to deal in the Software without restriction, including without limitation |
fneirab | 0:0bb9daf44568 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
fneirab | 0:0bb9daf44568 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
fneirab | 0:0bb9daf44568 | 9 | * Software is furnished to do so, subject to the following conditions: |
fneirab | 0:0bb9daf44568 | 10 | * |
fneirab | 0:0bb9daf44568 | 11 | * The above copyright notice and this permission notice shall be included |
fneirab | 0:0bb9daf44568 | 12 | * in all copies or substantial portions of the Software. |
fneirab | 0:0bb9daf44568 | 13 | * |
fneirab | 0:0bb9daf44568 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
fneirab | 0:0bb9daf44568 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
fneirab | 0:0bb9daf44568 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
fneirab | 0:0bb9daf44568 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
fneirab | 0:0bb9daf44568 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
fneirab | 0:0bb9daf44568 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
fneirab | 0:0bb9daf44568 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
fneirab | 0:0bb9daf44568 | 21 | * |
fneirab | 0:0bb9daf44568 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
fneirab | 0:0bb9daf44568 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
fneirab | 0:0bb9daf44568 | 24 | * Products, Inc. Branding Policy. |
fneirab | 0:0bb9daf44568 | 25 | * |
fneirab | 0:0bb9daf44568 | 26 | * The mere transfer of this software does not imply any licenses |
fneirab | 0:0bb9daf44568 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
fneirab | 0:0bb9daf44568 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
fneirab | 0:0bb9daf44568 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
fneirab | 0:0bb9daf44568 | 30 | * ownership rights. |
fneirab | 0:0bb9daf44568 | 31 | ******************************************************************************* |
fneirab | 0:0bb9daf44568 | 32 | */ |
fneirab | 0:0bb9daf44568 | 33 | |
fneirab | 0:0bb9daf44568 | 34 | #include "max77650.h" |
fneirab | 0:0bb9daf44568 | 35 | |
fneirab | 0:0bb9daf44568 | 36 | /* LIBRARY FUNCTION SUCCESS*/ |
fneirab | 0:0bb9daf44568 | 37 | #define F_SUCCESS_0 0 |
fneirab | 0:0bb9daf44568 | 38 | |
fneirab | 0:0bb9daf44568 | 39 | /* LIBRARY FUNCTION ERROR CODES */ |
fneirab | 0:0bb9daf44568 | 40 | #define F_ERROR_1 -1 //-1 if read/write errors exist |
fneirab | 0:0bb9daf44568 | 41 | |
fneirab | 0:0bb9daf44568 | 42 | |
fneirab | 0:0bb9daf44568 | 43 | MAX77650::MAX77650(I2C &i2c) : |
fneirab | 0:0bb9daf44568 | 44 | m_i2cBus(i2c) |
fneirab | 0:0bb9daf44568 | 45 | { |
fneirab | 0:0bb9daf44568 | 46 | |
fneirab | 0:0bb9daf44568 | 47 | } |
fneirab | 0:0bb9daf44568 | 48 | |
fneirab | 0:0bb9daf44568 | 49 | MAX77650::~MAX77650() |
fneirab | 0:0bb9daf44568 | 50 | { |
fneirab | 0:0bb9daf44568 | 51 | |
fneirab | 0:0bb9daf44568 | 52 | } |
fneirab | 0:0bb9daf44568 | 53 | |
fneirab | 0:0bb9daf44568 | 54 | |
fneirab | 0:0bb9daf44568 | 55 | int MAX77650::writeReg(registers_t reg_addr, uint8_t reg_data) |
fneirab | 0:0bb9daf44568 | 56 | { |
fneirab | 0:0bb9daf44568 | 57 | |
fneirab | 0:0bb9daf44568 | 58 | char addr_plus_data[2] = {reg_addr, reg_data}; |
fneirab | 0:0bb9daf44568 | 59 | |
fneirab | 0:0bb9daf44568 | 60 | if ( m_i2cBus.write(I2C_W_ADRS, addr_plus_data, 2, false) == F_SUCCESS_0) |
fneirab | 0:0bb9daf44568 | 61 | return F_SUCCESS_0; |
fneirab | 0:0bb9daf44568 | 62 | else |
fneirab | 0:0bb9daf44568 | 63 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 64 | |
fneirab | 0:0bb9daf44568 | 65 | } |
fneirab | 0:0bb9daf44568 | 66 | |
fneirab | 0:0bb9daf44568 | 67 | |
fneirab | 0:0bb9daf44568 | 68 | int MAX77650::readReg(registers_t reg_addr, uint8_t &value) |
fneirab | 0:0bb9daf44568 | 69 | { |
fneirab | 0:0bb9daf44568 | 70 | int result; |
fneirab | 0:0bb9daf44568 | 71 | char local_data[1]; |
fneirab | 0:0bb9daf44568 | 72 | local_data[0] = reg_addr; |
fneirab | 0:0bb9daf44568 | 73 | char read_data[1]; |
fneirab | 0:0bb9daf44568 | 74 | |
fneirab | 0:0bb9daf44568 | 75 | result = m_i2cBus.write(I2C_W_ADRS, local_data, 1); |
fneirab | 0:0bb9daf44568 | 76 | if(result == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 77 | result = m_i2cBus.read(I2C_R_ADRS, read_data , 1, false); |
fneirab | 0:0bb9daf44568 | 78 | if (result == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 79 | value = read_data[0]; |
fneirab | 0:0bb9daf44568 | 80 | result = F_SUCCESS_0; |
fneirab | 0:0bb9daf44568 | 81 | } |
fneirab | 0:0bb9daf44568 | 82 | } |
fneirab | 0:0bb9daf44568 | 83 | |
fneirab | 0:0bb9daf44568 | 84 | return result; |
fneirab | 0:0bb9daf44568 | 85 | |
fneirab | 0:0bb9daf44568 | 86 | } |
fneirab | 0:0bb9daf44568 | 87 | |
fneirab | 0:0bb9daf44568 | 88 | |
fneirab | 0:0bb9daf44568 | 89 | /** |
fneirab | 0:0bb9daf44568 | 90 | * @brief Initialization of Chager for the MAX77650 PMIC |
fneirab | 0:0bb9daf44568 | 91 | * @details Sets the parameters for the charger. |
fneirab | 0:0bb9daf44568 | 92 | * @return status |
fneirab | 0:0bb9daf44568 | 93 | */ |
fneirab | 0:0bb9daf44568 | 94 | int MAX77650::initCharger() //Only for Mobility Demo BU |
fneirab | 0:0bb9daf44568 | 95 | { |
fneirab | 0:0bb9daf44568 | 96 | // Set the charge current to 52.5mA: |
fneirab | 0:0bb9daf44568 | 97 | // i2c write -> write 0x19 to register 0x1C |
fneirab | 0:0bb9daf44568 | 98 | int status; |
fneirab | 0:0bb9daf44568 | 99 | |
fneirab | 0:0bb9daf44568 | 100 | status = writeReg(CNFG_CHG_E, 0X19); |
fneirab | 0:0bb9daf44568 | 101 | |
fneirab | 0:0bb9daf44568 | 102 | if (status == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 103 | // Set the charge voltage to 4.2V: |
fneirab | 0:0bb9daf44568 | 104 | // i2c write -> write 0x60 to register 0x1E |
fneirab | 0:0bb9daf44568 | 105 | status = writeReg(CNFG_CHG_G, 0X60); |
fneirab | 0:0bb9daf44568 | 106 | if (status == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 107 | status = writeReg(INT_M_CHG, 0XFB); |
fneirab | 0:0bb9daf44568 | 108 | if (status == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 109 | return F_SUCCESS_0; |
fneirab | 0:0bb9daf44568 | 110 | } |
fneirab | 0:0bb9daf44568 | 111 | } |
fneirab | 0:0bb9daf44568 | 112 | } else |
fneirab | 0:0bb9daf44568 | 113 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 114 | |
fneirab | 0:0bb9daf44568 | 115 | return status; |
fneirab | 0:0bb9daf44568 | 116 | |
fneirab | 0:0bb9daf44568 | 117 | } |
fneirab | 0:0bb9daf44568 | 118 | |
fneirab | 0:0bb9daf44568 | 119 | |
fneirab | 0:0bb9daf44568 | 120 | int MAX77650::enCharger() //Only for Mobility Demo BU |
fneirab | 0:0bb9daf44568 | 121 | { |
fneirab | 0:0bb9daf44568 | 122 | //You can set the charge current and charge voltage as soon |
fneirab | 0:0bb9daf44568 | 123 | //as MAX77650 gets up and running. |
fneirab | 0:0bb9daf44568 | 124 | //Then, you can just disable/enable the charger whenever you like to have it charge the battery. |
fneirab | 0:0bb9daf44568 | 125 | // Enable or disable the charger: |
fneirab | 0:0bb9daf44568 | 126 | // i2c write -> write 0x01 to register 0x19 (enables the charger) |
fneirab | 0:0bb9daf44568 | 127 | int status; |
fneirab | 0:0bb9daf44568 | 128 | //Incorporated some of the LEDS driven by the PMIC to indicate chargering status |
fneirab | 0:0bb9daf44568 | 129 | uint8_t reg, temp, mMASK = 3, pmicLED2_ON = 0x44, pmicLED2_OFF = 0x04, p1sDS50 = 0x17; |
fneirab | 0:0bb9daf44568 | 130 | |
fneirab | 0:0bb9daf44568 | 131 | status = readReg(STAT_CHG_B, reg); |
fneirab | 0:0bb9daf44568 | 132 | if (status == F_ERROR_1) |
fneirab | 0:0bb9daf44568 | 133 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 134 | temp = reg>>2; |
fneirab | 0:0bb9daf44568 | 135 | temp = temp & mMASK; |
fneirab | 0:0bb9daf44568 | 136 | |
fneirab | 0:0bb9daf44568 | 137 | if (temp == 3) { |
fneirab | 0:0bb9daf44568 | 138 | status = writeReg(CNFG_CHG_B, 0X01); |
fneirab | 0:0bb9daf44568 | 139 | readReg(CNFG_CHG_B, reg); |
fneirab | 0:0bb9daf44568 | 140 | readReg(STAT_CHG_B, reg); |
fneirab | 0:0bb9daf44568 | 141 | if (status == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 142 | writeReg(CNFG_LED2_B,p1sDS50); |
fneirab | 0:0bb9daf44568 | 143 | writeReg(CNFG_LED2_A,pmicLED2_ON); |
fneirab | 0:0bb9daf44568 | 144 | return F_SUCCESS_0; |
fneirab | 0:0bb9daf44568 | 145 | } |
fneirab | 0:0bb9daf44568 | 146 | } else if (temp == 0) { |
fneirab | 0:0bb9daf44568 | 147 | |
fneirab | 0:0bb9daf44568 | 148 | status = writeReg(CNFG_CHG_B, 0X00); |
fneirab | 0:0bb9daf44568 | 149 | if (status == F_SUCCESS_0) { |
fneirab | 0:0bb9daf44568 | 150 | writeReg(CNFG_LED2_A,pmicLED2_OFF); |
fneirab | 0:0bb9daf44568 | 151 | return F_SUCCESS_0; |
fneirab | 0:0bb9daf44568 | 152 | } |
fneirab | 0:0bb9daf44568 | 153 | |
fneirab | 0:0bb9daf44568 | 154 | } |
fneirab | 0:0bb9daf44568 | 155 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 156 | |
fneirab | 0:0bb9daf44568 | 157 | } |
fneirab | 0:0bb9daf44568 | 158 | |
fneirab | 0:0bb9daf44568 | 159 | /** |
fneirab | 0:0bb9daf44568 | 160 | * @brief Disable Charger Function |
fneirab | 0:0bb9daf44568 | 161 | * @details Sets the parameters for the charger. |
fneirab | 0:0bb9daf44568 | 162 | * |
fneirab | 0:0bb9daf44568 | 163 | * @parameters reg_addr Register to read |
fneirab | 0:0bb9daf44568 | 164 | */ |
fneirab | 0:0bb9daf44568 | 165 | int MAX77650::disblCharger() //Only for Mobility Demo BU |
fneirab | 0:0bb9daf44568 | 166 | { |
fneirab | 0:0bb9daf44568 | 167 | //You can set the charge current and charge voltage as soon |
fneirab | 0:0bb9daf44568 | 168 | //as MAX77650 gets up and running. |
fneirab | 0:0bb9daf44568 | 169 | //Then, you can just disable/enable the charger whenever you like to have it charge the battery. |
fneirab | 0:0bb9daf44568 | 170 | // Enable or disable the charger: |
fneirab | 0:0bb9daf44568 | 171 | // i2c write -> write 0x00 to register 0x19 (disables the charger) |
fneirab | 0:0bb9daf44568 | 172 | |
fneirab | 0:0bb9daf44568 | 173 | int status; |
fneirab | 0:0bb9daf44568 | 174 | uint8_t reg, temp, mMASK = 3; |
fneirab | 0:0bb9daf44568 | 175 | status = readReg(STAT_CHG_B, reg); |
fneirab | 0:0bb9daf44568 | 176 | if (status == F_ERROR_1) |
fneirab | 0:0bb9daf44568 | 177 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 178 | temp = reg>>2; |
fneirab | 0:0bb9daf44568 | 179 | temp = temp & mMASK; |
fneirab | 0:0bb9daf44568 | 180 | |
fneirab | 0:0bb9daf44568 | 181 | if (temp == 0) { |
fneirab | 0:0bb9daf44568 | 182 | |
fneirab | 0:0bb9daf44568 | 183 | status = writeReg(CNFG_CHG_B, 0X00); |
fneirab | 0:0bb9daf44568 | 184 | if (status == F_ERROR_1) { |
fneirab | 0:0bb9daf44568 | 185 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 186 | } |
fneirab | 0:0bb9daf44568 | 187 | return F_SUCCESS_0; |
fneirab | 0:0bb9daf44568 | 188 | } |
fneirab | 0:0bb9daf44568 | 189 | return F_ERROR_1; |
fneirab | 0:0bb9daf44568 | 190 | |
fneirab | 0:0bb9daf44568 | 191 | } |
fneirab | 0:0bb9daf44568 | 192 | |
fneirab | 0:0bb9daf44568 | 193 |