From 6486bf4efc9e97a9660fc3e4369e4f121c386c03 Mon Sep 17 00:00:00 2001 From: Andy CA6JAU Date: Tue, 4 Jul 2017 14:27:57 -0400 Subject: [PATCH] =?UTF-8?q?Adding=20Jonathan=E2=80=99s=20ring=20buffer=20t?= =?UTF-8?q?o=20serial=20repeater?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SerialArduino.cpp | 20 ++++++++++++++++++++ SerialPort.cpp | 27 ++++++++++++++++++++------- SerialPort.h | 3 +++ SerialSTM.cpp | 40 +++++++++++++++++++++++++++++++++------- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/SerialArduino.cpp b/SerialArduino.cpp index 021bfbc..65f05c6 100644 --- a/SerialArduino.cpp +++ b/SerialArduino.cpp @@ -112,5 +112,25 @@ void CSerialPort::writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool } } +int CSerialPort::availableForWriteInt(uint8_t n) +{ + switch (n) { + case 1U: + #if defined(STM32_USART1_HOST) && defined(__STM32F1__) + return Serial1.availableForWrite(); + #else + return Serial.availableForWrite(); + #endif + case 3U: + #if defined(SERIAL_REPEATER) && defined(__STM32F1__) + return Serial2.availableForWrite(); + #elif defined(SERIAL_REPEATER) && (defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)) + return Serial1.availableForWrite(); + #endif + default: + return false; + } +} + #endif diff --git a/SerialPort.cpp b/SerialPort.cpp index 9acfc52..891aafa 100644 --- a/SerialPort.cpp +++ b/SerialPort.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013,2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2013,2015,2016,2017 by Jonathan Naylor G4KLX * Copyright (C) 2016 by Colin Durbridge G4EML * Copyright (C) 2016, 2017 by Andy Uribe CA6JAU * @@ -90,7 +90,8 @@ CSerialPort::CSerialPort() : m_buffer(), m_ptr(0U), m_len(0U), -m_debug(false) +m_debug(false), +m_repeat() { } @@ -536,12 +537,11 @@ void CSerialPort::process() } break; -#if defined(SERIAL_REPEATER) case MMDVM_SERIAL: - writeInt(3U, m_buffer + 3U, m_len - 3U); + for (uint8_t i = 3U; i < m_len; i++) + m_repeat.put(m_buffer[i]); break; -#endif - + default: // Handle this, send a NAK back sendNAK(1U); @@ -555,7 +555,20 @@ void CSerialPort::process() } #if defined(SERIAL_REPEATER) - // Drain any incoming serial data + // Write any outgoing serial data + uint16_t space = m_repeat.getData(); + if (space > 0U) { + int avail = availableForWriteInt(3U); + if (avail < space) + space = avail; + + for (uint16_t i = 0U; i < space; i++) { + uint8_t c = m_repeat.get(); + writeInt(3U, &c, 1U); + } + } + + // Read any incoming serial data while (availableInt(3U)) readInt(3U); #endif diff --git a/SerialPort.h b/SerialPort.h index 920b220..7cf3300 100644 --- a/SerialPort.h +++ b/SerialPort.h @@ -20,6 +20,7 @@ #define SERIALPORT_H #include "Globals.h" +#include "SerialRB.h" class CSerialPort { public: @@ -55,6 +56,7 @@ private: uint8_t m_ptr; uint8_t m_len; bool m_debug; + CSerialRB m_repeat; void sendACK(); void sendNAK(uint8_t err); @@ -68,6 +70,7 @@ private: // Hardware versions void beginInt(uint8_t n, int speed); int availableInt(uint8_t n); + int availableForWriteInt(uint8_t n); uint8_t readInt(uint8_t n); void writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush = false); }; diff --git a/SerialSTM.cpp b/SerialSTM.cpp index d8dd0d7..7e1f639 100644 --- a/SerialSTM.cpp +++ b/SerialSTM.cpp @@ -41,8 +41,8 @@ USART2 - TXD PA2 - RXD PA3 */ -#define TX_SERIAL_FIFO_SIZE 512U -#define RX_SERIAL_FIFO_SIZE 512U +#define TX_SERIAL_FIFO_SIZE 256U +#define RX_SERIAL_FIFO_SIZE 256U #if defined(STM32_USART1_HOST) @@ -208,7 +208,7 @@ void InitUSART1(int speed) RXSerialfifoinit1(); } -uint8_t AvailUSART1(void) +uint8_t AvailUSART1() { if (RXSerialfifolevel1() > 0U) return 1U; @@ -216,7 +216,12 @@ uint8_t AvailUSART1(void) return 0U; } -uint8_t ReadUSART1(void) +int AvailForWriteUSART1() +{ + return TX_SERIAL_FIFO_SIZE - TXSerialfifolevel1(); +} + +uint8_t ReadUSART1() { uint8_t data_c = RXSerialfifo1[RXSerialfifotail1]; @@ -403,7 +408,7 @@ void InitUSART2(int speed) RXSerialfifoinit2(); } -uint8_t AvailUSART2(void) +uint8_t AvailUSART2() { if (RXSerialfifolevel2() > 0U) return 1U; @@ -411,7 +416,12 @@ uint8_t AvailUSART2(void) return 0U; } -uint8_t ReadUSART2(void) +int AvailForWriteUSART2() +{ + return TX_SERIAL_FIFO_SIZE - TXSerialfifolevel2(); +} + +uint8_t ReadUSART2() { uint8_t data_c = RXSerialfifo2[RXSerialfifotail2]; @@ -468,7 +478,23 @@ int CSerialPort::availableInt(uint8_t n) return AvailUSART2(); #endif default: - return false; + return 0; + } +} + +int CSerialPort::availableForWriteInt(uint8_t n) +{ + switch (n) { + case 1U: + #if defined(STM32_USART1_HOST) + return AvailForWriteUSART1(); + #endif + #if defined(SERIAL_REPEATER) + case 3U: + return AvailForWriteUSART2(); + #endif + default: + return 0; } }