eive-obsw/bsp_q7s/callbacks/rwSpiCallback.cpp

238 lines
8.2 KiB
C++
Raw Normal View History

2021-09-07 16:11:02 +02:00
#include "rwSpiCallback.h"
#include "devices/gpioIds.h"
2021-09-07 16:11:02 +02:00
#include "mission/devices/RwHandler.h"
#include "fsfw_hal/linux/spi/SpiCookie.h"
#include "fsfw_hal/linux/UnixFileGuard.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-06-21 09:50:26 +02:00
namespace rwSpiCallback {
ReturnValue_t spiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *sendData,
2021-06-21 09:50:26 +02:00
size_t sendLen, void* args) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
RwHandler* handler = reinterpret_cast<RwHandler*>(args);
if(handler == nullptr) {
sif::error << "rwSpiCallback::spiCallback: Pointer to handler is invalid"
2021-06-21 09:50:26 +02:00
<< std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
uint8_t writeBuffer[2];
uint8_t writeSize = 0;
2021-10-19 14:45:22 +02:00
gpioId_t gpioId = cookie->getChipSelectPin();
GpioIF* gpioIF = comIf->getGpioInterface();
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;
uint32_t timeoutMs = 0;
MutexIF* mutex = comIf->getMutex(&timeoutType, &timeoutMs);
if(mutex == nullptr or gpioIF == nullptr) {
sif::debug << "rwSpiCallback::spiCallback: Mutex or GPIO interface invalid" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
2021-06-21 09:50:26 +02:00
int fileDescriptor = 0;
std::string device = cookie->getSpiDevice();
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, "rwSpiCallback::spiCallback");
2021-06-21 09:50:26 +02:00
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback::spiCallback: Failed to open device file" << std::endl;
2021-06-21 09:50:26 +02:00
return SpiComIF::OPENING_FILE_FAILED;
}
2021-10-29 18:43:01 +02:00
spi::SpiModes spiMode = spi::SpiModes::MODE_0;
uint32_t spiSpeed = 0;
cookie->getSpiParameters(spiMode, spiSpeed, nullptr);
comIf->setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
2021-06-21 09:50:26 +02:00
result = mutex->lockMutex(timeoutType, timeoutMs);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "rwSpiCallback::spiCallback: Failed to lock mutex" << std::endl;
return result;
}
/** Disconnect PS SPI peripheral and select AXI SPI core */
if(gpioIF->pullHigh(gpioIds::SPI_MUX) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback::spiCallback: Failed to pull spi mux gpio high" << std::endl;
2021-06-21 09:50:26 +02:00
}
/** Sending frame start sign */
writeBuffer[0] = 0x7E;
writeSize = 1;
// Pull SPI CS low. For now, no support for active high given
if(gpioId != gpio::NO_GPIO) {
if(gpioIF->pullLow(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback::spiCallback: Failed to pull chip select low" << std::endl;
2021-06-21 09:50:26 +02:00
}
}
if (write(fileDescriptor, writeBuffer, writeSize) != static_cast<ssize_t>(writeSize)) {
sif::error << "rwSpiCallback::spiCallback: Write failed!" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
2021-06-21 09:50:26 +02:00
return RwHandler::SPI_WRITE_FAILURE;
}
/** Encoding and sending command */
size_t idx = 0;
while(idx < sendLen) {
switch(*(sendData + idx)) {
case 0x7E:
writeBuffer[0] = 0x7D;
writeBuffer[1] = 0x5E;
writeSize = 2;
break;
case 0x7D:
writeBuffer[0] = 0x7D;
writeBuffer[1] = 0x5D;
writeSize = 2;
break;
default:
writeBuffer[0] = *(sendData + idx);
writeSize = 1;
2021-06-24 12:04:36 +02:00
break;
2021-06-21 09:50:26 +02:00
}
if (write(fileDescriptor, writeBuffer, writeSize) != static_cast<ssize_t>(writeSize)) {
sif::error << "rwSpiCallback::spiCallback: Write failed!" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
2021-06-21 09:50:26 +02:00
return RwHandler::SPI_WRITE_FAILURE;
}
2021-06-21 14:46:46 +02:00
idx++;
2021-06-21 09:50:26 +02:00
}
2021-06-28 14:07:37 +02:00
/** Sending frame end sign */
2021-06-21 09:50:26 +02:00
writeBuffer[0] = 0x7E;
writeSize = 1;
if (write(fileDescriptor, writeBuffer, writeSize) != static_cast<ssize_t>(writeSize)) {
sif::error << "rwSpiCallback::spiCallback: Write failed!" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
2021-06-21 09:50:26 +02:00
return RwHandler::SPI_WRITE_FAILURE;
}
uint8_t* rxBuf = nullptr;
result = comIf->getReadBuffer(cookie->getSpiAddress(), &rxBuf);
if(result != HasReturnvaluesIF::RETURN_OK) {
closeSpi(gpioId, gpioIF, mutex);
2021-06-21 09:50:26 +02:00
return result;
}
size_t replyBufferSize = cookie->getMaxBufferSize();
2021-10-19 14:45:22 +02:00
/** There must be a delay of at least 20 ms after sending the command */
2021-06-24 12:04:36 +02:00
usleep(RwDefinitions::SPI_REPLY_DELAY);
/**
* The reaction wheel responds with empty frames while preparing the reply data.
* However, receiving more than 5 empty frames will be interpreted as an error.
*/
uint8_t byteRead = 0;
2021-06-24 12:04:36 +02:00
for (int idx = 0; idx < 10; idx++) {
2021-06-21 09:50:26 +02:00
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback::spiCallback: Read failed" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
2021-06-21 09:50:26 +02:00
return RwHandler::SPI_READ_FAILURE;
}
if (byteRead != 0x7E) {
break;
}
if (idx == 9) {
sif::error << "rwSpiCallback::spiCallback: Empty frame timeout" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
return RwHandler::NO_REPLY;
}
}
size_t decodedFrameLen = 0;
while(decodedFrameLen < replyBufferSize) {
/** First byte already read in */
if (decodedFrameLen != 0) {
byteRead = 0;
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback::spiCallback: Read failed" << std::endl;
result = RwHandler::SPI_READ_FAILURE;
break;
}
}
2021-06-21 09:50:26 +02:00
if (byteRead == 0x7E) {
/** Reached end of frame */
break;
}
2021-06-28 14:07:37 +02:00
else if (byteRead == 0x7D) {
2021-06-21 09:50:26 +02:00
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback::spiCallback: Read failed" << std::endl;
2021-06-21 09:50:26 +02:00
result = RwHandler::SPI_READ_FAILURE;
break;
}
if (byteRead == 0x5E) {
*(rxBuf + decodedFrameLen) = 0x7E;
decodedFrameLen++;
continue;
}
else if (byteRead == 0x5D) {
*(rxBuf + decodedFrameLen) = 0x7D;
decodedFrameLen++;
continue;
}
else {
sif::error << "rwSpiCallback::spiCallback: Invalid substitute" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
2021-06-21 09:50:26 +02:00
result = RwHandler::INVALID_SUBSTITUTE;
break;
}
}
else {
*(rxBuf + decodedFrameLen) = byteRead;
decodedFrameLen++;
continue;
}
/**
* There might be the unlikely case that each byte in a get-telemetry reply has been
* replaced by its substitute. Than the next byte must correspond to the end sign 0x7E.
* Otherwise there might be something wrong.
*/
if (decodedFrameLen == replyBufferSize) {
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback::spiCallback: Failed to read last byte" << std::endl;
2021-06-21 09:50:26 +02:00
result = RwHandler::SPI_READ_FAILURE;
break;
}
if (byteRead != 0x7E) {
sif::error << "rwSpiCallback::spiCallback: Missing end sign 0x7E" << std::endl;
2021-06-21 09:50:26 +02:00
decodedFrameLen--;
result = RwHandler::MISSING_END_SIGN;
break;
}
}
result = HasReturnvaluesIF::RETURN_OK;
}
2021-09-15 17:26:23 +02:00
cookie->setTransferSize(decodedFrameLen);
2021-06-21 09:50:26 +02:00
closeSpi(gpioId, gpioIF, mutex);
return result;
}
void closeSpi (gpioId_t gpioId, GpioIF* gpioIF, MutexIF* mutex) {
2021-06-21 09:50:26 +02:00
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "closeSpi: Failed to pull chip select high" << std::endl;
2021-06-21 09:50:26 +02:00
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback::closeSpi: Failed to unlock mutex" << std::endl;;
}
/** Route SPI interface again to PS SPI peripheral */
if(gpioIF->pullLow(gpioIds::SPI_MUX) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback::spiCallback: Failed to pull spi mux gpio low" << std::endl;
2021-06-21 09:50:26 +02:00
}
}
}