

#include <nrfx_example.h>
#include <nrfx_spim.h>

#define NRFX_LOG_MODULE                 EXAMPLE
#define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
#define NRFX_EXAMPLE_CONFIG_LOG_LEVEL   3
#include <nrfx_log.h>

/** @brief Symbol specifying SPIM instance to be used. */
#define SPIM_INST_IDX 1

#define APP_SPIM_CS_PIN (4)
#define APP_SPIM_SCK_PIN (5)
#define APP_SPIM_MISO_PIN (6)
#define APP_SPIM_MOSI_PIN (7)

#define NRFX_CUSTOM_ERROR_CODES 0 //used in nrfx_errors.h

static nrfx_spim_config_t spim_config =
	NRFX_SPIM_DEFAULT_CONFIG(APP_SPIM_SCK_PIN, APP_SPIM_MOSI_PIN, APP_SPIM_MISO_PIN, APP_SPIM_CS_PIN);

#define TEST_STRING "1234567890"

static uint8_t m_tx_buf[] = TEST_STRING;
static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1];
static const uint8_t m_length = sizeof(m_tx_buf);

static volatile bool
	spim_xfer_done; /**< Flag used to indicate that SPIM instance completed the transfer. */



static void spim_event_handler(nrfx_spim_evt_t const * p_event, void * p_context)
{

    NRFX_LOG_INFO("IRQ: %d\n", p_event->type);
    if (p_event->type == NRFX_SPIM_EVENT_DONE)
    {
        spim_xfer_done = true;
        char * p_msg = p_context;
        NRFX_LOG_INFO("SPIM finished. Context passed to the handler: >%s<", p_msg);
        NRFX_LOG_INFO("Message received: %s", p_event->xfer_desc.p_rx_buffer);
    }

    if (m_rx_buf[0] != 0)
    {
        NRFX_LOG_INFO("Received: ");
        for(int i = 0; i < sizeof(m_rx_buf); i++) {
			NRFX_LOG_INFO("%x ", m_rx_buf[i]);
		}
		NRFX_LOG_INFO("\n");
    }	
}

/**
 * @brief Function for application main entry.
 *
 * @return Nothing.
 */
int main(void)
{
    nrfx_err_t status;
    (void)status;

    NRFX_EXAMPLE_LOG_INIT();

    NRFX_LOG_INFO("Starting nrfx_spim basic non-blocking example.");
    NRFX_EXAMPLE_LOG_PROCESS();
    NRFX_LOG_INFO("SPIM example\n");

    nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf, m_length);
    nrfx_spim_t spim_inst = NRFX_SPIM_INSTANCE(SPIM_INST_IDX);
	/*if (NRFX_SUCCESS !=
	    nrfx_spim_init(&spim_inst, &spim_config, spim_event_handler, NULL)) {
		NRFX_LOG_INFO("Init Failed\n");
		return 1;
	}*/




    void * p_context = "Some context";
    status = nrfx_spim_init(&spim_inst, &spim_config, spim_event_handler, p_context);
    NRFX_LOG_INFO("status = %d \r\n", status);
    NRFX_ASSERT(status == NRFX_SUCCESS);

#if defined(__ZEPHYR__)
    #define SPIM_INST         NRFX_CONCAT_2(NRF_SPIM, SPIM_INST_IDX)
    #define SPIM_INST_HANDLER NRFX_CONCAT_3(nrfx_spim_, SPIM_INST_IDX, _irq_handler)
    IRQ_DIRECT_CONNECT(NRFX_IRQ_NUMBER_GET(SPIM_INST), IRQ_PRIO_LOWEST, SPIM_INST_HANDLER, 0);
#endif

while(1) {
		memset(m_rx_buf, 0, m_length);
		spim_xfer_done = false;

		nrfx_err_t err_code = nrfx_spim_xfer(&spim_inst, &xfer_desc, 0);

		if (err_code == NRFX_ERROR_BUSY) {
			NRFX_LOG_INFO("SPI busy\n");
		}

		else if (err_code != NRFX_SUCCESS){
			NRFX_LOG_INFO("Error code = %d\n", err_code);
		}

		while (!spim_xfer_done) {
			__WFE();
		}
		m_tx_buf[0]++;
		if (m_tx_buf[0] > '9') {
			m_tx_buf[0] = '0';
		}
        NRFX_EXAMPLE_LOG_PROCESS();
		k_sleep(K_MSEC(1000));
	}
}

/** @} */
