#include "bsp_can.h"
#include "bsp_queue.h"

#include "kit_time.h"
#include "kit_data.h"
#include "kit_debug.h"

/***********************************************同步接口*********************************************/
kit_ret_e bsp_can_sync_send(can_dev_e dev, uint32_t id, uint8_t *buf, uint8_t len, can_frame_type_e ide, CanRtr rtr)
{
    CanMsg msg;
    uint32_t dly = 0;
    kit_ret_e res = kKit_Ret_Ok;
    
    KIT_ASSERT_PARAM(dev <= kcan_dev_e_End);
    KIT_ASSERT_PARAM(((buf != NULL) && (len > 0) && (len < 9)) || ((buf == NULL) && (len == 0)));
    
    msg.ide = ide;
    msg.rtr = rtr;
    msg.len = len;
    msg.id.value = id;
    kit_copy_buf(msg.data.byte, buf, len); 
    
    while(drv_can_send(dev, &msg) != kKit_Ret_Ok)
    {
        kit_time_dly_ms(1);
        if(++dly > 5)
        {
            res = kKit_Ret_TimeOut;
            break;
        }
    }
    
    KIT_ASSERT_RES(dev, res);
    return res;
}

kit_ret_e bsp_can_ext_data_sync_send(can_dev_e dev, uint32_t id, uint8_t *buf, uint8_t len)
{
    KIT_ASSERT_PARAM(((buf != NULL) && (len > 0) && (len < 9)) || ((buf == NULL) && (len == 0)));
    
    return bsp_can_sync_send(dev, id, buf, len, kcan_frame_type_e_Ext, kCanRtr_Data);
}

kit_ret_e bsp_can_std_data_sync_send(can_dev_e dev, uint32_t id, uint8_t *buf, uint8_t len)
{
    KIT_ASSERT_PARAM(((buf != NULL) && (len > 0) && (len < 9)) || ((buf == NULL) && (len == 0)));
    
    return bsp_can_sync_send(dev, id, buf, len, kcan_frame_type_e_Std, kCanRtr_Data);
}

kit_ret_e bsp_can_ext_datas_sync_send(can_dev_e dev, uint32_t id, uint32_t add_value, uint8_t *buf, uint16_t len)
{
    uint16_t  remainder, quotient;
    kit_ret_e res = kKit_Ret_Ok;    
    
    KIT_ASSERT_PARAM((len > 0) && (buf != NULL));
    
    quotient = len >> 3;
    remainder = len % 8;
    for(len = 0; len < quotient; len++)
    {
        res |= bsp_can_ext_data_sync_send(dev, id, (uint8_t *)(buf + (len << 3)), 8);
        id += add_value;        
    }
    
    if(0 != remainder)
    {
        res |= bsp_can_ext_data_sync_send(dev, id, (uint8_t *)(buf + (len << 3)), remainder);
    }
    
    return res;     
}

/***********************************************异步接口*********************************************/
#if defined(STM32F40_41xxx) || defined(STM32F429_439xx)
    bool is_can_it_start[kcan_dev_e_End] = {false, false};
    QUEUE_STATIC_INIT_CCM(can0_tx_queue, 300, sizeof(CanMsg));
    QUEUE_STATIC_INIT_CCM(can1_tx_queue, 300, sizeof(CanMsg));
    QueueItem * const can_tx_queue[kcan_dev_e_End] = {&can0_tx_queue, &can1_tx_queue};
#else
    QUEUE_STATIC_INIT(can0_tx_queue, 200, sizeof(CanMsg));
    #if STM32F10X_CL
        bool is_can_it_start[kcan_dev_e_End] = {false, false};
        QUEUE_STATIC_INIT(can1_tx_queue, 200, sizeof(CanMsg));
        QueueItem * const can_tx_queue[kcan_dev_e_End] = {&can0_tx_queue, &can1_tx_queue};
    #else
        bool is_can_it_start[kcan_dev_e_End] = {false};
        QueueItem * const can_tx_queue[kcan_dev_e_End] = {&can0_tx_queue};
    #endif
#endif


kit_ret_e bsp_can_async_send(can_dev_e dev, uint32_t id, uint8_t *buf, uint8_t len, can_frame_type_e ide, CanRtr rtr)
{
    CanMsg *msg;
    uint32_t cnt = 0;
    kit_ret_e res = kKit_Ret_Error;
    
    KIT_ASSERT_PARAM((dev <= kcan_dev_e_End) && (msg != NULL));
    //当mailbox空闲时，优先使用mailbox发送
    //一但开启中断发送，始终都用中断直至队列为空
    if((drv_can_get_mailbox(dev) < 3) && (is_can_it_start[dev] == false))
    {  
        res = bsp_can_sync_send(dev, id, buf, len, ide, rtr);
    }
    else
    {       
        while(((bsp_queue_push_pointer(can_tx_queue[dev], (void** )&msg)) != kKit_Ret_Ok) && (cnt++ < 5))
        {
            kit_time_dly_ms(1);
        }
        if(cnt < 5)
        {
            msg->len = len;
            msg->id.value = id;
            msg->ide = ide;
            msg->rtr = rtr;    
            kit_copy_buf(msg->data.byte, buf, len);
            is_can_it_start[dev] = true;
            drv_can_ctrl_interrupt(dev, kcan_interrupt_e_Tx, true);
            res = kKit_Ret_Ok;
        }
    }
    
    return res;
}

kit_ret_e bsp_can_ext_data_async_send(can_dev_e dev, uint32_t id, uint8_t *buf, uint8_t len)
{
    KIT_ASSERT_PARAM(((buf != NULL) && (len > 0) && (len < 9)) || ((buf == NULL) && (len == 0)));
    
    return bsp_can_async_send(dev, id, buf, len, kcan_frame_type_e_Ext, kCanRtr_Data);
}

kit_ret_e bsp_can_std_data_async_send(can_dev_e dev, uint32_t id, uint8_t *buf, uint8_t len)
{
    KIT_ASSERT_PARAM(((buf != NULL) && (len > 0) && (len < 9)) || ((buf == NULL) && (len == 0)));
    
    return bsp_can_async_send(dev, id, buf, len, kcan_frame_type_e_Std, kCanRtr_Data);
}

kit_ret_e bsp_can_ext_datas_async_send(can_dev_e dev, uint32_t id, uint32_t add_value, uint8_t *buf, uint16_t len)
{
    uint16_t  remainder, quotient;
    kit_ret_e res = kKit_Ret_Ok;
    
    KIT_ASSERT_PARAM(len > 0);
    KIT_ASSERT_PARAM(buf != NULL);
    
    quotient = len >> 3;
    remainder = len % 8;
    for(len = 0; len < quotient; len++)
    {
        res |= bsp_can_ext_data_async_send(dev, id, (uint8_t *)(buf + (len << 3)), 8);            
        id += add_value;
    }
    
    if(0 != remainder)
    {
        res |= bsp_can_ext_data_async_send(dev, id, (uint8_t *)(buf + (len << 3)), remainder);
    }
    
    return res;    
}



void bsp_can_async_poll(can_dev_e dev)
{  
    CanMsg *msg;
    
    while(drv_can_get_mailbox(dev) < 3)
    {        
        if(bsp_queue_pop_pointer(can_tx_queue[dev], (void **)&msg) == kKit_Ret_Ok)
        {
            drv_can_send(dev, msg);
        }   
        else
        {
            is_can_it_start[dev] = false;
            drv_can_ctrl_interrupt(dev, kcan_interrupt_e_Tx, false);
            break;
        }
    }
}


