LTC2942 interface with interrupt and battery register calculations

Committer:
kaushalpkk
Date:
Thu Sep 17 11:20:29 2015 +0000
Revision:
0:f9e13080204a
Child:
1:547ec53dc37d
added DEBUG define in header

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kaushalpkk 0:f9e13080204a 1 #ifndef LTC294X_H
kaushalpkk 0:f9e13080204a 2 #define LTC294X_H
kaushalpkk 0:f9e13080204a 3
kaushalpkk 0:f9e13080204a 4 #include "mbed.h"
kaushalpkk 0:f9e13080204a 5
kaushalpkk 0:f9e13080204a 6 //#define LTC_CONSOLE_OUTOUT 1 /* Set this if you need debug messages on the console; */
kaushalpkk 0:f9e13080204a 7 /* it will have an impact on code-size and power consumption. */
kaushalpkk 0:f9e13080204a 8
kaushalpkk 0:f9e13080204a 9 #if LTC_CONSOLE_OUTOUT
kaushalpkk 0:f9e13080204a 10 //Serial usb(USBTX, USBRX); // tx, rx
kaushalpkk 0:f9e13080204a 11 extern Serial pc;
kaushalpkk 0:f9e13080204a 12 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
kaushalpkk 0:f9e13080204a 13 #else
kaushalpkk 0:f9e13080204a 14 #define DEBUG(...) /* nothing */
kaushalpkk 0:f9e13080204a 15 #endif /* #if NEED_CONSOLE_OUTPUT */
kaushalpkk 0:f9e13080204a 16
kaushalpkk 0:f9e13080204a 17 //gets a bit position by shifting 1
kaushalpkk 0:f9e13080204a 18 #define _BV(x) (1 << x)
kaushalpkk 0:f9e13080204a 19
kaushalpkk 0:f9e13080204a 20 // sbi sets a particular bit and cbi clears the bit (target byte-word, bit position)
kaushalpkk 0:f9e13080204a 21 #define sbi(y,x); y|=_BV(x)
kaushalpkk 0:f9e13080204a 22 #define cbi(y,x); y&=~_BV(x)
kaushalpkk 0:f9e13080204a 23
kaushalpkk 0:f9e13080204a 24
kaushalpkk 0:f9e13080204a 25 //BIT 7-6 ADC MODE SELECTOR
kaushalpkk 0:f9e13080204a 26 #define ADC_AUTO 0x00
kaushalpkk 0:f9e13080204a 27 #define ADC_VOLT 0x01
kaushalpkk 0:f9e13080204a 28 #define ADC_TEMP 0x02
kaushalpkk 0:f9e13080204a 29 #define ADC_SLEEP 0x03
kaushalpkk 0:f9e13080204a 30
kaushalpkk 0:f9e13080204a 31 //BIT 2-1 AL/CC MODE SELECTOR
kaushalpkk 0:f9e13080204a 32 #define ALCC_ALERT 0x04
kaushalpkk 0:f9e13080204a 33 #define ALCC_CCOMP 0x05
kaushalpkk 0:f9e13080204a 34 #define ALCC_OFF 0x06
kaushalpkk 0:f9e13080204a 35
kaushalpkk 0:f9e13080204a 36 //STATUS REGISTER.. READ BIT VALUES ONLY
kaushalpkk 0:f9e13080204a 37 #define CHIP_ID _BV(7)
kaushalpkk 0:f9e13080204a 38 #define RESERVE _BV(6)
kaushalpkk 0:f9e13080204a 39 #define CHARGE_OVUV_ALERT _BV(5)
kaushalpkk 0:f9e13080204a 40 #define TEMPERATURE_ALERT _BV(4)
kaushalpkk 0:f9e13080204a 41 #define CHARGE_HIGH_ALERT _BV(3)
kaushalpkk 0:f9e13080204a 42 #define CHARGE_LOW_ALERT _BV(2)
kaushalpkk 0:f9e13080204a 43 #define VOLT_ALERT _BV(1)
kaushalpkk 0:f9e13080204a 44 #define UVLO_ALERT _BV(0)
kaushalpkk 0:f9e13080204a 45
kaushalpkk 0:f9e13080204a 46
kaushalpkk 0:f9e13080204a 47 //prescaler options
kaushalpkk 0:f9e13080204a 48 #define PRESCALE_128 7
kaushalpkk 0:f9e13080204a 49 #define PRESCALE_64 6
kaushalpkk 0:f9e13080204a 50 #define PRESCALE_32 5
kaushalpkk 0:f9e13080204a 51 #define PRESCALE_16 4
kaushalpkk 0:f9e13080204a 52 #define PRESCALE_8 3
kaushalpkk 0:f9e13080204a 53 #define PRESCALE_4 2
kaushalpkk 0:f9e13080204a 54 #define PRESCALE_2 1
kaushalpkk 0:f9e13080204a 55 #define PRESCALE_1 0
kaushalpkk 0:f9e13080204a 56
kaushalpkk 0:f9e13080204a 57 //CONTROL REGISTER.. BIT LOCATIONS
kaushalpkk 0:f9e13080204a 58 #define ADC_VOLT_MODE 7
kaushalpkk 0:f9e13080204a 59 #define ADC_TEMP_MODE 6
kaushalpkk 0:f9e13080204a 60 #define PRESCALER_2 5
kaushalpkk 0:f9e13080204a 61 #define PRESCALER_1 4
kaushalpkk 0:f9e13080204a 62 #define PRESCALER_0 3
kaushalpkk 0:f9e13080204a 63 #define ALCC_ALERT_MODE 2
kaushalpkk 0:f9e13080204a 64 #define ALCC_CHRGC_MODE 1
kaushalpkk 0:f9e13080204a 65 #define IC_SHUTDOWN 0
kaushalpkk 0:f9e13080204a 66
kaushalpkk 0:f9e13080204a 67 //I2C REGISTER ADDRESSES
kaushalpkk 0:f9e13080204a 68 #define REG_STATUS 0x00 // 00h A Status R See Below
kaushalpkk 0:f9e13080204a 69 #define REG_CONTROL 0x01 // 01h B Control R/W 3Ch
kaushalpkk 0:f9e13080204a 70 #define ACC_MSB 0x02 // 02h C Accumulated Charge MSB R/W 7Fh
kaushalpkk 0:f9e13080204a 71 #define ACC_LSB 0x03 // 03h D Accumulated Charge LSB R/W FFh
kaushalpkk 0:f9e13080204a 72 #define CHRG_THH_MSB 0x04 // 04h E Charge Threshold High MSB R/W FFh
kaushalpkk 0:f9e13080204a 73 #define CHRG_THH_LSB 0x05 // 05h F Charge Threshold High LSB R/W FFh
kaushalpkk 0:f9e13080204a 74 #define CHRG_THL_MSB 0x06 // 06h G Charge Threshold Low MSB R/W 00h
kaushalpkk 0:f9e13080204a 75 #define CHRG_THL_LSB 0x07 // 07h H Charge Threshold Low LSB R/W 00h
kaushalpkk 0:f9e13080204a 76 #define VOLT_MSB 0x08 // 08h I Voltage MSB R XXh
kaushalpkk 0:f9e13080204a 77 #define VOLT_LSB 0x09 // 09h J Voltage LSB R XXh
kaushalpkk 0:f9e13080204a 78 #define VOLT_THH 0x0A // 0Ah K Voltage Threshold High R/W FFh
kaushalpkk 0:f9e13080204a 79 #define VOLT_THL 0x0B // 0Bh L Voltage Threshold Low R/W 00h
kaushalpkk 0:f9e13080204a 80 #define TEMP_MSB 0x0C // 0Ch M Temperature MSB R XXh
kaushalpkk 0:f9e13080204a 81 #define TEMP_LSB 0x0D // 0Dh N Temperature LSB R XXh
kaushalpkk 0:f9e13080204a 82 #define TEMPERATURE_THH 0x0E // 0Eh O Temperature Threshold High R/W FFh
kaushalpkk 0:f9e13080204a 83 #define TEMPERATURE_THL 0x0F // 0Fh P Temperature Threshold Low R/W 00h
kaushalpkk 0:f9e13080204a 84
kaushalpkk 0:f9e13080204a 85
kaushalpkk 0:f9e13080204a 86 /** LTC29421 class.
kaushalpkk 0:f9e13080204a 87 * interface LTC29421 coloumb counter on i2c port with interrupt, function name (and battery capacity in Ah Amp-hour).
kaushalpkk 0:f9e13080204a 88 *
kaushalpkk 0:f9e13080204a 89 * Example:
kaushalpkk 0:f9e13080204a 90 * @code
kaushalpkk 0:f9e13080204a 91 * #include "mbed.h"
kaushalpkk 0:f9e13080204a 92 * #include "LTC29421.h"
kaushalpkk 0:f9e13080204a 93 *
kaushalpkk 0:f9e13080204a 94 * LTC29421 ltc(PTC9, PTC8, PTA4, &interrDOWN, 0.560);
kaushalpkk 0:f9e13080204a 95 *
kaushalpkk 0:f9e13080204a 96 * int main() {
kaushalpkk 0:f9e13080204a 97 * float chLevel = ltc.accumulatedCharge();
kaushalpkk 0:f9e13080204a 98 * }
kaushalpkk 0:f9e13080204a 99 * @endcode
kaushalpkk 0:f9e13080204a 100 */
kaushalpkk 0:f9e13080204a 101
kaushalpkk 0:f9e13080204a 102 class LTC29421{
kaushalpkk 0:f9e13080204a 103 public:
kaushalpkk 0:f9e13080204a 104 /** create LTC29421 instance based on I2C, WARNING! use setPrescAndBattCap() to set Battery Capacity
kaushalpkk 0:f9e13080204a 105 *
kaushalpkk 0:f9e13080204a 106 * @param pin sda
kaushalpkk 0:f9e13080204a 107 * @param pin scl
kaushalpkk 0:f9e13080204a 108 * @param pin al/cc interrupt
kaushalpkk 0:f9e13080204a 109 */
kaushalpkk 0:f9e13080204a 110 LTC29421(PinName, PinName, PinName, void (*interruptFunc)(void));
kaushalpkk 0:f9e13080204a 111
kaushalpkk 0:f9e13080204a 112 /** create LTC29421 instance based on I2C,
kaushalpkk 0:f9e13080204a 113 * this will computer internal M prescale value and set internal max charge level
kaushalpkk 0:f9e13080204a 114 *
kaushalpkk 0:f9e13080204a 115 * @param pin sda
kaushalpkk 0:f9e13080204a 116 * @param pin scl
kaushalpkk 0:f9e13080204a 117 * @param pin al/cc interrupt
kaushalpkk 0:f9e13080204a 118 * @param float battery capacity in Ah (Amp-hour)
kaushalpkk 0:f9e13080204a 119 */
kaushalpkk 0:f9e13080204a 120 LTC29421(PinName, PinName, PinName, void (*interruptFunc)(void), float);
kaushalpkk 0:f9e13080204a 121
kaushalpkk 0:f9e13080204a 122
kaushalpkk 0:f9e13080204a 123 /** select an ADC measurement option for voltage / temperature
kaushalpkk 0:f9e13080204a 124 *
kaushalpkk 0:f9e13080204a 125 * @param ADC_x
kaushalpkk 0:f9e13080204a 126 */
kaushalpkk 0:f9e13080204a 127 void setADCMode(int);
kaushalpkk 0:f9e13080204a 128
kaushalpkk 0:f9e13080204a 129 /** select a prescaler value (not required if object initialized with battery capacity
kaushalpkk 0:f9e13080204a 130 *
kaushalpkk 0:f9e13080204a 131 * @param PRESCALE_x
kaushalpkk 0:f9e13080204a 132 */
kaushalpkk 0:f9e13080204a 133 void setPrescaler(int);
kaushalpkk 0:f9e13080204a 134
kaushalpkk 0:f9e13080204a 135 /** returns prescale value in integer
kaushalpkk 0:f9e13080204a 136 *
kaushalpkk 0:f9e13080204a 137 * @returns int prescale value
kaushalpkk 0:f9e13080204a 138 */
kaushalpkk 0:f9e13080204a 139 int getPrescaler();
kaushalpkk 0:f9e13080204a 140
kaushalpkk 0:f9e13080204a 141 /** configure the AL/CC interrupt: TODO works as Alarm only
kaushalpkk 0:f9e13080204a 142 *
kaushalpkk 0:f9e13080204a 143 * @param ALCC_x
kaushalpkk 0:f9e13080204a 144 */
kaushalpkk 0:f9e13080204a 145 void configALCC(int);
kaushalpkk 0:f9e13080204a 146
kaushalpkk 0:f9e13080204a 147 /** shutsdown the analog section of LTC2942, also used to read accumulated charge registers
kaushalpkk 0:f9e13080204a 148 *
kaushalpkk 0:f9e13080204a 149 * @param none
kaushalpkk 0:f9e13080204a 150 */
kaushalpkk 0:f9e13080204a 151 void shutdown();
kaushalpkk 0:f9e13080204a 152
kaushalpkk 0:f9e13080204a 153 /** wakes the analog section of LTC2942
kaushalpkk 0:f9e13080204a 154 *
kaushalpkk 0:f9e13080204a 155 * @param none
kaushalpkk 0:f9e13080204a 156 */
kaushalpkk 0:f9e13080204a 157 void wake();
kaushalpkk 0:f9e13080204a 158
kaushalpkk 0:f9e13080204a 159 /** wakes the analog section of LTC2942
kaushalpkk 0:f9e13080204a 160 *
kaushalpkk 0:f9e13080204a 161 * @param none
kaushalpkk 0:f9e13080204a 162 */
kaushalpkk 0:f9e13080204a 163
kaushalpkk 0:f9e13080204a 164 /** amend the battery capacity, also computers prescaler M and adjusts internal Max battery capacity
kaushalpkk 0:f9e13080204a 165 *
kaushalpkk 0:f9e13080204a 166 * @param battery capacity in Ah (Amp-hour)
kaushalpkk 0:f9e13080204a 167 */
kaushalpkk 0:f9e13080204a 168 void setPrescAndBattCap(float);
kaushalpkk 0:f9e13080204a 169
kaushalpkk 0:f9e13080204a 170 /** returns the 2 Byte accumulated charge register
kaushalpkk 0:f9e13080204a 171 *
kaushalpkk 0:f9e13080204a 172 * @return accumulated charge read as 2 byte hex value
kaushalpkk 0:f9e13080204a 173 */
kaushalpkk 0:f9e13080204a 174 int accumulatedChargeReg();
kaushalpkk 0:f9e13080204a 175
kaushalpkk 0:f9e13080204a 176 /** write into the 2 Byte accumulated charge register
kaushalpkk 0:f9e13080204a 177 *
kaushalpkk 0:f9e13080204a 178 * @param accumulated charge write as 2 byte hex value
kaushalpkk 0:f9e13080204a 179 */
kaushalpkk 0:f9e13080204a 180 void accumulatedChargeReg(int);
kaushalpkk 0:f9e13080204a 181
kaushalpkk 0:f9e13080204a 182 /** reads the accumulated charge register as a percent value computed from max battery capacity
kaushalpkk 0:f9e13080204a 183 *
kaushalpkk 0:f9e13080204a 184 * @return accumulated charge read as percent
kaushalpkk 0:f9e13080204a 185 */
kaushalpkk 0:f9e13080204a 186 float accumulatedCharge();
kaushalpkk 0:f9e13080204a 187
kaushalpkk 0:f9e13080204a 188 /** writes the accumulated charge register as a percent value computed from max battery capacity
kaushalpkk 0:f9e13080204a 189 * use this to initialise the battery capacity if known,
kaushalpkk 0:f9e13080204a 190 * or set it to 50% or set it to 100% if battery capacity reaches max
kaushalpkk 0:f9e13080204a 191 *
kaushalpkk 0:f9e13080204a 192 * @param accumulated charge as percent (0-100)
kaushalpkk 0:f9e13080204a 193 */
kaushalpkk 0:f9e13080204a 194 void accumulatedCharge(float);
kaushalpkk 0:f9e13080204a 195
kaushalpkk 0:f9e13080204a 196 /** set low battery capacity threshhold
kaushalpkk 0:f9e13080204a 197 *
kaushalpkk 0:f9e13080204a 198 * @param float percent remaining value(0-100)
kaushalpkk 0:f9e13080204a 199 */
kaushalpkk 0:f9e13080204a 200 void setChargeThresholdLow(float);
kaushalpkk 0:f9e13080204a 201
kaushalpkk 0:f9e13080204a 202 /** set high battery capacity threshhold
kaushalpkk 0:f9e13080204a 203 *
kaushalpkk 0:f9e13080204a 204 * @param float percent remaining value(0-100)
kaushalpkk 0:f9e13080204a 205 */
kaushalpkk 0:f9e13080204a 206 void setChargeThresholdHigh(float);
kaushalpkk 0:f9e13080204a 207
kaushalpkk 0:f9e13080204a 208 /** obtains battery voltage at (sense-) pin
kaushalpkk 0:f9e13080204a 209 * if required ADC is set
kaushalpkk 0:f9e13080204a 210 *
kaushalpkk 0:f9e13080204a 211 * @return battery level in Volts
kaushalpkk 0:f9e13080204a 212 */
kaushalpkk 0:f9e13080204a 213 float voltage();
kaushalpkk 0:f9e13080204a 214
kaushalpkk 0:f9e13080204a 215 /** set low battery level threshhold
kaushalpkk 0:f9e13080204a 216 *
kaushalpkk 0:f9e13080204a 217 * @param Voltage level in Volts
kaushalpkk 0:f9e13080204a 218 */
kaushalpkk 0:f9e13080204a 219 void setVoltageThresholdLow(float);
kaushalpkk 0:f9e13080204a 220
kaushalpkk 0:f9e13080204a 221 /** set high battery level threshhold
kaushalpkk 0:f9e13080204a 222 *
kaushalpkk 0:f9e13080204a 223 * @param Voltage level in Volts
kaushalpkk 0:f9e13080204a 224 */
kaushalpkk 0:f9e13080204a 225 void setVoltageThresholdHigh(float);
kaushalpkk 0:f9e13080204a 226
kaushalpkk 0:f9e13080204a 227 /** obtains IC temperature, if required ADC is set
kaushalpkk 0:f9e13080204a 228 *
kaushalpkk 0:f9e13080204a 229 * @return temperature in centigrade
kaushalpkk 0:f9e13080204a 230 */
kaushalpkk 0:f9e13080204a 231 float temperature();
kaushalpkk 0:f9e13080204a 232
kaushalpkk 0:f9e13080204a 233 /** set IC temperature low threshold
kaushalpkk 0:f9e13080204a 234 *
kaushalpkk 0:f9e13080204a 235 * @param temperauture in centigrade
kaushalpkk 0:f9e13080204a 236 */
kaushalpkk 0:f9e13080204a 237 void setTemperatureThresholdLow(float);
kaushalpkk 0:f9e13080204a 238
kaushalpkk 0:f9e13080204a 239 /** set IC temperature high threshold
kaushalpkk 0:f9e13080204a 240 *
kaushalpkk 0:f9e13080204a 241 * @param temperauture in centigrade
kaushalpkk 0:f9e13080204a 242 */
kaushalpkk 0:f9e13080204a 243 void setTemperatureThresholdHigh(float);
kaushalpkk 0:f9e13080204a 244
kaushalpkk 0:f9e13080204a 245
kaushalpkk 0:f9e13080204a 246 /** reads all LTC2942 register values prints on serial terminal
kaushalpkk 0:f9e13080204a 247 *
kaushalpkk 0:f9e13080204a 248 * @param none
kaushalpkk 0:f9e13080204a 249 */
kaushalpkk 0:f9e13080204a 250 void readAll();
kaushalpkk 0:f9e13080204a 251
kaushalpkk 0:f9e13080204a 252 /** reads the status register first
kaushalpkk 0:f9e13080204a 253 * uses the ARA alert response protocol for SMBus to clear AL/CC pin
kaushalpkk 0:f9e13080204a 254 *
kaushalpkk 0:f9e13080204a 255 * int resp = ic.alertResponse();
kaushalpkk 0:f9e13080204a 256 * if((resp & TEMPERATURE_ALERT) == TEMPERATURE_ALERT) { }
kaushalpkk 0:f9e13080204a 257 *
kaushalpkk 0:f9e13080204a 258 * @return STATUS register
kaushalpkk 0:f9e13080204a 259 */
kaushalpkk 0:f9e13080204a 260 int alertResponse();
kaushalpkk 0:f9e13080204a 261
kaushalpkk 0:f9e13080204a 262 private:
kaushalpkk 0:f9e13080204a 263 char getStatusReg();
kaushalpkk 0:f9e13080204a 264 char getControlReg();
kaushalpkk 0:f9e13080204a 265 void setControlReg(char);
kaushalpkk 0:f9e13080204a 266
kaushalpkk 0:f9e13080204a 267 I2C _i2c;
kaushalpkk 0:f9e13080204a 268 InterruptIn _alcc;
kaushalpkk 0:f9e13080204a 269
kaushalpkk 0:f9e13080204a 270 char _addr;
kaushalpkk 0:f9e13080204a 271 float _battCap;
kaushalpkk 0:f9e13080204a 272 float _battMax;
kaushalpkk 0:f9e13080204a 273 float _presc;
kaushalpkk 0:f9e13080204a 274 };
kaushalpkk 0:f9e13080204a 275
kaushalpkk 0:f9e13080204a 276
kaushalpkk 0:f9e13080204a 277 #endif