137 lines
2.7 KiB
C
137 lines
2.7 KiB
C
#include "main.h"
|
|
#include "can.h"
|
|
#include "dma.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
#include "math.h"
|
|
#include "remote.h"
|
|
#include "string.h"
|
|
#include "stdlib.h"
|
|
|
|
#define DBUS_MAX_LEN (50)
|
|
#define DBUS_BUFLEN (18)
|
|
#define DBUS_HUART huart3
|
|
|
|
typedef __packed struct
|
|
{
|
|
int16_t ch0;
|
|
int16_t ch1;
|
|
int16_t ch2;
|
|
int16_t ch3;
|
|
int16_t roll;
|
|
uint8_t sw1;
|
|
uint8_t sw2;
|
|
} rc_info_t;
|
|
#define rc_Init \
|
|
{ \
|
|
0, \
|
|
0, \
|
|
0, \
|
|
0, \
|
|
0, \
|
|
0, \
|
|
0, \
|
|
}
|
|
|
|
uint8_t dbus_buf[DBUS_BUFLEN];
|
|
rc_info_t rc = rc_Init;
|
|
extern UART_HandleTypeDef huart3;
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
if ((abs(rc->ch0) > 660) || \
|
|
(abs(rc->ch1) > 660) || \
|
|
(abs(rc->ch2) > 660) || \
|
|
(abs(rc->ch3) > 660))
|
|
|
|
{
|
|
memset(rc, 0, sizeof(rc_info_t));
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |