MAX14720 Library for MAX32620HSP (MAXREFDES100#)

Dependents:   HSP_PMIC_Demo HSP_SpO2 HSP_ECG HSP_ECG_LeadOFF_Detection

Fork of MAX14720 by Greg Steiert

Files at this revision

API Documentation at this revision

Comitter:
jessexm
Date:
Wed Oct 19 22:32:56 2016 +0000
Parent:
7:2e9f6e70b34f
Commit message:
Code review updates

Changed in this revision

MAX14720.cpp Show annotated file Show diff for this revision Revisions of this file
MAX14720.h Show annotated file Show diff for this revision Revisions of this file
diff -r 2e9f6e70b34f -r 2c3f2da51c5d MAX14720.cpp
--- a/MAX14720.cpp	Thu Oct 06 21:33:25 2016 +0000
+++ b/MAX14720.cpp	Wed Oct 19 22:32:56 2016 +0000
@@ -89,6 +89,7 @@
     buckActDsc = false;
     buckFScl = false;
 }
+
 //******************************************************************************
 MAX14720::~MAX14720()
 {
@@ -100,116 +101,127 @@
 //******************************************************************************
 int MAX14720::boostSetMode(boostEn_t mode)
 {
-    int result;
     char data;
+
     boostEn = mode;
     data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
-    result = writeReg(REG_BOOST_CFG, data);
-    if (result == MAX14720_ERROR) return result;
-    return 0;
+    return writeReg(REG_BOOST_CFG, data);
 }
 
 //******************************************************************************
 int MAX14720::boostSetVoltage(int mV)
 {
-    int result;
     char data;
-    if ((MAX14720_BOOST_MIN_MV <= mV)&&(mV <= MAX14720_BOOST_MAX_MV)) {
+
+    if ((MAX14720_BOOST_MIN_MV <= mV) && (mV <= MAX14720_BOOST_MAX_MV)) {
         boostMillivolts = mV;
         data = (mV - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV;
     } else {
         return MAX14720_ERROR;
     }
+
     if (boostEn == BOOST_ENABLED) {
-        result = writeReg(REG_BOOST_CFG, 0x00);
-        if (result == MAX14720_ERROR) return result;
+        if (writeReg(REG_BOOST_CFG, 0x00) != MAX14720_NO_ERROR) {
+            return MAX14720_ERROR;
+        }
     }
-    result = writeReg(REG_BOOST_VSET, data);
-    if (result == MAX14720_ERROR) return result;
+
+    if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) {
+        return MAX14720_ERROR;
+    }
+
     if (boostEn == BOOST_ENABLED) {
         data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
-        result = writeReg(REG_BOOST_CFG, data);
-        if (result == MAX14720_ERROR) return result;
+        if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) {
+            return MAX14720_ERROR;
+        }
     }
-    return 0;
+
+    return MAX14720_NO_ERROR;
 }
 
 //******************************************************************************
 int MAX14720::init()
 {
-    int result;
     char data;
+
     data = (clkDivEn << 7) | (clkDivSet);
-    result = writeReg(REG_BOOST_CDIV, data);
-    if (result == MAX14720_ERROR) return result;
+    if (writeReg(REG_BOOST_CDIV, data) != MAX14720_NO_ERROR) {
+        return MAX14720_ERROR;
+    }
+
     data = (boostISet);
-    result = writeReg(REG_BOOST_ISET, data);
-    if (result == MAX14720_ERROR) return result;
-    if ((MAX14720_BOOST_MIN_MV <= boostMillivolts)&&(boostMillivolts <= MAX14720_BOOST_MAX_MV)) {
+    if (writeReg(REG_BOOST_ISET, data) != MAX14720_NO_ERROR) {
+        return MAX14720_ERROR;
+    }
+
+    if ((MAX14720_BOOST_MIN_MV <= boostMillivolts) &&
+        (boostMillivolts <= MAX14720_BOOST_MAX_MV)) {
         data = (boostMillivolts - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV;
+        if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) {
+            return MAX14720_ERROR;
+        }
     } else {
         return MAX14720_ERROR;
     }
-    result = writeReg(REG_BOOST_VSET, data);
-    if (result == MAX14720_ERROR) return result;
+
     data = (buckMd << 1) | (buckFst);
-    result = writeReg(REG_BUCK_CFG, data);
-    if (result == MAX14720_ERROR) return result;
-    data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) | (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl);
-    result = writeReg(REG_BBB_EXTRA, data);
-    if (result == MAX14720_ERROR) return result;
+    if (writeReg(REG_BUCK_CFG, data) != MAX14720_NO_ERROR) {
+        return MAX14720_ERROR;
+    }
+
+    data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) |
+           (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl);
+    if (writeReg(REG_BBB_EXTRA, data) != MAX14720_NO_ERROR) {
+        return MAX14720_ERROR;
+    }
+
     // Write Boost Enable Register Last
     data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
-    result = writeReg(REG_BOOST_CFG, data);
-    if (result == MAX14720_ERROR) return result;
-    return 0;
+    if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) {
+        return MAX14720_ERROR;
+    }
+
+    return MAX14720_NO_ERROR;
 }
 
 //******************************************************************************
 int MAX14720::monSet(monCfg_t monCfg)
 {
-    int result;
-    result = writeReg(REG_MON_CFG, monCfg);
-    if (result == MAX14720_ERROR) return result;
-    return 0;
+    return writeReg(REG_MON_CFG, monCfg);
 }
 
 //******************************************************************************
 int MAX14720::shutdown()
 {
-    int result;
-    result = writeReg(REG_PWR_OFF, 0xB2);
-    if (result == MAX14720_ERROR) return result;
-    return 0;
+    return writeReg(REG_PWR_OFF, 0xB2);
 }
 
 
 //******************************************************************************
 int MAX14720::writeReg(registers_t reg,  char value)
 {
-    int result;
     char cmdData[2] = { (char)reg, value };
-    result = i2c->write(slaveAddress, cmdData, 2);
-    if (result != 0) return MAX14720_ERROR;
+
+    if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
+        return MAX14720_ERROR;
+    }
+
     return MAX14720_NO_ERROR;
 }
 
 //******************************************************************************
 int MAX14720::readReg(registers_t reg, char *value)
 {
-    int result;
     char cmdData[1] = { (char)reg };
 
-    result = i2c->write(slaveAddress, cmdData, 1);
-    if (result != 0) return MAX14720_ERROR;
-    result = i2c->read(slaveAddress, value, 1);
-    if (result != 0) return MAX14720_ERROR;
+    if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
+        return MAX14720_ERROR;
+    }
+
+    if (i2c->read(slaveAddress, value, 1) != 0) {
+        return MAX14720_ERROR;
+    }
+
     return MAX14720_NO_ERROR;
 }
-
-
-
-
-
-
-
diff -r 2e9f6e70b34f -r 2c3f2da51c5d MAX14720.h
--- a/MAX14720.h	Thu Oct 06 21:33:25 2016 +0000
+++ b/MAX14720.h	Wed Oct 19 22:32:56 2016 +0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
+ * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -44,7 +44,13 @@
 #define MAX14720_BOOST_STEP_MV 100
 
 /**
- * MAX14720 PMIC Driver
+ * @brief MAX14720 Power-Management Solution Driver
+ *
+ * @details The MAX14720/MAX14750 are compact power-management solutions for
+ * space-constrained, battery-powered applications where size and efficiency are
+ * critical. Both devices integrate a power switch, a linear regulator, a buck
+ * regulator, and a buck-boost regulator.
+ * <br>https://www.maximintegrated.com/en/products/power/battery-management/MAX14720.html
  *
  * @code
  * #include "mbed.h"
@@ -54,9 +60,9 @@
  * I2C i2c2(I2C2_SDA, I2C2_SCL);
  *
  * #define  I2C_ADDR_PMIC   (0x54)
- * MAX14720 max14720(&i2c2,I2C_ADDR_PMIC);
+ * MAX14720 max14720(&i2c2, I2C_ADDR_PMIC);
  *
- * DigitalOut led(LED1);
+ * DigitalOut led(LED1, 0);
  * InterruptIn button(SW1);
  *
  * void turnOff()
@@ -66,13 +72,15 @@
  *
  * int main()
  * {
- *     int result;
  *     button.fall(&turnOff);
- *     led = 0;
+ *
  *     max14720.boostEn = MAX14720::BOOST_ENABLED;
- *     result = max14720.init();
- *     if (result == MAX14720_ERROR) printf("Error initializing MAX14720");
+ *     if (max14720.init() == MAX14720_ERROR) {
+ *         printf("Error initializing MAX14720");
+ *     }
+ *
  *     wait(1);
+ *
  *     while(1) {
  *         max14720.boostSetMode(MAX14720::BOOST_DISABLED);
  *         max14720.boostEn = MAX14720::BOOST_ENABLED;
@@ -87,7 +95,6 @@
  */
 class MAX14720
 {
-
 public:
 
     /**
@@ -96,28 +103,28 @@
      */
     typedef enum {
         REG_CHIP_ID = 0x00,		///< Chip ID
-        REG_CHIP_REV = 0x01,		///< Chip Revision
-        REG_BOOST_CDIV = 0x03,		///< Boost Clock Divider
-        REG_BOOST_ISET = 0x04,		///< Boost Peak Current
-        REG_BOOST_VSET = 0x05,		///< Boost Voltage Setting
-        REG_BOOST_CFG = 0x06,		///< Boost Configuration
-        REG_BUCK_VSET = 0x07,		///< Buck Voltage Setting
-        REG_BUCK_CFG = 0x08,		///< Buck Configuration
-        REG_BUCK_ISET = 0x09,		///< Buck Peak Current and Settings
-        REG_LDO_VSET = 0x0A,		///< LDO Voltage Setting
+        REG_CHIP_REV = 0x01,	///< Chip Revision
+        REG_BOOST_CDIV = 0x03,	///< Boost Clock Divider
+        REG_BOOST_ISET = 0x04,	///< Boost Peak Current
+        REG_BOOST_VSET = 0x05,	///< Boost Voltage Setting
+        REG_BOOST_CFG = 0x06,	///< Boost Configuration
+        REG_BUCK_VSET = 0x07,	///< Buck Voltage Setting
+        REG_BUCK_CFG = 0x08,	///< Buck Configuration
+        REG_BUCK_ISET = 0x09,	///< Buck Peak Current and Settings
+        REG_LDO_VSET = 0x0A,	///< LDO Voltage Setting
         REG_LDO_CFG = 0x0B,		///< LDO Configuration
-        REG_SWITCH_CFG = 0x0C,		///< Switch Configuration
-        REG_BAT_TIME = 0x0D,		///< Battery Impedance Timing
+        REG_SWITCH_CFG = 0x0C,	///< Switch Configuration
+        REG_BAT_TIME = 0x0D,	///< Battery Impedance Timing
         REG_BAT_CFG = 0x0E,		///< Battery Impedance Configuration
         REG_BAT_BCV = 0x0F,		///< Battery Cell Voltage
         REG_BAT_OCV = 0x10,		///< Open Cell Voltage
         REG_BAT_LCV = 0x11,		///< Loaded Cell Voltage
         REG_MON_CFG = 0x19,		///< Monitor Multiplexer Configuration
-        REG_BOOT_CFG = 0x1A,		///< Boot Configuration
-        REG_PIN_STAT = 0x1B,		///< Pin Status
-        REG_BBB_EXTRA = 0x1C,		///< Buck/Buck-Boost Extra
+        REG_BOOT_CFG = 0x1A,	///< Boot Configuration
+        REG_PIN_STAT = 0x1B,	///< Pin Status
+        REG_BBB_EXTRA = 0x1C,	///< Buck/Buck-Boost Extra
         REG_HANDSHK = 0x1D,		///< Power-On Handshake
-        REG_UVLO_CFG = 0x1E,		///< Under-Voltage Lock Out
+        REG_UVLO_CFG = 0x1E,	///< Under-Voltage Lock Out
         REG_PWR_OFF = 0x1F,		///< Power Off Command
     } registers_t;
 
@@ -177,15 +184,15 @@
      */
     typedef enum {
         MON_PULLDOWN = 0x00,	///< Pulled down by 100k Ohm
-        MON_HI_Z = 0x08,	///< High Impedance
-        MON_SWIN = 0x80,	///< SWIN Selected
-        MON_SWOUT = 0x81,	///< SWOUT Selected
-        MON_BIN = 0x82,	///< BIN Selected
-        MON_BOUT = 0x83,	///< BOUT Selected
-        MON_HVIN = 0x84,	///< HVIN Selected
-        MON_HVOUT = 0x85,	///< HVOUT Selected
-        MON_LIN = 0x86,	///< LIN Selected
-        MON_LOUT = 0x87,	///< LOUT Selected
+        MON_HI_Z = 0x08,		///< High Impedance
+        MON_SWIN = 0x80,		///< SWIN Selected
+        MON_SWOUT = 0x81,		///< SWOUT Selected
+        MON_BIN = 0x82,			///< BIN Selected
+        MON_BOUT = 0x83,		///< BOUT Selected
+        MON_HVIN = 0x84,		///< HVIN Selected
+        MON_HVOUT = 0x85,		///< HVOUT Selected
+        MON_LIN = 0x86,			///< LIN Selected
+        MON_LOUT = 0x87,		///< LOUT Selected
     } monCfg_t;
 
     /**
@@ -343,7 +350,6 @@
     bool isOwner;
     /// Device slave address
     int slaveAddress;
-
 };
 
 #endif /* _MAX14720_H_ */