发送接收修改完毕,等待测试

This commit is contained in:
REASEARCHER\18383 2025-10-16 10:30:36 +08:00
parent d9df2548c5
commit a2bbae0271
7 changed files with 179 additions and 201 deletions

View File

@ -21,7 +21,10 @@ static void ptz_recv_hori_servo_task()
while(1) {
OSSemPend(g_horiMotorMutex, 0, &err);
if ( MotorReplyForWrite(H_MOTOR) == true )
{
OSSemPost(g_horiSpeedMutex);
}

View File

@ -289,8 +289,6 @@ static void creat_task_vert_servo_task(void)
}
}
void init_speed_module(void)
{
g_horiSpeedSem = OSSemCreate(0);
@ -327,14 +325,12 @@ void init_speed_module(void)
creat_task_hori_servo_task();
creat_task_vert_servo_task();
// creat_task_test();
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);
servoSendData(horiMotorType, WriteMotorOneReg(H_MOTOR, H02_CONTR_MODE_SELEC, 0)
, WRITE_ONE_REG_FRAME_NUM, lowPriority);
}

View File

@ -99,11 +99,9 @@ static MotorCommuDmaHwInfo_t g_MotorDmaBuff[] =
********************************************************************************************************
*/
/* 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_horiDmaRxBuff[DMA_BUFF_SIZE] = {0};//水平电机DMA接受缓存区
static uint8_t g_vertDmaRxBuff1[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区1
static uint8_t g_vertDmaRxBuff2[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区2
static uint8_t g_vertDmaRxBuff[DMA_BUFF_SIZE] = {0};//垂直电机DMA接受缓存区
/* 处理串口通讯与数据缓冲的数据结构 */
static CommuInfo_t g_horiCommuDeal; //水平电机
static CommuInfo_t g_vertCommuDeal; //垂直电机
@ -111,22 +109,19 @@ static CommuInfo_t g_vertCommuDeal; //
typedef struct
{
CommuInfo_t* pCommuInfo; //串口通讯与数据缓冲相关的数据结构
uint8_t* dmaRxBuff1; //dma接受缓存区1指针
uint8_t* dmaRxBuff2; //dma接受缓存区2指针
uint8_t* dmaRxBuff; //dma接受缓存区指针
}CommuHwInfo_t;//方便缓冲区初始化的结构体
static CommuHwInfo_t g_commuInfoBuff[] =
{
//水平电机
{
.pCommuInfo = &g_horiCommuDeal,
.dmaRxBuff1 = g_horiDmaRxBuff1,
.dmaRxBuff2 = g_horiDmaRxBuff2,
.dmaRxBuff = g_horiDmaRxBuff,
},
//垂直电机串口5
{
.pCommuInfo = &g_vertCommuDeal,
.dmaRxBuff1 = g_vertDmaRxBuff1,
.dmaRxBuff2 = g_vertDmaRxBuff2,
.dmaRxBuff = g_vertDmaRxBuff,
},
};
@ -219,7 +214,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->pDmaRsvBuff1);
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;
@ -229,11 +224,11 @@ 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_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);
//中断配置
nvic_irq_enable(g_MotorDmaBuff[i].dmaRxIrq, 4, 2);
dma_interrupt_enable(g_MotorDmaBuff[i].dmaNo, g_MotorDmaBuff[i].dmaRxch, DMA_CHXCTL_FTFIE);
// nvic_irq_enable(g_MotorDmaBuff[i].dmaRxIrq, 4, 2);
// 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);
}
}
@ -257,9 +252,7 @@ static void CommuStructInit()
CommuInfo_t *pCommuDeal = g_commuInfoBuff[i].pCommuInfo;
pCommuDeal->dmaTranFlag = DMA_TRANS_IDLE;
pCommuDeal->dmaSize = DMA_BUFF_SIZE;
pCommuDeal->pDmaRsvBuff1 = g_commuInfoBuff[i].dmaRxBuff1;
pCommuDeal->pDmaRsvBuff2 = g_commuInfoBuff[i].dmaRxBuff2;
pCommuDeal->pDmaRsvBuffSelect = DMA_RSVBUFF_SELECT1;//默认使用缓冲区1
pCommuDeal->pDmaRsvBuff = g_commuInfoBuff[i].dmaRxBuff;
}
}
@ -321,37 +314,25 @@ void DMA0_Channel3_IRQHandler(void)
* @return
***********************************************************
*/
static uint16_t g_horiLastPos;
static uint16_t g_horiNowPos;
void USART2_IRQHandler(void)
{
/* 串口的接收空闲中断方式进行了数据缓存。*/
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标志位
//释放信号量,通知接收到一包数据,任务可以处理了
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;
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);
/*计算在DMA缓冲区需要获取的数据长度*/
// DmaIdleNum = dma_transfer_number_get(g_MotorDmaBuff[H_MOTOR].dmaNo, g_MotorDmaBuff[H_MOTOR].dmaRxch);//获取的是还有多少个没传输,而不是已经传输了多少
@ -361,40 +342,14 @@ void USART2_IRQHandler(void)
void DMA0_Channel1_IRQHandler(void)
{
dma_single_data_parameter_struct dmaStruct;
/*
* 1DMA的偏移量
* 11 pUartAttr->DamOffset置为0;
* 2DMA为循环方式进行数据搬运的Cnt后
* 3;
* 4DMA为正常模式,DMA会自动disable掉
* 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);
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);
}
}
}
@ -436,9 +391,10 @@ void DMA1_Channel7_IRQHandler(void)
* @return
***********************************************************
*/
static uint16_t g_vertLastPos;
static uint16_t g_vertNowPos;
void USART5_IRQHandler(void)
{
dma_single_data_parameter_struct dmaStruct;
/* 串口的接收空闲中断方式进行了数据缓存。*/
if(RESET != usart_interrupt_flag_get(g_motorCommuBuff[V_MOTOR].uartNo, USART_INT_FLAG_IDLE))
{
@ -446,69 +402,26 @@ void USART5_IRQHandler(void)
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[H_MOTOR].dmaNo,
g_MotorDmaBuff[H_MOTOR].dmaRxch);
//释放信号量,通知接收到一包数据,任务可以处理了
OSSemPost(g_horiMotorMutex);
CommuInfo_t *pCommuDeal = g_commuInfoBuff[V_MOTOR].pCommuInfo;
/* 切换使用接收缓冲区,这样上层的解析永远从第一个字节起*/
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);
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);
}
OSSemPost(g_vertMotorMutex);
}
}
// uint8_t rx_OK = 0;
void DMA1_Channel1_IRQHandler(void)
{
dma_single_data_parameter_struct dmaStruct;
/*
* 1DMA的偏移量
* 11 pUartAttr->DamOffset置为0;
* 2DMA为循环方式进行数据搬运的Cnt后
* 3;
* 4DMA为正常模式,DMA会自动disable掉
* 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);
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);
}
// OSSemPost(g_vertMotorMutex);
}
}
@ -539,6 +452,13 @@ void CommuDrvInit(void)
*/
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;
@ -563,20 +483,43 @@ bool CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len)
/**
* @brief
* @param motorNoH_MOTORV_MOTOR
* @param userBuff
* @param userBuff
* @param len
* @return none
*/
void CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len)
bool CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len)
{
CommuInfo_t *pCommuDeal = g_commuInfoBuff[motorNo].pCommuInfo;
if ( pCommuDeal->pDmaRsvBuffSelect == DMA_RSVBUFF_SELECT1 )
/*简易判断*/
if( userBuff == NULL || len == 0 )
{
memcpy(userBuff, pCommuDeal->pDmaRsvBuff2, len);
return false;
}
/*接收DMA缓冲区数据*/
if ( motorNo == H_MOTOR )
{
if ( ( (g_horiLastPos + len) % DMA_BUFF_SIZE ) != g_horiNowPos )//假如是写寄存器电机返回消息正常是8字节但如果是写错误则返回非8字节错误码
{
return false;
}
for(uint8_t i = 0; i < len; i++)
{
*(userBuff+i) = pCommuDeal->pDmaRsvBuff[g_horiLastPos];
g_horiLastPos = (g_horiLastPos + 1) % DMA_BUFF_SIZE;
}
}
else
{
memcpy(userBuff, pCommuDeal->pDmaRsvBuff1, len);
if ( ( (g_vertLastPos + len) % DMA_BUFF_SIZE ) != g_vertNowPos )//假如是写寄存器电机返回消息正常是8字节但如果是写错误则返回非8字节错误码
{
return false;
}
for(uint8_t i = 0; i < len; i++)
{
*(userBuff+i) = pCommuDeal->pDmaRsvBuff[g_vertLastPos];
g_horiLastPos = (g_vertLastPos + 1) % DMA_BUFF_SIZE;
}
}
return true;
}

View File

@ -13,16 +13,12 @@ typedef struct
{
int16_t dmaTranFlag; /*dma发送是否在工作的标志位*/
int32_t dmaSize; /*DMA接收缓冲区的大小*/
uint8_t *pDmaRsvBuff1; /*指向接收DMA缓冲区1的首地址*/
uint8_t *pDmaRsvBuff2; /*指向接收DMA缓冲区2的首地址*/
uint8_t pDmaRsvBuffSelect; /*表示当前正在使用哪个接收缓冲区*/
uint8_t *pDmaRsvBuff; /*指向接收DMA缓冲区的首地址*/
}CommuInfo_t;
#define DMA_TRANS_IDLE 0//dma当前未在发送数据
#define DMA_TRANS_BUSY 1//dma当前正在发送数据
#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
@ -63,7 +59,7 @@ bool CommuTransData(uint8_t motorNo, uint8_t* buffer, int32_t len);
* @param len
* @return none
*/
void CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len);
bool CommuRsvData(uint8_t motorNo, uint8_t* userBuff, uint32_t len);
///*用于结构体数组赋值,方便外部使用此结构体数组*/

View File

@ -16,8 +16,6 @@ static void MotorSwitchGpioCofig(void)
}
static uint8_t g_writeOneRegBuff[WRITE_ONE_REG_BUFFNUM];//由于写寄存器,电机返回的数据和写入的数据完全一致,故设此数组
/*
crc校验高位 crc校验低位
@ -28,93 +26,122 @@ static uint8_t g_writeOneRegBuff[WRITE_ONE_REG_BUFFNUM];//
* @param motorNo
* @param regAddr
* @param data
* @return false:DMA正在发送数据
* @return
*/
bool WriteMotorOneReg(uint8_t motorNo, uint16_t regAddr, uint16_t data)
static uint8_t g_writeOneRegBuff[WRITE_ONE_REG_FRAME_NUM];
uint8_t* WriteMotorOneReg(uint8_t motorNo, uint16_t regAddr, uint16_t data)
{
uint8_t frameBuff[WRITE_ONE_REG_BUFFNUM] = {0};
uint16_t crc;
if ( motorNo == H_MOTOR )
{
frameBuff[0] = H_MOTOR_ADDR;//由于采用一主一从模式所以水平电机垂直电机从机地址都是0x01云台后期也不会扩展
g_writeOneRegBuff[0] = H_MOTOR_ADDR;//由于采用一主一从模式所以水平电机垂直电机从机地址都是0x01云台后期也不会扩展
}
else
{
frameBuff[0] = V_MOTOR_ADDR;
g_writeOneRegBuff[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;
g_writeOneRegBuff[1] = WRITE_ONE_REG;
g_writeOneRegBuff[2] = regAddr >> 8;
g_writeOneRegBuff[3] = regAddr & 0xff;
g_writeOneRegBuff[4] = data >> 8;
g_writeOneRegBuff[5] = data & 0xff;
crc = ModbusCRC16(g_writeOneRegBuff, WRITE_ONE_REG_FRAME_NUM - 2);
g_writeOneRegBuff[6] = crc & 0xff;
g_writeOneRegBuff[7] = crc >> 8;
return g_writeOneRegBuff;
}
/**
* @brief
* @param motorNo
* @param userBuff
* @param lenWRITE_ONE_REG_BUFFNUM
* @return false:
*/
bool MotorReplyForWrite(uint8_t motorNo, uint8_t* userBuff, uint8_t len)
bool MotorReplyForWrite(uint8_t motorNo)
{
CommuRsvData(motorNo, userBuff, len);
for( uint8_t i = 0; i < len; i++ )
static uint8_t motorReplybuff[WRITE_ONE_REG_FRAME_NUM];
/*提取数据失败*/
if ( CommuRsvData(motorNo, motorReplybuff, WRITE_ONE_REG_FRAME_NUM) == false )
{
if ( userBuff[i] != g_writeOneRegBuff[i] )
return false;
}
else
{
for( uint8_t i = 0; i < WRITE_ONE_REG_FRAME_NUM; i++ )
{
memset(g_writeOneRegBuff, 0x00, len);
return false;
/*提取的数据不对*/
if ( motorReplybuff[i] != g_writeOneRegBuff[i] )
{
return false;
}
}
}
memset(g_writeOneRegBuff, 0x00, len);
return true;
}
/*
crc校验高位 crc校验低位
01H 06H 02H 00H 00H 01H 49H B2H
reg数量高位 crc校验高位 crc校验低位
01H 03H 0BH 00H 00H 01H 86H 2EH
*/
/**
* @brief
* @param motorNo
* @param regAddr
* @param data
* @param frameBuff:
* @return frameBuff的长度
* @brief
* @param motorNo
* @param regAddr
* @return
*/
uint8_t WriteMotorOneReg_buffer(uint8_t motorNo, uint16_t regAddr, uint16_t data, uint8_t *frameBuff)
static uint8_t g_readOneRegBuff[READ_ONE_REG_FRAME_NUM];
uint8_t* ReadMotorOneReg(uint8_t motorNo, uint16_t regAddr)
{
uint16_t crc;
if ( motorNo == H_MOTOR )
uint16_t crc;
if ( motorNo == H_MOTOR )
{
g_readOneRegBuff[0] = H_MOTOR_ADDR;//由于采用一主一从模式所以水平电机垂直电机从机地址都是0x01云台后期也不会扩展
}
else
{
g_readOneRegBuff[0] = V_MOTOR_ADDR;
}
g_readOneRegBuff[1] = READ_ONE_REG;
g_readOneRegBuff[2] = regAddr >> 8;
g_readOneRegBuff[3] = regAddr & 0xff;
g_readOneRegBuff[4] = 0x00;
g_readOneRegBuff[5] = 0x01;
crc = ModbusCRC16(g_readOneRegBuff, READ_ONE_REG_FRAME_NUM - 2);
g_readOneRegBuff[6] = crc & 0xff;
g_readOneRegBuff[7] = crc >> 8;
return g_readOneRegBuff;
}
/*
crc校验高位 crc校验低位
01H 03H 02H 00H 00H B8H 44H
*/
/**
* @brief 7
* @param motorNo
* @param data
* @return false:
*/
bool MotorReplyForRead(uint8_t motorNo, uint16_t* data)
{
/*简易判断*/
if ( data == NULL )
{
frameBuff[0] = H_MOTOR_ADDR;//由于采用一主一从模式所以水平电机垂直电机从机地址都是0x01云台后期也不会扩展
return false;
}
static uint8_t motorReplybuff[WRITE_ONE_REG_FRAME_NUM - 1];
/*提取数据失败*/
if ( CommuRsvData(motorNo, motorReplybuff, WRITE_ONE_REG_FRAME_NUM - 1) == false )
{
return false;
}
else
{
frameBuff[0] = V_MOTOR_ADDR;
*data = motorReplybuff[3];
*data = (*data << 8) | motorReplybuff[4];
}
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;
return true;
}
/**
* @brief
* @param

View File

@ -26,7 +26,8 @@
#define WRITE_ONE_REG 0X06//写单个寄存器
#define WRITE_MULT_CONSE_REG 0x10//写多个连续的寄存器
#define WRITE_ONE_REG_BUFFNUM 8//写单个寄存器,数据帧的字节个数
#define WRITE_ONE_REG_FRAME_NUM 8//写单个寄存器,数据帧的字节个数
#define READ_ONE_REG_FRAME_NUM 8//读单个寄存器,数据帧的字节个数
/*
********************************************************************************************************
*
@ -61,23 +62,35 @@
* @param data
* @return false:DMA正在发送数据
*/
bool WriteMotorOneReg(uint8_t motorNo, uint16_t regAddr, uint16_t data);
uint8_t* 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);
bool MotorReplyForWrite(uint8_t motorNo);
/**
* @brief
* @param motorNo
* @param regAddr
* @return
*/
uint8_t* ReadMotorOneReg(uint8_t motorNo, uint16_t regAddr);
/**
* @brief 7
* @param motorNo
* @param data
* @return false:
*/
bool MotorReplyForRead(uint8_t motorNo, uint16_t* data);
/**
* @brief
* @param
* @return
*/
void servoMotorInit(void);
uint8_t WriteMotorOneReg_buffer(uint8_t motorNo, uint16_t regAddr, uint16_t data, uint8_t *frameBuff);
#endif

View File

@ -741,7 +741,7 @@
<data>
<extensions></extensions>
<cmdline></cmdline>
<hasPrio>56</hasPrio>
<hasPrio>1</hasPrio>
<buildSequence>inputOutputBased</buildSequence>
</data>
</settings>
@ -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>