/**
    @copyright None
    @file      protocol.c
    @author    Comment Vivre
    @date      2025-12-23
    @brief     None
*/
#include <MyProject.h>

// 1 / 115200 * 640 *1000 ≈ 5.6 ms
uint8_t xdata DebugDat[MAX_FRAME_LEN] = {0};
uint8_t RecvBuffer[MAX_FRAME_LEN] = {0};
bool RecMessageFalg = false;

/**
    @function     Dabug_Data_Update
    @brief        调试信息上载
    @date         2025-11-05
*/
void Dabug_Data_Update(void)
{
    static uint8_t update_delay_cnt = 0;
    uint8_t sumcheck = 0, addcheck = 0;
    uint16_t i = 0, switchTemp = 0;
    
    if (++ update_delay_cnt > 9)
    {
        update_delay_cnt = 0;
        memset(&DebugDat, 0, 0x40);
        *(_IO uint16_t xdata *)(&DebugDat +  0) = 0xABFF;
        *(_IO uint16_t xdata *)(&DebugDat +  2) = 0xFFF1;
        *(_IO uint16_t xdata *)(&DebugDat +  4) = 0x3800;
        switchTemp = (uint16_t)FOC__UD;
        *(_IO uint16_t xdata *)(&DebugDat +  6) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC__UQ;
        *(_IO uint16_t xdata *)(&DebugDat +  8) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC__IA;
        *(_IO uint16_t xdata *)(&DebugDat + 10) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC__EMF;
        *(_IO uint16_t xdata *)(&DebugDat + 12) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC__IQ;
        *(_IO uint16_t xdata *)(&DebugDat + 14) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC_IQREF;
        *(_IO uint16_t xdata *)(&DebugDat + 16) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC__EOME;
        *(_IO uint16_t xdata *)(&DebugDat + 18) = (switchTemp >> 8) | (switchTemp << 8);
        switchTemp = (uint16_t)FOC__POW;
        *(_IO uint16_t xdata *)(&DebugDat + 20) = (switchTemp >> 8) | (switchTemp << 8);
        // UD UQ IA EMF IQ IQREF EOME POW REF
        
        // 计算校验
        for (i = 0; i < 62; i++)
        {
            sumcheck += DebugDat[i];
            addcheck += sumcheck;
        }
        
        //将计算出来的校验数据写入数据帧
        DebugDat[62] = sumcheck;
        DebugDat[63] = addcheck;
        Switch_DMA(0);
    }
}
