#include "busIdleDetection.h"


#ifdef RS485BUSIDLE1
#include "uart_dev.h"

/* 通过软件定时器的方式来完成 */
static TimerHandle_t J1_485_Swtmr = NULL;
static TimerHandle_t J2_485_Swtmr = NULL;
static TimerHandle_t J3_485_Swtmr = NULL;
static TimerHandle_t J4_485_Swtmr = NULL;
static TimerHandle_t J5_0_485_Swtmr = NULL;

static void J1_485_tmrCallback(void* parameter);
static void J2_485_tmrCallback(void* parameter);
static void J3_485_tmrCallback(void* parameter);
static void J4_485_tmrCallback(void* parameter);
static void J5_0_485_tmrCallback(void* parameter);

#define softwareDelay (30 / (1000 / configTICK_RATE_HZ))

/*
 * @brief   初始化所有的软件定时器
 * @param   
 * @retval
 *
 */
void softwareTimeInit(void)
{
    J1_485_Swtmr = xTimerCreate((const char*)"J1Timer",
                                softwareDelay,              //延时
                                (UBaseType_t )pdFALSE,      //单次模式 
                                (void *)1,                  //为每个计时器分配一个索引的唯一ID
                                (TimerCallbackFunction_t)J1_485_tmrCallback);   //回调函数

    J2_485_Swtmr = xTimerCreate((const char*)"J2Timer",
                                softwareDelay,
                                (UBaseType_t )pdFALSE,
                                (void *)2,
                                (TimerCallbackFunction_t)J2_485_tmrCallback);
                                
    J3_485_Swtmr = xTimerCreate((const char*)"J3Timer",
                                softwareDelay,
                                (UBaseType_t )pdFALSE,
                                (void *)3,
                                (TimerCallbackFunction_t)J3_485_tmrCallback);
                                
    J4_485_Swtmr = xTimerCreate((const char*)"J4Timer",
                                softwareDelay,
                                (UBaseType_t )pdFALSE,
                                (void *)4,
                                (TimerCallbackFunction_t)J4_485_tmrCallback);
                                
    J5_0_485_Swtmr = xTimerCreate((const char*)"J5_0Timer",
                                softwareDelay,
                                (UBaseType_t )pdFALSE,
                                (void *)5,
                                (TimerCallbackFunction_t)J5_0_485_tmrCallback);
}

void J1_485_tmrCallback(void* parameter)
{
    setBUSIDLEFlag(g_J1_uart6_handle, 0);
}

void J2_485_tmrCallback(void* parameter)
{
    setBUSIDLEFlag(g_J2_uart7_handle, 0);
}

void J3_485_tmrCallback(void* parameter)
{
    setBUSIDLEFlag(g_J3_usart2_handle, 0);
}

void J4_485_tmrCallback(void* parameter)
{
    setBUSIDLEFlag(g_J4_uart8_handle, 0);
}

void J5_0_485_tmrCallback(void* parameter)
{
    setBUSIDLEFlag(g_J5_0_usart3_handle, 0);
}

/*
 * @brief   J1_485空闲中断内容
 * @param   
 * @retval
 *
 */
void J1_485_IN_IDLE(void)
{
    xTimerStartFromISR(J1_485_Swtmr, 0);
}

/*
 * @brief   J2_485空闲中断内容
 * @param   
 * @retval
 *
 */
void J2_485_IN_IDLE(void)
{
    xTimerStartFromISR(J2_485_Swtmr, 0);
}

/*
 * @brief   J3_485空闲中断内容
 * @param   
 * @retval
 *
 */
void J3_485_IN_IDLE(void)
{
    xTimerStartFromISR(J3_485_Swtmr, 0);
}

/*
 * @brief   J4_485空闲中断内容
 * @param   
 * @retval
 *
 */
void J4_485_IN_IDLE(void)
{
    xTimerStartFromISR(J4_485_Swtmr, 0);
}

/*
 * @brief   J5_0_485空闲中断内容
 * @param   
 * @retval
 *
 */
void J5_0_485_IN_IDLE(void)
{
    xTimerStartFromISR(J5_0_485_Swtmr, 0);
}

/*
 * @brief   J1_485空闲中断后，第一次进入接收中断内容
 * @param   
 * @retval
 *
 */
void J1_485_IN_RXNE(void)
{
    xTimerStopFromISR(J1_485_Swtmr, 0);
}

/*
 * @brief   J2_485空闲中断后，第一次进入接收中断内容
 * @param   
 * @retval
 *
 */
void J2_485_IN_RXNE(void)
{
    xTimerStopFromISR(J2_485_Swtmr, 0);
}

/*
 * @brief   J3_485空闲中断后，第一次进入接收中断内容
 * @param   
 * @retval
 *
 */
void J3_485_IN_RXNE(void)
{
    xTimerStopFromISR(J3_485_Swtmr, 0);
}

/*
 * @brief   J4_485空闲中断后，第一次进入接收中断内容
 * @param   
 * @retval
 *
 */
void J4_485_IN_RXNE(void)
{
    xTimerStopFromISR(J4_485_Swtmr, 0);
}

/*
 * @brief   J5_0_485空闲中断后，第一次进入接收中断内容
 * @param   
 * @retval
 *
 */
void J5_0_485_IN_RXNE(void)
{
    xTimerStopFromISR(J5_0_485_Swtmr, 0);
}
#endif

/*
 * @brief   得到串口能否发送
 * @param   
 * @retval  0 能发送
            1 不能发送
            0xFF 错误
 */
uint8_t getRs485State(device_handle device)
{
#ifdef RS485BUSIDLE1
#ifdef UARTINTERRUPTSEND
    return (getUartSendState(device) || getBUSIDLEFlag(device));
#endif
#endif
}

uint8_t getUartState(device_handle device)
{
#ifdef UARTINTERRUPTSEND
    return getUartSendState(device);
#endif
}


