#include <stdint.h>
#include <stdbool.h>
#include "nrf_peripherals.h"
#include "nrf_erratas.h"
#include "nrf.h"
#include "system_nrf52840.h"
#include "main.h"
#include <nrfx_twim.h>
#include <nrfx_twi.h>

#include <nRF52_I2C.h>

#ifdef CHECK_PHERIPHERIE
#define DELAY (6400000)
/** 
 uses to test the periphery
*/
#define i2CAddress (0x68)

uint32_t delay = DELAY;

uint8_t i2CRxBuffer[32];
uint8_t i2CTxBuffer[32];
uint8_t dataLength;

void testPeriphery();

#ifdef NRF_Driver
nrfx_err_t volatile status;

#ifdef USE_TWIM
static  nrfx_twim_t instance;
static nrfx_twim_config_t  config;
static void vI2CCallback(nrfx_twim_evt_t const * p_event,
                   void *                 p_context)
{
  (void)p_event;
  (void)p_context;
};
#else
#ifdef USE_TWI
static  nrfx_twi_t instance;
static nrfx_twi_config_t config;
static void vI2CCallback(nrfx_twi_evt_t const * p_event,
                   void *                 p_context)

{
  (void)p_event;
  (void)p_context;
};
#endif
#endif

#else 

static void vI2CCallback(uint8_t errorCode)
{
  (void)errorCode;
}

#endif
#endif

int main()
{
    SystemInit();
// for test only. 
#ifdef CHECK_PHERIPHERIE
// use nordic drivers
#ifdef NRF_Driver
#ifdef USE_TWIM   
    instance.drv_inst_idx = 0;
    instance.p_twim = (NRF_TWIM_Type*)(0x40004000);
    config.frequency = 0x06200000UL;//NRF_TWIM_FREQ_400K;
    config.scl = HOST_I2C_CLK_PIN_ID;
    config.sda = HOST_I2C_DATA_PIN_ID;
    config.interrupt_priority = 7;
    status = nrfx_twim_init(&instance, &config, vI2CCallback, NULL);
    if(status != NRFX_SUCCESS)
    {
      while(1)
      {
        __asm("NOP");
      }
    }
    
#else
#ifdef USE_TWI
    instance.p_twi = (NRF_TWI_Type*)(0x40004000);
    instance.drv_inst_idx = 1;
    
    config.scl = HOST_I2C_CLK_PIN_ID;
    config.sda = HOST_I2C_DATA_PIN_ID;
    config.frequency = TWI_FREQUENCY_FREQUENCY_K400;
    config.interrupt_priority = 7;
    // be sure the instance is free
    nrfx_twi_uninit(&instance);
    // init instance 
    status = nrfx_twi_init(&instance, &config, vI2CCallback, NULL);
    if(status != NRFX_SUCCESS)
    {
      while(1)
      {
        __asm("NOP");
      }
    }
#endif
#endif
#else
    nRF52_I2CInit(HOST_I2C_CLK_PIN_ID, HOST_I2C_DATA_PIN_ID, K400, vI2CCompleteHandler);
#endif
    testPeriphery();
#else
      while(1)
      {
        __asm("NOP");
      }
#endif

}

#ifdef CHECK_PHERIPHERIE
/** 
 uses to test the periphery
*/
void testPeriphery()
{
  
    i2CTxBuffer[0] = 0x35; 
    i2CTxBuffer[1] = 0x70;  
// use nordic drivers
#ifdef NRF_Driver
#ifdef USE_TWI
    status = nrfx_twi_tx(&instance, i2CAddress, i2CTxBuffer, 2, false);
#else
#ifdef USE_TWIM
    status = nrfx_twim_tx(&instance,
                        i2CAddress,
                        i2CTxBuffer,
                        2,
                        false);
#endif
#endif
// use own driver
#else
    nRF52_I2C_TX(i2CAddress, i2CTxBuffer[0], &i2CTxBuffer[1], 1);
#endif
    // wait some time...
    while(delay > 0)
    {
       delay--;
       __asm("NOP");
    }
   // NVIC_SetPendingIRQ(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
    i2CTxBuffer[0] = 0x01;
    i2CTxBuffer[1] = 0xA0;
    
// use nordic drivers
#ifdef NRF_Driver
#ifdef USE_TWI
    status = nrfx_twi_tx(&instance, i2CAddress, i2CTxBuffer, 2, false);
#else
#ifdef USE_TWIM
    status = nrfx_twim_tx(&instance,
                        i2CAddress,
                        i2CTxBuffer,
                        2,
                        false);
#endif
#endif
// use own driver
#else
    nRF52_I2C_TX(i2CAddress, i2CTxBuffer[0], &i2CTxBuffer[1], 1);
#endif
    // wait some time...
    while(delay > 0)
    {
       delay--;
       __asm("NOP");
    }
    i2CTxBuffer[0] = 0x02;
    i2CTxBuffer[1] = 0x00;
    
// use nordic drivers
#ifdef NRF_Driver
#ifdef USE_TWI
    status = nrfx_twi_tx(&instance, i2CAddress, i2CTxBuffer, 2, false);
#else
#ifdef USE_TWIM
    status = nrfx_twim_tx(&instance,
                        i2CAddress,
                        i2CTxBuffer,
                        2,
                        false);
#endif
#endif
    
// use own driver
#else
    nRF52_I2C_TX(i2CAddress, i2CTxBuffer[0], &i2CTxBuffer[1], 1);
#endif
    
    while(1)
    {

        
    }
}
#endif
