Compare commits

..

No commits in common. "d9df2548c5d05800ba22eb48bf129183af28a512" and "bd1d26de10c957246aa68d2d0dbf6bfa52dd34eb" have entirely different histories.

11 changed files with 279 additions and 329 deletions

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

@ -1,18 +0,0 @@
{
"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,16 +46,6 @@
"comm_cfginfo.h": "c", "comm_cfginfo.h": "c",
"servomotor.h": "c", "servomotor.h": "c",
"pdebug.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,7 +2,6 @@
#include "ptz_header_file.h" #include "ptz_header_file.h"
#include "servoMotor.h" #include "servoMotor.h"
#ifdef PTZ_SERVO_MOTOR #ifdef PTZ_SERVO_MOTOR
//电机数据解析任务互斥量 //电机数据解析任务互斥量
@ -78,7 +77,7 @@ static void creat_task_servo_recv_task(void)
(INT8U ) TASK_RECV_VERT_SERVO_PRIO, (INT8U ) TASK_RECV_VERT_SERVO_PRIO,
(INT16U ) TASK_RECV_VERT_SERVO_PRIO, (INT16U ) TASK_RECV_VERT_SERVO_PRIO,
(OS_STK *)&task_recv_vert_servo_stk[0], (OS_STK *)&task_recv_vert_servo_stk[0],
(INT32U ) TASK_RECV_HORI_SERVO_STK_SIZE, (INT32U ) task_recv_vert_servo_stk,
(void *) 0, (void *) 0,
(INT16U )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR)); (INT16U )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
#if (OS_TASK_NAME_EN > 0) #if (OS_TASK_NAME_EN > 0)

View File

@ -1,16 +1,15 @@
#ifndef __SERVOMOTOR_RECV_H_ #ifndef __DEVICE_SPEED_SERVOMOTOR_H_
#define __SERVOMOTOR_RECV_H_ #define __DEVICE_SPEED_SERVOMOTOR_H_
#include "ptz_type_select.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);
#endif #ifdef PTZ_SERVO_MOTOR
void Init_ServoMotorRecv(void);
#endif
#endif #endif

View File

@ -105,9 +105,6 @@ void servoSendData(uint8_t motor,uint8_t *data, uint8_t dataLen, uint8_t priorit
//保存数据到链表节点 //保存数据到链表节点
ptr = OSMemGet(g_memPtr, &err); ptr = OSMemGet(g_memPtr, &err);
if (ptr == NULL) {
return;
}
ptr->length = dataLen; ptr->length = dataLen;
memcpy(ptr->data, data, dataLen); memcpy(ptr->data, data, dataLen);
@ -179,8 +176,8 @@ static void ptz_hori_servo_task()
// 高优先级链表中数据先发送 // 高优先级链表中数据先发送
if (g_servoMotorLinkList.horiMotor.LinkListHead_H != NULL) { if (g_servoMotorLinkList.horiMotor.LinkListHead_H != NULL) {
//发送数据 //发送数据
CommuTransData(H_MOTOR, g_servoMotorLinkList.horiMotor.LinkListHead_H->data
, g_servoMotorLinkList.horiMotor.LinkListHead_H->length);
g_servoMotorLinkList.horiMotor.linkListNum = highPriority; g_servoMotorLinkList.horiMotor.linkListNum = highPriority;
continue; continue;
@ -189,8 +186,8 @@ static void ptz_hori_servo_task()
// 高优先级链表中无数据时,发送低优先级中的数据 // 高优先级链表中无数据时,发送低优先级中的数据
if (g_servoMotorLinkList.horiMotor.LinkListHead_L != NULL) { if (g_servoMotorLinkList.horiMotor.LinkListHead_L != NULL) {
//发送数据 //发送数据
CommuTransData(H_MOTOR, g_servoMotorLinkList.horiMotor.LinkListHead_L->data
, g_servoMotorLinkList.horiMotor.LinkListHead_L->length);
g_servoMotorLinkList.horiMotor.linkListNum = lowPriority; g_servoMotorLinkList.horiMotor.linkListNum = lowPriority;
} }
@ -217,8 +214,8 @@ static void ptz_vert_servo_task()
// 高优先级链表中数据先发送 // 高优先级链表中数据先发送
if (g_servoMotorLinkList.vertMotor.LinkListHead_H != NULL) { if (g_servoMotorLinkList.vertMotor.LinkListHead_H != NULL) {
g_servoMotorLinkList.vertMotor.linkListNum = highPriority; g_servoMotorLinkList.vertMotor.linkListNum = highPriority;
CommuTransData(V_MOTOR, g_servoMotorLinkList.vertMotor.LinkListHead_H->data
, g_servoMotorLinkList.vertMotor.LinkListHead_H->length);
continue; continue;
} }
@ -226,8 +223,8 @@ static void ptz_vert_servo_task()
// 高优先级链表中无数据时,发送低优先级中的数据 // 高优先级链表中无数据时,发送低优先级中的数据
if (g_servoMotorLinkList.vertMotor.LinkListHead_L != NULL) { if (g_servoMotorLinkList.vertMotor.LinkListHead_L != NULL) {
g_servoMotorLinkList.vertMotor.linkListNum = highPriority; g_servoMotorLinkList.vertMotor.linkListNum = highPriority;
CommuTransData(V_MOTOR, g_servoMotorLinkList.vertMotor.LinkListHead_L->data
, g_servoMotorLinkList.vertMotor.LinkListHead_L->length);
} }
else { else {
@ -327,14 +324,6 @@ void init_speed_module(void)
creat_task_hori_servo_task(); creat_task_hori_servo_task();
creat_task_vert_servo_task(); creat_task_vert_servo_task();
OSTimeDlyHMSM(0u, 0u, 0u, 500u);//等待硬件初始化成功
uint8_t buffer[20] = {0};
uint8_t buffer_len = 0;
buffer_len = WriteMotorOneReg_buffer(H_MOTOR, H02_CONTR_MODE_SELEC, 0, buffer);
servoSendData(horiMotorType, buffer, buffer_len, lowPriority);
} }

View File

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

View File

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

View File

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

View File

@ -14,8 +14,6 @@
#define V_MOTOR_OPEN gpio_bit_set(GPIOE,GPIO_PIN_1) //垂直电机电源打开 #define V_MOTOR_OPEN gpio_bit_set(GPIOE,GPIO_PIN_1) //垂直电机电源打开
#define V_MOTOR_STOP gpio_bit_reset(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//垂直电机地址
/* /*
******************************************************************************************************** ********************************************************************************************************
* *
@ -26,7 +24,6 @@
#define WRITE_ONE_REG 0X06//写单个寄存器 #define WRITE_ONE_REG 0X06//写单个寄存器
#define WRITE_MULT_CONSE_REG 0x10//写多个连续的寄存器 #define WRITE_MULT_CONSE_REG 0x10//写多个连续的寄存器
#define WRITE_ONE_REG_BUFFNUM 8//写单个寄存器,数据帧的字节个数
/* /*
******************************************************************************************************** ********************************************************************************************************
* *
@ -52,32 +49,18 @@
#define H0C_COMMU_PARAM_EEPR_UPDATE 0X0C13//MODBUS通讯写入是否更新到 EEPROM设置1为写入 #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); 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 * @brief
* @param * @param
* @return * @return
*/ */
void servoMotorInit(void); void servoMotorInit(void);
uint8_t WriteMotorOneReg_buffer(uint8_t motorNo, uint16_t regAddr, uint16_t data, uint8_t *frameBuff);
#endif #endif

View File

@ -741,7 +741,7 @@
<data> <data>
<extensions></extensions> <extensions></extensions>
<cmdline></cmdline> <cmdline></cmdline>
<hasPrio>56</hasPrio> <hasPrio>1</hasPrio>
<buildSequence>inputOutputBased</buildSequence> <buildSequence>inputOutputBased</buildSequence>
</data> </data>
</settings> </settings>
@ -2927,6 +2927,12 @@
</group> </group>
<group> <group>
<name>servoMotor</name> <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> <file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\modbus_crc.c</name> <name>$PROJ_DIR$\..\BSP\Driver\servoMotor\modbus_crc.c</name>
</file> </file>

View File

@ -3196,6 +3196,12 @@
</group> </group>
<group> <group>
<name>servoMotor</name> <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> <file>
<name>$PROJ_DIR$\..\BSP\Driver\servoMotor\modbus_crc.c</name> <name>$PROJ_DIR$\..\BSP\Driver\servoMotor\modbus_crc.c</name>
</file> </file>