max17050: Be explicit about hardcoded calculations

This commit is contained in:
CTCaer 2020-11-26 01:44:04 +02:00
parent b1c09f8a9c
commit 095b234fce
2 changed files with 15 additions and 10 deletions

View file

@ -26,6 +26,8 @@
#include <soc/i2c.h> #include <soc/i2c.h>
#include <utils/util.h> #include <utils/util.h>
#define BASE_SNS_UOHM 5000
/* Status register bits */ /* Status register bits */
#define STATUS_POR_BIT BIT(1) #define STATUS_POR_BIT BIT(1)
#define STATUS_BST_BIT BIT(3) #define STATUS_BST_BIT BIT(3)
@ -85,31 +87,31 @@ int max17050_get_property(enum MAX17050_reg reg, int *value)
break; break;
case MAX17050_VCELL: // Voltage now. case MAX17050_VCELL: // Voltage now.
data = max17050_get_reg(MAX17050_VCELL); data = max17050_get_reg(MAX17050_VCELL);
*value = data * 625 / 8 / 1000; *value = (data >> 3) * 625 / 1000; /* Units of LSB = 0.625mV */
battery_voltage = *value; battery_voltage = *value;
break; break;
case MAX17050_AvgVCELL: // Voltage avg. case MAX17050_AvgVCELL: // Voltage avg.
data = max17050_get_reg(MAX17050_AvgVCELL); data = max17050_get_reg(MAX17050_AvgVCELL);
*value = data * 625 / 8 / 1000; *value = (data >> 3) * 625 / 1000; /* Units of LSB = 0.625mV */
break; break;
case MAX17050_OCVInternal: // Voltage ocv. case MAX17050_OCVInternal: // Voltage ocv.
data = max17050_get_reg(MAX17050_OCVInternal); data = max17050_get_reg(MAX17050_OCVInternal);
*value = data * 625 / 8 / 1000; *value = (data >> 3) * 625 / 1000; /* Units of LSB = 0.625mV */
break; break;
case MAX17050_RepSOC: // Capacity %. case MAX17050_RepSOC: // Capacity %.
*value = max17050_get_reg(MAX17050_RepSOC); *value = max17050_get_reg(MAX17050_RepSOC);
break; break;
case MAX17050_DesignCap: // Charge full design. case MAX17050_DesignCap: // Charge full design.
data = max17050_get_reg(MAX17050_DesignCap); data = max17050_get_reg(MAX17050_DesignCap);
*value = data * 5 / 10; *value = data * (BASE_SNS_UOHM / MAX17050_BOARD_SNS_RESISTOR_UOHM) / MAX17050_BOARD_CGAIN;
break; break;
case MAX17050_FullCAP: // Charge full. case MAX17050_FullCAP: // Charge full.
data = max17050_get_reg(MAX17050_FullCAP); data = max17050_get_reg(MAX17050_FullCAP);
*value = data * 5 / 10; *value = data * (BASE_SNS_UOHM / MAX17050_BOARD_SNS_RESISTOR_UOHM) / MAX17050_BOARD_CGAIN;
break; break;
case MAX17050_RepCap: // Charge now. case MAX17050_RepCap: // Charge now.
data = max17050_get_reg(MAX17050_RepCap); data = max17050_get_reg(MAX17050_RepCap);
*value = data * 5 / 10; *value = data * (BASE_SNS_UOHM / MAX17050_BOARD_SNS_RESISTOR_UOHM) / MAX17050_BOARD_CGAIN;
break; break;
case MAX17050_TEMP: // Temp. case MAX17050_TEMP: // Temp.
data = max17050_get_reg(MAX17050_TEMP); data = max17050_get_reg(MAX17050_TEMP);
@ -119,12 +121,12 @@ int max17050_get_property(enum MAX17050_reg reg, int *value)
case MAX17050_Current: // Current now. case MAX17050_Current: // Current now.
data = max17050_get_reg(MAX17050_Current); data = max17050_get_reg(MAX17050_Current);
*value = (s16)data; *value = (s16)data;
*value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR; *value *= 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
break; break;
case MAX17050_AvgCurrent: // Current avg. case MAX17050_AvgCurrent: // Current avg.
data = max17050_get_reg(MAX17050_AvgCurrent); data = max17050_get_reg(MAX17050_AvgCurrent);
*value = (s16)data; *value = (s16)data;
*value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR; *value *= 1562500 / (MAX17050_BOARD_SNS_RESISTOR_UOHM * MAX17050_BOARD_CGAIN);
break; break;
default: default:
return -1; return -1;

View file

@ -25,8 +25,11 @@
#include <utils/types.h> #include <utils/types.h>
/* Board default values */
#define MAX17050_BOARD_CGAIN 2 /* Actual: 1.99993 */
#define MAX17050_BOARD_SNS_RESISTOR_UOHM 5000 /* 0.005 Ohm */
#define MAX17050_STATUS_BattAbsent BIT(3) #define MAX17050_STATUS_BattAbsent BIT(3)
#define MAX17050_DEFAULT_SNS_RESISTOR 10000
/* Consider RepCap which is less then 10 units below FullCAP full */ /* Consider RepCap which is less then 10 units below FullCAP full */
#define MAX17050_FULL_THRESHOLD 10 #define MAX17050_FULL_THRESHOLD 10