diff --git a/SerialArduino.cpp b/SerialArduino.cpp new file mode 100644 index 0000000..e723fee --- /dev/null +++ b/SerialArduino.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2016 by Jonathan Naylor G4KLX + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "Config.h" +#include "Globals.h" + +#include "SerialPort.h" + +#if defined(__SAM3X8E__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) + +void CSerialPort::beginInt(uint8_t n, int speed) +{ + switch (n) { + case 1U: + Serial.begin(speed); + break; + case 2U: + Serial2.begin(speed); + break; + case 3U: + Serial3.begin(speed); + break; + default: + break; + } +} + +int CSerialPort::availableInt(uint8_t n) +{ + switch (n) { + case 1U: + return Serial.available(); + case 2U: + return Serial2.available(); + case 3U: + return Serial3.available(); + default: + return false; + } +} + +uint8_t CSerialPort::readInt(uint8_t n) +{ + switch (n) { + case 1U: + return Serial.read(); + case 2U: + return Serial2.read(); + case 3U: + return Serial3.read(); + default: + return 0U; + } +} + +void CSerialPort::writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush) +{ + switch (n) { + case 1U: + Serial.write(data, length); + if (flush) + Serial.flush(); + break; + case 2U: + Serial2.write(data, length); + if (flush) + Serial2.flush(); + break; + case 3U: + Serial3.write(data, length); + if (flush) + Serial3.flush(); + break; + default: + break; + } +} + +#endif + diff --git a/SerialPort.cpp b/SerialPort.cpp index 30ea8b6..faf72a4 100644 --- a/SerialPort.cpp +++ b/SerialPort.cpp @@ -92,7 +92,7 @@ void CSerialPort::sendACK() reply[2U] = MMDVM_ACK; reply[3U] = m_buffer[2U]; - write(reply, 4); + writeInt(1U, reply, 4); } void CSerialPort::sendNAK(uint8_t err) @@ -105,7 +105,7 @@ void CSerialPort::sendNAK(uint8_t err) reply[3U] = m_buffer[2U]; reply[4U] = err; - write(reply, 5); + writeInt(1U, reply, 5); } void CSerialPort::getStatus() @@ -180,7 +180,7 @@ void CSerialPort::getStatus() else reply[10U] = 0U; - write(reply, 11); + writeInt(1U, reply, 11); } void CSerialPort::getVersion() @@ -199,7 +199,7 @@ void CSerialPort::getVersion() reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } uint8_t CSerialPort::setConfig(const uint8_t* data, uint8_t length) @@ -382,17 +382,17 @@ void CSerialPort::setMode(MMDVM_STATE modemState) void CSerialPort::start() { - Serial.begin(115200); + beginInt(1U, 115200); #if defined(SERIAL_REPEATER) - Serial3.begin(9600); + beginInt(3U, 9600); #endif } void CSerialPort::process() { - while (Serial.available()) { - uint8_t c = Serial.read(); + while (availableInt(1U)) { + uint8_t c = readInt(1U); if (m_ptr == 0U && c == MMDVM_FRAME_START) { // Handle the frame start correctly @@ -629,7 +629,7 @@ void CSerialPort::process() #if defined(SERIAL_REPEATER) case MMDVM_SERIAL: - Serial3.write(m_buffer + 3U, m_len - 3U); + writeInt(3U, m_buffer + 3U, m_len - 3U); break; #endif @@ -647,8 +647,8 @@ void CSerialPort::process() #if defined(SERIAL_REPEATER) // Drain any incoming serial data - while (Serial3.available()) - Serial3.read(); + while (availableInt(3U)) + readInt(3U); #endif } @@ -671,7 +671,7 @@ void CSerialPort::writeDStarHeader(const uint8_t* header, uint8_t length) reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } void CSerialPort::writeDStarData(const uint8_t* data, uint8_t length) @@ -694,7 +694,7 @@ void CSerialPort::writeDStarData(const uint8_t* data, uint8_t length) reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } void CSerialPort::writeDStarLost() @@ -711,7 +711,7 @@ void CSerialPort::writeDStarLost() reply[1U] = 3U; reply[2U] = MMDVM_DSTAR_LOST; - write(reply, 3); + writeInt(1U, reply, 3); } void CSerialPort::writeDStarEOT() @@ -728,7 +728,7 @@ void CSerialPort::writeDStarEOT() reply[1U] = 3U; reply[2U] = MMDVM_DSTAR_EOT; - write(reply, 3); + writeInt(1U, reply, 3); } void CSerialPort::writeDMRData(bool slot, const uint8_t* data, uint8_t length) @@ -751,7 +751,7 @@ void CSerialPort::writeDMRData(bool slot, const uint8_t* data, uint8_t length) reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } void CSerialPort::writeDMRLost(bool slot) @@ -768,7 +768,7 @@ void CSerialPort::writeDMRLost(bool slot) reply[1U] = 3U; reply[2U] = slot ? MMDVM_DMR_LOST2 : MMDVM_DMR_LOST1; - write(reply, 3); + writeInt(1U, reply, 3); } void CSerialPort::writeYSFData(const uint8_t* data, uint8_t length) @@ -791,7 +791,7 @@ void CSerialPort::writeYSFData(const uint8_t* data, uint8_t length) reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } void CSerialPort::writeYSFLost() @@ -808,7 +808,7 @@ void CSerialPort::writeYSFLost() reply[1U] = 3U; reply[2U] = MMDVM_YSF_LOST; - write(reply, 3); + writeInt(1U, reply, 3); } void CSerialPort::writeP25Hdr(const uint8_t* data, uint8_t length) @@ -831,7 +831,7 @@ void CSerialPort::writeP25Hdr(const uint8_t* data, uint8_t length) reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } void CSerialPort::writeP25Ldu(const uint8_t* data, uint8_t length) @@ -854,7 +854,7 @@ void CSerialPort::writeP25Ldu(const uint8_t* data, uint8_t length) reply[1U] = count; - write(reply, count); + writeInt(1U, reply, count); } void CSerialPort::writeP25Lost() @@ -871,7 +871,7 @@ void CSerialPort::writeP25Lost() reply[1U] = 3U; reply[2U] = MMDVM_P25_LOST; - write(reply, 3); + writeInt(1U, reply, 3); } void CSerialPort::writeCalData(const uint8_t* data, uint8_t length) @@ -891,15 +891,7 @@ void CSerialPort::writeCalData(const uint8_t* data, uint8_t length) reply[1U] = count; - write(reply, count); -} - -void CSerialPort::write(const uint8_t* data, uint16_t length, bool flush) -{ - Serial.write(data, length); - - if (flush) - Serial.flush(); + writeInt(1U, reply, count); } void CSerialPort::writeDebug(const char* text) @@ -916,7 +908,7 @@ void CSerialPort::writeDebug(const char* text) reply[1U] = count; - write(reply, count, true); + writeInt(1U, reply, count, true); } void CSerialPort::writeDebug(const char* text, int16_t n1) @@ -936,7 +928,7 @@ void CSerialPort::writeDebug(const char* text, int16_t n1) reply[1U] = count; - write(reply, count, true); + writeInt(1U, reply, count, true); } void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2) @@ -959,7 +951,7 @@ void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2) reply[1U] = count; - write(reply, count, true); + writeInt(1U, reply, count, true); } void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3) @@ -985,7 +977,7 @@ void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n reply[1U] = count; - write(reply, count, true); + writeInt(1U, reply, count, true); } void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3, int16_t n4) @@ -1014,7 +1006,7 @@ void CSerialPort::writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n reply[1U] = count; - write(reply, count, true); + writeInt(1U, reply, count, true); } void CSerialPort::writeAssert(bool cond, const char* text, const char* file, long line) @@ -1042,6 +1034,6 @@ void CSerialPort::writeAssert(bool cond, const char* text, const char* file, lon reply[1U] = count; - write(reply, count, true); + writeInt(1U, reply, count, true); } diff --git a/SerialPort.h b/SerialPort.h index e54ee7e..527ff64 100644 --- a/SerialPort.h +++ b/SerialPort.h @@ -67,8 +67,13 @@ private: void getVersion(); uint8_t setConfig(const uint8_t* data, uint8_t length); uint8_t setMode(const uint8_t* data, uint8_t length); - void write(const uint8_t* data, uint16_t length, bool flush = false); void setMode(MMDVM_STATE modemState); + + // Hardware versions + void beginInt(uint8_t n, int speed); + int availableInt(uint8_t n); + uint8_t readInt(uint8_t n); + void writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush = false); }; #endif