/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 */
/*******************************************************************************
* Copyright (C) 2014 Spansion LLC. All Rights Reserved. 
*
* This software is owned and published by: 
* Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA  94088-3453 ("Spansion").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND 
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with Spansion 
* components. This software is licensed by Spansion to be adapted only 
* for use in systems utilizing Spansion components. Spansion shall not be 
* responsible for misuse or illegal use of this software for devices not 
* supported herein.  Spansion is providing this software "AS IS" and will 
* not be responsible for issues arising from incorrect user implementation 
* of the software.  
*
* SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS), 
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING, 
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED 
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED 
* WARRANTY OF NONINFRINGEMENT.  
* SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT, 
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT 
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, 
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR 
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA, 
* SAVINGS OR PROFITS, 
* EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED 
* FROM, THE SOFTWARE.  
*
* This software may be replicated in part or whole for the licensed use, 
* with the restriction that this Disclaimer and Copyright notice must be 
* included with each copy of this software, whether used in part or whole, 
* at all times.  
*/

#include "tmp102.h"
#include "twi_master.h"
#include "nrf_delay.h"

/*lint ++flb "Enter library region" */

#define TMP102_BASE_ADDRESS 0x90 //!< 4 MSBs of the TMP102 TWI address

#define TMP102_ONESHOT_MODE 0x01 //!< Bit in configuration register for 1-shot mode 
#define TMP102_CONVERSION_DONE 0x80 //!< Bit in configuration register to indicate completed temperature conversion

static uint8_t m_device_address; //!< Device address in bits [7:1]

const uint8_t command_access_memory = 0x17; //!< Reads or writes to 256-byte EEPROM memory
const uint8_t command_access_config = 0x01; //!< Reads or writes configuration data to configuration register 
const uint8_t command_read_temp = 0x00; //!< Reads last converted temperature value from temperature register
const uint8_t command_start_convert_temp = 0xEE; //!< Initiates temperature conversion.
const uint8_t command_stop_convert_temp = 0x22; //!< Halts temperature conversion.

/**
 * @brief Function for reading the current configuration of the sensor.
 *
 * @return uint8_t Zero if communication with the sensor failed. Contents (always non-zero) of configuration register (@ref TMP102_ONESHOT_MODE and @ref TMP102_CONVERSION_DONE) if communication succeeded.
 */
static uint8_t tmp102_config_read(void)
{
  uint8_t config = 0;
    
  // Write: command protocol
  if (twi_master_transfer(m_device_address, (uint8_t*)&command_access_config, 1, TWI_DONT_ISSUE_STOP))
  {
    if (twi_master_transfer(m_device_address | TWI_READ_BIT, &config, 1, TWI_ISSUE_STOP)) // Read: current configuration
    {
      // Read succeeded, configuration stored to variable "config"
    }
    else
    {
      // Read failed
      config = 0;
    }
  } 

  return config;
}

bool tmp102_init(uint8_t device_address)
{
  bool transfer_succeeded = true;
  m_device_address = TMP102_BASE_ADDRESS + (uint8_t)(device_address << 1);

  return transfer_succeeded;
}

bool tmp102_start_temp_conversion(void)
{
  return twi_master_transfer(m_device_address, (uint8_t*)&command_start_convert_temp, 1, TWI_ISSUE_STOP);
}

bool tmp102_is_temp_conversion_done(void)
{
  uint8_t config = tmp102_config_read();

  if (config & TMP102_CONVERSION_DONE)
  {
    return true;
  }
  else
  {
    return false;
  }
}

bool tmp102_temp_read(int8_t *temperature_in_celcius, int8_t *temperature_fraction)
{
  bool transfer_succeeded = false;

  // Write: Begin read temperature command
  if (twi_master_transfer(m_device_address, (uint8_t*)&command_read_temp, 1, TWI_DONT_ISSUE_STOP))
  {
    uint8_t data_buffer[2];

    // Read: 2 temperature bytes to data_buffer
    if (twi_master_transfer(m_device_address | TWI_READ_BIT, data_buffer, 2, TWI_ISSUE_STOP)) 
    {
      *temperature_in_celcius = (int8_t)data_buffer[0];
      *temperature_fraction = (int8_t)data_buffer[1];
      
      transfer_succeeded = true;
    }
  }

  return transfer_succeeded;
}

/*lint --flb "Leave library region" */ 
