#include "can_link.h"
#include <string.h>
#include "soft_can.h"
#include "rkfifo.h"

//fdcan1 外设消息
void HAL_FDCAN_RxFifo0Callback( FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifoITs )
{
    //测试受干扰时can数据长度会出错，导致HAL_FDCAN_GetRxMessage接收超过8个字节
    uint8_t canRxData[64] = {0};
    FDCAN_RxHeaderTypeDef RxHeader = {0};

    //FIFO收到新数据
    if ( ( RxFifoITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE ) != RESET )
    {
        //清空rxfifo，每次只保留最新一个。
        do
        {
            /* Retreive Rx messages from RX FIFO0 */
            HAL_FDCAN_GetRxMessage( hfdcan, FDCAN_RX_FIFO0, &RxHeader, canRxData );
        }
        while ( HAL_FDCAN_GetRxFifoFillLevel( hfdcan, FDCAN_RX_FIFO0 ) >= 1 );

        short len = CanGetHalDataLengthToDec( RxHeader.DataLength );

        if ( RxHeader.IdType == FDCAN_EXTENDED_ID )
        {
			if (len >8)
				return;
			switch(RxHeader.Identifier)
			{
				//其余消息直接转发
				default:
					Can_Txmsg_Target_Init(RxHeader.Identifier, canRxData);
					can_send(&hfdcan2);
					break;
				
			}
			
		}
	}
	
	//CAN数据丢失中断
    if ( ( RxFifoITs & FDCAN_IT_RX_FIFO0_MESSAGE_LOST ) != RESET )
    {
        /* Retreive Rx messages from RX FIFO0 */
        HAL_FDCAN_GetRxMessage( hfdcan, FDCAN_RX_FIFO0, &RxHeader, canRxData );
    }
}

//fdcan2 PMU消息
void HAL_FDCAN_RxFifo1Callback( FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifoITs )
{
    //测试受干扰时can数据长度会出错，导致HAL_FDCAN_GetRxMessage接收超过8个字节
    uint8_t canRxData[64] = {0};
    FDCAN_RxHeaderTypeDef RxHeader = {0};
    //FIFO收到新数据
    if ( ( RxFifoITs & FDCAN_IT_RX_FIFO1_NEW_MESSAGE ) != RESET )
    {
        //上电初始化时有可能外设CAN有数据，fifo设置的可保存32组数据，导致RXFIFO累积，程序正常后读取数据一直读取之前的。
        //清空rxfifo，每次只保留最新一个。
        do
        {
            /* Retreive Rx messages from RX FIFO0 */
            HAL_FDCAN_GetRxMessage( hfdcan, FDCAN_RX_FIFO1, &RxHeader, canRxData );
        }
        while ( HAL_FDCAN_GetRxFifoFillLevel( hfdcan, FDCAN_RX_FIFO1 ) >= 1 );
		
		short len = CanGetHalDataLengthToDec( RxHeader.DataLength );

        //扩展帧
        if ( RxHeader.IdType == FDCAN_EXTENDED_ID )
        {
            if (len >8)
				return;
			switch(RxHeader.Identifier)
			{
				case WEIGHT_MSG_ID:
				{
					Can_Rx can_rx;
					memcpy(&can_rx.Rxheader , &RxHeader,sizeof(RxHeader));
					memcpy(&can_rx.Rxdata, canRxData, 8);
					rkfifo_in(&can_rx_kfifo, &can_rx, 1);
				}
					break;
				case PUMP_MSG_ID:
				{
					short pwm;
					memcpy(&pwm, canRxData, 2);
					PUMP1_PWM_CONTROL(pwm);
					
					memcpy(&pwm, &canRxData[2], 2);
					PUMP2_PWM_CONTROL(pwm);
				}
					break;
				case SPRAY_MSG_ID:
				{
					short pwm;
					memcpy(&pwm, canRxData, 2);
					SPRAY1_PWM_CONTROL(pwm);
					
					memcpy(&pwm, &canRxData[2], 2);
					SPRAY2_PWM_CONTROL(pwm);
				}
					break;
				//其余消息直接转发
				default:
					Can_Txmsg_Target_Init(RxHeader.Identifier, canRxData);
					can_send(&hfdcan1);
					break;
				
				
			}
        }
    }
	
	//CAN数据丢失中断
    if ( ( RxFifoITs & FDCAN_IT_RX_FIFO1_MESSAGE_LOST ) != RESET )
    {
        /* Retreive Rx messages from RX FIFO0 */
        HAL_FDCAN_GetRxMessage( hfdcan, FDCAN_RX_FIFO1, &RxHeader, canRxData );
    }
}

uint32_t CanGetHalDataLengthToDec( uint32_t DataLength )
{
    uint32_t dataLen = 0;

    if ( DataLength <= FDCAN_DLC_BYTES_8 )
    {
        dataLen = DataLength;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_12 )
    {
        dataLen = 12;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_16 )
    {
        dataLen = 16;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_20 )
    {
        dataLen = 20;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_24 )
    {
        dataLen = 24;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_32 )
    {
        dataLen = 32;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_48 )
    {
        dataLen = 48;
    }
    else if ( DataLength == FDCAN_DLC_BYTES_64 )
    {
        dataLen = 64;
    }

    return dataLen;
}

uint32_t CanSetDecToHalDataLength( uint32_t dataLen )
{
    uint32_t DataLength = FDCAN_DLC_BYTES_0;

    if ( dataLen <= 8 )
    {
        DataLength = dataLen;
    }
    else if ( dataLen == 12 )
    {
        DataLength = FDCAN_DLC_BYTES_12;
    }
    else if ( dataLen == 16 )
    {
        DataLength = FDCAN_DLC_BYTES_16;
    }
    else if ( dataLen == 20 )
    {
        DataLength = FDCAN_DLC_BYTES_20;
    }
    else if ( dataLen == 24 )
    {
        DataLength = FDCAN_DLC_BYTES_24;
    }
    else if ( dataLen == 32 )
    {
        DataLength = FDCAN_DLC_BYTES_32;
    }
    else if ( dataLen == 48 )
    {
        DataLength = FDCAN_DLC_BYTES_48;
    }
    else if ( dataLen == 64 )
    {
        DataLength = FDCAN_DLC_BYTES_64;
    }

    return DataLength;
}

void can_recv_HD_ESC_msg(uint8_t *recv_buf, uint32_t len, uint32_t id)
{
	uint8_t TypeID = 0;
  //uint8_t SerOrMsg = (id & HD_CANID_SNM_MASK) >> HD_CANID_SNM_POS; // 广播帧OR服务帧
  //uint8_t SrcNodeID = (id & HD_CANID_SRCNODE_MASK) >> HD_CANID_SRCNODE_POS;
	uint8_t buff[8];
	memcpy(buff,recv_buf,8);
	uint8_t finish_cnt = 0;
	
}


