get temperature and status of reaction wheel

This commit is contained in:
2021-06-24 17:32:42 +02:00
parent 8284ebec9f
commit b5fdeb93bb
5 changed files with 89 additions and 97 deletions

View File

@ -62,14 +62,7 @@ ReturnValue_t rwSpiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *s
if (write(fileDescriptor, writeBuffer, writeSize) != static_cast<ssize_t>(writeSize)) {
sif::error << "rwSpiCallback: Write failed!" << std::endl;
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to unlock mutex" << std::endl;;
}
closeSpi(gpioId, gpioIF, mutex);
return RwHandler::SPI_WRITE_FAILURE;
}
@ -94,14 +87,7 @@ ReturnValue_t rwSpiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *s
}
if (write(fileDescriptor, writeBuffer, writeSize) != static_cast<ssize_t>(writeSize)) {
sif::error << "rwSpiCallback: Write failed!" << std::endl;
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to unlock mutex";
}
closeSpi(gpioId, gpioIF, mutex);
return RwHandler::SPI_WRITE_FAILURE;
}
idx++;
@ -113,28 +99,14 @@ ReturnValue_t rwSpiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *s
if (write(fileDescriptor, writeBuffer, writeSize) != static_cast<ssize_t>(writeSize)) {
sif::error << "rwSpiCallback: Write failed!" << std::endl;
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to unlock mutex";
}
closeSpi(gpioId, gpioIF, mutex);
return RwHandler::SPI_WRITE_FAILURE;
}
uint8_t* rxBuf = nullptr;
result = comIf->getReadBuffer(cookie->getSpiAddress(), &rxBuf);
if(result != HasReturnvaluesIF::RETURN_OK) {
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to unlock mutex";
}
closeSpi(gpioId, gpioIF, mutex);
return result;
}
@ -143,56 +115,40 @@ ReturnValue_t rwSpiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *s
/** There must be a delay of 20 ms after sending the command */
usleep(RwDefinitions::SPI_REPLY_DELAY);
/** Receiving reply data */
uint8_t byteRead = 0;
/** Reading the reply frame */
// if(read(fileDescriptor, &byteRead, 1) != 1) {
// if(gpioId != gpio::NO_GPIO) {
// if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
// sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
// }
// }
// if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
// sif::error << "rwSpiCallback: Failed to unlock mutex";
// }
// sif::error << "rwSpiCallback: Failed to read first byte of reply frame" << std::endl;
// return RwHandler::SPI_READ_FAILURE;
// }
uint8_t readbuffer[10];
if(read(fileDescriptor, readbuffer, 10) != 10) {
sif::error << "Failed to read all bytes" << std::endl;;
}
/** First byte must be the start sign 0x7E */
if (byteRead != 0x7E) {
sif::error << "rwSpiCallback: First byte of reply is not 0x7E" << std::endl;
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to unlock mutex";
}
return RwHandler::MISSING_START_SIGN;
}
/**
* 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;
for (int idx = 0; idx < 10; idx++) {
sif::error << "rwSpiCallback: Empty frame timeout";
return RwHandler::NO_REPLY;
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback: Read failed" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
return RwHandler::SPI_READ_FAILURE;
}
if (byteRead != 0x7E) {
break;
}
if (idx == 9) {
sif::error << "rwSpiCallback: Empty frame timeout" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
return RwHandler::NO_REPLY;
}
}
size_t decodedFrameLen = 0;
for (; decodedFrameLen < replyBufferSize; decodedFrameLen++) {
byteRead = 0;
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback: Read failed" << std::endl;
return RwHandler::SPI_READ_FAILURE;
while(decodedFrameLen < replyBufferSize) {
/** First byte already read in */
if (decodedFrameLen != 0) {
byteRead = 0;
if(read(fileDescriptor, &byteRead, 1) != 1) {
sif::error << "rwSpiCallback: Read failed" << std::endl;
result = RwHandler::SPI_READ_FAILURE;
break;
}
}
if (byteRead == 0x7E) {
@ -217,6 +173,7 @@ ReturnValue_t rwSpiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *s
}
else {
sif::error << "rwSpiCallback: Invalid substitute" << std::endl;
closeSpi(gpioId, gpioIF, mutex);
result = RwHandler::INVALID_SUBSTITUTE;
break;
}
@ -250,16 +207,18 @@ ReturnValue_t rwSpiCallback(SpiComIF* comIf, SpiCookie *cookie, const uint8_t *s
cookie->assignTransferSize(decodedFrameLen);
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to pull chip select high" << std::endl;
}
}
closeSpi(gpioId, gpioIF, mutex);
if(gpioId != gpio::NO_GPIO) {
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "rwSpiCallback: Failed to unlock mutex" << std::endl;
}
}
return result;
}
void closeSpi (gpioId_t gpioId, GpioIF* gpioIF, MutexIF* mutex) {
if(gpioId != gpio::NO_GPIO) {
if (gpioIF->pullHigh(gpioId) != HasReturnvaluesIF::RETURN_OK) {
sif::error << "closeSpi: Failed to pull chip select high" << std::endl;
}
}
if(mutex->unlockMutex() != HasReturnvaluesIF::RETURN_OK) {
sif::error << "closeSpi: Failed to unlock mutex" << std::endl;;
}
}