82 #include "PIC16F_I2C.h"
83 #include "PIC16F_Counter.h"
84 #include "PIC16F_Timer.h"
96 #define LTC3335_BASE_ADDRESS 0x64
97 #define LTC3335_WRITE_BIT 0
98 #define LTC3335_READ_BIT 1
104 #define LTC3335_REGISTER_A 01
105 #define LTC3335_REGISTER_B 02
106 #define LTC3335_REGISTER_C 03
107 #define LTC3335_REGISTER_D 04
108 #define LTC3335_REGISTER_E 05
109 #define LTC3335_INVALID 0xFF
124 static uint8_t ltc3335_subaddress_last;
125 static LTC3335_OUTPUT_VOLTAGE_TYPE ltc3335_voltage_selection_last;
126 static bool_t ltc3335_voltage_selection_enabled_last;
127 static uint8_t ltc3335_prescaler_last;
132 static uint16_t ltc3335_hw_timer_last;
133 static uint16_t ltc3335_hw_counter_last;
134 static bool_t ltc3335_counter_test_last;
135 static uint32_t ltc3335_counter_test_edge_count;
136 static uint32_t ltc3335_counter_test_time;
142 static bool_t ltc3335_set_subaddress(uint8_t subaddress);
143 static bool_t ltc3335_set_register(uint8_t subaddress, uint8_t* ltc3335_data);
144 static bool_t ltc3335_get_register(uint8_t subaddress, uint8_t* ltc3335_data);
145 static uint8_t ltc3335_encode_register_a(bool_t enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage, uint8_t prescaler);
146 static void ltc3335_decode_register_a(uint8_t register_a);
147 static void ltc3335_report_error(uint8_t subaddress, bool_t set);
161 ltc3335_subaddress_last = LTC3335_INVALID;
162 ltc3335_voltage_selection_last = LTC3335_INVALID;
163 ltc3335_prescaler_last = LTC3335_INVALID;
164 ltc3335_voltage_selection_enabled_last =
false;
178 uint8_t ltc3335_data;
181 if(LTC3335_NUM_OUTPUT_VOLTAGES <= voltage)
187 ltc3335_data = ltc3335_encode_register_a(enabled, voltage, ltc3335_prescaler_last);
188 success = ltc3335_set_register(LTC3335_REGISTER_A, <c3335_data);
197 bool_t success =
true;
198 uint8_t ltc3335_data;
201 if (ltc3335_prescaler_last == LTC3335_INVALID)
206 success &= ltc3335_get_register(LTC3335_REGISTER_A, <c3335_data);
210 ltc3335_decode_register_a(ltc3335_data);
211 *voltage = ltc3335_voltage_selection_last;
212 *enabled = ltc3335_voltage_selection_enabled_last;
222 uint8_t ltc3335_data;
231 if (ltc3335_voltage_selection_last == LTC3335_INVALID)
233 success =
LTC3335_Get_Voltage(<c3335_voltage_selection_enabled_last, <c3335_voltage_selection_last);
237 ltc3335_data = ltc3335_encode_register_a(ltc3335_voltage_selection_enabled_last, ltc3335_voltage_selection_last, prescaler);
238 success = ltc3335_set_register(LTC3335_REGISTER_A, <c3335_data);
247 uint8_t ltc3335_data;
249 success = ltc3335_get_register(LTC3335_REGISTER_A, <c3335_data);
253 ltc3335_decode_register_a(ltc3335_data);
254 *prescaler = ltc3335_prescaler_last;
263 bool_t success =
true;
265 success = ltc3335_set_register(LTC3335_REGISTER_B, &threshold);
274 uint8_t ltc3335_data;
276 success = ltc3335_get_register(LTC3335_REGISTER_B, <c3335_data);
280 *threshold = ltc3335_data;
291 success = ltc3335_set_register(LTC3335_REGISTER_C, &accumulator);
300 uint8_t ltc3335_data;
302 success = ltc3335_get_register(LTC3335_REGISTER_C, <c3335_data);
306 *accumulator = ltc3335_data;
316 uint8_t ltc3335_data;
318 success = ltc3335_get_register(LTC3335_REGISTER_D, <c3335_data);
322 if(ltc3335_data & MASK(1, 0))
331 if(ltc3335_data & MASK(1, 1))
340 if(ltc3335_data & MASK(1, 2))
358 bool_t success =
true;
359 uint8_t ltc3335_data = 1;
363 if(ltc3335_counter_test_last ==
true)
368 success &= ltc3335_set_register(LTC3335_REGISTER_E, <c3335_data);
378 uint8_t ltc3335_data;
381 ltc3335_data = ((enabled ==
true) ? MASK(1, 1) : 0);
382 success = ltc3335_set_register(LTC3335_REGISTER_E, <c3335_data);
387 ltc3335_counter_test_last = enabled;
411 uint8_t ltc3335_data;
413 success = ltc3335_get_register(LTC3335_REGISTER_E, <c3335_data);
417 ltc3335_counter_test_last = ((ltc3335_data & MASK(1, 1)) == 0) ?
false :
true;
418 *enabled = ltc3335_counter_test_last;
431 bool_t success =
true;
432 uint8_t ltc3335_data;
434 *enabled = ltc3335_counter_test_last;
435 *edge_count = ltc3335_counter_test_edge_count;
436 *time = ltc3335_counter_test_time;
444 ltc3335_counter_test_edge_count = 0;
445 ltc3335_counter_test_time = 0;
446 ltc3335_hw_timer_last = PIC16F_Timer_Get16();
447 ltc3335_hw_counter_last = PIC16F_Counter_Get();
456 if (ltc3335_counter_test_last ==
true)
459 uint16_t hw_timer_new = PIC16F_Timer_Get16();
460 uint16_t hw_counter_new = PIC16F_Counter_Get();
463 ltc3335_counter_test_edge_count += (hw_counter_new - ltc3335_hw_counter_last);
464 ltc3335_counter_test_time += (hw_timer_new - ltc3335_hw_timer_last);
467 ltc3335_hw_timer_last = hw_timer_new;
468 ltc3335_hw_counter_last = hw_counter_new;
479 static bool_t ltc3335_set_subaddress(uint8_t subaddress)
482 uint8_t ltc3335_data[2];
485 ltc3335_data[0] = (LTC3335_BASE_ADDRESS << 1) | LTC3335_WRITE_BIT;
486 ltc3335_data[1] = subaddress;
488 success = PIC16F_I2C_Start(
false);
489 success &= PIC16F_I2C_Write(ltc3335_data,
sizeof(ltc3335_data));
493 ltc3335_subaddress_last = subaddress;
500 static bool_t ltc3335_set_register(uint8_t subaddress, uint8_t* ltc3335_data)
504 Status_LTC3335_Comm_Set();
507 success = ltc3335_set_subaddress(subaddress);
509 success &= PIC16F_I2C_Write(ltc3335_data,
sizeof(ltc3335_data));
510 success &= PIC16F_I2C_Stop();
512 if(success ==
false) ltc3335_report_error(subaddress,
true);
518 static bool_t ltc3335_get_register(uint8_t subaddress, uint8_t* ltc3335_data)
522 Status_LTC3335_Comm_Set();
525 if(subaddress != ltc3335_subaddress_last)
527 success = ltc3335_set_subaddress(subaddress);
528 success &= PIC16F_I2C_Start(
true);
532 success = PIC16F_I2C_Start(
false);
535 *ltc3335_data = ((LTC3335_BASE_ADDRESS << 1) | LTC3335_READ_BIT);
536 success &= PIC16F_I2C_Write(ltc3335_data,
sizeof(uint8_t));
537 success &= PIC16F_I2C_Read(ltc3335_data,
sizeof(uint8_t));
538 success &= PIC16F_I2C_Stop();
540 if(success ==
false) ltc3335_report_error(subaddress,
false);
546 static uint8_t ltc3335_encode_register_a(bool_t enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage, uint8_t prescaler)
548 return ((enabled ==
true) ? (1 << 7) : 0) | (voltage << 4) | prescaler;
552 static void ltc3335_decode_register_a(uint8_t register_a)
554 ltc3335_voltage_selection_enabled_last = ((register_a & MASK(1, 7)) == MASK(1, 7));
555 ltc3335_voltage_selection_last = ((register_a & MASK(3, 4)) >> 4);
556 ltc3335_prescaler_last = register_a & MASK(4, 0);
561 static void ltc3335_report_error(uint8_t subaddress, bool_t set)
563 uint8_t error_bytes[2];
564 error_bytes[0] = subaddress;
565 error_bytes[1] = (uint8_t) set;
566 Error_Data_Set(ERROR_CODE_LTC3335_I2C_FAIL, error_bytes,
sizeof(error_bytes));
bool_t LTC3335_Set_Prescaler(uint8_t prescaler)
Sets the specified prescaler setting for the LTC3335.
bool_t LTC3335_Get_Voltage(bool_t *enabled, LTC3335_OUTPUT_VOLTAGE_TYPE *voltage)
Get whether the software control of the LTC3335 output voltage is enabled/disabled.
bool_t LTC3335_Set_Counter_Test(bool_t enabled)
Enables/Disables the LTC3335 Counter Test feature.
bool_t LTC3335_Get_Counter_Test_Results(bool_t *enabled, uint32_t *edge_count, uint32_t *time)
Gets whether the LTC3335 Counter Test feature is Enabled/Disabled.
bool_t LTC3335_Set_Voltage(bool_t enabled, LTC3335_OUTPUT_VOLTAGE_TYPE voltage)
Enables/Disables software control of the LTC3335 output voltage.
bool_t LTC3335_Get_Alarm_Threshold(uint8_t *threshold)
Gets the alarm threshold from the LTC3335.
bool_t LTC3335_Get_Alarms(LTC3335_ALARM_TYPE *alarms)
Gets the alarms active from the LTC3335.
unsigned ac_on_time_overflow
AC(ON) time operating fault (tAC > tFS) due to improperly chosen inductor value timing out the AC(ON)...
bool_t LTC3335_Get_Prescaler(uint8_t *prescaler)
Gets the prescaler setting from the LTC3335.
unsigned coulomb_counter_overflow
Coulomb counter operating fault due to an improperly chosen prescalar causing the ripple counter to o...
void LTC3335_Init(void)
Initializes the LTC3335 driver.
Firmware Driver Header File for LTC3335 Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter...
bool_t LTC3335_Set_Alarm_Threshold(uint8_t threshold)
Sets the specified alarm threshold for the LTC3335.
bool_t LTC3335_Reset_Counter_Test(void)
Resets the number of edges and the amount of time stored for the Counter Test feature.
unsigned alarm_trip
Accumulator value has met or exceeded the alarm threshold value.
#define LTC3335_DVCC
DVCC pin must be high for LTC3335 to communicate.
bool_t LTC3335_Get_Accumulator(uint8_t *accumulator)
Gets the specified accumulator value from the LTC3335.
bool_t LTC3335_Clear_Int(void)
Sends the command to clear the INT condition.
void LTC3335_Counter_Test_Task(void)
Task that must be run periodically, for the edges and time to be stored for the LTC3335 Counter Test ...
The alarm conditions which cause the LTC3335 to activate the /INT pin.
bool_t LTC3335_Set_Accumulator(uint8_t accumulator)
Sets the specified accumulator value for the LTC3335.
#define LTC3335_PRESCALER_MAX
the maximum prescaler that be selected for the LTC3335.
bool_t LTC3335_Get_Counter_Test(bool_t *enabled)
Gets whether the LTC3335 Counter Test feature is Enabled/Disabled.