/*! ----------------------------------------------------------------------------
 *  @file    read_dev_id.c
 *  @brief   This example just read DW IC's device ID. It can be used to verify
 *           the SPI comms are working correctly.
 *
 *
 * @author Decawave
 *
 * @copyright SPDX-FileCopyrightText: Copyright (c) 2024 Qorvo US, Inc.
 *            SPDX-License-Identifier: LicenseRef-QORVO-2
 *
 */

#include "deca_probe_interface.h"
#include <deca_device_api.h>
#include <example_selection.h>
#include <port.h>
#include <stdio.h>
#include "priv/log.h"

#if defined(TEST_READING_DEV_ID)

static const char* LOG_TAG = "read_dev_id";
extern void test_run_info(unsigned char* data);

/* Example application name and version to display on LCD screen/VCOM port. */
#define APP_NAME "READ DEV ID      "

// #define USE_SPI2 1 // set this to 1 to use DW37X0 SPI2

/**
 * Application entry point.
 */
int read_dev_id(void)
{

#if USE_SPI2
    uint8_t sema_res;
#endif
    int err;
    uint32_t dev_id;

    ESP_LOGI(LOG_TAG, "task_uwb_test() start...@ read_dev_id.c");

    /* Display application name on LCD. */
    test_run_info((unsigned char*)APP_NAME);

    /* Configure SPI rate, DW3000 supports up to 38 MHz */
    port_set_dw_ic_spi_fastrate();
    LOG_INF("port_set_dw_ic_spi_fastrate OK");

    /* Reset DW IC */
    reset_DWIC(); /* Target specific drive of RSTn line into DW IC low for a period. */
    LOG_INF("reset_DWIC OK");

    Sleep(2); // Time needed for DW3000 to start up (transition from INIT_RC to IDLE_RC, or could wait for SPIRDY event)

    /* Probe for the correct device driver. */
    int ret = dwt_probe((struct dwt_probe_s*)&dw3000_probe_interf);
    if (ret < 0) {
        LOG_ERR("DWT Probe failed");
        return false;
    }
    LOG_INF("DWT Probe OK");

    dev_id = dwt_readdevid();
    if ((dev_id == (uint32_t)DWT_DW3720_PDOA_DEV_ID))
    {
        /* If host is using SPI 2 to connect to DW3000 the code in the USE_SPI2 above should be set to 1 */
#if USE_SPI2

        change_SPI(SPI_2);

        /* Configure SPI rate, DW3000 supports up to 38 MHz */
        port_set_dw_ic_spi_fastrate();

        /* Reset DW IC */
        reset_DWIC(); /* Target specific drive of RSTn line into DW IC low for a period. */

        Sleep(2); // Time needed for DW3000 to start up (transition from INIT_RC to IDLE_RC, or could wait for SPIRDY event)

        /* If host is using SPI 2 to connect to DW3000 the it needs to request access or force access */
        sema_res = dwt_ds_sema_status();

        if ((sema_res & (0x2)) == 0) // the SPI2 is free
        {
            dwt_ds_sema_request();
        }
        else
        {
            test_run_info((unsigned char*)"SPI2 IS NOT FREE"); // If SPI2 is not free the host can force access
            while (1) {};
        }
#endif
    }

    LOG_INF("dwt_check_dev_id OK");

    /* Reads and validate device ID returns DWT_ERROR if it does not match expected else DWT_SUCCESS */
    if ((err = dwt_check_dev_id()) == DWT_SUCCESS)
    {
        test_run_info((unsigned char*)"DEV ID OK");
    }
    else
    {
        test_run_info((unsigned char*)"DEV ID FAILED");
    }

    return err;
}

#endif
/*****************************************************************************************************************************************************
 * NOTES:
 ****************************************************************************************************************************************************/
