This repository has been archived on 2026-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
hero/PTZ/Core/Src/Remote.c
2025-11-27 20:08:38 +08:00

125 lines
2.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "main.h"
#include "dma.h"
#include "gpio.h"
#include "Remote.h"
#include "usart.h"
uint8_t Remote_Status=1;
uint8_t Remote_StartFlag=1;
uint8_t dbus_buf[DBUS_BUFLEN];
rc_info_t rc = rc_Init;
extern UART_HandleTypeDef huart3;//remote
//作用启动UART的DMA接收不使用中断
static int uart_receive_dma_no_it(UART_HandleTypeDef* huart, uint8_t* pData, uint32_t Size)
{
uint32_t tmp1 = 0;
tmp1 = huart->RxState;
if (tmp1 == HAL_UART_STATE_READY)
{
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}
huart->pRxBuffPtr = pData;
huart->RxXferSize = Size;
huart->ErrorCode = HAL_UART_ERROR_NONE;
HAL_DMA_Start(huart->hdmarx, (uint32_t)&huart->Instance->DR, (uint32_t)pData, Size);
SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
//初始化
void dbus_uart_init(void)
{
__HAL_UART_CLEAR_IDLEFLAG(&DBUS_HUART);
__HAL_UART_ENABLE_IT(&DBUS_HUART, UART_IT_IDLE);
uart_receive_dma_no_it(&DBUS_HUART, dbus_buf, DBUS_MAX_LEN);
}
//数据解析
Remote_Data Remote_RxData = rc_Init;
void rc_callback_handler(rc_info_t *rc, uint8_t *buff)
{
rc->ch0 = (buff[0] | buff[1] << 8) & 0x07FF;
rc->ch0 -= 1024;
rc->ch1 = (buff[1] >> 3 | buff[2] << 5) & 0x07FF;
rc->ch1 -= 1024;
rc->ch2 = (buff[2] >> 6 | buff[3] << 2 | buff[4] << 10) & 0x07FF;
rc->ch2 -= 1024;
rc->ch3 = (buff[4] >> 1 | buff[5] << 7) & 0x07FF;
rc->ch3 -= 1024;
rc->roll = (buff[16] | (buff[17] << 8)) & 0x07FF;
rc->roll -= 1024;
rc->sw1 = ((buff[5] >> 4) & 0x000C) >> 2;
rc->sw2 = (buff[5] >> 4) & 0x0003;
Remote_RxData.Remote_R_RL = rc->ch0 ;
Remote_RxData.Remote_R_UD = rc->ch1;
Remote_RxData.Remote_L_RL = rc->ch2 ;
Remote_RxData.Remote_L_UD = rc->ch3;
Remote_RxData.Remote_ThumbWheel = rc->roll ;
Remote_RxData.Remote_LS = rc->sw1 ;
Remote_RxData.Remote_RS = rc->sw2 ;
}
//获取DMA当前剩余数据计数
uint16_t dma_current_data_counter(DMA_Stream_TypeDef *dma_stream)
{
return ((uint16_t)(dma_stream->NDTR));
}
//空闲中断接受
static void uart_rx_idle_callback(UART_HandleTypeDef* huart)
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
if (huart == &DBUS_HUART)
{
__HAL_DMA_DISABLE(huart->hdmarx);
if ((DBUS_MAX_LEN - dma_current_data_counter(huart->hdmarx->Instance)) == DBUS_BUFLEN)
{
rc_callback_handler(&rc, dbus_buf);
}
__HAL_DMA_SET_COUNTER(huart->hdmarx, DBUS_MAX_LEN);
__HAL_DMA_ENABLE(huart->hdmarx);
}
}
//中断接受
void uart_receive_handler(UART_HandleTypeDef *huart)
{
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) &&
__HAL_UART_GET_IT_SOURCE(huart, UART_IT_IDLE))
{
uart_rx_idle_callback(huart);
}
}