伺服电机驱动修改,取消发送缓冲区(数据缓冲和DMA缓冲),将接收缓冲区改为双缓冲区,未测试

This commit is contained in:
REASEARCHER\18383 2025-10-15 10:10:02 +08:00
parent bd1d26de10
commit 3e88c8cd50
11 changed files with 271 additions and 266 deletions

18
.vscode/iar-vsc.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"workspace": {
"path": "${workspaceFolder}\\PROJECT\\OS2.eww"
},
"workspaces": {
"${workspaceFolder}\\PROJECT\\OS2.eww": {
"configs": {
"${workspaceFolder}\\PROJECT\\OS2.ewp": "Debug"
},
"selected": {
"path": "${workspaceFolder}\\PROJECT\\OS2.ewp"
}
}
},
"workbench": {
"path": "D:\\IAR"
}
}

12
.vscode/settings.json vendored
View File

@ -46,6 +46,16 @@
"comm_cfginfo.h": "c",
"servomotor.h": "c",
"pdebug.h": "c",
"limits": "c"
"limits": "c",
"motorcommu.h": "c",
"stdbool.h": "c",
"cfifo.h": "c",
"servomotor_recv.h": "c",
"ucos_ii.h": "c",
"__config": "c",
"utility": "c",
"exception": "c",
"new": "c",
"cstdio": "c"
}
}

View File

@ -2,6 +2,7 @@
#include "ptz_header_file.h"
#include "servoMotor.h"
#ifdef PTZ_SERVO_MOTOR
//电机数据解析任务互斥量

View File

@ -1,15 +1,16 @@
#ifndef __DEVICE_SPEED_SERVOMOTOR_H_
#define __DEVICE_SPEED_SERVOMOTOR_H_
#ifndef __SERVOMOTOR_RECV_H_
#define __SERVOMOTOR_RECV_H_
#include "ptz_type_select.h"
#include "bsp_os.h"
#ifdef PTZ_SERVO_MOTOR
extern BSP_OS_SEM g_horiMotorMutex;
extern BSP_OS_SEM g_vertMotorMutex;
void Init_ServoMotorRecv(void);
#ifdef PTZ_SERVO_MOTOR
void Init_ServoMotorRecv(void);
#endif
#endif
#endif

View File

@ -178,7 +178,7 @@ static void ptz_hori_servo_task()
//发送数据
g_servoMotorLinkList.horiMotor.linkListNum = highPriority;
continue;
}

View File

@ -1,6 +1,5 @@
#include "motorCommu.h"
#include "servoMotor_recv.h"
/*
********************************************************************************************************
@ -99,11 +98,12 @@ static MotorCommuDmaHwInfo_t g_MotorDmaBuff[] =
*
********************************************************************************************************
*/
/* dma发送与接收缓冲区 */
static uint8_t g_horiDmaTxBuff[DMA_BUFF_SIZE] = {0};//水平电机DMA发送缓存区
static uint8_t g_horiDmaRxBuff[DMA_BUFF_SIZE] = {0};//水平电机DMA接受缓存区
static uint8_t g_vertDmaTxBuff[DMA_BUFF_SIZE] = {0};//垂直电机DMA发送缓存区
static uint8_t g_vertDmaRxBuff[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区
/* dma接收缓冲区 */
static uint8_t g_horiDmaRxBuff1[DMA_BUFF_SIZE] = {0};//水平电机DMA接受缓存区1
static uint8_t g_horiDmaRxBuff2[DMA_BUFF_SIZE] = {0};//水平电机DMA接受缓存区2
static uint8_t g_vertDmaRxBuff1[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区1
static uint8_t g_vertDmaRxBuff2[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区2
/* 处理串口通讯与数据缓冲的数据结构 */
static CommuInfo_t g_horiCommuDeal; //水平电机
static CommuInfo_t g_vertCommuDeal; //垂直电机
@ -111,22 +111,22 @@ static CommuInfo_t g_vertCommuDeal; //
typedef struct
{
CommuInfo_t* pCommuInfo; //串口通讯与数据缓冲相关的数据结构
uint8_t* dmaTxBuff; //dma发送缓存区指针
uint8_t* dmaRxBuff; //dma接受缓存区指针
uint8_t* dmaRxBuff1; //dma接受缓存区1指针
uint8_t* dmaRxBuff2; //dma接受缓存区2指针
}CommuHwInfo_t;//方便缓冲区初始化的结构体
static CommuHwInfo_t g_commuInfoBuff[] =
{
//水平电机
{
.pCommuInfo = &g_horiCommuDeal,
.dmaTxBuff = g_horiDmaTxBuff,
.dmaRxBuff = g_horiDmaRxBuff,
.dmaRxBuff1 = g_horiDmaRxBuff1,
.dmaRxBuff2 = g_horiDmaRxBuff2,
},
//垂直电机串口5
{
.pCommuInfo = &g_vertCommuDeal,
.dmaTxBuff = g_vertDmaTxBuff,
.dmaRxBuff = g_vertDmaRxBuff,
.dmaRxBuff1 = g_vertDmaRxBuff1,
.dmaRxBuff2 = g_vertDmaRxBuff2,
},
};
@ -219,7 +219,7 @@ static void DmaCofig(void)
//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.memory0_addr = (uint32_t)(g_commuInfoBuff[i].pCommuInfo->pDmaRsvBuff1);
dmaStruct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dmaStruct.number = DMA_BUFF_SIZE;
dmaStruct.periph_addr = g_MotorDmaBuff[i].periphAddr;
@ -229,7 +229,7 @@ static void DmaCofig(void)
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_circulation_disable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch);//循环模式
dma_channel_subperipheral_select(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, g_MotorDmaBuff[i].dmaPeriph);
//中断配置
nvic_irq_enable(g_MotorDmaBuff[i].dmaRxIrq, 4, 2);
@ -244,7 +244,7 @@ static void DmaCofig(void)
********************************************************************************************************
*/
/*!
\brief DMA的空间大小DMA偏移量
\brief
\param[in] none
\param[out] none
\retval none
@ -255,13 +255,11 @@ static void CommuStructInit()
{
/*为属性的参数附初值*/
CommuInfo_t *pCommuDeal = g_commuInfoBuff[i].pCommuInfo;
pCommuDeal->dmaOffset = 0;
pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
pCommuDeal->dmaSize = DMA_BUFF_SIZE;
pCommuDeal->pDmaRsvBuff = g_commuInfoBuff[i].dmaRxBuff;
pCommuDeal->pDmaTraBuff = g_commuInfoBuff[i].dmaTxBuff;
CfifoBuffInit(&pCommuDeal->dataRsvCfifo); //用于数据接受
CfifoBuffInit(&pCommuDeal->dataTraCfifo); //用于数据发送
pCommuDeal->pDmaRsvBuff1 = g_commuInfoBuff[i].dmaRxBuff1;
pCommuDeal->pDmaRsvBuff2 = g_commuInfoBuff[i].dmaRxBuff2;
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT1;//默认使用缓冲区1
}
}
@ -305,25 +303,14 @@ void DMA0_Channel3_IRQHandler(void)
*
* CNT次数后
*/
int32_t TransNum = 0;//从&pCommuDeal->dataTraCfifo.BUFF[]中获取多少数据
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;
/*从发送循环缓冲区中获取数据*/
TransNum = CfifoBuffRead(&pCommuDeal->dataTraCfifo,(char *)(pCommuDeal->pDmaTraBuff),pCommuDeal->dmaSize);
if(TransNum > 0)
{
CommuDmaTra(H_MOTOR, pCommuDeal->pDmaTraBuff, TransNum);
}
else
{
while(usart_flag_get(g_motorCommuBuff[H_MOTOR].uartNo, USART_FLAG_TC) == RESET); // 等待串口发送完成
H_COMMU_RS485_RX; //485切换为接收
pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
}
while(usart_flag_get(g_motorCommuBuff[H_MOTOR].uartNo, USART_FLAG_TC) == RESET); // 等待串口发送完成
H_COMMU_RS485_RX; //485切换为接收
pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
}
}
@ -334,43 +321,47 @@ void DMA0_Channel3_IRQHandler(void)
* @return
***********************************************************
*/
static uint16_t g_hFrameRcvNum = 0;
void USART2_IRQHandler(void)
{
/* 串口的接收空闲中断方式进行了数据缓存。*/
int32_t RecvNum = 0;//dma缓冲区收到多少数据
int32_t WriteNum = 0;//向数据循环接收区写入的数据数正常WriteNum==RecvNum
int32_t DmaIdleNum = 0;//dmasize减已经传输的数据就是DmaIdleNum
dma_single_data_parameter_struct dmaStruct;
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_hFrameRcvNum++;
//释放信号量,通知接收到一包数据,任务可以处理了
OSSemPost(g_horiMotorMutex);
CommuInfo_t *pCommuDeal = g_commuInfoBuff[H_MOTOR].pCommuInfo;
/* 切换使用接收缓冲区,这样上层的解析永远从第一个字节起*/
if ( pCommuDeal->pDmaRsvBuffSelect == DMA_RSVBUFF_SELECT1 )
{
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT2;
dma_channel_disable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff2);
dma_single_data_mode_init(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);
}
else
{
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT1;
dma_channel_disable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff1);
dma_single_data_mode_init(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);
}
/*计算在DMA缓冲区需要获取的数据长度*/
DmaIdleNum = dma_transfer_number_get(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//获取的是还有多少个没传输,而不是已经传输了多少
RecvNum = pCommuDeal->dmaSize - DmaIdleNum - pCommuDeal->dmaOffset;
/*将获取到的数据放到数据接收缓冲区中*/
WriteNum = CfifoBuffWrite(&pCommuDeal->dataRsvCfifo,(char *)(pCommuDeal->pDmaRsvBuff + pCommuDeal->dmaOffset), RecvNum);
if(WriteNum != RecvNum)
{
printf("Uart ReadFifo is not enough\r\n");
}
/*计算获取数据位置的偏移量*/
pCommuDeal->dmaOffset += RecvNum;
// DmaIdleNum = dma_transfer_number_get(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//获取的是还有多少个没传输,而不是已经传输了多少
}
}
void DMA0_Channel1_IRQHandler(void)
{
int32_t RecvNum = 0;
int32_t WriteNum = 0;
dma_single_data_parameter_struct dmaStruct;
/*
* 1DMA的偏移量
* 11 pUartAttr->DamOffset置为0;
@ -383,17 +374,28 @@ void DMA0_Channel1_IRQHandler(void)
dma_interrupt_flag_clear(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, DMA_INT_FLAG_FTF);
CommuInfo_t *pCommuDeal = g_commuInfoBuff[H_MOTOR].pCommuInfo;
/* 将dma缓冲马上传输完成剩余最后一截子的数据拷贝到缓冲区中在进行偏移量的复位 */
RecvNum = pCommuDeal->dmaSize - pCommuDeal->dmaOffset;
/*将获取到的数据放到数据接收缓冲区中*/
WriteNum = CfifoBuffWrite(&pCommuDeal->dataRsvCfifo,(char *)(pCommuDeal->pDmaRsvBuff + pCommuDeal->dmaOffset), RecvNum);
if(WriteNum != RecvNum)
//释放信号量,通知接收到一包数据,任务可以处理了
OSSemPost(g_horiMotorMutex);
/* 切换使用接收缓冲区,这样上层的解析永远从第一个字节起*/
if ( pCommuDeal->pDmaRsvBuffSelect == DMA_RSVBUFF_SELECT1 )
{
/*add deal here*/
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT2;
dma_channel_disable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff2);
dmaStruct.number = DMA_BUFF_SIZE;
dma_single_data_mode_init(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);
}
/*复位DMA偏移量*/
pCommuDeal->dmaOffset = 0;
else
{
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT1;
dma_channel_disable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff1);
dmaStruct.number = DMA_BUFF_SIZE;
dma_single_data_mode_init(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);
}
}
}
@ -414,27 +416,16 @@ void DMA1_Channel7_IRQHandler(void)
*
* CNT次数后
*/
int32_t TransNum = 0;//从&pCommuDeal->dataTraCfifo.BUFF[]中获取多少数据
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;
/*从发送循环缓冲区中获取数据*/
TransNum = CfifoBuffRead(&pCommuDeal->dataTraCfifo,(char *)(pCommuDeal->pDmaTraBuff),pCommuDeal->dmaSize);
if(TransNum > 0)
{
CommuDmaTra(V_MOTOR, pCommuDeal->pDmaTraBuff, TransNum);
}
else
{
while(usart_flag_get(g_motorCommuBuff[V_MOTOR].uartNo, USART_FLAG_TC) == RESET); // 等待串口发送完成
V_COMMU_RS485_RX; //485切换为接收
pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
}
// pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
// V_COMMU_RS485_RX; //485切换为接收
while(usart_flag_get(g_motorCommuBuff[V_MOTOR].uartNo, USART_FLAG_TC) == RESET); // 等待串口发送完成
V_COMMU_RS485_RX; //485切换为接收
pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
}
}
@ -445,35 +436,36 @@ void DMA1_Channel7_IRQHandler(void)
* @return
***********************************************************
*/
static uint16_t g_vFrameRcvNum = 0;
void USART5_IRQHandler(void)
{
{
dma_single_data_parameter_struct dmaStruct;
/* 串口的接收空闲中断方式进行了数据缓存。*/
int32_t RecvNum = 0;//dma缓冲区收到多少数据
int32_t WriteNum = 0;//向数据循环接收区写入的数据数正常WriteNum==RecvNum
int32_t DmaIdleNum = 0;//dmasize减已经传输的数据就是DmaIdleNum
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_vFrameRcvNum++;
//释放信号量,通知接收到一包数据,任务可以处理了
OSSemPost(g_horiMotorMutex);
CommuInfo_t *pCommuDeal = g_commuInfoBuff[V_MOTOR].pCommuInfo;
/*计算在DMA缓冲区需要获取的数据长度*/
DmaIdleNum = dma_transfer_number_get(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);//获取的是还有多少个没传输,而不是已经传输了多少
RecvNum = pCommuDeal->dmaSize - DmaIdleNum - pCommuDeal->dmaOffset;
/*将获取到的数据放到数据接收缓冲区中*/
WriteNum = CfifoBuffWrite(&pCommuDeal->dataRsvCfifo,(char *)(pCommuDeal->pDmaRsvBuff + pCommuDeal->dmaOffset), RecvNum);
if(WriteNum != RecvNum)
/* 切换使用接收缓冲区,这样上层的解析永远从第一个字节起*/
if ( pCommuDeal->pDmaRsvBuffSelect == DMA_RSVBUFF_SELECT1 )
{
printf("Uart ReadFifo is not enough\r\n");
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT2;
dma_channel_disable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff2);
dma_single_data_mode_init(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);
}
else
{
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT1;
dma_channel_disable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff1);
dma_single_data_mode_init(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);
}
/*计算获取数据位置的偏移量*/
pCommuDeal->dmaOffset += RecvNum;
}
}
@ -481,8 +473,7 @@ void USART5_IRQHandler(void)
// uint8_t rx_OK = 0;
void DMA1_Channel1_IRQHandler(void)
{
int32_t RecvNum = 0;
int32_t WriteNum = 0;
dma_single_data_parameter_struct dmaStruct;
/*
* 1DMA的偏移量
* 11 pUartAttr->DamOffset置为0;
@ -493,20 +484,31 @@ void DMA1_Channel1_IRQHandler(void)
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);
// rx_OK++;
CommuInfo_t *pCommuDeal = g_commuInfoBuff[V_MOTOR].pCommuInfo;
/* 将dma缓冲马上传输完成剩余最后一截子的数据拷贝到缓冲区中在进行偏移量的复位 */
RecvNum = pCommuDeal->dmaSize - pCommuDeal->dmaOffset;
/*将获取到的数据放到数据接收缓冲区中*/
WriteNum = CfifoBuffWrite(&pCommuDeal->dataRsvCfifo,(char *)(pCommuDeal->pDmaRsvBuff + pCommuDeal->dmaOffset), RecvNum);
if(WriteNum != RecvNum)
//释放信号量,通知接收到一包数据,任务可以处理了
OSSemPost(g_horiMotorMutex);
/* 切换使用接收缓冲区,这样上层的解析永远从第一个字节起*/
if ( pCommuDeal->pDmaRsvBuffSelect == DMA_RSVBUFF_SELECT1 )
{
/*add deal here*/
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT2;
dma_channel_disable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff2);
dmaStruct.number = DMA_BUFF_SIZE;
dma_single_data_mode_init(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);
}
/*复位DMA偏移量*/
pCommuDeal->dmaOffset = 0;
else
{
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT1;
dma_channel_disable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);//切换缓冲区配置缓冲区长度需要先禁用DMA
dmaStruct.memory0_addr = (uint32_t)(pCommuDeal->pDmaRsvBuff1);
dmaStruct.number = DMA_BUFF_SIZE;
dma_single_data_mode_init(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch, &dmaStruct);
dma_channel_enable(g_MotorDmaBuff[V_MOTOR].dmaNo, g_MotorDmaBuff[V_MOTOR].dmaRxch);
}
}
}
@ -529,20 +531,14 @@ void CommuDrvInit(void)
}
/**
* @brief
1
* @brief
* @param motorNoH_MOTORV_MOTOR
* @param buffer
* @param len(DMA_BUFF_SIZE)
* @return
* @param buffer
* @param len
* @return ture:DMA空闲当前数据可以发送falseDMA正在发送数据
*/
int32_t CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len)
bool CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len)
{
int32_t TransNum = 0;
int32_t TransLen = 0;
// 使用数组为每个电机分配独立的计数器
static int32_t s_addUpDataNum[MOTOR_NUM] = {0};
if( motorNo == H_MOTOR )
{
H_COMMU_RS485_TX;
@ -553,22 +549,15 @@ int32_t CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len)
}
CommuInfo_t *pCommuDeal = g_commuInfoBuff[motorNo].pCommuInfo;
/*将要发送的数据写入循环缓冲区*/
TransNum = CfifoBuffWrite(&pCommuDeal->dataTraCfifo, (char *) buffer, len);
s_addUpDataNum[motorNo] += len; // 使用对应电机的计数器
/*如果DMA未在发送中触发发送*/
if(pCommuDeal->dmaTranFlag == DMA_TRANS_IDLE)
{
TransLen = CfifoBuffRead(&pCommuDeal->dataTraCfifo,(char *)(pCommuDeal->pDmaTraBuff), s_addUpDataNum[motorNo]);
s_addUpDataNum[motorNo] = 0; // 清零对应电机的计数器
if(TransLen > 0)
{
pCommuDeal->dmaTranFlag = DMA_TRANS_BUSY;
CommuDmaTra(motorNo, pCommuDeal->pDmaTraBuff, TransLen);
}
pCommuDeal->dmaTranFlag = DMA_TRANS_BUSY;
CommuDmaTra(motorNo, buffer, len);
return true;
}
return TransNum;
return false;
}
/**
@ -580,55 +569,14 @@ int32_t CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len)
*/
void CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len)
{
// if ( motorNo == H_MOTOR )
// {
// H_COMMU_RS485_RX;
// }
// else
// {
// V_COMMU_RS485_RX;
// }
CommuInfo_t *pCommuDeal = g_commuInfoBuff[motorNo].pCommuInfo;
CfifoBuffRead(&pCommuDeal->dataRsvCfifo, (char*)userBuff, len);
}
/**
* @brief 485422,
* @param motorNoH_MOTORV_MOTOR
* @return rs485或rs422接收到的包数量
*/
uint16_t GetRsvFrameNum(uint8_t motorNo)
{
if (motorNo == H_MOTOR)
if ( pCommuDeal->pDmaRsvBuffSelect == DMA_RSVBUFF_SELECT1 )
{
return g_hFrameRcvNum;
memcpy(userBuff, pCommuDeal->pDmaRsvBuff2, len);
}
return g_vFrameRcvNum;
}
/**
* @brief 485422,
1
* @param motorNoH_MOTORV_MOTOR
* @return null
*/
void DecRsvFrameNum(uint8_t motorNo)
{
if (motorNo == H_MOTOR && g_hFrameRcvNum > 0)
else
{
g_hFrameRcvNum--;
return;
}
if (motorNo == V_MOTOR && g_vFrameRcvNum > 0)
{
g_vFrameRcvNum--;
return;
memcpy(userBuff, pCommuDeal->pDmaRsvBuff1, len);
}
}
///*用于结构体数组赋值,方便外部使用此结构体数组*/
//CommuHwInfo_t GetMotorCommuBuffStr(uint8_t motorNo)
//{
// return g_commuInfoBuff[motorNo];
//}

View File

@ -1,8 +1,9 @@
#ifndef _MOTORCOMMU_
#define _MOTORCOMMU_
#ifndef _MOTORCOMMU_H_
#define _MOTORCOMMU_H_
#include "gd32f4xx.h"
#include "cfifo.h"
#include "stdbool.h"
#include "string.h"
/*
********************************************************************************************************
* dma缓冲区相关
@ -11,18 +12,19 @@
typedef struct
{
int16_t dmaTranFlag; /*dma发送是否在工作的标志位*/
int32_t dmaSize; /*DMA缓冲区的大小*/
int32_t dmaOffset; /*获取数据在DMA缓冲区的偏移量*/
uint8_t *pDmaRsvBuff; /*指向接收DMA缓冲区的首地址*/
uint8_t *pDmaTraBuff; /*指向发送DMA缓冲区的首地址*/
CfifoBuff dataRsvCfifo; /*接受数据的循环缓冲区,串口---dma搬运--->pDmaRsvBuff[]--->dataRsvCfifo.BUFF[]*/
CfifoBuff dataTraCfifo; /*发送数据的循环缓冲区dataTraCfifo.BUFF[]--->pDmaTraBuff[]---dma搬运--->串口*/
int32_t dmaSize; /*DMA接收缓冲区的大小*/
uint8_t *pDmaRsvBuff1; /*指向接收DMA缓冲区1的首地址*/
uint8_t *pDmaRsvBuff2; /*指向接收DMA缓冲区2的首地址*/
uint8_t pDmaRsvBuffSelect; /*表示当前正在使用哪个接收缓冲区*/
}CommuInfo_t;
#define DMA_TRANS_IDLE 0//dma当前未在发送数据
#define DMA_TRANS_BUSY 1//dma当前正在发送数据
#define DMA_BUFF_SIZE 256//dma缓冲区大小
extern CommuInfo_t g_commuDeal;//来自motorCommu.c
#define DMA_BUFF_SIZE 64//dma缓冲区大小
#define DMA_RSVBUFF_SELECT1 (uint8_t)(0)//当前使用dma接收缓冲区1
#define DMA_RSVBUFF_SELECT2 (uint8_t)(1)//当前使用dma接收缓冲区2
//extern CommuInfo_t g_commuDeal;//来自motorCommu.c
/*
********************************************************************************************************
@ -46,14 +48,13 @@ extern CommuInfo_t g_commuDeal;//
void CommuDrvInit(void);
/**
* @brief
1
* @brief
* @param motorNoH_MOTORV_MOTOR
* @param buffer
* @param len(DMA_BUFF_SIZE)
* @return
* @param buffer
* @param len
* @return ture:DMA空闲当前数据可以发送falseDMA正在发送数据
*/
int32_t CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len);
bool CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len);
/**
* @brief
@ -64,20 +65,6 @@ int32_t CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len);
*/
void CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len);
/**
* @brief 485422,
* @param motorNoH_MOTORV_MOTOR
* @return rs485或rs422接收到的包数量
*/
uint16_t GetRsvFrameNum(uint8_t motorNo);
/**
* @brief 485422,
1
* @param motorNoH_MOTORV_MOTOR
* @return null
*/
void DecRsvFrameNum(uint8_t motorNo);
///*用于结构体数组赋值,方便外部使用此结构体数组*/
//CommuHwInfo_t GetMotorCommuBuffStr(uint8_t motorNo);

View File

@ -16,40 +16,69 @@ static void MotorSwitchGpioCofig(void)
}
static uint8_t g_writeOneRegBuff[WRITE_ONE_REG_BUFFNUM];//由于写寄存器,电机返回的数据和写入的数据完全一致,故设此数组
/*
crc校验高位 crc校验低位
01H 06H 02H 00H 00H 01H 49H B2H
*/
/**
* @brief
* @param motorNo
* @param regAddr
* @param data
* @return false:DMA正在发送数据
*/
bool WriteMotorOneReg(uint8_t motorNo, uint16_t regAddr, uint16_t data)
{
uint8_t frameBuff[8] = {0};
uint8_t replyTemp[8];
uint8_t frameBuff[WRITE_ONE_REG_BUFFNUM] = {0};
uint16_t crc;
frameBuff[0] = 0x01;//由于采用一主一从模式所以水平电机垂直电机从机地址都是0x01云台后期也不会扩展
frameBuff[1] = WRITE_ONE_REG;
frameBuff[2] = regAddr >> 8;
frameBuff[3] = regAddr & 0xff;
frameBuff[4] = data >> 8;
frameBuff[5] = data & 0xff;
crc = ModbusCRC16(frameBuff, 6);
frameBuff[6] = crc & 0xff;
frameBuff[7] = crc >> 8;
CommuTransData(motorNo, frameBuff, 8);
OSTimeDlyHMSM(0u, 0u, 0u, 5u);//发送数据完成需要2ms接收电机返回的数据需要2ms延时5ms足够
CommuRsvData(motorNo, replyTemp, 8);//数据缓冲区已经有数据了才能调用此函数提取数据进行解析
for( uint8_t i = 0; i < 8; i++)
if ( motorNo == H_MOTOR )
{
if ( frameBuff[i] != replyTemp[i] )
{
H_MOTOR_STOP;
return false;
}
frameBuff[0] = H_MOTOR_ADDR;//由于采用一主一从模式所以水平电机垂直电机从机地址都是0x01云台后期也不会扩展
}
else
{
frameBuff[0] = V_MOTOR_ADDR;
}
frameBuff[1] = WRITE_ONE_REG;
frameBuff[2] = regAddr >> WRITE_ONE_REG_BUFFNUM;
frameBuff[3] = regAddr & 0xff;
frameBuff[4] = data >> WRITE_ONE_REG_BUFFNUM;
frameBuff[5] = data & 0xff;
crc = ModbusCRC16(frameBuff, WRITE_ONE_REG_BUFFNUM - 2);
frameBuff[6] = crc & 0xff;
frameBuff[7] = crc >> WRITE_ONE_REG_BUFFNUM;
if ( CommuTransData(motorNo, frameBuff, WRITE_ONE_REG_BUFFNUM) == false)
{
return false;
}
memcpy(g_writeOneRegBuff, frameBuff, WRITE_ONE_REG_BUFFNUM);
return true;
}
/**
* @brief
* @param motorNo
* @param userBuff
* @param lenWRITE_ONE_REG_BUFFNUM
* @return false:
*/
bool MotorReplyForWrite(uint8_t motorNo, uint8_t* userBuff, uint8_t len)
{
CommuRsvData(motorNo, userBuff, len);
for( uint8_t i = 0; i < len; i++ )
{
if ( userBuff[i] != g_writeOneRegBuff[i] )
{
memset(g_writeOneRegBuff, 0x00, len);
return false;
}
}
memset(g_writeOneRegBuff, 0x00, len);
return true;
}
/**
@ -67,14 +96,20 @@ void servoMotorInit(void)
CommuDrvInit();//伺服电机RS485通讯初始化
OSTimeDlyHMSM(0u, 0u, 0u, 500u);//等待硬件初始化成功
WriteMotorOneReg(H_MOTOR, H02_CONTR_MODE_SELEC, 0);//H0200,选择速度模式
OSTimeDlyHMSM(0u, 0u, 0u, 5u);
WriteMotorOneReg(H_MOTOR, H04_DO1_FUNC_SELEC, 19);
OSTimeDlyHMSM(0u, 0u, 0u, 5u);
WriteMotorOneReg(H_MOTOR, H06_SPEED_COMMU_SET_VALUE, 100);//速度设置为100rpm
OSTimeDlyHMSM(0u, 0u, 0u, 5u);
WriteMotorOneReg(H_MOTOR, H06_SPEED_UP_SLOPE_VALUE, 3000);//加速度3000
OSTimeDlyHMSM(0u, 0u, 0u, 5u);
WriteMotorOneReg(H_MOTOR, H06_SPEED_DOWN_SLOPE_VALUE, 2000);//减速度2000
WriteMotorOneReg(H_MOTOR, H03_DI1_LOGICAL_SELEC, 1);//只启动水平电机
OSTimeDlyHMSM(0u, 0u, 0u, 5u);
WriteMotorOneReg(H_MOTOR, H03_DI1_LOGICAL_SELEC, 1);//只启动水平电机
OSTimeDlyHMSM(0u, 0u, 0u, 5u);
}

View File

@ -14,6 +14,8 @@
#define V_MOTOR_OPEN gpio_bit_set(GPIOE,GPIO_PIN_1) //垂直电机电源打开
#define V_MOTOR_STOP gpio_bit_reset(GPIOE,GPIO_PIN_1) //垂直电机电源关闭
#define H_MOTOR_ADDR 0x01//水平电机地址
#define V_MOTOR_ADDR 0x01//垂直电机地址
/*
********************************************************************************************************
*
@ -24,6 +26,7 @@
#define WRITE_ONE_REG 0X06//写单个寄存器
#define WRITE_MULT_CONSE_REG 0x10//写多个连续的寄存器
#define WRITE_ONE_REG_BUFFNUM 8//写单个寄存器,数据帧的字节个数
/*
********************************************************************************************************
*
@ -49,12 +52,26 @@
#define H0C_COMMU_PARAM_EEPR_UPDATE 0X0C13//MODBUS通讯写入是否更新到 EEPROM设置1为写入
/*
crc校验高位 crc校验低位
01H 06H 02H 00H 00H 01H 49H B2H
/**
* @brief
* @param motorNo
* @param regAddr
* @param data
* @return false:DMA正在发送数据
*/
bool WriteMotorOneReg(uint8_t motorNo, uint16_t regAddr, uint16_t data);
/**
* @brief
* @param motorNo
* @param userBuff
* @param lenWRITE_ONE_REG_BUFFNUM
* @return false:
*/
bool MotorReplyForWrite(uint8_t motorNo, uint8_t* userBuff, uint8_t len);
/**
* @brief
* @param

View File

@ -826,7 +826,7 @@
</option>
<option>
<name>IlinkIcfFile</name>
<state>D:\psx\Pan-Tilt\1.software\HY\new_ptz\servoMotor\BSP\IAR\GD32F450xE.icf</state>
<state>D:\CompanyCode\newPro\servoMotor_xr\BSP\IAR\GD32F450xE.icf</state>
</option>
<option>
<name>IlinkIcfFileSlave</name>
@ -2927,12 +2927,6 @@
</group>
<group>
<name>servoMotor</name>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\cfifo.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\cfifo.h</name>
</file>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\modbus_crc.c</name>
</file>

View File

@ -3196,12 +3196,6 @@
</group>
<group>
<name>servoMotor</name>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\cfifo.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\cfifo.h</name>
</file>
<file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\modbus_crc.c</name>
</file>