/**
  *****************************************************************************
  * @file    sys_spi.c
  * @author  Solution Team
  * @version V1.00
  * @date    28-February-2023
  * @brief   SPI slave process engine
  *****************************************************************************
  * @attention 
  *  Copyright (c) 2017 - 2023 Unicmicro Co., Ltd. 
  *  All Rights Reserved. 
  *****************************************************************************
  */

#include "libc.h"
#include "drivers.h"
#include "sys_spi.h"
#include "sys_config.h"

#if (SPI_MODULE_ENABLED && SPI_MODULE_SLAVE_ENABLED)

void sys_spi_handler(void)
{
    u32_t rx_len;
    sync_enter_critical();
    rx_len = g_spi_info.sla_rx_buf_index;
    g_spi_info.sla_tx_buf_index = 0;
    for (int i = 0; i < rx_len; i++)
    {
        //spi_sla_tx_buf[i] = spi_sla_rx_buf[i];
        g_spi_info.sla_tx_buf[i] = 255-g_spi_info.sla_rx_buf[i];
    }
    drv_spi_transmit_data(g_spi_info.sla_tx_buf[0]);
    g_spi_info.sla_tx_buf_index = 1;
    g_spi_info.sla_tx_buf_cnt = rx_len;
    sync_exit_critical();
#if CLI_ENABLED
    if (sys_cfg_dbg_get())
    {
        if (rx_len)
        {
            drv_uart_puts("SPI RX:");
            drv_uart_puthex((u8_t *)&g_spi_info.sla_rx_buf[0], rx_len, FALSE);
        }
        if (g_spi_info.sla_tx_buf_cnt)
        {
            drv_uart_puts("SPI TX:");
            drv_uart_puthex((u8_t *)&g_spi_info.sla_tx_buf[0], g_spi_info.sla_tx_buf_cnt, FALSE);
        }
    }
#endif /* CLI_ENABLED */
}


#if SPI_MODULE_SLAVE_DMA_ENABLED
void sys_spi_dma_handler(void)
{
    u32_t rx_bytes = 0, tx_bytes = 0;
    bool_t session_cmplt = 0;

    sync_enter_critical();
    if (g_spi_info.dma_handling)
    {
        if (g_spi_info.receive_cmplt && g_spi_info.transmit_cmplt)
        {
            // session complete
            session_cmplt = 1;
            rx_bytes = g_spi_info.sla_rx_buf_cnt;
            tx_bytes = g_spi_info.sla_tx_buf_cnt;
        }
        else if ((g_spi_info.receive_cmplt) && (timer_expired(&(g_spi_info.sla_session_timer))))
        {
            // tx timeout
            session_cmplt = 1;
            rx_bytes = g_spi_info.sla_rx_buf_cnt;
            tx_bytes = drv_dma1_src_addr_get() - (u32_t)&g_spi_info.sla_tx_buf[0];
            timer_stop(&(g_spi_info.sla_session_timer));
        }

        if (session_cmplt)
        {
            timer_stop(&(g_spi_info.sla_session_timer));
            // start again
            drv_spi_sla_dma_start();
        }
    }
    sync_exit_critical();
#if CLI_ENABLED
    if (sys_cfg_dbg_get())
    {
        if (rx_bytes)
        {
            drv_uart_puts("SPI RX:");
            drv_uart_puthex((u8_t *)&g_spi_info.sla_rx_buf[0], rx_bytes, FALSE);
        }
        if (tx_bytes)
        {
            drv_uart_puts("SPI TX:");
            drv_uart_puthex((u8_t *)&g_spi_info.sla_tx_buf[0], tx_bytes, FALSE);
        }
    }
#endif /* CLI_ENABLED */
}
#endif /* SPI_MODULE_SLAVE_DMA_ENABLED */


#endif



