#if defined(STM32F769xx) || defined(JANUS2) || defined(GEN2)
#include "max14830_task.h"
#include "max14830.h"
#include "exo_ahw_vdp_uart_conv_max14830.h"
#include "exo_osal.h"
#include "exo_common.h"


#define SPI_TO_UART_ENB

os_timer_handle_ptr port0_tmr_handle;
os_timer_handle_ptr port1_tmr_handle;
os_timer_handle_ptr port2_tmr_handle;
os_timer_handle_ptr port3_tmr_handle;

os_queue_handle_ptr  max14820_rcv_queue_hdl;

os_sem_handle_ptr intr_semhandle;
os_sem_handle_ptr recieve_semhandle;
os_sem_handle_ptr transmit_semhandle;
os_sem_handle_ptr callback_semhandle;

os_sem_handle_ptr csn_semhandle;

extern s_max14830_hdle *inst_hdl;
uint8_t irq_id;
uint16_t ch0_sts=1;
uint16_t ch1_sts=1;
uint16_t ch2_sts=1;
uint16_t ch3_sts=1;

/* port0_cb function */
void port0_cb(void *argument)
{
    uint8_t buff[MAX1680_FIFO_LEN];
    uint8_t val;
    //DEBUG_CPRINT(("Port 0 timer CB\n\r"));
    max14830_read(inst_hdl,MAX14830_CH0,MAX14830_IRQSTS_REG,1,&val);
    max14830_read(inst_hdl,MAX14830_CH0,MAX14830_RHR_REG,MAX1680_FIFO_LEN,buff);
}

/* port1_cb function */
void port1_cb(void *argument)
{
    uint8_t val;
    uint8_t buff[MAX1680_FIFO_LEN];
    //DEBUG_CPRINT(("Port 1 timer CB\n\r"));
    max14830_read(inst_hdl,MAX14830_CH1,MAX14830_IRQSTS_REG,1,&val);
    max14830_read(inst_hdl,MAX14830_CH1,MAX14830_RHR_REG,MAX1680_FIFO_LEN,buff);
}

/* port2_cb function */
void port2_cb(void *argument)
{
    uint8_t val;
    uint8_t buff[MAX1680_FIFO_LEN];
    //DEBUG_CPRINT(("Port 2 timer CB\n\r"));
    max14830_read(inst_hdl,MAX14830_CH2,MAX14830_IRQSTS_REG,1,&val);
    max14830_read(inst_hdl,MAX14830_CH2,MAX14830_RHR_REG,MAX1680_FIFO_LEN,buff);
}

/* port3_cb function */
void port3_cb(void *argument)
{
    uint8_t val;
    uint8_t buff[MAX1680_FIFO_LEN];
    //DEBUG_CPRINT(("Port 3 timer CB\n\r"));
    max14830_read(inst_hdl,MAX14830_CH3,MAX14830_IRQSTS_REG,1,&val);
    max14830_read(inst_hdl,MAX14830_CH3,MAX14830_RHR_REG,MAX1680_FIFO_LEN,buff);
}

void recieve_task_func(void *argument)
{
  for(;;)
  {
        max14830_intr_hdlr();
  }
}

void rcv_callback_task_func()

{
    //os_itc_msg_handle_t max14830_pld;
    uint32_t max14830_queue_rcv = 0;
    while(1)
    {
        os_queue_receive(max14820_rcv_queue_hdl,&max14830_queue_rcv,os_wait_forever);
      //  os_itc_msg_rcv(SPI_TO_UART_CALLBACK_TASK,&max14830_pld,os_wait_forever);
        switch(max14830_queue_rcv)
        {
        case MAX14830_CH0:
        	if(inst_hdl->callback0_func != NULL)
        	{
            inst_hdl->callback0_func();
        	}
            break;
        case MAX14830_CH1:
        	if(inst_hdl->callback1_func != NULL)
        	{
        	inst_hdl->callback1_func();
        	}
            break;
        case MAX14830_CH2:
        	if(inst_hdl->callback2_func != NULL)
        	{
            inst_hdl->callback2_func();
        	}
            break;
        case MAX14830_CH3:
        	if(inst_hdl->callback3_func != NULL)
        	{
            inst_hdl->callback3_func();
        	}
            break;
        default:
            break;
        }
    }
}


uint8_t tx_data=(MAX14830_CH0 & 0x60) | (MAX14830_GLOBALIRQ_REG & 0x1F);
extern void* gpio_a;

void max14830_intr_hdlr()
{
    //os_queue_handle_ptr max14830_hdl;
    //os_itc_msg_handle_t max14830_pld;
    //uint32_t max14830_pld = 0;
    while(1)
    {
        sem_take(intr_semhandle,MAX14830_WAIT_FOREVER);
        if(inst_hdl != NULL)
        {
#ifdef SPI_TO_UART_ENB
        inst_hdl->gpio_write(inst_hdl->cs_pin, MAX14830_GPIO_RESET);
        inst_hdl->spi_read_write(inst_hdl->io_intf_hdle,&tx_data,&irq_id,1,100);
        inst_hdl->gpio_write(inst_hdl->cs_pin, MAX14830_GPIO_SET);
#endif
        irq_id = (irq_id & 0x0f);
        if((irq_id & 0x01)==0)
        {
            max14830_rx_ch0_it();

        }
        if((irq_id & 0x02)==0)
        {
            max14830_rx_ch1_it();
        }
        if((irq_id & 0x04)==0)
        {
            max14830_rx_ch2_it();
        }
        if((irq_id & 0x08)==0)
        {
            max14830_rx_ch3_it();  //&data3[cntr]
        }
        }
    }
}


void max14830_start()
{
  os_sem_create_bin(&intr_semhandle, "MAX14830_INTE_SEM");
  os_sem_create_bin(&transmit_semhandle, "MAX14830_TRANS_SEM");
  os_sem_create_bin(&recieve_semhandle, "MAX14830_RECV_SEM");
  os_sem_take(intr_semhandle,100);

  os_sem_create_bin(&csn_semhandle, "MAX14830_CSN_SEM");

  os_queue_create(&max14820_rcv_queue_hdl, 50, sizeof(uint32_t), NULL);

  os_timer_create(&port0_tmr_handle,"port0_timer",PERIODIC,port0_cb,NULL);
  os_timer_create(&port1_tmr_handle,"port1_timer",PERIODIC,port1_cb,NULL);
  os_timer_create(&port2_tmr_handle,"port2_timer",PERIODIC,port2_cb,NULL);
  os_timer_create(&port3_tmr_handle,"port3_timer",PERIODIC,port3_cb,NULL);

#if 1
  //os_thread_create(SPI_TO_UART_TRANS_TASK,128*4, P_SPI_TO_UART_CONV_TX, "spi_to_uart_transmit_task_thrd",transmit_task_func , NULL, NULL, NULL);

  os_thread_create(SPI_TO_UART_RCV_TASK,128*4, P_SPI_TO_UART_CONV_RX, "spi_to_uart_recv_task_thrd",recieve_task_func , NULL, NULL, NULL);

  os_thread_create(SPI_TO_UART_CALLBACK_TASK,128*4,P_SPI_TO_UART_CONV_CALLBACK,"spi_to_uart_callback_task_thrd",rcv_callback_task_func,NULL, NULL, NULL);
#endif
}

#endif
