/*!
 * \file globals.c
 *
 * \brief global variables
 *
 * \copyright  J Factor Embedded Technologies LLC. 2023
 * \copyright  TJM Embedded Software LLC. 2023
 * \author  T. J. Mulrooney
 *
 */
/*
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF J-Factor Embedded Technologies.
 *
 */

#include <stdint.h>
#include "jfet_files/jfet_common.h"

uint16_t manfID;
uint16_t deviceID;
float hdcTemp;
float hdcHumidity;
char *cmdLineParameter[MAX_PARAMETERS]; //!< array of pointers to command line parameter strings
uint8_t numParameters;
char devIDStr[50];
uint32_t sensorTypePresentAll = 0;
float adc_voltage[2] = {0}; //!< last ADC voltage read [0] battery
uint8_t flashReadIn[MAX_FLASH_BYTES];
char resetCause[100];

int ret;
uint8_t buffer_tx[SPI_BUF_SIZE] = {}; //!< SPI transmit buffer
uint8_t buffer_rx[SPI_BUF_SIZE] = {}; //!< SPI receive buffer0

#if 1
#define SPI_CS1N_NODE DT_ALIAS(spi_cs1n)
#if !DT_NODE_HAS_STATUS(SPI_CS1N_NODE, okay)
#error "SPI_CS1N alias is not defined"
#endif
const struct gpio_dt_spec spics1n = GPIO_DT_SPEC_GET_OR(SPI_CS1N_NODE, gpios, {0});
#endif

#if 1
#define SPI_CS2N_NODE DT_ALIAS(spi_cs2n)
#if !DT_NODE_HAS_STATUS(SPI_CS2N_NODE, okay)
#error "SPI_CS2N alias is not defined"
#endif
const struct gpio_dt_spec spics2n = GPIO_DT_SPEC_GET_OR(SPI_CS2N_NODE, gpios, {0});
#endif

// #if DT_NODE_HAS_STATUS(DT_ALIAS(spi0), okay)
// #define SPI_DEV_NODE	DT_ALIAS(spi0)
// #if DT_NODE_HAS_STATUS(DT_ALIAS(spi1), okay)
// #define SPI_DEV_NODE	DT_ALIAS(spi1)
#if DT_NODE_HAS_STATUS(DT_ALIAS(spi2), okay)
#define SPI_DEV_NODE DT_ALIAS(spi2)
// #elif DT_NODE_HAS_STATUS(DT_ALIAS(spi3), okay)
#//define SPI_DEV_NODE	DT_ALIAS(spi3)
#else
#error "Please set the correct SPI device"
#endif
// #define SPI_DRV_NAME	CONFIG_SPI_LOOPBACK_DRV_NAME

const struct device *const spi_dev = DEVICE_DT_GET(SPI_DEV_NODE);

const struct spi_cs_control spi_cs1 =
	{
		.gpio = GPIO_DT_SPEC_GET_OR(SPI_CS1N_NODE, gpios, {0}),
		.delay = 0,
};

const struct spi_cs_control spi_cs2 =
	{
		.gpio = GPIO_DT_SPEC_GET_OR(SPI_CS2N_NODE, gpios, {0}),
		.delay = 0,
};

struct spi_config spi_cfg1 = //!< SPI configuration
	{
		.frequency = SLOW_FREQ,
		.operation = SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE,
		.slave = SPI_SLAVE,
		.cs = spi_cs1,
		//		.cs = SPI_CS_CONTROL_INIT(SPI_CS1N_NODE, 0),
		//		.cs = NULL,
};

struct spi_config spi_cfg2 = //!< SPI configuration
	{
		.frequency = SLOW_FREQ,
		.operation = SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE,
		.slave = SPI_SLAVE,
		.cs = spi_cs2,
		//		.cs = SPI_CS_CONTROL_INIT(SPI_CS2N_NODE, 0),
		//		.cs = NULL,
};

struct spi_buf tx_bufs[] = //!< SPI TX buffer structure
	{
		{
			.buf = buffer_tx,
			.len = SPI_BUF_SIZE,
		},
};
struct spi_buf rx_bufs[] = //!< SPI RX buffer structure
	{
		{
			.buf = buffer_rx,
			.len = SPI_BUF_SIZE,
		},
};
const struct spi_buf_set tx = //!< SPI TX buffer structure
	{
		.buffers = tx_bufs,
		.count = ARRAY_SIZE(tx_bufs)};
const struct spi_buf_set rx = //!< SPI RX buffer structure
	{
		.buffers = rx_bufs,
		.count = ARRAY_SIZE(rx_bufs)};
