Maxim Integrated / max20303_pmic

Fork of max20303 by Maxim Integrated

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