#include "motorCommu.h"
#include "servoMotor_recv.h"
#include "speed_to_servoMotor.h"

#ifdef PTZ_MEDIUM_WORM_SERVO_MOTOR_24V
/*
********************************************************************************************************
*                             电机串口通讯驱动初始化相关结构体
********************************************************************************************************
*/
typedef struct
{
	uint32_t                gpio;				//引脚端口号
	rcu_periph_enum         rcuGpio;			//GPIO时钟
	uint32_t                uartNo;				//串口号
	rcu_periph_enum         rcuUart;			//串口时钟
    uint32_t                modePin;            //rs485收发模式切换引脚
	uint32_t                txPin;				//串口发送引脚
	uint32_t                rxPin;				//串口接收引脚
	IRQn_Type               uartIrq;			//串口中断号
}MotorCommuHwInfo_t;//电机与外部通讯的串口
static MotorCommuHwInfo_t g_motorCommuBuff[] = 
{
    //水平电机串口2
    {
        .gpio       =       GPIOD,
        .rcuGpio    =       RCU_GPIOD,
        .uartNo     =       USART2,
        .rcuUart    =       RCU_USART2,
        .modePin    =       GPIO_PIN_10,
        .txPin      =       GPIO_PIN_8,
        .rxPin      =       GPIO_PIN_9,
        .uartIrq    =       USART2_IRQn,
    },
    //垂直电机串口5
    {
        .gpio       =       GPIOC,
        .rcuGpio    =       RCU_GPIOC,
        .uartNo     =       USART5,
        .rcuUart    =       RCU_USART5,
        .modePin    =       GPIO_PIN_8,
        .txPin      =       GPIO_PIN_6,
        .rxPin      =       GPIO_PIN_7,
        .uartIrq    =       USART5_IRQn,
    },
};
#define MOTOR_NUM   ( sizeof(g_motorCommuBuff) / sizeof(g_motorCommuBuff[0]) )

/*
********************************************************************************************************
*                             电机串口DMA驱动初始化结构体
********************************************************************************************************
*/
typedef struct 
{
	uint32_t                dmaNo;				//dma号，dma0或dma1
	rcu_periph_enum         rcuDma;				//dma时钟
	dma_channel_enum        dmaTxch;			//dma发送通道号
	dma_channel_enum        dmaRxch;			//dma接收通道号
	dma_subperipheral_enum  dmaPeriph;	        //dma外设通道号 
    uint32_t                periphAddr;         //dma外设地址
	IRQn_Type               dmaTxIrq;			//dma发送通道中断号
	IRQn_Type               dmaRxIrq;			//dma接收通道中断号
	uint32_t                dmaTxPri;			//dma发送优先级
	uint32_t                dmaRxPri;			//dma接收优先级 
}MotorCommuDmaHwInfo_t;

static MotorCommuDmaHwInfo_t  g_MotorDmaBuff[] = 
{
    //水平电机串口2
    {
        .dmaNo      =       DMA0,
        .rcuDma     =       RCU_DMA0,
        .dmaTxch    =       DMA_CH3,
        .dmaRxch    =       DMA_CH1,
        .dmaPeriph  =       DMA_SUBPERI4,
        .periphAddr =       USART2 + 0x04,
        .dmaTxIrq   =       DMA0_Channel3_IRQn,
        .dmaRxIrq   =       DMA0_Channel1_IRQn,
        .dmaTxPri   =       2,//0,1,2,3对于优先级低、中、高、特高
        .dmaRxPri   =       2,
    },
    //垂直电机串口5
    {
        .dmaNo      =       DMA1,
        .rcuDma     =       RCU_DMA1,
        .dmaTxch    =       DMA_CH7,
        .dmaRxch    =       DMA_CH1,
        .dmaPeriph  =       DMA_SUBPERI5,
        .periphAddr =       USART5 + 0x04,
        .dmaTxIrq   =       DMA1_Channel7_IRQn,
        .dmaRxIrq   =       DMA1_Channel1_IRQn,
        .dmaTxPri   =       2,//0,1,2,3对于优先级低、中、高、特高
        .dmaRxPri   =       2,
    },
};

/*
********************************************************************************************************
*                             电机串口通讯缓冲区初始化结构体
********************************************************************************************************
*/
/* dma接收缓冲区 */
static uint8_t g_horiDmaRxBuff[DMA_BUFF_SIZE] = {0};//水平电机DMA接受缓存区

static uint8_t g_vertDmaRxBuff[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区
/* 处理串口通讯与数据缓冲的数据结构 */
static CommuInfo_t g_horiCommuDeal; //水平电机 
static CommuInfo_t g_vertCommuDeal; //垂直电机

typedef struct
{
	CommuInfo_t*            pCommuInfo;			//串口通讯与数据缓冲相关的数据结构
	uint8_t*                dmaRxBuff;          //dma接受缓存区指针
}CommuHwInfo_t;//方便缓冲区初始化的结构体
static CommuHwInfo_t g_commuInfoBuff[] = 
{
    //水平电机
    {
        .pCommuInfo     =       &g_horiCommuDeal,
        .dmaRxBuff      =       g_horiDmaRxBuff,
    },
    //垂直电机串口5
    {
        .pCommuInfo     =       &g_vertCommuDeal,
        .dmaRxBuff      =       g_vertDmaRxBuff,
    },
};

/*
********************************************************************************************************
*                             电机串口通讯驱动初始化
********************************************************************************************************
*/
static void GpioCofig(void)
{
    for(uint8_t i = 0; i < MOTOR_NUM; i++)//i==0初始化水平电机IO，i==1初始化垂直电机io
    {
        /*GPIO时钟初始化*/
        rcu_periph_clock_enable(g_motorCommuBuff[i].rcuGpio);
        /*io复用为串口uart*/
        if ( i == H_MOTOR )
        {
            gpio_af_set(g_motorCommuBuff[i].gpio, GPIO_AF_7, g_motorCommuBuff[i].txPin);
            gpio_af_set(g_motorCommuBuff[i].gpio, GPIO_AF_7, g_motorCommuBuff[i].rxPin);  
        }
        else
        {
            gpio_af_set(g_motorCommuBuff[i].gpio, GPIO_AF_8, g_motorCommuBuff[i].txPin);
            gpio_af_set(g_motorCommuBuff[i].gpio, GPIO_AF_8, g_motorCommuBuff[i].rxPin);        
        }
        /*tx引脚配置*/
        gpio_mode_set(g_motorCommuBuff[i].gpio, GPIO_MODE_AF, GPIO_PUPD_NONE,  g_motorCommuBuff[i].txPin);
        gpio_output_options_set(g_motorCommuBuff[i].gpio, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,  g_motorCommuBuff[i].txPin);
        /*rx引脚配置*/
        gpio_mode_set(g_motorCommuBuff[i].gpio, GPIO_MODE_AF, GPIO_PUPD_NONE,  g_motorCommuBuff[i].rxPin);
        gpio_output_options_set(g_motorCommuBuff[i].gpio, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,  g_motorCommuBuff[i].rxPin);
        /*rs485收发模式切换引脚配置*/
        gpio_mode_set(g_motorCommuBuff[i].gpio, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, g_motorCommuBuff[i].modePin);
        gpio_output_options_set(g_motorCommuBuff[i].gpio, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, g_motorCommuBuff[i].modePin);
    }
}

static void UartCofig(uint32_t baud)
{
    for(uint8_t i = 0; i < MOTOR_NUM; i++)//i==0初始化水平电机IO，i==1初始化垂直电机io
    {
        /*UART时钟初始化*/
        rcu_periph_clock_enable(g_motorCommuBuff[i].rcuUart);
        /*串口复位*/
        usart_deinit (g_motorCommuBuff[i].uartNo);
        /*串口配置*/
        usart_word_length_set(g_motorCommuBuff[i].uartNo, USART_WL_8BIT);
        usart_parity_config(g_motorCommuBuff[i].uartNo, USART_PM_NONE);
        usart_stop_bit_set(g_motorCommuBuff[i].uartNo, USART_STB_1BIT);
        usart_baudrate_set(g_motorCommuBuff[i].uartNo, baud);
        usart_hardware_flow_rts_config(g_motorCommuBuff[i].uartNo, USART_RTS_DISABLE);
        usart_hardware_flow_cts_config(g_motorCommuBuff[i].uartNo, USART_CTS_DISABLE);
        usart_transmit_config(g_motorCommuBuff[i].uartNo, USART_TRANSMIT_ENABLE);
        usart_receive_config(g_motorCommuBuff[i].uartNo, USART_RECEIVE_ENABLE);
        usart_dma_transmit_config(g_motorCommuBuff[i].uartNo, USART_DENT_ENABLE);
        usart_dma_receive_config(g_motorCommuBuff[i].uartNo, USART_DENR_ENABLE);
        /*中断配置*/
        usart_interrupt_enable(g_motorCommuBuff[i].uartNo, USART_INT_IDLE);
        nvic_irq_enable(g_motorCommuBuff[i].uartIrq, 3, 0);
        usart_enable(g_motorCommuBuff[i].uartNo);
    }
}

static void DmaCofig(void)
{
    for(uint8_t i = 0; i < MOTOR_NUM; i++)//i==0初始化水平电机IO，i==1初始化垂直电机io
    {
        dma_single_data_parameter_struct dmaStruct;
        rcu_periph_clock_enable(g_MotorDmaBuff[i].rcuDma);
        /*--------------------------------dma发送，数据完全传输中断初始化---------------------------------*/
        //dma配置
        dma_deinit(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaTxch);
        dmaStruct.direction = DMA_MEMORY_TO_PERIPH;
        dmaStruct.memory0_addr = (uint32_t)0;
        dmaStruct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
        dmaStruct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;	
        dmaStruct.periph_addr = g_MotorDmaBuff[i].periphAddr;
        dmaStruct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
        dmaStruct.number = 0;
        dmaStruct.priority = CHCTL_PRIO(g_MotorDmaBuff[i].dmaTxPri);
        dma_single_data_mode_init(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaTxch, &dmaStruct);
        dma_circulation_disable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaTxch);
        dma_channel_subperipheral_select(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaTxch, g_MotorDmaBuff[i].dmaPeriph);
        //dma中断配置
        if( i == H_MOTOR )
        {
          nvic_irq_enable(g_MotorDmaBuff[H_MOTOR].dmaTxIrq, 2, 2);
        }
        else
        {
          nvic_irq_enable(g_MotorDmaBuff[V_MOTOR].dmaTxIrq, 2, 1);
        }
        dma_interrupt_enable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaTxch, DMA_CHXCTL_FTFIE);//dma发送完成中断

        
        /*--------------------------------dma接收，配合串口空闲中断初始化---------------------------------*/
        //dma配置
        dma_deinit(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch);
        dmaStruct.direction = DMA_PERIPH_TO_MEMORY;
        dmaStruct.memory0_addr = (uint32_t)(g_commuInfoBuff[i].pCommuInfo->pDmaRsvBuff);  
        dmaStruct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
        dmaStruct.number = DMA_BUFF_SIZE;    
        dmaStruct.periph_addr = g_MotorDmaBuff[i].periphAddr;
        dmaStruct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
        dmaStruct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
        dmaStruct.priority = CHCTL_PRIO(g_MotorDmaBuff[i].dmaRxPri);;
        dma_memory_width_config(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, DMA_MEMORY_WIDTH_8BIT);
        dma_periph_width_config(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, DMA_PERIPH_WIDTH_8BIT);
        dma_single_data_mode_init(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, &dmaStruct);
        dma_circulation_enable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch);//循环模式
        dma_channel_subperipheral_select(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, g_MotorDmaBuff[i].dmaPeriph);
        //中断配置
        if ( i == H_MOTOR )
        {
          nvic_irq_enable(g_MotorDmaBuff[H_MOTOR].dmaRxIrq, 1, 2);
        }
        else
        {
          nvic_irq_enable(g_MotorDmaBuff[V_MOTOR].dmaRxIrq, 1, 1);
        }
        dma_interrupt_enable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, DMA_CHXCTL_FTFIE);
        dma_channel_enable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch); 
    }
}

/*
********************************************************************************************************
*                             串口缓冲区初始化
********************************************************************************************************
*/
/*!
    \brief      初始化串口的缓冲区
    \param[in]  none 
    \param[out] none 
    \retval     none
*/
static void CommuStructInit()
{ 
    for(uint8_t i = 0; i < MOTOR_NUM; i++)//i==0初始化水平电机IO，i==1初始化垂直电机io
    {
        /*为属性的参数附初值*/
        CommuInfo_t *pCommuDeal = g_commuInfoBuff[i].pCommuInfo;
        pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
        pCommuDeal->dmaSize = DMA_BUFF_SIZE;
        pCommuDeal->pDmaRsvBuff = g_commuInfoBuff[i].dmaRxBuff;
    }
}

/*!
    \brief      串口进行DMA的数据发送
    \param[in]  devNo：设备号，H_MOTOR：水平电机，V_MOTOR：垂直电机
    \param[in]  buffer：发送缓冲区
    \param[in]  len：发送缓冲区大小
    \param[out] none 
    \retval     none
*/
static void CommuDmaTra(uint8_t devNo, uint8_t *buffer,uint16_t len)
{
	dma_channel_disable(g_MotorDmaBuff[devNo].dmaNo, g_MotorDmaBuff[devNo].dmaTxch);
	
    dma_memory_address_config(g_MotorDmaBuff[devNo].dmaNo, g_MotorDmaBuff[devNo].dmaTxch, DMA_MEMORY_0, (uint32_t)buffer);
	
	dma_transfer_number_config(g_MotorDmaBuff[devNo].dmaNo, g_MotorDmaBuff[devNo].dmaTxch, len);
	
	/*清除DMA发送完成的标志*/
 	dma_flag_clear(g_MotorDmaBuff[devNo].dmaNo, g_MotorDmaBuff[devNo].dmaTxch, DMA_FLAG_FTF);
	usart_flag_clear(g_motorCommuBuff[devNo].uartNo, USART_FLAG_TC);
    
	dma_channel_enable(g_MotorDmaBuff[devNo].dmaNo, g_MotorDmaBuff[devNo].dmaTxch); 
}

/*
********************************************************************************************************
*                             水平电机相关中断服务函数
********************************************************************************************************
*/
/*!
    \brief      this function handles DMA0_Channel3_IRQHandler interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA0_Channel3_IRQHandler(void)
{
/* 
* 发送完成配置的CNT次数后，会进入此中断函数
*/
    if(dma_interrupt_flag_get(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaTxch, DMA_INT_FLAG_FTF)) 
    {
        dma_interrupt_flag_clear(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaTxch, DMA_INT_FLAG_FTF); 

        CommuInfo_t *pCommuDeal = g_commuInfoBuff[H_MOTOR].pCommuInfo;
        while(usart_flag_get(g_motorCommuBuff[H_MOTOR].uartNo, USART_FLAG_TC) == RESET); // 等待串口发送完成
        H_COMMU_RS485_RX; //485切换为接收
        pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
    }
}

/**
***********************************************************
* @brief 串口中断服务函数
* @param
* @return 
***********************************************************
*/
static uint16_t g_horiLastPos;
static uint16_t g_horiNowPos;
void USART2_IRQHandler(void)
{ 				
    /* 串口的接收空闲中断方式进行了数据缓存。*/
    if(RESET != usart_interrupt_flag_get(g_motorCommuBuff[H_MOTOR].uartNo, USART_INT_FLAG_IDLE))
    {
        /* clear IDLE flag */
        usart_interrupt_flag_clear(g_motorCommuBuff[H_MOTOR].uartNo, USART_INT_FLAG_IDLE);  	//第一步，读取stat0寄存器，清除IDLE标志位
        usart_data_receive(g_motorCommuBuff[H_MOTOR].uartNo);  								    //第二步，读取数据寄存器，清除IDLE标志位
   
        g_horiLastPos = g_horiNowPos;
        g_horiNowPos = DMA_BUFF_SIZE - dma_transfer_number_get(g_MotorDmaBuff[H_MOTOR].dmaNo, 
                                                               g_MotorDmaBuff[H_MOTOR].dmaRxch);//函数获取的是还有多少个没传输，而不是已经传输了多少
         //释放信号量，通知能处理数据
        OSSemPost(g_horiMotorMutex);
    } 
}


void DMA0_Channel1_IRQHandler(void)
{  	 
/* 

* 注意：DMA为正常模式，那么当完成一次拷贝后,DMA会自动disable掉。
*/
    if(dma_interrupt_flag_get(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, DMA_INT_FLAG_FTF)) 
    {
        dma_interrupt_flag_clear(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, DMA_INT_FLAG_FTF); 

        
    }
}

/*
********************************************************************************************************
*                             垂直电机相关中断服务函数
********************************************************************************************************
*/
/*!
    \brief      this function handles DMA0_Channel3_IRQHandler interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA1_Channel7_IRQHandler(void)
{
/* 
* 配合串口的发送。功能是继续发送缓冲区未发送的数据。
* 发送完成配置的CNT次数后，会进入此中断函数
*/
    if(dma_interrupt_flag_get(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaTxch, DMA_INT_FLAG_FTF)) 
    {
        dma_interrupt_flag_clear(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaTxch, DMA_INT_FLAG_FTF); 

        CommuInfo_t *pCommuDeal = g_commuInfoBuff[V_MOTOR].pCommuInfo;

        while(usart_flag_get(g_motorCommuBuff[V_MOTOR].uartNo, USART_FLAG_TC) == RESET); // 等待串口发送完成
        V_COMMU_RS485_RX; //485切换为接收
        pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
    }
}

/**
***********************************************************
* @brief 串口中断服务函数
* @param
* @return 
***********************************************************
*/
static uint16_t g_vertLastPos;
static uint16_t g_vertNowPos;
void USART5_IRQHandler(void)
{ 			
    /* 串口的接收空闲中断方式进行了数据缓存。*/
    if(RESET != usart_interrupt_flag_get(g_motorCommuBuff[V_MOTOR].uartNo, USART_INT_FLAG_IDLE))
    {
        /* clear IDLE flag */
        usart_interrupt_flag_clear(g_motorCommuBuff[V_MOTOR].uartNo, USART_INT_FLAG_IDLE);  	//第一步，读取stat0寄存器，清除IDLE标志位
        usart_data_receive(g_motorCommuBuff[V_MOTOR].uartNo);  								//第二步，读取数据寄存器，清除IDLE标志位

        g_vertLastPos = g_vertNowPos;
        g_vertNowPos = DMA_BUFF_SIZE - dma_transfer_number_get(g_MotorDmaBuff[V_MOTOR].dmaNo, 
                                                               g_MotorDmaBuff[V_MOTOR].dmaRxch);
        //释放信号量，通知接收任务处理
        OSSemPost(g_vertMotorMutex);
    } 
}

void DMA1_Channel1_IRQHandler(void)
{  	 
/* 
* 注意：如果DMA为正常模式，那么当完成一次拷贝后,DMA会自动disable掉。
*/
    if(dma_interrupt_flag_get(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch, DMA_INT_FLAG_FTF)) 
    {
        dma_interrupt_flag_clear(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch, DMA_INT_FLAG_FTF); 
    }
}

/*
********************************************************************************************************
*                             外部可调用API接口函数
********************************************************************************************************
*/
/**
* @brief 串口设备通讯驱动初始化
* @param
* @return 
*/
void CommuDrvInit(void)
{
    CommuStructInit();//先初始化这个缓冲区数据结构体，否则硬件配置得不到地址
    GpioCofig();
    UartCofig(57600);
    DmaCofig();
}

/**
* @brief    数据的发送
* @param    motorNo：电机号，H_MOTOR：水平电机，V_MOTOR：垂直电机
* @param    buffer：需要发送的数据
* @param    len：发送数据的长度
* @return   ture:DMA空闲当前数据可以发送；false：DMA正在发送数据，当前数据不能发送
*/
bool CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len)
{
   
     /*简易判断*/
    if( buffer == NULL ||  len == 0 )
    {
      return false;
    }
    /*485切换为发送*/
    if( motorNo == H_MOTOR )
    {
        H_COMMU_RS485_TX;
    }
    else
    {
        V_COMMU_RS485_TX;
    }

    CommuInfo_t *pCommuDeal = g_commuInfoBuff[motorNo].pCommuInfo; 
    /*如果DMA未在发送中，触发发送*/
    if(pCommuDeal->dmaTranFlag == DMA_TRANS_IDLE)
    {

        pCommuDeal->dmaTranFlag = DMA_TRANS_BUSY;
        CommuDmaTra(motorNo, buffer, len);
        return true;
    }
    return false;
}

/**
* @brief    从接收循环缓冲区中读取指定长度的数据到用户数组
* @param    motorNo：电机号，H_MOTOR：水平电机，V_MOTOR：垂直电机
* @param    userBuff：从接收缓冲区中接收数据的数组 
* @param    len：接收数据的长度
* @return   none
*/
bool CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len)
{
    
    CommuInfo_t *pCommuDeal = g_commuInfoBuff[motorNo].pCommuInfo;
    /*简易判断*/
    if( userBuff == NULL ||  len == 0 )
    {
      return false;
    }
    uint16_t horiPosTemp = g_horiLastPos;
    uint16_t vertPosTemp = g_vertLastPos;
    /*接收DMA缓冲区数据*/
    if ( motorNo == H_MOTOR )
    {
        if ( ( (horiPosTemp + len) % DMA_BUFF_SIZE ) != g_horiNowPos )//假如是写寄存器，电机返回消息正常是8字节，但如果是写错误，则返回非8字节错误码
        {
            return false;
        }
        for(uint8_t i = 0;  i < len; i++)
        {
           *(userBuff+i) = pCommuDeal->pDmaRsvBuff[horiPosTemp];
           horiPosTemp = (horiPosTemp + 1) % DMA_BUFF_SIZE;
        }
    }
    else
    {
        if ( ( (vertPosTemp + len) % DMA_BUFF_SIZE ) != g_vertNowPos )//假如是写寄存器，电机返回消息正常是8字节，但如果是写错误，则返回非8字节错误码
        {
            return false;
        }
        for(uint8_t i = 0;  i < len; i++)
        {
           *(userBuff+i) = pCommuDeal->pDmaRsvBuff[vertPosTemp];
           vertPosTemp = (vertPosTemp + 1) % DMA_BUFF_SIZE;
        } 
    } 
    return true;
}
#endif
