Fix FrSky D cable telementry for 9XR PRO and DJT module. (#5220)
[opentx.git] / radio / src / targets / sky9x / serial2_driver.cpp
blobb7a749ff7b7a2c80d7c4604c0a1199f35f9a48de
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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.
21 #include "opentx.h"
22 #include <stdarg.h>
24 /** Usart Hw interface used by the console (UART0). */
25 #define SECOND_SERIAL_UART UART0
26 /** Usart Hw ID used by the console (UART0). */
27 #define SECOND_SERIAL_ID ID_UART0
28 /** Pins description corresponding to Rxd,Txd, (UART pins) */
29 #define SECOND_SERIAL_PINS {PINS_UART}
31 Fifo<uint8_t, 512> serial2RxFifo;
33 #if !defined(SIMU)
35 * Outputs a character on the UART line.
37 * This function is synchronous (i.e. uses polling).
38 * c Character to send.
40 void serial2Putc(const unsigned char c)
42 Uart *pUart = SECOND_SERIAL_UART;
44 /* Wait for the transmitter to be ready */
45 while ( (pUart->UART_SR & UART_SR_TXRDY) == 0 ) ;
47 /* Send character */
48 pUart->UART_THR = c;
51 uint8_t serial2TracesEnabled()
53 return false;
56 /**
57 * Configures a UART peripheral with the specified parameters.
59 * baudrate Baudrate at which the UART should operate (in Hz).
60 * masterClock Frequency of the system master clock (in Hz).
61 * uses PA9 and PA10, RXD2 and TXD2
63 void SECOND_UART_Configure(uint32_t baudrate, uint32_t masterClock)
65 Uart *pUart = SECOND_SERIAL_UART;
67 /* Configure PIO */
68 configure_pins( (PIO_PA9 | PIO_PA10), PIN_PERIPHERAL | PIN_INPUT | PIN_PER_A | PIN_PORTA | PIN_NO_PULLUP ) ;
70 /* Configure PMC */
71 PMC->PMC_PCER0 = 1 << SECOND_SERIAL_ID;
73 /* Reset and disable receiver & transmitter */
74 pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX
75 | UART_CR_RXDIS | UART_CR_TXDIS;
77 /* Configure mode */
78 pUart->UART_MR = 0x800 ; // NORMAL, No Parity
80 /* Configure baudrate */
81 /* Asynchronous, no oversampling */
82 pUart->UART_BRGR = (masterClock / baudrate) / 16;
84 /* Disable PDC channel */
85 pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
87 /* Enable receiver and transmitter */
88 pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
90 pUart->UART_IER = UART_IER_RXRDY;
91 NVIC_EnableIRQ(UART0_IRQn);
94 void SECOND_UART_Stop()
96 SECOND_SERIAL_UART->UART_IDR = UART_IDR_RXRDY ;
97 NVIC_DisableIRQ(UART0_IRQn) ;
100 extern "C" void UART0_IRQHandler()
102 if ( SECOND_SERIAL_UART->UART_SR & UART_SR_RXRDY )
104 serial2RxFifo.push(SECOND_SERIAL_UART->UART_RHR);
108 #else
109 #define SECOND_UART_Configure(...)
110 #endif
112 #if defined(TELEMETRY_FRSKY)
113 void serial2TelemetryInit(unsigned int /*protocol*/)
115 SECOND_UART_Configure(FRSKY_D_BAUDRATE, Master_frequency);
118 bool telemetrySecondPortReceive(uint8_t & data)
120 return serial2RxFifo.pop(data);
122 #endif
124 #if defined(DEBUG)
125 void serialPrintf(const char*, ... ) {}
126 #endif