/*
 Linear Technology DC2343A Demonstration Board.
 Driver File for LTC3335 Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter.
 All datasheet references in this file refer to Linear Technology document 3335p.pdf

 @verbatim
    Demonstration Circuit DC2343A is a complete system level solution for a Nanopower Buck-Boost
    DC/DC with Integrated Coulomb Counter.  The DC2343A contains a PIC16F1459 embedded processor
    for communication to the PC over USB and the LTC3335 via its I2C port.  The GUI is capable of
    reading and writing all the control registers on the LTC3335 as well as displaying and
    resetting all its alarm registers.  The firmware and software for the embedded system and the GUI
    are available at the LTC3335 solutions page located at http://www.linear.com/product/LTC3335#demoboards.
    The DC2343A uses two Analog-to-Digital Convert-er channels to sample the Battery and Output voltages.
    The voltage samples improve the functionality of the GUI, and allow optimal software correction to the
    measured coulombs.  By adding the software correction the first order known errors are compensated
    for over the operating range and the resultant coulomb count is accurate to within 3%.

    The DC2343A contains an LTC3335 which is a high efficiency, low quiescent current (680nA) buck-boost
    DC/DC converter with an integrated precision cou-lomb counter which monitors accumulated battery discharge
    in long life battery powered applications. The buck-boost can operate down to 1.8V on its input and
    provides eight pin selectable output voltages with up to 50mA of output current. The coulomb counter
    stores the accumulated battery discharge in an internal register accessible via an I2C interface. The
    LTC3335 features a programmable discharge alarm threshold. When the threshold is reached, an interrupt
    is generated at the IRQ pin. To accommodate a wide range of battery types and sizes, the peak input
    current can be selected from as low as 5mA to as high as 250mA and the full-scale coulomb counter has a
    range from 1.1mAh (with 5mA IPEAK) to 1793Ah (with 250mA IPEAK).
 @endverbatim

 http://www.linear.com/product/LTC3335

 REVISION HISTORY
 $Revision: 687 $
 $Date: 2014-09-05 14:51:22 -0400 (Fri, 05 Sep 2014) $

 Copyright (c) 2014, Linear Technology Corp.(LTC)
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 The views and conclusions contained in the software and documentation are those
 of the authors and should not be interpreted as representing official policies,
 either expressed or implied, of Linear Technology Corp.

*/

/*! @file
    Firmware Driver Header File for LTC3335 Nanopower Buck-Boost DC/DC with Integrated Coulomb Counter.

    This driver was designed to provide the following services to application code:
       1. Perform the basic register commands necessary to access the features of the LTC3335.
       2. Minimize I2C communication in an effort to minimize power consumption.
       3. Use the Counter Test feature of the LTC3335 to provide a real time battery current measurement.

    Software correction was not included in this driver due to Program Memory limitations in the PIC16F1459.
    See the LTC3335.vb file in with the DC2343A GUI source code for an example of software correction of the
    LTC3335 coulomb count.
*/

#ifndef __adi_ltc3335_H__
#define __adi_ltc3335_H__

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Includes
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "../common.h"

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Definitions
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//! @name LTC3335 Driver Properties
//! @{
#define ADI_LTC3335_BAUD_RATE       400000 //!< in Hz, Max Clock Operating Frequency (fSCL from datasheet page 5)
#define ADI_LTC3335_BASE_ADDRESS    0x64   //!< BASE_ADDRESS for LTC3335.
#define ADI_LTC3335_PRESCALER_MAX   15     //!< the maximum prescaler that be selected for the LTC3335.
//! @}

//! @name Voltage Settings
//! @{
//! Options available for setting output voltage via I2C.
typedef enum
{
    ADI_LTC3335_OUTPUT_VOLTAGE_1_8V,
    ADI_LTC3335_OUTPUT_VOLTAGE_2_5V,
    ADI_LTC3335_OUTPUT_VOLTAGE_2_8V,
    ADI_LTC3335_OUTPUT_VOLTAGE_3_0V,
    ADI_LTC3335_OUTPUT_VOLTAGE_3_3V,
    ADI_LTC3335_OUTPUT_VOLTAGE_3_6V,
    ADI_LTC3335_OUTPUT_VOLTAGE_4_5V,
    ADI_LTC3335_OUTPUT_VOLTAGE_5_0V,
    ADI_LTC3335_NUM_OUTPUT_VOLTAGES,
		ADI_LTC3335_OUTPUT_VOLTAGE_INVALID
} adi_ltc3335_outputVoltage_t;
//! @}

//! @name IPeak Settings
//! @{
//! Options available for IPeak setting.
//! NOTE! This can not be set via I2C.  It can only be set with resistors on pins 13-15
//! of the LTC3335, along with the an appropriately sized inductor.  In order to
//! translate accumulator values to coulombs, and Counter Test results to amps,
//! this setting must be known.
typedef enum
{
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_5MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_10MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_15MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_25MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_50MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_100MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_150MA,
    ADI_LTC3335_IPEAK_CONFIGURATION_IPEAK_250MA,
    ADI_LTC3335_NUM_IPEAK_CONFIGURATIONS
} adi_ltc3335_iPeakConfiguration_t;
//! @}

//! The alarm conditions which cause the LTC3335 to activate the /INT pin.
typedef struct
{
    unsigned ac_on_time_overflow        :1;     //!< AC(ON) time operating fault (tAC > tFS) due to improperly chosen inductor value timing out the AC(ON) measurement.
    unsigned coulomb_counter_overflow   :1;     //!< Coulomb counter operating fault due to an improperly chosen prescalar causing the ripple counter to overflow.
    unsigned alarm_trip                 :1;     //!< Accumulator value has met or exceeded the alarm threshold value.
    unsigned unused                     :5;
} adi_ltc3335_alarm_t;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Global Data
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Global Prototypes
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//! Initializes the LTC3335 driver.
//! @return void
void adi_ltc3335_Init(void);

//! Enables/Disables software control of the LTC3335 output voltage.
//! If software control is enabled, the voltage is set to the specified setting.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Set_Voltage(bool enabled,                      //!< TRUE to enable software control of output voltage, FALSE to disable software control (output voltage set by pins 3-5).
                          adi_ltc3335_outputVoltage_t voltage  //!< The output voltage setting if software control is enabled.
                        );


//! Sets the specified prescaler setting for the LTC3335.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Set_Prescaler(uint8_t prescaler                  //!< The prescaler setting to set for the LTC3335.
                          );

//! Sets the specified alarm threshold for the LTC3335.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Set_Alarm_Threshold(uint8_t threshold            //!< The alarm threshold to set for the LTC3335.
                                );

//! Sets the specified accumulator value for the LTC3335.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Set_Accumulator(uint8_t accumulator              //!< The accumulator value to set for the LTC3335.
                            );

//! Gets the specified accumulator value from the LTC3335.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Get_Accumulator(uint8_t* accumulator             //!< The current LTC3335 accumulator value.
                            );

//! Gets the alarms active from the LTC3335.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Get_Alarms(adi_ltc3335_alarm_t* alarms            //!< Bitmap containing the LTC3335 active alarms.
                       );

//! Sends the command to clear the INT condition.
//! NOTE!  Additional registers will also need to be rewritten in order for the INT condition
//!        to not be immediately reset.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Clear_Int(void);

/*

//! Enables/Disables the LTC3335 Counter Test feature.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Set_Counter_Test(bool enabled                  //!< TRUE to enable Counter Test feature, FALSE to disable Counter Test feature.
                             );

//! Gets whether the LTC3335 Counter Test feature is Enabled/Disabled.
bool adi_ltc3335_Get_Counter_Test(bool* enabled                 //!< TRUE if Counter Test is enabled, FALSE is Counter Test is disabled.
                             );


//! Gets whether the LTC3335 Counter Test feature is Enabled/Disabled.
//! Gets the number of edges and the amount of time since the Counter Test
//! was last reset, which allows edges/sec to be calculated.  The edges/sec is proportional to
//! battery current.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Get_Counter_Test_Results(bool* enabled,        //!< TRUE if Counter Test is enabled, FALSE is Counter Test is disabled.
                                      uint32_t* edge_count,   //!< The number of edges counted since the Counter Test was last reset.
                                      uint32_t* time          //!< The number of timer ticks since the Counter Test was last reset.
                                     );

//! Resets the number of edges and the amount of time stored for the Counter Test feature.
//! @return TRUE if the LTC3335 communication was successful.
bool adi_ltc3335_Reset_Counter_Test(void);

//! Task that must be run periodically, for the edges and time to be stored for the
//! LTC3335 Counter Test feature.
//! @return TRUE if the LTC3335 communication was successful.
void adi_ltc3335_Counter_Test_Task(void);

*/

#endif
