| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LTC3335.cpp
Go to the documentation of this file.
1 /*!
2 LTC3335: Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter
3
4 This driver file defines a minimal set of functions to allow access to
5 the features of the LTC3335 part while abstracting away from the register
6 transactions. Configuration of this driver is done by modifying the
7 definitions in LTC3335_Config.h.
8
9 @verbatim
10
11 The LTC®3335 is a high efficiency, low quiescent current
12 (680nA) buck-boost DC/DC converter with an integrated
13 precision coulomb counter which monitors accumulated
14 battery discharge in long life battery powered applications.
15 The buck-boost can operate down to 1.8V on its input and
16 provides eight pin-selectable output voltages with up to
17 50mA of output current.
18
19 The coulomb counter stores the accumulated battery discharge
20 in an internal register accessible via an I2C interface.
21 The LTC3335 features a programmable discharge alarm
22 threshold. When the threshold is reached, an interrupt is
23 generated at the IRQ pin.
24
25 To accommodate a wide range of battery types and sizes,
26 the peak input current can be selected from as low as 5mA
27 to as high as 250mA and the full-scale coulomb counter
28 has a programmable range of 32,768:1.
29
30 @endverbatim
31
32 http://www.linear.com/product/LTC3335
33
34 http://www.linear.com/product/LTC3335#demoboards
35
36
37 Copyright 2018(c) Analog Devices, Inc.
38
39 All rights reserved.
40
41 Redistribution and use in source and binary forms, with or without
42 modification, are permitted provided that the following conditions are met:
43 - Redistributions of source code must retain the above copyright
44 notice, this list of conditions and the following disclaimer.
45 - Redistributions in binary form must reproduce the above copyright
46 notice, this list of conditions and the following disclaimer in
47 the documentation and/or other materials provided with the
48 distribution.
49 - Neither the name of Analog Devices, Inc. nor the names of its
50 contributors may be used to endorse or promote products derived
51 from this software without specific prior written permission.
52 - The use of this software may or may not infringe the patent rights
53 of one or more patent holders. This license does not release you
54 from the requirement that you obtain separate licenses from these
55 patent holders to use this software.
56 - Use of the software either in source or binary form, must be run
57 on or directly connected to an Analog Devices Inc. component.
58
59 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
60 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
61 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
63 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
64 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
65 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
66 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
67 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
68 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 //! @ingroup Switching_Regulators
72 //! @{
73 //! @defgroup LTC3335 LTC3335: Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter
74 //! @}
75
76 /*! @file
77 @ingroup LTC3335
78 Driver File for LTC3335: Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter
79 */
80
81 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
82 // Includes
83 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
84 #include "LTC3335.h"
85
86 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
87 // Definitions
88 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
89
90 // \cond
91 //! @name I2C Address Definitions
92 //! @{
93 //! I2C address and format from datasheet page 5
94 #define LTC3335_BASE_ADDRESS 0x64 //!< BASE_ADDRESS for LTC3335.
95 #define LTC3335_WRITE_BIT 0 //!< I2C R/W bit value for WRITE.
96 #define LTC3335_READ_BIT 1 //!< I2C R/W bit value for READ.
97 //! @}
98
99 //! @name LTC3335 Register Definitions
100 //! @{
101 //! Register Map from datasheet table 3
102 #define LTC3335_REGISTER_A 01 //!< VOUT selection and prescaler selection register.
103 #define LTC3335_REGISTER_B 02 //!< Alarm threshold register.
104 #define LTC3335_REGISTER_C 03 //!< Accumulator register.
105 #define LTC3335_REGISTER_D 04 //!< Alarms register.
106 #define LTC3335_REGISTER_E 05 //!< Status register.
107 #define LTC3335_INVALID 0xFF //!< code to indicate that cached data has not be set yet.
108 //! @}
109 // \endcond
110
111 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
112 // Global Data
113 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
114
115 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
116 // Local Data
117 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
118
119 //! @name Cached LTC3335 Data
120 //! @{
121 //! Cached copies of values in registers, so that they do not need to be read/modified/written each time a function is called to only set a portion of that register.
122 static uint8_t ltc3335_subaddress_last; //!< last subaddress written, so that it is not repeatedly sent when the same register is polled.
123 static LTC3335_OUTPUT_VOLTAGE_TYPE ltc3335_voltage_selection_last; //!< last voltage selected through software.
124 static boolean ltc3335_voltage_selection_enabled_last; //!< last enable for software selected voltage.
125 static uint8_t ltc3335_prescaler_last; //!< last prescaler selected.
126 static boolean ltc3335_counter_test_last; //!< value of the counter test bit last written to the LTC3335.
127 //! @}
128
129 //! @name Counter Test Variables
130 //! Variables used to count edges per second when the Counter Test feature is turned on, providing an instantaneous measurement of the battery current.
131 #if LTC3335_USE_CURRENT_MEASUREMENT == true
132 static uint16_t ltc3335_hw_timer_last; //!< last value of the hardware timer accessed by LTC3335 driver.
133 static uint16_t ltc3335_hw_counter_last; //!< last value of the hardware counter accessed by LTC3335 driver.
134 static uint32_t ltc3335_counter_test_edge_count; //!< the number of rising edges on the /IRQ pin since the Counter Test results were last cleared.
135 static uint32_t ltc3335_counter_test_time; //!< the amount of timer ticks since the Counter Test results were last cleared.
136 #endif // #if LTC3335_USE_CURRENT_MEASUREMENT == true
137 //! @}
138
139 //! @name
140 //!
141 #if LTC3335_USE_SOFTWARE_CORRECTION == true
142 static uint8_t ltc3335_accumulator_last; //!<
143 static uint32_t ltc3335_quiescent_current_timer; //!<
144 static uint32_t ltc3335_discharged_capacity; //!<
145 #endif // #if LTC3335_USE_SOFTWARE_CORRECTION == true
146 //! @}
147
148 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
149 // Local Prototypes
150 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
151
152 static int8_t ltc3335_set_register(uint8_t subaddress, uint8_t *ltc3335_data);
153 static int8_t ltc3335_get_register(uint8_t subaddress, uint8_t *ltc3335_data);
154 static uint8_t ltc3335_encode_register_a(boolean enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage, uint8_t prescaler);
155 static void ltc3335_decode_register_a(uint8_t register_a);
156
157 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
158 // Global Functions
159 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
160
161 // Initializes the LTC3335 driver.
162 int8_t LTC3335_Init(void)
163 {
164 int8_t result;
165 uint8_t ltc3335_data;
166
167 // Init all of the microprocessor hardware used by the LTC3335 driver.
168 LTC3335_MICRO_INIT();
169
170 // Init to INVALID, so that subaddress will be sent for first right. After this, subaddress will only be written if needed.
171 ltc3335_subaddress_last = LTC3335_INVALID;
172
173 // Send command to set the voltage and prescaler set in LTC3335_Config.h with one i2c transaction.
174 ltc3335_data = ltc3335_encode_register_a(true, (LTC3335_OUTPUT_VOLTAGE_TYPE) LTC3335_OUTPUT_VOLTAGE, LTC3335_PRESCALER);
175 result = ltc3335_set_register(LTC3335_REGISTER_A, &ltc3335_data);
176
177 // Set cached values in case voltage or prescaler are changed during runtime.
178 ltc3335_voltage_selection_last = (LTC3335_OUTPUT_VOLTAGE_TYPE) LTC3335_OUTPUT_VOLTAGE;
179 ltc3335_prescaler_last = LTC3335_PRESCALER;
180 ltc3335_voltage_selection_enabled_last = true;
181
182 // Set the alarm threshold. Round down so that set value is always <= to the desired value.
183 #if LTC3335_USE_SOFTWARE_CORRECTION == false
184 ltc3335_data = (1LL * LTC3335_ALARM_CAPACITY * SEC_PER_HR)/LTC3335_RESOLUTION(LTC3335_PRESCALER);
185 #else
186 ltc3335_data = (1LL * LTC3335_ALARM_CAPACITY * SEC_PER_HR)/(LTC3335_RESOLUTION(LTC3335_PRESCALER) * ((1LL << 16) + LTC3335_CORRECTION_FACTOR_TYP) / (1L << 16));
187 #endif // #if LTC3335_USE_SOFTWARE_CORRECTION == false
188 result |= ltc3335_set_register(LTC3335_REGISTER_B, &ltc3335_data);
189
190 // Turn counter test on or off given setting in in LTC3335_Config.h.
191 result |= LTC3335_Set_Counter_Test(LTC3335_USE_CURRENT_MEASUREMENT);
192
193 #if LTC3335_USE_SOFTWARE_CORRECTION == true
194 // Initialize the corrected discharged capacity and timer for quiescent current accumulation..
195 ltc3335_discharged_capacity = 0;
196 ltc3335_accumulator_last = 0;
197 ltc3335_quiescent_current_timer = LTC3335_TIMER_GET();
198 result |= LTC3335_Get_Discharged_Capacity(&ltc3335_discharged_capacity, LTC3335_VBAT_TYP);
199 #endif // #if LTC3335_USE_SOFTWARE_CORRECTION == true
200
201 return result;
202 }
203
204 // Enables/Disables software control of the LTC3335 output voltage.
205 // If software control is enabled, the voltage is set to the specified setting.
206 int8_t LTC3335_Set_Voltage(boolean enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage)
207 {
208 int8_t result;
209 uint8_t ltc3335_data;
210
211 // Can not set out of range.
212 if (LTC3335_NUM_OUTPUT_VOLTAGES <= voltage)
213 {
214 return 1;
215 }
216
217 // Send command to set the voltage.
218 ltc3335_data = ltc3335_encode_register_a(enabled, voltage, ltc3335_prescaler_last);
219 result = ltc3335_set_register(LTC3335_REGISTER_A, &ltc3335_data);
220
221 return result;
222 }
223
224 // Gets the alarms active from the LTC3335.
225 int8_t LTC3335_Get_Alarms(LTC3335_ALARM_TYPE *alarms)
226 {
227 int8_t result;
228 uint8_t ltc3335_data;
229
230 result = ltc3335_get_register(LTC3335_REGISTER_D, &ltc3335_data);
231
232 if (result == 0)
233 {
234 // Decode the register into its alarm bits.
235 if (ltc3335_data & MASK(1, 0))
236 {
237 alarms->ac_on_time_overflow = 1;
238 }
239 else
240 {
241 alarms->ac_on_time_overflow = 0;
242 }
243
244 if (ltc3335_data & MASK(1, 1))
245 {
246 alarms->coulomb_counter_overflow = 1;
247 }
248 else
249 {
250 alarms->coulomb_counter_overflow = 0;
251 }
252
253 if (ltc3335_data & MASK(1, 2))
254 {
255 alarms->alarm_trip = 1;
256 }
257 else
258 {
259 alarms->alarm_trip = 0;
260 }
261 }
262
263 return result;
264 }
265
266 // Sends the command to clear the INT condition.
267 // NOTE! Additional registers are rewritten in order for the INT condition to be reset.
268 int8_t LTC3335_Clear_Int(LTC3335_ALARM_TYPE *alarms)
269 {
270 int8_t result = 0;
271 uint8_t ltc3335_data;
272
273 // Write to other registers required depending upon the specific type of alarm
274 if (alarms->coulomb_counter_overflow)
275 {
276 // The accumulator must be rewritten in order for the clear int command to clear these alarms.
277 result |= ltc3335_get_register(LTC3335_REGISTER_C, &ltc3335_data);
278 if (result == 0) result |= ltc3335_set_register(LTC3335_REGISTER_C, &ltc3335_data);
279 }
280
281 if (alarms->alarm_trip)
282 {
283 // Note that if the accumulator is >= the alarm value, the alarm_trip bit will immediately trip again.
284 }
285
286 if (alarms->ac_on_time_overflow)
287 {
288 // Note AC ON Time Alarm can not be cleared until PK[2:0] (Pin 15, 14, 13) are changed to match inductor value.
289 }
290
291 // If currently doing a Counter Test, do not turn it off when clearing the int.
292 ltc3335_data = 1;
293 if (ltc3335_counter_test_last == true)
294 {
295 ltc3335_data |= 2;
296 }
297
298 result |= ltc3335_set_register(LTC3335_REGISTER_E, &ltc3335_data);
299 ltc3335_data++;
300
301 return result;
302 }
303
304 #if LTC3335_USE_SOFTWARE_CORRECTION == false
305 // Reads the accumulator and translates into discharged capacity.
306 int8_t LTC3335_Get_Discharged_Capacity(uint32_t *discharged_capacity)
307 {
308 int8_t result = true;
309 uint8_t ltc3335_data;
310
311 result = ltc3335_get_register(LTC3335_REGISTER_C, &ltc3335_data);
312
313 if (result == 0)
314 {
315 *discharged_capacity = ltc3335_data * LTC3335_RESOLUTION(LTC3335_PRESCALER);
316 }
317
318 return result;
319 }
320 #else
321 // Tracks changes in the accumulator and translates changes in discharged capacity, accounting
322 // for the resolution available at the current battery voltage, output voltage, and ipeak.
323 // The quiescent current is accumulated over time and used to update the discharged capacity.
324 int8_t LTC3335_Get_Discharged_Capacity(uint32_t *discharged_capacity, uint16_t vbat)
325 {
326 int8_t result;
327 uint8_t ltc3335_data;
328
329 result = ltc3335_get_register(LTC3335_REGISTER_C, &ltc3335_data);
330
331 if (result == 0)
332 {
333 if (ltc3335_data != ltc3335_accumulator_last)
334 {
335 // Add in changes in the accumulator, adjusted by the software correction factor.
336 int16_t temp16 = LTC3335_Get_Software_Correction_Factor(vbat);
337 uint32_t mAs = (uint8_t)(ltc3335_data - ltc3335_accumulator_last) * LTC3335_RESOLUTION(LTC3335_PRESCALER);
338 mAs += ((mAs * temp16 + (1L << 15)) >> 16) | (temp16 < 0 ? 0xFFFF0000LL : 0);
339 ltc3335_discharged_capacity += mAs;
340 ltc3335_accumulator_last = ltc3335_data;
341 }
342 }
343
344 // Look for enough time to pass to increment discharged capacity by 1mAs.
345 if (LTC3335_TIMER_GET() - ltc3335_quiescent_current_timer > LTC3335_TIMER_COUNTS_PER_IQ_MAS)
346 {
347 ltc3335_discharged_capacity++;
348 ltc3335_quiescent_current_timer += LTC3335_TIMER_COUNTS_PER_IQ_MAS;
349 }
350
351 *discharged_capacity = ltc3335_discharged_capacity;
352
353 return result;
354
355 }
356 #endif // LTC3335_USE_SOFTWARE_CORRECTION == false
357
358 // Enables/Disables the LTC3335 Counter Test feature. This can be used to verify the
359 // LTC3335 is functional, or it can be used to measure the battery current.
360 int8_t LTC3335_Set_Counter_Test(boolean enabled)
361 {
362 int8_t result;
363 uint8_t ltc3335_data;
364
365 // Send command to start the test.
366 ltc3335_data = ((enabled == true) ? MASK(1, 1) : 0);
367 result = ltc3335_set_register(LTC3335_REGISTER_E, &ltc3335_data);
368
369 // Store results of the test so that we don't need to poll the IC for that info.
370 if (result == 0)
371 {
372 ltc3335_counter_test_last = enabled;
373 }
374
375 #if LTC3335_USE_CURRENT_MEASUREMENT == true
376 // Initialize after the command is sent, as the first edge that occurs
377 // when the test is started should not be counted.
378 if (enabled == true)
379 {
380 // Take initial readings when test is started.
381 LTC3335_Counter_Test_Current_Task();
382 }
383 else
384 {
385 // Reset test results when test is stopped.
386 LTC3335_Reset_Counter_Test_Current();
387 }
388 #endif
389
390 return result;
391 }
392
393
394 #if LTC3335_USE_CURRENT_MEASUREMENT == true
395 // Resets the number of edges and the amount of time stored for the Counter Test feature.
396 int8_t LTC3335_Reset_Counter_Test_Current(void)
397 {
398 ltc3335_counter_test_edge_count = 0;
399 ltc3335_counter_test_time = 0;
400 ltc3335_hw_timer_last = LTC3335_TIMER_GET();
401 ltc3335_hw_counter_last = LTC3335_COUNTER_GET();
402
403 return 0;
404 }
405
406 // Accumulates the number of edges on the /IRQ pins provided by the LTC3335 Counter Test feature over a period of time,
407 // the ratio of which can be used to calculate battery current.
408 // Task that must be run periodically at a rate faster than LTC3335_MIN_CURRENT_TASK_RATE.
409 void LTC3335_Counter_Test_Current_Task(void)
410 {
411 if (ltc3335_counter_test_last == true)
412 {
413 // Get new timer and counter values
414 uint16_t hw_timer_new = LTC3335_TIMER_GET();
415 uint16_t hw_counter_new = LTC3335_COUNTER_GET();
416
417 // Add difference between last timer and counter to summations.
418 ltc3335_counter_test_edge_count += (hw_counter_new - ltc3335_hw_counter_last);
419 ltc3335_counter_test_time += (hw_timer_new - ltc3335_hw_timer_last);
420
421 // Save new timer and counter values for next execution of task.
422 ltc3335_hw_timer_last = hw_timer_new;
423 ltc3335_hw_counter_last = hw_counter_new;
424 }
425
426 return;
427 }
428
429 #if LTC3335_USE_SOFTWARE_CORRECTION == false
430 // Calculates the battery current from the number of edges on the /IRQ pins over a period of time accumulated in LTC3335_Counter_Test_Current_Task.
431 int8_t LTC3335_Get_Counter_Test_Current(uint16_t *microamps)
432 {
433 if ((ltc3335_counter_test_last == true) && ((ltc3335_counter_test_time != 0)))
434 {
435 if (ltc3335_counter_test_edge_count != 0)
436 {
437 uint64_t uAs = (uint64_t)(1LL * LTC3335_IPEAK_MA * UA_PER_MA * LTC3335_TFS * LTC3335_TIMER_COUNTS_PER_SEC) * ltc3335_counter_test_edge_count;
438 uAs += ltc3335_counter_test_time/2;
439 uAs /= ltc3335_counter_test_time;
440 if (uAs >= (1L << 16))
441 {
442 *microamps = (1L << 16) - 1;
443 }
444 else
445 {
446 *microamps = (uint16_t) uAs;
447 }
448 }
449 else
450 {
451 *microamps = 0;
452 }
453 return 0;
454 }
455 else
456 {
457 return 1;
458 }
459 }
460 #else
461 // Calculates the battery current from the number of edges on the /IRQ pins over a period of time accumulated in,
462 // LTC3335_Counter_Test_Current_Task accounting for the resolution available at the current battery voltage, output voltage, and ipeak.
463 int8_t LTC3335_Get_Counter_Test_Current(uint16_t *microamps, uint16_t vbat)
464 {
465
466 if ((ltc3335_counter_test_last == true) && ((ltc3335_counter_test_time != 0)))
467 {
468 if (ltc3335_counter_test_edge_count != 0)
469 {
470 int16_t temp16 = LTC3335_Get_Software_Correction_Factor(vbat);
471 uint64_t uAs = (uint64_t)(1LL * LTC3335_IPEAK_MA * UA_PER_MA * LTC3335_TFS * LTC3335_TIMER_COUNTS_PER_SEC) * ltc3335_counter_test_edge_count;
472 uAs += ((uAs * temp16 + (1L << 15)) >> 16) | (temp16 < 0 ? 0xFFFF000000000000LL : 0);
473 uAs += ltc3335_counter_test_time / 2;
474 uAs /= ltc3335_counter_test_time;
475 if (uAs >= (1L << 16))
476 {
477 *microamps = (1L << 16) - 1;
478 }
479 else
480 {
481 *microamps = (uint16_t) uAs;
482 }
483 }
484 else
485 {
486 *microamps = 0;
487 }
488 return 0;
489 }
490 else
491 {
492 return 1;
493 }
494 }
495 #endif // #if LTC3335_USE_SOFTWARE_CORRECTION == false
496 #endif // #if LTC3335_USE_CURRENT_MEASUREMENT == true
497
498 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
499 // Local Functions
500 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
501
502 // Writes to an LTC3335 register.
503 static int8_t ltc3335_set_register(uint8_t subaddress, uint8_t *ltc3335_data_ptr)
504 {
505 int8_t result = 0;
506 result |= i2c_write_byte_data(LTC3335_BASE_ADDRESS, subaddress, *ltc3335_data_ptr);
507 return result;
508 }
509
510 // Reads from a LTC3335 register.
511 static int8_t ltc3335_get_register(uint8_t subaddress, uint8_t *ltc3335_data_ptr)
512 {
513 int8_t result = 0;
514 if (subaddress != ltc3335_subaddress_last)
515 {
516 result |= i2c_write_byte(LTC3335_BASE_ADDRESS, subaddress);
517 }
518
519 result |= i2c_read_byte(LTC3335_BASE_ADDRESS, ltc3335_data_ptr);
520 return result;
521 }
522
523 // Encodes elements of register A.
524 static uint8_t ltc3335_encode_register_a(boolean enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage, uint8_t prescaler)
525 {
526 return ((enabled == true) ? (1 << 7) : 0) | (voltage << 4) | prescaler;
527 }
528
529 // Decodes elements of register A.
530 static void ltc3335_decode_register_a(uint8_t register_a)
531 {
532 ltc3335_voltage_selection_enabled_last = ((register_a & MASK(1, 7)) == MASK(1, 7));
533 ltc3335_voltage_selection_last = (LTC3335_OUTPUT_VOLTAGE_TYPE) ((register_a & MASK(3, 4)) >> 4);
534 ltc3335_prescaler_last = register_a & MASK(4, 0);
535 return;
536 }
LTC3335_Reset_Counter_Test_Current
int8_t LTC3335_Reset_Counter_Test_Current(void)
Resets the number of edges and the amount of time stored for the Counter Test feature.
Definition: LTC3335.cpp:396
LTC3335_Counter_Test_Current_Task
void LTC3335_Counter_Test_Current_Task(void)
Task that must be run periodically, for the edges and time to be stored for the LTC3335 Counter Test ...
Definition: LTC3335.cpp:409
LTC3335_VBAT_TYP
#define LTC3335_VBAT_TYP
in mV, the nominal battery voltage expected for the majority of the battery discharge.
Definition: LTC3335_Config.h:103
i2c_write_byte
int8_t i2c_write_byte(uint8_t address, uint8_t value)
Write "value" byte to device at "address".
Definition: LT_I2C.cpp:109
ltc3335_hw_timer_last
static uint16_t ltc3335_hw_timer_last
last value of the hardware timer accessed by LTC3335 driver.
Definition: LTC3335.cpp:132
LTC3335_Get_Alarms
int8_t LTC3335_Get_Alarms(LTC3335_ALARM_TYPE *alarms)
Gets the alarms active from the LTC3335.
Definition: LTC3335.cpp:225
LTC3335_OUTPUT_VOLTAGE_TYPE
LTC3335_OUTPUT_VOLTAGE_TYPE
Definition: LTC3335.h:120
LTC3335_MICRO_INIT
#define LTC3335_MICRO_INIT()
Definition: LTC3335_Config.h:139
LTC3335_ALARM_TYPE::ac_on_time_overflow
unsigned ac_on_time_overflow
AC(ON) time operating fault (tAC > tFS) due to improperly chosen inductor value timing out the AC(ON)...
Definition: LTC3335.h:154
discharged_capacity
static uint32_t discharged_capacity
in mAs, the discharged capacity calculated from the LTC3335 accumulator and
Definition: DC2343A.ino:110
LTC3335_TIMER_GET
#define LTC3335_TIMER_GET()
Definition: LTC3335_Config.h:124
ltc3335_decode_register_a
static void ltc3335_decode_register_a(uint8_t register_a)
Definition: LTC3335.cpp:530
ltc3335_discharged_capacity
static uint32_t ltc3335_discharged_capacity
Definition: LTC3335.cpp:144
ltc3335_counter_test_edge_count
static uint32_t ltc3335_counter_test_edge_count
the number of rising edges on the /IRQ pin since the Counter Test results were last cleared...
Definition: LTC3335.cpp:134
LTC3335_Set_Counter_Test
int8_t LTC3335_Set_Counter_Test(boolean enabled)
Enables/Disables the LTC3335 Counter Test feature.
Definition: LTC3335.cpp:360
LTC3335_Init
int8_t LTC3335_Init(void)
Verify that battery capacity isn&#39;t so gigantic that it would overflow a 32 bit number.
Definition: LTC3335.cpp:162
ltc3335_subaddress_last
static uint8_t ltc3335_subaddress_last
last subaddress written, so that it is not repeatedly sent when the same register is polled...
Definition: LTC3335.cpp:122
vbat
static uint16_t vbat
battery voltage, optimally measured with an adc, set to a constant in this sketch ...
Definition: DC2343A.ino:117
LTC3335_Get_Discharged_Capacity
int8_t LTC3335_Get_Discharged_Capacity(uint32_t *discharged_capacity)
Gets the discharged capacity from the battery in mAs.
Definition: LTC3335.cpp:306
LTC3335_RESOLUTION
#define LTC3335_RESOLUTION(p)
Macro to retrieve the LTC3335 coulomb count resolution with a given prescaler.
Definition: LTC3335.h:198
LTC3335_ALARM_TYPE::coulomb_counter_overflow
unsigned coulomb_counter_overflow
Coulomb counter operating fault due to an improperly chosen prescalar causing the ripple counter to o...
Definition: LTC3335.h:155
LTC3335_COUNTER_GET
#define LTC3335_COUNTER_GET()
Definition: LTC3335_Config.h:132
alarms
LTC3335_ALARM_TYPE alarms
active alarms read from the LTC3335.
Definition: DC2343A.ino:112
ltc3335_voltage_selection_enabled_last
static boolean ltc3335_voltage_selection_enabled_last
last enable for software selected voltage.
Definition: LTC3335.cpp:124
LTC3335_TIMER_COUNTS_PER_SEC
#define LTC3335_TIMER_COUNTS_PER_SEC
Definition: LTC3335_Config.h:125
i2c_write_byte_data
int8_t i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value)
Write a byte of data to register specified by "command".
Definition: LT_I2C.cpp:155
ltc3335_hw_counter_last
static uint16_t ltc3335_hw_counter_last
last value of the hardware counter accessed by LTC3335 driver.
Definition: LTC3335.cpp:133
LTC3335.h
LTC3335: Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter.
LTC3335_USE_CURRENT_MEASUREMENT
#define LTC3335_USE_CURRENT_MEASUREMENT
Set to true to use the /IRQ pin to measure the battery current real time.
Definition: LTC3335_Config.h:100
ltc3335_quiescent_current_timer
static uint32_t ltc3335_quiescent_current_timer
Definition: LTC3335.cpp:143
LTC3335_Get_Counter_Test_Current
int8_t LTC3335_Get_Counter_Test_Current(uint16_t *microamps)
Gets the battery current in uA.
Definition: LTC3335.cpp:431
LTC3335_Set_Voltage
int8_t LTC3335_Set_Voltage(boolean enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage)
Enables/Disables software control of the LTC3335 output voltage.
Definition: LTC3335.cpp:206
LTC3335_TFS
#define LTC3335_TFS
in s, full scale ON time from page 15 of datasheet.
Definition: LTC3335.h:113
LTC3335_Get_Software_Correction_Factor
int16_t LTC3335_Get_Software_Correction_Factor(uint16_t vbat)
Returns the software correction factor for a specified LTC3335_IPEAK_CONFIGURATION, LTC3335_OUTPUT_VOLTAGE, and battery voltage.
Definition: LTC3335_Correction_Tables.cpp:354
ltc3335_encode_register_a
static uint8_t ltc3335_encode_register_a(boolean enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage, uint8_t prescaler)
Definition: LTC3335.cpp:524
LTC3335_OUTPUT_VOLTAGE
#define LTC3335_OUTPUT_VOLTAGE
VOUT setting from LTC3335_NUM_OUTPUT_VOLTAGES in LTC3335.h.
Definition: LTC3335_Config.h:96
ltc3335_set_register
static int8_t ltc3335_set_register(uint8_t subaddress, uint8_t *ltc3335_data)
Definition: LTC3335.cpp:503
LTC3335_PRESCALER
#define LTC3335_PRESCALER
Macro to select the optimal prescaler for the specified battery capacity at compile time...
Definition: LTC3335.h:188
ltc3335_prescaler_last
static uint8_t ltc3335_prescaler_last
last prescaler selected.
Definition: LTC3335.cpp:125
LTC3335_TIMER_COUNTS_PER_IQ_MAS
#define LTC3335_TIMER_COUNTS_PER_IQ_MAS
counts for iq current to accumulate to 1 mAs
Definition: LTC3335.h:161
ltc3335_counter_test_time
static uint32_t ltc3335_counter_test_time
the amount of timer ticks since the Counter Test results were last cleared.
Definition: LTC3335.cpp:135
LTC3335_ALARM_TYPE::alarm_trip
unsigned alarm_trip
Accumulator value has met or exceeded the alarm threshold value.
Definition: LTC3335.h:156
ltc3335_counter_test_last
static boolean ltc3335_counter_test_last
value of the counter test bit last written to the LTC3335.
Definition: LTC3335.cpp:126
LTC3335_IPEAK_MA
#define LTC3335_IPEAK_MA
mA = 100mA, used in calculations of coulomb count and current.
Definition: LTC3335.h:178
ltc3335_voltage_selection_last
static LTC3335_OUTPUT_VOLTAGE_TYPE ltc3335_voltage_selection_last
last voltage selected through software.
Definition: LTC3335.cpp:123
LTC3335_NUM_OUTPUT_VOLTAGES
#define LTC3335_NUM_OUTPUT_VOLTAGES
Definition: LTC3335.h:129
voltage
static float voltage
Definition: DC2289AA.ino:71
LTC3335_CORRECTION_FACTOR_TYP
#define LTC3335_CORRECTION_FACTOR_TYP
Value of LTC3335_Software_Correction_Table[VBAT_TO_TABLE_INDEX(LTC3335_VBAT_TYP)].
Definition: LTC3335_Config.h:104
LTC3335_Clear_Int
int8_t LTC3335_Clear_Int(LTC3335_ALARM_TYPE *alarms)
Sends the command to clear the INT condition.
Definition: LTC3335.cpp:268
ltc3335_accumulator_last
static uint8_t ltc3335_accumulator_last
Definition: LTC3335.cpp:142
ltc3335_get_register
static int8_t ltc3335_get_register(uint8_t subaddress, uint8_t *ltc3335_data)
Definition: LTC3335.cpp:511
LTC3335_ALARM_TYPE
The alarm conditions which cause the LTC3335 to activate the /INT pin.
Definition: LTC3335.h:152
i2c_read_byte
int8_t i2c_read_byte(uint8_t address, uint8_t *value)
Read a byte, store in "value".
Definition: LT_I2C.cpp:93
LTC3335_ALARM_CAPACITY
#define LTC3335_ALARM_CAPACITY
in mAh, the capacity at which the alarm should be activated.
Definition: LTC3335_Config.h:98