5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 uint8_t serial2Mode
= 0;
24 Fifo
<uint8_t, 512> serial2TxFifo
;
25 DMAFifo
<32> serial2RxFifo
__DMA (SERIAL_DMA_Stream_RX
);
27 void uart3Setup(unsigned int baudrate
, bool dma
)
29 USART_InitTypeDef USART_InitStructure
;
30 GPIO_InitTypeDef GPIO_InitStructure
;
32 GPIO_PinAFConfig(SERIAL_GPIO
, SERIAL_GPIO_PinSource_RX
, SERIAL_GPIO_AF
);
33 GPIO_PinAFConfig(SERIAL_GPIO
, SERIAL_GPIO_PinSource_TX
, SERIAL_GPIO_AF
);
35 GPIO_InitStructure
.GPIO_Pin
= SERIAL_GPIO_PIN_TX
| SERIAL_GPIO_PIN_RX
;
36 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF
;
37 GPIO_InitStructure
.GPIO_OType
= GPIO_OType_PP
;
38 GPIO_InitStructure
.GPIO_PuPd
= GPIO_PuPd_UP
;
39 GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_2MHz
;
40 GPIO_Init(SERIAL_GPIO
, &GPIO_InitStructure
);
42 USART_InitStructure
.USART_BaudRate
= baudrate
;
43 USART_InitStructure
.USART_WordLength
= USART_WordLength_8b
;
44 USART_InitStructure
.USART_StopBits
= USART_StopBits_1
;
45 USART_InitStructure
.USART_Parity
= USART_Parity_No
;
46 USART_InitStructure
.USART_HardwareFlowControl
= USART_HardwareFlowControl_None
;
47 USART_InitStructure
.USART_Mode
= USART_Mode_Tx
| USART_Mode_Rx
;
48 USART_Init(SERIAL_USART
, &USART_InitStructure
);
51 DMA_InitTypeDef DMA_InitStructure
;
52 serial2RxFifo
.clear();
53 USART_ITConfig(SERIAL_USART
, USART_IT_RXNE
, DISABLE
);
54 USART_ITConfig(SERIAL_USART
, USART_IT_TXE
, DISABLE
);
55 DMA_InitStructure
.DMA_Channel
= SERIAL_DMA_Channel_RX
;
56 DMA_InitStructure
.DMA_PeripheralBaseAddr
= CONVERT_PTR_UINT(&SERIAL_USART
->DR
);
57 DMA_InitStructure
.DMA_Memory0BaseAddr
= CONVERT_PTR_UINT(serial2RxFifo
.buffer());
58 DMA_InitStructure
.DMA_DIR
= DMA_DIR_PeripheralToMemory
;
59 DMA_InitStructure
.DMA_BufferSize
= serial2RxFifo
.size();
60 DMA_InitStructure
.DMA_PeripheralInc
= DMA_PeripheralInc_Disable
;
61 DMA_InitStructure
.DMA_MemoryInc
= DMA_MemoryInc_Enable
;
62 DMA_InitStructure
.DMA_PeripheralDataSize
= DMA_PeripheralDataSize_Byte
;
63 DMA_InitStructure
.DMA_MemoryDataSize
= DMA_MemoryDataSize_Byte
;
64 DMA_InitStructure
.DMA_Mode
= DMA_Mode_Circular
;
65 DMA_InitStructure
.DMA_Priority
= DMA_Priority_Low
;
66 DMA_InitStructure
.DMA_FIFOMode
= DMA_FIFOMode_Disable
;
67 DMA_InitStructure
.DMA_FIFOThreshold
= DMA_FIFOThreshold_Full
;
68 DMA_InitStructure
.DMA_MemoryBurst
= DMA_MemoryBurst_Single
;
69 DMA_InitStructure
.DMA_PeripheralBurst
= DMA_PeripheralBurst_Single
;
70 DMA_Init(SERIAL_DMA_Stream_RX
, &DMA_InitStructure
);
71 USART_DMACmd(SERIAL_USART
, USART_DMAReq_Rx
, ENABLE
);
72 USART_Cmd(SERIAL_USART
, ENABLE
);
73 DMA_Cmd(SERIAL_DMA_Stream_RX
, ENABLE
);
76 USART_Cmd(SERIAL_USART
, ENABLE
);
77 USART_ITConfig(SERIAL_USART
, USART_IT_RXNE
, ENABLE
);
78 USART_ITConfig(SERIAL_USART
, USART_IT_TXE
, DISABLE
);
79 NVIC_SetPriority(SERIAL_USART_IRQn
, 7);
80 NVIC_EnableIRQ(SERIAL_USART_IRQn
);
84 void serial2Init(unsigned int mode
, unsigned int protocol
)
91 case UART_MODE_TELEMETRY_MIRROR
:
92 #if defined(CROSSFIRE)
93 if (protocol
== PROTOCOL_PULSES_CROSSFIRE
) {
94 uart3Setup(CROSSFIRE_TELEM_MIRROR_BAUDRATE
, false);
98 uart3Setup(FRSKY_SPORT_BAUDRATE
, false);
100 #if defined(DEBUG) || defined(CLI)
101 case UART_MODE_DEBUG
:
102 uart3Setup(DEBUG_BAUDRATE
, false);
105 case UART_MODE_TELEMETRY
:
106 if (protocol
== PROTOCOL_FRSKY_D_SECONDARY
) {
107 uart3Setup(FRSKY_D_BAUDRATE
, true);
113 void serial2Putc(char c
)
117 while (serial2TxFifo
.isFull()) {
119 if (++n
> 100) return;
121 serial2TxFifo
.push(c
);
122 USART_ITConfig(SERIAL_USART
, USART_IT_TXE
, ENABLE
);
126 void serial2SbusInit()
128 uart3Setup(SBUS_BAUDRATE
, true);
129 SERIAL_USART
->CR1
|= USART_CR1_M
| USART_CR1_PCE
;
134 DMA_DeInit(SERIAL_DMA_Stream_RX
);
135 USART_DeInit(SERIAL_USART
);
138 uint8_t serial2TracesEnabled()
141 return (serial2Mode
== UART_MODE_DEBUG
);
147 extern "C" void SERIAL_USART_IRQHandler(void)
149 DEBUG_INTERRUPT(INT_SER2
);
151 if (USART_GetITStatus(SERIAL_USART
, USART_IT_TXE
) != RESET
) {
153 if (serial2TxFifo
.pop(txchar
)) {
154 /* Write one byte to the transmit data register */
155 USART_SendData(SERIAL_USART
, txchar
);
158 USART_ITConfig(SERIAL_USART
, USART_IT_TXE
, DISABLE
);
163 if (!(getSelectedUsbMode() == USB_SERIAL_MODE
)) {
165 uint32_t status
= SERIAL_USART
->SR
;
166 while (status
& (USART_FLAG_RXNE
| USART_FLAG_ERRORS
)) {
167 uint8_t data
= SERIAL_USART
->DR
;
168 if (!(status
& USART_FLAG_ERRORS
)) {
169 switch (serial2Mode
) {
170 case UART_MODE_DEBUG
:
171 cliRxFifo
.push(data
);
175 status
= SERIAL_USART
->SR
;