Maxim Integrated / max20303

Dependents:   MAX20303_Wearable_PMIC_Activity_Tracker MAX20303_Wearable_PMIC_Activity_Tracker

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX20303.cpp Source File

MAX20303.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 
00035 #include "MAX20303.h"
00036 
00037 
00038 //******************************************************************************
00039 MAX20303::MAX20303(I2C *i2c):
00040     m_i2c(i2c), m_writeAddress(MAX20303_SLAVE_WR_ADDR),
00041     m_readAddress(MAX20303_SLAVE_RD_ADDR)
00042 {
00043 }
00044 
00045 
00046 //******************************************************************************
00047 MAX20303::~MAX20303(void)
00048 {
00049   //empty block
00050 }
00051 
00052 
00053 //******************************************************************************
00054 int MAX20303::LDO1Config()
00055 {
00056     int32_t ret = 0;
00057     uint8_t val;
00058     appcmdoutvalue_ = 0x40;
00059     appdatainoutbuffer_[0] = 0x05;
00060     appdatainoutbuffer_[1] = 0x34;
00061     AppWrite(2);
00062 
00063     return ret;
00064 }
00065 
00066 //******************************************************************************
00067 int MAX20303::writeReg(registers_t reg, uint8_t value)
00068 {
00069     int32_t ret;
00070 
00071     char cmdData[2] = {reg, value};
00072 
00073     ret = m_i2c->write(m_writeAddress, cmdData, sizeof(cmdData));
00074 
00075     if (ret != 0)
00076         return MAX20303_ERROR;
00077 
00078     return MAX20303_NO_ERROR;
00079 }
00080 
00081 
00082 //******************************************************************************
00083 int MAX20303::readReg(registers_t reg, uint8_t &value)
00084 {
00085     int32_t ret;
00086 
00087     char data = reg;
00088 
00089     ret = m_i2c->write(m_writeAddress, &data, sizeof(data));
00090     if (ret != 0) {
00091         printf("%s - failed - ret: %d\n", __func__);
00092         return MAX20303_ERROR;
00093     }
00094 
00095     ret = m_i2c->read(m_readAddress, &data, sizeof(data));
00096     if (ret != 0) {
00097         printf("%s - failed - ret: %d\n", __func__);
00098         return MAX20303_ERROR;
00099     }
00100 
00101     value = data;
00102     printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret);
00103     return MAX20303_NO_ERROR;
00104 }
00105 
00106 //******************************************************************************
00107 int MAX20303::readRegMulti(registers_t reg, uint8_t *value, uint8_t len){
00108     int32_t ret;
00109     char data = reg;
00110 
00111     ret = m_i2c->write(m_writeAddress, &data, sizeof(data));
00112     if (ret != 0) {
00113         printf("%s - failed - ret: %d\n", __func__);
00114         return MAX20303_ERROR;
00115     }
00116 
00117     ret = m_i2c->read(m_readAddress, (char *)value, len);
00118     if (ret != 0) {
00119         printf("%s - failed - ret: %d\n", __func__);
00120         return MAX20303_ERROR;
00121     }
00122 
00123     printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret);
00124     return MAX20303_NO_ERROR;
00125 }
00126 
00127 //******************************************************************************
00128 int MAX20303::writeRegMulti(registers_t reg, uint8_t *value, uint8_t len){
00129     int32_t ret;
00130     i2cbuffer_[0] = reg;
00131     memcpy(&i2cbuffer_[1], value, len);
00132 
00133     ret = m_i2c->write(m_writeAddress, (char *)i2cbuffer_, (len+1));
00134 
00135     if (ret != 0)
00136         return MAX20303_ERROR;
00137 
00138     return MAX20303_NO_ERROR;
00139 }
00140 //******************************************************************************
00141 int MAX20303::mv2bits(int mV)
00142 {
00143     int regBits;
00144 
00145     if (( MAX20303_LDO_MIN_MV <= mV) && (mV <= MAX20303_LDO_MAX_MV)) {
00146         regBits = (mV -  MAX20303_LDO_MIN_MV) /  MAX20303_LDO_STEP_MV;
00147     } else {
00148         return -1;
00149     }
00150 
00151     return regBits;
00152 }
00153 //******************************************************************************
00154 int MAX20303::PowerOffthePMIC(){
00155     int ret;
00156     appdatainoutbuffer_[0] = 0xB2;
00157     appcmdoutvalue_ = 0x80;
00158     ret = AppWrite(1);
00159 
00160     if(appcmdoutvalue_ != 0x80){
00161         ret |= MAX20303_ERROR;
00162     }
00163 
00164     return ret;
00165 }
00166 //******************************************************************************
00167 int MAX20303::SoftResetthePMIC(){
00168     int ret;
00169     appdatainoutbuffer_[0] = 0xB3;
00170     appcmdoutvalue_ = 0x81;
00171     ret = AppWrite(1);
00172 
00173     if(appcmdoutvalue_ != 0x81){
00174         ret |= MAX20303_ERROR;
00175     }
00176 
00177     return ret;
00178 }
00179 //******************************************************************************
00180 int MAX20303::HardResetthePMIC(){
00181     int ret;
00182     appdatainoutbuffer_[0] = 0xB4;
00183     appcmdoutvalue_ = 0x82;
00184     ret = AppWrite(1);
00185 
00186     if(appcmdoutvalue_ != 0x82){
00187         ret |= MAX20303_ERROR;
00188     }
00189 
00190     return ret;
00191 }
00192 
00193 //******************************************************************************
00194 int MAX20303::AppWrite(uint8_t dataoutlen){
00195     int ret;
00196 
00197     ret = writeRegMulti(MAX20303::REG_AP_DATOUT0, appdatainoutbuffer_, dataoutlen);
00198     ret |= writeReg(MAX20303::REG_AP_CMDOUT, appcmdoutvalue_);
00199     wait_ms(10);
00200     ret |= readReg(MAX20303::REG_AP_RESPONSE, appcmdoutvalue_);
00201 
00202     if(ret != 0)
00203         return MAX20303_ERROR;
00204 
00205     return MAX20303_NO_ERROR;
00206 }
00207 
00208 
00209 //******************************************************************************
00210 int MAX20303::AppRead(uint8_t datainlen){
00211     int ret;
00212 
00213     ret = writeReg(MAX20303::REG_AP_CMDOUT, appcmdoutvalue_);
00214     wait_ms(10);
00215     ret |= readRegMulti(MAX20303::REG_AP_RESPONSE, i2cbuffer_, datainlen);
00216     if(ret != 0)
00217         return MAX20303_ERROR;
00218 
00219     return MAX20303_NO_ERROR;
00220 }
00221 
00222 //******************************************************************************
00223 char MAX20303::CheckPMICHWID(){
00224     int ret;
00225     uint8_t value = 0x00;
00226 
00227     ret = readReg(MAX20303::REG_HARDWARE_ID, value);
00228     if(ret != MAX20303_NO_ERROR)
00229         return false;
00230 
00231     if(value == 0x02)
00232         return true;
00233     else
00234         return false;
00235 }
00236 
00237 //******************************************************************************
00238 int MAX20303::led0on(char enable) {
00239 
00240     if(enable)
00241         return writeReg(REG_LED0_DIRECT, 0x21);
00242     else
00243         return writeReg(REG_LED0_DIRECT, 0x01);
00244 }
00245 
00246 //******************************************************************************
00247 int MAX20303::led1on(char enable) {
00248     if(enable)
00249         return writeReg(REG_LED1_DIRECT, 0x21);
00250     else
00251         return writeReg(REG_LED1_DIRECT, 0x01);
00252 }
00253 
00254 //******************************************************************************
00255 int MAX20303::led2on(char enable) {
00256     if(enable)
00257         return writeReg(REG_LED2_DIRECT, 0x21);
00258     else
00259         return writeReg(REG_LED2_DIRECT, 0x01);
00260 }
00261 
00262 
00263 //******************************************************************************
00264 int MAX20303::BoostEnable(void) {
00265 
00266     writeReg(REG_AP_CMDOUT, 0x30);
00267     writeReg(REG_AP_DATOUT3, 0x00); // 00 : 5V
00268     writeReg(REG_AP_DATOUT0, 0x01); // Boost Enabled
00269 
00270     return MAX20303_NO_ERROR;
00271 }
00272 
00273 //******************************************************************************
00274 int MAX20303::BuckBoostEnable(void)
00275 {
00276     int ret = 0;
00277 
00278     ret |= writeReg( REG_AP_DATOUT0,  0x00);    // Reserved = 0x00
00279     ret |= writeReg( REG_AP_DATOUT1,  0x04);    // BBstlSet = 0b'100   Buck Boost Peak current Limit = 200mA
00280     ret |= writeReg( REG_AP_DATOUT2,  0x19);    // BBstVSet = 0b'11001  Buck Boost Output Voltage = 5V
00281     ret |= writeReg( REG_AP_DATOUT3,  0x01);    // BBstRipRed = 1 Ripple Reduction
00282     ret |= writeReg( REG_AP_CMDOUT, 0x70);
00283     if (ret != 0)
00284         return MAX20303_ERROR;
00285 
00286     return MAX20303_NO_ERROR;
00287 }