初始提交
This commit is contained in:
191
MDK-ARM/remote.c
Normal file
191
MDK-ARM/remote.c
Normal file
@@ -0,0 +1,191 @@
|
||||
#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"
|
||||
#include "can2.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));
|
||||
// 可以选择发送零值数据
|
||||
// send_rc_data_complete(rc);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 数据有效,立即发送到CAN总线
|
||||
send_rc_data_complete(rc);
|
||||
|
||||
// 如果需要单独发送roll数据
|
||||
// send_rc_roll_data(rc);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
//static CAN_TxHeaderTypeDef chassis_tx_message;
|
||||
//static uint8_t chassis_can_send_data[8];
|
||||
//void CAN_cmd_chassis(int8_t ch0, int8_t ch1, int8_t ch2, int8_t ch3,int8_t roll,uint8_t sw1,uint8_t sw2)
|
||||
////can总线数据发送函数
|
||||
//{
|
||||
// uint32_t send_mail_box;
|
||||
// chassis_tx_message.StdId = 0x200;
|
||||
// chassis_tx_message.IDE = CAN_ID_STD;
|
||||
// chassis_tx_message.RTR = CAN_RTR_DATA;
|
||||
// chassis_tx_message.DLC = 0x08;
|
||||
// chassis_can_send_data[0] = ch0 >> 8;
|
||||
// chassis_can_send_data[1] = ch1;
|
||||
// chassis_can_send_data[2] = ch2 >> 8;
|
||||
// chassis_can_send_data[3] = ch3;
|
||||
// chassis_can_send_data[4] = roll >> 8;
|
||||
// chassis_can_send_data[5] = sw1;
|
||||
// chassis_can_send_data[6] = sw2 >> 8;
|
||||
// HAL_CAN_AddTxMessage(&hcan2, &chassis_tx_message, chassis_can_send_data, &send_mail_box);
|
||||
//}
|
||||
////过滤器函数(还没设置过滤内容)
|
||||
//void can_filter_init(void)
|
||||
//{
|
||||
// CAN_FilterTypeDef can_filter_st;
|
||||
// can_filter_st.FilterActivation = ENABLE;
|
||||
// can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;
|
||||
// can_filter_st.FilterScale = CAN_FILTERSCALE_32BIT;
|
||||
// can_filter_st.FilterIdHigh = 0x0000;//高八位
|
||||
// can_filter_st.FilterIdLow = 0x0000;//低八位
|
||||
// can_filter_st.FilterMaskIdHigh = 0x0000;
|
||||
// can_filter_st.FilterMaskIdLow = 0x0007;
|
||||
// can_filter_st.FilterBank = 0;
|
||||
// can_filter_st.FilterFIFOAssignment = CAN_RX_FIFO0;
|
||||
// HAL_CAN_ConfigFilter(&hcan2, &can_filter_st);
|
||||
// HAL_CAN_Start(&hcan2);
|
||||
// HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
//
|
||||
|
||||
// can_filter_st.SlaveStartFilterBank = 14;
|
||||
// can_filter_st.FilterBank = 14;
|
||||
// HAL_CAN_ConfigFilter(&hcan2, &can_filter_st);
|
||||
// HAL_CAN_Start(&hcan2);
|
||||
// HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
//}
|
||||
|
||||
////过滤器笔记
|
||||
////SlaveStartFilterBank参数只有在双CAN模式(CAN1和CAN2)下才需要,如果只有CAN1,可以不用设置
|
||||
Reference in New Issue
Block a user