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

/// I2C API type definitions

/// @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_TYPES_H
#define I2CX_TYPES_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
/////////////////////////

// delay some # of milliseconds
#define DELAY_MS(ms)  __delay_ms((unsigned long) ms)
// delay some number of microseconds
#define DELAY_US(us)  __delay_us((unsigned long) us)

// handle macros
#define I2CP_HND_IS_BB(hnd) (hnd==I2CP_HND_BB1 || hnd==I2CP_HND_BB2 ? true : false)
#define I2CP_HND_IS_MMC(hnd) (hnd==I2CP_HND_MMC1 || hnd==I2CP_HND_MMC2 ? true : false)

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

/// @typedef uint16_t i2c_addr_t
/// @brief i2c address type
typedef uint16_t i2c_addr_t;

/// @typedef uint32_t i2cx_flags_t
/// @brief i2c state flag type
typedef uint32_t i2cx_flags_t;

/// @var I2CX_ESTART
/// @brief I2C flag: start error
#define I2CX_ESTART 0x00000001
/// @var I2CX_EDATA
/// @brief I2C flag: data error
#define I2CX_EDATA  0x00000002
/// @var I2CX_EREAD
/// @brief I2C flag: read error
#define I2CX_EREAD  0x00000004
/// @var I2CX_EWRITE
/// @brief I2C flag: write error
#define I2CX_EWRITE 0x00000008
/// @var I2CX_START
/// @brief I2C flag: start condition
#define I2CX_START  0x00000010
/// @var I2CX_DATA
/// @brief I2C flag: data bit value
#define I2CX_DATA   0x00000020
/// @var I2CX_STOP
/// @brief I2C flag: stop condition
#define I2CX_STOP   0x00000040
/// @var I2CX_RW
/// @brief I2C flag: read/write bit
#define I2CX_RW     0x00000080
/// @var I2CX_ALL
/// @brief I2C flag: mask (all)
#define I2CX_ALL    0x7FFFFFFF
/// @var I2CX_DEBUG
/// @brief I2C flag: debug mode
#define I2CX_DEBUG  0x80000000

// flag manipulation macros
#define I2CX_FLAG_SET(p,m) (p->flags |= (m))
#define I2CX_FLAG_CLR(p,m) (p->flags &= ~(m))
#define I2CX_FLAG_ISSET(p,m) ( ((p->flags&m) != 0) ? true : false )
#define I2CX_FLAG_ISCLR(p,m) ( ((p->flags&m) == 0) ? true : false )

// address manipulation macros
#define I2CX_LSB16(w) (byte)(w&0xFF)
#define I2CX_MSB16(w) (byte)((w&0xFF00)>>8)

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

#endif // THIS_FILE

