///
/// @file i2cx-bb.h
/// @authors k. headley
/// @date 05 dec 2019

/// I2C  definitions for bit-banged implementation

/// @sa doxygen-examples.c for more examples of Doxygen markup


/////////////////////////
// Terms of use
/////////////////////////
/*
 Copyright Information
  
 Copyright 2002-2019 MBARI
 Monterey Bay Aquarium Research Institute, all rights reserved.
 
 Terms of Use
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version. You can access the GPLv3 license at
 http://www.gnu.org/licenses/gpl-3.0.html
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details
 (http://www.gnu.org/licenses/gpl-3.0.html)
 
 MBARI provides the documentation and software code "as is", with no warranty,
 express or implied, as to the software, title, non-infringement of third party
 rights, merchantability, or fitness for any particular purpose, the accuracy of
 the code, or the performance or results which you may obtain from its use. You
 assume the entire risk associated with use of the code, and you agree to be
 responsible for the entire cost of repair or servicing of the program with
 which you are using the code.
 
 In no event shall MBARI be liable for any damages, whether general, special,
 incidental or consequential damages, arising out of your use of the software,
 including, but not limited to, the loss or corruption of your data or damages
 of any kind resulting from use of the software, any prohibited use, or your
 inability to use the software. You agree to defend, indemnify and hold harmless
 MBARI and its officers, directors, and employees against any claim, loss,
 liability or expense, including attorneys' fees, resulting from loss of or
 damage to property or the injury to or death of any person arising out of the
 use of the software.
 
 The MBARI software is provided without obligation on the part of the
 Monterey Bay Aquarium Research Institute to assist in its use, correction,
 modification, or enhancement.
 
 MBARI assumes no responsibility or liability for any third party and/or
 commercial software required for the database or applications. Licensee agrees
 to obtain and maintain valid licenses for any additional third party software
 required.
 */

// Always do this
#ifndef I2CX_BB_H
#define I2CX_BB_H

/////////////////////////
// Includes 
/////////////////////////
#ifdef WITH_PIC24F_CURIOSITY
#include "bbcommon.h"
#endif // WITH_PIC24F_CURIOSITY
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "libpic30.h"
#include "pic24.h"

/////////////////////////
// Macros
/////////////////////////

// Bit-banged I2C bus 1 clock, data pins
#define I2CP_I2C1_SDC_PIN 2
#define I2CP_I2C1_SDA_PIN 9

// Bit-banged I2C bus 2 clock, data pins
#define I2CP_I2C2_SDC_PIN 4
#define I2CP_I2C2_SDA_PIN 6

/////////////////////////
// Type Definitions
/////////////////////////

// compiler-specific delay function (use built-in)
#define I2CX_DELAY() __delay_us(5) // Nop()
#define I2CX_DELAY_USEC(x) __delay_us(x) // Nop()

#define I2CX_ADDR_WR(x) (x<<1)
#define I2CX_ADDR_RD(x) ((x<<1) | 0x01)
#define I2CX_ADDR_ISWR(x) (( (x&0x01) == 0) ? true : false )
#define I2CX_ADDR_ISRD(x) (( (x&0x01) == 1) ? true : false )
#define I2CX_ADDR(x) ((x>>1)&0xFF)

#define I2CX_LSB(w) (byte)((w & 0x00FF))
#define I2CX_MSB(w) (byte)((w & 0xFF00) >> 8)

#define I2CX_BIT(i)     (1 << i)
#define I2CX_SCL(p)     (1 << p->scl_bit)
#define I2CX_SDA(p)     (1 << p->sda_bit)
#define I2CX_ANSEL(p)  *(p->ans)
#define I2CX_ODC(p)    *(p->odc)
#define I2CX_PORT(p)   *(p->port)
#define I2CX_TRIS(p)   *(p->tris)

#define I2CX_SET_SCL(p) I2CX_TRIS(p) |= I2CX_SCL(p)
#define I2CX_CLR_SCL(p) I2CX_TRIS(p) &= ~I2CX_SCL(p)
#define I2CX_SCL_IS_LOW(p) ( (I2CX_PORT(p) & I2CX_SCL(p)) == 0 ? true : false)
#define I2CX_SCL_IS_HIGH(p) ( (I2CX_PORT(p) & I2CX_SCL(p)) == 0 ? false : true)

#define I2CX_SET_SDA(p) I2CX_TRIS(p) |= I2CX_SDA(p)
#define I2CX_CLR_SDA(p) I2CX_TRIS(p) &= ~I2CX_SDA(p)
#define I2CX_SDA_IS_LOW(p) ( (I2CX_PORT(p) & I2CX_SDA(p)) == 0 ? true : false)
#define I2CX_SDA_IS_HIGH(p) ( (I2CX_PORT(p) & I2CX_SDA(p)) == 0 ? false : true)

// PIC24 bit-banged I2C bus (state)
#pragma pack(push,1)
///@typedef struct i2cbus_s i2cbus_t
///@brief PIC24 bitbanged I2C bus
typedef struct i2c_bb_bus_s{
    /// @var i2cbus_s::ans
    /// @brief ANSEL (analog select) register reference
    volatile  uint16_t *ans;
    /// @var i2cbus_s::odc
    /// @brief ODC (open drain config) register reference (1 open drain)
    volatile  uint16_t *odc;
    /// @var i2cbus_s::port
    /// @brief IO Port register reference
    volatile  uint16_t *port;
    /// @var i2cbus_s::tris
    /// @brief TRIS (tri state/direction) register reference(1:input)
    volatile  uint16_t *tris;
    /// @var i2cbus_s::scl_bit
    /// @brief SCL bit (clock IO bit)
    int scl_bit;
    /// @var i2cbus_s::sda_bit
    /// @brief SDA bit (data IO bit)
    int sda_bit;
    /// @var i2cbus_s::flags
    /// @brief i2c state flags
    i2cx_flags_t flags;
    /// @var i2cbus_s::addr
    /// @brief i2c secondary self address
    byte addr;
}i2c_bb_bus_t;
#pragma pack(pop)

/////////////////////////
// Exports
/////////////////////////

#endif // THIS_FILE
