Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpW-R draft 0.4 applied, Shimafuji SpacePi functions added #4

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions includes/SpaceWire.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "SpaceWireSSDTPModule.hh"
#include "SpaceWireUtilities.hh"

#if defined(RASPBERRY_PI)
#include "SpaceWireIFOverSPI.hh"
#endif

#endif /* SPACEWIRE_HH_ */
204 changes: 204 additions & 0 deletions includes/SpaceWireIFOverSPI.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
============================================================================
SpaceWire/RMAP Library is provided under the MIT License.
============================================================================

Copyright (c) 2006-2013 Takayuki Yuasa and The Open-source SpaceWire Project
Copyright (c) 2020-

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* SpaceWireIFOverSPI.hh
*
* Created on: May 19, 2020
* Author: takada
*/

#ifndef SPACEWIREIFOVERSPI_HH_
#define SPACEWIREIFOVERSPI_HH_

#include "CxxUtilities/CxxUtilities.hh"

#include "SpaceWireIF.hh"
#include "SpaceWireUtilities.hh"
#include "SpaceWireSpacePiModule.hh"

/** SpaceWire IF class which is connected to a real SpaceWire IF
* via SPI interface on SpacePi.
*/
class SpaceWireIFOverSPI : public SpaceWireIF, public SpaceWireIFActionTimecodeScynchronizedAction {
private:
bool opened;
SpaceWireSpacePiModule* spacepi;

public:
enum {
InitiatorMode, TargetMode
};

private:
uint32_t operationMode;

public:
SpaceWireIFOverSPI() :
SpaceWireIF() {
}

virtual ~SpaceWireIFOverSPI() {
}

public:
void open() throw (SpaceWireIFException) {

if (state == Opened) {
return;
}

spacepi = NULL;
spacepi = new SpaceWireSpacePiModule();
if(spacepi->open() != 0)
state = Closed;
else
state = Opened;
}

void close() throw (SpaceWireIFException) {
using namespace CxxUtilities;
using namespace std;
if (state == Closed) {
return;
}
state = Closed;
//invoke SpaceWireIFCloseActions to tell other instances
//closing of this SpaceWire interface
invokeSpaceWireIFCloseActions();
if(spacepi != NULL){
spacepi->close();
delete spacepi;
}

}

public:
void send(uint8_t* data, size_t length, SpaceWireEOPMarker::EOPType eopType = SpaceWireEOPMarker::EOP)
throw (SpaceWireIFException) {
using namespace std;
try {
spacepi->send(data, length); // TODO:
} catch (SpaceWireIFException& e) {
throw SpaceWireIFException(SpaceWireIFException::Disconnected);
}
// dummy wait to avoid buffer bug
//usleep(3000);
}

public:
void receive(std::vector<uint8_t>* buffer) throw (SpaceWireIFException) {
try {
uint32_t length, i;
uint8_t buf[2056];
length = 2056;

spacepi->receive(&buf[0], &length);

buffer->resize(length);
if(length != 0){ // SPI DATA copy
memcpy(&(buffer->at(0)), buf+2, length);
}

// for(i=2;i<length+2;i++) {
// printf("%02X ", buf[i]);
// }
// printf("\n");
} catch (SpaceWireSpacePiException& e){
if(e.getStatus() == SpaceWireSpacePiException::Timeout){
throw SpaceWireIFException(SpaceWireIFException::Timeout);
}
} catch (SpaceWireIFException& e) {
using namespace std;
throw SpaceWireIFException(SpaceWireIFException::Disconnected);
}
}

public:
void emitTimecode(uint8_t timeIn, uint8_t controlFlagIn = 0x00) throw (SpaceWireIFException) {
using namespace std;
cerr << "SpaceWireIFOverSPI::emitTimecode() is not implemented." << endl;
throw SpaceWireIFException(SpaceWireIFException::FunctionNotImplemented);
}

public:
virtual void setTxLinkRate(uint32_t linkRateType) throw (SpaceWireIFException) {
using namespace std;
cerr << "SpaceWireIFOverSPI::setTxLinkRate() is not implemented." << endl;
cerr << "Please use SpaceWireIFOverIPClient::setTxDivCount() instead." << endl;
throw SpaceWireIFException(SpaceWireIFException::FunctionNotImplemented);
}

virtual uint32_t getTxLinkRateType() throw (SpaceWireIFException) {
using namespace std;
cerr << "SpaceWireIFOverSPI::getTxLinkRate() is not implemented." << endl;
throw SpaceWireIFException(SpaceWireIFException::FunctionNotImplemented);
}

public:
void setTxDivCount(uint8_t txdivcount) {
using namespace std;
cerr << "SpaceWireIFOverSPI::setTxDivCount() is not implemented." << endl;
throw SpaceWireIFException(SpaceWireIFException::FunctionNotImplemented);
}

public:
void setTimeoutDuration(double microsecond) throw (SpaceWireIFException) {
spacepi->setTimeout(microsecond/1000.);
timeoutDurationInMicroSec = microsecond;
}

public:
uint8_t getTimeCode() throw (SpaceWireIFException) {
using namespace std;
cerr << "SpaceWireIFOverSPI::getTimeCode() is not implemented." << endl;
throw SpaceWireIFException(SpaceWireIFException::FunctionNotImplemented);

return 0; //TODO
}

void doAction(uint8_t timecode) {
this->invokeTimecodeSynchronizedActions(timecode);
}

public:
uint32_t getOperationMode() const {
using namespace std;
cerr << "SpaceWireIFOverSPI::getOperationMode() is not implemented." << endl;
throw SpaceWireIFException(SpaceWireIFException::FunctionNotImplemented);

return InitiatorMode;
}


public:
/** Cancels ongoing receive() method if any exist.
*/
void cancelReceive(){}
};

#endif /* SPACEWIREIFOVERSPI_HH_ */
4 changes: 2 additions & 2 deletions includes/SpaceWireR/SpaceWireREngine.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "SpaceWireR/SpaceWireRClassInterfaces.hh"

//#define SpaceWireREngineDumpPacket
#define DebugSpaceWireREngine
//#define DebugSpaceWireREngine

#undef SpaceWireREngineDumpPacket
//#undef DebugSpaceWireREngine
#undef DebugSpaceWireREngine


/*
Expand Down
2 changes: 1 addition & 1 deletion includes/SpaceWireR/SpaceWireRPacket.hh
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ public:
#endif
for (size_t i = 0; i < payloadLengthValue; i++) {
payload.push_back(buffer->at(index));
index++;
}
index += payloadLengthValue;
} catch (...) {
throw SpaceWireRPacketException(SpaceWireRPacketException::InvalidPayloadLength);
}
Expand Down
20 changes: 2 additions & 18 deletions includes/SpaceWireR/SpaceWireRReceiveTEP.hh
Original file line number Diff line number Diff line change
Expand Up @@ -706,24 +706,8 @@ private:
cout << "SpaceWireRReceiveTEP::processHeartBeatPacket() nHeartBeat = " << nReceivedHeartBeatPackets
<< " sequence number = " << (uint32_t) packet->getSequenceNumber() << endl;
#endif

if (insideForwardReceiveSlidingWindow(sequenceNumber)) {
if (this->receiveSlidingWindowBuffer[sequenceNumber] == NULL) {
replyAckForPacket(packet);
this->receiveSlidingWindowBuffer[sequenceNumber] = packet;
slideReceiveSlidingWindow();
} else {
replyAckForPacket(packet);
delete packet;
}
} else if (insideBackwardReceiveSlidingWindow(sequenceNumber)) {
replyAckForPacket(packet);
delete packet;
} else {
malfunctioningTransportChannel();
nDiscardedHeartBeatPackets++;
}

replyAckForPacket(packet);
delete packet;
}

private:
Expand Down
57 changes: 30 additions & 27 deletions includes/SpaceWireR/SpaceWireRTEP.hh
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,29 @@ protected:
bool insideBackwardSlidingWindow(uint8_t sequenceNumber) =0;
*/

/*

private:
bool insideForwardSlidingWindow(uint8_t sequenceNumber) {
uint8_t n = this->slidingWindowFrom;
uint8_t k = this->slidingWindowSize;
if ((uint8_t) (n) < (uint8_t) (n + k - 1)) {
// n -- n + k - 1
if (n <= sequenceNumber && sequenceNumber <= (uint8_t) (n + k - 1)) {
return true;
} else {
return false;
uint8_t n = this->slidingWindowFrom;
uint8_t k = this->slidingWindowSize;
if ((uint8_t) (n) <= (uint8_t) (n + k - 1)) {
// n -- n + k - 1
if (n <= sequenceNumber && sequenceNumber <= (uint8_t) (n + k - 1)) {
return true;
} else {
return false;
}
} else {
// n -- 255, 0 -- n + k - 1
if ((n <= sequenceNumber && sequenceNumber <= 255)
|| (0 <= sequenceNumber && sequenceNumber <= (uint8_t) (n + k - 1))) {
return true;
} else {
return false;
}
}
}
} else {
// n -- 255, 0 -- n + k - 1
if ((n <= sequenceNumber && sequenceNumber <= 255)
|| (0 <= sequenceNumber && sequenceNumber <= (uint8_t) (n + k - 1))) {
return true;
} else {
return false;
}
}
}

/*
private:
bool insideBackwardSlidingWindow(uint8_t sequenceNumber) {
uint8_t n = this->slidingWindowFrom;
Expand Down Expand Up @@ -506,7 +506,7 @@ protected:
this->heartBeatTimer->resetHeartBeatTimer();

//configure SpaceWireRPacket instance
packet->setSequenceNumber(sequenceNumber);
packet->setSequenceNumber(specifiedSequenceNumber);

using namespace std;
#ifdef DebugSpaceWireRTEP
Expand All @@ -533,8 +533,6 @@ protected:

//update counters
retryTimeoutCounters[specifiedSequenceNumber] = 0;
specifiedSequenceNumber++;
incrementNOfOutstandingPackets();

//send segment
try {
Expand All @@ -544,8 +542,6 @@ protected:
cout << packet->toString() << endl;
#endif
spwREngine->sendPacket(packet);
nSentSegments++;
packetHasBeenSent[packet->getSequenceNumber()] = true;
} catch (...) {
sendMutex.unlock();
this->malfunctioningSpaceWireIF();
Expand Down Expand Up @@ -635,6 +631,10 @@ protected:
if (nOfOutstandingPackets < this->slidingWindowSize) {
nOfOutstandingPackets++;
} else {
#ifdef DebugSpaceWireRTEP
cout << "SpaceWireRTEP::incrementNOfOutstandingPackets() nOfOutstandingPackets=" << dec << nOfOutstandingPackets
<< " is over slidingWindowSize, MALFUNCTION" << endl;
#endif
this->malfunctioningTransportChannel();
}
mutexForNOfOutstandingPackets.unlock();
Expand All @@ -646,6 +646,9 @@ protected:
if (nOfOutstandingPackets != 0) {
nOfOutstandingPackets--;
} else {
#ifdef DebugSpaceWireRTEP
cout << "SpaceWireRTEP::decrementNOfOutstandingPackets() nOfOutstandingPackets=0, MALFUNCTION" << endl;
#endif
this->malfunctioningTransportChannel();
}
mutexForNOfOutstandingPackets.unlock();
Expand All @@ -668,7 +671,7 @@ protected:
cout << "SpaceWireRTEP::getAvailablePacketInstance() nOfOutstandingPackets=" << dec << nOfOutstandingPackets
<< endl;
#endif
if (nOfOutstandingPackets < this->slidingWindowSize) {
if (insideForwardSlidingWindow(this->sequenceNumber)) {
SpaceWireRPacket* packet = slidingWindowBuffer[this->sequenceNumber];
return packet;
} else {
Expand Down Expand Up @@ -823,7 +826,7 @@ private:
heartBeatPacket->setPacketType(SpaceWireRPacketType::HeartBeatPacket);
heartBeatPacket->clearPayload();
heartBeatPacket->setCompleteSegmentFlag();
sendPacket(heartBeatPacket);
sendPacketWithSpecifiedSequenceNumber(heartBeatPacket, 0x00);
nTransmittedHeartBeatPackets++;
#ifdef DebugSpaceWireRTEP
cout << "SpaceWireRTEP::emitHeartBeatPacket() HeartBeat packet has been transmitted and acknowledged." << endl;
Expand Down
8 changes: 4 additions & 4 deletions includes/SpaceWireR/SpaceWireRTransmitTEP.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

#include "SpaceWireR/SpaceWireRTEP.hh"

#define DebugSpaceWireRTransmitTEP
#define DebugSpaceWireRTransmitTEPDumpCriticalIncidents
//#undef DebugSpaceWireRTransmitTEP
//#undef DebugSpaceWireRTransmitTEPDumpCriticalIncidents
//#define DebugSpaceWireRTransmitTEP
//#define DebugSpaceWireRTransmitTEPDumpCriticalIncidents
#undef DebugSpaceWireRTransmitTEP
#undef DebugSpaceWireRTransmitTEPDumpCriticalIncidents

class SpaceWireRTransmitTEP: public SpaceWireRTEP, public CxxUtilities::StoppableThread {

Expand Down
Loading