Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
***************************************(C) COPYRIGHT 2018 DJI***************************************
* @file bsp_uart.c
* @brief this file contains rc data receive and processing function
* @note
* @Version V1.0.0
* @Date Jan-30-2018
***************************************(C) COPYRIGHT 2018 DJI***************************************
*/
#include "string.h"
#include "stdlib.h"
#include "bsp_uart.h"
#include "usart.h"
#include "main.h"
#include "oled.h"
uint8_t dbus_buf[DBUS_BUFLEN];
rc_info_t rc;
/**
* @brief enable global uart it and do not use DMA transfer done it
* @param[in] huart: uart IRQHandler id
* @param[in] pData: receive buff
* @param[in] Size: buff size
* @retval set success or fail
*/
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;
/* Enable the DMA Stream */
HAL_DMA_Start(huart->hdmarx, (uint32_t)&huart->Instance->DR, (uint32_t)pData, Size);
/*
* Enable the DMA transfer for the receiver request by setting the DMAR bit
* in the UART CR3 register
*/
SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @brief returns the number of remaining data units in the current DMAy Streamx transfer.
* @param[in] dma_stream: where y can be 1 or 2 to select the DMA and x can be 0
* to 7 to select the DMA Stream.
* @retval The number of remaining data units in the current DMAy Streamx transfer.
*/
uint16_t dma_current_data_counter(DMA_Stream_TypeDef *dma_stream)
{
/* Return the number of remaining data units for DMAy Streamx */
return ((uint16_t)(dma_stream->NDTR));
}
/**
* @brief handle received rc data
* @param[out] rc: structure to save handled rc data
* @param[in] buff: the buff which saved raw rc data
* @retval
*/
void rc_callback_handler(rc_info_t *rc, uint8_t *buff)
{
rc->ch4 = (buff[0] | buff[1] << 8) & 0x07FF; //0x07FF met la commande sur 11 bits (donc 2048 max)
rc->ch4 -= 1024; //on centre sur 0
rc->ch3 = (buff[1] >> 3 | buff[2] << 5) & 0x07FF;
rc->ch3 -= 1024;
rc->ch2 = (buff[2] >> 6 | buff[3] << 2 | buff[4] << 10) & 0x07FF;
rc->ch2 -= 1024;
rc->ch1 = (buff[4] >> 1 | buff[5] << 7) & 0x07FF;
rc->ch1 -= 1024;
rc->sw1 = ((buff[5] >> 4) & 0x000C) >> 2;
rc->sw2 = (buff[5] >> 4) & 0x0003;
if ((abs(rc->ch1) > 660) || \
(abs(rc->ch2) > 660) || \
(abs(rc->ch3) > 660) || \
(abs(rc->ch4) > 660))
{
memset(rc, 0, sizeof(rc_info_t));
}
}
/**
* @brief clear idle it flag after uart receive a frame data
* @param[in] huart: uart IRQHandler id
* @retval
*/
static void uart_rx_idle_callback(UART_HandleTypeDef* huart)
{
/* clear idle it flag avoid idle interrupt all the time */
__HAL_UART_CLEAR_IDLEFLAG(huart);
/* handle received data in idle interrupt */
if (huart == &DBUS_HUART)
{
/* clear DMA transfer complete flag */
__HAL_DMA_DISABLE(huart->hdmarx);
/* handle dbus data dbus_buf from DMA */
if ((DBUS_MAX_LEN - dma_current_data_counter(huart->hdmarx->Instance)) == DBUS_BUFLEN)
{
rc_callback_handler(&rc, dbus_buf);
}
/* restart dma transmission */
__HAL_DMA_SET_COUNTER(huart->hdmarx, DBUS_MAX_LEN);
__HAL_DMA_ENABLE(huart->hdmarx);
}
}
/**
* @brief callback this function when uart interrupt
* @param[in] huart: uart IRQHandler id
* @retval
*/
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);
}
}
/**
* @brief initialize dbus uart device
* @param
* @retval
*/
void dbus_uart_init(void)
{
/* open uart idle it */
__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);
}