//SUBLIME 3 TEXTFOLDING ctrl + shift + [  FOLDS and ctrl + shift + ] UNFLODS


/**
 * Copyright (c) 2009 - 2017, Nordic Semiconductor ASA
 * 
 * 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, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, 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 Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
 * 
 */

#include "tdk_ERmini.h"

//TWI VARIABLES
/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0
#define MAX_PENDING_TRANSACTIONS    5
NRF_TWI_MNGR_DEF(m_nrf_twi_mngr, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);

uint8_t tx_reg[256];
uint8_t rx_reg[256];


void ERmini_init(void)     
{

     uint32_t err_code;

    nrf_drv_twi_config_t const config = {
       .scl                = mpu_SCL,
       .sda                = mpu_SDA,
       .frequency          = NRF_DRV_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_twi_mngr_init(&m_nrf_twi_mngr, &config);
    APP_ERROR_CHECK(err_code);

    uint32_t err_codeI;

    err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_codeI);

    nrf_drv_clock_lfclk_request(NULL);

}

/**
 * @brief TWI events handler.
 */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    m_xfer_done = true;
}

int mpu_i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t length, uint8_t *data)
{
    unsigned char new_data[length + 1];
    memcpy(new_data + 1, data, length);
    new_data[0] = reg_addr;

    nrf_twi_mngr_transfer_t transfers[] = 
    {
        NRF_TWI_MNGR_WRITE(slave_addr, new_data, length + 1, 0)
    };

    nrf_twi_mngr_transaction_t NRF_TWI_MNGR_BUFFER_LOC_IND transaction =
    {
        .callback            = NULL,
        .p_user_data         = NULL,
        .p_transfers         = transfers,
        .number_of_transfers = sizeof(transfers) / sizeof(transfers[0])
    };
    
    //APP_ERROR_CHECK(nrf_twi_mngr_perform(&m_nrf_twi_mngr, &transaction));
    APP_ERROR_CHECK(nrf_twi_mngr_schedule(&m_nrf_twi_mngr, &transaction));
     return(0);
}

/**
 * @brief Function for reading data from MPU9250.
 */
int mpu_i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t length, uint8_t *data)
{
    if (length == 0)
        return NRF_SUCCESS;

    nrf_twi_mngr_transfer_t const transfers[] =
    {
        NRF_TWI_MNGR_WRITE(slave_addr, &reg_addr, 1, NRF_TWI_MNGR_NO_STOP),
        NRF_TWI_MNGR_READ (slave_addr, data, length, 0)
    };

    nrf_twi_mngr_transaction_t NRF_TWI_MNGR_BUFFER_LOC_IND transaction =
    {
        .callback            = null,
        .p_user_data         = NULL,
        .p_transfers         = transfers,
        .number_of_transfers = sizeof(transfers) / sizeof(transfers[0])
    };
    
    //APP_ERROR_CHECK(nrf_twi_mngr_perform(&m_nrf_twi_mngr, &transaction));
    APP_ERROR_CHECK(nrf_twi_mngr_schedule(&m_nrf_twi_mngr, &transaction));
    return 0;
}

void no_func1(unsigned long * timestamp)
{
   
}

void ms_get(long unsigned int *timestamp)
{
    *timestamp = timestamp_func();
}

uint16_t USART_ReceiveData(uint16_t temp_var)
{
  /* Receive Data */
  return (temp_var);
}

/*lint --flb "Leave library region" */
