Fixes #2595 - Cli added

Based on projectkk2glider work, with a lot of bugs I am sure. Still not
tested, this is why I preferred working on my own branch. I push it now
so that Jenkins do the first tests and you can review if I am on the
right track. 

I am really wondering if a merge with the Lua api would be possible /
nice, what do you think?
This commit is contained in:
bertrand
2015-08-10 22:52:26 +02:00
parent 70d77fd851
commit 2b312fdbb4
26 changed files with 970 additions and 653 deletions
@@ -0,0 +1,297 @@
/*
* Authors - Gerard Valade <gerard.valade@gmail.com>
*
* Adapted from frsky
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*/
#include "serial_driver.h"
#include "opentx.h"
/*
Receive serial (RS-232) characters, detecting and storing each Fr-Sky
0x7e-framed packet as it arrives. When a complete packet has been
received, process its data into storage variables. NOTE: This is an
interrupt routine and should not get too lengthy. I originally had
the buffer being checked in the perMain function (because per10ms
isn't quite often enough for data streaming at 9600baud) but alas
that scheme lost packets also. So each packet is parsed as it arrives,
directly at the ISR function (through a call to frskyProcessPacket).
If this proves a problem in the future, then I'll just have to implement
a second buffer to receive data while one buffer is being processed (slowly).
*/
#ifndef SIMU
ISR (USART0_RX_vect)
{
uint8_t iostat; //USART control and Status Register 0 A
// uint8_t rh; //USART control and Status Register 0 B
UCSR0B &= ~(1 << RXCIE0); // disable Interrupt
sei();
iostat = UCSR0A; //USART control and Status Register 0 A
uint8_t byte = UDR0;
/*
bit 7 6 5 4 3 2 1 0
RxC0 TxC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0
RxC0: Receive complete
TXC0: Transmit Complete
UDRE0: USART Data Register Empty
FE0: Frame Error
DOR0: Data OverRun
UPE0: USART Parity Error
U2X0: Double Tx Speed
MPCM0: MultiProcessor Comms Mode
*/
if (iostat & ((1 << FE0) | (1 << DOR0) | (1 << UPE0))) {
byte = 0;
}
//rh = UCSR0B; //USART control and Status Register 0 B
#ifdef MAVLINK
(RXHandler)(byte);
#endif
cli();
UCSR0B |= (1 << RXCIE0); // enable Interrupt
}
#endif
inline void SERIAL_EnableRXD(void) {
UCSR0B |= (1 << RXEN0); // enable RX
UCSR0B |= (1 << RXCIE0); // enable Interrupt
}
#if 0
void SERIAL_DisableRXD(void) {
UCSR0B &= ~(1 << RXEN0); // disable RX
UCSR0B &= ~(1 << RXCIE0); // disable Interrupt
}
#endif
/*
USART0 (transmit) Data Register Emtpy ISR
Usef to transmit FrSky data packets, which are buffered in frskyTXBuffer.
*/
uint8_t serialTxBuffer[MAX_TX_BUFFER]; // 32 characters
uint8_t serialTxBufferCount = 0;
uint8_t * ptrTxISR = 0;
serial_tx_state_t serialTxState = TX_STATE_EMPTY;
#ifndef SIMU
ISR(USART0_UDRE_vect)
{
if (serialTxBufferCount > 0) {
UDR0 = *ptrTxISR++;
serialTxBufferCount--;
} else {
UCSR0B &= ~(1 << UDRIE0); // disable UDRE0 interrupt
serialTxState = TX_STATE_EMPTY;
}
}
#endif
void SERIAL_start_uart_send() {
ptrTxISR = serialTxBuffer;
serialTxBufferCount = 0;
}
void SERIAL_end_uart_send() {
ptrTxISR = serialTxBuffer;
//UCSR0B |= (1 << UDRIE0); // enable UDRE0 interrupt
serialTxState = TX_STATE_READY;
}
void SERIAL_send_uart_bytes(const uint8_t * buf, uint16_t len) {
while (len--) {
*ptrTxISR++ = *buf++;
serialTxBufferCount++;
}
}
#if 0
void SERIAL_transmitBuffer(uint8_t len) {
serialTxBufferCount = len;
ptrTxISR = serialTxBuffer;
//UCSR0B |= (1 << UDRIE0); // enable UDRE0 interrupt
serialTxState = TX_STATE_READY;
}
#endif
void SERIAL_startTX(void) {
if (serialTxState == TX_STATE_READY) {
serialTxState = TX_STATE_BUSY;
UCSR0B |= (1 << UDRIE0); // enable UDRE0 interrupt
}
}
static void uart_4800(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 4800
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
static void uart_9600(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 9600
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
static void uart_14400(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 14400
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
static void uart_19200(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 19200
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
static void uart_38400(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 38400
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
static void uart_57600(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 57600
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
static void uart_76800(void) {
#ifndef SIMU
#undef BAUD // avoid compiler warning
#define BAUD 76800
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
#endif
}
inline void SERIAL_EnableTXD(void) {
//UCSR0B |= (1 << TXEN0); // enable TX
UCSR0B |= (1 << TXEN0) | (1 << UDRIE0); // enable TX and TX interrupt
}
#if 0
void SERIAL_DisableTXD(void) {
UCSR0B &= ~(1 << TXEN0); // disable TX
UCSR0B &= ~(1 << UDRIE0); // disable Interrupt
}
#endif
void SERIAL_Init(void) {
DDRE &= ~(1 << DDE0); // set RXD0 pin as input
PORTE &= ~(1 << PORTE0); // disable pullup on RXD0 pin
switch (g_eeGeneral.mavbaud) {
case BAUD_4800:
uart_4800();
break;
case BAUD_9600:
uart_9600();
break;
case BAUD_14400:
uart_14400();
break;
case BAUD_19200:
uart_19200();
break;
case BAUD_38400:
uart_38400();
break;
case BAUD_57600:
uart_57600();
break;
case BAUD_76800:
uart_76800();
break;
}
// UCSR0A &= ~(1 << U2X0); // disable double speed operation
// set 8N1
UCSR0B = 0 | (0 << RXCIE0) | (0 << TXCIE0) | (0 << UDRIE0) | (0 << RXEN0) | (0 << TXEN0) | (0 << UCSZ02);
UCSR0C = 0 | (1 << UCSZ01) | (1 << UCSZ00);
while (UCSR0A & (1 << RXC0))
UDR0; // flush receive buffer
SERIAL_EnableTXD();
SERIAL_EnableRXD();
}
@@ -0,0 +1,59 @@
/*
* Authors - Gerard Valade <gerard.valade@gmail.com>
*
* Adapted from frsky
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*/
#ifndef _SERIAL_H_
#define _SERIAL_H_
typedef enum serial_tx_state_ {
TX_STATE_EMPTY = 0, //
TX_STATE_READY, //
TX_STATE_BUSY
} serial_tx_state_t;
extern serial_tx_state_t serialTxState;
//! \brief Baudrate selection.
enum SERIAL_BAUDS {
BAUD_4800 = 0,
BAUD_9600,
BAUD_14400,
BAUD_19200,
BAUD_38400,
BAUD_57600,
BAUD_76800
};
//! \brief Definition of baudrate settings item choices.
typedef void (*SerialFuncP)(uint8_t event);
extern SerialFuncP RXHandler;
#define MAX_TX_BUFFER 32
//#if 0
extern uint8_t serialTxBuffer[MAX_TX_BUFFER]; // 32 characters
extern uint8_t serialTxBufferCount;
extern uint8_t * ptrTxISR;
//#endif
void SERIAL_Init(void);
//void SERIAL_transmitBuffer(uint8_t len);
extern void SERIAL_start_uart_send();
extern void SERIAL_end_uart_send();
extern void SERIAL_send_uart_bytes(uint8_t * buf, uint16_t len);
void SERIAL_startTX(void);
#define IS_TX_BUSY (serialTxState!=TX_STATE_EMPTY)
#endif
+2 -1
View File
@@ -290,7 +290,8 @@ uint32_t telemetryTransmitPending();
void telemetryTransmitBuffer(uint8_t * buffer, uint32_t size);
// Second UART driver
void telemetrySecondPortInit(unsigned int protocol);
#define serialTracesEnabled() false
void serial2TelemetryInit(unsigned int protocol);
bool telemetrySecondPortReceive(uint8_t & data);
#endif
@@ -53,7 +53,7 @@ Fifo<512> serial2RxFifo;
* This function is synchronous (i.e. uses polling).
* c Character to send.
*/
void debugPutc(const char c)
void serial2Putc(const char c)
{
Uart *pUart = SECOND_SERIAL_UART;
@@ -117,7 +117,7 @@ extern "C" void UART0_IRQHandler()
#endif
#if defined(FRSKY)
void telemetrySecondPortInit(unsigned int /*protocol*/)
void serial2TelemetryInit(unsigned int /*protocol*/)
{
SECOND_UART_Configure(FRSKY_D_BAUDRATE, Master_frequency);
// startPdcUsartReceive();
+3 -3
View File
@@ -260,7 +260,7 @@ void checkTrainerSettings()
stop_sbus_on_heartbeat_capture() ;
break;
case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT:
uart3Stop();
serial2Stop();
}
currentTrainerMode = requiredTrainerMode;
@@ -275,8 +275,8 @@ void checkTrainerSettings()
init_sbus_on_heartbeat_capture() ;
break;
case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT:
if (g_eeGeneral.uart3Mode == UART_MODE_SBUS_TRAINER) {
uart3SbusInit();
if (g_eeGeneral.serial2Mode == UART_MODE_SBUS_TRAINER) {
serial2SbusInit();
break;
}
// no break
+8 -6
View File
@@ -320,13 +320,15 @@ void hapticOff(void);
void hapticOn(void);
#endif
// SERIAL_USART driver
// Second serial port driver
#define DEBUG_BAUDRATE 115200
void uart3Init(unsigned int mode, unsigned int protocol);
void uart3Putc(const char c);
#define telemetrySecondPortInit(protocol) uart3Init(UART_MODE_TELEMETRY, protocol)
void uart3SbusInit(void);
void uart3Stop(void);
extern uint8_t serial2Mode;
#define serialTracesEnabled() (serial2Mode == 0)
void serial2Init(unsigned int mode, unsigned int protocol);
void serial2Putc(char c);
#define serial2TelemetryInit(protocol) serial2Init(UART_MODE_TELEMETRY, protocol)
void serial2SbusInit(void);
void serial2Stop(void);
// BT driver
#define BLUETOOTH_DEFAULT_BAUDRATE 115200
@@ -36,7 +36,7 @@
#include "../../opentx.h"
uint8_t uart3Mode = UART_MODE_NONE;
uint8_t serial2Mode = 0;
Fifo<512> uart3TxFifo;
extern Fifo<512> telemetryFifo;
extern Fifo<32> sbusFifo;
@@ -73,17 +73,17 @@ void uart3Setup(unsigned int baudrate)
NVIC_EnableIRQ(SERIAL_USART_IRQn);
}
void uart3Init(unsigned int mode, unsigned int protocol)
void serial2Init(unsigned int mode, unsigned int protocol)
{
USART_DeInit(SERIAL_USART);
uart3Mode = false;
serial2Mode = mode;
switch (mode) {
case UART_MODE_TELEMETRY_MIRROR:
uart3Setup(FRSKY_SPORT_BAUDRATE);
break;
#if defined(DEBUG)
#if !defined(USB_SERIAL) && (defined(DEBUG) || defined(CLI))
case UART_MODE_DEBUG:
uart3Setup(DEBUG_BAUDRATE);
break;
@@ -94,36 +94,21 @@ void uart3Init(unsigned int mode, unsigned int protocol)
}
break;
}
uart3Mode = mode;
}
void uart3Putc(const char c)
void serial2Putc(char c)
{
uart3TxFifo.push(c);
USART_ITConfig(SERIAL_USART, USART_IT_TXE, ENABLE);
}
#if defined(DEBUG)
void debugPutc(const char c)
{
if (uart3Mode == UART_MODE_DEBUG) {
uart3Putc(c);
#if defined(USB_SERIAL)
// also send debug output into USB serial port (if active)
sendUsbSerialChar(c);
#endif
}
}
#endif
void uart3SbusInit()
void serial2SbusInit()
{
uart3Setup(100000);
SERIAL_USART->CR1 |= USART_CR1_M | USART_CR1_PCE ;
}
void uart3Stop()
void serial2Stop()
{
USART_DeInit(SERIAL_USART);
}
@@ -149,13 +134,18 @@ extern "C" void SERIAL_USART_IRQHandler(void)
uint8_t data = SERIAL_USART->DR;
if (!(status & USART_FLAG_ERRORS)) {
switch (uart3Mode) {
switch (serial2Mode) {
case UART_MODE_TELEMETRY:
telemetryFifo.push(data);
break;
case UART_MODE_SBUS_TRAINER:
sbusFifo.push(data);
break;
#if !defined(USB_SERIAL) && defined(CLI)
case UART_MODE_DEBUG:
cliRxFifo.push(data);
break;
#endif
}
}