Seyhmus Cacina / max20303

Fork of max20303 by Maxim Integrated

Files at this revision

API Documentation at this revision

Comitter:
seyhmuscacina
Date:
Thu Jan 03 11:03:10 2019 +0300
Parent:
1:d85b06dc60a3
Commit message:
updates Max20303 code for HSP2.0 P2

Changed in this revision

MAX20303.cpp Show annotated file Show diff for this revision Revisions of this file
MAX20303.h Show annotated file Show diff for this revision Revisions of this file
--- a/MAX20303.cpp	Thu Jan 25 15:31:43 2018 +0300
+++ b/MAX20303.cpp	Thu Jan 03 11:03:10 2019 +0300
@@ -68,12 +68,23 @@
 	appdatainoutbuffer_[1] = 0x34;
 	AppWrite(2);
 
+	return ret;
+}
 
-
+//******************************************************************************
+int MAX20303::LDO2Config()
+{
+	int32_t ret = 0;
+	uint8_t val;
+	appcmdoutvalue_ = 0x42;
+	appdatainoutbuffer_[0] = 0x01;
+	appdatainoutbuffer_[1] = 0x15;     // 0.9V + (0.1V * number)   =  3V
+	AppWrite(2);
 
 	return ret;
 }
 
+
 //******************************************************************************
 int MAX20303::writeReg(registers_t reg, uint8_t value)
 {
@@ -177,6 +188,20 @@
 	return ret;
 }
 //******************************************************************************
+int MAX20303::PowerOffDelaythePMIC(){
+	int ret;
+	appdatainoutbuffer_[0] = 0xB2;
+	appcmdoutvalue_ = 0x84;
+	ret = AppWrite(1);
+
+	if(appcmdoutvalue_ != 0x80){
+		ret |= MAX20303_ERROR;
+	}
+
+	return ret;
+}
+
+//******************************************************************************
 int MAX20303::SoftResetthePMIC(){
 	int ret;
 	appdatainoutbuffer_[0] = 0xB3;
@@ -232,23 +257,90 @@
 	return MAX20303_NO_ERROR;
 }
 
+//******************************************************************************
+char MAX20303::CheckPMICHWID(){
+	int ret;
+	uint8_t value = 0x00;
+
+	ret = readReg(MAX20303::REG_HARDWARE_ID, value);
+	if(ret != MAX20303_NO_ERROR)
+		return false;
+
+	if(value == 0x02)
+		return true;
+	else
+		return false;
+}
+
+//******************************************************************************
+int MAX20303::CheckPMICStatusRegisters(unsigned char buf_results[5]){
+	int ret;
+	ret  = readReg(MAX20303::REG_STATUS0, buf_results[0]);
+	ret |= readReg(MAX20303::REG_STATUS1, buf_results[1]);
+	ret |= readReg(MAX20303::REG_STATUS2, buf_results[2]);
+	ret |= readReg(MAX20303::REG_STATUS3, buf_results[3]);
+	ret |= readReg(MAX20303::REG_SYSTEM_ERROR, buf_results[4]);
+	return ret;
+}
+
+//******************************************************************************
+int MAX20303::Max20303_BatteryGauge(unsigned char *batterylevel){
+    int ret;
+    char data[2];
+
+    data[0] = 0x04;
+    ret = m_i2c->write(MAX20303_I2C_ADDR_FUEL_GAUGE, data, 1);
+    if(ret != 0){
+        printf("Max20303_FuelGauge has failed\r\n");
+    }
+
+    ret = m_i2c->read(MAX20303_I2C_ADDR_FUEL_GAUGE | 1, data, 2);
+    if(ret != 0){
+        printf("Max20303_FuelGauge has failed\r\n");
+    }
+
+    // if the level is more than 100 assume the battery is not connected
+    if(data[0] > 100){
+        *batterylevel = 0;
+    } else{
+
+        *batterylevel = data[0];
+    }
+    return 0;
+}
 
 
 //******************************************************************************
-int MAX20303::led0on(void) {
+int MAX20303::led0on(char enable) {
 
-	writeReg(REG_LED0_DIRECT, 0x21);
-
-	return MAX20303_NO_ERROR;
+	if(enable)
+		return writeReg(REG_LED0_DIRECT, 0x21);
+	else
+		return writeReg(REG_LED0_DIRECT, 0x01);
 }
 
 //******************************************************************************
-int MAX20303::BoostEnable(void) {
+int MAX20303::led1on(char enable) {
+	if(enable)
+		return writeReg(REG_LED1_DIRECT, 0x21);
+	else
+		return writeReg(REG_LED1_DIRECT, 0x01);
+}
 
-	writeReg(REG_AP_CMDOUT, 0x30);
+//******************************************************************************
+int MAX20303::led2on(char enable) {
+	if(enable)
+		return writeReg(REG_LED2_DIRECT, 0x21);
+	else
+		return writeReg(REG_LED2_DIRECT, 0x01);
+}
+
+
+//******************************************************************************
+int MAX20303::BoostEnable(void) {
 	writeReg(REG_AP_DATOUT3, 0x00);	// 00 : 5V
 	writeReg(REG_AP_DATOUT0, 0x01);	// Boost Enabled
-
+	writeReg(REG_AP_CMDOUT, 0x30);
 	return MAX20303_NO_ERROR;
 }
 
--- a/MAX20303.h	Thu Jan 25 15:31:43 2018 +0300
+++ b/MAX20303.h	Thu Jan 03 11:03:10 2019 +0300
@@ -44,6 +44,7 @@
 #define MAX20303_ERROR      -1
 
 #define MAX20303_I2C_ADDR_FUEL_GAUGE	0x6c
+#define MAX20303_I2C_ADDR_FUEL_GAUGE    0x6C
 
 #define MAX20303_LDO_MIN_MV 800
 #define MAX20303_LDO_MAX_MV 3600
@@ -56,37 +57,6 @@
 
 public:
 	/**
-	* @brief  Constructor using reference to I2C object
-	* @param i2c - Reference to I2C object
-	* @param slaveAddress - 7-bit I2C address
-	*/
-	MAX20303(I2C *i2c);
-
-	/** @brief Destructor */
-	~MAX20303(void);
-
-	int led0on(void);
-	int BoostEnable(void);
-	int BuckBoostEnable(void);
-
-	/// @brief Enable the 1.8V output rail **/
-	int LDO1Config(void);
-	int mv2bits(int mV);
-
-	/** @brief Power Off the board
-	 */
-	int PowerOffthePMIC();
-
-	/** @brief Soft reset the PMIC
-	 */
-	int SoftResetthePMIC();
-
-	/** @brief Hard reset the PMIC
-	 */
-	int HardResetthePMIC();
-
-private:
-	/**
 	 * @brief   Register Addresses
 	 * @details Enumerated MAX20303 register addresses
 	 */
@@ -161,6 +131,58 @@
 		*/
 	};
 
+	/**
+	* @brief  Constructor using reference to I2C object
+	* @param i2c - Reference to I2C object
+	* @param slaveAddress - 7-bit I2C address
+	*/
+	MAX20303(I2C *i2c);
+
+	/** @brief Destructor */
+	~MAX20303(void);
+
+	int led0on(char enable);
+	int led1on(char enable);
+	int led2on(char enable);
+	int BoostEnable(void);
+	int BuckBoostEnable(void);
+
+	/// @brief Enable the 1.8V output rail **/
+	int LDO1Config(void);
+
+	/// @brief Enable the 3V output rail **/
+	int LDO2Config(void);
+
+
+	int mv2bits(int mV);
+
+	/** @brief Power Off the board
+	 */
+	int PowerOffthePMIC();
+
+	/** @brief Power Off the board with 30ms delay
+	 */
+	int PowerOffDelaythePMIC();
+
+	/** @brief Soft reset the PMIC
+	 */
+	int SoftResetthePMIC();
+
+	/** @brief Hard reset the PMIC
+	 */
+	int HardResetthePMIC();
+
+	/** @brief check if can communicate with max20303
+	*/
+	char CheckPMICHWID();
+
+	/** @brief CheckPMICStatusRegisters
+	*/
+	int CheckPMICStatusRegisters(unsigned char buf_results[5]);
+
+    int Max20303_BatteryGauge(unsigned char *batterylevel);
+
+private:
 
 	int writeReg(registers_t reg, uint8_t value);
 	int readReg(registers_t reg, uint8_t &value);