/*
 *  Copyright (c) 2022, Kunbus GmbH
 *  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.
 *
 *  3. Neither the name of the copyright holder nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 *  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 HOLDER 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.
 *
 */
#ifndef BOARD_I2C_H
#define BOARD_I2C_H

#include "main.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf.h"

/*
 * Turn A into a string literal without expanding macro definitions
 * (however, if invoked from a macro, macro arguments are expanded).
 */
#define STRINGIZE_NX(A) #A

/*
 * Turn A into a string literal after macro-expanding it.
 */
#define STRINGIZE(A) STRINGIZE_NX(A)

#define COMBINE_STR_VAR(p1, p2)       p1 ## p2

#define COMBINE_I2C_NAME(p1, p2)      COMBINE_STR_VAR(p1, p2)

#define MAX_NUM_SLAVES 4

typedef void (*i2cCallbackHandler_t)(uint8_t errorCode);

typedef enum errorCode_t
{
  I2C_SUCCESS   = 0x00000000u,
  I2C_OVERRUN   = 0x00000001u,
  I2C_ANACK     = 0x00000002u,
  I2C_DNACK     = 0x00000004u,
  I2C_BUSY      = 0x10000000u,
} errorCode_t;

typedef enum i2CClockRate
{
#ifdef USE_TWIM
    K100 = TWIM_FREQUENCY_FREQUENCY_K100 , /*!< 100 kbps */
    K250 = TWIM_FREQUENCY_FREQUENCY_K250 , /*!< 250 kbps */
    K400 = TWIM_FREQUENCY_FREQUENCY_K400 , /*!< 400 kbps */
#else
#ifdef USE_TWI
    K100 = TWI_FREQUENCY_FREQUENCY_K100,  /*!< 100 kbps */
    K250 = TWI_FREQUENCY_FREQUENCY_K250, /*!< 250 kbps */
    K400 = TWI_FREQUENCY_FREQUENCY_K400,  /*!< 400 kbps (actual rate 410.256 kbps) */
#endif
#endif
}i2CClockRate;

typedef enum nextOp_t
{
  TX = 0,
  RX = 1,
}nextOp_t;

typedef struct i2cTransaction_t
{
  bool busy;
  i2cCallbackHandler_t callback;
  uint8_t lastUsedAddress;
  uint8_t *dataRx;
  uint8_t lengthRX;
  uint8_t countTx;
  uint8_t *dataTx;
  uint8_t lengthTX;
  uint8_t countRx;
  nextOp_t nextOp;
  errorCode_t errorCode;
}i2cTransaction_t;


extern void nRF52_I2CInit(uint32_t clkPin, uint32_t dataPin, i2CClockRate clockRate, i2cCallbackHandler_t callback);
extern void nRF52_I2CSetAddress(uint8_t address);
extern void nRF52_I2CSetPins(uint32_t clkPin, uint32_t dataPin);
extern bool nRF52_I2CAddressExists(uint8_t address);
extern errorCode_t nRF52_I2C_TX(uint8_t address, uint8_t reg, uint8_t *data, uint8_t length);
extern errorCode_t nRF52_I2C_RX(uint8_t address, uint8_t reg, uint8_t *data, uint8_t length);
extern errorCode_t nRF52_I2C_FREE_TX(uint8_t address, uint8_t *data, uint8_t length);
extern errorCode_t nRF52_I2C_FREE_RX(uint8_t address, uint8_t *data, uint8_t length);


#endif