#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "SEGGER_RTT.h"
#include "ADS118.h"
#include "boards.h"
#include "nrf_gpio.h"


#define SPI_INSTANCE  0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
static  uint8_t m_tx_buf[4]={0x05,0x8b, 0x00,0x00};
static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
static  uint8_t m_rx_buf[4];
static  uint8_t m_rx_buf1[4];

static  uint8_t m_tx_buf1[4]={0x00,0x00,0x52,0x7a};

static const uint8_t m_length1 = sizeof(m_tx_buf1);  

void adcinit()
{
  nrf_gpio_cfg_output(31);
  nrf_gpio_cfg_output(29);
	 
	 nrf_delay_us(50);
 
	
	nrf_gpio_pin_clear(31);
	nrf_gpio_pin_clear(29);

  nrf_delay_us(100);
	nrf_drv_spi_transfer(&spi, m_tx_buf1, m_length1, m_rx_buf1, m_length1);
	//nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length);
	

	nrf_gpio_pin_set(31); 

}
/****************************************************************************************************************
 * ADC data read function

   \param none.

 * return: ADC conversion result
 ****************************************************************************************************************/

uint32_t ads1208ReadData()
{
    uint8_t pui32DataTx[3] = {0}, pui32DataRx[4] = {0,0,0,0};
   
    nrf_gpio_pin_clear(29);
	  nrf_gpio_pin_clear(31);

   
    nrf_drv_spi_transfer(&spi,  pui32DataTx, m_length1, pui32DataRx, m_length1);
		
    int16_t ADCdatainbits = (pui32DataRx[0]|pui32DataRx[1]);
    nrf_gpio_pin_set(31);
    SEGGER_RTT_printf(0," Data Received1: %x\n",pui32DataRx[0]);
		SEGGER_RTT_printf(0," Data Received2: %x\n",pui32DataRx[1]);
		SEGGER_RTT_printf(0," Data Received3: %x\n",pui32DataRx[2]);
		SEGGER_RTT_printf(0," Data Received4: %x\n",pui32DataRx[3]);
  	
  		

    
    return (ADCdatainbits);
}
