/******************************************************************************
 * @copyright
 * Copyright (c) 2026 NeuroGenesis Diagnostics Inc.
 *
 * This software is proprietary and confidential.
 ******************************************************************************
 * @file i2cDrv.c
 *****************************************************************************/

#include "i2cDrv.h"
#include "sdk_config.h"
#include "nrf.h"
#include "sdk_common.h"
//#include "nrf_drv_twi.h"
#include "nrf_twi_mngr.h"


 #define TWI_INSTANCE_ID      0          // TWIM0
#define TWI_QUEUE_LENGTH     20  
#define I2C_SCL_PIN          26
#define I2C_SDA_PIN          27

#define NRF_TWI_MNGR_OP_WRITE(addr) ((addr) << 1)
#define NRF_TWI_MNGR_OP_READ(addr)  (((addr) << 1) | 0x01)

// Create I2C manager instance
NRF_TWI_MNGR_DEF(m_twi_mngr, TWI_QUEUE_LENGTH, TWI_INSTANCE_ID);

void twi_init(void)
{
    nrf_drv_twi_config_t twi_config = {
        .scl                = I2C_SCL_PIN,
        .sda                = I2C_SDA_PIN,
        .frequency          = NRF_TWI_FREQ_400K,   
        .interrupt_priority = APP_IRQ_PRIORITY_LOW,
        .clear_bus_init     = false,
        .hold_bus_uninit    = false
    };

    ret_code_t err_code = nrf_twi_mngr_init(&m_twi_mngr, &twi_config);
    APP_ERROR_CHECK(err_code);
}
