/*******************************************************************************
 * @file	ada2200_spi.c
 * @author	CandyPops Co.
 * @version	V1.0.1
 * @date	2022-09-05
 * @brief   ADA2200 Lock-in Amplifier SPI driver (uses shared SPI2 bus)
 ******************************************************************************/

#include "sdk_common.h"

#include <stdbool.h>
#include "nrf.h"
#include "nrf_gpio.h"
#include "app_error.h"
#include "boards.h"
#include "nrf_delay.h"

#include "nrf_log.h"
#include "ada2200_spi.h"
#include "spi2_bus.h"      /* Shared SPI2 bus */
#include "debug_print.h"

/* Use CS pin from shared bus definition */
#define ADA2200_CS_PIN   SPI2_CS_ADA2200

//static uint8_t ada2200_startR[] ={ 0x00, 0x00, 0x81 };  	/* {addr 16bit, data}, Reset for Defaults */

static uint8_t ada2200_start0[] ={ 0x00, 0x00, 0x18 };  	/* {addr 16bit, data}, Set SDIO input only, Activate SDO */
static uint8_t ada2200_start1[] ={ 0x00, 0x2B, 0x06 };   	/* {addr 16bit, data}, Clock Configuration */
static uint8_t ada2200_start2[] ={ 0x00, 0x2A, 0x18 };		/* {addr 16bit, data}, Enable Mixer, Select SDO output for Pin 13, OFF RCLK. */
static uint8_t ada2200_start3[] ={ 0x00, 0x29, 0x23 };   	/* 0x27 {addr 16bit, data}, Disable SYNCO output, Select SYNCO edge location (Sync timing adjustment) */
static uint8_t ada2200_start4[] ={ 0x00, 0x2C, 0x01 };  	/* {addr 16bit, data}, Enable RCLK output */

static uint8_t ada2200_stop0[]  ={ 0x00, 0x00, 0x18 };  	/* {addr 16bit, data}, Set SDIO input only, Activate SDO */
static uint8_t ada2200_stop1[]  ={ 0x00, 0x2B, 0x06 };   	/* {addr 16bit, data}, Clock Configuration */
static uint8_t ada2200_stop2[]  ={ 0x00, 0x2A, 0x10 };		/* {addr 16bit, data}, Enable Mixer, Select SDO output for Pin 13, OFF RCLK. */
static uint8_t ada2200_stop3[]  ={ 0x00, 0x29, 0x01 };   	/* 0x07 {addr 16bit, data}, Disable SYNCO output, Select SYNCO edge location (Sync timing adjustment) */
static uint8_t ada2200_stop4[]  ={ 0x00, 0x2C, 0x00 };  	/* {addr 16bit, data}, Enable RCLK output */

static uint8_t m_tx_buf[3];       							/**< TX buffer. */
static uint8_t m_length = sizeof(m_tx_buf);       			/**< Transfer length. */


void ada2200_spi_write(const void * data, size_t size)
{
    /* Ensure bus is initialized */
    if (!spi2_bus_is_initialized()) {
        DBG_PRINTF("[ADA] auto-init SPI2\r\n");
        spi2_bus_init();
    }

	memcpy(m_tx_buf, data, size);
    /* Use shared SPI2 bus with manual CS control */
    nrf_gpio_pin_clear(ADA2200_CS_PIN);
    spi2_bus_transfer(m_tx_buf, size, NULL, 0);
    nrf_gpio_pin_set(ADA2200_CS_PIN);
}


extern void ada2200_start(void)
{
	//ada2200_spi_write(ada2200_startR, m_length);
	DBG_PRINTF("[ADA] start...\r\n");

	ada2200_spi_write(ada2200_start0, m_length);
	DBG_PRINTF("[ADA] cmd0 OK\r\n");
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_start1, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_start2, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_start3, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_start4, m_length);
	nrf_delay_us(2);
}


extern void ada2200_stop(void)
{
	ada2200_spi_write(ada2200_stop0, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_stop1, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_stop2, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_stop3, m_length);
	nrf_delay_us(2);
	ada2200_spi_write(ada2200_stop4, m_length);
	nrf_delay_us(2);
}


extern void ada2200_init(void)
{
    DBG_PRINTF("[ADA] init...\r\n");
    /* Initialize shared SPI2 bus (handles already-initialized case) */
    ret_code_t err = spi2_bus_init();
    if (err != NRF_SUCCESS) {
        APP_ERROR_CHECK(err);
    }
    DBG_PRINTF("[ADA] init OK\r\n");
    /* CS pin is configured by spi2_bus_init() */
}


extern void ada2200_uninit(void)
{
    /* Don't uninit the shared bus - other devices may be using it */
    /* Just ensure CS is deasserted */
    nrf_gpio_pin_set(ADA2200_CS_PIN);
}

