Merge pull request 'applied clang script' (#534) from mueller/apply-clang-script into development

Reviewed-on: fsfw/fsfw#534
This commit is contained in:
Robin Müller 2022-02-02 10:38:07 +01:00
commit bf5a11cbd3
809 changed files with 52010 additions and 56052 deletions

View File

@ -1,50 +1,48 @@
#include "fsfw_hal/common/gpio/GpioCookie.h" #include "fsfw_hal/common/gpio/GpioCookie.h"
#include "fsfw/serviceinterface/ServiceInterface.h" #include "fsfw/serviceinterface/ServiceInterface.h"
GpioCookie::GpioCookie() { GpioCookie::GpioCookie() {}
}
ReturnValue_t GpioCookie::addGpio(gpioId_t gpioId, GpioBase* gpioConfig) { ReturnValue_t GpioCookie::addGpio(gpioId_t gpioId, GpioBase* gpioConfig) {
if (gpioConfig == nullptr) { if (gpioConfig == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "GpioCookie::addGpio: gpioConfig is nullpointer" << std::endl; sif::warning << "GpioCookie::addGpio: gpioConfig is nullpointer" << std::endl;
#else #else
sif::printWarning("GpioCookie::addGpio: gpioConfig is nullpointer\n"); sif::printWarning("GpioCookie::addGpio: gpioConfig is nullpointer\n");
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
auto gpioMapIter = gpioMap.find(gpioId);
if(gpioMapIter == gpioMap.end()) {
auto statusPair = gpioMap.emplace(gpioId, gpioConfig);
if (statusPair.second == false) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "GpioCookie::addGpio: Failed to add GPIO " << gpioId <<
" to GPIO map" << std::endl;
#else
sif::printWarning("GpioCookie::addGpio: Failed to add GPIO %d to GPIO map\n", gpioId);
#endif
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "GpioCookie::addGpio: GPIO already exists in GPIO map " << std::endl;
#else
sif::printWarning("GpioCookie::addGpio: GPIO already exists in GPIO map\n");
#endif
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
auto gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) {
auto statusPair = gpioMap.emplace(gpioId, gpioConfig);
if (statusPair.second == false) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "GpioCookie::addGpio: Failed to add GPIO " << gpioId << " to GPIO map"
<< std::endl;
#else
sif::printWarning("GpioCookie::addGpio: Failed to add GPIO %d to GPIO map\n", gpioId);
#endif
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "GpioCookie::addGpio: GPIO already exists in GPIO map " << std::endl;
#else
sif::printWarning("GpioCookie::addGpio: GPIO already exists in GPIO map\n");
#endif
#endif
return HasReturnvaluesIF::RETURN_FAILED;
} }
GpioMap GpioCookie::getGpioMap() const { GpioMap GpioCookie::getGpioMap() const { return gpioMap; }
return gpioMap;
}
GpioCookie::~GpioCookie() { GpioCookie::~GpioCookie() {
for(auto& config: gpioMap) { for (auto& config : gpioMap) {
delete(config.second); delete (config.second);
} }
} }

View File

@ -1,12 +1,12 @@
#ifndef COMMON_GPIO_GPIOCOOKIE_H_ #ifndef COMMON_GPIO_GPIOCOOKIE_H_
#define COMMON_GPIO_GPIOCOOKIE_H_ #define COMMON_GPIO_GPIOCOOKIE_H_
#include "GpioIF.h"
#include "gpioDefinitions.h"
#include <fsfw/devicehandlers/CookieIF.h> #include <fsfw/devicehandlers/CookieIF.h>
#include <fsfw/returnvalues/HasReturnvaluesIF.h> #include <fsfw/returnvalues/HasReturnvaluesIF.h>
#include "GpioIF.h"
#include "gpioDefinitions.h"
/** /**
* @brief Cookie for the GpioIF. Allows the GpioIF to determine which * @brief Cookie for the GpioIF. Allows the GpioIF to determine which
* GPIOs to initialize and whether they should be configured as in- or * GPIOs to initialize and whether they should be configured as in- or
@ -17,25 +17,24 @@
* *
* @author J. Meier * @author J. Meier
*/ */
class GpioCookie: public CookieIF { class GpioCookie : public CookieIF {
public: public:
GpioCookie();
GpioCookie(); virtual ~GpioCookie();
virtual ~GpioCookie(); ReturnValue_t addGpio(gpioId_t gpioId, GpioBase* gpioConfig);
ReturnValue_t addGpio(gpioId_t gpioId, GpioBase* gpioConfig); /**
* @brief Get map with registered GPIOs.
*/
GpioMap getGpioMap() const;
/** private:
* @brief Get map with registered GPIOs. /**
*/ * Returns a copy of the internal GPIO map.
GpioMap getGpioMap() const; */
GpioMap gpioMap;
private:
/**
* Returns a copy of the internal GPIO map.
*/
GpioMap gpioMap;
}; };
#endif /* COMMON_GPIO_GPIOCOOKIE_H_ */ #endif /* COMMON_GPIO_GPIOCOOKIE_H_ */

View File

@ -1,9 +1,10 @@
#ifndef COMMON_GPIO_GPIOIF_H_ #ifndef COMMON_GPIO_GPIOIF_H_
#define COMMON_GPIO_GPIOIF_H_ #define COMMON_GPIO_GPIOIF_H_
#include "gpioDefinitions.h"
#include <fsfw/returnvalues/HasReturnvaluesIF.h>
#include <fsfw/devicehandlers/CookieIF.h> #include <fsfw/devicehandlers/CookieIF.h>
#include <fsfw/returnvalues/HasReturnvaluesIF.h>
#include "gpioDefinitions.h"
class GpioCookie; class GpioCookie;
@ -13,42 +14,41 @@ class GpioCookie;
* @author J. Meier * @author J. Meier
*/ */
class GpioIF : public HasReturnvaluesIF { class GpioIF : public HasReturnvaluesIF {
public: public:
virtual ~GpioIF(){};
virtual ~GpioIF() {}; /**
* @brief Called by the GPIO using object.
* @param cookie Cookie specifying informations of the GPIOs required
* by a object.
*/
virtual ReturnValue_t addGpios(GpioCookie* cookie) = 0;
/** /**
* @brief Called by the GPIO using object. * @brief By implementing this function a child must provide the
* @param cookie Cookie specifying informations of the GPIOs required * functionality to pull a certain GPIO to high logic level.
* by a object. *
*/ * @param gpioId A unique number which specifies the GPIO to drive.
virtual ReturnValue_t addGpios(GpioCookie* cookie) = 0; * @return Returns RETURN_OK for success. This should never return RETURN_FAILED.
*/
virtual ReturnValue_t pullHigh(gpioId_t gpioId) = 0;
/** /**
* @brief By implementing this function a child must provide the * @brief By implementing this function a child must provide the
* functionality to pull a certain GPIO to high logic level. * functionality to pull a certain GPIO to low logic level.
* *
* @param gpioId A unique number which specifies the GPIO to drive. * @param gpioId A unique number which specifies the GPIO to drive.
* @return Returns RETURN_OK for success. This should never return RETURN_FAILED. */
*/ virtual ReturnValue_t pullLow(gpioId_t gpioId) = 0;
virtual ReturnValue_t pullHigh(gpioId_t gpioId) = 0;
/** /**
* @brief By implementing this function a child must provide the * @brief This function requires a child to implement the functionality to read the state of
* functionality to pull a certain GPIO to low logic level. * an ouput or input gpio.
* *
* @param gpioId A unique number which specifies the GPIO to drive. * @param gpioId A unique number which specifies the GPIO to read.
*/ * @param gpioState State of GPIO will be written to this pointer.
virtual ReturnValue_t pullLow(gpioId_t gpioId) = 0; */
virtual ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) = 0;
/**
* @brief This function requires a child to implement the functionality to read the state of
* an ouput or input gpio.
*
* @param gpioId A unique number which specifies the GPIO to read.
* @param gpioState State of GPIO will be written to this pointer.
*/
virtual ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) = 0;
}; };
#endif /* COMMON_GPIO_GPIOIF_H_ */ #endif /* COMMON_GPIO_GPIOIF_H_ */

View File

@ -1,44 +1,34 @@
#ifndef COMMON_GPIO_GPIODEFINITIONS_H_ #ifndef COMMON_GPIO_GPIODEFINITIONS_H_
#define COMMON_GPIO_GPIODEFINITIONS_H_ #define COMMON_GPIO_GPIODEFINITIONS_H_
#include <map>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <map>
using gpioId_t = uint16_t; using gpioId_t = uint16_t;
namespace gpio { namespace gpio {
enum Levels: uint8_t { enum Levels : uint8_t { LOW = 0, HIGH = 1, NONE = 99 };
LOW = 0,
HIGH = 1,
NONE = 99
};
enum Direction: uint8_t { enum Direction : uint8_t { IN = 0, OUT = 1 };
IN = 0,
OUT = 1
};
enum GpioOperation { enum GpioOperation { READ, WRITE };
READ,
WRITE
};
enum class GpioTypes { enum class GpioTypes {
NONE, NONE,
GPIO_REGULAR_BY_CHIP, GPIO_REGULAR_BY_CHIP,
GPIO_REGULAR_BY_LABEL, GPIO_REGULAR_BY_LABEL,
GPIO_REGULAR_BY_LINE_NAME, GPIO_REGULAR_BY_LINE_NAME,
CALLBACK CALLBACK
}; };
static constexpr gpioId_t NO_GPIO = -1; static constexpr gpioId_t NO_GPIO = -1;
using gpio_cb_t = void (*) (gpioId_t gpioId, gpio::GpioOperation gpioOp, gpio::Levels value, using gpio_cb_t = void (*)(gpioId_t gpioId, gpio::GpioOperation gpioOp, gpio::Levels value,
void* args); void* args);
} } // namespace gpio
/** /**
* @brief Struct containing information about the GPIO to use. This is * @brief Struct containing information about the GPIO to use. This is
@ -55,78 +45,71 @@ using gpio_cb_t = void (*) (gpioId_t gpioId, gpio::GpioOperation gpioOp, gpio::L
* pointer. * pointer.
*/ */
class GpioBase { class GpioBase {
public: public:
GpioBase() = default;
GpioBase() = default; GpioBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction,
gpio::Levels initValue)
: gpioType(gpioType), consumer(consumer), direction(direction), initValue(initValue) {}
GpioBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction, virtual ~GpioBase(){};
gpio::Levels initValue):
gpioType(gpioType), consumer(consumer),direction(direction), initValue(initValue) {}
virtual~ GpioBase() {}; // Can be used to cast GpioBase to a concrete child implementation
gpio::GpioTypes gpioType = gpio::GpioTypes::NONE;
// Can be used to cast GpioBase to a concrete child implementation std::string consumer;
gpio::GpioTypes gpioType = gpio::GpioTypes::NONE; gpio::Direction direction = gpio::Direction::IN;
std::string consumer; gpio::Levels initValue = gpio::Levels::NONE;
gpio::Direction direction = gpio::Direction::IN;
gpio::Levels initValue = gpio::Levels::NONE;
}; };
class GpiodRegularBase: public GpioBase { class GpiodRegularBase : public GpioBase {
public: public:
GpiodRegularBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction, GpiodRegularBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction,
gpio::Levels initValue, int lineNum): gpio::Levels initValue, int lineNum)
GpioBase(gpioType, consumer, direction, initValue), lineNum(lineNum) { : GpioBase(gpioType, consumer, direction, initValue), lineNum(lineNum) {}
}
// line number will be configured at a later point for the open by line name configuration // line number will be configured at a later point for the open by line name configuration
GpiodRegularBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction, GpiodRegularBase(gpio::GpioTypes gpioType, std::string consumer, gpio::Direction direction,
gpio::Levels initValue): GpioBase(gpioType, consumer, direction, initValue) { gpio::Levels initValue)
} : GpioBase(gpioType, consumer, direction, initValue) {}
int lineNum = 0; int lineNum = 0;
struct gpiod_line* lineHandle = nullptr; struct gpiod_line* lineHandle = nullptr;
}; };
class GpiodRegularByChip: public GpiodRegularBase { class GpiodRegularByChip : public GpiodRegularBase {
public: public:
GpiodRegularByChip() : GpiodRegularByChip()
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP, std::string(), gpio::Direction::IN,
std::string(), gpio::Direction::IN, gpio::LOW, 0) { gpio::LOW, 0) {}
}
GpiodRegularByChip(std::string chipname_, int lineNum_, std::string consumer_, GpiodRegularByChip(std::string chipname_, int lineNum_, std::string consumer_,
gpio::Direction direction_, gpio::Levels initValue_) : gpio::Direction direction_, gpio::Levels initValue_)
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP, consumer_, direction_, initValue_,
consumer_, direction_, initValue_, lineNum_), lineNum_),
chipname(chipname_){ chipname(chipname_) {}
}
GpiodRegularByChip(std::string chipname_, int lineNum_, std::string consumer_) : GpiodRegularByChip(std::string chipname_, int lineNum_, std::string consumer_)
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP, consumer_, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP, consumer_, gpio::Direction::IN,
gpio::Direction::IN, gpio::LOW, lineNum_), gpio::LOW, lineNum_),
chipname(chipname_) { chipname(chipname_) {}
}
std::string chipname; std::string chipname;
}; };
class GpiodRegularByLabel: public GpiodRegularBase { class GpiodRegularByLabel : public GpiodRegularBase {
public: public:
GpiodRegularByLabel(std::string label_, int lineNum_, std::string consumer_, GpiodRegularByLabel(std::string label_, int lineNum_, std::string consumer_,
gpio::Direction direction_, gpio::Levels initValue_) : gpio::Direction direction_, gpio::Levels initValue_)
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL, consumer_, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL, consumer_, direction_, initValue_,
direction_, initValue_, lineNum_), lineNum_),
label(label_) { label(label_) {}
}
GpiodRegularByLabel(std::string label_, int lineNum_, std::string consumer_) : GpiodRegularByLabel(std::string label_, int lineNum_, std::string consumer_)
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL, consumer_, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL, consumer_, gpio::Direction::IN,
gpio::Direction::IN, gpio::LOW, lineNum_), gpio::LOW, lineNum_),
label(label_) { label(label_) {}
}
std::string label; std::string label;
}; };
/** /**
@ -134,34 +117,34 @@ public:
* line name. This line name can be set in the device tree and must be unique. Otherwise * line name. This line name can be set in the device tree and must be unique. Otherwise
* the driver will open the first line with the given name. * the driver will open the first line with the given name.
*/ */
class GpiodRegularByLineName: public GpiodRegularBase { class GpiodRegularByLineName : public GpiodRegularBase {
public: public:
GpiodRegularByLineName(std::string lineName_, std::string consumer_, gpio::Direction direction_, GpiodRegularByLineName(std::string lineName_, std::string consumer_, gpio::Direction direction_,
gpio::Levels initValue_) : gpio::Levels initValue_)
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME, consumer_, direction_, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME, consumer_, direction_,
initValue_), lineName(lineName_) { initValue_),
} lineName(lineName_) {}
GpiodRegularByLineName(std::string lineName_, std::string consumer_) : GpiodRegularByLineName(std::string lineName_, std::string consumer_)
GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME, consumer_, : GpiodRegularBase(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME, consumer_, gpio::Direction::IN,
gpio::Direction::IN, gpio::LOW), lineName(lineName_) { gpio::LOW),
} lineName(lineName_) {}
std::string lineName; std::string lineName;
}; };
class GpioCallback: public GpioBase { class GpioCallback : public GpioBase {
public: public:
GpioCallback(std::string consumer, gpio::Direction direction_, gpio::Levels initValue_, GpioCallback(std::string consumer, gpio::Direction direction_, gpio::Levels initValue_,
gpio::gpio_cb_t callback, void* callbackArgs): gpio::gpio_cb_t callback, void* callbackArgs)
GpioBase(gpio::GpioTypes::CALLBACK, consumer, direction_, initValue_), : GpioBase(gpio::GpioTypes::CALLBACK, consumer, direction_, initValue_),
callback(callback), callbackArgs(callbackArgs) {} callback(callback),
callbackArgs(callbackArgs) {}
gpio::gpio_cb_t callback = nullptr; gpio::gpio_cb_t callback = nullptr;
void* callbackArgs = nullptr; void* callbackArgs = nullptr;
}; };
using GpioMap = std::map<gpioId_t, GpioBase*>; using GpioMap = std::map<gpioId_t, GpioBase*>;
using GpioUnorderedMap = std::unordered_map<gpioId_t, GpioBase*>; using GpioUnorderedMap = std::unordered_map<gpioId_t, GpioBase*>;
using GpioMapIter = GpioMap::iterator; using GpioMapIter = GpioMap::iterator;

View File

@ -5,12 +5,7 @@
namespace spi { namespace spi {
enum SpiModes: uint8_t { enum SpiModes : uint8_t { MODE_0, MODE_1, MODE_2, MODE_3 };
MODE_0,
MODE_1,
MODE_2,
MODE_3
};
} }

View File

@ -1,287 +1,274 @@
#include "GyroL3GD20Handler.h" #include "GyroL3GD20Handler.h"
#include "fsfw/datapool/PoolReadGuard.h"
#include <cmath> #include <cmath>
#include "fsfw/datapool/PoolReadGuard.h"
GyroHandlerL3GD20H::GyroHandlerL3GD20H(object_id_t objectId, object_id_t deviceCommunication, GyroHandlerL3GD20H::GyroHandlerL3GD20H(object_id_t objectId, object_id_t deviceCommunication,
CookieIF *comCookie, uint32_t transitionDelayMs): CookieIF *comCookie, uint32_t transitionDelayMs)
DeviceHandlerBase(objectId, deviceCommunication, comCookie), : DeviceHandlerBase(objectId, deviceCommunication, comCookie),
transitionDelayMs(transitionDelayMs), dataset(this) { transitionDelayMs(transitionDelayMs),
dataset(this) {
#if FSFW_HAL_L3GD20_GYRO_DEBUG == 1 #if FSFW_HAL_L3GD20_GYRO_DEBUG == 1
debugDivider = new PeriodicOperationDivider(3); debugDivider = new PeriodicOperationDivider(3);
#endif #endif
} }
GyroHandlerL3GD20H::~GyroHandlerL3GD20H() {} GyroHandlerL3GD20H::~GyroHandlerL3GD20H() {}
void GyroHandlerL3GD20H::doStartUp() { void GyroHandlerL3GD20H::doStartUp() {
if(internalState == InternalState::NONE) { if (internalState == InternalState::NONE) {
internalState = InternalState::CONFIGURE; internalState = InternalState::CONFIGURE;
} }
if(internalState == InternalState::CONFIGURE) { if (internalState == InternalState::CONFIGURE) {
if(commandExecuted) { if (commandExecuted) {
internalState = InternalState::CHECK_REGS; internalState = InternalState::CHECK_REGS;
commandExecuted = false; commandExecuted = false;
}
} }
}
if(internalState == InternalState::CHECK_REGS) { if (internalState == InternalState::CHECK_REGS) {
if(commandExecuted) { if (commandExecuted) {
internalState = InternalState::NORMAL; internalState = InternalState::NORMAL;
if(goNormalModeImmediately) { if (goNormalModeImmediately) {
setMode(MODE_NORMAL); setMode(MODE_NORMAL);
} } else {
else { setMode(_MODE_TO_ON);
setMode(_MODE_TO_ON); }
} commandExecuted = false;
commandExecuted = false;
}
} }
}
} }
void GyroHandlerL3GD20H::doShutDown() { void GyroHandlerL3GD20H::doShutDown() { setMode(_MODE_POWER_DOWN); }
setMode(_MODE_POWER_DOWN);
}
ReturnValue_t GyroHandlerL3GD20H::buildTransitionDeviceCommand(DeviceCommandId_t *id) { ReturnValue_t GyroHandlerL3GD20H::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
switch(internalState) { switch (internalState) {
case(InternalState::NONE): case (InternalState::NONE):
case(InternalState::NORMAL): { case (InternalState::NORMAL): {
return NOTHING_TO_SEND; return NOTHING_TO_SEND;
} }
case(InternalState::CONFIGURE): { case (InternalState::CONFIGURE): {
*id = L3GD20H::CONFIGURE_CTRL_REGS; *id = L3GD20H::CONFIGURE_CTRL_REGS;
uint8_t command [5]; uint8_t command[5];
command[0] = L3GD20H::CTRL_REG_1_VAL; command[0] = L3GD20H::CTRL_REG_1_VAL;
command[1] = L3GD20H::CTRL_REG_2_VAL; command[1] = L3GD20H::CTRL_REG_2_VAL;
command[2] = L3GD20H::CTRL_REG_3_VAL; command[2] = L3GD20H::CTRL_REG_3_VAL;
command[3] = L3GD20H::CTRL_REG_4_VAL; command[3] = L3GD20H::CTRL_REG_4_VAL;
command[4] = L3GD20H::CTRL_REG_5_VAL; command[4] = L3GD20H::CTRL_REG_5_VAL;
return buildCommandFromCommand(*id, command, 5); return buildCommandFromCommand(*id, command, 5);
} }
case(InternalState::CHECK_REGS): { case (InternalState::CHECK_REGS): {
*id = L3GD20H::READ_REGS; *id = L3GD20H::READ_REGS;
return buildCommandFromCommand(*id, nullptr, 0); return buildCommandFromCommand(*id, nullptr, 0);
} }
default: default:
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
/* Might be a configuration error. */ /* Might be a configuration error. */
sif::warning << "GyroL3GD20Handler::buildTransitionDeviceCommand: " sif::warning << "GyroL3GD20Handler::buildTransitionDeviceCommand: "
"Unknown internal state!" << std::endl; "Unknown internal state!"
<< std::endl;
#else #else
sif::printDebug("GyroL3GD20Handler::buildTransitionDeviceCommand: " sif::printDebug(
"Unknown internal state!\n"); "GyroL3GD20Handler::buildTransitionDeviceCommand: "
"Unknown internal state!\n");
#endif #endif
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroHandlerL3GD20H::buildNormalDeviceCommand(DeviceCommandId_t *id) { ReturnValue_t GyroHandlerL3GD20H::buildNormalDeviceCommand(DeviceCommandId_t *id) {
*id = L3GD20H::READ_REGS; *id = L3GD20H::READ_REGS;
return buildCommandFromCommand(*id, nullptr, 0); return buildCommandFromCommand(*id, nullptr, 0);
} }
ReturnValue_t GyroHandlerL3GD20H::buildCommandFromCommand( ReturnValue_t GyroHandlerL3GD20H::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
DeviceCommandId_t deviceCommand, const uint8_t *commandData, const uint8_t *commandData,
size_t commandDataLen) { size_t commandDataLen) {
switch(deviceCommand) { switch (deviceCommand) {
case(L3GD20H::READ_REGS): { case (L3GD20H::READ_REGS): {
commandBuffer[0] = L3GD20H::READ_START | L3GD20H::AUTO_INCREMENT_MASK | L3GD20H::READ_MASK; commandBuffer[0] = L3GD20H::READ_START | L3GD20H::AUTO_INCREMENT_MASK | L3GD20H::READ_MASK;
std::memset(commandBuffer + 1, 0, L3GD20H::READ_LEN); std::memset(commandBuffer + 1, 0, L3GD20H::READ_LEN);
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = L3GD20H::READ_LEN + 1; rawPacketLen = L3GD20H::READ_LEN + 1;
break; break;
} }
case(L3GD20H::CONFIGURE_CTRL_REGS): { case (L3GD20H::CONFIGURE_CTRL_REGS): {
commandBuffer[0] = L3GD20H::CTRL_REG_1 | L3GD20H::AUTO_INCREMENT_MASK; commandBuffer[0] = L3GD20H::CTRL_REG_1 | L3GD20H::AUTO_INCREMENT_MASK;
if(commandData == nullptr or commandDataLen != 5) { if (commandData == nullptr or commandDataLen != 5) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER; return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
} }
ctrlReg1Value = commandData[0]; ctrlReg1Value = commandData[0];
ctrlReg2Value = commandData[1]; ctrlReg2Value = commandData[1];
ctrlReg3Value = commandData[2]; ctrlReg3Value = commandData[2];
ctrlReg4Value = commandData[3]; ctrlReg4Value = commandData[3];
ctrlReg5Value = commandData[4]; ctrlReg5Value = commandData[4];
bool fsH = ctrlReg4Value & L3GD20H::SET_FS_1; bool fsH = ctrlReg4Value & L3GD20H::SET_FS_1;
bool fsL = ctrlReg4Value & L3GD20H::SET_FS_0; bool fsL = ctrlReg4Value & L3GD20H::SET_FS_0;
if(not fsH and not fsL) { if (not fsH and not fsL) {
sensitivity = L3GD20H::SENSITIVITY_00; sensitivity = L3GD20H::SENSITIVITY_00;
} } else if (not fsH and fsL) {
else if(not fsH and fsL) { sensitivity = L3GD20H::SENSITIVITY_01;
sensitivity = L3GD20H::SENSITIVITY_01; } else {
} sensitivity = L3GD20H::SENSITIVITY_11;
else { }
sensitivity = L3GD20H::SENSITIVITY_11;
}
commandBuffer[1] = ctrlReg1Value; commandBuffer[1] = ctrlReg1Value;
commandBuffer[2] = ctrlReg2Value; commandBuffer[2] = ctrlReg2Value;
commandBuffer[3] = ctrlReg3Value; commandBuffer[3] = ctrlReg3Value;
commandBuffer[4] = ctrlReg4Value; commandBuffer[4] = ctrlReg4Value;
commandBuffer[5] = ctrlReg5Value; commandBuffer[5] = ctrlReg5Value;
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = 6; rawPacketLen = 6;
break; break;
} }
case(L3GD20H::READ_CTRL_REGS): { case (L3GD20H::READ_CTRL_REGS): {
commandBuffer[0] = L3GD20H::READ_START | L3GD20H::AUTO_INCREMENT_MASK | commandBuffer[0] = L3GD20H::READ_START | L3GD20H::AUTO_INCREMENT_MASK | L3GD20H::READ_MASK;
L3GD20H::READ_MASK;
std::memset(commandBuffer + 1, 0, 5); std::memset(commandBuffer + 1, 0, 5);
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = 6; rawPacketLen = 6;
break; break;
} }
default: default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED; return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroHandlerL3GD20H::scanForReply(const uint8_t *start, size_t len, ReturnValue_t GyroHandlerL3GD20H::scanForReply(const uint8_t *start, size_t len,
DeviceCommandId_t *foundId, size_t *foundLen) { DeviceCommandId_t *foundId, size_t *foundLen) {
// For SPI, the ID will always be the one of the last sent command // For SPI, the ID will always be the one of the last sent command
*foundId = this->getPendingCommand(); *foundId = this->getPendingCommand();
*foundLen = this->rawPacketLen; *foundLen = this->rawPacketLen;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroHandlerL3GD20H::interpretDeviceReply(DeviceCommandId_t id, ReturnValue_t GyroHandlerL3GD20H::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) { const uint8_t *packet) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
switch(id) { switch (id) {
case(L3GD20H::CONFIGURE_CTRL_REGS): { case (L3GD20H::CONFIGURE_CTRL_REGS): {
commandExecuted = true;
break;
}
case (L3GD20H::READ_CTRL_REGS): {
if (packet[1] == ctrlReg1Value and packet[2] == ctrlReg2Value and
packet[3] == ctrlReg3Value and packet[4] == ctrlReg4Value and
packet[5] == ctrlReg5Value) {
commandExecuted = true; commandExecuted = true;
break; } else {
// Attempt reconfiguration
internalState = InternalState::CONFIGURE;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
} }
case(L3GD20H::READ_CTRL_REGS): { case (L3GD20H::READ_REGS): {
if(packet[1] == ctrlReg1Value and packet[2] == ctrlReg2Value and if (packet[1] != ctrlReg1Value and packet[2] != ctrlReg2Value and
packet[3] == ctrlReg3Value and packet[4] == ctrlReg4Value and packet[3] != ctrlReg3Value and packet[4] != ctrlReg4Value and
packet[5] == ctrlReg5Value) { packet[5] != ctrlReg5Value) {
commandExecuted = true; return DeviceHandlerIF::DEVICE_REPLY_INVALID;
} } else {
else { if (internalState == InternalState::CHECK_REGS) {
// Attempt reconfiguration commandExecuted = true;
internalState = InternalState::CONFIGURE;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
}
case(L3GD20H::READ_REGS): {
if(packet[1] != ctrlReg1Value and packet[2] != ctrlReg2Value and
packet[3] != ctrlReg3Value and packet[4] != ctrlReg4Value and
packet[5] != ctrlReg5Value) {
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
else {
if(internalState == InternalState::CHECK_REGS) {
commandExecuted = true;
}
} }
}
statusReg = packet[L3GD20H::STATUS_IDX]; statusReg = packet[L3GD20H::STATUS_IDX];
int16_t angVelocXRaw = packet[L3GD20H::OUT_X_H] << 8 | packet[L3GD20H::OUT_X_L]; int16_t angVelocXRaw = packet[L3GD20H::OUT_X_H] << 8 | packet[L3GD20H::OUT_X_L];
int16_t angVelocYRaw = packet[L3GD20H::OUT_Y_H] << 8 | packet[L3GD20H::OUT_Y_L]; int16_t angVelocYRaw = packet[L3GD20H::OUT_Y_H] << 8 | packet[L3GD20H::OUT_Y_L];
int16_t angVelocZRaw = packet[L3GD20H::OUT_Z_H] << 8 | packet[L3GD20H::OUT_Z_L]; int16_t angVelocZRaw = packet[L3GD20H::OUT_Z_H] << 8 | packet[L3GD20H::OUT_Z_L];
float angVelocX = angVelocXRaw * sensitivity; float angVelocX = angVelocXRaw * sensitivity;
float angVelocY = angVelocYRaw * sensitivity; float angVelocY = angVelocYRaw * sensitivity;
float angVelocZ = angVelocZRaw * sensitivity; float angVelocZ = angVelocZRaw * sensitivity;
int8_t temperaturOffset = (-1) * packet[L3GD20H::TEMPERATURE_IDX]; int8_t temperaturOffset = (-1) * packet[L3GD20H::TEMPERATURE_IDX];
float temperature = 25.0 + temperaturOffset; float temperature = 25.0 + temperaturOffset;
#if FSFW_HAL_L3GD20_GYRO_DEBUG == 1 #if FSFW_HAL_L3GD20_GYRO_DEBUG == 1
if(debugDivider->checkAndIncrement()) { if (debugDivider->checkAndIncrement()) {
/* Set terminal to utf-8 if there is an issue with micro printout. */ /* Set terminal to utf-8 if there is an issue with micro printout. */
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "GyroHandlerL3GD20H: Angular velocities (deg/s):" << std::endl; sif::info << "GyroHandlerL3GD20H: Angular velocities (deg/s):" << std::endl;
sif::info << "X: " << angVelocX << std::endl; sif::info << "X: " << angVelocX << std::endl;
sif::info << "Y: " << angVelocY << std::endl; sif::info << "Y: " << angVelocY << std::endl;
sif::info << "Z: " << angVelocZ << std::endl; sif::info << "Z: " << angVelocZ << std::endl;
#else #else
sif::printInfo("GyroHandlerL3GD20H: Angular velocities (deg/s):\n"); sif::printInfo("GyroHandlerL3GD20H: Angular velocities (deg/s):\n");
sif::printInfo("X: %f\n", angVelocX); sif::printInfo("X: %f\n", angVelocX);
sif::printInfo("Y: %f\n", angVelocY); sif::printInfo("Y: %f\n", angVelocY);
sif::printInfo("Z: %f\n", angVelocZ); sif::printInfo("Z: %f\n", angVelocZ);
#endif #endif
} }
#endif #endif
PoolReadGuard readSet(&dataset); PoolReadGuard readSet(&dataset);
if(readSet.getReadResult() == HasReturnvaluesIF::RETURN_OK) { if (readSet.getReadResult() == HasReturnvaluesIF::RETURN_OK) {
if(std::abs(angVelocX) < this->absLimitX) { if (std::abs(angVelocX) < this->absLimitX) {
dataset.angVelocX = angVelocX; dataset.angVelocX = angVelocX;
dataset.angVelocX.setValid(true); dataset.angVelocX.setValid(true);
} } else {
else { dataset.angVelocX.setValid(false);
dataset.angVelocX.setValid(false);
}
if(std::abs(angVelocY) < this->absLimitY) {
dataset.angVelocY = angVelocY;
dataset.angVelocY.setValid(true);
}
else {
dataset.angVelocY.setValid(false);
}
if(std::abs(angVelocZ) < this->absLimitZ) {
dataset.angVelocZ = angVelocZ;
dataset.angVelocZ.setValid(true);
}
else {
dataset.angVelocZ.setValid(false);
}
dataset.temperature = temperature;
dataset.temperature.setValid(true);
} }
break;
if (std::abs(angVelocY) < this->absLimitY) {
dataset.angVelocY = angVelocY;
dataset.angVelocY.setValid(true);
} else {
dataset.angVelocY.setValid(false);
}
if (std::abs(angVelocZ) < this->absLimitZ) {
dataset.angVelocZ = angVelocZ;
dataset.angVelocZ.setValid(true);
} else {
dataset.angVelocZ.setValid(false);
}
dataset.temperature = temperature;
dataset.temperature.setValid(true);
}
break;
} }
default: default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED; return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
} }
return result; return result;
} }
uint32_t GyroHandlerL3GD20H::getTransitionDelayMs(Mode_t from, Mode_t to) { uint32_t GyroHandlerL3GD20H::getTransitionDelayMs(Mode_t from, Mode_t to) {
return this->transitionDelayMs; return this->transitionDelayMs;
} }
void GyroHandlerL3GD20H::setToGoToNormalMode(bool enable) { void GyroHandlerL3GD20H::setToGoToNormalMode(bool enable) { this->goNormalModeImmediately = true; }
this->goNormalModeImmediately = true;
}
ReturnValue_t GyroHandlerL3GD20H::initializeLocalDataPool( ReturnValue_t GyroHandlerL3GD20H::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) { LocalDataPoolManager &poolManager) {
localDataPoolMap.emplace(L3GD20H::ANG_VELOC_X, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(L3GD20H::ANG_VELOC_X, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(L3GD20H::ANG_VELOC_Y, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(L3GD20H::ANG_VELOC_Y, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(L3GD20H::ANG_VELOC_Z, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(L3GD20H::ANG_VELOC_Z, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(L3GD20H::TEMPERATURE, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(L3GD20H::TEMPERATURE, new PoolEntry<float>({0.0}));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void GyroHandlerL3GD20H::fillCommandAndReplyMap() { void GyroHandlerL3GD20H::fillCommandAndReplyMap() {
insertInCommandAndReplyMap(L3GD20H::READ_REGS, 1, &dataset); insertInCommandAndReplyMap(L3GD20H::READ_REGS, 1, &dataset);
insertInCommandAndReplyMap(L3GD20H::CONFIGURE_CTRL_REGS, 1); insertInCommandAndReplyMap(L3GD20H::CONFIGURE_CTRL_REGS, 1);
insertInCommandAndReplyMap(L3GD20H::READ_CTRL_REGS, 1); insertInCommandAndReplyMap(L3GD20H::READ_CTRL_REGS, 1);
} }
void GyroHandlerL3GD20H::modeChanged() { void GyroHandlerL3GD20H::modeChanged() { internalState = InternalState::NONE; }
internalState = InternalState::NONE;
}
void GyroHandlerL3GD20H::setAbsoluteLimits(float limitX, float limitY, float limitZ) { void GyroHandlerL3GD20H::setAbsoluteLimits(float limitX, float limitY, float limitZ) {
this->absLimitX = limitX; this->absLimitX = limitX;
this->absLimitY = limitY; this->absLimitY = limitY;
this->absLimitZ = limitZ; this->absLimitZ = limitZ;
} }

View File

@ -1,12 +1,12 @@
#ifndef MISSION_DEVICES_GYROL3GD20HANDLER_H_ #ifndef MISSION_DEVICES_GYROL3GD20HANDLER_H_
#define MISSION_DEVICES_GYROL3GD20HANDLER_H_ #define MISSION_DEVICES_GYROL3GD20HANDLER_H_
#include "fsfw/FSFW.h"
#include "devicedefinitions/GyroL3GD20Definitions.h"
#include <fsfw/devicehandlers/DeviceHandlerBase.h> #include <fsfw/devicehandlers/DeviceHandlerBase.h>
#include <fsfw/globalfunctions/PeriodicOperationDivider.h> #include <fsfw/globalfunctions/PeriodicOperationDivider.h>
#include "devicedefinitions/GyroL3GD20Definitions.h"
#include "fsfw/FSFW.h"
/** /**
* @brief Device Handler for the L3GD20H gyroscope sensor * @brief Device Handler for the L3GD20H gyroscope sensor
* (https://www.st.com/en/mems-and-sensors/l3gd20h.html) * (https://www.st.com/en/mems-and-sensors/l3gd20h.html)
@ -16,84 +16,73 @@
* *
* Data is read big endian with the smallest possible range of 245 degrees per second. * Data is read big endian with the smallest possible range of 245 degrees per second.
*/ */
class GyroHandlerL3GD20H: public DeviceHandlerBase { class GyroHandlerL3GD20H : public DeviceHandlerBase {
public: public:
GyroHandlerL3GD20H(object_id_t objectId, object_id_t deviceCommunication, GyroHandlerL3GD20H(object_id_t objectId, object_id_t deviceCommunication, CookieIF *comCookie,
CookieIF* comCookie, uint32_t transitionDelayMs); uint32_t transitionDelayMs);
virtual ~GyroHandlerL3GD20H(); virtual ~GyroHandlerL3GD20H();
/** /**
* Set the absolute limit for the values on the axis in degrees per second. * Set the absolute limit for the values on the axis in degrees per second.
* The dataset values will be marked as invalid if that limit is exceeded * The dataset values will be marked as invalid if that limit is exceeded
* @param xLimit * @param xLimit
* @param yLimit * @param yLimit
* @param zLimit * @param zLimit
*/ */
void setAbsoluteLimits(float limitX, float limitY, float limitZ); void setAbsoluteLimits(float limitX, float limitY, float limitZ);
/** /**
* @brief Configure device handler to go to normal mode immediately * @brief Configure device handler to go to normal mode immediately
*/ */
void setToGoToNormalMode(bool enable); void setToGoToNormalMode(bool enable);
protected:
/* DeviceHandlerBase overrides */ protected:
ReturnValue_t buildTransitionDeviceCommand( /* DeviceHandlerBase overrides */
DeviceCommandId_t *id) override; ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override;
void doStartUp() override; void doStartUp() override;
void doShutDown() override; void doShutDown() override;
ReturnValue_t buildNormalDeviceCommand( ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
DeviceCommandId_t *id) override; ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData,
ReturnValue_t buildCommandFromCommand( size_t commandDataLen) override;
DeviceCommandId_t deviceCommand, const uint8_t *commandData, ReturnValue_t scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId,
size_t commandDataLen) override; size_t *foundLen) override;
ReturnValue_t scanForReply(const uint8_t *start, size_t len, virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override;
DeviceCommandId_t *foundId, size_t *foundLen) override;
virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) override;
void fillCommandAndReplyMap() override; void fillCommandAndReplyMap() override;
void modeChanged() override; void modeChanged() override;
virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to) override; virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to) override;
ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap, ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
LocalDataPoolManager &poolManager) override; LocalDataPoolManager &poolManager) override;
private: private:
uint32_t transitionDelayMs = 0; uint32_t transitionDelayMs = 0;
GyroPrimaryDataset dataset; GyroPrimaryDataset dataset;
float absLimitX = L3GD20H::RANGE_DPS_00; float absLimitX = L3GD20H::RANGE_DPS_00;
float absLimitY = L3GD20H::RANGE_DPS_00; float absLimitY = L3GD20H::RANGE_DPS_00;
float absLimitZ = L3GD20H::RANGE_DPS_00; float absLimitZ = L3GD20H::RANGE_DPS_00;
enum class InternalState { enum class InternalState { NONE, CONFIGURE, CHECK_REGS, NORMAL };
NONE, InternalState internalState = InternalState::NONE;
CONFIGURE, bool commandExecuted = false;
CHECK_REGS,
NORMAL
};
InternalState internalState = InternalState::NONE;
bool commandExecuted = false;
uint8_t statusReg = 0; uint8_t statusReg = 0;
bool goNormalModeImmediately = false; bool goNormalModeImmediately = false;
uint8_t ctrlReg1Value = L3GD20H::CTRL_REG_1_VAL; uint8_t ctrlReg1Value = L3GD20H::CTRL_REG_1_VAL;
uint8_t ctrlReg2Value = L3GD20H::CTRL_REG_2_VAL; uint8_t ctrlReg2Value = L3GD20H::CTRL_REG_2_VAL;
uint8_t ctrlReg3Value = L3GD20H::CTRL_REG_3_VAL; uint8_t ctrlReg3Value = L3GD20H::CTRL_REG_3_VAL;
uint8_t ctrlReg4Value = L3GD20H::CTRL_REG_4_VAL; uint8_t ctrlReg4Value = L3GD20H::CTRL_REG_4_VAL;
uint8_t ctrlReg5Value = L3GD20H::CTRL_REG_5_VAL; uint8_t ctrlReg5Value = L3GD20H::CTRL_REG_5_VAL;
uint8_t commandBuffer[L3GD20H::READ_LEN + 1]; uint8_t commandBuffer[L3GD20H::READ_LEN + 1];
// Set default value // Set default value
float sensitivity = L3GD20H::SENSITIVITY_00; float sensitivity = L3GD20H::SENSITIVITY_00;
#if FSFW_HAL_L3GD20_GYRO_DEBUG == 1 #if FSFW_HAL_L3GD20_GYRO_DEBUG == 1
PeriodicOperationDivider* debugDivider = nullptr; PeriodicOperationDivider *debugDivider = nullptr;
#endif #endif
}; };
#endif /* MISSION_DEVICES_GYROL3GD20HANDLER_H_ */ #endif /* MISSION_DEVICES_GYROL3GD20HANDLER_H_ */

View File

@ -8,513 +8,477 @@
#include <cmath> #include <cmath>
MgmLIS3MDLHandler::MgmLIS3MDLHandler(object_id_t objectId, object_id_t deviceCommunication, MgmLIS3MDLHandler::MgmLIS3MDLHandler(object_id_t objectId, object_id_t deviceCommunication,
CookieIF* comCookie, uint32_t transitionDelay): CookieIF *comCookie, uint32_t transitionDelay)
DeviceHandlerBase(objectId, deviceCommunication, comCookie), : DeviceHandlerBase(objectId, deviceCommunication, comCookie),
dataset(this), transitionDelay(transitionDelay) { dataset(this),
transitionDelay(transitionDelay) {
#if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1 #if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1
debugDivider = new PeriodicOperationDivider(3); debugDivider = new PeriodicOperationDivider(3);
#endif #endif
// Set to default values right away // Set to default values right away
registers[0] = MGMLIS3MDL::CTRL_REG1_DEFAULT; registers[0] = MGMLIS3MDL::CTRL_REG1_DEFAULT;
registers[1] = MGMLIS3MDL::CTRL_REG2_DEFAULT; registers[1] = MGMLIS3MDL::CTRL_REG2_DEFAULT;
registers[2] = MGMLIS3MDL::CTRL_REG3_DEFAULT; registers[2] = MGMLIS3MDL::CTRL_REG3_DEFAULT;
registers[3] = MGMLIS3MDL::CTRL_REG4_DEFAULT; registers[3] = MGMLIS3MDL::CTRL_REG4_DEFAULT;
registers[4] = MGMLIS3MDL::CTRL_REG5_DEFAULT; registers[4] = MGMLIS3MDL::CTRL_REG5_DEFAULT;
}
MgmLIS3MDLHandler::~MgmLIS3MDLHandler() {
} }
MgmLIS3MDLHandler::~MgmLIS3MDLHandler() {}
void MgmLIS3MDLHandler::doStartUp() { void MgmLIS3MDLHandler::doStartUp() {
switch (internalState) { switch (internalState) {
case(InternalState::STATE_NONE): { case (InternalState::STATE_NONE): {
internalState = InternalState::STATE_FIRST_CONTACT; internalState = InternalState::STATE_FIRST_CONTACT;
break; break;
} }
case(InternalState::STATE_FIRST_CONTACT): { case (InternalState::STATE_FIRST_CONTACT): {
/* Will be set by checking device ID (WHO AM I register) */ /* Will be set by checking device ID (WHO AM I register) */
if(commandExecuted) { if (commandExecuted) {
commandExecuted = false; commandExecuted = false;
internalState = InternalState::STATE_SETUP; internalState = InternalState::STATE_SETUP;
}
break;
}
case (InternalState::STATE_SETUP): {
internalState = InternalState::STATE_CHECK_REGISTERS;
break;
}
case (InternalState::STATE_CHECK_REGISTERS): {
/* Set up cached registers which will be used to configure the MGM. */
if (commandExecuted) {
commandExecuted = false;
if (goToNormalMode) {
setMode(MODE_NORMAL);
} else {
setMode(_MODE_TO_ON);
} }
break; }
} break;
case(InternalState::STATE_SETUP): {
internalState = InternalState::STATE_CHECK_REGISTERS;
break;
}
case(InternalState::STATE_CHECK_REGISTERS): {
/* Set up cached registers which will be used to configure the MGM. */
if(commandExecuted) {
commandExecuted = false;
if(goToNormalMode) {
setMode(MODE_NORMAL);
}
else {
setMode(_MODE_TO_ON);
}
}
break;
} }
default: default:
break; break;
} }
} }
void MgmLIS3MDLHandler::doShutDown() { void MgmLIS3MDLHandler::doShutDown() { setMode(_MODE_POWER_DOWN); }
setMode(_MODE_POWER_DOWN);
}
ReturnValue_t MgmLIS3MDLHandler::buildTransitionDeviceCommand( ReturnValue_t MgmLIS3MDLHandler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
DeviceCommandId_t *id) { switch (internalState) {
switch (internalState) { case (InternalState::STATE_NONE):
case(InternalState::STATE_NONE): case (InternalState::STATE_NORMAL): {
case(InternalState::STATE_NORMAL): { return DeviceHandlerBase::NOTHING_TO_SEND;
return DeviceHandlerBase::NOTHING_TO_SEND;
} }
case(InternalState::STATE_FIRST_CONTACT): { case (InternalState::STATE_FIRST_CONTACT): {
*id = MGMLIS3MDL::IDENTIFY_DEVICE; *id = MGMLIS3MDL::IDENTIFY_DEVICE;
break; break;
} }
case(InternalState::STATE_SETUP): { case (InternalState::STATE_SETUP): {
*id = MGMLIS3MDL::SETUP_MGM; *id = MGMLIS3MDL::SETUP_MGM;
break; break;
} }
case(InternalState::STATE_CHECK_REGISTERS): { case (InternalState::STATE_CHECK_REGISTERS): {
*id = MGMLIS3MDL::READ_CONFIG_AND_DATA; *id = MGMLIS3MDL::READ_CONFIG_AND_DATA;
break; break;
} }
default: { default: {
/* might be a configuration error. */ /* might be a configuration error. */
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "GyroHandler::buildTransitionDeviceCommand: Unknown internal state!" << sif::warning << "GyroHandler::buildTransitionDeviceCommand: Unknown internal state!"
std::endl; << std::endl;
#else #else
sif::printWarning("GyroHandler::buildTransitionDeviceCommand: Unknown internal state!\n"); sif::printWarning("GyroHandler::buildTransitionDeviceCommand: Unknown internal state!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
}
} return buildCommandFromCommand(*id, NULL, 0);
return buildCommandFromCommand(*id, NULL, 0);
} }
uint8_t MgmLIS3MDLHandler::readCommand(uint8_t command, bool continuousCom) { uint8_t MgmLIS3MDLHandler::readCommand(uint8_t command, bool continuousCom) {
command |= (1 << MGMLIS3MDL::RW_BIT); command |= (1 << MGMLIS3MDL::RW_BIT);
if (continuousCom == true) { if (continuousCom == true) {
command |= (1 << MGMLIS3MDL::MS_BIT); command |= (1 << MGMLIS3MDL::MS_BIT);
} }
return command; return command;
} }
uint8_t MgmLIS3MDLHandler::writeCommand(uint8_t command, bool continuousCom) { uint8_t MgmLIS3MDLHandler::writeCommand(uint8_t command, bool continuousCom) {
command &= ~(1 << MGMLIS3MDL::RW_BIT); command &= ~(1 << MGMLIS3MDL::RW_BIT);
if (continuousCom == true) { if (continuousCom == true) {
command |= (1 << MGMLIS3MDL::MS_BIT); command |= (1 << MGMLIS3MDL::MS_BIT);
} }
return command; return command;
} }
void MgmLIS3MDLHandler::setupMgm() { void MgmLIS3MDLHandler::setupMgm() {
registers[0] = MGMLIS3MDL::CTRL_REG1_DEFAULT;
registers[1] = MGMLIS3MDL::CTRL_REG2_DEFAULT;
registers[2] = MGMLIS3MDL::CTRL_REG3_DEFAULT;
registers[3] = MGMLIS3MDL::CTRL_REG4_DEFAULT;
registers[4] = MGMLIS3MDL::CTRL_REG5_DEFAULT;
registers[0] = MGMLIS3MDL::CTRL_REG1_DEFAULT; prepareCtrlRegisterWrite();
registers[1] = MGMLIS3MDL::CTRL_REG2_DEFAULT;
registers[2] = MGMLIS3MDL::CTRL_REG3_DEFAULT;
registers[3] = MGMLIS3MDL::CTRL_REG4_DEFAULT;
registers[4] = MGMLIS3MDL::CTRL_REG5_DEFAULT;
prepareCtrlRegisterWrite();
} }
ReturnValue_t MgmLIS3MDLHandler::buildNormalDeviceCommand( ReturnValue_t MgmLIS3MDLHandler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
DeviceCommandId_t *id) { // Data/config register will be read in an alternating manner.
// Data/config register will be read in an alternating manner. if (communicationStep == CommunicationStep::DATA) {
if(communicationStep == CommunicationStep::DATA) { *id = MGMLIS3MDL::READ_CONFIG_AND_DATA;
*id = MGMLIS3MDL::READ_CONFIG_AND_DATA; communicationStep = CommunicationStep::TEMPERATURE;
communicationStep = CommunicationStep::TEMPERATURE; return buildCommandFromCommand(*id, NULL, 0);
return buildCommandFromCommand(*id, NULL, 0); } else {
} *id = MGMLIS3MDL::READ_TEMPERATURE;
else { communicationStep = CommunicationStep::DATA;
*id = MGMLIS3MDL::READ_TEMPERATURE; return buildCommandFromCommand(*id, NULL, 0);
communicationStep = CommunicationStep::DATA; }
return buildCommandFromCommand(*id, NULL, 0);
}
} }
ReturnValue_t MgmLIS3MDLHandler::buildCommandFromCommand( ReturnValue_t MgmLIS3MDLHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
DeviceCommandId_t deviceCommand, const uint8_t *commandData, const uint8_t *commandData,
size_t commandDataLen) { size_t commandDataLen) {
switch(deviceCommand) { switch (deviceCommand) {
case(MGMLIS3MDL::READ_CONFIG_AND_DATA): { case (MGMLIS3MDL::READ_CONFIG_AND_DATA): {
std::memset(commandBuffer, 0, sizeof(commandBuffer)); std::memset(commandBuffer, 0, sizeof(commandBuffer));
commandBuffer[0] = readCommand(MGMLIS3MDL::CTRL_REG1, true); commandBuffer[0] = readCommand(MGMLIS3MDL::CTRL_REG1, true);
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1; rawPacketLen = MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1;
return RETURN_OK; return RETURN_OK;
} }
case(MGMLIS3MDL::READ_TEMPERATURE): { case (MGMLIS3MDL::READ_TEMPERATURE): {
std::memset(commandBuffer, 0, 3); std::memset(commandBuffer, 0, 3);
commandBuffer[0] = readCommand(MGMLIS3MDL::TEMP_LOWBYTE, true); commandBuffer[0] = readCommand(MGMLIS3MDL::TEMP_LOWBYTE, true);
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = 3; rawPacketLen = 3;
return RETURN_OK; return RETURN_OK;
} }
case(MGMLIS3MDL::IDENTIFY_DEVICE): { case (MGMLIS3MDL::IDENTIFY_DEVICE): {
return identifyDevice(); return identifyDevice();
} }
case(MGMLIS3MDL::TEMP_SENSOR_ENABLE): { case (MGMLIS3MDL::TEMP_SENSOR_ENABLE): {
return enableTemperatureSensor(commandData, commandDataLen); return enableTemperatureSensor(commandData, commandDataLen);
} }
case(MGMLIS3MDL::SETUP_MGM): { case (MGMLIS3MDL::SETUP_MGM): {
setupMgm(); setupMgm();
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
case(MGMLIS3MDL::ACCURACY_OP_MODE_SET): { case (MGMLIS3MDL::ACCURACY_OP_MODE_SET): {
return setOperatingMode(commandData, commandDataLen); return setOperatingMode(commandData, commandDataLen);
} }
default: default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED; return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
} }
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
ReturnValue_t MgmLIS3MDLHandler::identifyDevice() { ReturnValue_t MgmLIS3MDLHandler::identifyDevice() {
uint32_t size = 2; uint32_t size = 2;
commandBuffer[0] = readCommand(MGMLIS3MDL::IDENTIFY_DEVICE_REG_ADDR); commandBuffer[0] = readCommand(MGMLIS3MDL::IDENTIFY_DEVICE_REG_ADDR);
commandBuffer[1] = 0x00; commandBuffer[1] = 0x00;
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = size; rawPacketLen = size;
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t MgmLIS3MDLHandler::scanForReply(const uint8_t *start, ReturnValue_t MgmLIS3MDLHandler::scanForReply(const uint8_t *start, size_t len,
size_t len, DeviceCommandId_t *foundId, size_t *foundLen) { DeviceCommandId_t *foundId, size_t *foundLen) {
*foundLen = len;
if (len == MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1) {
*foundLen = len; *foundLen = len;
if (len == MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1) { *foundId = MGMLIS3MDL::READ_CONFIG_AND_DATA;
*foundLen = len; // Check validity by checking config registers
*foundId = MGMLIS3MDL::READ_CONFIG_AND_DATA; if (start[1] != registers[0] or start[2] != registers[1] or start[3] != registers[2] or
// Check validity by checking config registers start[4] != registers[3] or start[5] != registers[4]) {
if (start[1] != registers[0] or start[2] != registers[1] or
start[3] != registers[2] or start[4] != registers[3] or
start[5] != registers[4]) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "MGMHandlerLIS3MDL::scanForReply: Invalid registers!" << std::endl; sif::warning << "MGMHandlerLIS3MDL::scanForReply: Invalid registers!" << std::endl;
#else #else
sif::printWarning("MGMHandlerLIS3MDL::scanForReply: Invalid registers!\n"); sif::printWarning("MGMHandlerLIS3MDL::scanForReply: Invalid registers!\n");
#endif #endif
#endif #endif
return DeviceHandlerIF::INVALID_DATA; return DeviceHandlerIF::INVALID_DATA;
} }
if(mode == _MODE_START_UP) { if (mode == _MODE_START_UP) {
commandExecuted = true; commandExecuted = true;
} }
} } else if (len == MGMLIS3MDL::TEMPERATURE_REPLY_LEN) {
else if(len == MGMLIS3MDL::TEMPERATURE_REPLY_LEN) { *foundLen = len;
*foundLen = len; *foundId = MGMLIS3MDL::READ_TEMPERATURE;
*foundId = MGMLIS3MDL::READ_TEMPERATURE; } else if (len == MGMLIS3MDL::SETUP_REPLY_LEN) {
} *foundLen = len;
else if (len == MGMLIS3MDL::SETUP_REPLY_LEN) { *foundId = MGMLIS3MDL::SETUP_MGM;
*foundLen = len; } else if (len == SINGLE_COMMAND_ANSWER_LEN) {
*foundId = MGMLIS3MDL::SETUP_MGM; *foundLen = len;
} *foundId = getPendingCommand();
else if (len == SINGLE_COMMAND_ANSWER_LEN) { if (*foundId == MGMLIS3MDL::IDENTIFY_DEVICE) {
*foundLen = len; if (start[1] != MGMLIS3MDL::DEVICE_ID) {
*foundId = getPendingCommand();
if(*foundId == MGMLIS3MDL::IDENTIFY_DEVICE) {
if(start[1] != MGMLIS3MDL::DEVICE_ID) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "MGMHandlerLIS3MDL::scanForReply: " sif::warning << "MGMHandlerLIS3MDL::scanForReply: "
"Device identification failed!" << std::endl; "Device identification failed!"
<< std::endl;
#else #else
sif::printWarning("MGMHandlerLIS3MDL::scanForReply: " sif::printWarning(
"Device identification failed!\n"); "MGMHandlerLIS3MDL::scanForReply: "
"Device identification failed!\n");
#endif #endif
#endif #endif
return DeviceHandlerIF::INVALID_DATA;
}
if(mode == _MODE_START_UP) {
commandExecuted = true;
}
}
}
else {
return DeviceHandlerIF::INVALID_DATA; return DeviceHandlerIF::INVALID_DATA;
} }
/* Data with SPI Interface always has this answer */ if (mode == _MODE_START_UP) {
if (start[0] == 0b11111111) { commandExecuted = true;
return RETURN_OK; }
}
else {
return DeviceHandlerIF::INVALID_DATA;
} }
} else {
return DeviceHandlerIF::INVALID_DATA;
}
/* Data with SPI Interface always has this answer */
if (start[0] == 0b11111111) {
return RETURN_OK;
} else {
return DeviceHandlerIF::INVALID_DATA;
}
} }
ReturnValue_t MgmLIS3MDLHandler::interpretDeviceReply(DeviceCommandId_t id, ReturnValue_t MgmLIS3MDLHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) {
const uint8_t *packet) { switch (id) {
switch (id) {
case MGMLIS3MDL::IDENTIFY_DEVICE: { case MGMLIS3MDL::IDENTIFY_DEVICE: {
break; break;
} }
case MGMLIS3MDL::SETUP_MGM: { case MGMLIS3MDL::SETUP_MGM: {
break; break;
} }
case MGMLIS3MDL::READ_CONFIG_AND_DATA: { case MGMLIS3MDL::READ_CONFIG_AND_DATA: {
// TODO: Store configuration in new local datasets. // TODO: Store configuration in new local datasets.
float sensitivityFactor = getSensitivityFactor(getSensitivity(registers[2])); float sensitivityFactor = getSensitivityFactor(getSensitivity(registers[2]));
int16_t mgmMeasurementRawX = packet[MGMLIS3MDL::X_HIGHBYTE_IDX] << 8 int16_t mgmMeasurementRawX =
| packet[MGMLIS3MDL::X_LOWBYTE_IDX] ; packet[MGMLIS3MDL::X_HIGHBYTE_IDX] << 8 | packet[MGMLIS3MDL::X_LOWBYTE_IDX];
int16_t mgmMeasurementRawY = packet[MGMLIS3MDL::Y_HIGHBYTE_IDX] << 8 int16_t mgmMeasurementRawY =
| packet[MGMLIS3MDL::Y_LOWBYTE_IDX] ; packet[MGMLIS3MDL::Y_HIGHBYTE_IDX] << 8 | packet[MGMLIS3MDL::Y_LOWBYTE_IDX];
int16_t mgmMeasurementRawZ = packet[MGMLIS3MDL::Z_HIGHBYTE_IDX] << 8 int16_t mgmMeasurementRawZ =
| packet[MGMLIS3MDL::Z_LOWBYTE_IDX] ; packet[MGMLIS3MDL::Z_HIGHBYTE_IDX] << 8 | packet[MGMLIS3MDL::Z_LOWBYTE_IDX];
/* Target value in microtesla */ /* Target value in microtesla */
float mgmX = static_cast<float>(mgmMeasurementRawX) * sensitivityFactor float mgmX = static_cast<float>(mgmMeasurementRawX) * sensitivityFactor *
* MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR; MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR;
float mgmY = static_cast<float>(mgmMeasurementRawY) * sensitivityFactor float mgmY = static_cast<float>(mgmMeasurementRawY) * sensitivityFactor *
* MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR; MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR;
float mgmZ = static_cast<float>(mgmMeasurementRawZ) * sensitivityFactor float mgmZ = static_cast<float>(mgmMeasurementRawZ) * sensitivityFactor *
* MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR; MGMLIS3MDL::GAUSS_TO_MICROTESLA_FACTOR;
#if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1 #if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1
if(debugDivider->checkAndIncrement()) { if (debugDivider->checkAndIncrement()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "MGMHandlerLIS3: Magnetic field strength in" sif::info << "MGMHandlerLIS3: Magnetic field strength in"
" microtesla:" << std::endl; " microtesla:"
sif::info << "X: " << mgmX << " uT" << std::endl; << std::endl;
sif::info << "Y: " << mgmY << " uT" << std::endl; sif::info << "X: " << mgmX << " uT" << std::endl;
sif::info << "Z: " << mgmZ << " uT" << std::endl; sif::info << "Y: " << mgmY << " uT" << std::endl;
sif::info << "Z: " << mgmZ << " uT" << std::endl;
#else #else
sif::printInfo("MGMHandlerLIS3: Magnetic field strength in microtesla:\n"); sif::printInfo("MGMHandlerLIS3: Magnetic field strength in microtesla:\n");
sif::printInfo("X: %f uT\n", mgmX); sif::printInfo("X: %f uT\n", mgmX);
sif::printInfo("Y: %f uT\n", mgmY); sif::printInfo("Y: %f uT\n", mgmY);
sif::printInfo("Z: %f uT\n", mgmZ); sif::printInfo("Z: %f uT\n", mgmZ);
#endif /* FSFW_CPP_OSTREAM_ENABLED == 0 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 0 */
} }
#endif /* OBSW_VERBOSE_LEVEL >= 1 */ #endif /* OBSW_VERBOSE_LEVEL >= 1 */
PoolReadGuard readHelper(&dataset); PoolReadGuard readHelper(&dataset);
if(readHelper.getReadResult() == HasReturnvaluesIF::RETURN_OK) { if (readHelper.getReadResult() == HasReturnvaluesIF::RETURN_OK) {
if(std::abs(mgmX) < absLimitX) { if (std::abs(mgmX) < absLimitX) {
dataset.fieldStrengthX = mgmX; dataset.fieldStrengthX = mgmX;
dataset.fieldStrengthX.setValid(true); dataset.fieldStrengthX.setValid(true);
} } else {
else { dataset.fieldStrengthX.setValid(false);
dataset.fieldStrengthX.setValid(false);
}
if(std::abs(mgmY) < absLimitY) {
dataset.fieldStrengthY = mgmY;
dataset.fieldStrengthY.setValid(true);
}
else {
dataset.fieldStrengthY.setValid(false);
}
if(std::abs(mgmZ) < absLimitZ) {
dataset.fieldStrengthZ = mgmZ;
dataset.fieldStrengthZ.setValid(true);
}
else {
dataset.fieldStrengthZ.setValid(false);
}
} }
break;
if (std::abs(mgmY) < absLimitY) {
dataset.fieldStrengthY = mgmY;
dataset.fieldStrengthY.setValid(true);
} else {
dataset.fieldStrengthY.setValid(false);
}
if (std::abs(mgmZ) < absLimitZ) {
dataset.fieldStrengthZ = mgmZ;
dataset.fieldStrengthZ.setValid(true);
} else {
dataset.fieldStrengthZ.setValid(false);
}
}
break;
} }
case MGMLIS3MDL::READ_TEMPERATURE: { case MGMLIS3MDL::READ_TEMPERATURE: {
int16_t tempValueRaw = packet[2] << 8 | packet[1]; int16_t tempValueRaw = packet[2] << 8 | packet[1];
float tempValue = 25.0 + ((static_cast<float>(tempValueRaw)) / 8.0); float tempValue = 25.0 + ((static_cast<float>(tempValueRaw)) / 8.0);
#if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1 #if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1
if(debugDivider->check()) { if (debugDivider->check()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "MGMHandlerLIS3: Temperature: " << tempValue << " C" << sif::info << "MGMHandlerLIS3: Temperature: " << tempValue << " C" << std::endl;
std::endl;
#else #else
sif::printInfo("MGMHandlerLIS3: Temperature: %f C\n"); sif::printInfo("MGMHandlerLIS3: Temperature: %f C\n");
#endif #endif
} }
#endif #endif
ReturnValue_t result = dataset.read(); ReturnValue_t result = dataset.read();
if(result == HasReturnvaluesIF::RETURN_OK) { if (result == HasReturnvaluesIF::RETURN_OK) {
dataset.temperature = tempValue; dataset.temperature = tempValue;
dataset.commit(); dataset.commit();
} }
break; break;
} }
default: { default: {
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY; return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
} }
}
} return RETURN_OK;
return RETURN_OK;
} }
MGMLIS3MDL::Sensitivies MgmLIS3MDLHandler::getSensitivity(uint8_t ctrlRegister2) { MGMLIS3MDL::Sensitivies MgmLIS3MDLHandler::getSensitivity(uint8_t ctrlRegister2) {
bool fs0Set = ctrlRegister2 & (1 << MGMLIS3MDL::FSO); // Checks if FS0 bit is set bool fs0Set = ctrlRegister2 & (1 << MGMLIS3MDL::FSO); // Checks if FS0 bit is set
bool fs1Set = ctrlRegister2 & (1 << MGMLIS3MDL::FS1); // Checks if FS1 bit is set bool fs1Set = ctrlRegister2 & (1 << MGMLIS3MDL::FS1); // Checks if FS1 bit is set
if (fs0Set && fs1Set) if (fs0Set && fs1Set)
return MGMLIS3MDL::Sensitivies::GAUSS_16; return MGMLIS3MDL::Sensitivies::GAUSS_16;
else if (!fs0Set && fs1Set) else if (!fs0Set && fs1Set)
return MGMLIS3MDL::Sensitivies::GAUSS_12; return MGMLIS3MDL::Sensitivies::GAUSS_12;
else if (fs0Set && !fs1Set) else if (fs0Set && !fs1Set)
return MGMLIS3MDL::Sensitivies::GAUSS_8; return MGMLIS3MDL::Sensitivies::GAUSS_8;
else else
return MGMLIS3MDL::Sensitivies::GAUSS_4; return MGMLIS3MDL::Sensitivies::GAUSS_4;
} }
float MgmLIS3MDLHandler::getSensitivityFactor(MGMLIS3MDL::Sensitivies sens) { float MgmLIS3MDLHandler::getSensitivityFactor(MGMLIS3MDL::Sensitivies sens) {
switch(sens) { switch (sens) {
case(MGMLIS3MDL::GAUSS_4): { case (MGMLIS3MDL::GAUSS_4): {
return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_4_SENS; return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_4_SENS;
} }
case(MGMLIS3MDL::GAUSS_8): { case (MGMLIS3MDL::GAUSS_8): {
return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_8_SENS; return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_8_SENS;
} }
case(MGMLIS3MDL::GAUSS_12): { case (MGMLIS3MDL::GAUSS_12): {
return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_12_SENS; return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_12_SENS;
} }
case(MGMLIS3MDL::GAUSS_16): { case (MGMLIS3MDL::GAUSS_16): {
return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_16_SENS; return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_16_SENS;
} }
default: { default: {
// Should never happen // Should never happen
return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_4_SENS; return MGMLIS3MDL::FIELD_LSB_PER_GAUSS_4_SENS;
}
} }
}
} }
ReturnValue_t MgmLIS3MDLHandler::enableTemperatureSensor(const uint8_t *commandData,
ReturnValue_t MgmLIS3MDLHandler::enableTemperatureSensor( size_t commandDataLen) {
const uint8_t *commandData, size_t commandDataLen) { triggerEvent(CHANGE_OF_SETUP_PARAMETER);
triggerEvent(CHANGE_OF_SETUP_PARAMETER); uint32_t size = 2;
uint32_t size = 2; commandBuffer[0] = writeCommand(MGMLIS3MDL::CTRL_REG1);
commandBuffer[0] = writeCommand(MGMLIS3MDL::CTRL_REG1); if (commandDataLen > 1) {
if (commandDataLen > 1) { return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS; }
} switch (*commandData) {
switch (*commandData) {
case (MGMLIS3MDL::ON): { case (MGMLIS3MDL::ON): {
commandBuffer[1] = registers[0] | (1 << 7); commandBuffer[1] = registers[0] | (1 << 7);
break; break;
} }
case (MGMLIS3MDL::OFF): { case (MGMLIS3MDL::OFF): {
commandBuffer[1] = registers[0] & ~(1 << 7); commandBuffer[1] = registers[0] & ~(1 << 7);
break; break;
} }
default: default:
return INVALID_COMMAND_PARAMETER; return INVALID_COMMAND_PARAMETER;
} }
registers[0] = commandBuffer[1]; registers[0] = commandBuffer[1];
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = size; rawPacketLen = size;
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t MgmLIS3MDLHandler::setOperatingMode(const uint8_t *commandData, ReturnValue_t MgmLIS3MDLHandler::setOperatingMode(const uint8_t *commandData,
size_t commandDataLen) { size_t commandDataLen) {
triggerEvent(CHANGE_OF_SETUP_PARAMETER); triggerEvent(CHANGE_OF_SETUP_PARAMETER);
if (commandDataLen != 1) { if (commandDataLen != 1) {
return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS; return INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
} }
switch (commandData[0]) { switch (commandData[0]) {
case MGMLIS3MDL::LOW: case MGMLIS3MDL::LOW:
registers[0] = (registers[0] & (~(1 << MGMLIS3MDL::OM1))) & (~(1 << MGMLIS3MDL::OM0)); registers[0] = (registers[0] & (~(1 << MGMLIS3MDL::OM1))) & (~(1 << MGMLIS3MDL::OM0));
registers[3] = (registers[3] & (~(1 << MGMLIS3MDL::OMZ1))) & (~(1 << MGMLIS3MDL::OMZ0)); registers[3] = (registers[3] & (~(1 << MGMLIS3MDL::OMZ1))) & (~(1 << MGMLIS3MDL::OMZ0));
break; break;
case MGMLIS3MDL::MEDIUM: case MGMLIS3MDL::MEDIUM:
registers[0] = (registers[0] & (~(1 << MGMLIS3MDL::OM1))) | (1 << MGMLIS3MDL::OM0); registers[0] = (registers[0] & (~(1 << MGMLIS3MDL::OM1))) | (1 << MGMLIS3MDL::OM0);
registers[3] = (registers[3] & (~(1 << MGMLIS3MDL::OMZ1))) | (1 << MGMLIS3MDL::OMZ0); registers[3] = (registers[3] & (~(1 << MGMLIS3MDL::OMZ1))) | (1 << MGMLIS3MDL::OMZ0);
break; break;
case MGMLIS3MDL::HIGH: case MGMLIS3MDL::HIGH:
registers[0] = (registers[0] | (1 << MGMLIS3MDL::OM1)) & (~(1 << MGMLIS3MDL::OM0)); registers[0] = (registers[0] | (1 << MGMLIS3MDL::OM1)) & (~(1 << MGMLIS3MDL::OM0));
registers[3] = (registers[3] | (1 << MGMLIS3MDL::OMZ1)) & (~(1 << MGMLIS3MDL::OMZ0)); registers[3] = (registers[3] | (1 << MGMLIS3MDL::OMZ1)) & (~(1 << MGMLIS3MDL::OMZ0));
break; break;
case MGMLIS3MDL::ULTRA: case MGMLIS3MDL::ULTRA:
registers[0] = (registers[0] | (1 << MGMLIS3MDL::OM1)) | (1 << MGMLIS3MDL::OM0); registers[0] = (registers[0] | (1 << MGMLIS3MDL::OM1)) | (1 << MGMLIS3MDL::OM0);
registers[3] = (registers[3] | (1 << MGMLIS3MDL::OMZ1)) | (1 << MGMLIS3MDL::OMZ0); registers[3] = (registers[3] | (1 << MGMLIS3MDL::OMZ1)) | (1 << MGMLIS3MDL::OMZ0);
break; break;
default: default:
break; break;
} }
return prepareCtrlRegisterWrite(); return prepareCtrlRegisterWrite();
} }
void MgmLIS3MDLHandler::fillCommandAndReplyMap() { void MgmLIS3MDLHandler::fillCommandAndReplyMap() {
insertInCommandAndReplyMap(MGMLIS3MDL::READ_CONFIG_AND_DATA, 1, &dataset); insertInCommandAndReplyMap(MGMLIS3MDL::READ_CONFIG_AND_DATA, 1, &dataset);
insertInCommandAndReplyMap(MGMLIS3MDL::READ_TEMPERATURE, 1); insertInCommandAndReplyMap(MGMLIS3MDL::READ_TEMPERATURE, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::SETUP_MGM, 1); insertInCommandAndReplyMap(MGMLIS3MDL::SETUP_MGM, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::IDENTIFY_DEVICE, 1); insertInCommandAndReplyMap(MGMLIS3MDL::IDENTIFY_DEVICE, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::TEMP_SENSOR_ENABLE, 1); insertInCommandAndReplyMap(MGMLIS3MDL::TEMP_SENSOR_ENABLE, 1);
insertInCommandAndReplyMap(MGMLIS3MDL::ACCURACY_OP_MODE_SET, 1); insertInCommandAndReplyMap(MGMLIS3MDL::ACCURACY_OP_MODE_SET, 1);
} }
void MgmLIS3MDLHandler::setToGoToNormalMode(bool enable) { void MgmLIS3MDLHandler::setToGoToNormalMode(bool enable) { this->goToNormalMode = enable; }
this->goToNormalMode = enable;
}
ReturnValue_t MgmLIS3MDLHandler::prepareCtrlRegisterWrite() { ReturnValue_t MgmLIS3MDLHandler::prepareCtrlRegisterWrite() {
commandBuffer[0] = writeCommand(MGMLIS3MDL::CTRL_REG1, true); commandBuffer[0] = writeCommand(MGMLIS3MDL::CTRL_REG1, true);
for (size_t i = 0; i < MGMLIS3MDL::NR_OF_CTRL_REGISTERS; i++) { for (size_t i = 0; i < MGMLIS3MDL::NR_OF_CTRL_REGISTERS; i++) {
commandBuffer[i + 1] = registers[i]; commandBuffer[i + 1] = registers[i];
} }
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = MGMLIS3MDL::NR_OF_CTRL_REGISTERS + 1; rawPacketLen = MGMLIS3MDL::NR_OF_CTRL_REGISTERS + 1;
// We dont have to check if this is working because we just did i // We dont have to check if this is working because we just did i
return RETURN_OK; return RETURN_OK;
} }
void MgmLIS3MDLHandler::doTransition(Mode_t modeFrom, Submode_t subModeFrom) { void MgmLIS3MDLHandler::doTransition(Mode_t modeFrom, Submode_t subModeFrom) {}
} uint32_t MgmLIS3MDLHandler::getTransitionDelayMs(Mode_t from, Mode_t to) { return transitionDelay; }
uint32_t MgmLIS3MDLHandler::getTransitionDelayMs(Mode_t from, Mode_t to) { void MgmLIS3MDLHandler::modeChanged(void) { internalState = InternalState::STATE_NONE; }
return transitionDelay;
}
void MgmLIS3MDLHandler::modeChanged(void) { ReturnValue_t MgmLIS3MDLHandler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
internalState = InternalState::STATE_NONE; LocalDataPoolManager &poolManager) {
} localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_X, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_Y, new PoolEntry<float>({0.0}));
ReturnValue_t MgmLIS3MDLHandler::initializeLocalDataPool( localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_Z, new PoolEntry<float>({0.0}));
localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) { localDataPoolMap.emplace(MGMLIS3MDL::TEMPERATURE_CELCIUS, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_X, return HasReturnvaluesIF::RETURN_OK;
new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_Y,
new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::FIELD_STRENGTH_Z,
new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(MGMLIS3MDL::TEMPERATURE_CELCIUS,
new PoolEntry<float>({0.0}));
return HasReturnvaluesIF::RETURN_OK;
} }
void MgmLIS3MDLHandler::setAbsoluteLimits(float xLimit, float yLimit, float zLimit) { void MgmLIS3MDLHandler::setAbsoluteLimits(float xLimit, float yLimit, float zLimit) {
this->absLimitX = xLimit; this->absLimitX = xLimit;
this->absLimitY = yLimit; this->absLimitY = yLimit;
this->absLimitZ = zLimit; this->absLimitZ = zLimit;
} }

View File

@ -1,10 +1,9 @@
#ifndef MISSION_DEVICES_MGMLIS3MDLHANDLER_H_ #ifndef MISSION_DEVICES_MGMLIS3MDLHANDLER_H_
#define MISSION_DEVICES_MGMLIS3MDLHANDLER_H_ #define MISSION_DEVICES_MGMLIS3MDLHANDLER_H_
#include "fsfw/FSFW.h"
#include "events/subsystemIdRanges.h"
#include "devicedefinitions/MgmLIS3HandlerDefs.h" #include "devicedefinitions/MgmLIS3HandlerDefs.h"
#include "events/subsystemIdRanges.h"
#include "fsfw/FSFW.h"
#include "fsfw/devicehandlers/DeviceHandlerBase.h" #include "fsfw/devicehandlers/DeviceHandlerBase.h"
class PeriodicOperationDivider; class PeriodicOperationDivider;
@ -18,168 +17,158 @@ class PeriodicOperationDivider;
* https://egit.irs.uni-stuttgart.de/redmine/projects/eive-flight-manual/wiki/LIS3MDL_MGM * https://egit.irs.uni-stuttgart.de/redmine/projects/eive-flight-manual/wiki/LIS3MDL_MGM
* @author L. Loidold, R. Mueller * @author L. Loidold, R. Mueller
*/ */
class MgmLIS3MDLHandler: public DeviceHandlerBase { class MgmLIS3MDLHandler : public DeviceHandlerBase {
public: public:
enum class CommunicationStep { enum class CommunicationStep { DATA, TEMPERATURE };
DATA,
TEMPERATURE
};
static const uint8_t INTERFACE_ID = CLASS_ID::MGM_LIS3MDL; static const uint8_t INTERFACE_ID = CLASS_ID::MGM_LIS3MDL;
static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::MGM_LIS3MDL; static const uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::MGM_LIS3MDL;
//Notifies a command to change the setup parameters // Notifies a command to change the setup parameters
static const Event CHANGE_OF_SETUP_PARAMETER = MAKE_EVENT(0, severity::LOW); static const Event CHANGE_OF_SETUP_PARAMETER = MAKE_EVENT(0, severity::LOW);
MgmLIS3MDLHandler(uint32_t objectId, object_id_t deviceCommunication, CookieIF* comCookie, MgmLIS3MDLHandler(uint32_t objectId, object_id_t deviceCommunication, CookieIF *comCookie,
uint32_t transitionDelay); uint32_t transitionDelay);
virtual ~MgmLIS3MDLHandler(); virtual ~MgmLIS3MDLHandler();
/** /**
* Set the absolute limit for the values on the axis in microtesla. The dataset values will * Set the absolute limit for the values on the axis in microtesla. The dataset values will
* be marked as invalid if that limit is exceeded * be marked as invalid if that limit is exceeded
* @param xLimit * @param xLimit
* @param yLimit * @param yLimit
* @param zLimit * @param zLimit
*/ */
void setAbsoluteLimits(float xLimit, float yLimit, float zLimit); void setAbsoluteLimits(float xLimit, float yLimit, float zLimit);
void setToGoToNormalMode(bool enable); void setToGoToNormalMode(bool enable);
protected: protected:
/** DeviceHandlerBase overrides */
void doShutDown() override;
void doStartUp() override;
void doTransition(Mode_t modeFrom, Submode_t subModeFrom) override;
virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to) override;
ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData,
size_t commandDataLen) override;
ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override;
ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
ReturnValue_t scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId,
size_t *foundLen) override;
/**
* This implementation is tailored towards space applications and will flag values larger
* than 100 microtesla on X,Y and 150 microtesla on Z as invalid
* @param id
* @param packet
* @return
*/
virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override;
void fillCommandAndReplyMap() override;
void modeChanged(void) override;
ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
LocalDataPoolManager &poolManager) override;
/** DeviceHandlerBase overrides */ private:
void doShutDown() override; MGMLIS3MDL::MgmPrimaryDataset dataset;
void doStartUp() override; // Length a single command SPI answer
void doTransition(Mode_t modeFrom, Submode_t subModeFrom) override; static const uint8_t SINGLE_COMMAND_ANSWER_LEN = 2;
virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to) override;
ReturnValue_t buildCommandFromCommand(
DeviceCommandId_t deviceCommand, const uint8_t *commandData,
size_t commandDataLen) override;
ReturnValue_t buildTransitionDeviceCommand(
DeviceCommandId_t *id) override;
ReturnValue_t buildNormalDeviceCommand(
DeviceCommandId_t *id) override;
ReturnValue_t scanForReply(const uint8_t *start, size_t len,
DeviceCommandId_t *foundId, size_t *foundLen) override;
/**
* This implementation is tailored towards space applications and will flag values larger
* than 100 microtesla on X,Y and 150 microtesla on Z as invalid
* @param id
* @param packet
* @return
*/
virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id,
const uint8_t *packet) override;
void fillCommandAndReplyMap() override;
void modeChanged(void) override;
ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
LocalDataPoolManager &poolManager) override;
private: uint32_t transitionDelay;
MGMLIS3MDL::MgmPrimaryDataset dataset; // Single SPI command has 2 bytes, first for adress, second for content
//Length a single command SPI answer size_t singleComandSize = 2;
static const uint8_t SINGLE_COMMAND_ANSWER_LEN = 2; // Has the size for all adresses of the lis3mdl + the continous write bit
uint8_t commandBuffer[MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1];
uint32_t transitionDelay; float absLimitX = 100;
// Single SPI command has 2 bytes, first for adress, second for content float absLimitY = 100;
size_t singleComandSize = 2; float absLimitZ = 150;
// Has the size for all adresses of the lis3mdl + the continous write bit
uint8_t commandBuffer[MGMLIS3MDL::NR_OF_DATA_AND_CFG_REGISTERS + 1];
float absLimitX = 100; /**
float absLimitY = 100; * We want to save the registers we set, so we dont have to read the
float absLimitZ = 150; * registers when we want to change something.
* --> everytime we change set a register we have to save it
*/
uint8_t registers[MGMLIS3MDL::NR_OF_CTRL_REGISTERS];
/** uint8_t statusRegister = 0;
* We want to save the registers we set, so we dont have to read the bool goToNormalMode = false;
* registers when we want to change something.
* --> everytime we change set a register we have to save it
*/
uint8_t registers[MGMLIS3MDL::NR_OF_CTRL_REGISTERS];
uint8_t statusRegister = 0; enum class InternalState {
bool goToNormalMode = false; STATE_NONE,
STATE_FIRST_CONTACT,
STATE_SETUP,
STATE_CHECK_REGISTERS,
STATE_NORMAL
};
enum class InternalState { InternalState internalState = InternalState::STATE_NONE;
STATE_NONE, CommunicationStep communicationStep = CommunicationStep::DATA;
STATE_FIRST_CONTACT, bool commandExecuted = false;
STATE_SETUP,
STATE_CHECK_REGISTERS,
STATE_NORMAL
};
InternalState internalState = InternalState::STATE_NONE; /*------------------------------------------------------------------------*/
CommunicationStep communicationStep = CommunicationStep::DATA; /* Device specific commands and variables */
bool commandExecuted = false; /*------------------------------------------------------------------------*/
/**
* Sets the read bit for the command
* @param single command to set the read-bit at
* @param boolean to select a continuous read bit, default = false
*/
uint8_t readCommand(uint8_t command, bool continuousCom = false);
/*------------------------------------------------------------------------*/ /**
/* Device specific commands and variables */ * Sets the write bit for the command
/*------------------------------------------------------------------------*/ * @param single command to set the write-bit at
/** * @param boolean to select a continuous write bit, default = false
* Sets the read bit for the command */
* @param single command to set the read-bit at uint8_t writeCommand(uint8_t command, bool continuousCom = false);
* @param boolean to select a continuous read bit, default = false
*/
uint8_t readCommand(uint8_t command, bool continuousCom = false);
/** /**
* Sets the write bit for the command * This Method gets the full scale for the measurement range
* @param single command to set the write-bit at * e.g.: +- 4 gauss. See p.25 datasheet.
* @param boolean to select a continuous write bit, default = false * @return The ReturnValue does not contain the sign of the value
*/ */
uint8_t writeCommand(uint8_t command, bool continuousCom = false); MGMLIS3MDL::Sensitivies getSensitivity(uint8_t ctrlReg2);
/** /**
* This Method gets the full scale for the measurement range * The 16 bit value needs to be multiplied with a sensitivity factor
* e.g.: +- 4 gauss. See p.25 datasheet. * which depends on the sensitivity configuration
* @return The ReturnValue does not contain the sign of the value *
*/ * @param sens Configured sensitivity of the LIS3 device
MGMLIS3MDL::Sensitivies getSensitivity(uint8_t ctrlReg2); * @return Multiplication factor to get the sensor value from raw data.
*/
float getSensitivityFactor(MGMLIS3MDL::Sensitivies sens);
/** /**
* The 16 bit value needs to be multiplied with a sensitivity factor * This Command detects the device ID
* which depends on the sensitivity configuration */
* ReturnValue_t identifyDevice();
* @param sens Configured sensitivity of the LIS3 device
* @return Multiplication factor to get the sensor value from raw data.
*/
float getSensitivityFactor(MGMLIS3MDL::Sensitivies sens);
/** virtual void setupMgm();
* This Command detects the device ID
*/
ReturnValue_t identifyDevice();
virtual void setupMgm(); /*------------------------------------------------------------------------*/
/* Non normal commands */
/*------------------------------------------------------------------------*/
/**
* Enables/Disables the integrated Temperaturesensor
* @param commandData On or Off
* @param length of the commandData: has to be 1
*/
virtual ReturnValue_t enableTemperatureSensor(const uint8_t *commandData, size_t commandDataLen);
/*------------------------------------------------------------------------*/ /**
/* Non normal commands */ * Sets the accuracy of the measurement of the axis. The noise is changing.
/*------------------------------------------------------------------------*/ * @param commandData LOW, MEDIUM, HIGH, ULTRA
/** * @param length of the command, has to be 1
* Enables/Disables the integrated Temperaturesensor */
* @param commandData On or Off virtual ReturnValue_t setOperatingMode(const uint8_t *commandData, size_t commandDataLen);
* @param length of the commandData: has to be 1
*/
virtual ReturnValue_t enableTemperatureSensor(const uint8_t *commandData,
size_t commandDataLen);
/** /**
* Sets the accuracy of the measurement of the axis. The noise is changing. * We always update all registers together, so this method updates
* @param commandData LOW, MEDIUM, HIGH, ULTRA * the rawpacket and rawpacketLen, so we just manipulate the local
* @param length of the command, has to be 1 * saved register
*/ *
virtual ReturnValue_t setOperatingMode(const uint8_t *commandData, */
size_t commandDataLen); ReturnValue_t prepareCtrlRegisterWrite();
/**
* We always update all registers together, so this method updates
* the rawpacket and rawpacketLen, so we just manipulate the local
* saved register
*
*/
ReturnValue_t prepareCtrlRegisterWrite();
#if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1 #if FSFW_HAL_LIS3MDL_MGM_DEBUG == 1
PeriodicOperationDivider* debugDivider; PeriodicOperationDivider *debugDivider;
#endif #endif
}; };

View File

@ -1,376 +1,367 @@
#include "MgmRM3100Handler.h" #include "MgmRM3100Handler.h"
#include "fsfw/datapool/PoolReadGuard.h" #include "fsfw/datapool/PoolReadGuard.h"
#include "fsfw/globalfunctions/bitutility.h"
#include "fsfw/devicehandlers/DeviceHandlerMessage.h" #include "fsfw/devicehandlers/DeviceHandlerMessage.h"
#include "fsfw/globalfunctions/bitutility.h"
#include "fsfw/objectmanager/SystemObjectIF.h" #include "fsfw/objectmanager/SystemObjectIF.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
MgmRM3100Handler::MgmRM3100Handler(object_id_t objectId, object_id_t deviceCommunication,
MgmRM3100Handler::MgmRM3100Handler(object_id_t objectId, CookieIF *comCookie, uint32_t transitionDelay)
object_id_t deviceCommunication, CookieIF* comCookie, uint32_t transitionDelay): : DeviceHandlerBase(objectId, deviceCommunication, comCookie),
DeviceHandlerBase(objectId, deviceCommunication, comCookie), primaryDataset(this),
primaryDataset(this), transitionDelay(transitionDelay) { transitionDelay(transitionDelay) {
#if FSFW_HAL_RM3100_MGM_DEBUG == 1 #if FSFW_HAL_RM3100_MGM_DEBUG == 1
debugDivider = new PeriodicOperationDivider(3); debugDivider = new PeriodicOperationDivider(3);
#endif #endif
} }
MgmRM3100Handler::~MgmRM3100Handler() {} MgmRM3100Handler::~MgmRM3100Handler() {}
void MgmRM3100Handler::doStartUp() { void MgmRM3100Handler::doStartUp() {
switch(internalState) { switch (internalState) {
case(InternalState::NONE): { case (InternalState::NONE): {
internalState = InternalState::CONFIGURE_CMM; internalState = InternalState::CONFIGURE_CMM;
break; break;
} }
case(InternalState::CONFIGURE_CMM): { case (InternalState::CONFIGURE_CMM): {
internalState = InternalState::READ_CMM; internalState = InternalState::READ_CMM;
break; break;
} }
case(InternalState::READ_CMM): { case (InternalState::READ_CMM): {
if(commandExecuted) { if (commandExecuted) {
internalState = InternalState::STATE_CONFIGURE_TMRC; internalState = InternalState::STATE_CONFIGURE_TMRC;
}
break;
}
case (InternalState::STATE_CONFIGURE_TMRC): {
if (commandExecuted) {
internalState = InternalState::STATE_READ_TMRC;
}
break;
}
case (InternalState::STATE_READ_TMRC): {
if (commandExecuted) {
internalState = InternalState::NORMAL;
if (goToNormalModeAtStartup) {
setMode(MODE_NORMAL);
} else {
setMode(_MODE_TO_ON);
} }
break; }
} break;
case(InternalState::STATE_CONFIGURE_TMRC): {
if(commandExecuted) {
internalState = InternalState::STATE_READ_TMRC;
}
break;
}
case(InternalState::STATE_READ_TMRC): {
if(commandExecuted) {
internalState = InternalState::NORMAL;
if(goToNormalModeAtStartup) {
setMode(MODE_NORMAL);
}
else {
setMode(_MODE_TO_ON);
}
}
break;
} }
default: { default: {
break; break;
}
} }
}
} }
void MgmRM3100Handler::doShutDown() { void MgmRM3100Handler::doShutDown() { setMode(_MODE_POWER_DOWN); }
setMode(_MODE_POWER_DOWN);
}
ReturnValue_t MgmRM3100Handler::buildTransitionDeviceCommand( ReturnValue_t MgmRM3100Handler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
DeviceCommandId_t *id) { size_t commandLen = 0;
size_t commandLen = 0; switch (internalState) {
switch(internalState) { case (InternalState::NONE):
case(InternalState::NONE): case (InternalState::NORMAL): {
case(InternalState::NORMAL): { return NOTHING_TO_SEND;
return NOTHING_TO_SEND;
} }
case(InternalState::CONFIGURE_CMM): { case (InternalState::CONFIGURE_CMM): {
*id = RM3100::CONFIGURE_CMM; *id = RM3100::CONFIGURE_CMM;
break; break;
} }
case(InternalState::READ_CMM): { case (InternalState::READ_CMM): {
*id = RM3100::READ_CMM; *id = RM3100::READ_CMM;
break; break;
} }
case(InternalState::STATE_CONFIGURE_TMRC): { case (InternalState::STATE_CONFIGURE_TMRC): {
commandBuffer[0] = RM3100::TMRC_DEFAULT_VALUE; commandBuffer[0] = RM3100::TMRC_DEFAULT_VALUE;
commandLen = 1; commandLen = 1;
*id = RM3100::CONFIGURE_TMRC; *id = RM3100::CONFIGURE_TMRC;
break; break;
} }
case(InternalState::STATE_READ_TMRC): { case (InternalState::STATE_READ_TMRC): {
*id = RM3100::READ_TMRC; *id = RM3100::READ_TMRC;
break; break;
} }
default: default:
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
// Might be a configuration error // Might be a configuration error
sif::warning << "MgmRM3100Handler::buildTransitionDeviceCommand: " sif::warning << "MgmRM3100Handler::buildTransitionDeviceCommand: "
"Unknown internal state" << std::endl; "Unknown internal state"
<< std::endl;
#else #else
sif::printWarning("MgmRM3100Handler::buildTransitionDeviceCommand: " sif::printWarning(
"Unknown internal state\n"); "MgmRM3100Handler::buildTransitionDeviceCommand: "
"Unknown internal state\n");
#endif #endif
#endif #endif
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
return buildCommandFromCommand(*id, commandBuffer, commandLen); return buildCommandFromCommand(*id, commandBuffer, commandLen);
} }
ReturnValue_t MgmRM3100Handler::buildCommandFromCommand(DeviceCommandId_t deviceCommand, ReturnValue_t MgmRM3100Handler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData, size_t commandDataLen) { const uint8_t *commandData,
switch(deviceCommand) { size_t commandDataLen) {
case(RM3100::CONFIGURE_CMM): { switch (deviceCommand) {
commandBuffer[0] = RM3100::CMM_REGISTER; case (RM3100::CONFIGURE_CMM): {
commandBuffer[1] = RM3100::CMM_VALUE; commandBuffer[0] = RM3100::CMM_REGISTER;
rawPacket = commandBuffer; commandBuffer[1] = RM3100::CMM_VALUE;
rawPacketLen = 2; rawPacket = commandBuffer;
break; rawPacketLen = 2;
break;
} }
case(RM3100::READ_CMM): { case (RM3100::READ_CMM): {
commandBuffer[0] = RM3100::CMM_REGISTER | RM3100::READ_MASK; commandBuffer[0] = RM3100::CMM_REGISTER | RM3100::READ_MASK;
commandBuffer[1] = 0; commandBuffer[1] = 0;
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = 2; rawPacketLen = 2;
break; break;
} }
case(RM3100::CONFIGURE_TMRC): { case (RM3100::CONFIGURE_TMRC): {
return handleTmrcConfigCommand(deviceCommand, commandData, commandDataLen); return handleTmrcConfigCommand(deviceCommand, commandData, commandDataLen);
} }
case(RM3100::READ_TMRC): { case (RM3100::READ_TMRC): {
commandBuffer[0] = RM3100::TMRC_REGISTER | RM3100::READ_MASK; commandBuffer[0] = RM3100::TMRC_REGISTER | RM3100::READ_MASK;
commandBuffer[1] = 0; commandBuffer[1] = 0;
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = 2; rawPacketLen = 2;
break; break;
} }
case(RM3100::CONFIGURE_CYCLE_COUNT): { case (RM3100::CONFIGURE_CYCLE_COUNT): {
return handleCycleCountConfigCommand(deviceCommand, commandData, commandDataLen); return handleCycleCountConfigCommand(deviceCommand, commandData, commandDataLen);
} }
case(RM3100::READ_CYCLE_COUNT): { case (RM3100::READ_CYCLE_COUNT): {
commandBuffer[0] = RM3100::CYCLE_COUNT_START_REGISTER | RM3100::READ_MASK; commandBuffer[0] = RM3100::CYCLE_COUNT_START_REGISTER | RM3100::READ_MASK;
std::memset(commandBuffer + 1, 0, 6); std::memset(commandBuffer + 1, 0, 6);
rawPacket = commandBuffer; rawPacket = commandBuffer;
rawPacketLen = 7; rawPacketLen = 7;
break; break;
} }
case(RM3100::READ_DATA): { case (RM3100::READ_DATA): {
commandBuffer[0] = RM3100::MEASUREMENT_REG_START | RM3100::READ_MASK; commandBuffer[0] = RM3100::MEASUREMENT_REG_START | RM3100::READ_MASK;
std::memset(commandBuffer + 1, 0, 9); std::memset(commandBuffer + 1, 0, 9);
rawPacketLen = 10; rawPacketLen = 10;
break; break;
} }
default: default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED; return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
} }
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t MgmRM3100Handler::buildNormalDeviceCommand( ReturnValue_t MgmRM3100Handler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
DeviceCommandId_t *id) { *id = RM3100::READ_DATA;
*id = RM3100::READ_DATA; return buildCommandFromCommand(*id, nullptr, 0);
return buildCommandFromCommand(*id, nullptr, 0);
} }
ReturnValue_t MgmRM3100Handler::scanForReply(const uint8_t *start, ReturnValue_t MgmRM3100Handler::scanForReply(const uint8_t *start, size_t len,
size_t len, DeviceCommandId_t *foundId, DeviceCommandId_t *foundId, size_t *foundLen) {
size_t *foundLen) { // For SPI, ID will always be the one of the last sent command
*foundId = this->getPendingCommand();
// For SPI, ID will always be the one of the last sent command *foundLen = len;
*foundId = this->getPendingCommand(); return HasReturnvaluesIF::RETURN_OK;
*foundLen = len;
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t MgmRM3100Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { ReturnValue_t MgmRM3100Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
switch(id) { switch (id) {
case(RM3100::CONFIGURE_CMM): case (RM3100::CONFIGURE_CMM):
case(RM3100::CONFIGURE_CYCLE_COUNT): case (RM3100::CONFIGURE_CYCLE_COUNT):
case(RM3100::CONFIGURE_TMRC): { case (RM3100::CONFIGURE_TMRC): {
// We can only check whether write was successful with read operation // We can only check whether write was successful with read operation
if(mode == _MODE_START_UP) { if (mode == _MODE_START_UP) {
commandExecuted = true; commandExecuted = true;
} }
break; break;
} }
case(RM3100::READ_CMM): { case (RM3100::READ_CMM): {
uint8_t cmmValue = packet[1]; uint8_t cmmValue = packet[1];
// We clear the seventh bit in any case // We clear the seventh bit in any case
// because this one is zero sometimes for some reason // because this one is zero sometimes for some reason
bitutil::clear(&cmmValue, 6); bitutil::clear(&cmmValue, 6);
if(cmmValue == cmmRegValue and internalState == InternalState::READ_CMM) { if (cmmValue == cmmRegValue and internalState == InternalState::READ_CMM) {
commandExecuted = true; commandExecuted = true;
} } else {
else { // Attempt reconfiguration
// Attempt reconfiguration internalState = InternalState::CONFIGURE_CMM;
internalState = InternalState::CONFIGURE_CMM; return DeviceHandlerIF::DEVICE_REPLY_INVALID;
return DeviceHandlerIF::DEVICE_REPLY_INVALID; }
} break;
break;
} }
case(RM3100::READ_TMRC): { case (RM3100::READ_TMRC): {
if(packet[1] == tmrcRegValue) { if (packet[1] == tmrcRegValue) {
commandExecuted = true; commandExecuted = true;
// Reading TMRC was commanded. Trigger event to inform ground
if(mode != _MODE_START_UP) {
triggerEvent(tmrcSet, tmrcRegValue, 0);
}
}
else {
// Attempt reconfiguration
internalState = InternalState::STATE_CONFIGURE_TMRC;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
}
case(RM3100::READ_CYCLE_COUNT): {
uint16_t cycleCountX = packet[1] << 8 | packet[2];
uint16_t cycleCountY = packet[3] << 8 | packet[4];
uint16_t cycleCountZ = packet[5] << 8 | packet[6];
if(cycleCountX != cycleCountRegValueX or cycleCountY != cycleCountRegValueY or
cycleCountZ != cycleCountRegValueZ) {
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
// Reading TMRC was commanded. Trigger event to inform ground // Reading TMRC was commanded. Trigger event to inform ground
if(mode != _MODE_START_UP) { if (mode != _MODE_START_UP) {
uint32_t eventParam1 = (cycleCountX << 16) | cycleCountY; triggerEvent(tmrcSet, tmrcRegValue, 0);
triggerEvent(cycleCountersSet, eventParam1, cycleCountZ);
} }
break; } else {
// Attempt reconfiguration
internalState = InternalState::STATE_CONFIGURE_TMRC;
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
break;
} }
case(RM3100::READ_DATA): { case (RM3100::READ_CYCLE_COUNT): {
result = handleDataReadout(packet); uint16_t cycleCountX = packet[1] << 8 | packet[2];
break; uint16_t cycleCountY = packet[3] << 8 | packet[4];
uint16_t cycleCountZ = packet[5] << 8 | packet[6];
if (cycleCountX != cycleCountRegValueX or cycleCountY != cycleCountRegValueY or
cycleCountZ != cycleCountRegValueZ) {
return DeviceHandlerIF::DEVICE_REPLY_INVALID;
}
// Reading TMRC was commanded. Trigger event to inform ground
if (mode != _MODE_START_UP) {
uint32_t eventParam1 = (cycleCountX << 16) | cycleCountY;
triggerEvent(cycleCountersSet, eventParam1, cycleCountZ);
}
break;
}
case (RM3100::READ_DATA): {
result = handleDataReadout(packet);
break;
} }
default: default:
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY; return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
} }
return result; return result;
} }
ReturnValue_t MgmRM3100Handler::handleCycleCountConfigCommand(DeviceCommandId_t deviceCommand, ReturnValue_t MgmRM3100Handler::handleCycleCountConfigCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData, size_t commandDataLen) { const uint8_t *commandData,
if(commandData == nullptr) { size_t commandDataLen) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER; if (commandData == nullptr) {
} return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
// Set cycle count // Set cycle count
if(commandDataLen == 2) { if (commandDataLen == 2) {
handleCycleCommand(true, commandData, commandDataLen); handleCycleCommand(true, commandData, commandDataLen);
} } else if (commandDataLen == 6) {
else if(commandDataLen == 6) { handleCycleCommand(false, commandData, commandDataLen);
handleCycleCommand(false, commandData, commandDataLen); } else {
} return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
else { }
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
commandBuffer[0] = RM3100::CYCLE_COUNT_VALUE; commandBuffer[0] = RM3100::CYCLE_COUNT_VALUE;
std::memcpy(commandBuffer + 1, &cycleCountRegValueX, 2); std::memcpy(commandBuffer + 1, &cycleCountRegValueX, 2);
std::memcpy(commandBuffer + 3, &cycleCountRegValueY, 2); std::memcpy(commandBuffer + 3, &cycleCountRegValueY, 2);
std::memcpy(commandBuffer + 5, &cycleCountRegValueZ, 2); std::memcpy(commandBuffer + 5, &cycleCountRegValueZ, 2);
rawPacketLen = 7; rawPacketLen = 7;
rawPacket = commandBuffer; rawPacket = commandBuffer;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t MgmRM3100Handler::handleCycleCommand(bool oneCycleValue, ReturnValue_t MgmRM3100Handler::handleCycleCommand(bool oneCycleValue, const uint8_t *commandData,
const uint8_t *commandData, size_t commandDataLen) { size_t commandDataLen) {
RM3100::CycleCountCommand command(oneCycleValue); RM3100::CycleCountCommand command(oneCycleValue);
ReturnValue_t result = command.deSerialize(&commandData, &commandDataLen, ReturnValue_t result =
SerializeIF::Endianness::BIG); command.deSerialize(&commandData, &commandDataLen, SerializeIF::Endianness::BIG);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
// Data sheet p.30 "while noise limits the useful upper range to ~400 cycle counts." // Data sheet p.30 "while noise limits the useful upper range to ~400 cycle counts."
if(command.cycleCountX > 450 ) { if (command.cycleCountX > 450) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER; return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
} }
if(not oneCycleValue and (command.cycleCountY > 450 or command.cycleCountZ > 450)) { if (not oneCycleValue and (command.cycleCountY > 450 or command.cycleCountZ > 450)) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER; return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
} }
cycleCountRegValueX = command.cycleCountX; cycleCountRegValueX = command.cycleCountX;
cycleCountRegValueY = command.cycleCountY; cycleCountRegValueY = command.cycleCountY;
cycleCountRegValueZ = command.cycleCountZ; cycleCountRegValueZ = command.cycleCountZ;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t MgmRM3100Handler::handleTmrcConfigCommand(DeviceCommandId_t deviceCommand, ReturnValue_t MgmRM3100Handler::handleTmrcConfigCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData, size_t commandDataLen) { const uint8_t *commandData,
if(commandData == nullptr or commandDataLen != 1) { size_t commandDataLen) {
return DeviceHandlerIF::INVALID_COMMAND_PARAMETER; if (commandData == nullptr or commandDataLen != 1) {
} return DeviceHandlerIF::INVALID_COMMAND_PARAMETER;
}
commandBuffer[0] = RM3100::TMRC_REGISTER; commandBuffer[0] = RM3100::TMRC_REGISTER;
commandBuffer[1] = commandData[0]; commandBuffer[1] = commandData[0];
tmrcRegValue = commandData[0]; tmrcRegValue = commandData[0];
rawPacketLen = 2; rawPacketLen = 2;
rawPacket = commandBuffer; rawPacket = commandBuffer;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void MgmRM3100Handler::fillCommandAndReplyMap() { void MgmRM3100Handler::fillCommandAndReplyMap() {
insertInCommandAndReplyMap(RM3100::CONFIGURE_CMM, 3); insertInCommandAndReplyMap(RM3100::CONFIGURE_CMM, 3);
insertInCommandAndReplyMap(RM3100::READ_CMM, 3); insertInCommandAndReplyMap(RM3100::READ_CMM, 3);
insertInCommandAndReplyMap(RM3100::CONFIGURE_TMRC, 3); insertInCommandAndReplyMap(RM3100::CONFIGURE_TMRC, 3);
insertInCommandAndReplyMap(RM3100::READ_TMRC, 3); insertInCommandAndReplyMap(RM3100::READ_TMRC, 3);
insertInCommandAndReplyMap(RM3100::CONFIGURE_CYCLE_COUNT, 3); insertInCommandAndReplyMap(RM3100::CONFIGURE_CYCLE_COUNT, 3);
insertInCommandAndReplyMap(RM3100::READ_CYCLE_COUNT, 3); insertInCommandAndReplyMap(RM3100::READ_CYCLE_COUNT, 3);
insertInCommandAndReplyMap(RM3100::READ_DATA, 3, &primaryDataset); insertInCommandAndReplyMap(RM3100::READ_DATA, 3, &primaryDataset);
} }
void MgmRM3100Handler::modeChanged(void) { void MgmRM3100Handler::modeChanged(void) { internalState = InternalState::NONE; }
internalState = InternalState::NONE;
}
ReturnValue_t MgmRM3100Handler::initializeLocalDataPool( ReturnValue_t MgmRM3100Handler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) { LocalDataPoolManager &poolManager) {
localDataPoolMap.emplace(RM3100::FIELD_STRENGTH_X, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(RM3100::FIELD_STRENGTH_X, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(RM3100::FIELD_STRENGTH_Y, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(RM3100::FIELD_STRENGTH_Y, new PoolEntry<float>({0.0}));
localDataPoolMap.emplace(RM3100::FIELD_STRENGTH_Z, new PoolEntry<float>({0.0})); localDataPoolMap.emplace(RM3100::FIELD_STRENGTH_Z, new PoolEntry<float>({0.0}));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
uint32_t MgmRM3100Handler::getTransitionDelayMs(Mode_t from, Mode_t to) { uint32_t MgmRM3100Handler::getTransitionDelayMs(Mode_t from, Mode_t to) {
return this->transitionDelay; return this->transitionDelay;
} }
void MgmRM3100Handler::setToGoToNormalMode(bool enable) { void MgmRM3100Handler::setToGoToNormalMode(bool enable) { goToNormalModeAtStartup = enable; }
goToNormalModeAtStartup = enable;
}
ReturnValue_t MgmRM3100Handler::handleDataReadout(const uint8_t *packet) { ReturnValue_t MgmRM3100Handler::handleDataReadout(const uint8_t *packet) {
// Analyze data here. The sensor generates 24 bit signed values so we need to do some bitshift // Analyze data here. The sensor generates 24 bit signed values so we need to do some bitshift
// trickery here to calculate the raw values first // trickery here to calculate the raw values first
int32_t fieldStrengthRawX = ((packet[1] << 24) | (packet[2] << 16) | (packet[3] << 8)) >> 8; int32_t fieldStrengthRawX = ((packet[1] << 24) | (packet[2] << 16) | (packet[3] << 8)) >> 8;
int32_t fieldStrengthRawY = ((packet[4] << 24) | (packet[5] << 16) | (packet[6] << 8)) >> 8; int32_t fieldStrengthRawY = ((packet[4] << 24) | (packet[5] << 16) | (packet[6] << 8)) >> 8;
int32_t fieldStrengthRawZ = ((packet[7] << 24) | (packet[8] << 16) | (packet[3] << 8)) >> 8; int32_t fieldStrengthRawZ = ((packet[7] << 24) | (packet[8] << 16) | (packet[3] << 8)) >> 8;
// Now scale to physical value in microtesla // Now scale to physical value in microtesla
float fieldStrengthX = fieldStrengthRawX * scaleFactorX; float fieldStrengthX = fieldStrengthRawX * scaleFactorX;
float fieldStrengthY = fieldStrengthRawY * scaleFactorX; float fieldStrengthY = fieldStrengthRawY * scaleFactorX;
float fieldStrengthZ = fieldStrengthRawZ * scaleFactorX; float fieldStrengthZ = fieldStrengthRawZ * scaleFactorX;
#if FSFW_HAL_RM3100_MGM_DEBUG == 1 #if FSFW_HAL_RM3100_MGM_DEBUG == 1
if(debugDivider->checkAndIncrement()) { if (debugDivider->checkAndIncrement()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "MgmRM3100Handler: Magnetic field strength in" sif::info << "MgmRM3100Handler: Magnetic field strength in"
" microtesla:" << std::endl; " microtesla:"
sif::info << "X: " << fieldStrengthX << " uT" << std::endl; << std::endl;
sif::info << "Y: " << fieldStrengthY << " uT" << std::endl; sif::info << "X: " << fieldStrengthX << " uT" << std::endl;
sif::info << "Z: " << fieldStrengthZ << " uT" << std::endl; sif::info << "Y: " << fieldStrengthY << " uT" << std::endl;
sif::info << "Z: " << fieldStrengthZ << " uT" << std::endl;
#else #else
sif::printInfo("MgmRM3100Handler: Magnetic field strength in microtesla:\n"); sif::printInfo("MgmRM3100Handler: Magnetic field strength in microtesla:\n");
sif::printInfo("X: %f uT\n", fieldStrengthX); sif::printInfo("X: %f uT\n", fieldStrengthX);
sif::printInfo("Y: %f uT\n", fieldStrengthY); sif::printInfo("Y: %f uT\n", fieldStrengthY);
sif::printInfo("Z: %f uT\n", fieldStrengthZ); sif::printInfo("Z: %f uT\n", fieldStrengthZ);
#endif #endif
} }
#endif #endif
// TODO: Sanity check on values? // TODO: Sanity check on values?
PoolReadGuard readGuard(&primaryDataset); PoolReadGuard readGuard(&primaryDataset);
if(readGuard.getReadResult() == HasReturnvaluesIF::RETURN_OK) { if (readGuard.getReadResult() == HasReturnvaluesIF::RETURN_OK) {
primaryDataset.fieldStrengthX = fieldStrengthX; primaryDataset.fieldStrengthX = fieldStrengthX;
primaryDataset.fieldStrengthY = fieldStrengthY; primaryDataset.fieldStrengthY = fieldStrengthY;
primaryDataset.fieldStrengthZ = fieldStrengthZ; primaryDataset.fieldStrengthZ = fieldStrengthZ;
primaryDataset.setValidity(true, true); primaryDataset.setValidity(true, true);
} }
return RETURN_OK; return RETURN_OK;
} }

View File

@ -1,8 +1,8 @@
#ifndef MISSION_DEVICES_MGMRM3100HANDLER_H_ #ifndef MISSION_DEVICES_MGMRM3100HANDLER_H_
#define MISSION_DEVICES_MGMRM3100HANDLER_H_ #define MISSION_DEVICES_MGMRM3100HANDLER_H_
#include "fsfw/FSFW.h"
#include "devicedefinitions/MgmRM3100HandlerDefs.h" #include "devicedefinitions/MgmRM3100HandlerDefs.h"
#include "fsfw/FSFW.h"
#include "fsfw/devicehandlers/DeviceHandlerBase.h" #include "fsfw/devicehandlers/DeviceHandlerBase.h"
#if FSFW_HAL_RM3100_MGM_DEBUG == 1 #if FSFW_HAL_RM3100_MGM_DEBUG == 1
@ -16,94 +16,90 @@
* Flight manual: * Flight manual:
* https://egit.irs.uni-stuttgart.de/redmine/projects/eive-flight-manual/wiki/RM3100_MGM * https://egit.irs.uni-stuttgart.de/redmine/projects/eive-flight-manual/wiki/RM3100_MGM
*/ */
class MgmRM3100Handler: public DeviceHandlerBase { class MgmRM3100Handler : public DeviceHandlerBase {
public: public:
static const uint8_t INTERFACE_ID = CLASS_ID::MGM_RM3100; static const uint8_t INTERFACE_ID = CLASS_ID::MGM_RM3100;
//! [EXPORT] : [COMMENT] P1: TMRC value which was set, P2: 0 //! [EXPORT] : [COMMENT] P1: TMRC value which was set, P2: 0
static constexpr Event tmrcSet = event::makeEvent(SUBSYSTEM_ID::MGM_RM3100, static constexpr Event tmrcSet = event::makeEvent(SUBSYSTEM_ID::MGM_RM3100, 0x00, severity::INFO);
0x00, severity::INFO);
//! [EXPORT] : [COMMENT] Cycle counter set. P1: First two bytes new Cycle Count X //! [EXPORT] : [COMMENT] Cycle counter set. P1: First two bytes new Cycle Count X
//! P1: Second two bytes new Cycle Count Y //! P1: Second two bytes new Cycle Count Y
//! P2: New cycle count Z //! P2: New cycle count Z
static constexpr Event cycleCountersSet = event::makeEvent( static constexpr Event cycleCountersSet =
SUBSYSTEM_ID::MGM_RM3100, 0x01, severity::INFO); event::makeEvent(SUBSYSTEM_ID::MGM_RM3100, 0x01, severity::INFO);
MgmRM3100Handler(object_id_t objectId, object_id_t deviceCommunication, MgmRM3100Handler(object_id_t objectId, object_id_t deviceCommunication, CookieIF *comCookie,
CookieIF* comCookie, uint32_t transitionDelay); uint32_t transitionDelay);
virtual ~MgmRM3100Handler(); virtual ~MgmRM3100Handler();
/** /**
* Configure device handler to go to normal mode after startup immediately * Configure device handler to go to normal mode after startup immediately
* @param enable * @param enable
*/ */
void setToGoToNormalMode(bool enable); void setToGoToNormalMode(bool enable);
protected: protected:
/* DeviceHandlerBase overrides */
ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override;
void doStartUp() override;
void doShutDown() override;
ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData,
size_t commandDataLen) override;
ReturnValue_t scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId,
size_t *foundLen) override;
ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override;
/* DeviceHandlerBase overrides */ void fillCommandAndReplyMap() override;
ReturnValue_t buildTransitionDeviceCommand( void modeChanged(void) override;
DeviceCommandId_t *id) override; virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to) override;
void doStartUp() override; ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
void doShutDown() override; LocalDataPoolManager &poolManager) override;
ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData, size_t commandDataLen) override;
ReturnValue_t scanForReply(const uint8_t *start, size_t len,
DeviceCommandId_t *foundId, size_t *foundLen) override;
ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override;
void fillCommandAndReplyMap() override; private:
void modeChanged(void) override; enum class InternalState {
virtual uint32_t getTransitionDelayMs(Mode_t from, Mode_t to) override; NONE,
ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap, CONFIGURE_CMM,
LocalDataPoolManager &poolManager) override; READ_CMM,
// The cycle count states are propably not going to be used because
// the default cycle count will be used.
STATE_CONFIGURE_CYCLE_COUNT,
STATE_READ_CYCLE_COUNT,
STATE_CONFIGURE_TMRC,
STATE_READ_TMRC,
NORMAL
};
InternalState internalState = InternalState::NONE;
bool commandExecuted = false;
RM3100::Rm3100PrimaryDataset primaryDataset;
private: uint8_t commandBuffer[10];
uint8_t commandBufferLen = 0;
enum class InternalState { uint8_t cmmRegValue = RM3100::CMM_VALUE;
NONE, uint8_t tmrcRegValue = RM3100::TMRC_DEFAULT_VALUE;
CONFIGURE_CMM, uint16_t cycleCountRegValueX = RM3100::CYCLE_COUNT_VALUE;
READ_CMM, uint16_t cycleCountRegValueY = RM3100::CYCLE_COUNT_VALUE;
// The cycle count states are propably not going to be used because uint16_t cycleCountRegValueZ = RM3100::CYCLE_COUNT_VALUE;
// the default cycle count will be used. float scaleFactorX = 1.0 / RM3100::DEFAULT_GAIN;
STATE_CONFIGURE_CYCLE_COUNT, float scaleFactorY = 1.0 / RM3100::DEFAULT_GAIN;
STATE_READ_CYCLE_COUNT, float scaleFactorZ = 1.0 / RM3100::DEFAULT_GAIN;
STATE_CONFIGURE_TMRC,
STATE_READ_TMRC,
NORMAL
};
InternalState internalState = InternalState::NONE;
bool commandExecuted = false;
RM3100::Rm3100PrimaryDataset primaryDataset;
uint8_t commandBuffer[10]; bool goToNormalModeAtStartup = false;
uint8_t commandBufferLen = 0; uint32_t transitionDelay;
uint8_t cmmRegValue = RM3100::CMM_VALUE; ReturnValue_t handleCycleCountConfigCommand(DeviceCommandId_t deviceCommand,
uint8_t tmrcRegValue = RM3100::TMRC_DEFAULT_VALUE; const uint8_t *commandData, size_t commandDataLen);
uint16_t cycleCountRegValueX = RM3100::CYCLE_COUNT_VALUE; ReturnValue_t handleCycleCommand(bool oneCycleValue, const uint8_t *commandData,
uint16_t cycleCountRegValueY = RM3100::CYCLE_COUNT_VALUE; size_t commandDataLen);
uint16_t cycleCountRegValueZ = RM3100::CYCLE_COUNT_VALUE;
float scaleFactorX = 1.0 / RM3100::DEFAULT_GAIN;
float scaleFactorY = 1.0 / RM3100::DEFAULT_GAIN;
float scaleFactorZ = 1.0 / RM3100::DEFAULT_GAIN;
bool goToNormalModeAtStartup = false; ReturnValue_t handleTmrcConfigCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData,
uint32_t transitionDelay; size_t commandDataLen);
ReturnValue_t handleCycleCountConfigCommand(DeviceCommandId_t deviceCommand, ReturnValue_t handleDataReadout(const uint8_t *packet);
const uint8_t *commandData,size_t commandDataLen);
ReturnValue_t handleCycleCommand(bool oneCycleValue,
const uint8_t *commandData, size_t commandDataLen);
ReturnValue_t handleTmrcConfigCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData,size_t commandDataLen);
ReturnValue_t handleDataReadout(const uint8_t* packet);
#if FSFW_HAL_RM3100_MGM_DEBUG == 1 #if FSFW_HAL_RM3100_MGM_DEBUG == 1
PeriodicOperationDivider* debugDivider; PeriodicOperationDivider *debugDivider;
#endif #endif
}; };

View File

@ -3,6 +3,7 @@
#include <fsfw/datapoollocal/StaticLocalDataSet.h> #include <fsfw/datapoollocal/StaticLocalDataSet.h>
#include <fsfw/devicehandlers/DeviceHandlerIF.h> #include <fsfw/devicehandlers/DeviceHandlerIF.h>
#include <cstdint> #include <cstdint>
namespace L3GD20H { namespace L3GD20H {
@ -36,8 +37,8 @@ static constexpr uint8_t SET_Z_ENABLE = 1 << 2;
static constexpr uint8_t SET_X_ENABLE = 1 << 1; static constexpr uint8_t SET_X_ENABLE = 1 << 1;
static constexpr uint8_t SET_Y_ENABLE = 1; static constexpr uint8_t SET_Y_ENABLE = 1;
static constexpr uint8_t CTRL_REG_1_VAL = SET_POWER_NORMAL_MODE | SET_Z_ENABLE | static constexpr uint8_t CTRL_REG_1_VAL =
SET_Y_ENABLE | SET_X_ENABLE; SET_POWER_NORMAL_MODE | SET_Z_ENABLE | SET_Y_ENABLE | SET_X_ENABLE;
/* Register 2 */ /* Register 2 */
static constexpr uint8_t EXTERNAL_EDGE_ENB = 1 << 7; static constexpr uint8_t EXTERNAL_EDGE_ENB = 1 << 7;
@ -104,40 +105,29 @@ static constexpr DeviceCommandId_t READ_CTRL_REGS = 2;
static constexpr uint32_t GYRO_DATASET_ID = READ_REGS; static constexpr uint32_t GYRO_DATASET_ID = READ_REGS;
enum GyroPoolIds: lp_id_t { enum GyroPoolIds : lp_id_t { ANG_VELOC_X, ANG_VELOC_Y, ANG_VELOC_Z, TEMPERATURE };
ANG_VELOC_X,
ANG_VELOC_Y, } // namespace L3GD20H
ANG_VELOC_Z,
TEMPERATURE class GyroPrimaryDataset : public StaticLocalDataSet<5> {
public:
/** Constructor for data users like controllers */
GyroPrimaryDataset(object_id_t mgmId)
: StaticLocalDataSet(sid_t(mgmId, L3GD20H::GYRO_DATASET_ID)) {
setAllVariablesReadOnly();
}
/* Angular velocities in degrees per second (DPS) */
lp_var_t<float> angVelocX = lp_var_t<float>(sid.objectId, L3GD20H::ANG_VELOC_X, this);
lp_var_t<float> angVelocY = lp_var_t<float>(sid.objectId, L3GD20H::ANG_VELOC_Y, this);
lp_var_t<float> angVelocZ = lp_var_t<float>(sid.objectId, L3GD20H::ANG_VELOC_Z, this);
lp_var_t<float> temperature = lp_var_t<float>(sid.objectId, L3GD20H::TEMPERATURE, this);
private:
friend class GyroHandlerL3GD20H;
/** Constructor for the data creator */
GyroPrimaryDataset(HasLocalDataPoolIF* hkOwner)
: StaticLocalDataSet(hkOwner, L3GD20H::GYRO_DATASET_ID) {}
}; };
}
class GyroPrimaryDataset: public StaticLocalDataSet<5> {
public:
/** Constructor for data users like controllers */
GyroPrimaryDataset(object_id_t mgmId):
StaticLocalDataSet(sid_t(mgmId, L3GD20H::GYRO_DATASET_ID)) {
setAllVariablesReadOnly();
}
/* Angular velocities in degrees per second (DPS) */
lp_var_t<float> angVelocX = lp_var_t<float>(sid.objectId,
L3GD20H::ANG_VELOC_X, this);
lp_var_t<float> angVelocY = lp_var_t<float>(sid.objectId,
L3GD20H::ANG_VELOC_Y, this);
lp_var_t<float> angVelocZ = lp_var_t<float>(sid.objectId,
L3GD20H::ANG_VELOC_Z, this);
lp_var_t<float> temperature = lp_var_t<float>(sid.objectId,
L3GD20H::TEMPERATURE, this);
private:
friend class GyroHandlerL3GD20H;
/** Constructor for the data creator */
GyroPrimaryDataset(HasLocalDataPoolIF* hkOwner):
StaticLocalDataSet(hkOwner, L3GD20H::GYRO_DATASET_ID) {}
};
#endif /* MISSION_DEVICES_DEVICEDEFINITIONS_GYROL3GD20DEFINITIONS_H_ */ #endif /* MISSION_DEVICES_DEVICEDEFINITIONS_GYROL3GD20DEFINITIONS_H_ */

View File

@ -1,26 +1,18 @@
#ifndef MISSION_DEVICES_DEVICEDEFINITIONS_MGMLIS3HANDLERDEFS_H_ #ifndef MISSION_DEVICES_DEVICEDEFINITIONS_MGMLIS3HANDLERDEFS_H_
#define MISSION_DEVICES_DEVICEDEFINITIONS_MGMLIS3HANDLERDEFS_H_ #define MISSION_DEVICES_DEVICEDEFINITIONS_MGMLIS3HANDLERDEFS_H_
#include <fsfw/datapoollocal/StaticLocalDataSet.h>
#include <fsfw/datapoollocal/LocalPoolVariable.h> #include <fsfw/datapoollocal/LocalPoolVariable.h>
#include <fsfw/datapoollocal/StaticLocalDataSet.h>
#include <fsfw/devicehandlers/DeviceHandlerIF.h> #include <fsfw/devicehandlers/DeviceHandlerIF.h>
#include <cstdint> #include <cstdint>
namespace MGMLIS3MDL { namespace MGMLIS3MDL {
enum Set { enum Set { ON, OFF };
ON, OFF enum OpMode { LOW, MEDIUM, HIGH, ULTRA };
};
enum OpMode {
LOW, MEDIUM, HIGH, ULTRA
};
enum Sensitivies: uint8_t { enum Sensitivies : uint8_t { GAUSS_4 = 4, GAUSS_8 = 8, GAUSS_12 = 12, GAUSS_16 = 16 };
GAUSS_4 = 4,
GAUSS_8 = 8,
GAUSS_12 = 12,
GAUSS_16 = 16
};
/* Actually 15, we just round up a bit */ /* Actually 15, we just round up a bit */
static constexpr size_t MAX_BUFFER_SIZE = 16; static constexpr size_t MAX_BUFFER_SIZE = 16;
@ -54,7 +46,7 @@ static const uint8_t SETUP_REPLY_LEN = 6;
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/* Register adress returns identifier of device with default 0b00111101 */ /* Register adress returns identifier of device with default 0b00111101 */
static const uint8_t IDENTIFY_DEVICE_REG_ADDR = 0b00001111; static const uint8_t IDENTIFY_DEVICE_REG_ADDR = 0b00001111;
static const uint8_t DEVICE_ID = 0b00111101; // Identifier for Device static const uint8_t DEVICE_ID = 0b00111101; // Identifier for Device
/* Register adress to access register 1 */ /* Register adress to access register 1 */
static const uint8_t CTRL_REG1 = 0b00100000; static const uint8_t CTRL_REG1 = 0b00100000;
@ -105,74 +97,67 @@ static const uint8_t RW_BIT = 7;
static const uint8_t MS_BIT = 6; static const uint8_t MS_BIT = 6;
/* CTRL_REG1 bits */ /* CTRL_REG1 bits */
static const uint8_t ST = 0; // Self test enable bit, enabled = 1 static const uint8_t ST = 0; // Self test enable bit, enabled = 1
// Enable rates higher than 80 Hz enabled = 1 // Enable rates higher than 80 Hz enabled = 1
static const uint8_t FAST_ODR = 1; static const uint8_t FAST_ODR = 1;
static const uint8_t DO0 = 2; // Output data rate bit 2 static const uint8_t DO0 = 2; // Output data rate bit 2
static const uint8_t DO1 = 3; // Output data rate bit 3 static const uint8_t DO1 = 3; // Output data rate bit 3
static const uint8_t DO2 = 4; // Output data rate bit 4 static const uint8_t DO2 = 4; // Output data rate bit 4
static const uint8_t OM0 = 5; // XY operating mode bit 5 static const uint8_t OM0 = 5; // XY operating mode bit 5
static const uint8_t OM1 = 6; // XY operating mode bit 6 static const uint8_t OM1 = 6; // XY operating mode bit 6
static const uint8_t TEMP_EN = 7; // Temperature sensor enable enabled = 1 static const uint8_t TEMP_EN = 7; // Temperature sensor enable enabled = 1
static const uint8_t CTRL_REG1_DEFAULT = (1 << TEMP_EN) | (1 << OM1) | static const uint8_t CTRL_REG1_DEFAULT =
(1 << DO0) | (1 << DO1) | (1 << DO2); (1 << TEMP_EN) | (1 << OM1) | (1 << DO0) | (1 << DO1) | (1 << DO2);
/* CTRL_REG2 bits */ /* CTRL_REG2 bits */
//reset configuration registers and user registers // reset configuration registers and user registers
static const uint8_t SOFT_RST = 2; static const uint8_t SOFT_RST = 2;
static const uint8_t REBOOT = 3; //reboot memory content static const uint8_t REBOOT = 3; // reboot memory content
static const uint8_t FSO = 5; //full-scale selection bit 5 static const uint8_t FSO = 5; // full-scale selection bit 5
static const uint8_t FS1 = 6; //full-scale selection bit 6 static const uint8_t FS1 = 6; // full-scale selection bit 6
static const uint8_t CTRL_REG2_DEFAULT = 0; static const uint8_t CTRL_REG2_DEFAULT = 0;
/* CTRL_REG3 bits */ /* CTRL_REG3 bits */
static const uint8_t MD0 = 0; //Operating mode bit 0 static const uint8_t MD0 = 0; // Operating mode bit 0
static const uint8_t MD1 = 1; //Operating mode bit 1 static const uint8_t MD1 = 1; // Operating mode bit 1
//SPI serial interface mode selection enabled = 3-wire-mode // SPI serial interface mode selection enabled = 3-wire-mode
static const uint8_t SIM = 2; static const uint8_t SIM = 2;
static const uint8_t LP = 5; //low-power mode static const uint8_t LP = 5; // low-power mode
static const uint8_t CTRL_REG3_DEFAULT = 0; static const uint8_t CTRL_REG3_DEFAULT = 0;
/* CTRL_REG4 bits */ /* CTRL_REG4 bits */
//big/little endian data selection enabled = MSb at lower adress // big/little endian data selection enabled = MSb at lower adress
static const uint8_t BLE = 1; static const uint8_t BLE = 1;
static const uint8_t OMZ0 = 2; //Z operating mode bit 2 static const uint8_t OMZ0 = 2; // Z operating mode bit 2
static const uint8_t OMZ1 = 3; //Z operating mode bit 3 static const uint8_t OMZ1 = 3; // Z operating mode bit 3
static const uint8_t CTRL_REG4_DEFAULT = (1 << OMZ1); static const uint8_t CTRL_REG4_DEFAULT = (1 << OMZ1);
/* CTRL_REG5 bits */ /* CTRL_REG5 bits */
static const uint8_t BDU = 6; //Block data update static const uint8_t BDU = 6; // Block data update
static const uint8_t FAST_READ = 7; //Fast read enabled = 1 static const uint8_t FAST_READ = 7; // Fast read enabled = 1
static const uint8_t CTRL_REG5_DEFAULT = 0; static const uint8_t CTRL_REG5_DEFAULT = 0;
static const uint32_t MGM_DATA_SET_ID = READ_CONFIG_AND_DATA; static const uint32_t MGM_DATA_SET_ID = READ_CONFIG_AND_DATA;
enum MgmPoolIds: lp_id_t { enum MgmPoolIds : lp_id_t {
FIELD_STRENGTH_X, FIELD_STRENGTH_X,
FIELD_STRENGTH_Y, FIELD_STRENGTH_Y,
FIELD_STRENGTH_Z, FIELD_STRENGTH_Z,
TEMPERATURE_CELCIUS TEMPERATURE_CELCIUS
}; };
class MgmPrimaryDataset: public StaticLocalDataSet<4> { class MgmPrimaryDataset : public StaticLocalDataSet<4> {
public: public:
MgmPrimaryDataset(HasLocalDataPoolIF* hkOwner): MgmPrimaryDataset(HasLocalDataPoolIF* hkOwner) : StaticLocalDataSet(hkOwner, MGM_DATA_SET_ID) {}
StaticLocalDataSet(hkOwner, MGM_DATA_SET_ID) {}
MgmPrimaryDataset(object_id_t mgmId): MgmPrimaryDataset(object_id_t mgmId) : StaticLocalDataSet(sid_t(mgmId, MGM_DATA_SET_ID)) {}
StaticLocalDataSet(sid_t(mgmId, MGM_DATA_SET_ID)) {}
lp_var_t<float> fieldStrengthX = lp_var_t<float>(sid.objectId, lp_var_t<float> fieldStrengthX = lp_var_t<float>(sid.objectId, FIELD_STRENGTH_X, this);
FIELD_STRENGTH_X, this); lp_var_t<float> fieldStrengthY = lp_var_t<float>(sid.objectId, FIELD_STRENGTH_Y, this);
lp_var_t<float> fieldStrengthY = lp_var_t<float>(sid.objectId, lp_var_t<float> fieldStrengthZ = lp_var_t<float>(sid.objectId, FIELD_STRENGTH_Z, this);
FIELD_STRENGTH_Y, this); lp_var_t<float> temperature = lp_var_t<float>(sid.objectId, TEMPERATURE_CELCIUS, this);
lp_var_t<float> fieldStrengthZ = lp_var_t<float>(sid.objectId,
FIELD_STRENGTH_Z, this);
lp_var_t<float> temperature = lp_var_t<float>(sid.objectId,
TEMPERATURE_CELCIUS, this);
}; };
} } // namespace MGMLIS3MDL
#endif /* MISSION_DEVICES_DEVICEDEFINITIONS_MGMLIS3HANDLERDEFS_H_ */ #endif /* MISSION_DEVICES_DEVICEDEFINITIONS_MGMLIS3HANDLERDEFS_H_ */

View File

@ -1,10 +1,11 @@
#ifndef MISSION_DEVICES_DEVICEDEFINITIONS_MGMHANDLERRM3100DEFINITIONS_H_ #ifndef MISSION_DEVICES_DEVICEDEFINITIONS_MGMHANDLERRM3100DEFINITIONS_H_
#define MISSION_DEVICES_DEVICEDEFINITIONS_MGMHANDLERRM3100DEFINITIONS_H_ #define MISSION_DEVICES_DEVICEDEFINITIONS_MGMHANDLERRM3100DEFINITIONS_H_
#include <fsfw/datapoollocal/StaticLocalDataSet.h>
#include <fsfw/datapoollocal/LocalPoolVariable.h> #include <fsfw/datapoollocal/LocalPoolVariable.h>
#include <fsfw/datapoollocal/StaticLocalDataSet.h>
#include <fsfw/devicehandlers/DeviceHandlerIF.h> #include <fsfw/devicehandlers/DeviceHandlerIF.h>
#include <fsfw/serialize/SerialLinkedListAdapter.h> #include <fsfw/serialize/SerialLinkedListAdapter.h>
#include <cstdint> #include <cstdint>
namespace RM3100 { namespace RM3100 {
@ -24,8 +25,8 @@ static constexpr uint8_t SET_CMM_DRDM = 1 << 2;
static constexpr uint8_t SET_CMM_START = 1; static constexpr uint8_t SET_CMM_START = 1;
static constexpr uint8_t CMM_REGISTER = 0x01; static constexpr uint8_t CMM_REGISTER = 0x01;
static constexpr uint8_t CMM_VALUE = SET_CMM_CMZ | SET_CMM_CMY | SET_CMM_CMX | static constexpr uint8_t CMM_VALUE =
SET_CMM_DRDM | SET_CMM_START; SET_CMM_CMZ | SET_CMM_CMY | SET_CMM_CMX | SET_CMM_DRDM | SET_CMM_START;
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* Cycle count register */ /* Cycle count register */
@ -33,8 +34,7 @@ static constexpr uint8_t CMM_VALUE = SET_CMM_CMZ | SET_CMM_CMY | SET_CMM_CMX |
// Default value (200) // Default value (200)
static constexpr uint8_t CYCLE_COUNT_VALUE = 0xC8; static constexpr uint8_t CYCLE_COUNT_VALUE = 0xC8;
static constexpr float DEFAULT_GAIN = static_cast<float>(CYCLE_COUNT_VALUE) / static constexpr float DEFAULT_GAIN = static_cast<float>(CYCLE_COUNT_VALUE) / 100 * 38;
100 * 38;
static constexpr uint8_t CYCLE_COUNT_START_REGISTER = 0x04; static constexpr uint8_t CYCLE_COUNT_START_REGISTER = 0x04;
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -67,66 +67,58 @@ static constexpr DeviceCommandId_t READ_TMRC = 4;
static constexpr DeviceCommandId_t CONFIGURE_CYCLE_COUNT = 5; static constexpr DeviceCommandId_t CONFIGURE_CYCLE_COUNT = 5;
static constexpr DeviceCommandId_t READ_CYCLE_COUNT = 6; static constexpr DeviceCommandId_t READ_CYCLE_COUNT = 6;
class CycleCountCommand: public SerialLinkedListAdapter<SerializeIF> { class CycleCountCommand : public SerialLinkedListAdapter<SerializeIF> {
public: public:
CycleCountCommand(bool oneCycleCount = true): oneCycleCount(oneCycleCount) { CycleCountCommand(bool oneCycleCount = true) : oneCycleCount(oneCycleCount) {
setLinks(oneCycleCount); setLinks(oneCycleCount);
} }
ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
Endianness streamEndianness) override { Endianness streamEndianness) override {
ReturnValue_t result = SerialLinkedListAdapter::deSerialize(buffer, ReturnValue_t result = SerialLinkedListAdapter::deSerialize(buffer, size, streamEndianness);
size, streamEndianness); if (oneCycleCount) {
if(oneCycleCount) { cycleCountY = cycleCountX;
cycleCountY = cycleCountX; cycleCountZ = cycleCountX;
cycleCountZ = cycleCountX; }
} return result;
return result; }
}
SerializeElement<uint16_t> cycleCountX; SerializeElement<uint16_t> cycleCountX;
SerializeElement<uint16_t> cycleCountY; SerializeElement<uint16_t> cycleCountY;
SerializeElement<uint16_t> cycleCountZ; SerializeElement<uint16_t> cycleCountZ;
private: private:
void setLinks(bool oneCycleCount) { void setLinks(bool oneCycleCount) {
setStart(&cycleCountX); setStart(&cycleCountX);
if(not oneCycleCount) { if (not oneCycleCount) {
cycleCountX.setNext(&cycleCountY); cycleCountX.setNext(&cycleCountY);
cycleCountY.setNext(&cycleCountZ); cycleCountY.setNext(&cycleCountZ);
} }
} }
bool oneCycleCount; bool oneCycleCount;
}; };
static constexpr uint32_t MGM_DATASET_ID = READ_DATA; static constexpr uint32_t MGM_DATASET_ID = READ_DATA;
enum MgmPoolIds: lp_id_t { enum MgmPoolIds : lp_id_t {
FIELD_STRENGTH_X, FIELD_STRENGTH_X,
FIELD_STRENGTH_Y, FIELD_STRENGTH_Y,
FIELD_STRENGTH_Z, FIELD_STRENGTH_Z,
}; };
class Rm3100PrimaryDataset: public StaticLocalDataSet<3> { class Rm3100PrimaryDataset : public StaticLocalDataSet<3> {
public: public:
Rm3100PrimaryDataset(HasLocalDataPoolIF* hkOwner): Rm3100PrimaryDataset(HasLocalDataPoolIF* hkOwner) : StaticLocalDataSet(hkOwner, MGM_DATASET_ID) {}
StaticLocalDataSet(hkOwner, MGM_DATASET_ID) {}
Rm3100PrimaryDataset(object_id_t mgmId): Rm3100PrimaryDataset(object_id_t mgmId) : StaticLocalDataSet(sid_t(mgmId, MGM_DATASET_ID)) {}
StaticLocalDataSet(sid_t(mgmId, MGM_DATASET_ID)) {}
// Field strengths in micro Tesla. // Field strengths in micro Tesla.
lp_var_t<float> fieldStrengthX = lp_var_t<float>(sid.objectId, lp_var_t<float> fieldStrengthX = lp_var_t<float>(sid.objectId, FIELD_STRENGTH_X, this);
FIELD_STRENGTH_X, this); lp_var_t<float> fieldStrengthY = lp_var_t<float>(sid.objectId, FIELD_STRENGTH_Y, this);
lp_var_t<float> fieldStrengthY = lp_var_t<float>(sid.objectId, lp_var_t<float> fieldStrengthZ = lp_var_t<float>(sid.objectId, FIELD_STRENGTH_Z, this);
FIELD_STRENGTH_Y, this);
lp_var_t<float> fieldStrengthZ = lp_var_t<float>(sid.objectId,
FIELD_STRENGTH_Z, this);
}; };
} } // namespace RM3100
#endif /* MISSION_DEVICES_DEVICEDEFINITIONS_MGMHANDLERRM3100DEFINITIONS_H_ */ #endif /* MISSION_DEVICES_DEVICEDEFINITIONS_MGMHANDLERRM3100DEFINITIONS_H_ */

View File

@ -1,213 +1,207 @@
#include "CommandExecutor.h" #include "CommandExecutor.h"
#include "fsfw/serviceinterface.h"
#include "fsfw/container/SimpleRingBuffer.h"
#include "fsfw/container/DynamicFIFO.h"
#include <unistd.h> #include <unistd.h>
#include <cstring> #include <cstring>
CommandExecutor::CommandExecutor(const size_t maxSize): #include "fsfw/container/DynamicFIFO.h"
readVec(maxSize) { #include "fsfw/container/SimpleRingBuffer.h"
waiter.events = POLLIN; #include "fsfw/serviceinterface.h"
CommandExecutor::CommandExecutor(const size_t maxSize) : readVec(maxSize) {
waiter.events = POLLIN;
} }
ReturnValue_t CommandExecutor::load(std::string command, bool blocking, bool printOutput) { ReturnValue_t CommandExecutor::load(std::string command, bool blocking, bool printOutput) {
if(state == States::PENDING) { if (state == States::PENDING) {
return COMMAND_PENDING; return COMMAND_PENDING;
} }
currentCmd = command; currentCmd = command;
this->blocking = blocking; this->blocking = blocking;
this->printOutput = printOutput; this->printOutput = printOutput;
if(state == States::IDLE) { if (state == States::IDLE) {
state = States::COMMAND_LOADED; state = States::COMMAND_LOADED;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t CommandExecutor::execute() { ReturnValue_t CommandExecutor::execute() {
if(state == States::IDLE) { if (state == States::IDLE) {
return NO_COMMAND_LOADED_OR_PENDING; return NO_COMMAND_LOADED_OR_PENDING;
} } else if (state == States::PENDING) {
else if(state == States::PENDING) { return COMMAND_PENDING;
return COMMAND_PENDING; }
} currentCmdFile = popen(currentCmd.c_str(), "r");
currentCmdFile = popen(currentCmd.c_str(), "r"); if (currentCmdFile == nullptr) {
if(currentCmdFile == nullptr) { lastError = errno;
lastError = errno; return HasReturnvaluesIF::RETURN_FAILED;
return HasReturnvaluesIF::RETURN_FAILED; }
} if (blocking) {
if(blocking) { ReturnValue_t result = executeBlocking();
ReturnValue_t result = executeBlocking(); state = States::IDLE;
state = States::IDLE; return result;
return result; } else {
} currentFd = fileno(currentCmdFile);
else { waiter.fd = currentFd;
currentFd = fileno(currentCmdFile); }
waiter.fd = currentFd; state = States::PENDING;
} return HasReturnvaluesIF::RETURN_OK;
state = States::PENDING;
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t CommandExecutor::close() { ReturnValue_t CommandExecutor::close() {
if(state == States::PENDING) { if (state == States::PENDING) {
// Attempt to close process, irrespective of if it is running or not // Attempt to close process, irrespective of if it is running or not
if(currentCmdFile != nullptr) { if (currentCmdFile != nullptr) {
pclose(currentCmdFile); pclose(currentCmdFile);
}
} }
return HasReturnvaluesIF::RETURN_OK; }
return HasReturnvaluesIF::RETURN_OK;
} }
void CommandExecutor::printLastError(std::string funcName) const { void CommandExecutor::printLastError(std::string funcName) const {
if(lastError != 0) { if (lastError != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << funcName << " pclose failed with code " << lastError << ": " << sif::warning << funcName << " pclose failed with code " << lastError << ": "
strerror(lastError) << std::endl; << strerror(lastError) << std::endl;
#else #else
sif::printError("%s pclose failed with code %d: %s\n", sif::printError("%s pclose failed with code %d: %s\n", funcName, lastError,
funcName, lastError, strerror(lastError)); strerror(lastError));
#endif #endif
} }
} }
void CommandExecutor::setRingBuffer(SimpleRingBuffer *ringBuffer, void CommandExecutor::setRingBuffer(SimpleRingBuffer* ringBuffer,
DynamicFIFO<uint16_t>* sizesFifo) { DynamicFIFO<uint16_t>* sizesFifo) {
this->ringBuffer = ringBuffer; this->ringBuffer = ringBuffer;
this->sizesFifo = sizesFifo; this->sizesFifo = sizesFifo;
} }
ReturnValue_t CommandExecutor::check(bool& replyReceived) { ReturnValue_t CommandExecutor::check(bool& replyReceived) {
if(blocking) { if (blocking) {
return HasReturnvaluesIF::RETURN_OK;
}
switch(state) {
case(States::IDLE):
case(States::COMMAND_LOADED): {
return NO_COMMAND_LOADED_OR_PENDING;
}
case(States::PENDING): {
break;
}
}
int result = poll(&waiter, 1, 0);
switch(result) {
case(0): {
return HasReturnvaluesIF::RETURN_OK;
break;
}
case(1): {
if (waiter.revents & POLLIN) {
ssize_t readBytes = read(currentFd, readVec.data(), readVec.size());
if(readBytes == 0) {
// Should not happen
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandExecutor::check: No bytes read "
"after poll event.." << std::endl;
#else
sif::printWarning("CommandExecutor::check: No bytes read after poll event..\n");
#endif
break;
}
else if(readBytes > 0) {
replyReceived = true;
if(printOutput) {
// It is assumed the command output is line terminated
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << currentCmd << " | " << readVec.data();
#else
sif::printInfo("%s | %s", currentCmd, readVec.data());
#endif
}
if(ringBuffer != nullptr) {
ringBuffer->writeData(reinterpret_cast<const uint8_t*>(
readVec.data()), readBytes);
}
if(sizesFifo != nullptr) {
if(not sizesFifo->full()) {
sizesFifo->insert(readBytes);
}
}
}
else {
// Should also not happen
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandExecutor::check: Error " << errno << ": " <<
strerror(errno) << std::endl;
#else
sif::printWarning("CommandExecutor::check: Error %d: %s\n", errno, strerror(errno));
#endif
}
}
if(waiter.revents & POLLERR) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandExecuter::check: Poll error" << std::endl;
#else
sif::printWarning("CommandExecuter::check: Poll error\n");
#endif
return COMMAND_ERROR;
}
if(waiter.revents & POLLHUP) {
result = pclose(currentCmdFile);
ReturnValue_t retval = EXECUTION_FINISHED;
if(result != 0) {
lastError = result;
retval = HasReturnvaluesIF::RETURN_FAILED;
}
state = States::IDLE;
currentCmdFile = nullptr;
currentFd = 0;
return retval;
}
break;
}
}
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
}
switch (state) {
case (States::IDLE):
case (States::COMMAND_LOADED): {
return NO_COMMAND_LOADED_OR_PENDING;
}
case (States::PENDING): {
break;
}
}
int result = poll(&waiter, 1, 0);
switch (result) {
case (0): {
return HasReturnvaluesIF::RETURN_OK;
break;
}
case (1): {
if (waiter.revents & POLLIN) {
ssize_t readBytes = read(currentFd, readVec.data(), readVec.size());
if (readBytes == 0) {
// Should not happen
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandExecutor::check: No bytes read "
"after poll event.."
<< std::endl;
#else
sif::printWarning("CommandExecutor::check: No bytes read after poll event..\n");
#endif
break;
} else if (readBytes > 0) {
replyReceived = true;
if (printOutput) {
// It is assumed the command output is line terminated
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << currentCmd << " | " << readVec.data();
#else
sif::printInfo("%s | %s", currentCmd, readVec.data());
#endif
}
if (ringBuffer != nullptr) {
ringBuffer->writeData(reinterpret_cast<const uint8_t*>(readVec.data()), readBytes);
}
if (sizesFifo != nullptr) {
if (not sizesFifo->full()) {
sizesFifo->insert(readBytes);
}
}
} else {
// Should also not happen
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandExecutor::check: Error " << errno << ": " << strerror(errno)
<< std::endl;
#else
sif::printWarning("CommandExecutor::check: Error %d: %s\n", errno, strerror(errno));
#endif
}
}
if (waiter.revents & POLLERR) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandExecuter::check: Poll error" << std::endl;
#else
sif::printWarning("CommandExecuter::check: Poll error\n");
#endif
return COMMAND_ERROR;
}
if (waiter.revents & POLLHUP) {
result = pclose(currentCmdFile);
ReturnValue_t retval = EXECUTION_FINISHED;
if (result != 0) {
lastError = result;
retval = HasReturnvaluesIF::RETURN_FAILED;
}
state = States::IDLE;
currentCmdFile = nullptr;
currentFd = 0;
return retval;
}
break;
}
}
return HasReturnvaluesIF::RETURN_OK;
} }
void CommandExecutor::reset() { void CommandExecutor::reset() {
CommandExecutor::close(); CommandExecutor::close();
currentCmdFile = nullptr; currentCmdFile = nullptr;
currentFd = 0; currentFd = 0;
state = States::IDLE; state = States::IDLE;
} }
int CommandExecutor::getLastError() const { int CommandExecutor::getLastError() const {
// See: https://stackoverflow.com/questions/808541/any-benefit-in-using-wexitstatus-macro-in-c-over-division-by-256-on-exit-statu // See:
return WEXITSTATUS(this->lastError); // https://stackoverflow.com/questions/808541/any-benefit-in-using-wexitstatus-macro-in-c-over-division-by-256-on-exit-statu
return WEXITSTATUS(this->lastError);
} }
CommandExecutor::States CommandExecutor::getCurrentState() const { CommandExecutor::States CommandExecutor::getCurrentState() const { return state; }
return state;
}
ReturnValue_t CommandExecutor::executeBlocking() { ReturnValue_t CommandExecutor::executeBlocking() {
while(fgets(readVec.data(), readVec.size(), currentCmdFile) != nullptr) { while (fgets(readVec.data(), readVec.size(), currentCmdFile) != nullptr) {
std::string output(readVec.data()); std::string output(readVec.data());
if(printOutput) { if (printOutput) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << currentCmd << " | " << output; sif::info << currentCmd << " | " << output;
#else #else
sif::printInfo("%s | %s", currentCmd, output); sif::printInfo("%s | %s", currentCmd, output);
#endif #endif
}
if(ringBuffer != nullptr) {
ringBuffer->writeData(reinterpret_cast<const uint8_t*>(output.data()), output.size());
}
if(sizesFifo != nullptr) {
if(not sizesFifo->full()) {
sizesFifo->insert(output.size());
}
}
} }
int result = pclose(currentCmdFile); if (ringBuffer != nullptr) {
if(result != 0) { ringBuffer->writeData(reinterpret_cast<const uint8_t*>(output.data()), output.size());
lastError = result;
return HasReturnvaluesIF::RETURN_FAILED;
} }
return HasReturnvaluesIF::RETURN_OK; if (sizesFifo != nullptr) {
if (not sizesFifo->full()) {
sizesFifo->insert(output.size());
}
}
}
int result = pclose(currentCmdFile);
if (result != 0) {
lastError = result;
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -1,16 +1,17 @@
#ifndef FSFW_SRC_FSFW_OSAL_LINUX_COMMANDEXECUTOR_H_ #ifndef FSFW_SRC_FSFW_OSAL_LINUX_COMMANDEXECUTOR_H_
#define FSFW_SRC_FSFW_OSAL_LINUX_COMMANDEXECUTOR_H_ #define FSFW_SRC_FSFW_OSAL_LINUX_COMMANDEXECUTOR_H_
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "fsfw/returnvalues/FwClassIds.h"
#include <poll.h> #include <poll.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include "fsfw/returnvalues/FwClassIds.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
class SimpleRingBuffer; class SimpleRingBuffer;
template <typename T> class DynamicFIFO; template <typename T>
class DynamicFIFO;
/** /**
* @brief Helper class to execute shell commands in blocking and non-blocking mode * @brief Helper class to execute shell commands in blocking and non-blocking mode
@ -24,112 +25,105 @@ template <typename T> class DynamicFIFO;
* has finished * has finished
*/ */
class CommandExecutor { class CommandExecutor {
public: public:
enum class States { enum class States { IDLE, COMMAND_LOADED, PENDING };
IDLE,
COMMAND_LOADED,
PENDING
};
static constexpr uint8_t CLASS_ID = CLASS_ID::LINUX_OSAL; static constexpr uint8_t CLASS_ID = CLASS_ID::LINUX_OSAL;
//! [EXPORT] : [COMMENT] Execution of the current command has finished //! [EXPORT] : [COMMENT] Execution of the current command has finished
static constexpr ReturnValue_t EXECUTION_FINISHED = static constexpr ReturnValue_t EXECUTION_FINISHED =
HasReturnvaluesIF::makeReturnCode(CLASS_ID, 0); HasReturnvaluesIF::makeReturnCode(CLASS_ID, 0);
//! [EXPORT] : [COMMENT] Command is pending. This will also be returned if the user tries //! [EXPORT] : [COMMENT] Command is pending. This will also be returned if the user tries
//! to load another command but a command is still pending //! to load another command but a command is still pending
static constexpr ReturnValue_t COMMAND_PENDING = static constexpr ReturnValue_t COMMAND_PENDING = HasReturnvaluesIF::makeReturnCode(CLASS_ID, 1);
HasReturnvaluesIF::makeReturnCode(CLASS_ID, 1); //! [EXPORT] : [COMMENT] Some bytes have been read from the executing process
//! [EXPORT] : [COMMENT] Some bytes have been read from the executing process static constexpr ReturnValue_t BYTES_READ = HasReturnvaluesIF::makeReturnCode(CLASS_ID, 2);
static constexpr ReturnValue_t BYTES_READ = //! [EXPORT] : [COMMENT] Command execution failed
HasReturnvaluesIF::makeReturnCode(CLASS_ID, 2); static constexpr ReturnValue_t COMMAND_ERROR = HasReturnvaluesIF::makeReturnCode(CLASS_ID, 3);
//! [EXPORT] : [COMMENT] Command execution failed //! [EXPORT] : [COMMENT]
static constexpr ReturnValue_t COMMAND_ERROR = static constexpr ReturnValue_t NO_COMMAND_LOADED_OR_PENDING =
HasReturnvaluesIF::makeReturnCode(CLASS_ID, 3); HasReturnvaluesIF::makeReturnCode(CLASS_ID, 4);
//! [EXPORT] : [COMMENT] static constexpr ReturnValue_t PCLOSE_CALL_ERROR = HasReturnvaluesIF::makeReturnCode(CLASS_ID, 6);
static constexpr ReturnValue_t NO_COMMAND_LOADED_OR_PENDING =
HasReturnvaluesIF::makeReturnCode(CLASS_ID, 4);
static constexpr ReturnValue_t PCLOSE_CALL_ERROR =
HasReturnvaluesIF::makeReturnCode(CLASS_ID, 6);
/** /**
* Constructor. Is initialized with maximum size of internal buffer to read data from the * Constructor. Is initialized with maximum size of internal buffer to read data from the
* executed process. * executed process.
* @param maxSize * @param maxSize
*/ */
CommandExecutor(const size_t maxSize); CommandExecutor(const size_t maxSize);
/** /**
* Load a new command which should be executed * Load a new command which should be executed
* @param command * @param command
* @param blocking * @param blocking
* @param printOutput * @param printOutput
* @return * @return
*/ */
ReturnValue_t load(std::string command, bool blocking, bool printOutput = true); ReturnValue_t load(std::string command, bool blocking, bool printOutput = true);
/** /**
* Execute the loaded command. * Execute the loaded command.
* @return * @return
* - In blocking mode, it will return RETURN_FAILED if * - In blocking mode, it will return RETURN_FAILED if
* the result of the system call was not 0. The error value can be accessed using * the result of the system call was not 0. The error value can be accessed using
* getLastError * getLastError
* - In non-blocking mode, this call will start * - In non-blocking mode, this call will start
* the execution and then return RETURN_OK * the execution and then return RETURN_OK
*/ */
ReturnValue_t execute(); ReturnValue_t execute();
/** /**
* Only used in non-blocking mode. Checks the currently running command. * Only used in non-blocking mode. Checks the currently running command.
* @param bytesRead Will be set to the number of bytes read, if bytes have been read * @param bytesRead Will be set to the number of bytes read, if bytes have been read
* @return * @return
* - BYTES_READ if bytes have been read from the executing process. It is recommended to call * - BYTES_READ if bytes have been read from the executing process. It is recommended to call
* check again after this * check again after this
* - RETURN_OK execution is pending, but no bytes have been read from the executing process * - RETURN_OK execution is pending, but no bytes have been read from the executing process
* - RETURN_FAILED if execution has failed, error value can be accessed using getLastError * - RETURN_FAILED if execution has failed, error value can be accessed using getLastError
* - EXECUTION_FINISHED if the process was executed successfully * - EXECUTION_FINISHED if the process was executed successfully
* - NO_COMMAND_LOADED_OR_PENDING self-explanatory * - NO_COMMAND_LOADED_OR_PENDING self-explanatory
* - COMMAND_ERROR internal poll error * - COMMAND_ERROR internal poll error
*/ */
ReturnValue_t check(bool& replyReceived); ReturnValue_t check(bool& replyReceived);
/** /**
* Abort the current command. Should normally not be necessary, check can be used to find * Abort the current command. Should normally not be necessary, check can be used to find
* out whether command execution was successful * out whether command execution was successful
* @return RETURN_OK * @return RETURN_OK
*/ */
ReturnValue_t close(); ReturnValue_t close();
States getCurrentState() const; States getCurrentState() const;
int getLastError() const; int getLastError() const;
void printLastError(std::string funcName) const; void printLastError(std::string funcName) const;
/** /**
* Assign a ring buffer and a FIFO which will be filled by the executor with the output * Assign a ring buffer and a FIFO which will be filled by the executor with the output
* read from the started process * read from the started process
* @param ringBuffer * @param ringBuffer
* @param sizesFifo * @param sizesFifo
*/ */
void setRingBuffer(SimpleRingBuffer* ringBuffer, DynamicFIFO<uint16_t>* sizesFifo); void setRingBuffer(SimpleRingBuffer* ringBuffer, DynamicFIFO<uint16_t>* sizesFifo);
/** /**
* Reset the executor. This calls close internally and then reset the state machine so new * Reset the executor. This calls close internally and then reset the state machine so new
* commands can be loaded and executed * commands can be loaded and executed
*/ */
void reset(); void reset();
private:
std::string currentCmd;
bool blocking = true;
FILE* currentCmdFile = nullptr;
int currentFd = 0;
bool printOutput = true;
std::vector<char> readVec;
struct pollfd waiter {};
SimpleRingBuffer* ringBuffer = nullptr;
DynamicFIFO<uint16_t>* sizesFifo = nullptr;
States state = States::IDLE; private:
int lastError = 0; std::string currentCmd;
bool blocking = true;
FILE* currentCmdFile = nullptr;
int currentFd = 0;
bool printOutput = true;
std::vector<char> readVec;
struct pollfd waiter {};
SimpleRingBuffer* ringBuffer = nullptr;
DynamicFIFO<uint16_t>* sizesFifo = nullptr;
ReturnValue_t executeBlocking(); States state = States::IDLE;
int lastError = 0;
ReturnValue_t executeBlocking();
}; };
#endif /* FSFW_SRC_FSFW_OSAL_LINUX_COMMANDEXECUTOR_H_ */ #endif /* FSFW_SRC_FSFW_OSAL_LINUX_COMMANDEXECUTOR_H_ */

View File

@ -1,37 +1,36 @@
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
#include "fsfw_hal/linux/UnixFileGuard.h" #include "fsfw_hal/linux/UnixFileGuard.h"
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
UnixFileGuard::UnixFileGuard(std::string device, int* fileDescriptor, int flags, UnixFileGuard::UnixFileGuard(std::string device, int* fileDescriptor, int flags,
std::string diagnosticPrefix): std::string diagnosticPrefix)
fileDescriptor(fileDescriptor) { : fileDescriptor(fileDescriptor) {
if(fileDescriptor == nullptr) { if (fileDescriptor == nullptr) {
return; return;
} }
*fileDescriptor = open(device.c_str(), flags); *fileDescriptor = open(device.c_str(), flags);
if (*fileDescriptor < 0) { if (*fileDescriptor < 0) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << diagnosticPrefix << ": Opening device failed with error code " << sif::warning << diagnosticPrefix << ": Opening device failed with error code " << errno << ": "
errno << ": " << strerror(errno) << std::endl; << strerror(errno) << std::endl;
#else #else
sif::printWarning("%s: Opening device failed with error code %d: %s\n", sif::printWarning("%s: Opening device failed with error code %d: %s\n", diagnosticPrefix, errno,
diagnosticPrefix, errno, strerror(errno)); strerror(errno));
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
openStatus = OPEN_FILE_FAILED; openStatus = OPEN_FILE_FAILED;
} }
} }
UnixFileGuard::~UnixFileGuard() { UnixFileGuard::~UnixFileGuard() {
if(fileDescriptor != nullptr) { if (fileDescriptor != nullptr) {
close(*fileDescriptor); close(*fileDescriptor);
} }
} }
ReturnValue_t UnixFileGuard::getOpenResult() const { ReturnValue_t UnixFileGuard::getOpenResult() const { return openStatus; }
return openStatus;
}

View File

@ -1,33 +1,30 @@
#ifndef LINUX_UTILITY_UNIXFILEGUARD_H_ #ifndef LINUX_UTILITY_UNIXFILEGUARD_H_
#define LINUX_UTILITY_UNIXFILEGUARD_H_ #define LINUX_UTILITY_UNIXFILEGUARD_H_
#include <fcntl.h>
#include <fsfw/returnvalues/HasReturnvaluesIF.h> #include <fsfw/returnvalues/HasReturnvaluesIF.h>
#include <unistd.h>
#include <string> #include <string>
#include <fcntl.h>
#include <unistd.h>
class UnixFileGuard { class UnixFileGuard {
public: public:
static constexpr int READ_WRITE_FLAG = O_RDWR; static constexpr int READ_WRITE_FLAG = O_RDWR;
static constexpr int READ_ONLY_FLAG = O_RDONLY; static constexpr int READ_ONLY_FLAG = O_RDONLY;
static constexpr int NON_BLOCKING_IO_FLAG = O_NONBLOCK; static constexpr int NON_BLOCKING_IO_FLAG = O_NONBLOCK;
static constexpr ReturnValue_t OPEN_FILE_FAILED = 1; static constexpr ReturnValue_t OPEN_FILE_FAILED = 1;
UnixFileGuard(std::string device, int* fileDescriptor, int flags, UnixFileGuard(std::string device, int* fileDescriptor, int flags,
std::string diagnosticPrefix = ""); std::string diagnosticPrefix = "");
virtual~ UnixFileGuard(); virtual ~UnixFileGuard();
ReturnValue_t getOpenResult() const; ReturnValue_t getOpenResult() const;
private:
int* fileDescriptor = nullptr; private:
ReturnValue_t openStatus = HasReturnvaluesIF::RETURN_OK; int* fileDescriptor = nullptr;
ReturnValue_t openStatus = HasReturnvaluesIF::RETURN_OK;
}; };
#endif /* LINUX_UTILITY_UNIXFILEGUARD_H_ */ #endif /* LINUX_UTILITY_UNIXFILEGUARD_H_ */

View File

@ -1,442 +1,446 @@
#include "LinuxLibgpioIF.h" #include "LinuxLibgpioIF.h"
#include "fsfw_hal/common/gpio/gpioDefinitions.h" #include <gpiod.h>
#include "fsfw_hal/common/gpio/GpioCookie.h" #include <unistd.h>
#include "fsfw/serviceinterface/ServiceInterface.h"
#include <utility> #include <utility>
#include <unistd.h>
#include <gpiod.h>
LinuxLibgpioIF::LinuxLibgpioIF(object_id_t objectId) : SystemObject(objectId) { #include "fsfw/serviceinterface/ServiceInterface.h"
} #include "fsfw_hal/common/gpio/GpioCookie.h"
#include "fsfw_hal/common/gpio/gpioDefinitions.h"
LinuxLibgpioIF::LinuxLibgpioIF(object_id_t objectId) : SystemObject(objectId) {}
LinuxLibgpioIF::~LinuxLibgpioIF() { LinuxLibgpioIF::~LinuxLibgpioIF() {
for(auto& config: gpioMap) { for (auto& config : gpioMap) {
delete(config.second); delete (config.second);
} }
} }
ReturnValue_t LinuxLibgpioIF::addGpios(GpioCookie* gpioCookie) { ReturnValue_t LinuxLibgpioIF::addGpios(GpioCookie* gpioCookie) {
ReturnValue_t result; ReturnValue_t result;
if(gpioCookie == nullptr) { if (gpioCookie == nullptr) {
sif::error << "LinuxLibgpioIF::addGpios: Invalid cookie" << std::endl; sif::error << "LinuxLibgpioIF::addGpios: Invalid cookie" << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
GpioMap mapToAdd = gpioCookie->getGpioMap(); GpioMap mapToAdd = gpioCookie->getGpioMap();
/* Check whether this ID already exists in the map and remove duplicates */ /* Check whether this ID already exists in the map and remove duplicates */
result = checkForConflicts(mapToAdd); result = checkForConflicts(mapToAdd);
if (result != RETURN_OK){ if (result != RETURN_OK) {
return result; return result;
} }
result = configureGpios(mapToAdd); result = configureGpios(mapToAdd);
if (result != RETURN_OK) { if (result != RETURN_OK) {
return RETURN_FAILED; return RETURN_FAILED;
} }
/* Register new GPIOs in gpioMap */ /* Register new GPIOs in gpioMap */
gpioMap.insert(mapToAdd.begin(), mapToAdd.end()); gpioMap.insert(mapToAdd.begin(), mapToAdd.end());
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap& mapToAdd) { ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap& mapToAdd) {
for(auto& gpioConfig: mapToAdd) { for (auto& gpioConfig : mapToAdd) {
auto& gpioType = gpioConfig.second->gpioType; auto& gpioType = gpioConfig.second->gpioType;
switch(gpioType) { switch (gpioType) {
case(gpio::GpioTypes::NONE): { case (gpio::GpioTypes::NONE): {
return GPIO_INVALID_INSTANCE; return GPIO_INVALID_INSTANCE;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_CHIP): {
auto regularGpio = dynamic_cast<GpiodRegularByChip*>(gpioConfig.second);
if (regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
} }
case(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP): { configureGpioByChip(gpioConfig.first, *regularGpio);
auto regularGpio = dynamic_cast<GpiodRegularByChip*>(gpioConfig.second); break;
if(regularGpio == nullptr) { }
return GPIO_INVALID_INSTANCE; case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL): {
} auto regularGpio = dynamic_cast<GpiodRegularByLabel*>(gpioConfig.second);
configureGpioByChip(gpioConfig.first, *regularGpio); if (regularGpio == nullptr) {
break; return GPIO_INVALID_INSTANCE;
} }
case(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):{ configureGpioByLabel(gpioConfig.first, *regularGpio);
auto regularGpio = dynamic_cast<GpiodRegularByLabel*>(gpioConfig.second); break;
if(regularGpio == nullptr) { }
return GPIO_INVALID_INSTANCE; case (gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
} auto regularGpio = dynamic_cast<GpiodRegularByLineName*>(gpioConfig.second);
configureGpioByLabel(gpioConfig.first, *regularGpio); if (regularGpio == nullptr) {
break; return GPIO_INVALID_INSTANCE;
}
case(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME):{
auto regularGpio = dynamic_cast<GpiodRegularByLineName*>(gpioConfig.second);
if(regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
}
configureGpioByLineName(gpioConfig.first, *regularGpio);
break;
}
case(gpio::GpioTypes::CALLBACK): {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioConfig.second);
if(gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioConfig.first, gpio::GpioOperation::WRITE,
gpioCallback->initValue, gpioCallback->callbackArgs);
} }
configureGpioByLineName(gpioConfig.first, *regularGpio);
break;
}
case (gpio::GpioTypes::CALLBACK): {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioConfig.second);
if (gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
} }
gpioCallback->callback(gpioConfig.first, gpio::GpioOperation::WRITE,
gpioCallback->initValue, gpioCallback->callbackArgs);
}
} }
return RETURN_OK; }
return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::configureGpioByLabel(gpioId_t gpioId, ReturnValue_t LinuxLibgpioIF::configureGpioByLabel(gpioId_t gpioId,
GpiodRegularByLabel &gpioByLabel) { GpiodRegularByLabel& gpioByLabel) {
std::string& label = gpioByLabel.label; std::string& label = gpioByLabel.label;
struct gpiod_chip* chip = gpiod_chip_open_by_label(label.c_str()); struct gpiod_chip* chip = gpiod_chip_open_by_label(label.c_str());
if (chip == nullptr) { if (chip == nullptr) {
sif::warning << "LinuxLibgpioIF::configureGpioByLabel: Failed to open gpio from gpio " sif::warning << "LinuxLibgpioIF::configureGpioByLabel: Failed to open gpio from gpio "
<< "group with label " << label << ". Gpio ID: " << gpioId << std::endl; << "group with label " << label << ". Gpio ID: " << gpioId << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
}
} std::string failOutput = "label: " + label;
std::string failOutput = "label: " + label; return configureRegularGpio(gpioId, chip, gpioByLabel, failOutput);
return configureRegularGpio(gpioId, chip, gpioByLabel, failOutput);
} }
ReturnValue_t LinuxLibgpioIF::configureGpioByChip(gpioId_t gpioId, ReturnValue_t LinuxLibgpioIF::configureGpioByChip(gpioId_t gpioId, GpiodRegularByChip& gpioByChip) {
GpiodRegularByChip &gpioByChip) { std::string& chipname = gpioByChip.chipname;
std::string& chipname = gpioByChip.chipname; struct gpiod_chip* chip = gpiod_chip_open_by_name(chipname.c_str());
struct gpiod_chip* chip = gpiod_chip_open_by_name(chipname.c_str()); if (chip == nullptr) {
if (chip == nullptr) { sif::warning << "LinuxLibgpioIF::configureGpioByChip: Failed to open chip " << chipname
sif::warning << "LinuxLibgpioIF::configureGpioByChip: Failed to open chip " << ". Gpio ID: " << gpioId << std::endl;
<< chipname << ". Gpio ID: " << gpioId << std::endl; return RETURN_FAILED;
return RETURN_FAILED; }
} std::string failOutput = "chipname: " + chipname;
std::string failOutput = "chipname: " + chipname; return configureRegularGpio(gpioId, chip, gpioByChip, failOutput);
return configureRegularGpio(gpioId, chip, gpioByChip, failOutput);
} }
ReturnValue_t LinuxLibgpioIF::configureGpioByLineName(gpioId_t gpioId, ReturnValue_t LinuxLibgpioIF::configureGpioByLineName(gpioId_t gpioId,
GpiodRegularByLineName &gpioByLineName) { GpiodRegularByLineName& gpioByLineName) {
std::string& lineName = gpioByLineName.lineName; std::string& lineName = gpioByLineName.lineName;
char chipname[MAX_CHIPNAME_LENGTH]; char chipname[MAX_CHIPNAME_LENGTH];
unsigned int lineOffset; unsigned int lineOffset;
int result = gpiod_ctxless_find_line(lineName.c_str(), chipname, MAX_CHIPNAME_LENGTH, int result =
&lineOffset); gpiod_ctxless_find_line(lineName.c_str(), chipname, MAX_CHIPNAME_LENGTH, &lineOffset);
if (result != LINE_FOUND) { if (result != LINE_FOUND) {
parseFindeLineResult(result, lineName); parseFindeLineResult(result, lineName);
return RETURN_FAILED; return RETURN_FAILED;
} }
gpioByLineName.lineNum = static_cast<int>(lineOffset); gpioByLineName.lineNum = static_cast<int>(lineOffset);
struct gpiod_chip* chip = gpiod_chip_open_by_name(chipname); struct gpiod_chip* chip = gpiod_chip_open_by_name(chipname);
if (chip == nullptr) { if (chip == nullptr) {
sif::warning << "LinuxLibgpioIF::configureGpioByLineName: Failed to open chip " sif::warning << "LinuxLibgpioIF::configureGpioByLineName: Failed to open chip " << chipname
<< chipname << ". <Gpio ID: " << gpioId << std::endl; << ". <Gpio ID: " << gpioId << std::endl;
return RETURN_FAILED; return RETURN_FAILED;
} }
std::string failOutput = "line name: " + lineName; std::string failOutput = "line name: " + lineName;
return configureRegularGpio(gpioId, chip, gpioByLineName, failOutput); return configureRegularGpio(gpioId, chip, gpioByLineName, failOutput);
} }
ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip, ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip,
GpiodRegularBase& regularGpio, std::string failOutput) { GpiodRegularBase& regularGpio,
unsigned int lineNum; std::string failOutput) {
gpio::Direction direction; unsigned int lineNum;
std::string consumer; gpio::Direction direction;
struct gpiod_line *lineHandle; std::string consumer;
int result = 0; struct gpiod_line* lineHandle;
int result = 0;
lineNum = regularGpio.lineNum; lineNum = regularGpio.lineNum;
lineHandle = gpiod_chip_get_line(chip, lineNum); lineHandle = gpiod_chip_get_line(chip, lineNum);
if (!lineHandle) { if (!lineHandle) {
sif::warning << "LinuxLibgpioIF::configureRegularGpio: Failed to open line " << std::endl; sif::warning << "LinuxLibgpioIF::configureRegularGpio: Failed to open line " << std::endl;
sif::warning << "GPIO ID: " << gpioId << ", line number: " << lineNum << sif::warning << "GPIO ID: " << gpioId << ", line number: " << lineNum << ", " << failOutput
", " << failOutput << std::endl; << std::endl;
sif::warning << "Check if Linux GPIO configuration has changed. " << std::endl; sif::warning << "Check if Linux GPIO configuration has changed. " << std::endl;
gpiod_chip_close(chip); gpiod_chip_close(chip);
return RETURN_FAILED; return RETURN_FAILED;
} }
direction = regularGpio.direction; direction = regularGpio.direction;
consumer = regularGpio.consumer; consumer = regularGpio.consumer;
/* Configure direction and add a description to the GPIO */ /* Configure direction and add a description to the GPIO */
switch (direction) { switch (direction) {
case(gpio::OUT): { case (gpio::OUT): {
result = gpiod_line_request_output(lineHandle, consumer.c_str(), result = gpiod_line_request_output(lineHandle, consumer.c_str(), regularGpio.initValue);
regularGpio.initValue); break;
break;
} }
case(gpio::IN): { case (gpio::IN): {
result = gpiod_line_request_input(lineHandle, consumer.c_str()); result = gpiod_line_request_input(lineHandle, consumer.c_str());
break; break;
} }
default: { default: {
sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified" sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified" << std::endl;
<< std::endl; return GPIO_INVALID_INSTANCE;
return GPIO_INVALID_INSTANCE;
} }
if (result < 0) { if (result < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "LinuxLibgpioIF::configureRegularGpio: Failed to request line " << sif::error << "LinuxLibgpioIF::configureRegularGpio: Failed to request line " << lineNum
lineNum << " from GPIO instance with ID: " << gpioId << std::endl; << " from GPIO instance with ID: " << gpioId << std::endl;
#else #else
sif::printError("LinuxLibgpioIF::configureRegularGpio: " sif::printError(
"Failed to request line %d from GPIO instance with ID: %d\n", lineNum, gpioId); "LinuxLibgpioIF::configureRegularGpio: "
"Failed to request line %d from GPIO instance with ID: %d\n",
lineNum, gpioId);
#endif #endif
gpiod_line_release(lineHandle); gpiod_line_release(lineHandle);
return RETURN_FAILED; return RETURN_FAILED;
} }
}
} /**
/** * Write line handle to GPIO configuration instance so it can later be used to set or
* Write line handle to GPIO configuration instance so it can later be used to set or * read states of GPIOs.
* read states of GPIOs. */
*/ regularGpio.lineHandle = lineHandle;
regularGpio.lineHandle = lineHandle; return RETURN_OK;
return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId) { ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId) {
gpioMapIter = gpioMap.find(gpioId); gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) { if (gpioMapIter == gpioMap.end()) {
sif::warning << "LinuxLibgpioIF::pullHigh: Unknown GPIO ID " << gpioId << std::endl; sif::warning << "LinuxLibgpioIF::pullHigh: Unknown GPIO ID " << gpioId << std::endl;
return UNKNOWN_GPIO_ID; return UNKNOWN_GPIO_ID;
} }
auto gpioType = gpioMapIter->second->gpioType; auto gpioType = gpioMapIter->second->gpioType;
if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP or
or gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL or
or gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) { gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second); auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if(regularGpio == nullptr) { if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE; return GPIO_TYPE_FAILURE;
}
return driveGpio(gpioId, *regularGpio, gpio::HIGH);
} }
else { return driveGpio(gpioId, *regularGpio, gpio::HIGH);
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second); } else {
if(gpioCallback->callback == nullptr) { auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
return GPIO_INVALID_INSTANCE; if (gpioCallback->callback == nullptr) {
} return GPIO_INVALID_INSTANCE;
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE,
gpio::Levels::HIGH, gpioCallback->callbackArgs);
return RETURN_OK;
} }
return GPIO_TYPE_FAILURE; gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE, gpio::Levels::HIGH,
gpioCallback->callbackArgs);
return RETURN_OK;
}
return GPIO_TYPE_FAILURE;
} }
ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId) { ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId) {
gpioMapIter = gpioMap.find(gpioId); gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) { if (gpioMapIter == gpioMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::pullLow: Unknown GPIO ID " << gpioId << std::endl; sif::warning << "LinuxLibgpioIF::pullLow: Unknown GPIO ID " << gpioId << std::endl;
#else #else
sif::printWarning("LinuxLibgpioIF::pullLow: Unknown GPIO ID %d\n", gpioId); sif::printWarning("LinuxLibgpioIF::pullLow: Unknown GPIO ID %d\n", gpioId);
#endif #endif
return UNKNOWN_GPIO_ID; return UNKNOWN_GPIO_ID;
} }
auto& gpioType = gpioMapIter->second->gpioType; auto& gpioType = gpioMapIter->second->gpioType;
if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP or
or gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL or
or gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) { gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second); auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if(regularGpio == nullptr) { if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE; return GPIO_TYPE_FAILURE;
}
return driveGpio(gpioId, *regularGpio, gpio::LOW);
} }
else { return driveGpio(gpioId, *regularGpio, gpio::LOW);
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second); } else {
if(gpioCallback->callback == nullptr) { auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
return GPIO_INVALID_INSTANCE; if (gpioCallback->callback == nullptr) {
} return GPIO_INVALID_INSTANCE;
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE,
gpio::Levels::LOW, gpioCallback->callbackArgs);
return RETURN_OK;
} }
return GPIO_TYPE_FAILURE; gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE, gpio::Levels::LOW,
gpioCallback->callbackArgs);
return RETURN_OK;
}
return GPIO_TYPE_FAILURE;
} }
ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId, ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId, GpiodRegularBase& regularGpio,
GpiodRegularBase& regularGpio, gpio::Levels logicLevel) { gpio::Levels logicLevel) {
int result = gpiod_line_set_value(regularGpio.lineHandle, logicLevel); int result = gpiod_line_set_value(regularGpio.lineHandle, logicLevel);
if (result < 0) { if (result < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID " << gpioId << sif::warning << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID " << gpioId
" to logic level " << logicLevel << std::endl; << " to logic level " << logicLevel << std::endl;
#else #else
sif::printWarning("LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID %d to " sif::printWarning(
"logic level %d\n", gpioId, logicLevel); "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID %d to "
"logic level %d\n",
gpioId, logicLevel);
#endif #endif
return DRIVE_GPIO_FAILURE; return DRIVE_GPIO_FAILURE;
} }
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) { ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
gpioMapIter = gpioMap.find(gpioId); gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()){ if (gpioMapIter == gpioMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::readGpio: Unknown GPIOD ID " << gpioId << std::endl; sif::warning << "LinuxLibgpioIF::readGpio: Unknown GPIOD ID " << gpioId << std::endl;
#else #else
sif::printWarning("LinuxLibgpioIF::readGpio: Unknown GPIOD ID %d\n", gpioId); sif::printWarning("LinuxLibgpioIF::readGpio: Unknown GPIOD ID %d\n", gpioId);
#endif #endif
return UNKNOWN_GPIO_ID; return UNKNOWN_GPIO_ID;
} }
auto gpioType = gpioMapIter->second->gpioType; auto gpioType = gpioMapIter->second->gpioType;
if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP or
or gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL or
or gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) { gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second); auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if(regularGpio == nullptr) { if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE; return GPIO_TYPE_FAILURE;
}
*gpioState = gpiod_line_get_value(regularGpio->lineHandle);
} }
else { *gpioState = gpiod_line_get_value(regularGpio->lineHandle);
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second); } else {
if(gpioCallback->callback == nullptr) { auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
return GPIO_INVALID_INSTANCE; if (gpioCallback->callback == nullptr) {
} return GPIO_INVALID_INSTANCE;
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::READ,
gpio::Levels::NONE, gpioCallback->callbackArgs);
return RETURN_OK;
} }
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::READ, gpio::Levels::NONE,
gpioCallback->callbackArgs);
return RETURN_OK; return RETURN_OK;
}
return RETURN_OK;
} }
ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd){ ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd) {
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK; ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
for(auto& gpioConfig: mapToAdd) { for (auto& gpioConfig : mapToAdd) {
switch(gpioConfig.second->gpioType) { switch (gpioConfig.second->gpioType) {
case(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP): case (gpio::GpioTypes::GPIO_REGULAR_BY_CHIP):
case(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL): case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):
case(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): { case (gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioConfig.second); auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioConfig.second);
if(regularGpio == nullptr) { if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE; return GPIO_TYPE_FAILURE;
}
// Check for conflicts and remove duplicates if necessary
result = checkForConflictsById(gpioConfig.first, gpioConfig.second->gpioType, mapToAdd);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
break;
} }
case(gpio::GpioTypes::CALLBACK): { // Check for conflicts and remove duplicates if necessary
auto callbackGpio = dynamic_cast<GpioCallback*>(gpioConfig.second); result = checkForConflictsById(gpioConfig.first, gpioConfig.second->gpioType, mapToAdd);
if(callbackGpio == nullptr) { if (result != HasReturnvaluesIF::RETURN_OK) {
return GPIO_TYPE_FAILURE; status = result;
}
// Check for conflicts and remove duplicates if necessary
result = checkForConflictsById(gpioConfig.first,
gpioConfig.second->gpioType, mapToAdd);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
break;
} }
default: { break;
}
case (gpio::GpioTypes::CALLBACK): {
auto callbackGpio = dynamic_cast<GpioCallback*>(gpioConfig.second);
if (callbackGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
// Check for conflicts and remove duplicates if necessary
result = checkForConflictsById(gpioConfig.first, gpioConfig.second->gpioType, mapToAdd);
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
break;
}
default: {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "Invalid GPIO type detected for GPIO ID " << gpioConfig.first sif::warning << "Invalid GPIO type detected for GPIO ID " << gpioConfig.first << std::endl;
<< std::endl;
#else #else
sif::printWarning("Invalid GPIO type detected for GPIO ID %d\n", gpioConfig.first); sif::printWarning("Invalid GPIO type detected for GPIO ID %d\n", gpioConfig.first);
#endif #endif
status = GPIO_TYPE_FAILURE; status = GPIO_TYPE_FAILURE;
} }
}
} }
return status; }
return status;
} }
ReturnValue_t LinuxLibgpioIF::checkForConflictsById(gpioId_t gpioIdToCheck, ReturnValue_t LinuxLibgpioIF::checkForConflictsById(gpioId_t gpioIdToCheck,
gpio::GpioTypes expectedType, GpioMap& mapToAdd) { gpio::GpioTypes expectedType,
// Cross check with private map GpioMap& mapToAdd) {
gpioMapIter = gpioMap.find(gpioIdToCheck); // Cross check with private map
if(gpioMapIter != gpioMap.end()) { gpioMapIter = gpioMap.find(gpioIdToCheck);
auto& gpioType = gpioMapIter->second->gpioType; if (gpioMapIter != gpioMap.end()) {
bool eraseDuplicateDifferentType = false; auto& gpioType = gpioMapIter->second->gpioType;
switch(expectedType) { bool eraseDuplicateDifferentType = false;
case(gpio::GpioTypes::NONE): { switch (expectedType) {
break; case (gpio::GpioTypes::NONE): {
break;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_CHIP):
case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):
case (gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
if (gpioType == gpio::GpioTypes::NONE or gpioType == gpio::GpioTypes::CALLBACK) {
eraseDuplicateDifferentType = true;
} }
case(gpio::GpioTypes::GPIO_REGULAR_BY_CHIP): break;
case(gpio::GpioTypes::GPIO_REGULAR_BY_LABEL): }
case(gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): { case (gpio::GpioTypes::CALLBACK): {
if(gpioType == gpio::GpioTypes::NONE or gpioType == gpio::GpioTypes::CALLBACK) { if (gpioType != gpio::GpioTypes::CALLBACK) {
eraseDuplicateDifferentType = true; eraseDuplicateDifferentType = true;
}
break;
} }
case(gpio::GpioTypes::CALLBACK): { }
if(gpioType != gpio::GpioTypes::CALLBACK) {
eraseDuplicateDifferentType = true;
}
}
}
if(eraseDuplicateDifferentType) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type " << gpioIdToCheck <<
". Removing duplicate from map to add" << std::endl;
#else
sif::printWarning("LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type %d. Removing duplicate from map to add\n", gpioIdToCheck);
#endif
mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
}
// Remove element from map to add because a entry for this GPIO already exists
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO "
"definition with ID " << gpioIdToCheck << " detected. " <<
"Duplicate will be removed from map to add" << std::endl;
#else
sif::printWarning("LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition "
"with ID %d detected. Duplicate will be removed from map to add\n", gpioIdToCheck);
#endif
mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
} }
return HasReturnvaluesIF::RETURN_OK; if (eraseDuplicateDifferentType) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type "
<< gpioIdToCheck << ". Removing duplicate from map to add" << std::endl;
#else
sif::printWarning(
"LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type %d. Removing duplicate from map to add\n",
gpioIdToCheck);
#endif
mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
}
// Remove element from map to add because a entry for this GPIO already exists
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO "
"definition with ID "
<< gpioIdToCheck << " detected. "
<< "Duplicate will be removed from map to add" << std::endl;
#else
sif::printWarning(
"LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition "
"with ID %d detected. Duplicate will be removed from map to add\n",
gpioIdToCheck);
#endif
mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
}
return HasReturnvaluesIF::RETURN_OK;
} }
void LinuxLibgpioIF::parseFindeLineResult(int result, std::string& lineName) { void LinuxLibgpioIF::parseFindeLineResult(int result, std::string& lineName) {
switch (result) { switch (result) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
case LINE_NOT_EXISTS: case LINE_NOT_EXISTS:
case LINE_ERROR: { case LINE_ERROR: {
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Line with name " << lineName << sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Line with name " << lineName
" does not exist" << std::endl; << " does not exist" << std::endl;
break; break;
} }
default: { default: {
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line " sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line "
"with name " << lineName << std::endl; "with name "
break; << lineName << std::endl;
break;
} }
#else #else
case LINE_NOT_EXISTS: case LINE_NOT_EXISTS:
case LINE_ERROR: { case LINE_ERROR: {
sif::printWarning("LinuxLibgpioIF::parseFindeLineResult: Line with name %s " sif::printWarning(
"does not exist\n", lineName); "LinuxLibgpioIF::parseFindeLineResult: Line with name %s "
break; "does not exist\n",
lineName);
break;
} }
default: { default: {
sif::printWarning("LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line " sif::printWarning(
"with name %s\n", lineName); "LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line "
break; "with name %s\n",
lineName);
break;
} }
#endif #endif
} }
} }

View File

@ -1,9 +1,9 @@
#ifndef LINUX_GPIO_LINUXLIBGPIOIF_H_ #ifndef LINUX_GPIO_LINUXLIBGPIOIF_H_
#define LINUX_GPIO_LINUXLIBGPIOIF_H_ #define LINUX_GPIO_LINUXLIBGPIOIF_H_
#include "fsfw/objectmanager/SystemObject.h"
#include "fsfw/returnvalues/FwClassIds.h" #include "fsfw/returnvalues/FwClassIds.h"
#include "fsfw_hal/common/gpio/GpioIF.h" #include "fsfw_hal/common/gpio/GpioIF.h"
#include "fsfw/objectmanager/SystemObject.h"
class GpioCookie; class GpioCookie;
class GpiodRegularIF; class GpiodRegularIF;
@ -16,76 +16,71 @@ class GpiodRegularIF;
* The Petalinux SDK from Xilinx supports libgpiod since Petalinux 2019.1. * The Petalinux SDK from Xilinx supports libgpiod since Petalinux 2019.1.
*/ */
class LinuxLibgpioIF : public GpioIF, public SystemObject { class LinuxLibgpioIF : public GpioIF, public SystemObject {
public: public:
static const uint8_t gpioRetvalId = CLASS_ID::HAL_GPIO;
static const uint8_t gpioRetvalId = CLASS_ID::HAL_GPIO; static constexpr ReturnValue_t UNKNOWN_GPIO_ID =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 1);
static constexpr ReturnValue_t DRIVE_GPIO_FAILURE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 2);
static constexpr ReturnValue_t GPIO_TYPE_FAILURE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 3);
static constexpr ReturnValue_t GPIO_INVALID_INSTANCE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 4);
static constexpr ReturnValue_t GPIO_DUPLICATE_DETECTED =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 5);
static constexpr ReturnValue_t UNKNOWN_GPIO_ID = LinuxLibgpioIF(object_id_t objectId);
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 1); virtual ~LinuxLibgpioIF();
static constexpr ReturnValue_t DRIVE_GPIO_FAILURE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 2);
static constexpr ReturnValue_t GPIO_TYPE_FAILURE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 3);
static constexpr ReturnValue_t GPIO_INVALID_INSTANCE =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 4);
static constexpr ReturnValue_t GPIO_DUPLICATE_DETECTED =
HasReturnvaluesIF::makeReturnCode(gpioRetvalId, 5);
LinuxLibgpioIF(object_id_t objectId); ReturnValue_t addGpios(GpioCookie* gpioCookie) override;
virtual ~LinuxLibgpioIF(); ReturnValue_t pullHigh(gpioId_t gpioId) override;
ReturnValue_t pullLow(gpioId_t gpioId) override;
ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) override;
ReturnValue_t addGpios(GpioCookie* gpioCookie) override; private:
ReturnValue_t pullHigh(gpioId_t gpioId) override; static const size_t MAX_CHIPNAME_LENGTH = 11;
ReturnValue_t pullLow(gpioId_t gpioId) override; static const int LINE_NOT_EXISTS = 0;
ReturnValue_t readGpio(gpioId_t gpioId, int* gpioState) override; static const int LINE_ERROR = -1;
static const int LINE_FOUND = 1;
private: // Holds the information and configuration of all used GPIOs
GpioUnorderedMap gpioMap;
GpioUnorderedMapIter gpioMapIter;
static const size_t MAX_CHIPNAME_LENGTH = 11; /**
static const int LINE_NOT_EXISTS = 0; * @brief This functions drives line of a GPIO specified by the GPIO ID.
static const int LINE_ERROR = -1; *
static const int LINE_FOUND = 1; * @param gpioId The GPIO ID of the GPIO to drive.
* @param logiclevel The logic level to set. O or 1.
*/
ReturnValue_t driveGpio(gpioId_t gpioId, GpiodRegularBase& regularGpio, gpio::Levels logicLevel);
// Holds the information and configuration of all used GPIOs ReturnValue_t configureGpioByLabel(gpioId_t gpioId, GpiodRegularByLabel& gpioByLabel);
GpioUnorderedMap gpioMap; ReturnValue_t configureGpioByChip(gpioId_t gpioId, GpiodRegularByChip& gpioByChip);
GpioUnorderedMapIter gpioMapIter; ReturnValue_t configureGpioByLineName(gpioId_t gpioId, GpiodRegularByLineName& gpioByLineName);
ReturnValue_t configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip,
GpiodRegularBase& regularGpio, std::string failOutput);
/** /**
* @brief This functions drives line of a GPIO specified by the GPIO ID. * @brief This function checks if GPIOs are already registered and whether
* * there exists a conflict in the GPIO configuration. E.g. the
* @param gpioId The GPIO ID of the GPIO to drive. * direction.
* @param logiclevel The logic level to set. O or 1. *
*/ * @param mapToAdd The GPIOs which shall be added to the gpioMap.
ReturnValue_t driveGpio(gpioId_t gpioId, GpiodRegularBase& regularGpio, *
gpio::Levels logicLevel); * @return RETURN_OK if successful, otherwise RETURN_FAILED
*/
ReturnValue_t checkForConflicts(GpioMap& mapToAdd);
ReturnValue_t configureGpioByLabel(gpioId_t gpioId, GpiodRegularByLabel& gpioByLabel); ReturnValue_t checkForConflictsById(gpioId_t gpiodId, gpio::GpioTypes type, GpioMap& mapToAdd);
ReturnValue_t configureGpioByChip(gpioId_t gpioId, GpiodRegularByChip& gpioByChip);
ReturnValue_t configureGpioByLineName(gpioId_t gpioId,
GpiodRegularByLineName &gpioByLineName);
ReturnValue_t configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip,
GpiodRegularBase& regularGpio, std::string failOutput);
/** /**
* @brief This function checks if GPIOs are already registered and whether * @brief Performs the initial configuration of all GPIOs specified in the GpioMap mapToAdd.
* there exists a conflict in the GPIO configuration. E.g. the */
* direction. ReturnValue_t configureGpios(GpioMap& mapToAdd);
*
* @param mapToAdd The GPIOs which shall be added to the gpioMap.
*
* @return RETURN_OK if successful, otherwise RETURN_FAILED
*/
ReturnValue_t checkForConflicts(GpioMap& mapToAdd);
ReturnValue_t checkForConflictsById(gpioId_t gpiodId, gpio::GpioTypes type, void parseFindeLineResult(int result, std::string& lineName);
GpioMap& mapToAdd);
/**
* @brief Performs the initial configuration of all GPIOs specified in the GpioMap mapToAdd.
*/
ReturnValue_t configureGpios(GpioMap& mapToAdd);
void parseFindeLineResult(int result, std::string& lineName);
}; };
#endif /* LINUX_GPIO_LINUXLIBGPIOIF_H_ */ #endif /* LINUX_GPIO_LINUXLIBGPIOIF_H_ */

View File

@ -1,233 +1,223 @@
#include "fsfw/FSFW.h"
#include "fsfw_hal/linux/i2c/I2cComIF.h" #include "fsfw_hal/linux/i2c/I2cComIF.h"
#include "fsfw_hal/linux/utility.h"
#include "fsfw_hal/linux/UnixFileGuard.h"
#include "fsfw/serviceinterface.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <cstring> #include <cstring>
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
#include "fsfw_hal/linux/UnixFileGuard.h"
#include "fsfw_hal/linux/utility.h"
I2cComIF::I2cComIF(object_id_t objectId): SystemObject(objectId){ I2cComIF::I2cComIF(object_id_t objectId) : SystemObject(objectId) {}
}
I2cComIF::~I2cComIF() {} I2cComIF::~I2cComIF() {}
ReturnValue_t I2cComIF::initializeInterface(CookieIF* cookie) { ReturnValue_t I2cComIF::initializeInterface(CookieIF* cookie) {
address_t i2cAddress;
std::string deviceFile;
address_t i2cAddress; if (cookie == nullptr) {
std::string deviceFile;
if(cookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::initializeInterface: Invalid cookie!" << std::endl; sif::error << "I2cComIF::initializeInterface: Invalid cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie); I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if(i2cCookie == nullptr) { if (i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::initializeInterface: Invalid I2C cookie!" << std::endl; sif::error << "I2cComIF::initializeInterface: Invalid I2C cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
i2cAddress = i2cCookie->getAddress(); i2cAddress = i2cCookie->getAddress();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress); i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if(i2cDeviceMapIter == i2cDeviceMap.end()) { if (i2cDeviceMapIter == i2cDeviceMap.end()) {
size_t maxReplyLen = i2cCookie->getMaxReplyLen(); size_t maxReplyLen = i2cCookie->getMaxReplyLen();
I2cInstance i2cInstance = {std::vector<uint8_t>(maxReplyLen), 0}; I2cInstance i2cInstance = {std::vector<uint8_t>(maxReplyLen), 0};
auto statusPair = i2cDeviceMap.emplace(i2cAddress, i2cInstance); auto statusPair = i2cDeviceMap.emplace(i2cAddress, i2cInstance);
if (not statusPair.second) { if (not statusPair.second) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::initializeInterface: Failed to insert device with address " << sif::error << "I2cComIF::initializeInterface: Failed to insert device with address "
i2cAddress << "to I2C device " << "map" << std::endl; << i2cAddress << "to I2C device "
<< "map" << std::endl;
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
} }
return HasReturnvaluesIF::RETURN_OK;
}
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::initializeInterface: Device with address " << i2cAddress << sif::error << "I2cComIF::initializeInterface: Device with address " << i2cAddress
"already in use" << std::endl; << "already in use" << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
ReturnValue_t I2cComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
ReturnValue_t result;
int fd;
std::string deviceFile;
if (sendData == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: Send Data is nullptr" << std::endl;
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
ReturnValue_t I2cComIF::sendMessage(CookieIF *cookie, if (sendLen == 0) {
const uint8_t *sendData, size_t sendLen) {
ReturnValue_t result;
int fd;
std::string deviceFile;
if(sendData == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: Send Data is nullptr"
<< std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
if(sendLen == 0) {
return HasReturnvaluesIF::RETURN_OK;
}
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if(i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: Invalid I2C Cookie!" << std::endl;
#endif
return NULLPOINTER;
}
address_t i2cAddress = i2cCookie->getAddress();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: i2cAddress of Cookie not "
<< "registered in i2cDeviceMap" << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::sendMessage");
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult();
}
result = openDevice(deviceFile, i2cAddress, &fd);
if (result != HasReturnvaluesIF::RETURN_OK){
return result;
}
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: Failed to send data to I2C "
"device with error code " << errno << ". Error description: "
<< strerror(errno) << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
}
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if (i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: Invalid I2C Cookie!" << std::endl;
#endif
return NULLPOINTER;
}
address_t i2cAddress = i2cCookie->getAddress();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: i2cAddress of Cookie not "
<< "registered in i2cDeviceMap" << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::sendMessage");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult();
}
result = openDevice(deviceFile, i2cAddress, &fd);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::sendMessage: Failed to send data to I2C "
"device with error code "
<< errno << ". Error description: " << strerror(errno) << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t I2cComIF::getSendSuccess(CookieIF *cookie) { ReturnValue_t I2cComIF::getSendSuccess(CookieIF* cookie) { return HasReturnvaluesIF::RETURN_OK; }
ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
ReturnValue_t result;
int fd;
std::string deviceFile;
if (requestLen == 0) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie, I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
size_t requestLen) { if (i2cCookie == nullptr) {
ReturnValue_t result;
int fd;
std::string deviceFile;
if (requestLen == 0) {
return HasReturnvaluesIF::RETURN_OK;
}
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if(i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::requestReceiveMessage: Invalid I2C Cookie!" << std::endl; sif::error << "I2cComIF::requestReceiveMessage: Invalid I2C Cookie!" << std::endl;
#endif #endif
i2cDeviceMapIter->second.replyLen = 0; i2cDeviceMapIter->second.replyLen = 0;
return NULLPOINTER; return NULLPOINTER;
} }
address_t i2cAddress = i2cCookie->getAddress(); address_t i2cAddress = i2cCookie->getAddress();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress); i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) { if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not " sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not "
<< "registered in i2cDeviceMap" << std::endl; << "registered in i2cDeviceMap" << std::endl;
#endif #endif
i2cDeviceMapIter->second.replyLen = 0; i2cDeviceMapIter->second.replyLen = 0;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
deviceFile = i2cCookie->getDeviceFile(); deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::requestReceiveMessage"); UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::requestReceiveMessage");
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) { if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult(); return fileHelper.getOpenResult();
} }
result = openDevice(deviceFile, i2cAddress, &fd); result = openDevice(deviceFile, i2cAddress, &fd);
if (result != HasReturnvaluesIF::RETURN_OK){ if (result != HasReturnvaluesIF::RETURN_OK) {
i2cDeviceMapIter->second.replyLen = 0; i2cDeviceMapIter->second.replyLen = 0;
return result; return result;
} }
uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data(); uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data();
int readLen = read(fd, replyBuffer, requestLen); int readLen = read(fd, replyBuffer, requestLen);
if (readLen != static_cast<int>(requestLen)) { if (readLen != static_cast<int>(requestLen)) {
#if FSFW_VERBOSE_LEVEL >= 1 and FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_VERBOSE_LEVEL >= 1 and FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::requestReceiveMessage: Reading from I2C " sif::error << "I2cComIF::requestReceiveMessage: Reading from I2C "
<< "device failed with error code " << errno <<". Description" << "device failed with error code " << errno << ". Description"
<< " of error: " << strerror(errno) << std::endl; << " of error: " << strerror(errno) << std::endl;
sif::error << "I2cComIF::requestReceiveMessage: Read only " << readLen << " from " sif::error << "I2cComIF::requestReceiveMessage: Read only " << readLen << " from " << requestLen
<< requestLen << " bytes" << std::endl; << " bytes" << std::endl;
#endif #endif
i2cDeviceMapIter->second.replyLen = 0; i2cDeviceMapIter->second.replyLen = 0;
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "I2cComIF::requestReceiveMessage: Read " << readLen << " of " << requestLen << " bytes" << std::endl; sif::debug << "I2cComIF::requestReceiveMessage: Read " << readLen << " of " << requestLen
<< " bytes" << std::endl;
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
i2cDeviceMapIter->second.replyLen = requestLen; i2cDeviceMapIter->second.replyLen = requestLen;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t I2cComIF::readReceivedMessage(CookieIF *cookie, ReturnValue_t I2cComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
uint8_t **buffer, size_t* size) { I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie); if (i2cCookie == nullptr) {
if(i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::readReceivedMessage: Invalid I2C Cookie!" << std::endl; sif::error << "I2cComIF::readReceivedMessage: Invalid I2C Cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
address_t i2cAddress = i2cCookie->getAddress(); address_t i2cAddress = i2cCookie->getAddress();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress); i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) { if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not " sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not "
<< "found in i2cDeviceMap" << std::endl; << "found in i2cDeviceMap" << std::endl;
#endif #endif
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
*buffer = i2cDeviceMapIter->second.replyBuffer.data(); *buffer = i2cDeviceMapIter->second.replyBuffer.data();
*size = i2cDeviceMapIter->second.replyLen; *size = i2cDeviceMapIter->second.replyLen;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t I2cComIF::openDevice(std::string deviceFile, ReturnValue_t I2cComIF::openDevice(std::string deviceFile, address_t i2cAddress,
address_t i2cAddress, int* fileDescriptor) { int* fileDescriptor) {
if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) {
if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "I2cComIF: Specifying target device failed with error code " << errno << "." sif::warning << "I2cComIF: Specifying target device failed with error code " << errno << "."
<< std::endl; << std::endl;
sif::warning << "Error description " << strerror(errno) << std::endl; sif::warning << "Error description " << strerror(errno) << std::endl;
#else #else
sif::printWarning("I2cComIF: Specifying target device failed with error code %d.\n"); sif::printWarning("I2cComIF: Specifying target device failed with error code %d.\n");
sif::printWarning("Error description: %s\n", strerror(errno)); sif::printWarning("Error description: %s\n", strerror(errno));
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -1,13 +1,14 @@
#ifndef LINUX_I2C_I2COMIF_H_ #ifndef LINUX_I2C_I2COMIF_H_
#define LINUX_I2C_I2COMIF_H_ #define LINUX_I2C_I2COMIF_H_
#include "I2cCookie.h"
#include <fsfw/objectmanager/SystemObject.h>
#include <fsfw/devicehandlers/DeviceCommunicationIF.h> #include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <fsfw/objectmanager/SystemObject.h>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "I2cCookie.h"
/** /**
* @brief This is the communication interface for I2C devices connected * @brief This is the communication interface for I2C devices connected
* to a system running a Linux OS. * to a system running a Linux OS.
@ -16,46 +17,41 @@
* *
* @author J. Meier * @author J. Meier
*/ */
class I2cComIF: public DeviceCommunicationIF, public SystemObject { class I2cComIF : public DeviceCommunicationIF, public SystemObject {
public: public:
I2cComIF(object_id_t objectId); I2cComIF(object_id_t objectId);
virtual ~I2cComIF(); virtual ~I2cComIF();
ReturnValue_t initializeInterface(CookieIF * cookie) override; ReturnValue_t initializeInterface(CookieIF *cookie) override;
ReturnValue_t sendMessage(CookieIF *cookie,const uint8_t *sendData, ReturnValue_t sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) override;
size_t sendLen) override; ReturnValue_t getSendSuccess(CookieIF *cookie) override;
ReturnValue_t getSendSuccess(CookieIF *cookie) override; ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen) override;
ReturnValue_t requestReceiveMessage(CookieIF *cookie, ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) override;
size_t requestLen) override;
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
size_t *size) override;
private: private:
struct I2cInstance {
std::vector<uint8_t> replyBuffer;
size_t replyLen;
};
struct I2cInstance { using I2cDeviceMap = std::unordered_map<address_t, I2cInstance>;
std::vector<uint8_t> replyBuffer; using I2cDeviceMapIter = I2cDeviceMap::iterator;
size_t replyLen;
};
using I2cDeviceMap = std::unordered_map<address_t, I2cInstance>; /* In this map all i2c devices will be registered with their address and
using I2cDeviceMapIter = I2cDeviceMap::iterator; * the appropriate file descriptor will be stored */
I2cDeviceMap i2cDeviceMap;
I2cDeviceMapIter i2cDeviceMapIter;
/* In this map all i2c devices will be registered with their address and /**
* the appropriate file descriptor will be stored */ * @brief This function opens an I2C device and binds the opened file
I2cDeviceMap i2cDeviceMap; * to a specific I2C address.
I2cDeviceMapIter i2cDeviceMapIter; * @param deviceFile The name of the device file. E.g. i2c-0
* @param i2cAddress The address of the i2c slave device.
/** * @param fileDescriptor Pointer to device descriptor.
* @brief This function opens an I2C device and binds the opened file * @return RETURN_OK if successful, otherwise RETURN_FAILED.
* to a specific I2C address. */
* @param deviceFile The name of the device file. E.g. i2c-0 ReturnValue_t openDevice(std::string deviceFile, address_t i2cAddress, int *fileDescriptor);
* @param i2cAddress The address of the i2c slave device.
* @param fileDescriptor Pointer to device descriptor.
* @return RETURN_OK if successful, otherwise RETURN_FAILED.
*/
ReturnValue_t openDevice(std::string deviceFile,
address_t i2cAddress, int* fileDescriptor);
}; };
#endif /* LINUX_I2C_I2COMIF_H_ */ #endif /* LINUX_I2C_I2COMIF_H_ */

View File

@ -1,20 +1,12 @@
#include "fsfw_hal/linux/i2c/I2cCookie.h" #include "fsfw_hal/linux/i2c/I2cCookie.h"
I2cCookie::I2cCookie(address_t i2cAddress_, size_t maxReplyLen_, I2cCookie::I2cCookie(address_t i2cAddress_, size_t maxReplyLen_, std::string deviceFile_)
std::string deviceFile_) : : i2cAddress(i2cAddress_), maxReplyLen(maxReplyLen_), deviceFile(deviceFile_) {}
i2cAddress(i2cAddress_), maxReplyLen(maxReplyLen_), deviceFile(deviceFile_) {
}
address_t I2cCookie::getAddress() const { address_t I2cCookie::getAddress() const { return i2cAddress; }
return i2cAddress;
}
size_t I2cCookie::getMaxReplyLen() const { size_t I2cCookie::getMaxReplyLen() const { return maxReplyLen; }
return maxReplyLen;
}
std::string I2cCookie::getDeviceFile() const { std::string I2cCookie::getDeviceFile() const { return deviceFile; }
return deviceFile;
}
I2cCookie::~I2cCookie() {} I2cCookie::~I2cCookie() {}

View File

@ -2,6 +2,7 @@
#define LINUX_I2C_I2CCOOKIE_H_ #define LINUX_I2C_I2CCOOKIE_H_
#include <fsfw/devicehandlers/CookieIF.h> #include <fsfw/devicehandlers/CookieIF.h>
#include <string> #include <string>
/** /**
@ -9,30 +10,27 @@
* *
* @author J. Meier * @author J. Meier
*/ */
class I2cCookie: public CookieIF { class I2cCookie : public CookieIF {
public: public:
/**
* @brief Constructor for the I2C cookie.
* @param i2cAddress_ The i2c address of the target device.
* @param maxReplyLen_ The maximum expected length of a reply from the
* target device.
* @param devicFile_ The device file specifying the i2c interface to use. E.g. "/dev/i2c-0".
*/
I2cCookie(address_t i2cAddress_, size_t maxReplyLen_, std::string deviceFile_);
/** virtual ~I2cCookie();
* @brief Constructor for the I2C cookie.
* @param i2cAddress_ The i2c address of the target device.
* @param maxReplyLen_ The maximum expected length of a reply from the
* target device.
* @param devicFile_ The device file specifying the i2c interface to use. E.g. "/dev/i2c-0".
*/
I2cCookie(address_t i2cAddress_, size_t maxReplyLen_,
std::string deviceFile_);
virtual ~I2cCookie(); address_t getAddress() const;
size_t getMaxReplyLen() const;
std::string getDeviceFile() const;
address_t getAddress() const; private:
size_t getMaxReplyLen() const; address_t i2cAddress = 0;
std::string getDeviceFile() const; size_t maxReplyLen = 0;
std::string deviceFile;
private:
address_t i2cAddress = 0;
size_t maxReplyLen = 0;
std::string deviceFile;
}; };
#endif /* LINUX_I2C_I2CCOOKIE_H_ */ #endif /* LINUX_I2C_I2CCOOKIE_H_ */

View File

@ -1,38 +1,38 @@
#include "fsfw/FSFW.h"
#include "fsfw_hal/linux/rpi/GpioRPi.h" #include "fsfw_hal/linux/rpi/GpioRPi.h"
#include "fsfw_hal/common/gpio/GpioCookie.h"
#include <fsfw/serviceinterface/ServiceInterface.h> #include <fsfw/serviceinterface/ServiceInterface.h>
#include "fsfw/FSFW.h"
#include "fsfw_hal/common/gpio/GpioCookie.h"
ReturnValue_t gpio::createRpiGpioConfig(GpioCookie* cookie, gpioId_t gpioId, int bcmPin, ReturnValue_t gpio::createRpiGpioConfig(GpioCookie* cookie, gpioId_t gpioId, int bcmPin,
std::string consumer, gpio::Direction direction, int initValue) { std::string consumer, gpio::Direction direction,
if(cookie == nullptr) { int initValue) {
return HasReturnvaluesIF::RETURN_FAILED; if (cookie == nullptr) {
} return HasReturnvaluesIF::RETURN_FAILED;
}
auto config = new GpiodRegularByChip(); auto config = new GpiodRegularByChip();
/* Default chipname for Raspberry Pi. There is still gpiochip1 for expansion, but most users /* Default chipname for Raspberry Pi. There is still gpiochip1 for expansion, but most users
will not need this */ will not need this */
config->chipname = "gpiochip0"; config->chipname = "gpiochip0";
config->consumer = consumer; config->consumer = consumer;
config->direction = direction; config->direction = direction;
config->initValue = initValue; config->initValue = initValue;
/* Sanity check for the BCM pins before assigning it */ /* Sanity check for the BCM pins before assigning it */
if(bcmPin > 27) { if (bcmPin > 27) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "createRpiGpioConfig: BCM pin " << bcmPin << " invalid!" << std::endl; sif::error << "createRpiGpioConfig: BCM pin " << bcmPin << " invalid!" << std::endl;
#else #else
sif::printError("createRpiGpioConfig: BCM pin %d invalid!\n", bcmPin); sif::printError("createRpiGpioConfig: BCM pin %d invalid!\n", bcmPin);
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
config->lineNum = bcmPin; config->lineNum = bcmPin;
cookie->addGpio(gpioId, config); cookie->addGpio(gpioId, config);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -2,6 +2,7 @@
#define BSP_RPI_GPIO_GPIORPI_H_ #define BSP_RPI_GPIO_GPIORPI_H_
#include <fsfw/returnvalues/HasReturnvaluesIF.h> #include <fsfw/returnvalues/HasReturnvaluesIF.h>
#include "../../common/gpio/gpioDefinitions.h" #include "../../common/gpio/gpioDefinitions.h"
class GpioCookie; class GpioCookie;
@ -20,7 +21,7 @@ namespace gpio {
* @return * @return
*/ */
ReturnValue_t createRpiGpioConfig(GpioCookie* cookie, gpioId_t gpioId, int bcmPin, ReturnValue_t createRpiGpioConfig(GpioCookie* cookie, gpioId_t gpioId, int bcmPin,
std::string consumer, gpio::Direction direction, int initValue); std::string consumer, gpio::Direction direction, int initValue);
} } // namespace gpio
#endif /* BSP_RPI_GPIO_GPIORPI_H_ */ #endif /* BSP_RPI_GPIO_GPIORPI_H_ */

View File

@ -1,408 +1,404 @@
#include "fsfw/FSFW.h"
#include "fsfw_hal/linux/spi/SpiComIF.h" #include "fsfw_hal/linux/spi/SpiComIF.h"
#include "fsfw_hal/linux/spi/SpiCookie.h"
#include "fsfw_hal/linux/utility.h"
#include "fsfw_hal/linux/UnixFileGuard.h"
#include <fsfw/ipc/MutexFactory.h>
#include <fsfw/globalfunctions/arrayprinter.h>
#include <linux/spi/spidev.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <fsfw/globalfunctions/arrayprinter.h>
#include <fsfw/ipc/MutexFactory.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h>
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
SpiComIF::SpiComIF(object_id_t objectId, GpioIF* gpioComIF): #include "fsfw/FSFW.h"
SystemObject(objectId), gpioComIF(gpioComIF) { #include "fsfw_hal/linux/UnixFileGuard.h"
if(gpioComIF == nullptr) { #include "fsfw_hal/linux/spi/SpiCookie.h"
#include "fsfw_hal/linux/utility.h"
SpiComIF::SpiComIF(object_id_t objectId, GpioIF* gpioComIF)
: SystemObject(objectId), gpioComIF(gpioComIF) {
if (gpioComIF == nullptr) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::SpiComIF: GPIO communication interface invalid!" << std::endl; sif::error << "SpiComIF::SpiComIF: GPIO communication interface invalid!" << std::endl;
#else #else
sif::printError("SpiComIF::SpiComIF: GPIO communication interface invalid!\n"); sif::printError("SpiComIF::SpiComIF: GPIO communication interface invalid!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
} }
spiMutex = MutexFactory::instance()->createMutex(); spiMutex = MutexFactory::instance()->createMutex();
} }
ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) { ReturnValue_t SpiComIF::initializeInterface(CookieIF* cookie) {
int retval = 0; int retval = 0;
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie); SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
return NULLPOINTER; return NULLPOINTER;
} }
address_t spiAddress = spiCookie->getSpiAddress(); address_t spiAddress = spiCookie->getSpiAddress();
auto iter = spiDeviceMap.find(spiAddress); auto iter = spiDeviceMap.find(spiAddress);
if(iter == spiDeviceMap.end()) { if (iter == spiDeviceMap.end()) {
size_t bufferSize = spiCookie->getMaxBufferSize(); size_t bufferSize = spiCookie->getMaxBufferSize();
SpiInstance spiInstance(bufferSize); SpiInstance spiInstance(bufferSize);
auto statusPair = spiDeviceMap.emplace(spiAddress, spiInstance); auto statusPair = spiDeviceMap.emplace(spiAddress, spiInstance);
if (not statusPair.second) { if (not statusPair.second) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::initializeInterface: Failed to insert device with address " << sif::error << "SpiComIF::initializeInterface: Failed to insert device with address "
spiAddress << "to SPI device map" << std::endl; << spiAddress << "to SPI device map" << std::endl;
#else #else
sif::printError("SpiComIF::initializeInterface: Failed to insert device with address " sif::printError(
"%lu to SPI device map\n", static_cast<unsigned long>(spiAddress)); "SpiComIF::initializeInterface: Failed to insert device with address "
"%lu to SPI device map\n",
static_cast<unsigned long>(spiAddress));
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
/* Now we emplaced the read buffer in the map, we still need to assign that location
to the SPI driver transfer struct */
spiCookie->assignReadBuffer(statusPair.first->second.replyBuffer.data());
} }
else { /* Now we emplaced the read buffer in the map, we still need to assign that location
to the SPI driver transfer struct */
spiCookie->assignReadBuffer(statusPair.first->second.replyBuffer.data());
} else {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::initializeInterface: SPI address already exists!" << std::endl; sif::error << "SpiComIF::initializeInterface: SPI address already exists!" << std::endl;
#else #else
sif::printError("SpiComIF::initializeInterface: SPI address already exists!\n"); sif::printError("SpiComIF::initializeInterface: SPI address already exists!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
/* Pull CS high in any case to be sure that device is inactive */
gpioId_t gpioId = spiCookie->getChipSelectPin();
if (gpioId != gpio::NO_GPIO) {
gpioComIF->pullHigh(gpioId);
}
uint32_t spiSpeed = 0;
spi::SpiModes spiMode = spi::SpiModes::MODE_0;
SpiCookie::UncommonParameters params;
spiCookie->getSpiParameters(spiMode, spiSpeed, &params);
int fileDescriptor = 0;
UnixFileGuard fileHelper(spiCookie->getSpiDevice(), &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult();
}
/* These flags are rather uncommon */
if (params.threeWireSpi or params.noCs or params.csHigh) {
uint32_t currentMode = 0;
retval = ioctl(fileDescriptor, SPI_IOC_RD_MODE32, &currentMode);
if (retval != 0) {
utility::handleIoctlError("SpiComIF::initialiezInterface: Could not read full mode!");
} }
/* Pull CS high in any case to be sure that device is inactive */ if (params.threeWireSpi) {
gpioId_t gpioId = spiCookie->getChipSelectPin(); currentMode |= SPI_3WIRE;
if(gpioId != gpio::NO_GPIO) {
gpioComIF->pullHigh(gpioId);
} }
if (params.noCs) {
uint32_t spiSpeed = 0; /* Some drivers like the Raspberry Pi ignore this flag in any case */
spi::SpiModes spiMode = spi::SpiModes::MODE_0; currentMode |= SPI_NO_CS;
SpiCookie::UncommonParameters params;
spiCookie->getSpiParameters(spiMode, spiSpeed, &params);
int fileDescriptor = 0;
UnixFileGuard fileHelper(spiCookie->getSpiDevice(), &fileDescriptor, O_RDWR,
"SpiComIF::initializeInterface");
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult();
} }
if (params.csHigh) {
/* These flags are rather uncommon */ currentMode |= SPI_CS_HIGH;
if(params.threeWireSpi or params.noCs or params.csHigh) {
uint32_t currentMode = 0;
retval = ioctl(fileDescriptor, SPI_IOC_RD_MODE32, &currentMode);
if(retval != 0) {
utility::handleIoctlError("SpiComIF::initialiezInterface: Could not read full mode!");
}
if(params.threeWireSpi) {
currentMode |= SPI_3WIRE;
}
if(params.noCs) {
/* Some drivers like the Raspberry Pi ignore this flag in any case */
currentMode |= SPI_NO_CS;
}
if(params.csHigh) {
currentMode |= SPI_CS_HIGH;
}
/* Write adapted mode */
retval = ioctl(fileDescriptor, SPI_IOC_WR_MODE32, &currentMode);
if(retval != 0) {
utility::handleIoctlError("SpiComIF::initialiezInterface: Could not write full mode!");
}
} }
if(params.lsbFirst) { /* Write adapted mode */
retval = ioctl(fileDescriptor, SPI_IOC_WR_LSB_FIRST, &params.lsbFirst); retval = ioctl(fileDescriptor, SPI_IOC_WR_MODE32, &currentMode);
if(retval != 0) { if (retval != 0) {
utility::handleIoctlError("SpiComIF::initializeInterface: Setting LSB first failed"); utility::handleIoctlError("SpiComIF::initialiezInterface: Could not write full mode!");
}
} }
if(params.bitsPerWord != 8) { }
retval = ioctl(fileDescriptor, SPI_IOC_WR_BITS_PER_WORD, &params.bitsPerWord); if (params.lsbFirst) {
if(retval != 0) { retval = ioctl(fileDescriptor, SPI_IOC_WR_LSB_FIRST, &params.lsbFirst);
utility::handleIoctlError("SpiComIF::initializeInterface: " if (retval != 0) {
"Could not write bits per word!"); utility::handleIoctlError("SpiComIF::initializeInterface: Setting LSB first failed");
}
} }
return HasReturnvaluesIF::RETURN_OK; }
if (params.bitsPerWord != 8) {
retval = ioctl(fileDescriptor, SPI_IOC_WR_BITS_PER_WORD, &params.bitsPerWord);
if (retval != 0) {
utility::handleIoctlError(
"SpiComIF::initializeInterface: "
"Could not write bits per word!");
}
}
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) { ReturnValue_t SpiComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie); SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
return NULLPOINTER; return NULLPOINTER;
} }
if(sendLen > spiCookie->getMaxBufferSize()) { if (sendLen > spiCookie->getMaxBufferSize()) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Too much data sent, send length " << sendLen << sif::warning << "SpiComIF::sendMessage: Too much data sent, send length " << sendLen
"larger than maximum buffer length " << spiCookie->getMaxBufferSize() << std::endl; << "larger than maximum buffer length " << spiCookie->getMaxBufferSize()
<< std::endl;
#else #else
sif::printWarning("SpiComIF::sendMessage: Too much data sent, send length %lu larger " sif::printWarning(
"than maximum buffer length %lu!\n", static_cast<unsigned long>(sendLen), "SpiComIF::sendMessage: Too much data sent, send length %lu larger "
static_cast<unsigned long>(spiCookie->getMaxBufferSize())); "than maximum buffer length %lu!\n",
static_cast<unsigned long>(sendLen),
static_cast<unsigned long>(spiCookie->getMaxBufferSize()));
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return DeviceCommunicationIF::TOO_MUCH_DATA; return DeviceCommunicationIF::TOO_MUCH_DATA;
} }
if(spiCookie->getComIfMode() == spi::SpiComIfModes::REGULAR) { if (spiCookie->getComIfMode() == spi::SpiComIfModes::REGULAR) {
result = performRegularSendOperation(spiCookie, sendData, sendLen); result = performRegularSendOperation(spiCookie, sendData, sendLen);
} else if (spiCookie->getComIfMode() == spi::SpiComIfModes::CALLBACK) {
spi::send_callback_function_t sendFunc = nullptr;
void* funcArgs = nullptr;
spiCookie->getCallback(&sendFunc, &funcArgs);
if (sendFunc != nullptr) {
result = sendFunc(this, spiCookie, sendData, sendLen, funcArgs);
} }
else if(spiCookie->getComIfMode() == spi::SpiComIfModes::CALLBACK) { }
spi::send_callback_function_t sendFunc = nullptr; return result;
void* funcArgs = nullptr;
spiCookie->getCallback(&sendFunc, &funcArgs);
if(sendFunc != nullptr) {
result = sendFunc(this, spiCookie, sendData, sendLen, funcArgs);
}
}
return result;
} }
ReturnValue_t SpiComIF::performRegularSendOperation(SpiCookie *spiCookie, const uint8_t *sendData, ReturnValue_t SpiComIF::performRegularSendOperation(SpiCookie* spiCookie, const uint8_t* sendData,
size_t sendLen) { size_t sendLen) {
address_t spiAddress = spiCookie->getSpiAddress(); address_t spiAddress = spiCookie->getSpiAddress();
auto iter = spiDeviceMap.find(spiAddress); auto iter = spiDeviceMap.find(spiAddress);
if(iter != spiDeviceMap.end()) { if (iter != spiDeviceMap.end()) {
spiCookie->assignReadBuffer(iter->second.replyBuffer.data()); spiCookie->assignReadBuffer(iter->second.replyBuffer.data());
} }
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
int retval = 0; int retval = 0;
/* Prepare transfer */ /* Prepare transfer */
int fileDescriptor = 0; int fileDescriptor = 0;
std::string device = spiCookie->getSpiDevice(); std::string device = spiCookie->getSpiDevice();
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, "SpiComIF::sendMessage"); UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, "SpiComIF::sendMessage");
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) { if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return OPENING_FILE_FAILED; return OPENING_FILE_FAILED;
} }
spi::SpiModes spiMode = spi::SpiModes::MODE_0; spi::SpiModes spiMode = spi::SpiModes::MODE_0;
uint32_t spiSpeed = 0; uint32_t spiSpeed = 0;
spiCookie->getSpiParameters(spiMode, spiSpeed, nullptr); spiCookie->getSpiParameters(spiMode, spiSpeed, nullptr);
setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed); setSpiSpeedAndMode(fileDescriptor, spiMode, spiSpeed);
spiCookie->assignWriteBuffer(sendData); spiCookie->assignWriteBuffer(sendData);
spiCookie->setTransferSize(sendLen); spiCookie->setTransferSize(sendLen);
bool fullDuplex = spiCookie->isFullDuplex(); bool fullDuplex = spiCookie->isFullDuplex();
gpioId_t gpioId = spiCookie->getChipSelectPin(); gpioId_t gpioId = spiCookie->getChipSelectPin();
/* Pull SPI CS low. For now, no support for active high given */ /* Pull SPI CS low. For now, no support for active high given */
if(gpioId != gpio::NO_GPIO) { if (gpioId != gpio::NO_GPIO) {
result = spiMutex->lockMutex(timeoutType, timeoutMs); result = spiMutex->lockMutex(timeoutType, timeoutMs);
if (result != RETURN_OK) { if (result != RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::sendMessage: Failed to lock mutex" << std::endl; sif::error << "SpiComIF::sendMessage: Failed to lock mutex" << std::endl;
#else #else
sif::printError("SpiComIF::sendMessage: Failed to lock mutex\n"); sif::printError("SpiComIF::sendMessage: Failed to lock mutex\n");
#endif #endif
#endif #endif
return result; return result;
} }
ReturnValue_t result = gpioComIF->pullLow(gpioId); ReturnValue_t result = gpioComIF->pullLow(gpioId);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Pulling low CS pin failed" << std::endl; sif::warning << "SpiComIF::sendMessage: Pulling low CS pin failed" << std::endl;
#else #else
sif::printWarning("SpiComIF::sendMessage: Pulling low CS pin failed"); sif::printWarning("SpiComIF::sendMessage: Pulling low CS pin failed");
#endif #endif
#endif #endif
return result; return result;
}
} }
}
/* Execute transfer */ /* Execute transfer */
if(fullDuplex) { if (fullDuplex) {
/* Initiate a full duplex SPI transfer. */ /* Initiate a full duplex SPI transfer. */
retval = ioctl(fileDescriptor, SPI_IOC_MESSAGE(1), spiCookie->getTransferStructHandle()); retval = ioctl(fileDescriptor, SPI_IOC_MESSAGE(1), spiCookie->getTransferStructHandle());
if(retval < 0) { if (retval < 0) {
utility::handleIoctlError("SpiComIF::sendMessage: ioctl error."); utility::handleIoctlError("SpiComIF::sendMessage: ioctl error.");
result = FULL_DUPLEX_TRANSFER_FAILED; result = FULL_DUPLEX_TRANSFER_FAILED;
} }
#if FSFW_HAL_SPI_WIRETAPPING == 1 #if FSFW_HAL_SPI_WIRETAPPING == 1
performSpiWiretapping(spiCookie); performSpiWiretapping(spiCookie);
#endif /* FSFW_LINUX_SPI_WIRETAPPING == 1 */ #endif /* FSFW_LINUX_SPI_WIRETAPPING == 1 */
} } else {
else { /* We write with a blocking half-duplex transfer here */
/* We write with a blocking half-duplex transfer here */ if (write(fileDescriptor, sendData, sendLen) != static_cast<ssize_t>(sendLen)) {
if (write(fileDescriptor, sendData, sendLen) != static_cast<ssize_t>(sendLen)) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Half-Duplex write operation failed!" << sif::warning << "SpiComIF::sendMessage: Half-Duplex write operation failed!" << std::endl;
std::endl;
#else #else
sif::printWarning("SpiComIF::sendMessage: Half-Duplex write operation failed!\n"); sif::printWarning("SpiComIF::sendMessage: Half-Duplex write operation failed!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
result = HALF_DUPLEX_TRANSFER_FAILED; result = HALF_DUPLEX_TRANSFER_FAILED;
}
} }
}
if(gpioId != gpio::NO_GPIO) { if (gpioId != gpio::NO_GPIO) {
gpioComIF->pullHigh(gpioId); gpioComIF->pullHigh(gpioId);
result = spiMutex->unlockMutex(); result = spiMutex->unlockMutex();
if (result != RETURN_OK) { if (result != RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::sendMessage: Failed to unlock mutex" << std::endl; sif::error << "SpiComIF::sendMessage: Failed to unlock mutex" << std::endl;
#endif #endif
return result; return result;
}
} }
return result; }
return result;
} }
ReturnValue_t SpiComIF::getSendSuccess(CookieIF *cookie) { ReturnValue_t SpiComIF::getSendSuccess(CookieIF* cookie) { return HasReturnvaluesIF::RETURN_OK; }
ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
if (spiCookie == nullptr) {
return NULLPOINTER;
}
if (spiCookie->isFullDuplex()) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
}
return performHalfDuplexReception(spiCookie);
} }
ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
if(spiCookie == nullptr) {
return NULLPOINTER;
}
if(spiCookie->isFullDuplex()) {
return HasReturnvaluesIF::RETURN_OK;
}
return performHalfDuplexReception(spiCookie);
}
ReturnValue_t SpiComIF::performHalfDuplexReception(SpiCookie* spiCookie) { ReturnValue_t SpiComIF::performHalfDuplexReception(SpiCookie* spiCookie) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
std::string device = spiCookie->getSpiDevice(); std::string device = spiCookie->getSpiDevice();
int fileDescriptor = 0; int fileDescriptor = 0;
UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, UnixFileGuard fileHelper(device, &fileDescriptor, O_RDWR, "SpiComIF::requestReceiveMessage");
"SpiComIF::requestReceiveMessage"); if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
if(fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) { return OPENING_FILE_FAILED;
return OPENING_FILE_FAILED; }
}
uint8_t* rxBuf = nullptr; uint8_t* rxBuf = nullptr;
size_t readSize = spiCookie->getCurrentTransferSize(); size_t readSize = spiCookie->getCurrentTransferSize();
result = getReadBuffer(spiCookie->getSpiAddress(), &rxBuf); result = getReadBuffer(spiCookie->getSpiAddress(), &rxBuf);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
gpioId_t gpioId = spiCookie->getChipSelectPin(); gpioId_t gpioId = spiCookie->getChipSelectPin();
if(gpioId != gpio::NO_GPIO) { if (gpioId != gpio::NO_GPIO) {
result = spiMutex->lockMutex(timeoutType, timeoutMs); result = spiMutex->lockMutex(timeoutType, timeoutMs);
if (result != RETURN_OK) { if (result != RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::getSendSuccess: Failed to lock mutex" << std::endl; sif::error << "SpiComIF::getSendSuccess: Failed to lock mutex" << std::endl;
#endif #endif
return result; return result;
}
gpioComIF->pullLow(gpioId);
} }
gpioComIF->pullLow(gpioId);
}
if(read(fileDescriptor, rxBuf, readSize) != static_cast<ssize_t>(readSize)) { if (read(fileDescriptor, rxBuf, readSize) != static_cast<ssize_t>(readSize)) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Half-Duplex read operation failed!" << std::endl; sif::warning << "SpiComIF::sendMessage: Half-Duplex read operation failed!" << std::endl;
#else #else
sif::printWarning("SpiComIF::sendMessage: Half-Duplex read operation failed!\n"); sif::printWarning("SpiComIF::sendMessage: Half-Duplex read operation failed!\n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
result = HALF_DUPLEX_TRANSFER_FAILED; result = HALF_DUPLEX_TRANSFER_FAILED;
} }
if(gpioId != gpio::NO_GPIO) { if (gpioId != gpio::NO_GPIO) {
gpioComIF->pullHigh(gpioId); gpioComIF->pullHigh(gpioId);
result = spiMutex->unlockMutex(); result = spiMutex->unlockMutex();
if (result != RETURN_OK) { if (result != RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::getSendSuccess: Failed to unlock mutex" << std::endl; sif::error << "SpiComIF::getSendSuccess: Failed to unlock mutex" << std::endl;
#endif #endif
return result; return result;
}
} }
}
return result; return result;
} }
ReturnValue_t SpiComIF::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) { ReturnValue_t SpiComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie); SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie);
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
uint8_t* rxBuf = nullptr; uint8_t* rxBuf = nullptr;
ReturnValue_t result = getReadBuffer(spiCookie->getSpiAddress(), &rxBuf); ReturnValue_t result = getReadBuffer(spiCookie->getSpiAddress(), &rxBuf);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
*buffer = rxBuf; *buffer = rxBuf;
*size = spiCookie->getCurrentTransferSize(); *size = spiCookie->getCurrentTransferSize();
spiCookie->setTransferSize(0); spiCookie->setTransferSize(0);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
MutexIF* SpiComIF::getMutex(MutexIF::TimeoutType* timeoutType, uint32_t* timeoutMs) { MutexIF* SpiComIF::getMutex(MutexIF::TimeoutType* timeoutType, uint32_t* timeoutMs) {
if(timeoutType != nullptr) { if (timeoutType != nullptr) {
*timeoutType = this->timeoutType; *timeoutType = this->timeoutType;
} }
if(timeoutMs != nullptr) { if (timeoutMs != nullptr) {
*timeoutMs = this->timeoutMs; *timeoutMs = this->timeoutMs;
} }
return spiMutex; return spiMutex;
} }
void SpiComIF::performSpiWiretapping(SpiCookie* spiCookie) { void SpiComIF::performSpiWiretapping(SpiCookie* spiCookie) {
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
return; return;
} }
size_t dataLen = spiCookie->getTransferStructHandle()->len; size_t dataLen = spiCookie->getTransferStructHandle()->len;
uint8_t* dataPtr = reinterpret_cast<uint8_t*>(spiCookie->getTransferStructHandle()->tx_buf); uint8_t* dataPtr = reinterpret_cast<uint8_t*>(spiCookie->getTransferStructHandle()->tx_buf);
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "Sent SPI data: " << std::endl; sif::info << "Sent SPI data: " << std::endl;
arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false); arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false);
sif::info << "Received SPI data: " << std::endl; sif::info << "Received SPI data: " << std::endl;
#else #else
sif::printInfo("Sent SPI data: \n"); sif::printInfo("Sent SPI data: \n");
arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false); arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false);
sif::printInfo("Received SPI data: \n"); sif::printInfo("Received SPI data: \n");
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
dataPtr = reinterpret_cast<uint8_t*>(spiCookie->getTransferStructHandle()->rx_buf); dataPtr = reinterpret_cast<uint8_t*>(spiCookie->getTransferStructHandle()->rx_buf);
arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false); arrayprinter::print(dataPtr, dataLen, OutputType::HEX, false);
} }
ReturnValue_t SpiComIF::getReadBuffer(address_t spiAddress, uint8_t** buffer) { ReturnValue_t SpiComIF::getReadBuffer(address_t spiAddress, uint8_t** buffer) {
if(buffer == nullptr) { if (buffer == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
auto iter = spiDeviceMap.find(spiAddress); auto iter = spiDeviceMap.find(spiAddress);
if(iter == spiDeviceMap.end()) { if (iter == spiDeviceMap.end()) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
*buffer = iter->second.replyBuffer.data(); *buffer = iter->second.replyBuffer.data();
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
GpioIF* SpiComIF::getGpioInterface() { GpioIF* SpiComIF::getGpioInterface() { return gpioComIF; }
return gpioComIF;
}
void SpiComIF::setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed) { void SpiComIF::setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed) {
int retval = ioctl(spiFd, SPI_IOC_WR_MODE, reinterpret_cast<uint8_t*>(&mode)); int retval = ioctl(spiFd, SPI_IOC_WR_MODE, reinterpret_cast<uint8_t*>(&mode));
if(retval != 0) { if (retval != 0) {
utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI mode failed"); utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI mode failed");
} }
retval = ioctl(spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); retval = ioctl(spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if(retval != 0) { if (retval != 0) {
utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI speed failed"); utility::handleIoctlError("SpiComIF::setSpiSpeedAndMode: Setting SPI speed failed");
} }
} }

View File

@ -1,16 +1,15 @@
#ifndef LINUX_SPI_SPICOMIF_H_ #ifndef LINUX_SPI_SPICOMIF_H_
#define LINUX_SPI_SPICOMIF_H_ #define LINUX_SPI_SPICOMIF_H_
#include "fsfw/FSFW.h" #include <unordered_map>
#include "spiDefinitions.h" #include <vector>
#include "returnvalues/classIds.h"
#include "fsfw_hal/common/gpio/GpioIF.h"
#include "fsfw/FSFW.h"
#include "fsfw/devicehandlers/DeviceCommunicationIF.h" #include "fsfw/devicehandlers/DeviceCommunicationIF.h"
#include "fsfw/objectmanager/SystemObject.h" #include "fsfw/objectmanager/SystemObject.h"
#include "fsfw_hal/common/gpio/GpioIF.h"
#include <vector> #include "returnvalues/classIds.h"
#include <unordered_map> #include "spiDefinitions.h"
class SpiCookie; class SpiCookie;
@ -21,71 +20,67 @@ class SpiCookie;
* are contained in the SPI cookie. * are contained in the SPI cookie.
* @author R. Mueller * @author R. Mueller
*/ */
class SpiComIF: public DeviceCommunicationIF, public SystemObject { class SpiComIF : public DeviceCommunicationIF, public SystemObject {
public: public:
static constexpr uint8_t spiRetvalId = CLASS_ID::HAL_SPI; static constexpr uint8_t spiRetvalId = CLASS_ID::HAL_SPI;
static constexpr ReturnValue_t OPENING_FILE_FAILED = static constexpr ReturnValue_t OPENING_FILE_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0); HasReturnvaluesIF::makeReturnCode(spiRetvalId, 0);
/* Full duplex (ioctl) transfer failure */ /* Full duplex (ioctl) transfer failure */
static constexpr ReturnValue_t FULL_DUPLEX_TRANSFER_FAILED = static constexpr ReturnValue_t FULL_DUPLEX_TRANSFER_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 1); HasReturnvaluesIF::makeReturnCode(spiRetvalId, 1);
/* Half duplex (read/write) transfer failure */ /* Half duplex (read/write) transfer failure */
static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED = static constexpr ReturnValue_t HALF_DUPLEX_TRANSFER_FAILED =
HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2); HasReturnvaluesIF::makeReturnCode(spiRetvalId, 2);
SpiComIF(object_id_t objectId, GpioIF* gpioComIF); SpiComIF(object_id_t objectId, GpioIF* gpioComIF);
ReturnValue_t initializeInterface(CookieIF * cookie) override; ReturnValue_t initializeInterface(CookieIF* cookie) override;
ReturnValue_t sendMessage(CookieIF *cookie,const uint8_t *sendData, ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
size_t sendLen) override; ReturnValue_t getSendSuccess(CookieIF* cookie) override;
ReturnValue_t getSendSuccess(CookieIF *cookie) override; ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
ReturnValue_t requestReceiveMessage(CookieIF *cookie, ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) override;
size_t requestLen) override;
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
size_t *size) override;
/** /**
* @brief This function returns the mutex which can be used to protect the spi bus when * @brief This function returns the mutex which can be used to protect the spi bus when
* the chip select must be driven from outside of the com if. * the chip select must be driven from outside of the com if.
*/ */
MutexIF* getMutex(MutexIF::TimeoutType* timeoutType = nullptr, uint32_t* timeoutMs = nullptr); MutexIF* getMutex(MutexIF::TimeoutType* timeoutType = nullptr, uint32_t* timeoutMs = nullptr);
/** /**
* Perform a regular send operation using Linux iotcl. This is public so it can be used * Perform a regular send operation using Linux iotcl. This is public so it can be used
* in functions like a user callback if special handling is only necessary for certain commands. * in functions like a user callback if special handling is only necessary for certain commands.
* @param spiCookie * @param spiCookie
* @param sendData * @param sendData
* @param sendLen * @param sendLen
* @return * @return
*/ */
ReturnValue_t performRegularSendOperation(SpiCookie* spiCookie, const uint8_t *sendData, ReturnValue_t performRegularSendOperation(SpiCookie* spiCookie, const uint8_t* sendData,
size_t sendLen); size_t sendLen);
GpioIF* getGpioInterface(); GpioIF* getGpioInterface();
void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed); void setSpiSpeedAndMode(int spiFd, spi::SpiModes mode, uint32_t speed);
void performSpiWiretapping(SpiCookie* spiCookie); void performSpiWiretapping(SpiCookie* spiCookie);
ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer); ReturnValue_t getReadBuffer(address_t spiAddress, uint8_t** buffer);
private: private:
struct SpiInstance {
SpiInstance(size_t maxRecvSize) : replyBuffer(std::vector<uint8_t>(maxRecvSize)) {}
std::vector<uint8_t> replyBuffer;
};
struct SpiInstance { GpioIF* gpioComIF = nullptr;
SpiInstance(size_t maxRecvSize): replyBuffer(std::vector<uint8_t>(maxRecvSize)) {}
std::vector<uint8_t> replyBuffer;
};
GpioIF* gpioComIF = nullptr; MutexIF* spiMutex = nullptr;
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING;
uint32_t timeoutMs = 20;
MutexIF* spiMutex = nullptr; using SpiDeviceMap = std::unordered_map<address_t, SpiInstance>;
MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::WAITING; using SpiDeviceMapIter = SpiDeviceMap::iterator;
uint32_t timeoutMs = 20;
using SpiDeviceMap = std::unordered_map<address_t, SpiInstance>; SpiDeviceMap spiDeviceMap;
using SpiDeviceMapIter = SpiDeviceMap::iterator;
SpiDeviceMap spiDeviceMap; ReturnValue_t performHalfDuplexReception(SpiCookie* spiCookie);
ReturnValue_t performHalfDuplexReception(SpiCookie* spiCookie);
}; };
#endif /* LINUX_SPI_SPICOMIF_H_ */ #endif /* LINUX_SPI_SPICOMIF_H_ */

View File

@ -1,144 +1,109 @@
#include "fsfw_hal/linux/spi/SpiCookie.h" #include "fsfw_hal/linux/spi/SpiCookie.h"
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev,
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed): const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed)
SpiCookie(spi::SpiComIfModes::REGULAR, spiAddress, chipSelect, spiDev, maxSize, spiMode, : SpiCookie(spi::SpiComIfModes::REGULAR, spiAddress, chipSelect, spiDev, maxSize, spiMode,
spiSpeed, nullptr, nullptr) { spiSpeed, nullptr, nullptr) {}
}
SpiCookie::SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxSize, SpiCookie::SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxSize,
spi::SpiModes spiMode, uint32_t spiSpeed): spi::SpiModes spiMode, uint32_t spiSpeed)
SpiCookie(spiAddress, gpio::NO_GPIO, spiDev, maxSize, spiMode, spiSpeed) { : SpiCookie(spiAddress, gpio::NO_GPIO, spiDev, maxSize, spiMode, spiSpeed) {}
}
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev,
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed,
spi::send_callback_function_t callback, void *args): spi::send_callback_function_t callback, void* args)
SpiCookie(spi::SpiComIfModes::CALLBACK, spiAddress, chipSelect, spiDev, maxSize, : SpiCookie(spi::SpiComIfModes::CALLBACK, spiAddress, chipSelect, spiDev, maxSize, spiMode,
spiMode, spiSpeed, callback, args) { spiSpeed, callback, args) {}
}
SpiCookie::SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect, SpiCookie::SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect,
std::string spiDev, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed, std::string spiDev, const size_t maxSize, spi::SpiModes spiMode,
spi::send_callback_function_t callback, void* args): uint32_t spiSpeed, spi::send_callback_function_t callback, void* args)
spiAddress(spiAddress), chipSelectPin(chipSelect), spiDevice(spiDev), : spiAddress(spiAddress),
comIfMode(comIfMode), maxSize(maxSize), spiMode(spiMode), spiSpeed(spiSpeed), chipSelectPin(chipSelect),
sendCallback(callback), callbackArgs(args) { spiDevice(spiDev),
} comIfMode(comIfMode),
maxSize(maxSize),
spiMode(spiMode),
spiSpeed(spiSpeed),
sendCallback(callback),
callbackArgs(args) {}
spi::SpiComIfModes SpiCookie::getComIfMode() const { spi::SpiComIfModes SpiCookie::getComIfMode() const { return this->comIfMode; }
return this->comIfMode;
}
void SpiCookie::getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed, void SpiCookie::getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed,
UncommonParameters* parameters) const { UncommonParameters* parameters) const {
spiMode = this->spiMode; spiMode = this->spiMode;
spiSpeed = this->spiSpeed; spiSpeed = this->spiSpeed;
if(parameters != nullptr) { if (parameters != nullptr) {
parameters->threeWireSpi = uncommonParameters.threeWireSpi; parameters->threeWireSpi = uncommonParameters.threeWireSpi;
parameters->lsbFirst = uncommonParameters.lsbFirst; parameters->lsbFirst = uncommonParameters.lsbFirst;
parameters->noCs = uncommonParameters.noCs; parameters->noCs = uncommonParameters.noCs;
parameters->bitsPerWord = uncommonParameters.bitsPerWord; parameters->bitsPerWord = uncommonParameters.bitsPerWord;
parameters->csHigh = uncommonParameters.csHigh; parameters->csHigh = uncommonParameters.csHigh;
} }
} }
gpioId_t SpiCookie::getChipSelectPin() const { gpioId_t SpiCookie::getChipSelectPin() const { return chipSelectPin; }
return chipSelectPin;
}
size_t SpiCookie::getMaxBufferSize() const { size_t SpiCookie::getMaxBufferSize() const { return maxSize; }
return maxSize;
}
address_t SpiCookie::getSpiAddress() const { address_t SpiCookie::getSpiAddress() const { return spiAddress; }
return spiAddress;
}
std::string SpiCookie::getSpiDevice() const { std::string SpiCookie::getSpiDevice() const { return spiDevice; }
return spiDevice;
}
void SpiCookie::setThreeWireSpi(bool enable) { void SpiCookie::setThreeWireSpi(bool enable) { uncommonParameters.threeWireSpi = enable; }
uncommonParameters.threeWireSpi = enable;
}
void SpiCookie::setLsbFirst(bool enable) { void SpiCookie::setLsbFirst(bool enable) { uncommonParameters.lsbFirst = enable; }
uncommonParameters.lsbFirst = enable;
}
void SpiCookie::setNoCs(bool enable) { void SpiCookie::setNoCs(bool enable) { uncommonParameters.noCs = enable; }
uncommonParameters.noCs = enable;
}
void SpiCookie::setBitsPerWord(uint8_t bitsPerWord) { void SpiCookie::setBitsPerWord(uint8_t bitsPerWord) {
uncommonParameters.bitsPerWord = bitsPerWord; uncommonParameters.bitsPerWord = bitsPerWord;
} }
void SpiCookie::setCsHigh(bool enable) { void SpiCookie::setCsHigh(bool enable) { uncommonParameters.csHigh = enable; }
uncommonParameters.csHigh = enable;
}
void SpiCookie::activateCsDeselect(bool deselectCs, uint16_t delayUsecs) { void SpiCookie::activateCsDeselect(bool deselectCs, uint16_t delayUsecs) {
spiTransferStruct.cs_change = deselectCs; spiTransferStruct.cs_change = deselectCs;
spiTransferStruct.delay_usecs = delayUsecs; spiTransferStruct.delay_usecs = delayUsecs;
} }
void SpiCookie::assignReadBuffer(uint8_t* rx) { void SpiCookie::assignReadBuffer(uint8_t* rx) {
if(rx != nullptr) { if (rx != nullptr) {
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(rx); spiTransferStruct.rx_buf = reinterpret_cast<__u64>(rx);
} }
} }
void SpiCookie::assignWriteBuffer(const uint8_t* tx) { void SpiCookie::assignWriteBuffer(const uint8_t* tx) {
if(tx != nullptr) { if (tx != nullptr) {
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(tx); spiTransferStruct.tx_buf = reinterpret_cast<__u64>(tx);
} }
} }
void SpiCookie::setCallbackMode(spi::send_callback_function_t callback, void SpiCookie::setCallbackMode(spi::send_callback_function_t callback, void* args) {
void *args) { this->comIfMode = spi::SpiComIfModes::CALLBACK;
this->comIfMode = spi::SpiComIfModes::CALLBACK; this->sendCallback = callback;
this->sendCallback = callback; this->callbackArgs = args;
this->callbackArgs = args;
} }
void SpiCookie::setCallbackArgs(void *args) { void SpiCookie::setCallbackArgs(void* args) { this->callbackArgs = args; }
this->callbackArgs = args;
}
spi_ioc_transfer* SpiCookie::getTransferStructHandle() { spi_ioc_transfer* SpiCookie::getTransferStructHandle() { return &spiTransferStruct; }
return &spiTransferStruct;
}
void SpiCookie::setFullOrHalfDuplex(bool halfDuplex) { void SpiCookie::setFullOrHalfDuplex(bool halfDuplex) { this->halfDuplex = halfDuplex; }
this->halfDuplex = halfDuplex;
}
bool SpiCookie::isFullDuplex() const { bool SpiCookie::isFullDuplex() const { return not this->halfDuplex; }
return not this->halfDuplex;
}
void SpiCookie::setTransferSize(size_t transferSize) { void SpiCookie::setTransferSize(size_t transferSize) { spiTransferStruct.len = transferSize; }
spiTransferStruct.len = transferSize;
}
size_t SpiCookie::getCurrentTransferSize() const { size_t SpiCookie::getCurrentTransferSize() const { return spiTransferStruct.len; }
return spiTransferStruct.len;
}
void SpiCookie::setSpiSpeed(uint32_t newSpeed) { void SpiCookie::setSpiSpeed(uint32_t newSpeed) { this->spiSpeed = newSpeed; }
this->spiSpeed = newSpeed;
}
void SpiCookie::setSpiMode(spi::SpiModes newMode) { void SpiCookie::setSpiMode(spi::SpiModes newMode) { this->spiMode = newMode; }
this->spiMode = newMode;
}
void SpiCookie::getCallback(spi::send_callback_function_t *callback, void SpiCookie::getCallback(spi::send_callback_function_t* callback, void** args) {
void **args) { *callback = this->sendCallback;
*callback = this->sendCallback; *args = this->callbackArgs;
*args = this->callbackArgs;
} }

View File

@ -1,13 +1,12 @@
#ifndef LINUX_SPI_SPICOOKIE_H_ #ifndef LINUX_SPI_SPICOOKIE_H_
#define LINUX_SPI_SPICOOKIE_H_ #define LINUX_SPI_SPICOOKIE_H_
#include "spiDefinitions.h"
#include "../../common/gpio/gpioDefinitions.h"
#include <fsfw/devicehandlers/CookieIF.h> #include <fsfw/devicehandlers/CookieIF.h>
#include <linux/spi/spidev.h> #include <linux/spi/spidev.h>
#include "../../common/gpio/gpioDefinitions.h"
#include "spiDefinitions.h"
/** /**
* @brief This cookie class is passed to the SPI communication interface * @brief This cookie class is passed to the SPI communication interface
* @details * @details
@ -19,165 +18,163 @@
* special requirements like expander slave select switching (e.g. GPIO or I2C expander) * special requirements like expander slave select switching (e.g. GPIO or I2C expander)
* or special timing related requirements. * or special timing related requirements.
*/ */
class SpiCookie: public CookieIF { class SpiCookie : public CookieIF {
public: public:
/** /**
* Each SPI device will have a corresponding cookie. The cookie is used by the communication * Each SPI device will have a corresponding cookie. The cookie is used by the communication
* interface and contains device specific information like the largest expected size to be * interface and contains device specific information like the largest expected size to be
* sent and received and the GPIO pin used to toggle the SPI slave select pin. * sent and received and the GPIO pin used to toggle the SPI slave select pin.
* @param spiAddress * @param spiAddress
* @param chipSelect Chip select. gpio::NO_GPIO can be used for hardware slave selects. * @param chipSelect Chip select. gpio::NO_GPIO can be used for hardware slave selects.
* @param spiDev * @param spiDev
* @param maxSize * @param maxSize
*/ */
SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, const size_t maxSize,
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed);
/**
* Like constructor above, but without a dedicated GPIO CS. Can be used for hardware
* slave select or if CS logic is performed with decoders.
*/
SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxReplySize,
spi::SpiModes spiMode, uint32_t spiSpeed); spi::SpiModes spiMode, uint32_t spiSpeed);
/** /**
* Use the callback mode of the SPI communication interface. The user can pass the callback * Like constructor above, but without a dedicated GPIO CS. Can be used for hardware
* function here or by using the setter function #setCallbackMode * slave select or if CS logic is performed with decoders.
*/ */
SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, const size_t maxSize, SpiCookie(address_t spiAddress, std::string spiDev, const size_t maxReplySize,
spi::SpiModes spiMode, uint32_t spiSpeed);
/**
* Use the callback mode of the SPI communication interface. The user can pass the callback
* function here or by using the setter function #setCallbackMode
*/
SpiCookie(address_t spiAddress, gpioId_t chipSelect, std::string spiDev, const size_t maxSize,
spi::SpiModes spiMode, uint32_t spiSpeed, spi::send_callback_function_t callback, spi::SpiModes spiMode, uint32_t spiSpeed, spi::send_callback_function_t callback,
void *args); void* args);
/** /**
* Get the callback function * Get the callback function
* @param callback * @param callback
* @param args * @param args
*/ */
void getCallback(spi::send_callback_function_t* callback, void** args); void getCallback(spi::send_callback_function_t* callback, void** args);
address_t getSpiAddress() const; address_t getSpiAddress() const;
std::string getSpiDevice() const; std::string getSpiDevice() const;
gpioId_t getChipSelectPin() const; gpioId_t getChipSelectPin() const;
size_t getMaxBufferSize() const; size_t getMaxBufferSize() const;
spi::SpiComIfModes getComIfMode() const; spi::SpiComIfModes getComIfMode() const;
/** Enables changing SPI speed at run-time */ /** Enables changing SPI speed at run-time */
void setSpiSpeed(uint32_t newSpeed); void setSpiSpeed(uint32_t newSpeed);
/** Enables changing the SPI mode at run-time */ /** Enables changing the SPI mode at run-time */
void setSpiMode(spi::SpiModes newMode); void setSpiMode(spi::SpiModes newMode);
/** /**
* Set the SPI to callback mode and assigns the user supplied callback and an argument * Set the SPI to callback mode and assigns the user supplied callback and an argument
* passed to the callback. * passed to the callback.
* @param callback * @param callback
* @param args * @param args
*/ */
void setCallbackMode(spi::send_callback_function_t callback, void* args); void setCallbackMode(spi::send_callback_function_t callback, void* args);
/** /**
* Can be used to set the callback arguments and a later point than initialization. * Can be used to set the callback arguments and a later point than initialization.
* @param args * @param args
*/ */
void setCallbackArgs(void* args); void setCallbackArgs(void* args);
/** /**
* True if SPI transfers should be performed in full duplex mode * True if SPI transfers should be performed in full duplex mode
* @return * @return
*/ */
bool isFullDuplex() const; bool isFullDuplex() const;
/** /**
* Set transfer type to full duplex or half duplex. Full duplex is the default setting, * Set transfer type to full duplex or half duplex. Full duplex is the default setting,
* ressembling common SPI hardware implementation with shift registers, where read and writes * ressembling common SPI hardware implementation with shift registers, where read and writes
* happen simultaneosly. * happen simultaneosly.
* @param fullDuplex * @param fullDuplex
*/ */
void setFullOrHalfDuplex(bool halfDuplex); void setFullOrHalfDuplex(bool halfDuplex);
/** /**
* This needs to be called to specify where the SPI driver writes to or reads from. * This needs to be called to specify where the SPI driver writes to or reads from.
* @param readLocation * @param readLocation
* @param writeLocation * @param writeLocation
*/ */
void assignReadBuffer(uint8_t* rx); void assignReadBuffer(uint8_t* rx);
void assignWriteBuffer(const uint8_t* tx); void assignWriteBuffer(const uint8_t* tx);
/** /**
* Set size for the next transfer. Set to 0 for no transfer * Set size for the next transfer. Set to 0 for no transfer
* @param transferSize * @param transferSize
*/ */
void setTransferSize(size_t transferSize); void setTransferSize(size_t transferSize);
size_t getCurrentTransferSize() const; size_t getCurrentTransferSize() const;
struct UncommonParameters { struct UncommonParameters {
uint8_t bitsPerWord = 8; uint8_t bitsPerWord = 8;
bool noCs = false; bool noCs = false;
bool csHigh = false; bool csHigh = false;
bool threeWireSpi = false; bool threeWireSpi = false;
/* MSB first is more common */ /* MSB first is more common */
bool lsbFirst = false; bool lsbFirst = false;
}; };
/** /**
* Can be used to explicitely disable hardware chip select. * Can be used to explicitely disable hardware chip select.
* Some drivers like the Raspberry Pi Linux driver will not use hardware chip select by default * Some drivers like the Raspberry Pi Linux driver will not use hardware chip select by default
* (see https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md) * (see https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md)
* @param enable * @param enable
*/ */
void setNoCs(bool enable); void setNoCs(bool enable);
void setThreeWireSpi(bool enable); void setThreeWireSpi(bool enable);
void setLsbFirst(bool enable); void setLsbFirst(bool enable);
void setCsHigh(bool enable); void setCsHigh(bool enable);
void setBitsPerWord(uint8_t bitsPerWord); void setBitsPerWord(uint8_t bitsPerWord);
void getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed, void getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed,
UncommonParameters* parameters = nullptr) const; UncommonParameters* parameters = nullptr) const;
/** /**
* See spidev.h cs_change and delay_usecs * See spidev.h cs_change and delay_usecs
* @param deselectCs * @param deselectCs
* @param delayUsecs * @param delayUsecs
*/ */
void activateCsDeselect(bool deselectCs, uint16_t delayUsecs); void activateCsDeselect(bool deselectCs, uint16_t delayUsecs);
spi_ioc_transfer* getTransferStructHandle(); spi_ioc_transfer* getTransferStructHandle();
private:
/** private:
* Internal constructor which initializes every field /**
* @param spiAddress * Internal constructor which initializes every field
* @param chipSelect * @param spiAddress
* @param spiDev * @param chipSelect
* @param maxSize * @param spiDev
* @param spiMode * @param maxSize
* @param spiSpeed * @param spiMode
* @param callback * @param spiSpeed
* @param args * @param callback
*/ * @param args
SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect, */
SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect,
std::string spiDev, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed, std::string spiDev, const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed,
spi::send_callback_function_t callback, void* args); spi::send_callback_function_t callback, void* args);
address_t spiAddress; address_t spiAddress;
gpioId_t chipSelectPin; gpioId_t chipSelectPin;
std::string spiDevice; std::string spiDevice;
spi::SpiComIfModes comIfMode; spi::SpiComIfModes comIfMode;
// Required for regular mode // Required for regular mode
const size_t maxSize; const size_t maxSize;
spi::SpiModes spiMode; spi::SpiModes spiMode;
uint32_t spiSpeed; uint32_t spiSpeed;
bool halfDuplex = false; bool halfDuplex = false;
// Required for callback mode // Required for callback mode
spi::send_callback_function_t sendCallback = nullptr; spi::send_callback_function_t sendCallback = nullptr;
void* callbackArgs = nullptr; void* callbackArgs = nullptr;
struct spi_ioc_transfer spiTransferStruct = {}; struct spi_ioc_transfer spiTransferStruct = {};
UncommonParameters uncommonParameters; UncommonParameters uncommonParameters;
}; };
#endif /* LINUX_SPI_SPICOOKIE_H_ */ #endif /* LINUX_SPI_SPICOOKIE_H_ */

View File

@ -1,28 +1,25 @@
#ifndef LINUX_SPI_SPIDEFINITONS_H_ #ifndef LINUX_SPI_SPIDEFINITONS_H_
#define LINUX_SPI_SPIDEFINITONS_H_ #define LINUX_SPI_SPIDEFINITONS_H_
#include "../../common/gpio/gpioDefinitions.h"
#include "../../common/spi/spiCommon.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include <linux/spi/spidev.h> #include <linux/spi/spidev.h>
#include <cstdint> #include <cstdint>
#include "../../common/gpio/gpioDefinitions.h"
#include "../../common/spi/spiCommon.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
class SpiCookie; class SpiCookie;
class SpiComIF; class SpiComIF;
namespace spi { namespace spi {
enum SpiComIfModes { enum SpiComIfModes { REGULAR, CALLBACK };
REGULAR,
CALLBACK
};
using send_callback_function_t = ReturnValue_t (*)(SpiComIF* comIf, SpiCookie* cookie,
const uint8_t* sendData, size_t sendLen,
void* args);
using send_callback_function_t = ReturnValue_t (*) (SpiComIF* comIf, SpiCookie *cookie, } // namespace spi
const uint8_t *sendData, size_t sendLen, void* args);
}
#endif /* LINUX_SPI_SPIDEFINITONS_H_ */ #endif /* LINUX_SPI_SPIDEFINITONS_H_ */

View File

@ -1,569 +1,557 @@
#include "UartComIF.h" #include "UartComIF.h"
#include "fsfw/FSFW.h"
#include "fsfw_hal/linux/utility.h"
#include "fsfw/serviceinterface.h"
#include <cstring>
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
UartComIF::UartComIF(object_id_t objectId): SystemObject(objectId){ #include <cstring>
}
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
#include "fsfw_hal/linux/utility.h"
UartComIF::UartComIF(object_id_t objectId) : SystemObject(objectId) {}
UartComIF::~UartComIF() {} UartComIF::~UartComIF() {}
ReturnValue_t UartComIF::initializeInterface(CookieIF* cookie) { ReturnValue_t UartComIF::initializeInterface(CookieIF* cookie) {
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
std::string deviceFile; if (cookie == nullptr) {
UartDeviceMapIter uartDeviceMapIter; return NULLPOINTER;
}
if(cookie == nullptr) { UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
return NULLPOINTER; if (uartCookie == nullptr) {
}
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if (uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UartComIF::initializeInterface: Invalid UART Cookie!" << std::endl; sif::error << "UartComIF::initializeInterface: Invalid UART Cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) {
int fileDescriptor = configureUartPort(uartCookie);
if (fileDescriptor < 0) {
return RETURN_FAILED;
} }
size_t maxReplyLen = uartCookie->getMaxReplyLen();
deviceFile = uartCookie->getDeviceFile(); UartElements uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
auto status = uartDeviceMap.emplace(deviceFile, uartElements);
uartDeviceMapIter = uartDeviceMap.find(deviceFile); if (status.second == false) {
if(uartDeviceMapIter == uartDeviceMap.end()) {
int fileDescriptor = configureUartPort(uartCookie);
if (fileDescriptor < 0) {
return RETURN_FAILED;
}
size_t maxReplyLen = uartCookie->getMaxReplyLen();
UartElements uartElements = {fileDescriptor, std::vector<uint8_t>(maxReplyLen), 0};
auto status = uartDeviceMap.emplace(deviceFile, uartElements);
if (status.second == false) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::initializeInterface: Failed to insert device " << sif::warning << "UartComIF::initializeInterface: Failed to insert device " << deviceFile
deviceFile << "to UART device map" << std::endl; << "to UART device map" << std::endl;
#endif #endif
return RETURN_FAILED; return RETURN_FAILED;
}
} }
else { } else {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::initializeInterface: UART device " << deviceFile << sif::warning << "UartComIF::initializeInterface: UART device " << deviceFile
" already in use" << std::endl; << " already in use" << std::endl;
#endif #endif
return RETURN_FAILED; return RETURN_FAILED;
} }
return RETURN_OK; return RETURN_OK;
} }
int UartComIF::configureUartPort(UartCookie* uartCookie) { int UartComIF::configureUartPort(UartCookie* uartCookie) {
struct termios options = {};
struct termios options = {}; std::string deviceFile = uartCookie->getDeviceFile();
int flags = O_RDWR;
if (uartCookie->getUartMode() == UartModes::CANONICAL) {
// In non-canonical mode, don't specify O_NONBLOCK because these properties will be
// controlled by the VTIME and VMIN parameters and O_NONBLOCK would override this
flags |= O_NONBLOCK;
}
int fd = open(deviceFile.c_str(), flags);
std::string deviceFile = uartCookie->getDeviceFile(); if (fd < 0) {
int flags = O_RDWR;
if(uartCookie->getUartMode() == UartModes::CANONICAL) {
// In non-canonical mode, don't specify O_NONBLOCK because these properties will be
// controlled by the VTIME and VMIN parameters and O_NONBLOCK would override this
flags |= O_NONBLOCK;
}
int fd = open(deviceFile.c_str(), flags);
if (fd < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::configureUartPort: Failed to open uart " << deviceFile << sif::warning << "UartComIF::configureUartPort: Failed to open uart " << deviceFile
"with error code " << errno << strerror(errno) << std::endl; << "with error code " << errno << strerror(errno) << std::endl;
#endif #endif
return fd;
}
/* Read in existing settings */
if(tcgetattr(fd, &options) != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::configureUartPort: Error " << errno << "from tcgetattr: "
<< strerror(errno) << std::endl;
#endif
return fd;
}
setParityOptions(&options, uartCookie);
setStopBitOptions(&options, uartCookie);
setDatasizeOptions(&options, uartCookie);
setFixedOptions(&options);
setUartMode(&options, *uartCookie);
if(uartCookie->getInputShouldBeFlushed()) {
tcflush(fd, TCIFLUSH);
}
/* Sets uart to non-blocking mode. Read returns immediately when there are no data available */
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
configureBaudrate(&options, uartCookie);
/* Save option settings */
if (tcsetattr(fd, TCSANOW, &options) != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::configureUartPort: Failed to set options with error " <<
errno << ": " << strerror(errno);
#endif
return fd;
}
return fd; return fd;
}
/* Read in existing settings */
if (tcgetattr(fd, &options) != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::configureUartPort: Error " << errno
<< "from tcgetattr: " << strerror(errno) << std::endl;
#endif
return fd;
}
setParityOptions(&options, uartCookie);
setStopBitOptions(&options, uartCookie);
setDatasizeOptions(&options, uartCookie);
setFixedOptions(&options);
setUartMode(&options, *uartCookie);
if (uartCookie->getInputShouldBeFlushed()) {
tcflush(fd, TCIFLUSH);
}
/* Sets uart to non-blocking mode. Read returns immediately when there are no data available */
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
configureBaudrate(&options, uartCookie);
/* Save option settings */
if (tcsetattr(fd, TCSANOW, &options) != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::configureUartPort: Failed to set options with error " << errno
<< ": " << strerror(errno);
#endif
return fd;
}
return fd;
} }
void UartComIF::setParityOptions(struct termios* options, UartCookie* uartCookie) { void UartComIF::setParityOptions(struct termios* options, UartCookie* uartCookie) {
/* Clear parity bit */ /* Clear parity bit */
options->c_cflag &= ~PARENB; options->c_cflag &= ~PARENB;
switch (uartCookie->getParity()) { switch (uartCookie->getParity()) {
case Parity::EVEN: case Parity::EVEN:
options->c_cflag |= PARENB; options->c_cflag |= PARENB;
options->c_cflag &= ~PARODD; options->c_cflag &= ~PARODD;
break; break;
case Parity::ODD: case Parity::ODD:
options->c_cflag |= PARENB; options->c_cflag |= PARENB;
options->c_cflag |= PARODD; options->c_cflag |= PARODD;
break; break;
default: default:
break; break;
} }
} }
void UartComIF::setStopBitOptions(struct termios* options, UartCookie* uartCookie) { void UartComIF::setStopBitOptions(struct termios* options, UartCookie* uartCookie) {
/* Clear stop field. Sets stop bit to one bit */ /* Clear stop field. Sets stop bit to one bit */
options->c_cflag &= ~CSTOPB; options->c_cflag &= ~CSTOPB;
switch (uartCookie->getStopBits()) { switch (uartCookie->getStopBits()) {
case StopBits::TWO_STOP_BITS: case StopBits::TWO_STOP_BITS:
options->c_cflag |= CSTOPB; options->c_cflag |= CSTOPB;
break; break;
default: default:
break; break;
} }
} }
void UartComIF::setDatasizeOptions(struct termios* options, UartCookie* uartCookie) { void UartComIF::setDatasizeOptions(struct termios* options, UartCookie* uartCookie) {
/* Clear size bits */ /* Clear size bits */
options->c_cflag &= ~CSIZE; options->c_cflag &= ~CSIZE;
switch (uartCookie->getBitsPerWord()) { switch (uartCookie->getBitsPerWord()) {
case 5: case 5:
options->c_cflag |= CS5; options->c_cflag |= CS5;
break; break;
case 6: case 6:
options->c_cflag |= CS6; options->c_cflag |= CS6;
break; break;
case 7: case 7:
options->c_cflag |= CS7; options->c_cflag |= CS7;
break; break;
case 8: case 8:
options->c_cflag |= CS8; options->c_cflag |= CS8;
break; break;
default: default:
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl; sif::warning << "UartComIF::setDatasizeOptions: Invalid size specified" << std::endl;
#endif #endif
break; break;
} }
} }
void UartComIF::setFixedOptions(struct termios* options) { void UartComIF::setFixedOptions(struct termios* options) {
/* Disable RTS/CTS hardware flow control */ /* Disable RTS/CTS hardware flow control */
options->c_cflag &= ~CRTSCTS; options->c_cflag &= ~CRTSCTS;
/* Turn on READ & ignore ctrl lines (CLOCAL = 1) */ /* Turn on READ & ignore ctrl lines (CLOCAL = 1) */
options->c_cflag |= CREAD | CLOCAL; options->c_cflag |= CREAD | CLOCAL;
/* Disable echo */ /* Disable echo */
options->c_lflag &= ~ECHO; options->c_lflag &= ~ECHO;
/* Disable erasure */ /* Disable erasure */
options->c_lflag &= ~ECHOE; options->c_lflag &= ~ECHOE;
/* Disable new-line echo */ /* Disable new-line echo */
options->c_lflag &= ~ECHONL; options->c_lflag &= ~ECHONL;
/* Disable interpretation of INTR, QUIT and SUSP */ /* Disable interpretation of INTR, QUIT and SUSP */
options->c_lflag &= ~ISIG; options->c_lflag &= ~ISIG;
/* Turn off s/w flow ctrl */ /* Turn off s/w flow ctrl */
options->c_iflag &= ~(IXON | IXOFF | IXANY); options->c_iflag &= ~(IXON | IXOFF | IXANY);
/* Disable any special handling of received bytes */ /* Disable any special handling of received bytes */
options->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); options->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
/* Prevent special interpretation of output bytes (e.g. newline chars) */ /* Prevent special interpretation of output bytes (e.g. newline chars) */
options->c_oflag &= ~OPOST; options->c_oflag &= ~OPOST;
/* Prevent conversion of newline to carriage return/line feed */ /* Prevent conversion of newline to carriage return/line feed */
options->c_oflag &= ~ONLCR; options->c_oflag &= ~ONLCR;
} }
void UartComIF::configureBaudrate(struct termios* options, UartCookie* uartCookie) { void UartComIF::configureBaudrate(struct termios* options, UartCookie* uartCookie) {
switch (uartCookie->getBaudrate()) { switch (uartCookie->getBaudrate()) {
case 50: case 50:
cfsetispeed(options, B50); cfsetispeed(options, B50);
cfsetospeed(options, B50); cfsetospeed(options, B50);
break; break;
case 75: case 75:
cfsetispeed(options, B75); cfsetispeed(options, B75);
cfsetospeed(options, B75); cfsetospeed(options, B75);
break; break;
case 110: case 110:
cfsetispeed(options, B110); cfsetispeed(options, B110);
cfsetospeed(options, B110); cfsetospeed(options, B110);
break; break;
case 134: case 134:
cfsetispeed(options, B134); cfsetispeed(options, B134);
cfsetospeed(options, B134); cfsetospeed(options, B134);
break; break;
case 150: case 150:
cfsetispeed(options, B150); cfsetispeed(options, B150);
cfsetospeed(options, B150); cfsetospeed(options, B150);
break; break;
case 200: case 200:
cfsetispeed(options, B200); cfsetispeed(options, B200);
cfsetospeed(options, B200); cfsetospeed(options, B200);
break; break;
case 300: case 300:
cfsetispeed(options, B300); cfsetispeed(options, B300);
cfsetospeed(options, B300); cfsetospeed(options, B300);
break; break;
case 600: case 600:
cfsetispeed(options, B600); cfsetispeed(options, B600);
cfsetospeed(options, B600); cfsetospeed(options, B600);
break; break;
case 1200: case 1200:
cfsetispeed(options, B1200); cfsetispeed(options, B1200);
cfsetospeed(options, B1200); cfsetospeed(options, B1200);
break; break;
case 1800: case 1800:
cfsetispeed(options, B1800); cfsetispeed(options, B1800);
cfsetospeed(options, B1800); cfsetospeed(options, B1800);
break; break;
case 2400: case 2400:
cfsetispeed(options, B2400); cfsetispeed(options, B2400);
cfsetospeed(options, B2400); cfsetospeed(options, B2400);
break; break;
case 4800: case 4800:
cfsetispeed(options, B4800); cfsetispeed(options, B4800);
cfsetospeed(options, B4800); cfsetospeed(options, B4800);
break; break;
case 9600: case 9600:
cfsetispeed(options, B9600); cfsetispeed(options, B9600);
cfsetospeed(options, B9600); cfsetospeed(options, B9600);
break; break;
case 19200: case 19200:
cfsetispeed(options, B19200); cfsetispeed(options, B19200);
cfsetospeed(options, B19200); cfsetospeed(options, B19200);
break; break;
case 38400: case 38400:
cfsetispeed(options, B38400); cfsetispeed(options, B38400);
cfsetospeed(options, B38400); cfsetospeed(options, B38400);
break; break;
case 57600: case 57600:
cfsetispeed(options, B57600); cfsetispeed(options, B57600);
cfsetospeed(options, B57600); cfsetospeed(options, B57600);
break; break;
case 115200: case 115200:
cfsetispeed(options, B115200); cfsetispeed(options, B115200);
cfsetospeed(options, B115200); cfsetospeed(options, B115200);
break; break;
case 230400: case 230400:
cfsetispeed(options, B230400); cfsetispeed(options, B230400);
cfsetospeed(options, B230400); cfsetospeed(options, B230400);
break; break;
case 460800: case 460800:
cfsetispeed(options, B460800); cfsetispeed(options, B460800);
cfsetospeed(options, B460800); cfsetospeed(options, B460800);
break; break;
default: default:
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl; sif::warning << "UartComIF::configureBaudrate: Baudrate not supported" << std::endl;
#endif #endif
break; break;
} }
} }
ReturnValue_t UartComIF::sendMessage(CookieIF *cookie, ReturnValue_t UartComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
const uint8_t *sendData, size_t sendLen) { int fd = 0;
int fd = 0; std::string deviceFile;
std::string deviceFile; UartDeviceMapIter uartDeviceMapIter;
UartDeviceMapIter uartDeviceMapIter;
if(sendLen == 0) {
return RETURN_OK;
}
if(sendData == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::sendMessage: Send data is nullptr" << std::endl;
#endif
return RETURN_FAILED;
}
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::sendMessasge: Invalid UART Cookie!" << std::endl;
#endif
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile <<
"not in UART map" << std::endl;
#endif
return RETURN_FAILED;
}
fd = uartDeviceMapIter->second.fileDescriptor;
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UartComIF::sendMessage: Failed to send data with error code " <<
errno << ": Error description: " << strerror(errno) << std::endl;
#endif
return RETURN_FAILED;
}
if (sendLen == 0) {
return RETURN_OK; return RETURN_OK;
}
if (sendData == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::sendMessage: Send data is nullptr" << std::endl;
#endif
return RETURN_FAILED;
}
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if (uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::sendMessasge: Invalid UART Cookie!" << std::endl;
#endif
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::sendMessage: Device file " << deviceFile << "not in UART map"
<< std::endl;
#endif
return RETURN_FAILED;
}
fd = uartDeviceMapIter->second.fileDescriptor;
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "UartComIF::sendMessage: Failed to send data with error code " << errno
<< ": Error description: " << strerror(errno) << std::endl;
#endif
return RETURN_FAILED;
}
return RETURN_OK;
} }
ReturnValue_t UartComIF::getSendSuccess(CookieIF *cookie) { ReturnValue_t UartComIF::getSendSuccess(CookieIF* cookie) { return RETURN_OK; }
ReturnValue_t UartComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if (uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::requestReceiveMessage: Invalid Uart Cookie!" << std::endl;
#endif
return NULLPOINTER;
}
UartModes uartMode = uartCookie->getUartMode();
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartMode == UartModes::NON_CANONICAL and requestLen == 0) {
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t UartComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) { if (uartDeviceMapIter == uartDeviceMap.end()) {
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::requestReceiveMessage: Invalid Uart Cookie!" << std::endl; sif::debug << "UartComIF::requestReceiveMessage: Device file " << deviceFile
<< " not in uart map" << std::endl;
#endif #endif
return NULLPOINTER; return RETURN_FAILED;
} }
UartModes uartMode = uartCookie->getUartMode(); if (uartMode == UartModes::CANONICAL) {
deviceFile = uartCookie->getDeviceFile(); return handleCanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
uartDeviceMapIter = uartDeviceMap.find(deviceFile); } else if (uartMode == UartModes::NON_CANONICAL) {
return handleNoncanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
if(uartMode == UartModes::NON_CANONICAL and requestLen == 0) { } else {
return RETURN_OK; return HasReturnvaluesIF::RETURN_FAILED;
} }
if (uartDeviceMapIter == uartDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::requestReceiveMessage: Device file " << deviceFile
<< " not in uart map" << std::endl;
#endif
return RETURN_FAILED;
}
if (uartMode == UartModes::CANONICAL) {
return handleCanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
}
else if (uartMode == UartModes::NON_CANONICAL) {
return handleNoncanonicalRead(*uartCookie, uartDeviceMapIter, requestLen);
}
else {
return HasReturnvaluesIF::RETURN_FAILED;
}
} }
ReturnValue_t UartComIF::handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter, ReturnValue_t UartComIF::handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen) { size_t requestLen) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
uint8_t maxReadCycles = uartCookie.getReadCycles(); uint8_t maxReadCycles = uartCookie.getReadCycles();
uint8_t currentReadCycles = 0; uint8_t currentReadCycles = 0;
int bytesRead = 0; int bytesRead = 0;
size_t currentBytesRead = 0; size_t currentBytesRead = 0;
size_t maxReplySize = uartCookie.getMaxReplyLen(); size_t maxReplySize = uartCookie.getMaxReplyLen();
int fd = iter->second.fileDescriptor; int fd = iter->second.fileDescriptor;
auto bufferPtr = iter->second.replyBuffer.data(); auto bufferPtr = iter->second.replyBuffer.data();
iter->second.replyLen = 0; iter->second.replyLen = 0;
do { do {
size_t allowedReadSize = 0; size_t allowedReadSize = 0;
if(currentBytesRead >= maxReplySize) { if (currentBytesRead >= maxReplySize) {
// Overflow risk. Emit warning, trigger event and break. If this happens, // Overflow risk. Emit warning, trigger event and break. If this happens,
// the reception buffer is not large enough or data is not polled often enough. // the reception buffer is not large enough or data is not polled often enough.
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!" sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
<< std::endl; << std::endl;
#else #else
sif::printWarning("UartComIF::requestReceiveMessage: " sif::printWarning(
"Next read would cause overflow!"); "UartComIF::requestReceiveMessage: "
"Next read would cause overflow!");
#endif #endif
#endif #endif
result = UART_RX_BUFFER_TOO_SMALL; result = UART_RX_BUFFER_TOO_SMALL;
break; break;
} } else {
else { allowedReadSize = maxReplySize - currentBytesRead;
allowedReadSize = maxReplySize - currentBytesRead; }
}
bytesRead = read(fd, bufferPtr, allowedReadSize); bytesRead = read(fd, bufferPtr, allowedReadSize);
if (bytesRead < 0) { if (bytesRead < 0) {
// EAGAIN: No data available in non-blocking mode // EAGAIN: No data available in non-blocking mode
if(errno != EAGAIN) { if (errno != EAGAIN) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::handleCanonicalRead: read failed with code" << sif::warning << "UartComIF::handleCanonicalRead: read failed with code" << errno << ": "
errno << ": " << strerror(errno) << std::endl; << strerror(errno) << std::endl;
#else #else
sif::printWarning("UartComIF::handleCanonicalRead: read failed with code %d: %s\n", sif::printWarning("UartComIF::handleCanonicalRead: read failed with code %d: %s\n", errno,
errno, strerror(errno)); strerror(errno));
#endif #endif
#endif #endif
return RETURN_FAILED; return RETURN_FAILED;
} }
} } else if (bytesRead > 0) {
else if(bytesRead > 0) { iter->second.replyLen += bytesRead;
iter->second.replyLen += bytesRead; bufferPtr += bytesRead;
bufferPtr += bytesRead; currentBytesRead += bytesRead;
currentBytesRead += bytesRead; }
} currentReadCycles++;
currentReadCycles++; } while (bytesRead > 0 and currentReadCycles < maxReadCycles);
} while(bytesRead > 0 and currentReadCycles < maxReadCycles); return result;
return result;
} }
ReturnValue_t UartComIF::handleNoncanonicalRead(UartCookie &uartCookie, UartDeviceMapIter &iter, ReturnValue_t UartComIF::handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen) { size_t requestLen) {
int fd = iter->second.fileDescriptor; int fd = iter->second.fileDescriptor;
auto bufferPtr = iter->second.replyBuffer.data(); auto bufferPtr = iter->second.replyBuffer.data();
// Size check to prevent buffer overflow // Size check to prevent buffer overflow
if(requestLen > uartCookie.getMaxReplyLen()) { if (requestLen > uartCookie.getMaxReplyLen()) {
#if OBSW_VERBOSE_LEVEL >= 1 #if OBSW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!" sif::warning << "UartComIF::requestReceiveMessage: Next read would cause overflow!"
<< std::endl; << std::endl;
#else #else
sif::printWarning("UartComIF::requestReceiveMessage: " sif::printWarning(
"Next read would cause overflow!"); "UartComIF::requestReceiveMessage: "
"Next read would cause overflow!");
#endif #endif
#endif #endif
return UART_RX_BUFFER_TOO_SMALL; return UART_RX_BUFFER_TOO_SMALL;
} }
int bytesRead = read(fd, bufferPtr, requestLen); int bytesRead = read(fd, bufferPtr, requestLen);
if (bytesRead < 0) { if (bytesRead < 0) {
return RETURN_FAILED; return RETURN_FAILED;
} } else if (bytesRead != static_cast<int>(requestLen)) {
else if (bytesRead != static_cast<int>(requestLen)) { if (uartCookie.isReplySizeFixed()) {
if(uartCookie.isReplySizeFixed()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::requestReceiveMessage: Only read " << bytesRead << sif::warning << "UartComIF::requestReceiveMessage: Only read " << bytesRead << " of "
" of " << requestLen << " bytes" << std::endl; << requestLen << " bytes" << std::endl;
#endif #endif
return RETURN_FAILED; return RETURN_FAILED;
}
} }
iter->second.replyLen = bytesRead; }
return HasReturnvaluesIF::RETURN_OK; iter->second.replyLen = bytesRead;
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie, ReturnValue_t UartComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
uint8_t **buffer, size_t* size) { std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
std::string deviceFile; UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
UartDeviceMapIter uartDeviceMapIter; if (uartCookie == nullptr) {
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::readReceivedMessage: Invalid uart cookie!" << std::endl; sif::debug << "UartComIF::readReceivedMessage: Invalid uart cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
deviceFile = uartCookie->getDeviceFile(); deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile); uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter == uartDeviceMap.end()) { if (uartDeviceMapIter == uartDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile << sif::debug << "UartComIF::readReceivedMessage: Device file " << deviceFile << " not in uart map"
" not in uart map" << std::endl; << std::endl;
#endif #endif
return RETURN_FAILED; return RETURN_FAILED;
} }
*buffer = uartDeviceMapIter->second.replyBuffer.data(); *buffer = uartDeviceMapIter->second.replyBuffer.data();
*size = uartDeviceMapIter->second.replyLen; *size = uartDeviceMapIter->second.replyLen;
/* Length is reset to 0 to prevent reading the same data twice */ /* Length is reset to 0 to prevent reading the same data twice */
uartDeviceMapIter->second.replyLen = 0; uartDeviceMapIter->second.replyLen = 0;
return RETURN_OK;
}
ReturnValue_t UartComIF::flushUartRxBuffer(CookieIF* cookie) {
std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter;
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if (uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::flushUartRxBuffer: Invalid uart cookie!" << std::endl;
#endif
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if (uartDeviceMapIter != uartDeviceMap.end()) {
int fd = uartDeviceMapIter->second.fileDescriptor;
tcflush(fd, TCIFLUSH);
return RETURN_OK; return RETURN_OK;
}
return RETURN_FAILED;
} }
ReturnValue_t UartComIF::flushUartRxBuffer(CookieIF *cookie) { ReturnValue_t UartComIF::flushUartTxBuffer(CookieIF* cookie) {
std::string deviceFile; std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter; UartDeviceMapIter uartDeviceMapIter;
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie); UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) { if (uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::flushUartRxBuffer: Invalid uart cookie!" << std::endl; sif::warning << "UartComIF::flushUartTxBuffer: Invalid uart cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
deviceFile = uartCookie->getDeviceFile(); deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile); uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if(uartDeviceMapIter != uartDeviceMap.end()) { if (uartDeviceMapIter != uartDeviceMap.end()) {
int fd = uartDeviceMapIter->second.fileDescriptor; int fd = uartDeviceMapIter->second.fileDescriptor;
tcflush(fd, TCIFLUSH); tcflush(fd, TCOFLUSH);
return RETURN_OK; return RETURN_OK;
} }
return RETURN_FAILED; return RETURN_FAILED;
} }
ReturnValue_t UartComIF::flushUartTxBuffer(CookieIF *cookie) { ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF* cookie) {
std::string deviceFile; std::string deviceFile;
UartDeviceMapIter uartDeviceMapIter; UartDeviceMapIter uartDeviceMapIter;
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie); UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie);
if(uartCookie == nullptr) { if (uartCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "UartComIF::flushUartTxBuffer: Invalid uart cookie!" << std::endl; sif::warning << "UartComIF::flushUartTxAndRxBuf: Invalid uart cookie!" << std::endl;
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
deviceFile = uartCookie->getDeviceFile(); deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile); uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if(uartDeviceMapIter != uartDeviceMap.end()) { if (uartDeviceMapIter != uartDeviceMap.end()) {
int fd = uartDeviceMapIter->second.fileDescriptor; int fd = uartDeviceMapIter->second.fileDescriptor;
tcflush(fd, TCOFLUSH); tcflush(fd, TCIOFLUSH);
return RETURN_OK; return RETURN_OK;
} }
return RETURN_FAILED; return RETURN_FAILED;
} }
ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF *cookie) { void UartComIF::setUartMode(struct termios* options, UartCookie& uartCookie) {
std::string deviceFile; UartModes uartMode = uartCookie.getUartMode();
UartDeviceMapIter uartDeviceMapIter; if (uartMode == UartModes::NON_CANONICAL) {
UartCookie* uartCookie = dynamic_cast<UartCookie*>(cookie); /* Disable canonical mode */
if(uartCookie == nullptr) { options->c_lflag &= ~ICANON;
#if FSFW_CPP_OSTREAM_ENABLED == 1 } else if (uartMode == UartModes::CANONICAL) {
sif::warning << "UartComIF::flushUartTxAndRxBuf: Invalid uart cookie!" << std::endl; options->c_lflag |= ICANON;
#endif }
return NULLPOINTER;
}
deviceFile = uartCookie->getDeviceFile();
uartDeviceMapIter = uartDeviceMap.find(deviceFile);
if(uartDeviceMapIter != uartDeviceMap.end()) {
int fd = uartDeviceMapIter->second.fileDescriptor;
tcflush(fd, TCIOFLUSH);
return RETURN_OK;
}
return RETURN_FAILED;
}
void UartComIF::setUartMode(struct termios *options, UartCookie &uartCookie) {
UartModes uartMode = uartCookie.getUartMode();
if(uartMode == UartModes::NON_CANONICAL) {
/* Disable canonical mode */
options->c_lflag &= ~ICANON;
}
else if(uartMode == UartModes::CANONICAL) {
options->c_lflag |= ICANON;
}
} }

View File

@ -1,13 +1,14 @@
#ifndef BSP_Q7S_COMIF_UARTCOMIF_H_ #ifndef BSP_Q7S_COMIF_UARTCOMIF_H_
#define BSP_Q7S_COMIF_UARTCOMIF_H_ #define BSP_Q7S_COMIF_UARTCOMIF_H_
#include "UartCookie.h"
#include <fsfw/objectmanager/SystemObject.h>
#include <fsfw/devicehandlers/DeviceCommunicationIF.h> #include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <fsfw/objectmanager/SystemObject.h>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "UartCookie.h"
/** /**
* @brief This is the communication interface to access serial ports on linux based operating * @brief This is the communication interface to access serial ports on linux based operating
* systems. * systems.
@ -17,109 +18,104 @@
* *
* @author J. Meier * @author J. Meier
*/ */
class UartComIF: public DeviceCommunicationIF, public SystemObject { class UartComIF : public DeviceCommunicationIF, public SystemObject {
public: public:
static constexpr uint8_t uartRetvalId = CLASS_ID::HAL_UART; static constexpr uint8_t uartRetvalId = CLASS_ID::HAL_UART;
static constexpr ReturnValue_t UART_READ_FAILURE = static constexpr ReturnValue_t UART_READ_FAILURE =
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 1); HasReturnvaluesIF::makeReturnCode(uartRetvalId, 1);
static constexpr ReturnValue_t UART_READ_SIZE_MISSMATCH = static constexpr ReturnValue_t UART_READ_SIZE_MISSMATCH =
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 2); HasReturnvaluesIF::makeReturnCode(uartRetvalId, 2);
static constexpr ReturnValue_t UART_RX_BUFFER_TOO_SMALL = static constexpr ReturnValue_t UART_RX_BUFFER_TOO_SMALL =
HasReturnvaluesIF::makeReturnCode(uartRetvalId, 3); HasReturnvaluesIF::makeReturnCode(uartRetvalId, 3);
UartComIF(object_id_t objectId); UartComIF(object_id_t objectId);
virtual ~UartComIF(); virtual ~UartComIF();
ReturnValue_t initializeInterface(CookieIF * cookie) override; ReturnValue_t initializeInterface(CookieIF* cookie) override;
ReturnValue_t sendMessage(CookieIF *cookie,const uint8_t *sendData, ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
size_t sendLen) override; ReturnValue_t getSendSuccess(CookieIF* cookie) override;
ReturnValue_t getSendSuccess(CookieIF *cookie) override; ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
ReturnValue_t requestReceiveMessage(CookieIF *cookie, ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) override;
size_t requestLen) override;
ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
size_t *size) override;
/** /**
* @brief This function discards all data received but not read in the UART buffer. * @brief This function discards all data received but not read in the UART buffer.
*/ */
ReturnValue_t flushUartRxBuffer(CookieIF *cookie); ReturnValue_t flushUartRxBuffer(CookieIF* cookie);
/** /**
* @brief This function discards all data in the transmit buffer of the UART driver. * @brief This function discards all data in the transmit buffer of the UART driver.
*/ */
ReturnValue_t flushUartTxBuffer(CookieIF *cookie); ReturnValue_t flushUartTxBuffer(CookieIF* cookie);
/** /**
* @brief This function discards both data in the transmit and receive buffer of the UART. * @brief This function discards both data in the transmit and receive buffer of the UART.
*/ */
ReturnValue_t flushUartTxAndRxBuf(CookieIF *cookie); ReturnValue_t flushUartTxAndRxBuf(CookieIF* cookie);
private: private:
using UartDeviceFile_t = std::string;
using UartDeviceFile_t = std::string; struct UartElements {
int fileDescriptor;
std::vector<uint8_t> replyBuffer;
/** Number of bytes read will be written to this variable */
size_t replyLen;
};
struct UartElements { using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements>;
int fileDescriptor; using UartDeviceMapIter = UartDeviceMap::iterator;
std::vector<uint8_t> replyBuffer;
/** Number of bytes read will be written to this variable */
size_t replyLen;
};
using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements>; /**
using UartDeviceMapIter = UartDeviceMap::iterator; * The uart devie map stores informations of initialized uart ports.
*/
UartDeviceMap uartDeviceMap;
/** /**
* The uart devie map stores informations of initialized uart ports. * @brief This function opens and configures a uart device by using the information stored
*/ * in the uart cookie.
UartDeviceMap uartDeviceMap; * @param uartCookie Pointer to uart cookie with information about the uart. Contains the
* uart device file, baudrate, parity, stopbits etc.
* @return The file descriptor of the configured uart.
*/
int configureUartPort(UartCookie* uartCookie);
/** /**
* @brief This function opens and configures a uart device by using the information stored * @brief This function adds the parity settings to the termios options struct.
* in the uart cookie. *
* @param uartCookie Pointer to uart cookie with information about the uart. Contains the * @param options Pointer to termios options struct which will be modified to enable or disable
* uart device file, baudrate, parity, stopbits etc. * parity checking.
* @return The file descriptor of the configured uart. * @param uartCookie Pointer to uart cookie containing the information about the desired
*/ * parity settings.
int configureUartPort(UartCookie* uartCookie); *
*/
void setParityOptions(struct termios* options, UartCookie* uartCookie);
/** void setStopBitOptions(struct termios* options, UartCookie* uartCookie);
* @brief This function adds the parity settings to the termios options struct.
*
* @param options Pointer to termios options struct which will be modified to enable or disable
* parity checking.
* @param uartCookie Pointer to uart cookie containing the information about the desired
* parity settings.
*
*/
void setParityOptions(struct termios* options, UartCookie* uartCookie);
void setStopBitOptions(struct termios* options, UartCookie* uartCookie); /**
* @brief This function sets options which are not configurable by the uartCookie.
*/
void setFixedOptions(struct termios* options);
/** /**
* @brief This function sets options which are not configurable by the uartCookie. * @brief With this function the datasize settings are added to the termios options struct.
*/ */
void setFixedOptions(struct termios* options); void setDatasizeOptions(struct termios* options, UartCookie* uartCookie);
/** /**
* @brief With this function the datasize settings are added to the termios options struct. * @brief This functions adds the baudrate specified in the uartCookie to the termios options
*/ * struct.
void setDatasizeOptions(struct termios* options, UartCookie* uartCookie); */
void configureBaudrate(struct termios* options, UartCookie* uartCookie);
/** void setUartMode(struct termios* options, UartCookie& uartCookie);
* @brief This functions adds the baudrate specified in the uartCookie to the termios options
* struct.
*/
void configureBaudrate(struct termios* options, UartCookie* uartCookie);
void setUartMode(struct termios* options, UartCookie& uartCookie);
ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen);
ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen);
ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen);
ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
size_t requestLen);
}; };
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */ #endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */

View File

@ -3,97 +3,63 @@
#include <fsfw/serviceinterface.h> #include <fsfw/serviceinterface.h>
UartCookie::UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode, UartCookie::UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode,
uint32_t baudrate, size_t maxReplyLen): uint32_t baudrate, size_t maxReplyLen)
handlerId(handlerId), deviceFile(deviceFile), uartMode(uartMode), : handlerId(handlerId),
baudrate(baudrate), maxReplyLen(maxReplyLen) { deviceFile(deviceFile),
} uartMode(uartMode),
baudrate(baudrate),
maxReplyLen(maxReplyLen) {}
UartCookie::~UartCookie() {} UartCookie::~UartCookie() {}
uint32_t UartCookie::getBaudrate() const { uint32_t UartCookie::getBaudrate() const { return baudrate; }
return baudrate;
}
size_t UartCookie::getMaxReplyLen() const { size_t UartCookie::getMaxReplyLen() const { return maxReplyLen; }
return maxReplyLen;
}
std::string UartCookie::getDeviceFile() const { std::string UartCookie::getDeviceFile() const { return deviceFile; }
return deviceFile;
}
void UartCookie::setParityOdd() { void UartCookie::setParityOdd() { parity = Parity::ODD; }
parity = Parity::ODD;
}
void UartCookie::setParityEven() { void UartCookie::setParityEven() { parity = Parity::EVEN; }
parity = Parity::EVEN;
}
Parity UartCookie::getParity() const { Parity UartCookie::getParity() const { return parity; }
return parity;
}
void UartCookie::setBitsPerWord(uint8_t bitsPerWord_) { void UartCookie::setBitsPerWord(uint8_t bitsPerWord_) {
switch(bitsPerWord_) { switch (bitsPerWord_) {
case 5: case 5:
case 6: case 6:
case 7: case 7:
case 8: case 8:
break; break;
default: default:
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "UartCookie::setBitsPerWord: Invalid bits per word specified" << std::endl; sif::debug << "UartCookie::setBitsPerWord: Invalid bits per word specified" << std::endl;
#endif #endif
return; return;
} }
bitsPerWord = bitsPerWord_; bitsPerWord = bitsPerWord_;
} }
uint8_t UartCookie::getBitsPerWord() const { uint8_t UartCookie::getBitsPerWord() const { return bitsPerWord; }
return bitsPerWord;
}
StopBits UartCookie::getStopBits() const { StopBits UartCookie::getStopBits() const { return stopBits; }
return stopBits;
}
void UartCookie::setTwoStopBits() { void UartCookie::setTwoStopBits() { stopBits = StopBits::TWO_STOP_BITS; }
stopBits = StopBits::TWO_STOP_BITS;
}
void UartCookie::setOneStopBit() { void UartCookie::setOneStopBit() { stopBits = StopBits::ONE_STOP_BIT; }
stopBits = StopBits::ONE_STOP_BIT;
}
UartModes UartCookie::getUartMode() const { UartModes UartCookie::getUartMode() const { return uartMode; }
return uartMode;
}
void UartCookie::setReadCycles(uint8_t readCycles) { void UartCookie::setReadCycles(uint8_t readCycles) { this->readCycles = readCycles; }
this->readCycles = readCycles;
}
void UartCookie::setToFlushInput(bool enable) { void UartCookie::setToFlushInput(bool enable) { this->flushInput = enable; }
this->flushInput = enable;
}
uint8_t UartCookie::getReadCycles() const { uint8_t UartCookie::getReadCycles() const { return readCycles; }
return readCycles;
}
bool UartCookie::getInputShouldBeFlushed() { bool UartCookie::getInputShouldBeFlushed() { return this->flushInput; }
return this->flushInput;
}
object_id_t UartCookie::getHandlerId() const { object_id_t UartCookie::getHandlerId() const { return this->handlerId; }
return this->handlerId;
}
void UartCookie::setNoFixedSizeReply() { void UartCookie::setNoFixedSizeReply() { replySizeFixed = false; }
replySizeFixed = false;
}
bool UartCookie::isReplySizeFixed() { bool UartCookie::isReplySizeFixed() { return replySizeFixed; }
return replySizeFixed;
}

View File

@ -6,21 +6,11 @@
#include <string> #include <string>
enum class Parity { enum class Parity { NONE, EVEN, ODD };
NONE,
EVEN,
ODD
};
enum class StopBits { enum class StopBits { ONE_STOP_BIT, TWO_STOP_BITS };
ONE_STOP_BIT,
TWO_STOP_BITS
};
enum class UartModes { enum class UartModes { CANONICAL, NON_CANONICAL };
CANONICAL,
NON_CANONICAL
};
/** /**
* @brief Cookie for the UartComIF. There are many options available to configure the UART driver. * @brief Cookie for the UartComIF. There are many options available to configure the UART driver.
@ -29,93 +19,91 @@ enum class UartModes {
* *
* @author J. Meier * @author J. Meier
*/ */
class UartCookie: public CookieIF { class UartCookie : public CookieIF {
public: public:
/**
* @brief Constructor for the uart cookie.
* @param deviceFile The device file specifying the uart to use, e.g. "/dev/ttyPS1"
* @param uartMode Specify the UART mode. The canonical mode should be used if the
* messages are separated by a delimited character like '\n'. See the
* termios documentation for more information
* @param baudrate The baudrate to use for input and output. Possible Baudrates are: 50,
* 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, B19200,
* 38400, 57600, 115200, 230400, 460800
* @param maxReplyLen The maximum size an object using this cookie expects
* @details
* Default configuration: No parity
* 8 databits (number of bits transfered with one uart frame)
* One stop bit
*/
UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode, uint32_t baudrate,
size_t maxReplyLen);
/** virtual ~UartCookie();
* @brief Constructor for the uart cookie.
* @param deviceFile The device file specifying the uart to use, e.g. "/dev/ttyPS1"
* @param uartMode Specify the UART mode. The canonical mode should be used if the
* messages are separated by a delimited character like '\n'. See the
* termios documentation for more information
* @param baudrate The baudrate to use for input and output. Possible Baudrates are: 50,
* 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, B19200,
* 38400, 57600, 115200, 230400, 460800
* @param maxReplyLen The maximum size an object using this cookie expects
* @details
* Default configuration: No parity
* 8 databits (number of bits transfered with one uart frame)
* One stop bit
*/
UartCookie(object_id_t handlerId, std::string deviceFile, UartModes uartMode,
uint32_t baudrate, size_t maxReplyLen);
virtual ~UartCookie(); uint32_t getBaudrate() const;
size_t getMaxReplyLen() const;
std::string getDeviceFile() const;
Parity getParity() const;
uint8_t getBitsPerWord() const;
StopBits getStopBits() const;
UartModes getUartMode() const;
object_id_t getHandlerId() const;
uint32_t getBaudrate() const; /**
size_t getMaxReplyLen() const; * The UART ComIF will only perform a specified number of read cycles for the canonical mode.
std::string getDeviceFile() const; * The user can specify how many of those read cycles are performed for one device handler
Parity getParity() const; * communication cycle. An example use-case would be to read all available GPS NMEA strings
uint8_t getBitsPerWord() const; * at once.
StopBits getStopBits() const; * @param readCycles
UartModes getUartMode() const; */
object_id_t getHandlerId() const; void setReadCycles(uint8_t readCycles);
uint8_t getReadCycles() const;
/** /**
* The UART ComIF will only perform a specified number of read cycles for the canonical mode. * Allows to flush the data which was received but has not been read yet. This is useful
* The user can specify how many of those read cycles are performed for one device handler * to discard obsolete data at software startup.
* communication cycle. An example use-case would be to read all available GPS NMEA strings */
* at once. void setToFlushInput(bool enable);
* @param readCycles bool getInputShouldBeFlushed();
*/
void setReadCycles(uint8_t readCycles);
uint8_t getReadCycles() const;
/** /**
* Allows to flush the data which was received but has not been read yet. This is useful * Functions two enable parity checking.
* to discard obsolete data at software startup. */
*/ void setParityOdd();
void setToFlushInput(bool enable); void setParityEven();
bool getInputShouldBeFlushed();
/** /**
* Functions two enable parity checking. * Function two set number of bits per UART frame.
*/ */
void setParityOdd(); void setBitsPerWord(uint8_t bitsPerWord_);
void setParityEven();
/** /**
* Function two set number of bits per UART frame. * Function to specify the number of stopbits.
*/ */
void setBitsPerWord(uint8_t bitsPerWord_); void setTwoStopBits();
void setOneStopBit();
/** /**
* Function to specify the number of stopbits. * Calling this function prevents the UartComIF to return failed if not all requested bytes
*/ * could be read. This is required by a device handler when the size of a reply is not known.
void setTwoStopBits(); */
void setOneStopBit(); void setNoFixedSizeReply();
/** bool isReplySizeFixed();
* Calling this function prevents the UartComIF to return failed if not all requested bytes
* could be read. This is required by a device handler when the size of a reply is not known.
*/
void setNoFixedSizeReply();
bool isReplySizeFixed(); private:
const object_id_t handlerId;
private: std::string deviceFile;
const UartModes uartMode;
const object_id_t handlerId; bool flushInput = false;
std::string deviceFile; uint32_t baudrate;
const UartModes uartMode; size_t maxReplyLen = 0;
bool flushInput = false; Parity parity = Parity::NONE;
uint32_t baudrate; uint8_t bitsPerWord = 8;
size_t maxReplyLen = 0; uint8_t readCycles = 1;
Parity parity = Parity::NONE; StopBits stopBits = StopBits::ONE_STOP_BIT;
uint8_t bitsPerWord = 8; bool replySizeFixed = true;
uint8_t readCycles = 1;
StopBits stopBits = StopBits::ONE_STOP_BIT;
bool replySizeFixed = true;
}; };
#endif #endif

View File

@ -1,26 +1,23 @@
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw_hal/linux/utility.h" #include "fsfw_hal/linux/utility.h"
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
void utility::handleIoctlError(const char* const customPrintout) { void utility::handleIoctlError(const char* const customPrintout) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
if(customPrintout != nullptr) { if (customPrintout != nullptr) {
sif::warning << customPrintout << std::endl; sif::warning << customPrintout << std::endl;
} }
sif::warning << "handleIoctlError: Error code " << errno << ", "<< strerror(errno) << sif::warning << "handleIoctlError: Error code " << errno << ", " << strerror(errno) << std::endl;
std::endl;
#else #else
if(customPrintout != nullptr) { if (customPrintout != nullptr) {
sif::printWarning("%s\n", customPrintout); sif::printWarning("%s\n", customPrintout);
} }
sif::printWarning("handleIoctlError: Error code %d, %s\n", errno, strerror(errno)); sif::printWarning("handleIoctlError: Error code %d, %s\n", errno, strerror(errno));
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
} }

View File

@ -2,6 +2,7 @@
#define FSFW_HAL_STM32H7_DEFINITIONS_H_ #define FSFW_HAL_STM32H7_DEFINITIONS_H_
#include <utility> #include <utility>
#include "stm32h7xx.h" #include "stm32h7xx.h"
namespace stm32h7 { namespace stm32h7 {
@ -11,15 +12,15 @@ namespace stm32h7 {
* and the second entry is the pin number * and the second entry is the pin number
*/ */
struct GpioCfg { struct GpioCfg {
GpioCfg(): port(nullptr), pin(0), altFnc(0) {}; GpioCfg() : port(nullptr), pin(0), altFnc(0){};
GpioCfg(GPIO_TypeDef* port, uint16_t pin, uint8_t altFnc = 0): GpioCfg(GPIO_TypeDef* port, uint16_t pin, uint8_t altFnc = 0)
port(port), pin(pin), altFnc(altFnc) {}; : port(port), pin(pin), altFnc(altFnc){};
GPIO_TypeDef* port; GPIO_TypeDef* port;
uint16_t pin; uint16_t pin;
uint8_t altFnc; uint8_t altFnc;
}; };
} } // namespace stm32h7
#endif /* #ifndef FSFW_HAL_STM32H7_DEFINITIONS_H_ */ #endif /* #ifndef FSFW_HAL_STM32H7_DEFINITIONS_H_ */

View File

@ -1,549 +1,547 @@
#include "fsfw_hal/stm32h7/devicetest/GyroL3GD20H.h" #include "fsfw_hal/stm32h7/devicetest/GyroL3GD20H.h"
#include "fsfw_hal/stm32h7/spi/mspInit.h"
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "fsfw_hal/stm32h7/spi/stm32h743zi.h"
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "stm32h7xx_hal_spi.h"
#include "stm32h7xx_hal_rcc.h"
#include <cstring> #include <cstring>
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw_hal/stm32h7/spi/mspInit.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "fsfw_hal/stm32h7/spi/stm32h743zi.h"
#include "stm32h7xx_hal_rcc.h"
#include "stm32h7xx_hal_spi.h"
alignas(32) std::array<uint8_t, GyroL3GD20H::recvBufferSize> GyroL3GD20H::rxBuffer; alignas(32) std::array<uint8_t, GyroL3GD20H::recvBufferSize> GyroL3GD20H::rxBuffer;
alignas(32) std::array<uint8_t, GyroL3GD20H::txBufferSize> alignas(32) std::array<uint8_t, GyroL3GD20H::txBufferSize> GyroL3GD20H::txBuffer
GyroL3GD20H::txBuffer __attribute__((section(".dma_buffer"))); __attribute__((section(".dma_buffer")));
TransferStates transferState = TransferStates::IDLE; TransferStates transferState = TransferStates::IDLE;
spi::TransferModes GyroL3GD20H::transferMode = spi::TransferModes::POLLING; spi::TransferModes GyroL3GD20H::transferMode = spi::TransferModes::POLLING;
GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transferMode_)
: spiHandle(spiHandle) {
txDmaHandle = new DMA_HandleTypeDef();
rxDmaHandle = new DMA_HandleTypeDef();
spi::setSpiHandle(spiHandle);
spi::assignSpiUserArgs(spi::SpiBus::SPI_1, spiHandle);
transferMode = transferMode_;
if (transferMode == spi::TransferModes::DMA) {
mspCfg = new spi::MspDmaConfigStruct();
auto typedCfg = dynamic_cast<spi::MspDmaConfigStruct *>(mspCfg);
spi::setDmaHandles(txDmaHandle, rxDmaHandle);
stm32h7::h743zi::standardDmaCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS,
IrqPriorities::HIGHEST_FREERTOS,
IrqPriorities::HIGHEST_FREERTOS);
spi::setSpiDmaMspFunctions(typedCfg);
} else if (transferMode == spi::TransferModes::INTERRUPT) {
mspCfg = new spi::MspIrqConfigStruct();
auto typedCfg = dynamic_cast<spi::MspIrqConfigStruct *>(mspCfg);
stm32h7::h743zi::standardInterruptCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS);
spi::setSpiIrqMspFunctions(typedCfg);
} else if (transferMode == spi::TransferModes::POLLING) {
mspCfg = new spi::MspPollingConfigStruct();
auto typedCfg = dynamic_cast<spi::MspPollingConfigStruct *>(mspCfg);
stm32h7::h743zi::standardPollingCfg(*typedCfg);
spi::setSpiPollingMspFunctions(typedCfg);
}
GyroL3GD20H::GyroL3GD20H(SPI_HandleTypeDef *spiHandle, spi::TransferModes transferMode_): spi::assignTransferRxTxCompleteCallback(&spiTransferCompleteCallback, nullptr);
spiHandle(spiHandle) { spi::assignTransferErrorCallback(&spiTransferErrorCallback, nullptr);
txDmaHandle = new DMA_HandleTypeDef();
rxDmaHandle = new DMA_HandleTypeDef();
spi::setSpiHandle(spiHandle);
spi::assignSpiUserArgs(spi::SpiBus::SPI_1, spiHandle);
transferMode = transferMode_;
if(transferMode == spi::TransferModes::DMA) {
mspCfg = new spi::MspDmaConfigStruct();
auto typedCfg = dynamic_cast<spi::MspDmaConfigStruct*>(mspCfg);
spi::setDmaHandles(txDmaHandle, rxDmaHandle);
stm32h7::h743zi::standardDmaCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS,
IrqPriorities::HIGHEST_FREERTOS, IrqPriorities::HIGHEST_FREERTOS);
spi::setSpiDmaMspFunctions(typedCfg);
}
else if(transferMode == spi::TransferModes::INTERRUPT) {
mspCfg = new spi::MspIrqConfigStruct();
auto typedCfg = dynamic_cast<spi::MspIrqConfigStruct*>(mspCfg);
stm32h7::h743zi::standardInterruptCfg(*typedCfg, IrqPriorities::HIGHEST_FREERTOS);
spi::setSpiIrqMspFunctions(typedCfg);
}
else if(transferMode == spi::TransferModes::POLLING) {
mspCfg = new spi::MspPollingConfigStruct();
auto typedCfg = dynamic_cast<spi::MspPollingConfigStruct*>(mspCfg);
stm32h7::h743zi::standardPollingCfg(*typedCfg);
spi::setSpiPollingMspFunctions(typedCfg);
}
spi::assignTransferRxTxCompleteCallback(&spiTransferCompleteCallback, nullptr); GPIO_InitTypeDef chipSelect = {};
spi::assignTransferErrorCallback(&spiTransferErrorCallback, nullptr); __HAL_RCC_GPIOD_CLK_ENABLE();
chipSelect.Pin = GPIO_PIN_14;
GPIO_InitTypeDef chipSelect = {}; chipSelect.Mode = GPIO_MODE_OUTPUT_PP;
__HAL_RCC_GPIOD_CLK_ENABLE(); HAL_GPIO_Init(GPIOD, &chipSelect);
chipSelect.Pin = GPIO_PIN_14; HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
chipSelect.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOD, &chipSelect);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
} }
GyroL3GD20H::~GyroL3GD20H() { GyroL3GD20H::~GyroL3GD20H() {
delete txDmaHandle; delete txDmaHandle;
delete rxDmaHandle; delete rxDmaHandle;
if(mspCfg != nullptr) { if (mspCfg != nullptr) {
delete mspCfg; delete mspCfg;
} }
} }
ReturnValue_t GyroL3GD20H::initialize() { ReturnValue_t GyroL3GD20H::initialize() {
// Configure the SPI peripheral // Configure the SPI peripheral
spiHandle->Instance = SPI1; spiHandle->Instance = SPI1;
spiHandle->Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), 3900000); spiHandle->Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), 3900000);
spiHandle->Init.Direction = SPI_DIRECTION_2LINES; spiHandle->Init.Direction = SPI_DIRECTION_2LINES;
spi::assignSpiMode(spi::SpiModes::MODE_3, *spiHandle); spi::assignSpiMode(spi::SpiModes::MODE_3, *spiHandle);
spiHandle->Init.DataSize = SPI_DATASIZE_8BIT; spiHandle->Init.DataSize = SPI_DATASIZE_8BIT;
spiHandle->Init.FirstBit = SPI_FIRSTBIT_MSB; spiHandle->Init.FirstBit = SPI_FIRSTBIT_MSB;
spiHandle->Init.TIMode = SPI_TIMODE_DISABLE; spiHandle->Init.TIMode = SPI_TIMODE_DISABLE;
spiHandle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; spiHandle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
spiHandle->Init.CRCPolynomial = 7; spiHandle->Init.CRCPolynomial = 7;
spiHandle->Init.CRCLength = SPI_CRC_LENGTH_8BIT; spiHandle->Init.CRCLength = SPI_CRC_LENGTH_8BIT;
spiHandle->Init.NSS = SPI_NSS_SOFT; spiHandle->Init.NSS = SPI_NSS_SOFT;
spiHandle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE; spiHandle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
// Recommended setting to avoid glitches // Recommended setting to avoid glitches
spiHandle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE; spiHandle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
spiHandle->Init.Mode = SPI_MODE_MASTER; spiHandle->Init.Mode = SPI_MODE_MASTER;
if(HAL_SPI_Init(spiHandle) != HAL_OK) { if (HAL_SPI_Init(spiHandle) != HAL_OK) {
sif::printWarning("Error initializing SPI\n"); sif::printWarning("Error initializing SPI\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
delete mspCfg;
transferState = TransferStates::WAIT;
sif::printInfo("GyroL3GD20H::performOperation: Reading WHO AM I register\n");
txBuffer[0] = WHO_AM_I_REG | STM_READ_MASK;
txBuffer[1] = 0;
switch (transferMode) {
case (spi::TransferModes::DMA): {
return handleDmaTransferInit();
} }
case (spi::TransferModes::INTERRUPT): {
delete mspCfg; return handleInterruptTransferInit();
transferState = TransferStates::WAIT;
sif::printInfo("GyroL3GD20H::performOperation: Reading WHO AM I register\n");
txBuffer[0] = WHO_AM_I_REG | STM_READ_MASK;
txBuffer[1] = 0;
switch(transferMode) {
case(spi::TransferModes::DMA): {
return handleDmaTransferInit();
} }
case(spi::TransferModes::INTERRUPT): { case (spi::TransferModes::POLLING): {
return handleInterruptTransferInit(); return handlePollingTransferInit();
}
case(spi::TransferModes::POLLING): {
return handlePollingTransferInit();
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroL3GD20H::performOperation() { ReturnValue_t GyroL3GD20H::performOperation() {
switch(transferMode) { switch (transferMode) {
case(spi::TransferModes::DMA): { case (spi::TransferModes::DMA): {
return handleDmaSensorRead(); return handleDmaSensorRead();
} }
case(spi::TransferModes::POLLING): { case (spi::TransferModes::POLLING): {
return handlePollingSensorRead(); return handlePollingSensorRead();
} }
case(spi::TransferModes::INTERRUPT): { case (spi::TransferModes::INTERRUPT): {
return handleInterruptSensorRead(); return handleInterruptSensorRead();
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroL3GD20H::handleDmaTransferInit() { ReturnValue_t GyroL3GD20H::handleDmaTransferInit() {
/* Clean D-cache */ /* Clean D-cache */
/* Make sure the address is 32-byte aligned and add 32-bytes to length, /* Make sure the address is 32-byte aligned and add 32-bytes to length,
in case it overlaps cacheline */ in case it overlaps cacheline */
// See https://community.st.com/s/article/FAQ-DMA-is-not-working-on-STM32H7-devices // See https://community.st.com/s/article/FAQ-DMA-is-not-working-on-STM32H7-devices
HAL_StatusTypeDef result = performDmaTransfer(2); HAL_StatusTypeDef result = performDmaTransfer(2);
if(result != HAL_OK) { if (result != HAL_OK) {
// Transfer error in transmission process // Transfer error in transmission process
sif::printWarning("GyroL3GD20H::initialize: Error transmitting SPI with DMA\n"); sif::printWarning("GyroL3GD20H::initialize: Error transmitting SPI with DMA\n");
} }
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
switch(transferState) { switch (transferState) {
case(TransferStates::SUCCESS): { case (TransferStates::SUCCESS): {
uint8_t whoAmIVal = rxBuffer[1]; uint8_t whoAmIVal = rxBuffer[1];
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) { if (whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
sif::printDebug("GyroL3GD20H::initialize: " sif::printDebug(
"Read WHO AM I value %d not equal to expected value!\n", whoAmIVal); "GyroL3GD20H::initialize: "
} "Read WHO AM I value %d not equal to expected value!\n",
transferState = TransferStates::IDLE; whoAmIVal);
break; }
transferState = TransferStates::IDLE;
break;
} }
case(TransferStates::FAILURE): { case (TransferStates::FAILURE): {
sif::printWarning("Transfer failure\n"); sif::printWarning("Transfer failure\n");
transferState = TransferStates::FAILURE; transferState = TransferStates::FAILURE;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
sif::printInfo("GyroL3GD20H::initialize: Configuring device\n"); sif::printInfo("GyroL3GD20H::initialize: Configuring device\n");
// Configure the 5 configuration registers // Configure the 5 configuration registers
uint8_t configRegs[5]; uint8_t configRegs[5];
prepareConfigRegs(configRegs); prepareConfigRegs(configRegs);
result = performDmaTransfer(6); result = performDmaTransfer(6);
if(result != HAL_OK) { if (result != HAL_OK) {
// Transfer error in transmission process // Transfer error in transmission process
sif::printWarning("Error transmitting SPI with DMA\n"); sif::printWarning("Error transmitting SPI with DMA\n");
} }
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
switch(transferState) { switch (transferState) {
case(TransferStates::SUCCESS): { case (TransferStates::SUCCESS): {
sif::printInfo("GyroL3GD20H::initialize: Configuration transfer success\n"); sif::printInfo("GyroL3GD20H::initialize: Configuration transfer success\n");
transferState = TransferStates::IDLE; transferState = TransferStates::IDLE;
break; break;
} }
case(TransferStates::FAILURE): { case (TransferStates::FAILURE): {
sif::printWarning("GyroL3GD20H::initialize: Configuration transfer failure\n"); sif::printWarning("GyroL3GD20H::initialize: Configuration transfer failure\n");
transferState = TransferStates::FAILURE; transferState = TransferStates::FAILURE;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK;
std::memset(txBuffer.data() + 1, 0, 5);
result = performDmaTransfer(6);
if (result != HAL_OK) {
// Transfer error in transmission process
sif::printWarning("Error transmitting SPI with DMA\n");
}
// Wait for the transfer to complete
while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1);
}
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK; switch (transferState) {
std::memset(txBuffer.data() + 1, 0 , 5); case (TransferStates::SUCCESS): {
result = performDmaTransfer(6); if (rxBuffer[1] != configRegs[0] or rxBuffer[2] != configRegs[1] or
if(result != HAL_OK) { rxBuffer[3] != configRegs[2] or rxBuffer[4] != configRegs[3] or
// Transfer error in transmission process rxBuffer[5] != configRegs[4]) {
sif::printWarning("Error transmitting SPI with DMA\n"); sif::printWarning("GyroL3GD20H::initialize: Configuration failure\n");
} else {
sif::printInfo("GyroL3GD20H::initialize: Configuration success\n");
}
transferState = TransferStates::IDLE;
break;
} }
// Wait for the transfer to complete case (TransferStates::FAILURE): {
while (transferState == TransferStates::WAIT) { sif::printWarning("GyroL3GD20H::initialize: Configuration transfer failure\n");
TaskFactory::delayTask(1); transferState = TransferStates::FAILURE;
} return HasReturnvaluesIF::RETURN_FAILED;
switch(transferState) {
case(TransferStates::SUCCESS): {
if(rxBuffer[1] != configRegs[0] or rxBuffer[2] != configRegs[1] or
rxBuffer[3] != configRegs[2] or rxBuffer[4] != configRegs[3] or
rxBuffer[5] != configRegs[4]) {
sif::printWarning("GyroL3GD20H::initialize: Configuration failure\n");
}
else {
sif::printInfo("GyroL3GD20H::initialize: Configuration success\n");
}
transferState = TransferStates::IDLE;
break;
}
case(TransferStates::FAILURE): {
sif::printWarning("GyroL3GD20H::initialize: Configuration transfer failure\n");
transferState = TransferStates::FAILURE;
return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroL3GD20H::handleDmaSensorRead() { ReturnValue_t GyroL3GD20H::handleDmaSensorRead() {
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK; txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK;
std::memset(txBuffer.data() + 1, 0 , 14); std::memset(txBuffer.data() + 1, 0, 14);
HAL_StatusTypeDef result = performDmaTransfer(15); HAL_StatusTypeDef result = performDmaTransfer(15);
if(result != HAL_OK) { if (result != HAL_OK) {
// Transfer error in transmission process // Transfer error in transmission process
sif::printDebug("GyroL3GD20H::handleDmaSensorRead: Error transmitting SPI with DMA\n"); sif::printDebug("GyroL3GD20H::handleDmaSensorRead: Error transmitting SPI with DMA\n");
} }
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
switch(transferState) { switch (transferState) {
case(TransferStates::SUCCESS): { case (TransferStates::SUCCESS): {
handleSensorReadout(); handleSensorReadout();
break; break;
} }
case(TransferStates::FAILURE): { case (TransferStates::FAILURE): {
sif::printWarning("GyroL3GD20H::handleDmaSensorRead: Sensor read failure\n"); sif::printWarning("GyroL3GD20H::handleDmaSensorRead: Sensor read failure\n");
transferState = TransferStates::FAILURE; transferState = TransferStates::FAILURE;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
HAL_StatusTypeDef GyroL3GD20H::performDmaTransfer(size_t sendSize) { HAL_StatusTypeDef GyroL3GD20H::performDmaTransfer(size_t sendSize) {
transferState = TransferStates::WAIT; transferState = TransferStates::WAIT;
#if STM_USE_PERIPHERAL_TX_BUFFER_MPU_PROTECTION == 0 #if STM_USE_PERIPHERAL_TX_BUFFER_MPU_PROTECTION == 0
SCB_CleanDCache_by_Addr((uint32_t*)(((uint32_t)txBuffer.data()) & ~(uint32_t)0x1F), SCB_CleanDCache_by_Addr((uint32_t *)(((uint32_t)txBuffer.data()) & ~(uint32_t)0x1F),
txBuffer.size()+32); txBuffer.size() + 32);
#endif #endif
// Start SPI transfer via DMA // Start SPI transfer via DMA
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
return HAL_SPI_TransmitReceive_DMA(spiHandle, txBuffer.data(), rxBuffer.data(), sendSize); return HAL_SPI_TransmitReceive_DMA(spiHandle, txBuffer.data(), rxBuffer.data(), sendSize);
} }
ReturnValue_t GyroL3GD20H::handlePollingTransferInit() { ReturnValue_t GyroL3GD20H::handlePollingTransferInit() {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
auto result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 2, 1000); auto result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 2, 1000);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
switch(result) { switch (result) {
case(HAL_OK): { case (HAL_OK): {
sif::printInfo("GyroL3GD20H::initialize: Polling transfer success\n"); sif::printInfo("GyroL3GD20H::initialize: Polling transfer success\n");
uint8_t whoAmIVal = rxBuffer[1]; uint8_t whoAmIVal = rxBuffer[1];
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) { if (whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
sif::printDebug("GyroL3GD20H::performOperation: " sif::printDebug(
"Read WHO AM I value %d not equal to expected value!\n", whoAmIVal); "GyroL3GD20H::performOperation: "
} "Read WHO AM I value %d not equal to expected value!\n",
break; whoAmIVal);
}
break;
} }
case(HAL_TIMEOUT): { case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
case(HAL_ERROR): { case (HAL_ERROR): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
sif::printInfo("GyroL3GD20H::initialize: Configuring device\n"); sif::printInfo("GyroL3GD20H::initialize: Configuring device\n");
// Configure the 5 configuration registers // Configure the 5 configuration registers
uint8_t configRegs[5]; uint8_t configRegs[5];
prepareConfigRegs(configRegs); prepareConfigRegs(configRegs);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 6, 1000); result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 6, 1000);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
switch(result) { switch (result) {
case(HAL_OK): { case (HAL_OK): {
break; break;
} }
case(HAL_TIMEOUT): { case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
case(HAL_ERROR): { case (HAL_ERROR): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK; txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK;
std::memset(txBuffer.data() + 1, 0 , 5); std::memset(txBuffer.data() + 1, 0, 5);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 6, 1000); result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 6, 1000);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
switch(result) { switch (result) {
case(HAL_OK): { case (HAL_OK): {
if(rxBuffer[1] != configRegs[0] or rxBuffer[2] != configRegs[1] or if (rxBuffer[1] != configRegs[0] or rxBuffer[2] != configRegs[1] or
rxBuffer[3] != configRegs[2] or rxBuffer[4] != configRegs[3] or rxBuffer[3] != configRegs[2] or rxBuffer[4] != configRegs[3] or
rxBuffer[5] != configRegs[4]) { rxBuffer[5] != configRegs[4]) {
sif::printWarning("GyroL3GD20H::initialize: Configuration failure\n"); sif::printWarning("GyroL3GD20H::initialize: Configuration failure\n");
} } else {
else { sif::printInfo("GyroL3GD20H::initialize: Configuration success\n");
sif::printInfo("GyroL3GD20H::initialize: Configuration success\n"); }
} break;
break;
} }
case(HAL_TIMEOUT): { case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
case(HAL_ERROR): { case (HAL_ERROR): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroL3GD20H::handlePollingSensorRead() { ReturnValue_t GyroL3GD20H::handlePollingSensorRead() {
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK; txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK;
std::memset(txBuffer.data() + 1, 0 , 14); std::memset(txBuffer.data() + 1, 0, 14);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
auto result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 15, 1000); auto result = HAL_SPI_TransmitReceive(spiHandle, txBuffer.data(), rxBuffer.data(), 15, 1000);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
switch(result) { switch (result) {
case(HAL_OK): { case (HAL_OK): {
handleSensorReadout(); handleSensorReadout();
break; break;
} }
case(HAL_TIMEOUT): { case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer timeout\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
case(HAL_ERROR): { case (HAL_ERROR): {
sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n"); sif::printDebug("GyroL3GD20H::initialize: Polling transfer failure\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroL3GD20H::handleInterruptTransferInit() { ReturnValue_t GyroL3GD20H::handleInterruptTransferInit() {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
switch(HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 2)) { switch (HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 2)) {
case(HAL_OK): { case (HAL_OK): {
sif::printInfo("GyroL3GD20H::initialize: Interrupt transfer success\n"); sif::printInfo("GyroL3GD20H::initialize: Interrupt transfer success\n");
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
uint8_t whoAmIVal = rxBuffer[1]; uint8_t whoAmIVal = rxBuffer[1];
if(whoAmIVal != EXPECTED_WHO_AM_I_VAL) { if (whoAmIVal != EXPECTED_WHO_AM_I_VAL) {
sif::printDebug("GyroL3GD20H::initialize: " sif::printDebug(
"Read WHO AM I value %d not equal to expected value!\n", whoAmIVal); "GyroL3GD20H::initialize: "
} "Read WHO AM I value %d not equal to expected value!\n",
break; whoAmIVal);
} }
case(HAL_BUSY): break;
case(HAL_ERROR):
case(HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Initialization failure using interrupts\n");
return HasReturnvaluesIF::RETURN_FAILED;
} }
case (HAL_BUSY):
case (HAL_ERROR):
case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Initialization failure using interrupts\n");
return HasReturnvaluesIF::RETURN_FAILED;
} }
}
sif::printInfo("GyroL3GD20H::initialize: Configuring device\n"); sif::printInfo("GyroL3GD20H::initialize: Configuring device\n");
transferState = TransferStates::WAIT; transferState = TransferStates::WAIT;
// Configure the 5 configuration registers // Configure the 5 configuration registers
uint8_t configRegs[5]; uint8_t configRegs[5];
prepareConfigRegs(configRegs); prepareConfigRegs(configRegs);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
switch(HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 6)) { switch (HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 6)) {
case(HAL_OK): { case (HAL_OK): {
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
break; break;
}
case(HAL_BUSY):
case(HAL_ERROR):
case(HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Initialization failure using interrupts\n");
return HasReturnvaluesIF::RETURN_FAILED;
} }
case (HAL_BUSY):
case (HAL_ERROR):
case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Initialization failure using interrupts\n");
return HasReturnvaluesIF::RETURN_FAILED;
} }
}
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK; txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK;
std::memset(txBuffer.data() + 1, 0 , 5); std::memset(txBuffer.data() + 1, 0, 5);
transferState = TransferStates::WAIT; transferState = TransferStates::WAIT;
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
switch(HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 6)) { switch (HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 6)) {
case(HAL_OK): { case (HAL_OK): {
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
if(rxBuffer[1] != configRegs[0] or rxBuffer[2] != configRegs[1] or if (rxBuffer[1] != configRegs[0] or rxBuffer[2] != configRegs[1] or
rxBuffer[3] != configRegs[2] or rxBuffer[4] != configRegs[3] or rxBuffer[3] != configRegs[2] or rxBuffer[4] != configRegs[3] or
rxBuffer[5] != configRegs[4]) { rxBuffer[5] != configRegs[4]) {
sif::printWarning("GyroL3GD20H::initialize: Configuration failure\n"); sif::printWarning("GyroL3GD20H::initialize: Configuration failure\n");
} } else {
else { sif::printInfo("GyroL3GD20H::initialize: Configuration success\n");
sif::printInfo("GyroL3GD20H::initialize: Configuration success\n"); }
} break;
break;
} }
case(HAL_BUSY): case (HAL_BUSY):
case(HAL_ERROR): case (HAL_ERROR):
case(HAL_TIMEOUT): { case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Initialization failure using interrupts\n"); sif::printDebug("GyroL3GD20H::initialize: Initialization failure using interrupts\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t GyroL3GD20H::handleInterruptSensorRead() { ReturnValue_t GyroL3GD20H::handleInterruptSensorRead() {
transferState = TransferStates::WAIT; transferState = TransferStates::WAIT;
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK; txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK | STM_READ_MASK;
std::memset(txBuffer.data() + 1, 0 , 14); std::memset(txBuffer.data() + 1, 0, 14);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
switch(HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 15)) { switch (HAL_SPI_TransmitReceive_IT(spiHandle, txBuffer.data(), rxBuffer.data(), 15)) {
case(HAL_OK): { case (HAL_OK): {
// Wait for the transfer to complete // Wait for the transfer to complete
while (transferState == TransferStates::WAIT) { while (transferState == TransferStates::WAIT) {
TaskFactory::delayTask(1); TaskFactory::delayTask(1);
} }
handleSensorReadout(); handleSensorReadout();
break; break;
} }
case(HAL_BUSY): case (HAL_BUSY):
case(HAL_ERROR): case (HAL_ERROR):
case(HAL_TIMEOUT): { case (HAL_TIMEOUT): {
sif::printDebug("GyroL3GD20H::initialize: Sensor read failure using interrupts\n"); sif::printDebug("GyroL3GD20H::initialize: Sensor read failure using interrupts\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void GyroL3GD20H::prepareConfigRegs(uint8_t* configRegs) { void GyroL3GD20H::prepareConfigRegs(uint8_t *configRegs) {
// Enable sensor // Enable sensor
configRegs[0] = 0b00001111; configRegs[0] = 0b00001111;
configRegs[1] = 0b00000000; configRegs[1] = 0b00000000;
configRegs[2] = 0b00000000; configRegs[2] = 0b00000000;
// Big endian select // Big endian select
configRegs[3] = 0b01000000; configRegs[3] = 0b01000000;
configRegs[4] = 0b00000000; configRegs[4] = 0b00000000;
txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK; txBuffer[0] = CTRL_REG_1 | STM_AUTO_INCREMENT_MASK;
std::memcpy(txBuffer.data() + 1, configRegs, 5); std::memcpy(txBuffer.data() + 1, configRegs, 5);
} }
uint8_t GyroL3GD20H::readRegPolling(uint8_t reg) { uint8_t GyroL3GD20H::readRegPolling(uint8_t reg) {
uint8_t rxBuf[2] = {}; uint8_t rxBuf[2] = {};
uint8_t txBuf[2] = {}; uint8_t txBuf[2] = {};
txBuf[0] = reg | STM_READ_MASK; txBuf[0] = reg | STM_READ_MASK;
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
auto result = HAL_SPI_TransmitReceive(spiHandle, txBuf, rxBuf, 2, 1000); auto result = HAL_SPI_TransmitReceive(spiHandle, txBuf, rxBuf, 2, 1000);
if(result) {}; if (result) {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); };
return rxBuf[1]; HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
return rxBuf[1];
} }
void GyroL3GD20H::handleSensorReadout() { void GyroL3GD20H::handleSensorReadout() {
uint8_t statusReg = rxBuffer[8]; uint8_t statusReg = rxBuffer[8];
int16_t gyroXRaw = rxBuffer[9] << 8 | rxBuffer[10]; int16_t gyroXRaw = rxBuffer[9] << 8 | rxBuffer[10];
float gyroX = static_cast<float>(gyroXRaw) * 0.00875; float gyroX = static_cast<float>(gyroXRaw) * 0.00875;
int16_t gyroYRaw = rxBuffer[11] << 8 | rxBuffer[12]; int16_t gyroYRaw = rxBuffer[11] << 8 | rxBuffer[12];
float gyroY = static_cast<float>(gyroYRaw) * 0.00875; float gyroY = static_cast<float>(gyroYRaw) * 0.00875;
int16_t gyroZRaw = rxBuffer[13] << 8 | rxBuffer[14]; int16_t gyroZRaw = rxBuffer[13] << 8 | rxBuffer[14];
float gyroZ = static_cast<float>(gyroZRaw) * 0.00875; float gyroZ = static_cast<float>(gyroZRaw) * 0.00875;
sif::printInfo("Status register: 0b" BYTE_TO_BINARY_PATTERN "\n", BYTE_TO_BINARY(statusReg)); sif::printInfo("Status register: 0b" BYTE_TO_BINARY_PATTERN "\n", BYTE_TO_BINARY(statusReg));
sif::printInfo("Gyro X: %f\n", gyroX); sif::printInfo("Gyro X: %f\n", gyroX);
sif::printInfo("Gyro Y: %f\n", gyroY); sif::printInfo("Gyro Y: %f\n", gyroY);
sif::printInfo("Gyro Z: %f\n", gyroZ); sif::printInfo("Gyro Z: %f\n", gyroZ);
} }
void GyroL3GD20H::spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void *args) {
void GyroL3GD20H::spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void* args) { transferState = TransferStates::SUCCESS;
transferState = TransferStates::SUCCESS; HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET); if (GyroL3GD20H::transferMode == spi::TransferModes::DMA) {
if(GyroL3GD20H::transferMode == spi::TransferModes::DMA) { // Invalidate cache prior to access by CPU
// Invalidate cache prior to access by CPU SCB_InvalidateDCache_by_Addr((uint32_t *)GyroL3GD20H::rxBuffer.data(),
SCB_InvalidateDCache_by_Addr ((uint32_t *)GyroL3GD20H::rxBuffer.data(), GyroL3GD20H::recvBufferSize);
GyroL3GD20H::recvBufferSize); }
}
} }
/** /**
@ -553,6 +551,6 @@ void GyroL3GD20H::spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void* arg
* add your own implementation. * add your own implementation.
* @retval None * @retval None
*/ */
void GyroL3GD20H::spiTransferErrorCallback(SPI_HandleTypeDef *hspi, void* args) { void GyroL3GD20H::spiTransferErrorCallback(SPI_HandleTypeDef *hspi, void *args) {
transferState = TransferStates::FAILURE; transferState = TransferStates::FAILURE;
} }

View File

@ -1,70 +1,61 @@
#ifndef FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ #ifndef FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_
#define FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ #define FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_
#include "stm32h7xx_hal.h" #include <array>
#include "stm32h7xx_hal_spi.h" #include <cstdint>
#include "../spi/mspInit.h" #include "../spi/mspInit.h"
#include "../spi/spiDefinitions.h" #include "../spi/spiDefinitions.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_spi.h"
#include <cstdint> enum class TransferStates { IDLE, WAIT, SUCCESS, FAILURE };
#include <array>
enum class TransferStates {
IDLE,
WAIT,
SUCCESS,
FAILURE
};
class GyroL3GD20H { class GyroL3GD20H {
public: public:
GyroL3GD20H(SPI_HandleTypeDef* spiHandle, spi::TransferModes transferMode); GyroL3GD20H(SPI_HandleTypeDef* spiHandle, spi::TransferModes transferMode);
~GyroL3GD20H(); ~GyroL3GD20H();
ReturnValue_t initialize(); ReturnValue_t initialize();
ReturnValue_t performOperation(); ReturnValue_t performOperation();
private: private:
const uint8_t WHO_AM_I_REG = 0b00001111;
const uint8_t STM_READ_MASK = 0b10000000;
const uint8_t STM_AUTO_INCREMENT_MASK = 0b01000000;
const uint8_t EXPECTED_WHO_AM_I_VAL = 0b11010111;
const uint8_t CTRL_REG_1 = 0b00100000;
const uint32_t L3G_RANGE = 245;
const uint8_t WHO_AM_I_REG = 0b00001111; SPI_HandleTypeDef* spiHandle;
const uint8_t STM_READ_MASK = 0b10000000;
const uint8_t STM_AUTO_INCREMENT_MASK = 0b01000000;
const uint8_t EXPECTED_WHO_AM_I_VAL = 0b11010111;
const uint8_t CTRL_REG_1 = 0b00100000;
const uint32_t L3G_RANGE = 245;
SPI_HandleTypeDef* spiHandle; static spi::TransferModes transferMode;
static constexpr size_t recvBufferSize = 32 * 10;
static std::array<uint8_t, recvBufferSize> rxBuffer;
static constexpr size_t txBufferSize = 32;
static std::array<uint8_t, txBufferSize> txBuffer;
static spi::TransferModes transferMode; ReturnValue_t handleDmaTransferInit();
static constexpr size_t recvBufferSize = 32 * 10; ReturnValue_t handlePollingTransferInit();
static std::array<uint8_t, recvBufferSize> rxBuffer; ReturnValue_t handleInterruptTransferInit();
static constexpr size_t txBufferSize = 32;
static std::array<uint8_t, txBufferSize> txBuffer;
ReturnValue_t handleDmaTransferInit(); ReturnValue_t handleDmaSensorRead();
ReturnValue_t handlePollingTransferInit(); HAL_StatusTypeDef performDmaTransfer(size_t sendSize);
ReturnValue_t handleInterruptTransferInit(); ReturnValue_t handlePollingSensorRead();
ReturnValue_t handleInterruptSensorRead();
ReturnValue_t handleDmaSensorRead(); uint8_t readRegPolling(uint8_t reg);
HAL_StatusTypeDef performDmaTransfer(size_t sendSize);
ReturnValue_t handlePollingSensorRead();
ReturnValue_t handleInterruptSensorRead();
uint8_t readRegPolling(uint8_t reg); static void spiTransferCompleteCallback(SPI_HandleTypeDef* hspi, void* args);
static void spiTransferErrorCallback(SPI_HandleTypeDef* hspi, void* args);
static void spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void* args); void prepareConfigRegs(uint8_t* configRegs);
static void spiTransferErrorCallback(SPI_HandleTypeDef *hspi, void* args); void handleSensorReadout();
DMA_HandleTypeDef* txDmaHandle = {};
void prepareConfigRegs(uint8_t* configRegs); DMA_HandleTypeDef* rxDmaHandle = {};
void handleSensorReadout(); spi::MspCfgBase* mspCfg = {};
DMA_HandleTypeDef* txDmaHandle = {};
DMA_HandleTypeDef* rxDmaHandle = {};
spi::MspCfgBase* mspCfg = {};
}; };
#endif /* FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ */ #endif /* FSFW_HAL_STM32H7_DEVICETEST_GYRO_L3GD20H_H_ */

View File

@ -1,7 +1,7 @@
#include <fsfw_hal/stm32h7/dma.h> #include <fsfw_hal/stm32h7/dma.h>
#include <cstdint>
#include <cstddef> #include <cstddef>
#include <cstdint>
user_handler_t DMA_1_USER_HANDLERS[8]; user_handler_t DMA_1_USER_HANDLERS[8];
user_args_t DMA_1_USER_ARGS[8]; user_args_t DMA_1_USER_ARGS[8];
@ -10,15 +10,14 @@ user_handler_t DMA_2_USER_HANDLERS[8];
user_args_t DMA_2_USER_ARGS[8]; user_args_t DMA_2_USER_ARGS[8];
void dma::assignDmaUserHandler(DMAIndexes dma_idx, DMAStreams stream_idx, void dma::assignDmaUserHandler(DMAIndexes dma_idx, DMAStreams stream_idx,
user_handler_t user_handler, user_args_t user_args) { user_handler_t user_handler, user_args_t user_args) {
if(dma_idx == DMA_1) { if (dma_idx == DMA_1) {
DMA_1_USER_HANDLERS[stream_idx] = user_handler; DMA_1_USER_HANDLERS[stream_idx] = user_handler;
DMA_1_USER_ARGS[stream_idx] = user_args; DMA_1_USER_ARGS[stream_idx] = user_args;
} } else if (dma_idx == DMA_2) {
else if(dma_idx == DMA_2) { DMA_2_USER_HANDLERS[stream_idx] = user_handler;
DMA_2_USER_HANDLERS[stream_idx] = user_handler; DMA_2_USER_ARGS[stream_idx] = user_args;
DMA_2_USER_ARGS[stream_idx] = user_args; }
}
} }
// The interrupt handlers in the format required for the IRQ vector table // The interrupt handlers in the format required for the IRQ vector table
@ -26,59 +25,27 @@ void dma::assignDmaUserHandler(DMAIndexes dma_idx, DMAStreams stream_idx,
/* Do not change these function names! They need to be exactly equal to the name of the functions /* Do not change these function names! They need to be exactly equal to the name of the functions
defined in the startup_stm32h743xx.s files! */ defined in the startup_stm32h743xx.s files! */
#define GENERIC_DMA_IRQ_HANDLER(DMA_IDX, STREAM_IDX) \ #define GENERIC_DMA_IRQ_HANDLER(DMA_IDX, STREAM_IDX) \
if(DMA_##DMA_IDX##_USER_HANDLERS[STREAM_IDX] != NULL) { \ if (DMA_##DMA_IDX##_USER_HANDLERS[STREAM_IDX] != NULL) { \
DMA_##DMA_IDX##_USER_HANDLERS[STREAM_IDX](DMA_##DMA_IDX##_USER_ARGS[STREAM_IDX]); \ DMA_##DMA_IDX##_USER_HANDLERS[STREAM_IDX](DMA_##DMA_IDX##_USER_ARGS[STREAM_IDX]); \
return; \ return; \
} \ } \
Default_Handler() \ Default_Handler()
extern"C" void DMA1_Stream0_IRQHandler() { extern "C" void DMA1_Stream0_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 0); }
GENERIC_DMA_IRQ_HANDLER(1, 0); extern "C" void DMA1_Stream1_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 1); }
} extern "C" void DMA1_Stream2_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 2); }
extern"C" void DMA1_Stream1_IRQHandler() { extern "C" void DMA1_Stream3_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 3); }
GENERIC_DMA_IRQ_HANDLER(1, 1); extern "C" void DMA1_Stream4_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 4); }
} extern "C" void DMA1_Stream5_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 5); }
extern"C" void DMA1_Stream2_IRQHandler() { extern "C" void DMA1_Stream6_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 6); }
GENERIC_DMA_IRQ_HANDLER(1, 2); extern "C" void DMA1_Stream7_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(1, 7); }
}
extern"C" void DMA1_Stream3_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(1, 3);
}
extern"C" void DMA1_Stream4_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(1, 4);
}
extern"C" void DMA1_Stream5_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(1, 5);
}
extern"C" void DMA1_Stream6_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(1, 6);
}
extern"C" void DMA1_Stream7_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(1, 7);
}
extern"C" void DMA2_Stream0_IRQHandler() { extern "C" void DMA2_Stream0_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 0); }
GENERIC_DMA_IRQ_HANDLER(2, 0); extern "C" void DMA2_Stream1_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 1); }
} extern "C" void DMA2_Stream2_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 2); }
extern"C" void DMA2_Stream1_IRQHandler() { extern "C" void DMA2_Stream3_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 3); }
GENERIC_DMA_IRQ_HANDLER(2, 1); extern "C" void DMA2_Stream4_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 4); }
} extern "C" void DMA2_Stream5_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 5); }
extern"C" void DMA2_Stream2_IRQHandler() { extern "C" void DMA2_Stream6_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 6); }
GENERIC_DMA_IRQ_HANDLER(2, 2); extern "C" void DMA2_Stream7_IRQHandler() { GENERIC_DMA_IRQ_HANDLER(2, 7); }
}
extern"C" void DMA2_Stream3_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(2, 3);
}
extern"C" void DMA2_Stream4_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(2, 4);
}
extern"C" void DMA2_Stream5_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(2, 5);
}
extern"C" void DMA2_Stream6_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(2, 6);
}
extern"C" void DMA2_Stream7_IRQHandler() {
GENERIC_DMA_IRQ_HANDLER(2, 7);
}

View File

@ -5,31 +5,26 @@
extern "C" { extern "C" {
#endif #endif
#include "interrupts.h"
#include <cstdint> #include <cstdint>
#include "interrupts.h"
namespace dma { namespace dma {
enum DMAType { enum DMAType { TX = 0, RX = 1 };
TX = 0,
RX = 1
};
enum DMAIndexes: uint8_t { enum DMAIndexes : uint8_t { DMA_1 = 1, DMA_2 = 2 };
DMA_1 = 1,
DMA_2 = 2
};
enum DMAStreams { enum DMAStreams {
STREAM_0 = 0, STREAM_0 = 0,
STREAM_1 = 1, STREAM_1 = 1,
STREAM_2 = 2, STREAM_2 = 2,
STREAM_3 = 3, STREAM_3 = 3,
STREAM_4 = 4, STREAM_4 = 4,
STREAM_5 = 5, STREAM_5 = 5,
STREAM_6 = 6, STREAM_6 = 6,
STREAM_7 = 7, STREAM_7 = 7,
} ; };
/** /**
* Assign user interrupt handlers for DMA streams, allowing to pass an * Assign user interrupt handlers for DMA streams, allowing to pass an
@ -37,10 +32,10 @@ enum DMAStreams {
* @param user_handler * @param user_handler
* @param user_args * @param user_args
*/ */
void assignDmaUserHandler(DMAIndexes dma_idx, DMAStreams stream_idx, void assignDmaUserHandler(DMAIndexes dma_idx, DMAStreams stream_idx, user_handler_t user_handler,
user_handler_t user_handler, user_args_t user_args); user_args_t user_args);
} } // namespace dma
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -4,68 +4,68 @@
void gpio::initializeGpioClock(GPIO_TypeDef* gpioPort) { void gpio::initializeGpioClock(GPIO_TypeDef* gpioPort) {
#ifdef GPIOA #ifdef GPIOA
if(gpioPort == GPIOA) { if (gpioPort == GPIOA) {
__HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOB #ifdef GPIOB
if(gpioPort == GPIOB) { if (gpioPort == GPIOB) {
__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOC #ifdef GPIOC
if(gpioPort == GPIOC) { if (gpioPort == GPIOC) {
__HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOD #ifdef GPIOD
if(gpioPort == GPIOD) { if (gpioPort == GPIOD) {
__HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOE #ifdef GPIOE
if(gpioPort == GPIOE) { if (gpioPort == GPIOE) {
__HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOF #ifdef GPIOF
if(gpioPort == GPIOF) { if (gpioPort == GPIOF) {
__HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOG #ifdef GPIOG
if(gpioPort == GPIOG) { if (gpioPort == GPIOG) {
__HAL_RCC_GPIOG_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOH #ifdef GPIOH
if(gpioPort == GPIOH) { if (gpioPort == GPIOH) {
__HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOI #ifdef GPIOI
if(gpioPort == GPIOI) { if (gpioPort == GPIOI) {
__HAL_RCC_GPIOI_CLK_ENABLE(); __HAL_RCC_GPIOI_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOJ #ifdef GPIOJ
if(gpioPort == GPIOJ) { if (gpioPort == GPIOJ) {
__HAL_RCC_GPIOJ_CLK_ENABLE(); __HAL_RCC_GPIOJ_CLK_ENABLE();
} }
#endif #endif
#ifdef GPIOK #ifdef GPIOK
if(gpioPort == GPIOK) { if (gpioPort == GPIOK) {
__HAL_RCC_GPIOK_CLK_ENABLE(); __HAL_RCC_GPIOK_CLK_ENABLE();
} }
#endif #endif
} }

View File

@ -12,14 +12,10 @@ extern "C" {
*/ */
extern void Default_Handler(); extern void Default_Handler();
typedef void (*user_handler_t) (void*); typedef void (*user_handler_t)(void*);
typedef void* user_args_t; typedef void* user_args_t;
enum IrqPriorities: uint8_t { enum IrqPriorities : uint8_t { HIGHEST = 0, HIGHEST_FREERTOS = 6, LOWEST = 15 };
HIGHEST = 0,
HIGHEST_FREERTOS = 6,
LOWEST = 15
};
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,11 +1,11 @@
#include "fsfw_hal/stm32h7/spi/SpiComIF.h" #include "fsfw_hal/stm32h7/spi/SpiComIF.h"
#include "fsfw_hal/stm32h7/spi/SpiCookie.h"
#include "fsfw/tasks/SemaphoreFactory.h" #include "fsfw/tasks/SemaphoreFactory.h"
#include "fsfw_hal/stm32h7/gpio/gpio.h"
#include "fsfw_hal/stm32h7/spi/SpiCookie.h"
#include "fsfw_hal/stm32h7/spi/mspInit.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h" #include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h" #include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "fsfw_hal/stm32h7/spi/mspInit.h"
#include "fsfw_hal/stm32h7/gpio/gpio.h"
// FreeRTOS required special Semaphore handling from an ISR. Therefore, we use the concrete // FreeRTOS required special Semaphore handling from an ISR. Therefore, we use the concrete
// instance here, because RTEMS and FreeRTOS are the only relevant OSALs currently // instance here, because RTEMS and FreeRTOS are the only relevant OSALs currently
@ -13,468 +13,462 @@
#if defined FSFW_OSAL_RTEMS #if defined FSFW_OSAL_RTEMS
#include "fsfw/osal/rtems/BinarySemaphore.h" #include "fsfw/osal/rtems/BinarySemaphore.h"
#elif defined FSFW_OSAL_FREERTOS #elif defined FSFW_OSAL_FREERTOS
#include "fsfw/osal/freertos/TaskManagement.h"
#include "fsfw/osal/freertos/BinarySemaphore.h" #include "fsfw/osal/freertos/BinarySemaphore.h"
#include "fsfw/osal/freertos/TaskManagement.h"
#endif #endif
#include "stm32h7xx_hal_gpio.h" #include "stm32h7xx_hal_gpio.h"
SpiComIF::SpiComIF(object_id_t objectId): SystemObject(objectId) { SpiComIF::SpiComIF(object_id_t objectId) : SystemObject(objectId) {
void* irqArgsVoided = reinterpret_cast<void*>(&irqArgs); void *irqArgsVoided = reinterpret_cast<void *>(&irqArgs);
spi::assignTransferRxTxCompleteCallback(&spiTransferCompleteCallback, irqArgsVoided); spi::assignTransferRxTxCompleteCallback(&spiTransferCompleteCallback, irqArgsVoided);
spi::assignTransferRxCompleteCallback(&spiTransferRxCompleteCallback, irqArgsVoided); spi::assignTransferRxCompleteCallback(&spiTransferRxCompleteCallback, irqArgsVoided);
spi::assignTransferTxCompleteCallback(&spiTransferTxCompleteCallback, irqArgsVoided); spi::assignTransferTxCompleteCallback(&spiTransferTxCompleteCallback, irqArgsVoided);
spi::assignTransferErrorCallback(&spiTransferErrorCallback, irqArgsVoided); spi::assignTransferErrorCallback(&spiTransferErrorCallback, irqArgsVoided);
} }
void SpiComIF::configureCacheMaintenanceOnTxBuffer(bool enable) { void SpiComIF::configureCacheMaintenanceOnTxBuffer(bool enable) {
this->cacheMaintenanceOnTxBuffer = enable; this->cacheMaintenanceOnTxBuffer = enable;
} }
void SpiComIF::addDmaHandles(DMA_HandleTypeDef *txHandle, DMA_HandleTypeDef *rxHandle) { void SpiComIF::addDmaHandles(DMA_HandleTypeDef *txHandle, DMA_HandleTypeDef *rxHandle) {
spi::setDmaHandles(txHandle, rxHandle); spi::setDmaHandles(txHandle, rxHandle);
} }
ReturnValue_t SpiComIF::initialize() { ReturnValue_t SpiComIF::initialize() { return HasReturnvaluesIF::RETURN_OK; }
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) { ReturnValue_t SpiComIF::initializeInterface(CookieIF *cookie) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie); SpiCookie *spiCookie = dynamic_cast<SpiCookie *>(cookie);
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error < "SpiComIF::initializeInterface: Invalid cookie" << std::endl; sif::error < "SpiComIF::initializeInterface: Invalid cookie" << std::endl;
#else #else
sif::printError("SpiComIF::initializeInterface: Invalid cookie\n"); sif::printError("SpiComIF::initializeInterface: Invalid cookie\n");
#endif #endif
return NULLPOINTER; return NULLPOINTER;
} }
auto transferMode = spiCookie->getTransferMode(); auto transferMode = spiCookie->getTransferMode();
if(transferMode == spi::TransferModes::DMA) { if (transferMode == spi::TransferModes::DMA) {
DMA_HandleTypeDef *txHandle = nullptr; DMA_HandleTypeDef *txHandle = nullptr;
DMA_HandleTypeDef *rxHandle = nullptr; DMA_HandleTypeDef *rxHandle = nullptr;
spi::getDmaHandles(&txHandle, &rxHandle); spi::getDmaHandles(&txHandle, &rxHandle);
if(txHandle == nullptr or rxHandle == nullptr) { if (txHandle == nullptr or rxHandle == nullptr) {
sif::printError("SpiComIF::initialize: DMA handles not set!\n"); sif::printError("SpiComIF::initialize: DMA handles not set!\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
// This semaphore ensures thread-safety for a given bus }
spiSemaphore = dynamic_cast<BinarySemaphore*>( // This semaphore ensures thread-safety for a given bus
SemaphoreFactory::instance()->createBinarySemaphore()); spiSemaphore =
address_t spiAddress = spiCookie->getDeviceAddress(); dynamic_cast<BinarySemaphore *>(SemaphoreFactory::instance()->createBinarySemaphore());
address_t spiAddress = spiCookie->getDeviceAddress();
auto iter = spiDeviceMap.find(spiAddress); auto iter = spiDeviceMap.find(spiAddress);
if(iter == spiDeviceMap.end()) { if (iter == spiDeviceMap.end()) {
size_t bufferSize = spiCookie->getMaxRecvSize(); size_t bufferSize = spiCookie->getMaxRecvSize();
auto statusPair = spiDeviceMap.emplace(spiAddress, SpiInstance(bufferSize)); auto statusPair = spiDeviceMap.emplace(spiAddress, SpiInstance(bufferSize));
if (not statusPair.second) { if (not statusPair.second) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "SpiComIF::initializeInterface: Failed to insert device with address " << sif::error << "SpiComIF::initializeInterface: Failed to insert device with address "
spiAddress << "to SPI device map" << std::endl; << spiAddress << "to SPI device map" << std::endl;
#else #else
sif::printError("SpiComIF::initializeInterface: Failed to insert device with address " sif::printError(
"%lu to SPI device map\n", static_cast<unsigned long>(spiAddress)); "SpiComIF::initializeInterface: Failed to insert device with address "
"%lu to SPI device map\n",
static_cast<unsigned long>(spiAddress));
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
auto gpioPin = spiCookie->getChipSelectGpioPin(); }
auto gpioPort = spiCookie->getChipSelectGpioPort(); auto gpioPin = spiCookie->getChipSelectGpioPin();
auto gpioPort = spiCookie->getChipSelectGpioPort();
SPI_HandleTypeDef& spiHandle = spiCookie->getSpiHandle(); SPI_HandleTypeDef &spiHandle = spiCookie->getSpiHandle();
auto spiIdx = spiCookie->getSpiIdx(); auto spiIdx = spiCookie->getSpiIdx();
if(spiIdx == spi::SpiBus::SPI_1) { if (spiIdx == spi::SpiBus::SPI_1) {
#ifdef SPI1 #ifdef SPI1
spiHandle.Instance = SPI1; spiHandle.Instance = SPI1;
#endif #endif
} } else if (spiIdx == spi::SpiBus::SPI_2) {
else if(spiIdx == spi::SpiBus::SPI_2) {
#ifdef SPI2 #ifdef SPI2
spiHandle.Instance = SPI2; spiHandle.Instance = SPI2;
#endif #endif
} } else {
else { printCfgError("SPI Bus Index");
printCfgError("SPI Bus Index"); return HasReturnvaluesIF::RETURN_FAILED;
return HasReturnvaluesIF::RETURN_FAILED; }
}
auto mspCfg = spiCookie->getMspCfg(); auto mspCfg = spiCookie->getMspCfg();
if(transferMode == spi::TransferModes::POLLING) { if (transferMode == spi::TransferModes::POLLING) {
auto typedCfg = dynamic_cast<spi::MspPollingConfigStruct*>(mspCfg); auto typedCfg = dynamic_cast<spi::MspPollingConfigStruct *>(mspCfg);
if(typedCfg == nullptr) { if (typedCfg == nullptr) {
printCfgError("Polling MSP"); printCfgError("Polling MSP");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
spi::setSpiPollingMspFunctions(typedCfg);
} }
else if(transferMode == spi::TransferModes::INTERRUPT) { spi::setSpiPollingMspFunctions(typedCfg);
auto typedCfg = dynamic_cast<spi::MspIrqConfigStruct*>(mspCfg); } else if (transferMode == spi::TransferModes::INTERRUPT) {
if(typedCfg == nullptr) { auto typedCfg = dynamic_cast<spi::MspIrqConfigStruct *>(mspCfg);
printCfgError("IRQ MSP"); if (typedCfg == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; printCfgError("IRQ MSP");
} return HasReturnvaluesIF::RETURN_FAILED;
spi::setSpiIrqMspFunctions(typedCfg);
} }
else if(transferMode == spi::TransferModes::DMA) { spi::setSpiIrqMspFunctions(typedCfg);
auto typedCfg = dynamic_cast<spi::MspDmaConfigStruct*>(mspCfg); } else if (transferMode == spi::TransferModes::DMA) {
if(typedCfg == nullptr) { auto typedCfg = dynamic_cast<spi::MspDmaConfigStruct *>(mspCfg);
printCfgError("DMA MSP"); if (typedCfg == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; printCfgError("DMA MSP");
} return HasReturnvaluesIF::RETURN_FAILED;
// Check DMA handles
DMA_HandleTypeDef* txHandle = nullptr;
DMA_HandleTypeDef* rxHandle = nullptr;
spi::getDmaHandles(&txHandle, &rxHandle);
if(txHandle == nullptr or rxHandle == nullptr) {
printCfgError("DMA Handle");
return HasReturnvaluesIF::RETURN_FAILED;
}
spi::setSpiDmaMspFunctions(typedCfg);
} }
// Check DMA handles
DMA_HandleTypeDef *txHandle = nullptr;
DMA_HandleTypeDef *rxHandle = nullptr;
spi::getDmaHandles(&txHandle, &rxHandle);
if (txHandle == nullptr or rxHandle == nullptr) {
printCfgError("DMA Handle");
return HasReturnvaluesIF::RETURN_FAILED;
}
spi::setSpiDmaMspFunctions(typedCfg);
}
if(gpioPort != nullptr) { if (gpioPort != nullptr) {
gpio::initializeGpioClock(gpioPort); gpio::initializeGpioClock(gpioPort);
GPIO_InitTypeDef chipSelect = {}; GPIO_InitTypeDef chipSelect = {};
chipSelect.Pin = gpioPin; chipSelect.Pin = gpioPin;
chipSelect.Mode = GPIO_MODE_OUTPUT_PP; chipSelect.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(gpioPort, &chipSelect); HAL_GPIO_Init(gpioPort, &chipSelect);
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET); HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
} }
if(HAL_SPI_Init(&spiHandle) != HAL_OK) { if (HAL_SPI_Init(&spiHandle) != HAL_OK) {
sif::printWarning("SpiComIF::initialize: Error initializing SPI\n"); sif::printWarning("SpiComIF::initialize: Error initializing SPI\n");
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
// The MSP configuration struct is not required anymore // The MSP configuration struct is not required anymore
spiCookie->deleteMspCfg(); spiCookie->deleteMspCfg();
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) { ReturnValue_t SpiComIF::sendMessage(CookieIF *cookie, const uint8_t *sendData, size_t sendLen) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie); SpiCookie *spiCookie = dynamic_cast<SpiCookie *>(cookie);
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
return NULLPOINTER; return NULLPOINTER;
} }
SPI_HandleTypeDef& spiHandle = spiCookie->getSpiHandle(); SPI_HandleTypeDef &spiHandle = spiCookie->getSpiHandle();
auto iter = spiDeviceMap.find(spiCookie->getDeviceAddress()); auto iter = spiDeviceMap.find(spiCookie->getDeviceAddress());
if(iter == spiDeviceMap.end()) { if (iter == spiDeviceMap.end()) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
iter->second.currentTransferLen = sendLen; iter->second.currentTransferLen = sendLen;
auto transferMode = spiCookie->getTransferMode(); auto transferMode = spiCookie->getTransferMode();
switch(spiCookie->getTransferState()) { switch (spiCookie->getTransferState()) {
case(spi::TransferStates::IDLE): { case (spi::TransferStates::IDLE): {
break; break;
} }
case(spi::TransferStates::WAIT): case (spi::TransferStates::WAIT):
case(spi::TransferStates::FAILURE): case (spi::TransferStates::FAILURE):
case(spi::TransferStates::SUCCESS): case (spi::TransferStates::SUCCESS):
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
switch(transferMode) { switch (transferMode) {
case(spi::TransferModes::POLLING): { case (spi::TransferModes::POLLING): {
return handlePollingSendOperation(iter->second.replyBuffer.data(), spiHandle, *spiCookie, return handlePollingSendOperation(iter->second.replyBuffer.data(), spiHandle, *spiCookie,
sendData, sendLen); sendData, sendLen);
} }
case(spi::TransferModes::INTERRUPT): { case (spi::TransferModes::INTERRUPT): {
return handleInterruptSendOperation(iter->second.replyBuffer.data(), spiHandle, *spiCookie, return handleInterruptSendOperation(iter->second.replyBuffer.data(), spiHandle, *spiCookie,
sendData, sendLen); sendData, sendLen);
} }
case(spi::TransferModes::DMA): { case (spi::TransferModes::DMA): {
return handleDmaSendOperation(iter->second.replyBuffer.data(), spiHandle, *spiCookie, return handleDmaSendOperation(iter->second.replyBuffer.data(), spiHandle, *spiCookie,
sendData, sendLen); sendData, sendLen);
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t SpiComIF::getSendSuccess(CookieIF *cookie) { ReturnValue_t SpiComIF::getSendSuccess(CookieIF *cookie) { return HasReturnvaluesIF::RETURN_OK; }
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) { ReturnValue_t SpiComIF::requestReceiveMessage(CookieIF *cookie, size_t requestLen) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t SpiComIF::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) { ReturnValue_t SpiComIF::readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) {
SpiCookie* spiCookie = dynamic_cast<SpiCookie*>(cookie); SpiCookie *spiCookie = dynamic_cast<SpiCookie *>(cookie);
if(spiCookie == nullptr) { if (spiCookie == nullptr) {
return NULLPOINTER; return NULLPOINTER;
}
switch (spiCookie->getTransferState()) {
case (spi::TransferStates::SUCCESS): {
auto iter = spiDeviceMap.find(spiCookie->getDeviceAddress());
if (iter == spiDeviceMap.end()) {
return HasReturnvaluesIF::RETURN_FAILED;
}
*buffer = iter->second.replyBuffer.data();
*size = iter->second.currentTransferLen;
spiCookie->setTransferState(spi::TransferStates::IDLE);
break;
} }
switch(spiCookie->getTransferState()) { case (spi::TransferStates::FAILURE): {
case(spi::TransferStates::SUCCESS): {
auto iter = spiDeviceMap.find(spiCookie->getDeviceAddress());
if(iter == spiDeviceMap.end()) {
return HasReturnvaluesIF::RETURN_FAILED;
}
*buffer = iter->second.replyBuffer.data();
*size = iter->second.currentTransferLen;
spiCookie->setTransferState(spi::TransferStates::IDLE);
break;
}
case(spi::TransferStates::FAILURE): {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::readReceivedMessage: Transfer failure" << std::endl; sif::warning << "SpiComIF::readReceivedMessage: Transfer failure" << std::endl;
#else #else
sif::printWarning("SpiComIF::readReceivedMessage: Transfer failure\n"); sif::printWarning("SpiComIF::readReceivedMessage: Transfer failure\n");
#endif #endif
#endif #endif
spiCookie->setTransferState(spi::TransferStates::IDLE); spiCookie->setTransferState(spi::TransferStates::IDLE);
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
case(spi::TransferStates::WAIT): case (spi::TransferStates::WAIT):
case(spi::TransferStates::IDLE): { case (spi::TransferStates::IDLE): {
break; break;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void SpiComIF::setDefaultPollingTimeout(dur_millis_t timeout) { void SpiComIF::setDefaultPollingTimeout(dur_millis_t timeout) {
this->defaultPollingTimeout = timeout; this->defaultPollingTimeout = timeout;
} }
ReturnValue_t SpiComIF::handlePollingSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle, ReturnValue_t SpiComIF::handlePollingSendOperation(uint8_t *recvPtr, SPI_HandleTypeDef &spiHandle,
SpiCookie& spiCookie, const uint8_t *sendData, size_t sendLen) { SpiCookie &spiCookie, const uint8_t *sendData,
auto gpioPort = spiCookie.getChipSelectGpioPort(); size_t sendLen) {
auto gpioPin = spiCookie.getChipSelectGpioPin(); auto gpioPort = spiCookie.getChipSelectGpioPort();
auto returnval = spiSemaphore->acquire(timeoutType, timeoutMs); auto gpioPin = spiCookie.getChipSelectGpioPin();
if(returnval != HasReturnvaluesIF::RETURN_OK) { auto returnval = spiSemaphore->acquire(timeoutType, timeoutMs);
return returnval; if (returnval != HasReturnvaluesIF::RETURN_OK) {
} return returnval;
spiCookie.setTransferState(spi::TransferStates::WAIT); }
if(gpioPort != nullptr) { spiCookie.setTransferState(spi::TransferStates::WAIT);
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET); if (gpioPort != nullptr) {
} HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_RESET);
}
auto result = HAL_SPI_TransmitReceive(&spiHandle, const_cast<uint8_t*>(sendData), auto result = HAL_SPI_TransmitReceive(&spiHandle, const_cast<uint8_t *>(sendData), recvPtr,
recvPtr, sendLen, defaultPollingTimeout); sendLen, defaultPollingTimeout);
if(gpioPort != nullptr) { if (gpioPort != nullptr) {
HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET); HAL_GPIO_WritePin(gpioPort, gpioPin, GPIO_PIN_SET);
}
spiSemaphore->release();
switch (result) {
case (HAL_OK): {
spiCookie.setTransferState(spi::TransferStates::SUCCESS);
break;
} }
spiSemaphore->release(); case (HAL_TIMEOUT): {
switch(result) {
case(HAL_OK): {
spiCookie.setTransferState(spi::TransferStates::SUCCESS);
break;
}
case(HAL_TIMEOUT): {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Polling Mode | Timeout for SPI device" << sif::warning << "SpiComIF::sendMessage: Polling Mode | Timeout for SPI device"
spiCookie->getDeviceAddress() << std::endl; << spiCookie->getDeviceAddress() << std::endl;
#else #else
sif::printWarning("SpiComIF::sendMessage: Polling Mode | Timeout for SPI device %d\n", sif::printWarning("SpiComIF::sendMessage: Polling Mode | Timeout for SPI device %d\n",
spiCookie.getDeviceAddress()); spiCookie.getDeviceAddress());
#endif #endif
#endif #endif
spiCookie.setTransferState(spi::TransferStates::FAILURE); spiCookie.setTransferState(spi::TransferStates::FAILURE);
return spi::HAL_TIMEOUT_RETVAL; return spi::HAL_TIMEOUT_RETVAL;
} }
case(HAL_ERROR): case (HAL_ERROR):
default: { default: {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::sendMessage: Polling Mode | HAL error for SPI device" << sif::warning << "SpiComIF::sendMessage: Polling Mode | HAL error for SPI device"
spiCookie->getDeviceAddress() << std::endl; << spiCookie->getDeviceAddress() << std::endl;
#else #else
sif::printWarning("SpiComIF::sendMessage: Polling Mode | HAL error for SPI device %d\n", sif::printWarning("SpiComIF::sendMessage: Polling Mode | HAL error for SPI device %d\n",
spiCookie.getDeviceAddress()); spiCookie.getDeviceAddress());
#endif #endif
#endif #endif
spiCookie.setTransferState(spi::TransferStates::FAILURE); spiCookie.setTransferState(spi::TransferStates::FAILURE);
return spi::HAL_ERROR_RETVAL; return spi::HAL_ERROR_RETVAL;
} }
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t SpiComIF::handleInterruptSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle, ReturnValue_t SpiComIF::handleInterruptSendOperation(uint8_t *recvPtr, SPI_HandleTypeDef &spiHandle,
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen) { SpiCookie &spiCookie, const uint8_t *sendData,
return handleIrqSendOperation(recvPtr, spiHandle, spiCookie, sendData, sendLen); size_t sendLen) {
return handleIrqSendOperation(recvPtr, spiHandle, spiCookie, sendData, sendLen);
} }
ReturnValue_t SpiComIF::handleDmaSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle, ReturnValue_t SpiComIF::handleDmaSendOperation(uint8_t *recvPtr, SPI_HandleTypeDef &spiHandle,
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen) { SpiCookie &spiCookie, const uint8_t *sendData,
return handleIrqSendOperation(recvPtr, spiHandle, spiCookie, sendData, sendLen); size_t sendLen) {
return handleIrqSendOperation(recvPtr, spiHandle, spiCookie, sendData, sendLen);
} }
ReturnValue_t SpiComIF::handleIrqSendOperation(uint8_t *recvPtr, SPI_HandleTypeDef& spiHandle, ReturnValue_t SpiComIF::handleIrqSendOperation(uint8_t *recvPtr, SPI_HandleTypeDef &spiHandle,
SpiCookie& spiCookie, const uint8_t *sendData, size_t sendLen) { SpiCookie &spiCookie, const uint8_t *sendData,
ReturnValue_t result = genericIrqSendSetup(recvPtr, spiHandle, spiCookie, sendData, sendLen); size_t sendLen) {
if(result != HasReturnvaluesIF::RETURN_OK) { ReturnValue_t result = genericIrqSendSetup(recvPtr, spiHandle, spiCookie, sendData, sendLen);
return result; if (result != HasReturnvaluesIF::RETURN_OK) {
}
// yet another HAL driver which is not const-correct..
HAL_StatusTypeDef status = HAL_OK;
auto transferMode = spiCookie.getTransferMode();
if(transferMode == spi::TransferModes::DMA) {
if(cacheMaintenanceOnTxBuffer) {
/* Clean D-cache. Make sure the address is 32-byte aligned and add 32-bytes to length,
in case it overlaps cacheline */
SCB_CleanDCache_by_Addr((uint32_t*)(((uint32_t) sendData ) & ~(uint32_t)0x1F),
sendLen + 32);
}
status = HAL_SPI_TransmitReceive_DMA(&spiHandle, const_cast<uint8_t*>(sendData),
currentRecvPtr, sendLen);
}
else {
status = HAL_SPI_TransmitReceive_IT(&spiHandle, const_cast<uint8_t*>(sendData),
currentRecvPtr, sendLen);
}
switch(status) {
case(HAL_OK): {
break;
}
default: {
return halErrorHandler(status, transferMode);
}
}
return result; return result;
}
// yet another HAL driver which is not const-correct..
HAL_StatusTypeDef status = HAL_OK;
auto transferMode = spiCookie.getTransferMode();
if (transferMode == spi::TransferModes::DMA) {
if (cacheMaintenanceOnTxBuffer) {
/* Clean D-cache. Make sure the address is 32-byte aligned and add 32-bytes to length,
in case it overlaps cacheline */
SCB_CleanDCache_by_Addr((uint32_t *)(((uint32_t)sendData) & ~(uint32_t)0x1F), sendLen + 32);
}
status = HAL_SPI_TransmitReceive_DMA(&spiHandle, const_cast<uint8_t *>(sendData),
currentRecvPtr, sendLen);
} else {
status = HAL_SPI_TransmitReceive_IT(&spiHandle, const_cast<uint8_t *>(sendData), currentRecvPtr,
sendLen);
}
switch (status) {
case (HAL_OK): {
break;
}
default: {
return halErrorHandler(status, transferMode);
}
}
return result;
} }
ReturnValue_t SpiComIF::halErrorHandler(HAL_StatusTypeDef status, spi::TransferModes transferMode) { ReturnValue_t SpiComIF::halErrorHandler(HAL_StatusTypeDef status, spi::TransferModes transferMode) {
char modeString[10]; char modeString[10];
if(transferMode == spi::TransferModes::DMA) { if (transferMode == spi::TransferModes::DMA) {
std::snprintf(modeString, sizeof(modeString), "Dma"); std::snprintf(modeString, sizeof(modeString), "Dma");
} else {
std::snprintf(modeString, sizeof(modeString), "Interrupt");
}
sif::printWarning("SpiComIF::handle%sSendOperation: HAL error %d occured\n", modeString, status);
switch (status) {
case (HAL_BUSY): {
return spi::HAL_BUSY_RETVAL;
} }
else { case (HAL_ERROR): {
std::snprintf(modeString, sizeof(modeString), "Interrupt"); return spi::HAL_ERROR_RETVAL;
} }
sif::printWarning("SpiComIF::handle%sSendOperation: HAL error %d occured\n", modeString, case (HAL_TIMEOUT): {
status); return spi::HAL_TIMEOUT_RETVAL;
switch(status) {
case(HAL_BUSY): {
return spi::HAL_BUSY_RETVAL;
}
case(HAL_ERROR): {
return spi::HAL_ERROR_RETVAL;
}
case(HAL_TIMEOUT): {
return spi::HAL_TIMEOUT_RETVAL;
} }
default: { default: {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
} }
}
} }
ReturnValue_t SpiComIF::genericIrqSendSetup(uint8_t *recvPtr, SPI_HandleTypeDef &spiHandle,
SpiCookie &spiCookie, const uint8_t *sendData,
size_t sendLen) {
currentRecvPtr = recvPtr;
currentRecvBuffSize = sendLen;
ReturnValue_t SpiComIF::genericIrqSendSetup(uint8_t *recvPtr, SPI_HandleTypeDef& spiHandle, // Take the semaphore which will be released by a callback when the transfer is complete
SpiCookie& spiCookie, const uint8_t *sendData, size_t sendLen) { ReturnValue_t result = spiSemaphore->acquire(SemaphoreIF::TimeoutType::WAITING, timeoutMs);
currentRecvPtr = recvPtr; if (result != HasReturnvaluesIF::RETURN_OK) {
currentRecvBuffSize = sendLen; // Configuration error
sif::printWarning(
// Take the semaphore which will be released by a callback when the transfer is complete "SpiComIF::handleInterruptSendOperation: Semaphore "
ReturnValue_t result = spiSemaphore->acquire(SemaphoreIF::TimeoutType::WAITING, timeoutMs); "could not be acquired after %d ms\n",
if(result != HasReturnvaluesIF::RETURN_OK) { timeoutMs);
// Configuration error return result;
sif::printWarning("SpiComIF::handleInterruptSendOperation: Semaphore " }
"could not be acquired after %d ms\n", timeoutMs); // Cache the current SPI handle in any case
return result; spi::setSpiHandle(&spiHandle);
} // Assign the IRQ arguments for the user callbacks
// Cache the current SPI handle in any case irqArgs.comIF = this;
spi::setSpiHandle(&spiHandle); irqArgs.spiCookie = &spiCookie;
// Assign the IRQ arguments for the user callbacks // The SPI handle is passed to the default SPI callback as a void argument. This callback
irqArgs.comIF = this; // is different from the user callbacks specified above!
irqArgs.spiCookie = &spiCookie; spi::assignSpiUserArgs(spiCookie.getSpiIdx(), reinterpret_cast<void *>(&spiHandle));
// The SPI handle is passed to the default SPI callback as a void argument. This callback if (spiCookie.getChipSelectGpioPort() != nullptr) {
// is different from the user callbacks specified above! HAL_GPIO_WritePin(spiCookie.getChipSelectGpioPort(), spiCookie.getChipSelectGpioPin(),
spi::assignSpiUserArgs(spiCookie.getSpiIdx(), reinterpret_cast<void*>(&spiHandle)); GPIO_PIN_RESET);
if(spiCookie.getChipSelectGpioPort() != nullptr) { }
HAL_GPIO_WritePin(spiCookie.getChipSelectGpioPort(), spiCookie.getChipSelectGpioPin(), return HasReturnvaluesIF::RETURN_OK;
GPIO_PIN_RESET);
}
return HasReturnvaluesIF::RETURN_OK;
} }
void SpiComIF::spiTransferTxCompleteCallback(SPI_HandleTypeDef *hspi, void *args) { void SpiComIF::spiTransferTxCompleteCallback(SPI_HandleTypeDef *hspi, void *args) {
genericIrqHandler(args, spi::TransferStates::SUCCESS); genericIrqHandler(args, spi::TransferStates::SUCCESS);
} }
void SpiComIF::spiTransferRxCompleteCallback(SPI_HandleTypeDef *hspi, void *args) { void SpiComIF::spiTransferRxCompleteCallback(SPI_HandleTypeDef *hspi, void *args) {
genericIrqHandler(args, spi::TransferStates::SUCCESS); genericIrqHandler(args, spi::TransferStates::SUCCESS);
} }
void SpiComIF::spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void *args) { void SpiComIF::spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void *args) {
genericIrqHandler(args, spi::TransferStates::SUCCESS); genericIrqHandler(args, spi::TransferStates::SUCCESS);
} }
void SpiComIF::spiTransferErrorCallback(SPI_HandleTypeDef *hspi, void *args) { void SpiComIF::spiTransferErrorCallback(SPI_HandleTypeDef *hspi, void *args) {
genericIrqHandler(args, spi::TransferStates::FAILURE); genericIrqHandler(args, spi::TransferStates::FAILURE);
} }
void SpiComIF::genericIrqHandler(void *irqArgsVoid, spi::TransferStates targetState) { void SpiComIF::genericIrqHandler(void *irqArgsVoid, spi::TransferStates targetState) {
IrqArgs* irqArgs = reinterpret_cast<IrqArgs*>(irqArgsVoid); IrqArgs *irqArgs = reinterpret_cast<IrqArgs *>(irqArgsVoid);
if(irqArgs == nullptr) { if (irqArgs == nullptr) {
return; return;
} }
SpiCookie* spiCookie = irqArgs->spiCookie; SpiCookie *spiCookie = irqArgs->spiCookie;
SpiComIF* comIF = irqArgs->comIF; SpiComIF *comIF = irqArgs->comIF;
if(spiCookie == nullptr or comIF == nullptr) { if (spiCookie == nullptr or comIF == nullptr) {
return; return;
} }
spiCookie->setTransferState(targetState); spiCookie->setTransferState(targetState);
if(spiCookie->getChipSelectGpioPort() != nullptr) {
// Pull CS pin high again
HAL_GPIO_WritePin(spiCookie->getChipSelectGpioPort(), spiCookie->getChipSelectGpioPin(),
GPIO_PIN_SET);
}
if (spiCookie->getChipSelectGpioPort() != nullptr) {
// Pull CS pin high again
HAL_GPIO_WritePin(spiCookie->getChipSelectGpioPort(), spiCookie->getChipSelectGpioPin(),
GPIO_PIN_SET);
}
#if defined FSFW_OSAL_FREERTOS #if defined FSFW_OSAL_FREERTOS
// Release the task semaphore // Release the task semaphore
BaseType_t taskWoken = pdFALSE; BaseType_t taskWoken = pdFALSE;
ReturnValue_t result = BinarySemaphore::releaseFromISR(comIF->spiSemaphore->getSemaphore(), ReturnValue_t result =
&taskWoken); BinarySemaphore::releaseFromISR(comIF->spiSemaphore->getSemaphore(), &taskWoken);
#elif defined FSFW_OSAL_RTEMS #elif defined FSFW_OSAL_RTEMS
ReturnValue_t result = comIF->spiSemaphore->release(); ReturnValue_t result = comIF->spiSemaphore->release();
#endif #endif
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
// Configuration error // Configuration error
printf("SpiComIF::genericIrqHandler: Failure releasing Semaphore!\n"); printf("SpiComIF::genericIrqHandler: Failure releasing Semaphore!\n");
} }
// Perform cache maintenance operation for DMA transfers // Perform cache maintenance operation for DMA transfers
if(spiCookie->getTransferMode() == spi::TransferModes::DMA) { if (spiCookie->getTransferMode() == spi::TransferModes::DMA) {
// Invalidate cache prior to access by CPU // Invalidate cache prior to access by CPU
SCB_InvalidateDCache_by_Addr ((uint32_t *) comIF->currentRecvPtr, SCB_InvalidateDCache_by_Addr((uint32_t *)comIF->currentRecvPtr, comIF->currentRecvBuffSize);
comIF->currentRecvBuffSize); }
}
#if defined FSFW_OSAL_FREERTOS #if defined FSFW_OSAL_FREERTOS
/* Request a context switch if the SPI ComIF task was woken up and has a higher priority /* Request a context switch if the SPI ComIF task was woken up and has a higher priority
than the currently running task */ than the currently running task */
if(taskWoken == pdTRUE) { if (taskWoken == pdTRUE) {
TaskManagement::requestContextSwitch(CallContext::ISR); TaskManagement::requestContextSwitch(CallContext::ISR);
} }
#endif #endif
} }
void SpiComIF::printCfgError(const char *const type) { void SpiComIF::printCfgError(const char *const type) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "SpiComIF::initializeInterface: Invalid " << type << " configuration" sif::warning << "SpiComIF::initializeInterface: Invalid " << type << " configuration"
<< std::endl; << std::endl;
#else #else
sif::printWarning("SpiComIF::initializeInterface: Invalid %s configuration\n", type); sif::printWarning("SpiComIF::initializeInterface: Invalid %s configuration\n", type);
#endif #endif
} }

View File

@ -1,16 +1,15 @@
#ifndef FSFW_HAL_STM32H7_SPI_SPICOMIF_H_ #ifndef FSFW_HAL_STM32H7_SPI_SPICOMIF_H_
#define FSFW_HAL_STM32H7_SPI_SPICOMIF_H_ #define FSFW_HAL_STM32H7_SPI_SPICOMIF_H_
#include "fsfw/tasks/SemaphoreIF.h" #include <map>
#include <vector>
#include "fsfw/devicehandlers/DeviceCommunicationIF.h" #include "fsfw/devicehandlers/DeviceCommunicationIF.h"
#include "fsfw/objectmanager/SystemObject.h" #include "fsfw/objectmanager/SystemObject.h"
#include "fsfw/tasks/SemaphoreIF.h"
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h" #include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
#include "stm32h7xx_hal_spi.h"
#include "stm32h743xx.h" #include "stm32h743xx.h"
#include "stm32h7xx_hal_spi.h"
#include <vector>
#include <map>
class SpiCookie; class SpiCookie;
class BinarySemaphore; class BinarySemaphore;
@ -28,102 +27,100 @@ class BinarySemaphore;
* implementation limits the transfer mode for a given SPI bus. * implementation limits the transfer mode for a given SPI bus.
* @author R. Mueller * @author R. Mueller
*/ */
class SpiComIF: class SpiComIF : public SystemObject, public DeviceCommunicationIF {
public SystemObject, public:
public DeviceCommunicationIF { /**
public: * Create a SPI communication interface for the given SPI peripheral (spiInstance)
/** * @param objectId
* Create a SPI communication interface for the given SPI peripheral (spiInstance) * @param spiInstance
* @param objectId * @param spiHandle
* @param spiInstance * @param transferMode
* @param spiHandle */
* @param transferMode SpiComIF(object_id_t objectId);
*/
SpiComIF(object_id_t objectId);
/** /**
* Allows the user to disable cache maintenance on the TX buffer. This can be done if the * Allows the user to disable cache maintenance on the TX buffer. This can be done if the
* TX buffers are places and MPU protected properly like specified in this link: * TX buffers are places and MPU protected properly like specified in this link:
* https://community.st.com/s/article/FAQ-DMA-is-not-working-on-STM32H7-devices * https://community.st.com/s/article/FAQ-DMA-is-not-working-on-STM32H7-devices
* The cache maintenace is enabled by default. * The cache maintenace is enabled by default.
* @param enable * @param enable
*/ */
void configureCacheMaintenanceOnTxBuffer(bool enable); void configureCacheMaintenanceOnTxBuffer(bool enable);
void setDefaultPollingTimeout(dur_millis_t timeout); void setDefaultPollingTimeout(dur_millis_t timeout);
/** /**
* Add the DMA handles. These need to be set in the DMA transfer mode is used. * Add the DMA handles. These need to be set in the DMA transfer mode is used.
* @param txHandle * @param txHandle
* @param rxHandle * @param rxHandle
*/ */
void addDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle); void addDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle);
ReturnValue_t initialize() override; ReturnValue_t initialize() override;
// DeviceCommunicationIF overrides // DeviceCommunicationIF overrides
virtual ReturnValue_t initializeInterface(CookieIF * cookie) override; virtual ReturnValue_t initializeInterface(CookieIF* cookie) override;
virtual ReturnValue_t sendMessage(CookieIF *cookie, virtual ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData,
const uint8_t * sendData, size_t sendLen) override; size_t sendLen) override;
virtual ReturnValue_t getSendSuccess(CookieIF *cookie) override; virtual ReturnValue_t getSendSuccess(CookieIF* cookie) override;
virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, virtual ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
size_t requestLen) override; virtual ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer,
virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, size_t* size) override;
uint8_t **buffer, size_t *size) override;
protected: protected:
struct SpiInstance {
SpiInstance(size_t maxRecvSize) : replyBuffer(std::vector<uint8_t>(maxRecvSize)) {}
std::vector<uint8_t> replyBuffer;
size_t currentTransferLen = 0;
};
struct SpiInstance { struct IrqArgs {
SpiInstance(size_t maxRecvSize): replyBuffer(std::vector<uint8_t>(maxRecvSize)) {} SpiComIF* comIF = nullptr;
std::vector<uint8_t> replyBuffer; SpiCookie* spiCookie = nullptr;
size_t currentTransferLen = 0; };
};
struct IrqArgs { IrqArgs irqArgs;
SpiComIF* comIF = nullptr;
SpiCookie* spiCookie = nullptr;
};
IrqArgs irqArgs; uint32_t defaultPollingTimeout = 50;
uint32_t defaultPollingTimeout = 50; SemaphoreIF::TimeoutType timeoutType = SemaphoreIF::TimeoutType::WAITING;
dur_millis_t timeoutMs = 20;
SemaphoreIF::TimeoutType timeoutType = SemaphoreIF::TimeoutType::WAITING; BinarySemaphore* spiSemaphore = nullptr;
dur_millis_t timeoutMs = 20; bool cacheMaintenanceOnTxBuffer = true;
BinarySemaphore* spiSemaphore = nullptr; using SpiDeviceMap = std::map<address_t, SpiInstance>;
bool cacheMaintenanceOnTxBuffer = true; using SpiDeviceMapIter = SpiDeviceMap::iterator;
using SpiDeviceMap = std::map<address_t, SpiInstance>; uint8_t* currentRecvPtr = nullptr;
using SpiDeviceMapIter = SpiDeviceMap::iterator; size_t currentRecvBuffSize = 0;
uint8_t* currentRecvPtr = nullptr; SpiDeviceMap spiDeviceMap;
size_t currentRecvBuffSize = 0;
SpiDeviceMap spiDeviceMap; ReturnValue_t handlePollingSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t* sendData,
size_t sendLen);
ReturnValue_t handleInterruptSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t* sendData,
size_t sendLen);
ReturnValue_t handleDmaSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t* sendData,
size_t sendLen);
ReturnValue_t handleIrqSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t* sendData,
size_t sendLen);
ReturnValue_t genericIrqSendSetup(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t* sendData, size_t sendLen);
ReturnValue_t halErrorHandler(HAL_StatusTypeDef status, spi::TransferModes transferMode);
ReturnValue_t handlePollingSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle, static void spiTransferTxCompleteCallback(SPI_HandleTypeDef* hspi, void* args);
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen); static void spiTransferRxCompleteCallback(SPI_HandleTypeDef* hspi, void* args);
ReturnValue_t handleInterruptSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle, static void spiTransferCompleteCallback(SPI_HandleTypeDef* hspi, void* args);
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen); static void spiTransferErrorCallback(SPI_HandleTypeDef* hspi, void* args);
ReturnValue_t handleDmaSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen);
ReturnValue_t handleIrqSendOperation(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen);
ReturnValue_t genericIrqSendSetup(uint8_t* recvPtr, SPI_HandleTypeDef& spiHandle,
SpiCookie& spiCookie, const uint8_t * sendData, size_t sendLen);
ReturnValue_t halErrorHandler(HAL_StatusTypeDef status, spi::TransferModes transferMode);
static void spiTransferTxCompleteCallback(SPI_HandleTypeDef *hspi, void* args); static void genericIrqHandler(void* irqArgs, spi::TransferStates targetState);
static void spiTransferRxCompleteCallback(SPI_HandleTypeDef *hspi, void* args);
static void spiTransferCompleteCallback(SPI_HandleTypeDef *hspi, void* args);
static void spiTransferErrorCallback(SPI_HandleTypeDef *hspi, void* args);
static void genericIrqHandler(void* irqArgs, spi::TransferStates targetState); void printCfgError(const char* const type);
void printCfgError(const char* const type);
}; };
#endif /* FSFW_HAL_STM32H7_SPI_SPICOMIF_H_ */ #endif /* FSFW_HAL_STM32H7_SPI_SPICOMIF_H_ */

View File

@ -1,78 +1,60 @@
#include "fsfw_hal/stm32h7/spi/SpiCookie.h" #include "fsfw_hal/stm32h7/spi/SpiCookie.h"
SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode, SpiCookie::SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode, spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode,
size_t maxRecvSize, stm32h7::GpioCfg csGpio): size_t maxRecvSize, stm32h7::GpioCfg csGpio)
deviceAddress(deviceAddress), spiIdx(spiIdx), spiSpeed(spiSpeed), spiMode(spiMode), : deviceAddress(deviceAddress),
transferMode(transferMode), csGpio(csGpio), spiIdx(spiIdx),
mspCfg(mspCfg), maxRecvSize(maxRecvSize) { spiSpeed(spiSpeed),
spiHandle.Init.DataSize = SPI_DATASIZE_8BIT; spiMode(spiMode),
spiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; transferMode(transferMode),
spiHandle.Init.TIMode = SPI_TIMODE_DISABLE; csGpio(csGpio),
spiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; mspCfg(mspCfg),
spiHandle.Init.CRCPolynomial = 7; maxRecvSize(maxRecvSize) {
spiHandle.Init.CRCLength = SPI_CRC_LENGTH_8BIT; spiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
spiHandle.Init.NSS = SPI_NSS_SOFT; spiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
spiHandle.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; spiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
spiHandle.Init.Direction = SPI_DIRECTION_2LINES; spiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
// Recommended setting to avoid glitches spiHandle.Init.CRCPolynomial = 7;
spiHandle.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE; spiHandle.Init.CRCLength = SPI_CRC_LENGTH_8BIT;
spiHandle.Init.Mode = SPI_MODE_MASTER; spiHandle.Init.NSS = SPI_NSS_SOFT;
spi::assignSpiMode(spiMode, spiHandle); spiHandle.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
spiHandle.Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), spiSpeed); spiHandle.Init.Direction = SPI_DIRECTION_2LINES;
// Recommended setting to avoid glitches
spiHandle.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
spiHandle.Init.Mode = SPI_MODE_MASTER;
spi::assignSpiMode(spiMode, spiHandle);
spiHandle.Init.BaudRatePrescaler = spi::getPrescaler(HAL_RCC_GetHCLKFreq(), spiSpeed);
} }
uint16_t SpiCookie::getChipSelectGpioPin() const { uint16_t SpiCookie::getChipSelectGpioPin() const { return csGpio.pin; }
return csGpio.pin;
}
GPIO_TypeDef* SpiCookie::getChipSelectGpioPort() { GPIO_TypeDef* SpiCookie::getChipSelectGpioPort() { return csGpio.port; }
return csGpio.port;
}
address_t SpiCookie::getDeviceAddress() const { address_t SpiCookie::getDeviceAddress() const { return deviceAddress; }
return deviceAddress;
}
spi::SpiBus SpiCookie::getSpiIdx() const { spi::SpiBus SpiCookie::getSpiIdx() const { return spiIdx; }
return spiIdx;
}
spi::SpiModes SpiCookie::getSpiMode() const { spi::SpiModes SpiCookie::getSpiMode() const { return spiMode; }
return spiMode;
}
uint32_t SpiCookie::getSpiSpeed() const { uint32_t SpiCookie::getSpiSpeed() const { return spiSpeed; }
return spiSpeed;
}
size_t SpiCookie::getMaxRecvSize() const { size_t SpiCookie::getMaxRecvSize() const { return maxRecvSize; }
return maxRecvSize;
}
SPI_HandleTypeDef& SpiCookie::getSpiHandle() { SPI_HandleTypeDef& SpiCookie::getSpiHandle() { return spiHandle; }
return spiHandle;
}
spi::MspCfgBase* SpiCookie::getMspCfg() { spi::MspCfgBase* SpiCookie::getMspCfg() { return mspCfg; }
return mspCfg;
}
void SpiCookie::deleteMspCfg() { void SpiCookie::deleteMspCfg() {
if(mspCfg != nullptr) { if (mspCfg != nullptr) {
delete mspCfg; delete mspCfg;
} }
} }
spi::TransferModes SpiCookie::getTransferMode() const { spi::TransferModes SpiCookie::getTransferMode() const { return transferMode; }
return transferMode;
}
void SpiCookie::setTransferState(spi::TransferStates transferState) { void SpiCookie::setTransferState(spi::TransferStates transferState) {
this->transferState = transferState; this->transferState = transferState;
} }
spi::TransferStates SpiCookie::getTransferState() const { spi::TransferStates SpiCookie::getTransferState() const { return this->transferState; }
return this->transferState;
}

View File

@ -1,16 +1,14 @@
#ifndef FSFW_HAL_STM32H7_SPI_SPICOOKIE_H_ #ifndef FSFW_HAL_STM32H7_SPI_SPICOOKIE_H_
#define FSFW_HAL_STM32H7_SPI_SPICOOKIE_H_ #define FSFW_HAL_STM32H7_SPI_SPICOOKIE_H_
#include "spiDefinitions.h"
#include "mspInit.h"
#include "../definitions.h"
#include "fsfw/devicehandlers/CookieIF.h"
#include "stm32h743xx.h"
#include <utility> #include <utility>
#include "../definitions.h"
#include "fsfw/devicehandlers/CookieIF.h"
#include "mspInit.h"
#include "spiDefinitions.h"
#include "stm32h743xx.h"
/** /**
* @brief SPI cookie implementation for the STM32H7 device family * @brief SPI cookie implementation for the STM32H7 device family
* @details * @details
@ -18,63 +16,61 @@
* SPI communication interface * SPI communication interface
* @author R. Mueller * @author R. Mueller
*/ */
class SpiCookie: public CookieIF { class SpiCookie : public CookieIF {
friend class SpiComIF; friend class SpiComIF;
public:
/** public:
* Allows construction of a SPI cookie for a connected SPI device /**
* @param deviceAddress * Allows construction of a SPI cookie for a connected SPI device
* @param spiIdx SPI bus, e.g. SPI1 or SPI2 * @param deviceAddress
* @param transferMode * @param spiIdx SPI bus, e.g. SPI1 or SPI2
* @param mspCfg This is the MSP configuration. The user is expected to supply * @param transferMode
* a valid MSP configuration. See mspInit.h for functions * @param mspCfg This is the MSP configuration. The user is expected to supply
* to create one. * a valid MSP configuration. See mspInit.h for functions
* @param spiSpeed * to create one.
* @param spiMode * @param spiSpeed
* @param chipSelectGpioPin GPIO port. Don't use a number here, use the 16 bit type * @param spiMode
* definitions supplied in the MCU header file! (e.g. GPIO_PIN_X) * @param chipSelectGpioPin GPIO port. Don't use a number here, use the 16 bit type
* @param chipSelectGpioPort GPIO port (e.g. GPIOA) * definitions supplied in the MCU header file! (e.g. GPIO_PIN_X)
* @param maxRecvSize Maximum expected receive size. Chose as small as possible. * @param chipSelectGpioPort GPIO port (e.g. GPIOA)
* @param csGpio Optional CS GPIO definition. * @param maxRecvSize Maximum expected receive size. Chose as small as possible.
*/ * @param csGpio Optional CS GPIO definition.
SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode, */
spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode, SpiCookie(address_t deviceAddress, spi::SpiBus spiIdx, spi::TransferModes transferMode,
size_t maxRecvSize, stm32h7::GpioCfg csGpio = stm32h7::GpioCfg(nullptr, 0, 0)); spi::MspCfgBase* mspCfg, uint32_t spiSpeed, spi::SpiModes spiMode, size_t maxRecvSize,
stm32h7::GpioCfg csGpio = stm32h7::GpioCfg(nullptr, 0, 0));
uint16_t getChipSelectGpioPin() const; uint16_t getChipSelectGpioPin() const;
GPIO_TypeDef* getChipSelectGpioPort(); GPIO_TypeDef* getChipSelectGpioPort();
address_t getDeviceAddress() const; address_t getDeviceAddress() const;
spi::SpiBus getSpiIdx() const; spi::SpiBus getSpiIdx() const;
spi::SpiModes getSpiMode() const; spi::SpiModes getSpiMode() const;
spi::TransferModes getTransferMode() const; spi::TransferModes getTransferMode() const;
uint32_t getSpiSpeed() const; uint32_t getSpiSpeed() const;
size_t getMaxRecvSize() const; size_t getMaxRecvSize() const;
SPI_HandleTypeDef& getSpiHandle(); SPI_HandleTypeDef& getSpiHandle();
private: private:
address_t deviceAddress; address_t deviceAddress;
SPI_HandleTypeDef spiHandle = {}; SPI_HandleTypeDef spiHandle = {};
spi::SpiBus spiIdx; spi::SpiBus spiIdx;
uint32_t spiSpeed; uint32_t spiSpeed;
spi::SpiModes spiMode; spi::SpiModes spiMode;
spi::TransferModes transferMode; spi::TransferModes transferMode;
volatile spi::TransferStates transferState = spi::TransferStates::IDLE; volatile spi::TransferStates transferState = spi::TransferStates::IDLE;
stm32h7::GpioCfg csGpio; stm32h7::GpioCfg csGpio;
// The MSP configuration is cached here. Be careful when using this, it is automatically // The MSP configuration is cached here. Be careful when using this, it is automatically
// deleted by the SPI communication interface if it is not required anymore! // deleted by the SPI communication interface if it is not required anymore!
spi::MspCfgBase* mspCfg = nullptr; spi::MspCfgBase* mspCfg = nullptr;
const size_t maxRecvSize; const size_t maxRecvSize;
// Only the SpiComIF is allowed to use this to prevent dangling pointers issues // Only the SpiComIF is allowed to use this to prevent dangling pointers issues
spi::MspCfgBase* getMspCfg(); spi::MspCfgBase* getMspCfg();
void deleteMspCfg(); void deleteMspCfg();
void setTransferState(spi::TransferStates transferState); void setTransferState(spi::TransferStates transferState);
spi::TransferStates getTransferState() const; spi::TransferStates getTransferState() const;
}; };
#endif /* FSFW_HAL_STM32H7_SPI_SPICOOKIE_H_ */ #endif /* FSFW_HAL_STM32H7_SPI_SPICOOKIE_H_ */

View File

@ -1,15 +1,15 @@
#include "fsfw_hal/stm32h7/dma.h"
#include "fsfw_hal/stm32h7/spi/mspInit.h" #include "fsfw_hal/stm32h7/spi/mspInit.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "stm32h743xx.h"
#include "stm32h7xx_hal_spi.h"
#include "stm32h7xx_hal_dma.h"
#include "stm32h7xx_hal_def.h"
#include <cstdio> #include <cstdio>
#include "fsfw_hal/stm32h7/dma.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "stm32h743xx.h"
#include "stm32h7xx_hal_def.h"
#include "stm32h7xx_hal_dma.h"
#include "stm32h7xx_hal_spi.h"
spi::msp_func_t mspInitFunc = nullptr; spi::msp_func_t mspInitFunc = nullptr;
spi::MspCfgBase* mspInitArgs = nullptr; spi::MspCfgBase* mspInitArgs = nullptr;
@ -27,56 +27,55 @@ spi::MspCfgBase* mspDeinitArgs = nullptr;
* @retval None * @retval None
*/ */
void spi::halMspInitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) { void spi::halMspInitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = dynamic_cast<MspDmaConfigStruct*>(cfgBase); auto cfg = dynamic_cast<MspDmaConfigStruct*>(cfgBase);
if(hspi == nullptr or cfg == nullptr) { if (hspi == nullptr or cfg == nullptr) {
return; return;
} }
setSpiHandle(hspi); setSpiHandle(hspi);
DMA_HandleTypeDef* hdma_tx = nullptr; DMA_HandleTypeDef* hdma_tx = nullptr;
DMA_HandleTypeDef* hdma_rx = nullptr; DMA_HandleTypeDef* hdma_rx = nullptr;
spi::getDmaHandles(&hdma_tx, &hdma_rx); spi::getDmaHandles(&hdma_tx, &hdma_rx);
if(hdma_tx == nullptr or hdma_rx == nullptr) { if (hdma_tx == nullptr or hdma_rx == nullptr) {
printf("HAL_SPI_MspInit: Invalid DMA handles. Make sure to call setDmaHandles!\n"); printf("HAL_SPI_MspInit: Invalid DMA handles. Make sure to call setDmaHandles!\n");
return; return;
} }
spi::halMspInitInterrupt(hspi, cfg); spi::halMspInitInterrupt(hspi, cfg);
// DMA setup // DMA setup
if(cfg->dmaClkEnableWrapper == nullptr) { if (cfg->dmaClkEnableWrapper == nullptr) {
mspErrorHandler("spi::halMspInitDma", "DMA Clock init invalid"); mspErrorHandler("spi::halMspInitDma", "DMA Clock init invalid");
} }
cfg->dmaClkEnableWrapper(); cfg->dmaClkEnableWrapper();
// Configure the DMA // Configure the DMA
/* Configure the DMA handler for Transmission process */ /* Configure the DMA handler for Transmission process */
if(hdma_tx->Instance == nullptr) { if (hdma_tx->Instance == nullptr) {
// Assume it was not configured properly // Assume it was not configured properly
mspErrorHandler("spi::halMspInitDma", "DMA TX handle invalid"); mspErrorHandler("spi::halMspInitDma", "DMA TX handle invalid");
} }
HAL_DMA_Init(hdma_tx); HAL_DMA_Init(hdma_tx);
/* Associate the initialized DMA handle to the the SPI handle */ /* Associate the initialized DMA handle to the the SPI handle */
__HAL_LINKDMA(hspi, hdmatx, *hdma_tx); __HAL_LINKDMA(hspi, hdmatx, *hdma_tx);
HAL_DMA_Init(hdma_rx); HAL_DMA_Init(hdma_rx);
/* Associate the initialized DMA handle to the the SPI handle */ /* Associate the initialized DMA handle to the the SPI handle */
__HAL_LINKDMA(hspi, hdmarx, *hdma_rx); __HAL_LINKDMA(hspi, hdmarx, *hdma_rx);
/*##-4- Configure the NVIC for DMA #########################################*/ /*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (SPI1_RX) */ /* NVIC configuration for DMA transfer complete interrupt (SPI1_RX) */
// Assign the interrupt handler // Assign the interrupt handler
dma::assignDmaUserHandler(cfg->rxDmaIndex, cfg->rxDmaStream, &spi::dmaRxIrqHandler, hdma_rx); dma::assignDmaUserHandler(cfg->rxDmaIndex, cfg->rxDmaStream, &spi::dmaRxIrqHandler, hdma_rx);
HAL_NVIC_SetPriority(cfg->rxDmaIrqNumber, cfg->rxPreEmptPriority, cfg->rxSubpriority); HAL_NVIC_SetPriority(cfg->rxDmaIrqNumber, cfg->rxPreEmptPriority, cfg->rxSubpriority);
HAL_NVIC_EnableIRQ(cfg->rxDmaIrqNumber); HAL_NVIC_EnableIRQ(cfg->rxDmaIrqNumber);
/* NVIC configuration for DMA transfer complete interrupt (SPI1_TX) */ /* NVIC configuration for DMA transfer complete interrupt (SPI1_TX) */
// Assign the interrupt handler // Assign the interrupt handler
dma::assignDmaUserHandler(cfg->txDmaIndex, cfg->txDmaStream, dma::assignDmaUserHandler(cfg->txDmaIndex, cfg->txDmaStream, &spi::dmaTxIrqHandler, hdma_tx);
&spi::dmaTxIrqHandler, hdma_tx); HAL_NVIC_SetPriority(cfg->txDmaIrqNumber, cfg->txPreEmptPriority, cfg->txSubpriority);
HAL_NVIC_SetPriority(cfg->txDmaIrqNumber, cfg->txPreEmptPriority, cfg->txSubpriority); HAL_NVIC_EnableIRQ(cfg->txDmaIrqNumber);
HAL_NVIC_EnableIRQ(cfg->txDmaIrqNumber);
} }
/** /**
@ -88,128 +87,126 @@ void spi::halMspInitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
* @retval None * @retval None
*/ */
void spi::halMspDeinitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) { void spi::halMspDeinitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = dynamic_cast<MspDmaConfigStruct*>(cfgBase); auto cfg = dynamic_cast<MspDmaConfigStruct*>(cfgBase);
if(hspi == nullptr or cfg == nullptr) { if (hspi == nullptr or cfg == nullptr) {
return; return;
} }
spi::halMspDeinitInterrupt(hspi, cfgBase); spi::halMspDeinitInterrupt(hspi, cfgBase);
DMA_HandleTypeDef* hdma_tx = NULL; DMA_HandleTypeDef* hdma_tx = NULL;
DMA_HandleTypeDef* hdma_rx = NULL; DMA_HandleTypeDef* hdma_rx = NULL;
spi::getDmaHandles(&hdma_tx, &hdma_rx); spi::getDmaHandles(&hdma_tx, &hdma_rx);
if(hdma_tx == NULL || hdma_rx == NULL) { if (hdma_tx == NULL || hdma_rx == NULL) {
printf("HAL_SPI_MspInit: Invalid DMA handles. Make sure to call setDmaHandles!\n"); printf("HAL_SPI_MspInit: Invalid DMA handles. Make sure to call setDmaHandles!\n");
} } else {
else { // Disable the DMA
// Disable the DMA /* De-Initialize the DMA associated to transmission process */
/* De-Initialize the DMA associated to transmission process */ HAL_DMA_DeInit(hdma_tx);
HAL_DMA_DeInit(hdma_tx); /* De-Initialize the DMA associated to reception process */
/* De-Initialize the DMA associated to reception process */ HAL_DMA_DeInit(hdma_rx);
HAL_DMA_DeInit(hdma_rx); }
}
// Disable the NVIC for DMA
HAL_NVIC_DisableIRQ(cfg->txDmaIrqNumber);
HAL_NVIC_DisableIRQ(cfg->rxDmaIrqNumber);
// Disable the NVIC for DMA
HAL_NVIC_DisableIRQ(cfg->txDmaIrqNumber);
HAL_NVIC_DisableIRQ(cfg->rxDmaIrqNumber);
} }
void spi::halMspInitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) { void spi::halMspInitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = dynamic_cast<MspPollingConfigStruct*>(cfgBase); auto cfg = dynamic_cast<MspPollingConfigStruct*>(cfgBase);
GPIO_InitTypeDef GPIO_InitStruct = {}; GPIO_InitTypeDef GPIO_InitStruct = {};
/*##-1- Enable peripherals and GPIO Clocks #################################*/ /*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */ /* Enable GPIO TX/RX clock */
cfg->setupCb(); cfg->setupCb();
/*##-2- Configure peripheral GPIO ##########################################*/ /*##-2- Configure peripheral GPIO ##########################################*/
/* SPI SCK GPIO pin configuration */ /* SPI SCK GPIO pin configuration */
GPIO_InitStruct.Pin = cfg->sck.pin; GPIO_InitStruct.Pin = cfg->sck.pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = cfg->sck.altFnc; GPIO_InitStruct.Alternate = cfg->sck.altFnc;
HAL_GPIO_Init(cfg->sck.port, &GPIO_InitStruct); HAL_GPIO_Init(cfg->sck.port, &GPIO_InitStruct);
/* SPI MISO GPIO pin configuration */ /* SPI MISO GPIO pin configuration */
GPIO_InitStruct.Pin = cfg->miso.pin; GPIO_InitStruct.Pin = cfg->miso.pin;
GPIO_InitStruct.Alternate = cfg->miso.altFnc; GPIO_InitStruct.Alternate = cfg->miso.altFnc;
HAL_GPIO_Init(cfg->miso.port, &GPIO_InitStruct); HAL_GPIO_Init(cfg->miso.port, &GPIO_InitStruct);
/* SPI MOSI GPIO pin configuration */ /* SPI MOSI GPIO pin configuration */
GPIO_InitStruct.Pin = cfg->mosi.pin; GPIO_InitStruct.Pin = cfg->mosi.pin;
GPIO_InitStruct.Alternate = cfg->mosi.altFnc; GPIO_InitStruct.Alternate = cfg->mosi.altFnc;
HAL_GPIO_Init(cfg->mosi.port, &GPIO_InitStruct); HAL_GPIO_Init(cfg->mosi.port, &GPIO_InitStruct);
} }
void spi::halMspDeinitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) { void spi::halMspDeinitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = reinterpret_cast<MspPollingConfigStruct*>(cfgBase); auto cfg = reinterpret_cast<MspPollingConfigStruct*>(cfgBase);
// Reset peripherals // Reset peripherals
cfg->cleanupCb(); cfg->cleanupCb();
// Disable peripherals and GPIO Clocks // Disable peripherals and GPIO Clocks
/* Configure SPI SCK as alternate function */ /* Configure SPI SCK as alternate function */
HAL_GPIO_DeInit(cfg->sck.port, cfg->sck.pin); HAL_GPIO_DeInit(cfg->sck.port, cfg->sck.pin);
/* Configure SPI MISO as alternate function */ /* Configure SPI MISO as alternate function */
HAL_GPIO_DeInit(cfg->miso.port, cfg->miso.pin); HAL_GPIO_DeInit(cfg->miso.port, cfg->miso.pin);
/* Configure SPI MOSI as alternate function */ /* Configure SPI MOSI as alternate function */
HAL_GPIO_DeInit(cfg->mosi.port, cfg->mosi.pin); HAL_GPIO_DeInit(cfg->mosi.port, cfg->mosi.pin);
} }
void spi::halMspInitInterrupt(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) { void spi::halMspInitInterrupt(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = dynamic_cast<MspIrqConfigStruct*>(cfgBase); auto cfg = dynamic_cast<MspIrqConfigStruct*>(cfgBase);
if(cfg == nullptr or hspi == nullptr) { if (cfg == nullptr or hspi == nullptr) {
return; return;
} }
spi::halMspInitPolling(hspi, cfg); spi::halMspInitPolling(hspi, cfg);
// Configure the NVIC for SPI // Configure the NVIC for SPI
spi::assignSpiUserHandler(cfg->spiBus, cfg->spiIrqHandler, cfg->spiUserArgs); spi::assignSpiUserHandler(cfg->spiBus, cfg->spiIrqHandler, cfg->spiUserArgs);
HAL_NVIC_SetPriority(cfg->spiIrqNumber, cfg->preEmptPriority, cfg->subpriority); HAL_NVIC_SetPriority(cfg->spiIrqNumber, cfg->preEmptPriority, cfg->subpriority);
HAL_NVIC_EnableIRQ(cfg->spiIrqNumber); HAL_NVIC_EnableIRQ(cfg->spiIrqNumber);
} }
void spi::halMspDeinitInterrupt(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) { void spi::halMspDeinitInterrupt(SPI_HandleTypeDef* hspi, MspCfgBase* cfgBase) {
auto cfg = dynamic_cast<MspIrqConfigStruct*>(cfgBase); auto cfg = dynamic_cast<MspIrqConfigStruct*>(cfgBase);
spi::halMspDeinitPolling(hspi, cfg); spi::halMspDeinitPolling(hspi, cfg);
// Disable the NVIC for SPI // Disable the NVIC for SPI
HAL_NVIC_DisableIRQ(cfg->spiIrqNumber); HAL_NVIC_DisableIRQ(cfg->spiIrqNumber);
} }
void spi::getMspInitFunction(msp_func_t* init_func, MspCfgBase** args) { void spi::getMspInitFunction(msp_func_t* init_func, MspCfgBase** args) {
if(init_func != NULL && args != NULL) { if (init_func != NULL && args != NULL) {
*init_func = mspInitFunc; *init_func = mspInitFunc;
*args = mspInitArgs; *args = mspInitArgs;
} }
} }
void spi::getMspDeinitFunction(msp_func_t* deinit_func, MspCfgBase** args) { void spi::getMspDeinitFunction(msp_func_t* deinit_func, MspCfgBase** args) {
if(deinit_func != NULL && args != NULL) { if (deinit_func != NULL && args != NULL) {
*deinit_func = mspDeinitFunc; *deinit_func = mspDeinitFunc;
*args = mspDeinitArgs; *args = mspDeinitArgs;
} }
} }
void spi::setSpiDmaMspFunctions(MspDmaConfigStruct* cfg, void spi::setSpiDmaMspFunctions(MspDmaConfigStruct* cfg, msp_func_t initFunc,
msp_func_t initFunc, msp_func_t deinitFunc) { msp_func_t deinitFunc) {
mspInitFunc = initFunc; mspInitFunc = initFunc;
mspDeinitFunc = deinitFunc; mspDeinitFunc = deinitFunc;
mspInitArgs = cfg; mspInitArgs = cfg;
mspDeinitArgs = cfg; mspDeinitArgs = cfg;
} }
void spi::setSpiIrqMspFunctions(MspIrqConfigStruct *cfg, msp_func_t initFunc, void spi::setSpiIrqMspFunctions(MspIrqConfigStruct* cfg, msp_func_t initFunc,
msp_func_t deinitFunc) { msp_func_t deinitFunc) {
mspInitFunc = initFunc; mspInitFunc = initFunc;
mspDeinitFunc = deinitFunc; mspDeinitFunc = deinitFunc;
mspInitArgs = cfg; mspInitArgs = cfg;
mspDeinitArgs = cfg; mspDeinitArgs = cfg;
} }
void spi::setSpiPollingMspFunctions(MspPollingConfigStruct *cfg, msp_func_t initFunc, void spi::setSpiPollingMspFunctions(MspPollingConfigStruct* cfg, msp_func_t initFunc,
msp_func_t deinitFunc) { msp_func_t deinitFunc) {
mspInitFunc = initFunc; mspInitFunc = initFunc;
mspDeinitFunc = deinitFunc; mspDeinitFunc = deinitFunc;
mspInitArgs = cfg; mspInitArgs = cfg;
mspDeinitArgs = cfg; mspDeinitArgs = cfg;
} }
/** /**
@ -222,13 +219,12 @@ void spi::setSpiPollingMspFunctions(MspPollingConfigStruct *cfg, msp_func_t init
* @param hspi: SPI handle pointer * @param hspi: SPI handle pointer
* @retval None * @retval None
*/ */
extern "C" void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) { extern "C" void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
if(mspInitFunc != NULL) { if (mspInitFunc != NULL) {
mspInitFunc(hspi, mspInitArgs); mspInitFunc(hspi, mspInitArgs);
} } else {
else { printf("HAL_SPI_MspInit: Please call set_msp_functions to assign SPI MSP functions\n");
printf("HAL_SPI_MspInit: Please call set_msp_functions to assign SPI MSP functions\n"); }
}
} }
/** /**
@ -239,15 +235,14 @@ extern "C" void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) {
* @param hspi: SPI handle pointer * @param hspi: SPI handle pointer
* @retval None * @retval None
*/ */
extern "C" void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) { extern "C" void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) {
if(mspDeinitFunc != NULL) { if (mspDeinitFunc != NULL) {
mspDeinitFunc(hspi, mspDeinitArgs); mspDeinitFunc(hspi, mspDeinitArgs);
} } else {
else { printf("HAL_SPI_MspDeInit: Please call set_msp_functions to assign SPI MSP functions\n");
printf("HAL_SPI_MspDeInit: Please call set_msp_functions to assign SPI MSP functions\n"); }
}
} }
void spi::mspErrorHandler(const char* const function, const char *const message) { void spi::mspErrorHandler(const char* const function, const char* const message) {
printf("%s failure: %s\n", function, message); printf("%s failure: %s\n", function, message);
} }

View File

@ -1,19 +1,18 @@
#ifndef FSFW_HAL_STM32H7_SPI_MSPINIT_H_ #ifndef FSFW_HAL_STM32H7_SPI_MSPINIT_H_
#define FSFW_HAL_STM32H7_SPI_MSPINIT_H_ #define FSFW_HAL_STM32H7_SPI_MSPINIT_H_
#include "spiDefinitions.h" #include <cstdint>
#include "../definitions.h" #include "../definitions.h"
#include "../dma.h" #include "../dma.h"
#include "spiDefinitions.h"
#include "stm32h7xx_hal_spi.h" #include "stm32h7xx_hal_spi.h"
#include <cstdint>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
using mspCb = void (*) (void); using mspCb = void (*)(void);
/** /**
* @brief This file provides MSP implementation for DMA, IRQ and Polling mode for the * @brief This file provides MSP implementation for DMA, IRQ and Polling mode for the
@ -22,74 +21,72 @@ using mspCb = void (*) (void);
namespace spi { namespace spi {
struct MspCfgBase { struct MspCfgBase {
MspCfgBase(); MspCfgBase();
MspCfgBase(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso, MspCfgBase(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr): mspCb cleanupCb = nullptr, mspCb setupCb = nullptr)
sck(sck), mosi(mosi), miso(miso), cleanupCb(cleanupCb), : sck(sck), mosi(mosi), miso(miso), cleanupCb(cleanupCb), setupCb(setupCb) {}
setupCb(setupCb) {}
virtual ~MspCfgBase() = default; virtual ~MspCfgBase() = default;
stm32h7::GpioCfg sck; stm32h7::GpioCfg sck;
stm32h7::GpioCfg mosi; stm32h7::GpioCfg mosi;
stm32h7::GpioCfg miso; stm32h7::GpioCfg miso;
mspCb cleanupCb = nullptr; mspCb cleanupCb = nullptr;
mspCb setupCb = nullptr; mspCb setupCb = nullptr;
}; };
struct MspPollingConfigStruct: public MspCfgBase { struct MspPollingConfigStruct : public MspCfgBase {
MspPollingConfigStruct(): MspCfgBase() {}; MspPollingConfigStruct() : MspCfgBase(){};
MspPollingConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso, MspPollingConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr): mspCb cleanupCb = nullptr, mspCb setupCb = nullptr)
MspCfgBase(sck, mosi, miso, cleanupCb, setupCb) {} : MspCfgBase(sck, mosi, miso, cleanupCb, setupCb) {}
}; };
/* A valid instance of this struct must be passed to the MSP initialization function as a void* /* A valid instance of this struct must be passed to the MSP initialization function as a void*
argument */ argument */
struct MspIrqConfigStruct: public MspPollingConfigStruct { struct MspIrqConfigStruct : public MspPollingConfigStruct {
MspIrqConfigStruct(): MspPollingConfigStruct() {}; MspIrqConfigStruct() : MspPollingConfigStruct(){};
MspIrqConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso, MspIrqConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr): mspCb cleanupCb = nullptr, mspCb setupCb = nullptr)
MspPollingConfigStruct(sck, mosi, miso, cleanupCb, setupCb) {} : MspPollingConfigStruct(sck, mosi, miso, cleanupCb, setupCb) {}
SpiBus spiBus = SpiBus::SPI_1; SpiBus spiBus = SpiBus::SPI_1;
user_handler_t spiIrqHandler = nullptr; user_handler_t spiIrqHandler = nullptr;
user_args_t spiUserArgs = nullptr; user_args_t spiUserArgs = nullptr;
IRQn_Type spiIrqNumber = SPI1_IRQn; IRQn_Type spiIrqNumber = SPI1_IRQn;
// Priorities for NVIC // Priorities for NVIC
// Pre-Empt priority ranging from 0 to 15. If FreeRTOS calls are used, only 5-15 are allowed // Pre-Empt priority ranging from 0 to 15. If FreeRTOS calls are used, only 5-15 are allowed
IrqPriorities preEmptPriority = IrqPriorities::LOWEST; IrqPriorities preEmptPriority = IrqPriorities::LOWEST;
IrqPriorities subpriority = IrqPriorities::LOWEST; IrqPriorities subpriority = IrqPriorities::LOWEST;
}; };
/* A valid instance of this struct must be passed to the MSP initialization function as a void* /* A valid instance of this struct must be passed to the MSP initialization function as a void*
argument */ argument */
struct MspDmaConfigStruct: public MspIrqConfigStruct { struct MspDmaConfigStruct : public MspIrqConfigStruct {
MspDmaConfigStruct(): MspIrqConfigStruct() {}; MspDmaConfigStruct() : MspIrqConfigStruct(){};
MspDmaConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso, MspDmaConfigStruct(stm32h7::GpioCfg sck, stm32h7::GpioCfg mosi, stm32h7::GpioCfg miso,
mspCb cleanupCb = nullptr, mspCb setupCb = nullptr): mspCb cleanupCb = nullptr, mspCb setupCb = nullptr)
MspIrqConfigStruct(sck, mosi, miso, cleanupCb, setupCb) {} : MspIrqConfigStruct(sck, mosi, miso, cleanupCb, setupCb) {}
void (* dmaClkEnableWrapper) (void) = nullptr; void (*dmaClkEnableWrapper)(void) = nullptr;
dma::DMAIndexes txDmaIndex = dma::DMAIndexes::DMA_1; dma::DMAIndexes txDmaIndex = dma::DMAIndexes::DMA_1;
dma::DMAIndexes rxDmaIndex = dma::DMAIndexes::DMA_1; dma::DMAIndexes rxDmaIndex = dma::DMAIndexes::DMA_1;
dma::DMAStreams txDmaStream = dma::DMAStreams::STREAM_0; dma::DMAStreams txDmaStream = dma::DMAStreams::STREAM_0;
dma::DMAStreams rxDmaStream = dma::DMAStreams::STREAM_0; dma::DMAStreams rxDmaStream = dma::DMAStreams::STREAM_0;
IRQn_Type txDmaIrqNumber = DMA1_Stream0_IRQn; IRQn_Type txDmaIrqNumber = DMA1_Stream0_IRQn;
IRQn_Type rxDmaIrqNumber = DMA1_Stream1_IRQn; IRQn_Type rxDmaIrqNumber = DMA1_Stream1_IRQn;
// Priorities for NVIC // Priorities for NVIC
IrqPriorities txPreEmptPriority = IrqPriorities::LOWEST; IrqPriorities txPreEmptPriority = IrqPriorities::LOWEST;
IrqPriorities rxPreEmptPriority = IrqPriorities::LOWEST; IrqPriorities rxPreEmptPriority = IrqPriorities::LOWEST;
IrqPriorities txSubpriority = IrqPriorities::LOWEST; IrqPriorities txSubpriority = IrqPriorities::LOWEST;
IrqPriorities rxSubpriority = IrqPriorities::LOWEST; IrqPriorities rxSubpriority = IrqPriorities::LOWEST;
}; };
using msp_func_t = void (*) (SPI_HandleTypeDef* hspi, MspCfgBase* cfg); using msp_func_t = void (*)(SPI_HandleTypeDef* hspi, MspCfgBase* cfg);
void getMspInitFunction(msp_func_t* init_func, MspCfgBase** args);
void getMspInitFunction(msp_func_t* init_func, MspCfgBase **args); void getMspDeinitFunction(msp_func_t* deinit_func, MspCfgBase** args);
void getMspDeinitFunction(msp_func_t* deinit_func, MspCfgBase **args);
void halMspInitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfg); void halMspInitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfg);
void halMspDeinitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfg); void halMspDeinitDma(SPI_HandleTypeDef* hspi, MspCfgBase* cfg);
@ -107,23 +104,17 @@ void halMspDeinitPolling(SPI_HandleTypeDef* hspi, MspCfgBase* cfg);
* @param deinit_func * @param deinit_func
* @param deinit_args * @param deinit_args
*/ */
void setSpiDmaMspFunctions(MspDmaConfigStruct* cfg, void setSpiDmaMspFunctions(MspDmaConfigStruct* cfg, msp_func_t initFunc = &spi::halMspInitDma,
msp_func_t initFunc = &spi::halMspInitDma, msp_func_t deinitFunc = &spi::halMspDeinitDma);
msp_func_t deinitFunc= &spi::halMspDeinitDma void setSpiIrqMspFunctions(MspIrqConfigStruct* cfg, msp_func_t initFunc = &spi::halMspInitInterrupt,
); msp_func_t deinitFunc = &spi::halMspDeinitInterrupt);
void setSpiIrqMspFunctions(MspIrqConfigStruct* cfg,
msp_func_t initFunc = &spi::halMspInitInterrupt,
msp_func_t deinitFunc= &spi::halMspDeinitInterrupt
);
void setSpiPollingMspFunctions(MspPollingConfigStruct* cfg, void setSpiPollingMspFunctions(MspPollingConfigStruct* cfg,
msp_func_t initFunc = &spi::halMspInitPolling, msp_func_t initFunc = &spi::halMspInitPolling,
msp_func_t deinitFunc= &spi::halMspDeinitPolling msp_func_t deinitFunc = &spi::halMspDeinitPolling);
);
void mspErrorHandler(const char* const function, const char *const message); void mspErrorHandler(const char* const function, const char* const message);
}
} // namespace spi
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,8 +1,9 @@
#include "fsfw_hal/stm32h7/spi/spiCore.h" #include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
#include <cstdio> #include <cstdio>
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
SPI_HandleTypeDef* spiHandle = nullptr; SPI_HandleTypeDef* spiHandle = nullptr;
DMA_HandleTypeDef* hdmaTx = nullptr; DMA_HandleTypeDef* hdmaTx = nullptr;
DMA_HandleTypeDef* hdmaRx = nullptr; DMA_HandleTypeDef* hdmaRx = nullptr;
@ -17,117 +18,109 @@ spi_transfer_cb_t errorCb = nullptr;
void* errorArgs = nullptr; void* errorArgs = nullptr;
void mapIndexAndStream(DMA_HandleTypeDef* handle, dma::DMAType dmaType, dma::DMAIndexes dmaIdx, void mapIndexAndStream(DMA_HandleTypeDef* handle, dma::DMAType dmaType, dma::DMAIndexes dmaIdx,
dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber); dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber);
void mapSpiBus(DMA_HandleTypeDef *handle, dma::DMAType dmaType, spi::SpiBus spiBus); void mapSpiBus(DMA_HandleTypeDef* handle, dma::DMAType dmaType, spi::SpiBus spiBus);
void spi::configureDmaHandle(DMA_HandleTypeDef *handle, spi::SpiBus spiBus, dma::DMAType dmaType, void spi::configureDmaHandle(DMA_HandleTypeDef* handle, spi::SpiBus spiBus, dma::DMAType dmaType,
dma::DMAIndexes dmaIdx, dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber, dma::DMAIndexes dmaIdx, dma::DMAStreams dmaStream,
uint32_t dmaMode, uint32_t dmaPriority) { IRQn_Type* dmaIrqNumber, uint32_t dmaMode, uint32_t dmaPriority) {
using namespace dma; using namespace dma;
mapIndexAndStream(handle, dmaType, dmaIdx, dmaStream, dmaIrqNumber); mapIndexAndStream(handle, dmaType, dmaIdx, dmaStream, dmaIrqNumber);
mapSpiBus(handle, dmaType, spiBus); mapSpiBus(handle, dmaType, spiBus);
if(dmaType == DMAType::TX) { if (dmaType == DMAType::TX) {
handle->Init.Direction = DMA_MEMORY_TO_PERIPH; handle->Init.Direction = DMA_MEMORY_TO_PERIPH;
} } else {
else { handle->Init.Direction = DMA_PERIPH_TO_MEMORY;
handle->Init.Direction = DMA_PERIPH_TO_MEMORY; }
}
handle->Init.Priority = dmaPriority; handle->Init.Priority = dmaPriority;
handle->Init.Mode = dmaMode; handle->Init.Mode = dmaMode;
// Standard settings for the rest for now // Standard settings for the rest for now
handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE; handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE;
handle->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; handle->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
handle->Init.MemBurst = DMA_MBURST_INC4; handle->Init.MemBurst = DMA_MBURST_INC4;
handle->Init.PeriphBurst = DMA_PBURST_INC4; handle->Init.PeriphBurst = DMA_PBURST_INC4;
handle->Init.PeriphInc = DMA_PINC_DISABLE; handle->Init.PeriphInc = DMA_PINC_DISABLE;
handle->Init.MemInc = DMA_MINC_ENABLE; handle->Init.MemInc = DMA_MINC_ENABLE;
handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
} }
void spi::setDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle) { void spi::setDmaHandles(DMA_HandleTypeDef* txHandle, DMA_HandleTypeDef* rxHandle) {
hdmaTx = txHandle; hdmaTx = txHandle;
hdmaRx = rxHandle; hdmaRx = rxHandle;
} }
void spi::getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle) { void spi::getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle) {
*txHandle = hdmaTx; *txHandle = hdmaTx;
*rxHandle = hdmaRx; *rxHandle = hdmaRx;
} }
void spi::setSpiHandle(SPI_HandleTypeDef *spiHandle_) { void spi::setSpiHandle(SPI_HandleTypeDef* spiHandle_) {
if(spiHandle_ == NULL) { if (spiHandle_ == NULL) {
return; return;
} }
spiHandle = spiHandle_; spiHandle = spiHandle_;
} }
void spi::assignTransferRxTxCompleteCallback(spi_transfer_cb_t callback, void *userArgs) { void spi::assignTransferRxTxCompleteCallback(spi_transfer_cb_t callback, void* userArgs) {
rxTxCb = callback; rxTxCb = callback;
rxTxArgs = userArgs; rxTxArgs = userArgs;
} }
void spi::assignTransferRxCompleteCallback(spi_transfer_cb_t callback, void *userArgs) { void spi::assignTransferRxCompleteCallback(spi_transfer_cb_t callback, void* userArgs) {
rxCb = callback; rxCb = callback;
rxArgs = userArgs; rxArgs = userArgs;
} }
void spi::assignTransferTxCompleteCallback(spi_transfer_cb_t callback, void *userArgs) { void spi::assignTransferTxCompleteCallback(spi_transfer_cb_t callback, void* userArgs) {
txCb = callback; txCb = callback;
txArgs = userArgs; txArgs = userArgs;
} }
void spi::assignTransferErrorCallback(spi_transfer_cb_t callback, void *userArgs) { void spi::assignTransferErrorCallback(spi_transfer_cb_t callback, void* userArgs) {
errorCb = callback; errorCb = callback;
errorArgs = userArgs; errorArgs = userArgs;
} }
SPI_HandleTypeDef* spi::getSpiHandle() { SPI_HandleTypeDef* spi::getSpiHandle() { return spiHandle; }
return spiHandle;
}
/** /**
* @brief TxRx Transfer completed callback. * @brief TxRx Transfer completed callback.
* @param hspi: SPI handle * @param hspi: SPI handle
*/ */
extern "C" void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { extern "C" void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef* hspi) {
if(rxTxCb != NULL) { if (rxTxCb != NULL) {
rxTxCb(hspi, rxTxArgs); rxTxCb(hspi, rxTxArgs);
} } else {
else { printf("HAL_SPI_TxRxCpltCallback: No user callback specified\n");
printf("HAL_SPI_TxRxCpltCallback: No user callback specified\n"); }
}
} }
/** /**
* @brief TxRx Transfer completed callback. * @brief TxRx Transfer completed callback.
* @param hspi: SPI handle * @param hspi: SPI handle
*/ */
extern "C" void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) { extern "C" void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef* hspi) {
if(txCb != NULL) { if (txCb != NULL) {
txCb(hspi, txArgs); txCb(hspi, txArgs);
} } else {
else { printf("HAL_SPI_TxCpltCallback: No user callback specified\n");
printf("HAL_SPI_TxCpltCallback: No user callback specified\n"); }
}
} }
/** /**
* @brief TxRx Transfer completed callback. * @brief TxRx Transfer completed callback.
* @param hspi: SPI handle * @param hspi: SPI handle
*/ */
extern "C" void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) { extern "C" void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef* hspi) {
if(rxCb != nullptr) { if (rxCb != nullptr) {
rxCb(hspi, rxArgs); rxCb(hspi, rxArgs);
} } else {
else { printf("HAL_SPI_RxCpltCallback: No user callback specified\n");
printf("HAL_SPI_RxCpltCallback: No user callback specified\n"); }
}
} }
/** /**
@ -137,205 +130,200 @@ extern "C" void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
* add your own implementation. * add your own implementation.
* @retval None * @retval None
*/ */
extern "C" void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) { extern "C" void HAL_SPI_ErrorCallback(SPI_HandleTypeDef* hspi) {
if(errorCb != nullptr) { if (errorCb != nullptr) {
errorCb(hspi, rxArgs); errorCb(hspi, rxArgs);
} } else {
else { printf("HAL_SPI_ErrorCallback: No user callback specified\n");
printf("HAL_SPI_ErrorCallback: No user callback specified\n"); }
}
} }
void mapIndexAndStream(DMA_HandleTypeDef* handle, dma::DMAType dmaType, dma::DMAIndexes dmaIdx, void mapIndexAndStream(DMA_HandleTypeDef* handle, dma::DMAType dmaType, dma::DMAIndexes dmaIdx,
dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber) { dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber) {
using namespace dma; using namespace dma;
if(dmaIdx == DMAIndexes::DMA_1) { if (dmaIdx == DMAIndexes::DMA_1) {
#ifdef DMA1 #ifdef DMA1
switch(dmaStream) { switch (dmaStream) {
case(DMAStreams::STREAM_0): { case (DMAStreams::STREAM_0): {
#ifdef DMA1_Stream0 #ifdef DMA1_Stream0
handle->Instance = DMA1_Stream0; handle->Instance = DMA1_Stream0;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream0_IRQn; *dmaIrqNumber = DMA1_Stream0_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_1): { #endif
break;
}
case (DMAStreams::STREAM_1): {
#ifdef DMA1_Stream1 #ifdef DMA1_Stream1
handle->Instance = DMA1_Stream1; handle->Instance = DMA1_Stream1;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream1_IRQn; *dmaIrqNumber = DMA1_Stream1_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_2): { #endif
break;
}
case (DMAStreams::STREAM_2): {
#ifdef DMA1_Stream2 #ifdef DMA1_Stream2
handle->Instance = DMA1_Stream2; handle->Instance = DMA1_Stream2;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream2_IRQn; *dmaIrqNumber = DMA1_Stream2_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_3): { #endif
break;
}
case (DMAStreams::STREAM_3): {
#ifdef DMA1_Stream3 #ifdef DMA1_Stream3
handle->Instance = DMA1_Stream3; handle->Instance = DMA1_Stream3;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream3_IRQn; *dmaIrqNumber = DMA1_Stream3_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_4): { #endif
break;
}
case (DMAStreams::STREAM_4): {
#ifdef DMA1_Stream4 #ifdef DMA1_Stream4
handle->Instance = DMA1_Stream4; handle->Instance = DMA1_Stream4;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream4_IRQn; *dmaIrqNumber = DMA1_Stream4_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_5): { #endif
break;
}
case (DMAStreams::STREAM_5): {
#ifdef DMA1_Stream5 #ifdef DMA1_Stream5
handle->Instance = DMA1_Stream5; handle->Instance = DMA1_Stream5;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream5_IRQn; *dmaIrqNumber = DMA1_Stream5_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_6): { #endif
break;
}
case (DMAStreams::STREAM_6): {
#ifdef DMA1_Stream6 #ifdef DMA1_Stream6
handle->Instance = DMA1_Stream6; handle->Instance = DMA1_Stream6;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream6_IRQn; *dmaIrqNumber = DMA1_Stream6_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_7): { #endif
break;
}
case (DMAStreams::STREAM_7): {
#ifdef DMA1_Stream7 #ifdef DMA1_Stream7
handle->Instance = DMA1_Stream7; handle->Instance = DMA1_Stream7;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA1_Stream7_IRQn; *dmaIrqNumber = DMA1_Stream7_IRQn;
} }
#endif #endif
break; break;
} }
} }
if(dmaType == DMAType::TX) { if (dmaType == DMAType::TX) {
handle->Init.Request = DMA_REQUEST_SPI1_TX; handle->Init.Request = DMA_REQUEST_SPI1_TX;
} } else {
else { handle->Init.Request = DMA_REQUEST_SPI1_RX;
handle->Init.Request = DMA_REQUEST_SPI1_RX; }
}
#endif /* DMA1 */ #endif /* DMA1 */
} }
if(dmaIdx == DMAIndexes::DMA_2) { if (dmaIdx == DMAIndexes::DMA_2) {
#ifdef DMA2 #ifdef DMA2
switch(dmaStream) { switch (dmaStream) {
case(DMAStreams::STREAM_0): { case (DMAStreams::STREAM_0): {
#ifdef DMA2_Stream0 #ifdef DMA2_Stream0
handle->Instance = DMA2_Stream0; handle->Instance = DMA2_Stream0;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream0_IRQn; *dmaIrqNumber = DMA2_Stream0_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_1): { #endif
break;
}
case (DMAStreams::STREAM_1): {
#ifdef DMA2_Stream1 #ifdef DMA2_Stream1
handle->Instance = DMA2_Stream1; handle->Instance = DMA2_Stream1;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream1_IRQn; *dmaIrqNumber = DMA2_Stream1_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_2): { #endif
break;
}
case (DMAStreams::STREAM_2): {
#ifdef DMA2_Stream2 #ifdef DMA2_Stream2
handle->Instance = DMA2_Stream2; handle->Instance = DMA2_Stream2;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream2_IRQn; *dmaIrqNumber = DMA2_Stream2_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_3): { #endif
break;
}
case (DMAStreams::STREAM_3): {
#ifdef DMA2_Stream3 #ifdef DMA2_Stream3
handle->Instance = DMA2_Stream3; handle->Instance = DMA2_Stream3;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream3_IRQn; *dmaIrqNumber = DMA2_Stream3_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_4): { #endif
break;
}
case (DMAStreams::STREAM_4): {
#ifdef DMA2_Stream4 #ifdef DMA2_Stream4
handle->Instance = DMA2_Stream4; handle->Instance = DMA2_Stream4;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream4_IRQn; *dmaIrqNumber = DMA2_Stream4_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_5): { #endif
break;
}
case (DMAStreams::STREAM_5): {
#ifdef DMA2_Stream5 #ifdef DMA2_Stream5
handle->Instance = DMA2_Stream5; handle->Instance = DMA2_Stream5;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream5_IRQn; *dmaIrqNumber = DMA2_Stream5_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_6): { #endif
break;
}
case (DMAStreams::STREAM_6): {
#ifdef DMA2_Stream6 #ifdef DMA2_Stream6
handle->Instance = DMA2_Stream6; handle->Instance = DMA2_Stream6;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream6_IRQn; *dmaIrqNumber = DMA2_Stream6_IRQn;
}
#endif
break;
} }
case(DMAStreams::STREAM_7): { #endif
break;
}
case (DMAStreams::STREAM_7): {
#ifdef DMA2_Stream7 #ifdef DMA2_Stream7
handle->Instance = DMA2_Stream7; handle->Instance = DMA2_Stream7;
if(dmaIrqNumber != nullptr) { if (dmaIrqNumber != nullptr) {
*dmaIrqNumber = DMA2_Stream7_IRQn; *dmaIrqNumber = DMA2_Stream7_IRQn;
} }
#endif #endif
break; break;
} }
}
#endif /* DMA2 */
} }
#endif /* DMA2 */
}
} }
void mapSpiBus(DMA_HandleTypeDef *handle, dma::DMAType dmaType, spi::SpiBus spiBus) { void mapSpiBus(DMA_HandleTypeDef* handle, dma::DMAType dmaType, spi::SpiBus spiBus) {
if(dmaType == dma::DMAType::TX) { if (dmaType == dma::DMAType::TX) {
if(spiBus == spi::SpiBus::SPI_1) { if (spiBus == spi::SpiBus::SPI_1) {
#ifdef DMA_REQUEST_SPI1_TX #ifdef DMA_REQUEST_SPI1_TX
handle->Init.Request = DMA_REQUEST_SPI1_TX; handle->Init.Request = DMA_REQUEST_SPI1_TX;
#endif #endif
} } else if (spiBus == spi::SpiBus::SPI_2) {
else if(spiBus == spi::SpiBus::SPI_2) {
#ifdef DMA_REQUEST_SPI2_TX #ifdef DMA_REQUEST_SPI2_TX
handle->Init.Request = DMA_REQUEST_SPI2_TX; handle->Init.Request = DMA_REQUEST_SPI2_TX;
#endif #endif
}
} }
else { } else {
if(spiBus == spi::SpiBus::SPI_1) { if (spiBus == spi::SpiBus::SPI_1) {
#ifdef DMA_REQUEST_SPI1_RX #ifdef DMA_REQUEST_SPI1_RX
handle->Init.Request = DMA_REQUEST_SPI1_RX; handle->Init.Request = DMA_REQUEST_SPI1_RX;
#endif #endif
} } else if (spiBus == spi::SpiBus::SPI_2) {
else if(spiBus == spi::SpiBus::SPI_2) {
#ifdef DMA_REQUEST_SPI2_RX #ifdef DMA_REQUEST_SPI2_RX
handle->Init.Request = DMA_REQUEST_SPI2_RX; handle->Init.Request = DMA_REQUEST_SPI2_RX;
#endif #endif
}
} }
}
} }

View File

@ -3,7 +3,6 @@
#include "fsfw_hal/stm32h7/dma.h" #include "fsfw_hal/stm32h7/dma.h"
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h" #include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
#include "stm32h7xx_hal.h" #include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_dma.h" #include "stm32h7xx_hal_dma.h"
@ -11,14 +10,13 @@
extern "C" { extern "C" {
#endif #endif
using spi_transfer_cb_t = void (*) (SPI_HandleTypeDef *hspi, void* userArgs); using spi_transfer_cb_t = void (*)(SPI_HandleTypeDef* hspi, void* userArgs);
namespace spi { namespace spi {
void configureDmaHandle(DMA_HandleTypeDef* handle, spi::SpiBus spiBus, void configureDmaHandle(DMA_HandleTypeDef* handle, spi::SpiBus spiBus, dma::DMAType dmaType,
dma::DMAType dmaType, dma::DMAIndexes dmaIdx, dma::DMAIndexes dmaIdx, dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber,
dma::DMAStreams dmaStream, IRQn_Type* dmaIrqNumber, uint32_t dmaMode = DMA_NORMAL, uint32_t dmaMode = DMA_NORMAL, uint32_t dmaPriority = DMA_PRIORITY_LOW);
uint32_t dmaPriority = DMA_PRIORITY_LOW);
/** /**
* Assign DMA handles. Required to use DMA for SPI transfers. * Assign DMA handles. Required to use DMA for SPI transfers.
@ -32,7 +30,7 @@ void getDmaHandles(DMA_HandleTypeDef** txHandle, DMA_HandleTypeDef** rxHandle);
* Assign SPI handle. Needs to be done before using the SPI * Assign SPI handle. Needs to be done before using the SPI
* @param spiHandle * @param spiHandle
*/ */
void setSpiHandle(SPI_HandleTypeDef *spiHandle); void setSpiHandle(SPI_HandleTypeDef* spiHandle);
void assignTransferRxTxCompleteCallback(spi_transfer_cb_t callback, void* userArgs); void assignTransferRxTxCompleteCallback(spi_transfer_cb_t callback, void* userArgs);
void assignTransferRxCompleteCallback(spi_transfer_cb_t callback, void* userArgs); void assignTransferRxCompleteCallback(spi_transfer_cb_t callback, void* userArgs);
@ -45,7 +43,7 @@ void assignTransferErrorCallback(spi_transfer_cb_t callback, void* userArgs);
*/ */
SPI_HandleTypeDef* getSpiHandle(); SPI_HandleTypeDef* getSpiHandle();
} } // namespace spi
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,52 +1,46 @@
#include "fsfw_hal/stm32h7/spi/spiDefinitions.h" #include "fsfw_hal/stm32h7/spi/spiDefinitions.h"
void spi::assignSpiMode(SpiModes spiMode, SPI_HandleTypeDef& spiHandle) { void spi::assignSpiMode(SpiModes spiMode, SPI_HandleTypeDef& spiHandle) {
switch(spiMode) { switch (spiMode) {
case(SpiModes::MODE_0): { case (SpiModes::MODE_0): {
spiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; spiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
spiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; spiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
break; break;
} }
case(SpiModes::MODE_1): { case (SpiModes::MODE_1): {
spiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; spiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
spiHandle.Init.CLKPhase = SPI_PHASE_2EDGE; spiHandle.Init.CLKPhase = SPI_PHASE_2EDGE;
break; break;
} }
case(SpiModes::MODE_2): { case (SpiModes::MODE_2): {
spiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; spiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
spiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; spiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
break; break;
}
case(SpiModes::MODE_3): {
spiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
spiHandle.Init.CLKPhase = SPI_PHASE_2EDGE;
break;
} }
case (SpiModes::MODE_3): {
spiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
spiHandle.Init.CLKPhase = SPI_PHASE_2EDGE;
break;
} }
}
} }
uint32_t spi::getPrescaler(uint32_t clock_src_freq, uint32_t baudrate_mbps) { uint32_t spi::getPrescaler(uint32_t clock_src_freq, uint32_t baudrate_mbps) {
uint32_t divisor = 0; uint32_t divisor = 0;
uint32_t spi_clk = clock_src_freq; uint32_t spi_clk = clock_src_freq;
uint32_t presc = 0; uint32_t presc = 0;
static const uint32_t baudrate[] = { static const uint32_t baudrate[] = {
SPI_BAUDRATEPRESCALER_2, SPI_BAUDRATEPRESCALER_2, SPI_BAUDRATEPRESCALER_4, SPI_BAUDRATEPRESCALER_8,
SPI_BAUDRATEPRESCALER_4, SPI_BAUDRATEPRESCALER_16, SPI_BAUDRATEPRESCALER_32, SPI_BAUDRATEPRESCALER_64,
SPI_BAUDRATEPRESCALER_8, SPI_BAUDRATEPRESCALER_128, SPI_BAUDRATEPRESCALER_256,
SPI_BAUDRATEPRESCALER_16, };
SPI_BAUDRATEPRESCALER_32,
SPI_BAUDRATEPRESCALER_64,
SPI_BAUDRATEPRESCALER_128,
SPI_BAUDRATEPRESCALER_256,
};
while( spi_clk > baudrate_mbps) { while (spi_clk > baudrate_mbps) {
presc = baudrate[divisor]; presc = baudrate[divisor];
if (++divisor > 7) if (++divisor > 7) break;
break;
spi_clk = ( spi_clk >> 1); spi_clk = (spi_clk >> 1);
} }
return presc; return presc;
} }

View File

@ -2,37 +2,24 @@
#define FSFW_HAL_STM32H7_SPI_SPIDEFINITIONS_H_ #define FSFW_HAL_STM32H7_SPI_SPIDEFINITIONS_H_
#include "../../common/spi/spiCommon.h" #include "../../common/spi/spiCommon.h"
#include "fsfw/returnvalues/FwClassIds.h" #include "fsfw/returnvalues/FwClassIds.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "stm32h7xx_hal.h" #include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_spi.h" #include "stm32h7xx_hal_spi.h"
namespace spi { namespace spi {
static constexpr uint8_t HAL_SPI_ID = CLASS_ID::HAL_SPI; static constexpr uint8_t HAL_SPI_ID = CLASS_ID::HAL_SPI;
static constexpr ReturnValue_t HAL_TIMEOUT_RETVAL = HasReturnvaluesIF::makeReturnCode(HAL_SPI_ID, 0); static constexpr ReturnValue_t HAL_TIMEOUT_RETVAL =
HasReturnvaluesIF::makeReturnCode(HAL_SPI_ID, 0);
static constexpr ReturnValue_t HAL_BUSY_RETVAL = HasReturnvaluesIF::makeReturnCode(HAL_SPI_ID, 1); static constexpr ReturnValue_t HAL_BUSY_RETVAL = HasReturnvaluesIF::makeReturnCode(HAL_SPI_ID, 1);
static constexpr ReturnValue_t HAL_ERROR_RETVAL = HasReturnvaluesIF::makeReturnCode(HAL_SPI_ID, 2); static constexpr ReturnValue_t HAL_ERROR_RETVAL = HasReturnvaluesIF::makeReturnCode(HAL_SPI_ID, 2);
enum class TransferStates { enum class TransferStates { IDLE, WAIT, SUCCESS, FAILURE };
IDLE,
WAIT,
SUCCESS,
FAILURE
};
enum SpiBus { enum SpiBus { SPI_1, SPI_2 };
SPI_1,
SPI_2
};
enum TransferModes { enum TransferModes { POLLING, INTERRUPT, DMA };
POLLING,
INTERRUPT,
DMA
};
void assignSpiMode(SpiModes spiMode, SPI_HandleTypeDef& spiHandle); void assignSpiMode(SpiModes spiMode, SPI_HandleTypeDef& spiHandle);
@ -44,7 +31,6 @@ void assignSpiMode(SpiModes spiMode, SPI_HandleTypeDef& spiHandle);
*/ */
uint32_t getPrescaler(uint32_t clock_src_freq, uint32_t baudrate_mbps); uint32_t getPrescaler(uint32_t clock_src_freq, uint32_t baudrate_mbps);
} } // namespace spi
#endif /* FSFW_HAL_STM32H7_SPI_SPIDEFINITIONS_H_ */ #endif /* FSFW_HAL_STM32H7_SPI_SPIDEFINITIONS_H_ */

View File

@ -1,12 +1,12 @@
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h" #include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include <stddef.h>
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "stm32h7xx_hal.h" #include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_dma.h" #include "stm32h7xx_hal_dma.h"
#include "stm32h7xx_hal_spi.h" #include "stm32h7xx_hal_spi.h"
#include <stddef.h>
user_handler_t spi1UserHandler = &spi::spiIrqHandler; user_handler_t spi1UserHandler = &spi::spiIrqHandler;
user_args_t spi1UserArgs = nullptr; user_args_t spi1UserArgs = nullptr;
@ -18,11 +18,11 @@ user_args_t spi2UserArgs = nullptr;
* @param None * @param None
* @retval None * @retval None
*/ */
void spi::dmaRxIrqHandler(void* dmaHandle) { void spi::dmaRxIrqHandler(void *dmaHandle) {
if(dmaHandle == nullptr) { if (dmaHandle == nullptr) {
return; return;
} }
HAL_DMA_IRQHandler((DMA_HandleTypeDef *) dmaHandle); HAL_DMA_IRQHandler((DMA_HandleTypeDef *)dmaHandle);
} }
/** /**
@ -30,11 +30,11 @@ void spi::dmaRxIrqHandler(void* dmaHandle) {
* @param None * @param None
* @retval None * @retval None
*/ */
void spi::dmaTxIrqHandler(void* dmaHandle) { void spi::dmaTxIrqHandler(void *dmaHandle) {
if(dmaHandle == nullptr) { if (dmaHandle == nullptr) {
return; return;
} }
HAL_DMA_IRQHandler((DMA_HandleTypeDef *) dmaHandle); HAL_DMA_IRQHandler((DMA_HandleTypeDef *)dmaHandle);
} }
/** /**
@ -42,65 +42,62 @@ void spi::dmaTxIrqHandler(void* dmaHandle) {
* @param None * @param None
* @retval None * @retval None
*/ */
void spi::spiIrqHandler(void* spiHandle) { void spi::spiIrqHandler(void *spiHandle) {
if(spiHandle == nullptr) { if (spiHandle == nullptr) {
return; return;
} }
//auto currentSpiHandle = spi::getSpiHandle(); // auto currentSpiHandle = spi::getSpiHandle();
HAL_SPI_IRQHandler((SPI_HandleTypeDef *) spiHandle); HAL_SPI_IRQHandler((SPI_HandleTypeDef *)spiHandle);
} }
void spi::assignSpiUserHandler(spi::SpiBus spiIdx, user_handler_t userHandler, void spi::assignSpiUserHandler(spi::SpiBus spiIdx, user_handler_t userHandler,
user_args_t userArgs) { user_args_t userArgs) {
if(spiIdx == spi::SpiBus::SPI_1) { if (spiIdx == spi::SpiBus::SPI_1) {
spi1UserHandler = userHandler; spi1UserHandler = userHandler;
spi1UserArgs = userArgs; spi1UserArgs = userArgs;
} } else {
else { spi2UserHandler = userHandler;
spi2UserHandler = userHandler; spi2UserArgs = userArgs;
spi2UserArgs = userArgs; }
}
} }
void spi::getSpiUserHandler(spi::SpiBus spiBus, user_handler_t *userHandler, void spi::getSpiUserHandler(spi::SpiBus spiBus, user_handler_t *userHandler,
user_args_t *userArgs) { user_args_t *userArgs) {
if(userHandler == nullptr or userArgs == nullptr) { if (userHandler == nullptr or userArgs == nullptr) {
return; return;
} }
if(spiBus == spi::SpiBus::SPI_1) { if (spiBus == spi::SpiBus::SPI_1) {
*userArgs = spi1UserArgs; *userArgs = spi1UserArgs;
*userHandler = spi1UserHandler; *userHandler = spi1UserHandler;
} } else {
else { *userArgs = spi2UserArgs;
*userArgs = spi2UserArgs; *userHandler = spi2UserHandler;
*userHandler = spi2UserHandler; }
}
} }
void spi::assignSpiUserArgs(spi::SpiBus spiBus, user_args_t userArgs) { void spi::assignSpiUserArgs(spi::SpiBus spiBus, user_args_t userArgs) {
if(spiBus == spi::SpiBus::SPI_1) { if (spiBus == spi::SpiBus::SPI_1) {
spi1UserArgs = userArgs; spi1UserArgs = userArgs;
} } else {
else { spi2UserArgs = userArgs;
spi2UserArgs = userArgs; }
}
} }
/* Do not change these function names! They need to be exactly equal to the name of the functions /* Do not change these function names! They need to be exactly equal to the name of the functions
defined in the startup_stm32h743xx.s files! */ defined in the startup_stm32h743xx.s files! */
extern "C" void SPI1_IRQHandler() { extern "C" void SPI1_IRQHandler() {
if(spi1UserHandler != NULL) { if (spi1UserHandler != NULL) {
spi1UserHandler(spi1UserArgs); spi1UserHandler(spi1UserArgs);
return; return;
} }
Default_Handler(); Default_Handler();
} }
extern "C" void SPI2_IRQHandler() { extern "C" void SPI2_IRQHandler() {
if(spi2UserHandler != nullptr) { if (spi2UserHandler != nullptr) {
spi2UserHandler(spi2UserArgs); spi2UserHandler(spi2UserArgs);
return; return;
} }
Default_Handler(); Default_Handler();
} }

View File

@ -18,10 +18,8 @@ void assignSpiUserArgs(spi::SpiBus spiBus, user_args_t userArgs);
* @param user_handler * @param user_handler
* @param user_args * @param user_args
*/ */
void assignSpiUserHandler(spi::SpiBus spiBus, user_handler_t user_handler, void assignSpiUserHandler(spi::SpiBus spiBus, user_handler_t user_handler, user_args_t user_args);
user_args_t user_args); void getSpiUserHandler(spi::SpiBus spiBus, user_handler_t* user_handler, user_args_t* user_args);
void getSpiUserHandler(spi::SpiBus spiBus, user_handler_t* user_handler,
user_args_t* user_args);
/** /**
* Generic interrupt handlers supplied for convenience. Do not call these directly! Set them * Generic interrupt handlers supplied for convenience. Do not call these directly! Set them
@ -32,7 +30,7 @@ void dmaRxIrqHandler(void* dma_handle);
void dmaTxIrqHandler(void* dma_handle); void dmaTxIrqHandler(void* dma_handle);
void spiIrqHandler(void* spi_handle); void spiIrqHandler(void* spi_handle);
} } // namespace spi
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,82 +1,81 @@
#include "fsfw_hal/stm32h7/spi/stm32h743zi.h" #include "fsfw_hal/stm32h7/spi/stm32h743zi.h"
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_rcc.h"
#include <cstdio> #include <cstdio>
#include "fsfw_hal/stm32h7/spi/spiCore.h"
#include "fsfw_hal/stm32h7/spi/spiInterrupts.h"
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_rcc.h"
void spiSetupWrapper() { void spiSetupWrapper() {
__HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_SPI1_CLK_ENABLE();
} }
void spiCleanUpWrapper() { void spiCleanUpWrapper() {
__HAL_RCC_SPI1_FORCE_RESET(); __HAL_RCC_SPI1_FORCE_RESET();
__HAL_RCC_SPI1_RELEASE_RESET(); __HAL_RCC_SPI1_RELEASE_RESET();
} }
void spiDmaClockEnableWrapper() { void spiDmaClockEnableWrapper() { __HAL_RCC_DMA2_CLK_ENABLE(); }
__HAL_RCC_DMA2_CLK_ENABLE();
}
void stm32h7::h743zi::standardPollingCfg(spi::MspPollingConfigStruct& cfg) { void stm32h7::h743zi::standardPollingCfg(spi::MspPollingConfigStruct& cfg) {
cfg.setupCb = &spiSetupWrapper; cfg.setupCb = &spiSetupWrapper;
cfg.cleanupCb = &spiCleanUpWrapper; cfg.cleanupCb = &spiCleanUpWrapper;
cfg.sck.port = GPIOA; cfg.sck.port = GPIOA;
cfg.sck.pin = GPIO_PIN_5; cfg.sck.pin = GPIO_PIN_5;
cfg.miso.port = GPIOA; cfg.miso.port = GPIOA;
cfg.miso.pin = GPIO_PIN_6; cfg.miso.pin = GPIO_PIN_6;
cfg.mosi.port = GPIOA; cfg.mosi.port = GPIOA;
cfg.mosi.pin = GPIO_PIN_7; cfg.mosi.pin = GPIO_PIN_7;
cfg.sck.altFnc = GPIO_AF5_SPI1; cfg.sck.altFnc = GPIO_AF5_SPI1;
cfg.mosi.altFnc = GPIO_AF5_SPI1; cfg.mosi.altFnc = GPIO_AF5_SPI1;
cfg.miso.altFnc = GPIO_AF5_SPI1; cfg.miso.altFnc = GPIO_AF5_SPI1;
} }
void stm32h7::h743zi::standardInterruptCfg(spi::MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio, void stm32h7::h743zi::standardInterruptCfg(spi::MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities spiSubprio) { IrqPriorities spiSubprio) {
// High, but works on FreeRTOS as well (priorities range from 0 to 15) // High, but works on FreeRTOS as well (priorities range from 0 to 15)
cfg.preEmptPriority = spiIrqPrio; cfg.preEmptPriority = spiIrqPrio;
cfg.subpriority = spiSubprio; cfg.subpriority = spiSubprio;
cfg.spiIrqNumber = SPI1_IRQn; cfg.spiIrqNumber = SPI1_IRQn;
cfg.spiBus = spi::SpiBus::SPI_1; cfg.spiBus = spi::SpiBus::SPI_1;
user_handler_t spiUserHandler = nullptr; user_handler_t spiUserHandler = nullptr;
user_args_t spiUserArgs = nullptr; user_args_t spiUserArgs = nullptr;
getSpiUserHandler(spi::SpiBus::SPI_1, &spiUserHandler, &spiUserArgs); getSpiUserHandler(spi::SpiBus::SPI_1, &spiUserHandler, &spiUserArgs);
if(spiUserHandler == nullptr) { if (spiUserHandler == nullptr) {
printf("spi::h743zi::standardInterruptCfg: Invalid SPI user handlers\n"); printf("spi::h743zi::standardInterruptCfg: Invalid SPI user handlers\n");
return; return;
} }
cfg.spiUserArgs = spiUserArgs; cfg.spiUserArgs = spiUserArgs;
cfg.spiIrqHandler = spiUserHandler; cfg.spiIrqHandler = spiUserHandler;
standardPollingCfg(cfg); standardPollingCfg(cfg);
} }
void stm32h7::h743zi::standardDmaCfg(spi::MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio, void stm32h7::h743zi::standardDmaCfg(spi::MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities txIrqPrio, IrqPriorities rxIrqPrio, IrqPriorities spiSubprio, IrqPriorities txIrqPrio, IrqPriorities rxIrqPrio,
IrqPriorities txSubprio, IrqPriorities rxSubprio) { IrqPriorities spiSubprio, IrqPriorities txSubprio,
cfg.dmaClkEnableWrapper = &spiDmaClockEnableWrapper; IrqPriorities rxSubprio) {
cfg.rxDmaIndex = dma::DMAIndexes::DMA_2; cfg.dmaClkEnableWrapper = &spiDmaClockEnableWrapper;
cfg.txDmaIndex = dma::DMAIndexes::DMA_2; cfg.rxDmaIndex = dma::DMAIndexes::DMA_2;
cfg.txDmaStream = dma::DMAStreams::STREAM_3; cfg.txDmaIndex = dma::DMAIndexes::DMA_2;
cfg.rxDmaStream = dma::DMAStreams::STREAM_2; cfg.txDmaStream = dma::DMAStreams::STREAM_3;
DMA_HandleTypeDef* txHandle; cfg.rxDmaStream = dma::DMAStreams::STREAM_2;
DMA_HandleTypeDef* rxHandle; DMA_HandleTypeDef* txHandle;
spi::getDmaHandles(&txHandle, &rxHandle); DMA_HandleTypeDef* rxHandle;
if(txHandle == nullptr or rxHandle == nullptr) { spi::getDmaHandles(&txHandle, &rxHandle);
printf("spi::h743zi::standardDmaCfg: Invalid DMA handles\n"); if (txHandle == nullptr or rxHandle == nullptr) {
return; printf("spi::h743zi::standardDmaCfg: Invalid DMA handles\n");
} return;
spi::configureDmaHandle(txHandle, spi::SpiBus::SPI_1, dma::DMAType::TX, cfg.txDmaIndex, }
cfg.txDmaStream, &cfg.txDmaIrqNumber); spi::configureDmaHandle(txHandle, spi::SpiBus::SPI_1, dma::DMAType::TX, cfg.txDmaIndex,
spi::configureDmaHandle(rxHandle, spi::SpiBus::SPI_1, dma::DMAType::RX, cfg.rxDmaIndex, cfg.txDmaStream, &cfg.txDmaIrqNumber);
cfg.rxDmaStream, &cfg.rxDmaIrqNumber, DMA_NORMAL, DMA_PRIORITY_HIGH); spi::configureDmaHandle(rxHandle, spi::SpiBus::SPI_1, dma::DMAType::RX, cfg.rxDmaIndex,
cfg.txPreEmptPriority = txIrqPrio; cfg.rxDmaStream, &cfg.rxDmaIrqNumber, DMA_NORMAL, DMA_PRIORITY_HIGH);
cfg.rxPreEmptPriority = txSubprio; cfg.txPreEmptPriority = txIrqPrio;
cfg.txSubpriority = rxIrqPrio; cfg.rxPreEmptPriority = txSubprio;
cfg.rxSubpriority = rxSubprio; cfg.txSubpriority = rxIrqPrio;
standardInterruptCfg(cfg, spiIrqPrio, spiSubprio); cfg.rxSubpriority = rxSubprio;
standardInterruptCfg(cfg, spiIrqPrio, spiSubprio);
} }

View File

@ -9,14 +9,12 @@ namespace h743zi {
void standardPollingCfg(spi::MspPollingConfigStruct& cfg); void standardPollingCfg(spi::MspPollingConfigStruct& cfg);
void standardInterruptCfg(spi::MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio, void standardInterruptCfg(spi::MspIrqConfigStruct& cfg, IrqPriorities spiIrqPrio,
IrqPriorities spiSubprio = HIGHEST); IrqPriorities spiSubprio = HIGHEST);
void standardDmaCfg(spi::MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio, void standardDmaCfg(spi::MspDmaConfigStruct& cfg, IrqPriorities spiIrqPrio, IrqPriorities txIrqPrio,
IrqPriorities txIrqPrio, IrqPriorities rxIrqPrio, IrqPriorities rxIrqPrio, IrqPriorities spiSubprio = HIGHEST,
IrqPriorities spiSubprio = HIGHEST, IrqPriorities txSubPrio = HIGHEST, IrqPriorities txSubPrio = HIGHEST, IrqPriorities rxSubprio = HIGHEST);
IrqPriorities rxSubprio = HIGHEST);
} // namespace h743zi
} } // namespace stm32h7
}
#endif /* FSFW_HAL_STM32H7_SPI_STM32H743ZISPI_H_ */ #endif /* FSFW_HAL_STM32H7_SPI_STM32H743ZISPI_H_ */

View File

@ -4,8 +4,8 @@
#include "fsfw/action/ActionHelper.h" #include "fsfw/action/ActionHelper.h"
#include "fsfw/action/ActionMessage.h" #include "fsfw/action/ActionMessage.h"
#include "fsfw/action/CommandActionHelper.h" #include "fsfw/action/CommandActionHelper.h"
#include "fsfw/action/HasActionsIF.h"
#include "fsfw/action/CommandsActionsIF.h" #include "fsfw/action/CommandsActionsIF.h"
#include "fsfw/action/HasActionsIF.h"
#include "fsfw/action/SimpleActionHelper.h" #include "fsfw/action/SimpleActionHelper.h"
#endif /* FSFW_INC_FSFW_ACTION_H_ */ #endif /* FSFW_INC_FSFW_ACTION_H_ */

View File

@ -1,177 +1,165 @@
#include "fsfw/action.h" #include "fsfw/action.h"
#include "fsfw/ipc/MessageQueueSenderIF.h" #include "fsfw/ipc/MessageQueueSenderIF.h"
#include "fsfw/objectmanager/ObjectManager.h" #include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/serviceinterface/ServiceInterface.h" #include "fsfw/serviceinterface/ServiceInterface.h"
ActionHelper::ActionHelper(HasActionsIF* setOwner, ActionHelper::ActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue)
MessageQueueIF* useThisQueue) : : owner(setOwner), queueToUse(useThisQueue) {}
owner(setOwner), queueToUse(useThisQueue) {
}
ActionHelper::~ActionHelper() { ActionHelper::~ActionHelper() {}
}
ReturnValue_t ActionHelper::handleActionMessage(CommandMessage* command) { ReturnValue_t ActionHelper::handleActionMessage(CommandMessage* command) {
if (command->getCommand() == ActionMessage::EXECUTE_ACTION) { if (command->getCommand() == ActionMessage::EXECUTE_ACTION) {
ActionId_t currentAction = ActionMessage::getActionId(command); ActionId_t currentAction = ActionMessage::getActionId(command);
prepareExecution(command->getSender(), currentAction, prepareExecution(command->getSender(), currentAction, ActionMessage::getStoreId(command));
ActionMessage::getStoreId(command)); return HasReturnvaluesIF::RETURN_OK;
return HasReturnvaluesIF::RETURN_OK; } else {
} else { return CommandMessage::UNKNOWN_COMMAND;
return CommandMessage::UNKNOWN_COMMAND; }
}
} }
ReturnValue_t ActionHelper::initialize(MessageQueueIF* queueToUse_) { ReturnValue_t ActionHelper::initialize(MessageQueueIF* queueToUse_) {
ipcStore = ObjectManager::instance()->get<StorageManagerIF>(objects::IPC_STORE); ipcStore = ObjectManager::instance()->get<StorageManagerIF>(objects::IPC_STORE);
if (ipcStore == nullptr) { if (ipcStore == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
if(queueToUse_ != nullptr) { if (queueToUse_ != nullptr) {
setQueueToUse(queueToUse_); setQueueToUse(queueToUse_);
} }
if(queueToUse == nullptr) { if (queueToUse == nullptr) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "ActionHelper::initialize: No queue set" << std::endl; sif::warning << "ActionHelper::initialize: No queue set" << std::endl;
#else #else
sif::printWarning("ActionHelper::initialize: No queue set\n"); sif::printWarning("ActionHelper::initialize: No queue set\n");
#endif #endif
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void ActionHelper::step(uint8_t step, MessageQueueId_t reportTo, void ActionHelper::step(uint8_t step, MessageQueueId_t reportTo, ActionId_t commandId,
ActionId_t commandId, ReturnValue_t result) { ReturnValue_t result) {
CommandMessage reply; CommandMessage reply;
ActionMessage::setStepReply(&reply, commandId, step + STEP_OFFSET, result); ActionMessage::setStepReply(&reply, commandId, step + STEP_OFFSET, result);
queueToUse->sendMessage(reportTo, &reply); queueToUse->sendMessage(reportTo, &reply);
} }
void ActionHelper::finish(bool success, MessageQueueId_t reportTo, ActionId_t commandId, void ActionHelper::finish(bool success, MessageQueueId_t reportTo, ActionId_t commandId,
ReturnValue_t result) { ReturnValue_t result) {
CommandMessage reply;
ActionMessage::setCompletionReply(&reply, commandId, success, result);
queueToUse->sendMessage(reportTo, &reply);
}
void ActionHelper::setQueueToUse(MessageQueueIF* queue) { queueToUse = queue; }
void ActionHelper::prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId,
store_address_t dataAddress) {
const uint8_t* dataPtr = NULL;
size_t size = 0;
ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
CommandMessage reply; CommandMessage reply;
ActionMessage::setCompletionReply(&reply, commandId, success, result); ActionMessage::setStepReply(&reply, actionId, 0, result);
queueToUse->sendMessage(reportTo, &reply); queueToUse->sendMessage(commandedBy, &reply);
} return;
}
void ActionHelper::setQueueToUse(MessageQueueIF* queue) { result = owner->executeAction(actionId, commandedBy, dataPtr, size);
queueToUse = queue; ipcStore->deleteData(dataAddress);
} if (result == HasActionsIF::EXECUTION_FINISHED) {
void ActionHelper::prepareExecution(MessageQueueId_t commandedBy,
ActionId_t actionId, store_address_t dataAddress) {
const uint8_t* dataPtr = NULL;
size_t size = 0;
ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
CommandMessage reply;
ActionMessage::setStepReply(&reply, actionId, 0, result);
queueToUse->sendMessage(commandedBy, &reply);
return;
}
result = owner->executeAction(actionId, commandedBy, dataPtr, size);
ipcStore->deleteData(dataAddress);
if(result == HasActionsIF::EXECUTION_FINISHED) {
CommandMessage reply;
ActionMessage::setCompletionReply(&reply, actionId, true, result);
queueToUse->sendMessage(commandedBy, &reply);
}
if (result != HasReturnvaluesIF::RETURN_OK) {
CommandMessage reply;
ActionMessage::setStepReply(&reply, actionId, 0, result);
queueToUse->sendMessage(commandedBy, &reply);
return;
}
}
ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo,
ActionId_t replyId, SerializeIF* data, bool hideSender) {
CommandMessage reply; CommandMessage reply;
store_address_t storeAddress; ActionMessage::setCompletionReply(&reply, actionId, true, result);
uint8_t *dataPtr; queueToUse->sendMessage(commandedBy, &reply);
size_t maxSize = data->getSerializedSize(); }
if (maxSize == 0) { if (result != HasReturnvaluesIF::RETURN_OK) {
/* No error, there's simply nothing to report. */ CommandMessage reply;
return HasReturnvaluesIF::RETURN_OK; ActionMessage::setStepReply(&reply, actionId, 0, result);
} queueToUse->sendMessage(commandedBy, &reply);
size_t size = 0; return;
ReturnValue_t result = ipcStore->getFreeElement(&storeAddress, maxSize, }
&dataPtr); }
if (result != HasReturnvaluesIF::RETURN_OK) {
ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo, ActionId_t replyId,
SerializeIF* data, bool hideSender) {
CommandMessage reply;
store_address_t storeAddress;
uint8_t* dataPtr;
size_t maxSize = data->getSerializedSize();
if (maxSize == 0) {
/* No error, there's simply nothing to report. */
return HasReturnvaluesIF::RETURN_OK;
}
size_t size = 0;
ReturnValue_t result = ipcStore->getFreeElement(&storeAddress, maxSize, &dataPtr);
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "ActionHelper::reportData: Getting free element from IPC store failed!" << sif::warning << "ActionHelper::reportData: Getting free element from IPC store failed!"
std::endl; << std::endl;
#else #else
sif::printWarning("ActionHelper::reportData: Getting free element from IPC " sif::printWarning(
"store failed!\n"); "ActionHelper::reportData: Getting free element from IPC "
"store failed!\n");
#endif #endif
return result;
}
result = data->serialize(&dataPtr, &size, maxSize,
SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeAddress);
return result;
}
/* We don't need to report the objectId, as we receive REQUESTED data before the completion
success message. True aperiodic replies need to be reported with another dedicated message. */
ActionMessage::setDataReply(&reply, replyId, storeAddress);
/* If the sender needs to be hidden, for example to handle packet
as unrequested reply, this will be done here. */
if (hideSender) {
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
}
else {
result = queueToUse->sendMessage(reportTo, &reply);
}
if (result != HasReturnvaluesIF::RETURN_OK){
ipcStore->deleteData(storeAddress);
}
return result; return result;
}
result = data->serialize(&dataPtr, &size, maxSize, SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeAddress);
return result;
}
/* We don't need to report the objectId, as we receive REQUESTED data before the completion
success message. True aperiodic replies need to be reported with another dedicated message. */
ActionMessage::setDataReply(&reply, replyId, storeAddress);
/* If the sender needs to be hidden, for example to handle packet
as unrequested reply, this will be done here. */
if (hideSender) {
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
} else {
result = queueToUse->sendMessage(reportTo, &reply);
}
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeAddress);
}
return result;
} }
void ActionHelper::resetHelper() { void ActionHelper::resetHelper() {}
}
ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo, ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo, ActionId_t replyId,
ActionId_t replyId, const uint8_t *data, size_t dataSize, const uint8_t* data, size_t dataSize, bool hideSender) {
bool hideSender) { CommandMessage reply;
CommandMessage reply; store_address_t storeAddress;
store_address_t storeAddress; ReturnValue_t result = ipcStore->addData(&storeAddress, data, dataSize);
ReturnValue_t result = ipcStore->addData(&storeAddress, data, dataSize); if (result != HasReturnvaluesIF::RETURN_OK) {
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "ActionHelper::reportData: Adding data to IPC store failed!" << std::endl; sif::warning << "ActionHelper::reportData: Adding data to IPC store failed!" << std::endl;
#else #else
sif::printWarning("ActionHelper::reportData: Adding data to IPC store failed!\n"); sif::printWarning("ActionHelper::reportData: Adding data to IPC store failed!\n");
#endif #endif
return result;
}
/* We don't need to report the objectId, as we receive REQUESTED data before the completion
success message. True aperiodic replies need to be reported with another dedicated message. */
ActionMessage::setDataReply(&reply, replyId, storeAddress);
/* If the sender needs to be hidden, for example to handle packet
as unrequested reply, this will be done here. */
if (hideSender) {
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
}
else {
result = queueToUse->sendMessage(reportTo, &reply);
}
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeAddress);
}
return result; return result;
}
/* We don't need to report the objectId, as we receive REQUESTED data before the completion
success message. True aperiodic replies need to be reported with another dedicated message. */
ActionMessage::setDataReply(&reply, replyId, storeAddress);
/* If the sender needs to be hidden, for example to handle packet
as unrequested reply, this will be done here. */
if (hideSender) {
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
} else {
result = queueToUse->sendMessage(reportTo, &reply);
}
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeAddress);
}
return result;
} }

View File

@ -1,9 +1,9 @@
#ifndef FSFW_ACTION_ACTIONHELPER_H_ #ifndef FSFW_ACTION_ACTIONHELPER_H_
#define FSFW_ACTION_ACTIONHELPER_H_ #define FSFW_ACTION_ACTIONHELPER_H_
#include "ActionMessage.h"
#include "../serialize/SerializeIF.h"
#include "../ipc/MessageQueueIF.h" #include "../ipc/MessageQueueIF.h"
#include "../serialize/SerializeIF.h"
#include "ActionMessage.h"
/** /**
* @brief Action Helper is a helper class which handles action messages * @brief Action Helper is a helper class which handles action messages
* *
@ -17,110 +17,110 @@
class HasActionsIF; class HasActionsIF;
class ActionHelper { class ActionHelper {
public: public:
/** /**
* Constructor of the action helper * Constructor of the action helper
* @param setOwner Pointer to the owner of the interface * @param setOwner Pointer to the owner of the interface
* @param useThisQueue messageQueue to be used, can be set during * @param useThisQueue messageQueue to be used, can be set during
* initialize function as well. * initialize function as well.
*/ */
ActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue); ActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue);
virtual ~ActionHelper(); virtual ~ActionHelper();
/** /**
* Function to be called from the owner with a new command message * Function to be called from the owner with a new command message
* *
* If the message is a valid action message the helper will use the * If the message is a valid action message the helper will use the
* executeAction function from HasActionsIF. * executeAction function from HasActionsIF.
* If the message is invalid or the callback fails a message reply will be * If the message is invalid or the callback fails a message reply will be
* send to the sender of the message automatically. * send to the sender of the message automatically.
* *
* @param command Pointer to a command message received by the owner * @param command Pointer to a command message received by the owner
* @return HasReturnvaluesIF::RETURN_OK if the message is a action message, * @return HasReturnvaluesIF::RETURN_OK if the message is a action message,
* CommandMessage::UNKNOW_COMMAND if this message ID is unkown * CommandMessage::UNKNOW_COMMAND if this message ID is unkown
*/ */
ReturnValue_t handleActionMessage(CommandMessage* command); ReturnValue_t handleActionMessage(CommandMessage* command);
/** /**
* Helper initialize function. Must be called before use of any other * Helper initialize function. Must be called before use of any other
* helper function * helper function
* @param queueToUse_ Pointer to the messageQueue to be used, optional * @param queueToUse_ Pointer to the messageQueue to be used, optional
* if queue was set in constructor * if queue was set in constructor
* @return Returns RETURN_OK if successful * @return Returns RETURN_OK if successful
*/ */
ReturnValue_t initialize(MessageQueueIF* queueToUse_ = nullptr); ReturnValue_t initialize(MessageQueueIF* queueToUse_ = nullptr);
/** /**
* Function to be called from the owner to send a step message. * Function to be called from the owner to send a step message.
* Success or failure will be determined by the result value. * Success or failure will be determined by the result value.
* *
* @param step Number of steps already done * @param step Number of steps already done
* @param reportTo The messageQueueId to report the step message to * @param reportTo The messageQueueId to report the step message to
* @param commandId ID of the executed command * @param commandId ID of the executed command
* @param result Result of the execution * @param result Result of the execution
*/ */
void step(uint8_t step, MessageQueueId_t reportTo, void step(uint8_t step, MessageQueueId_t reportTo, ActionId_t commandId,
ActionId_t commandId,
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
/** /**
* Function to be called by the owner to send a action completion message * Function to be called by the owner to send a action completion message
* @param success Specify whether action was completed successfully or not. * @param success Specify whether action was completed successfully or not.
* @param reportTo MessageQueueId_t to report the action completion message to * @param reportTo MessageQueueId_t to report the action completion message to
* @param commandId ID of the executed command * @param commandId ID of the executed command
* @param result Result of the execution * @param result Result of the execution
*/ */
void finish(bool success, MessageQueueId_t reportTo, ActionId_t commandId, void finish(bool success, MessageQueueId_t reportTo, ActionId_t commandId,
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
/** /**
* Function to be called by the owner if an action does report data. * Function to be called by the owner if an action does report data.
* Takes a SerializeIF* pointer and serializes it into the IPC store. * Takes a SerializeIF* pointer and serializes it into the IPC store.
* @param reportTo MessageQueueId_t to report the action completion * @param reportTo MessageQueueId_t to report the action completion
* message to * message to
* @param replyId ID of the executed command * @param replyId ID of the executed command
* @param data Pointer to the data * @param data Pointer to the data
* @return Returns RETURN_OK if successful, otherwise failure code * @return Returns RETURN_OK if successful, otherwise failure code
*/ */
ReturnValue_t reportData(MessageQueueId_t reportTo, ActionId_t replyId, ReturnValue_t reportData(MessageQueueId_t reportTo, ActionId_t replyId, SerializeIF* data,
SerializeIF* data, bool hideSender = false); bool hideSender = false);
/** /**
* Function to be called by the owner if an action does report data. * Function to be called by the owner if an action does report data.
* Takes the raw data and writes it into the IPC store. * Takes the raw data and writes it into the IPC store.
* @param reportTo MessageQueueId_t to report the action completion * @param reportTo MessageQueueId_t to report the action completion
* message to * message to
* @param replyId ID of the executed command * @param replyId ID of the executed command
* @param data Pointer to the data * @param data Pointer to the data
* @return Returns RETURN_OK if successful, otherwise failure code * @return Returns RETURN_OK if successful, otherwise failure code
*/ */
ReturnValue_t reportData(MessageQueueId_t reportTo, ActionId_t replyId, ReturnValue_t reportData(MessageQueueId_t reportTo, ActionId_t replyId, const uint8_t* data,
const uint8_t* data, size_t dataSize, bool hideSender = false); size_t dataSize, bool hideSender = false);
/** /**
* Function to setup the MessageQueueIF* of the helper. Can be used to * Function to setup the MessageQueueIF* of the helper. Can be used to
* set the MessageQueueIF* if message queue is unavailable at construction * set the MessageQueueIF* if message queue is unavailable at construction
* and initialize but must be setup before first call of other functions. * and initialize but must be setup before first call of other functions.
* @param queue Queue to be used by the helper * @param queue Queue to be used by the helper
*/ */
void setQueueToUse(MessageQueueIF *queue); void setQueueToUse(MessageQueueIF* queue);
protected:
//! Increase of value of this per step
static const uint8_t STEP_OFFSET = 1;
//! Pointer to the owner
HasActionsIF* owner;
//! Queue to be used as response sender, has to be set in ctor or with
//! setQueueToUse
MessageQueueIF* queueToUse;
//! Pointer to an IPC Store, initialized during construction or
StorageManagerIF* ipcStore = nullptr;
/** protected:
* Internal function called by handleActionMessage //! Increase of value of this per step
* @param commandedBy MessageQueueID of Commander static const uint8_t STEP_OFFSET = 1;
* @param actionId ID of action to be done //! Pointer to the owner
* @param dataAddress Address of additional data in IPC Store HasActionsIF* owner;
*/ //! Queue to be used as response sender, has to be set in ctor or with
virtual void prepareExecution(MessageQueueId_t commandedBy, //! setQueueToUse
ActionId_t actionId, store_address_t dataAddress); MessageQueueIF* queueToUse;
/** //! Pointer to an IPC Store, initialized during construction or
* @brief Default implementation is empty. StorageManagerIF* ipcStore = nullptr;
*/
virtual void resetHelper(); /**
* Internal function called by handleActionMessage
* @param commandedBy MessageQueueID of Commander
* @param actionId ID of action to be done
* @param dataAddress Address of additional data in IPC Store
*/
virtual void prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId,
store_address_t dataAddress);
/**
* @brief Default implementation is empty.
*/
virtual void resetHelper();
}; };
#endif /* FSFW_ACTION_ACTIONHELPER_H_ */ #endif /* FSFW_ACTION_ACTIONHELPER_H_ */

View File

@ -1,81 +1,77 @@
#include "fsfw/action.h" #include "fsfw/action.h"
#include "fsfw/objectmanager/ObjectManager.h" #include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/storagemanager/StorageManagerIF.h" #include "fsfw/storagemanager/StorageManagerIF.h"
ActionMessage::ActionMessage() { ActionMessage::ActionMessage() {}
}
ActionMessage::~ActionMessage() { ActionMessage::~ActionMessage() {}
}
void ActionMessage::setCommand(CommandMessage* message, ActionId_t fid, void ActionMessage::setCommand(CommandMessage* message, ActionId_t fid,
store_address_t parameters) { store_address_t parameters) {
message->setCommand(EXECUTE_ACTION); message->setCommand(EXECUTE_ACTION);
message->setParameter(fid); message->setParameter(fid);
message->setParameter2(parameters.raw); message->setParameter2(parameters.raw);
} }
ActionId_t ActionMessage::getActionId(const CommandMessage* message) { ActionId_t ActionMessage::getActionId(const CommandMessage* message) {
return ActionId_t(message->getParameter()); return ActionId_t(message->getParameter());
} }
store_address_t ActionMessage::getStoreId(const CommandMessage* message) { store_address_t ActionMessage::getStoreId(const CommandMessage* message) {
store_address_t temp; store_address_t temp;
temp.raw = message->getParameter2(); temp.raw = message->getParameter2();
return temp; return temp;
} }
void ActionMessage::setStepReply(CommandMessage* message, ActionId_t fid, uint8_t step, void ActionMessage::setStepReply(CommandMessage* message, ActionId_t fid, uint8_t step,
ReturnValue_t result) { ReturnValue_t result) {
if (result == HasReturnvaluesIF::RETURN_OK) { if (result == HasReturnvaluesIF::RETURN_OK) {
message->setCommand(STEP_SUCCESS); message->setCommand(STEP_SUCCESS);
} else { } else {
message->setCommand(STEP_FAILED); message->setCommand(STEP_FAILED);
} }
message->setParameter(fid); message->setParameter(fid);
message->setParameter2((step << 16) + result); message->setParameter2((step << 16) + result);
} }
uint8_t ActionMessage::getStep(const CommandMessage* message) { uint8_t ActionMessage::getStep(const CommandMessage* message) {
return uint8_t((message->getParameter2() >> 16) & 0xFF); return uint8_t((message->getParameter2() >> 16) & 0xFF);
} }
ReturnValue_t ActionMessage::getReturnCode(const CommandMessage* message) { ReturnValue_t ActionMessage::getReturnCode(const CommandMessage* message) {
return message->getParameter2() & 0xFFFF; return message->getParameter2() & 0xFFFF;
} }
void ActionMessage::setDataReply(CommandMessage* message, ActionId_t actionId, void ActionMessage::setDataReply(CommandMessage* message, ActionId_t actionId,
store_address_t data) { store_address_t data) {
message->setCommand(DATA_REPLY); message->setCommand(DATA_REPLY);
message->setParameter(actionId); message->setParameter(actionId);
message->setParameter2(data.raw); message->setParameter2(data.raw);
} }
void ActionMessage::setCompletionReply(CommandMessage* message, void ActionMessage::setCompletionReply(CommandMessage* message, ActionId_t fid, bool success,
ActionId_t fid, bool success, ReturnValue_t result) { ReturnValue_t result) {
if (success) { if (success) {
message->setCommand(COMPLETION_SUCCESS); message->setCommand(COMPLETION_SUCCESS);
} } else {
else { message->setCommand(COMPLETION_FAILED);
message->setCommand(COMPLETION_FAILED); }
} message->setParameter(fid);
message->setParameter(fid); message->setParameter2(result);
message->setParameter2(result);
} }
void ActionMessage::clear(CommandMessage* message) { void ActionMessage::clear(CommandMessage* message) {
switch(message->getCommand()) { switch (message->getCommand()) {
case EXECUTE_ACTION: case EXECUTE_ACTION:
case DATA_REPLY: { case DATA_REPLY: {
StorageManagerIF *ipcStore = ObjectManager::instance()->get<StorageManagerIF>( StorageManagerIF* ipcStore =
objects::IPC_STORE); ObjectManager::instance()->get<StorageManagerIF>(objects::IPC_STORE);
if (ipcStore != NULL) { if (ipcStore != NULL) {
ipcStore->deleteData(getStoreId(message)); ipcStore->deleteData(getStoreId(message));
} }
break; break;
} }
default: default:
break; break;
} }
} }

View File

@ -14,34 +14,33 @@ using ActionId_t = uint32_t;
* ActionHelper are able to process these messages. * ActionHelper are able to process these messages.
*/ */
class ActionMessage { class ActionMessage {
private: private:
ActionMessage(); ActionMessage();
public:
static const uint8_t MESSAGE_ID = messagetypes::ACTION;
static const Command_t EXECUTE_ACTION = MAKE_COMMAND_ID(1);
static const Command_t STEP_SUCCESS = MAKE_COMMAND_ID(2);
static const Command_t STEP_FAILED = MAKE_COMMAND_ID(3);
static const Command_t DATA_REPLY = MAKE_COMMAND_ID(4);
static const Command_t COMPLETION_SUCCESS = MAKE_COMMAND_ID(5);
static const Command_t COMPLETION_FAILED = MAKE_COMMAND_ID(6);
virtual ~ActionMessage(); public:
static void setCommand(CommandMessage* message, ActionId_t fid, static const uint8_t MESSAGE_ID = messagetypes::ACTION;
store_address_t parameters); static const Command_t EXECUTE_ACTION = MAKE_COMMAND_ID(1);
static const Command_t STEP_SUCCESS = MAKE_COMMAND_ID(2);
static const Command_t STEP_FAILED = MAKE_COMMAND_ID(3);
static const Command_t DATA_REPLY = MAKE_COMMAND_ID(4);
static const Command_t COMPLETION_SUCCESS = MAKE_COMMAND_ID(5);
static const Command_t COMPLETION_FAILED = MAKE_COMMAND_ID(6);
static ActionId_t getActionId(const CommandMessage* message ); virtual ~ActionMessage();
static store_address_t getStoreId(const CommandMessage* message); static void setCommand(CommandMessage* message, ActionId_t fid, store_address_t parameters);
static void setStepReply(CommandMessage* message, ActionId_t fid, static ActionId_t getActionId(const CommandMessage* message);
uint8_t step, ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); static store_address_t getStoreId(const CommandMessage* message);
static uint8_t getStep(const CommandMessage* message );
static ReturnValue_t getReturnCode(const CommandMessage* message );
static void setDataReply(CommandMessage* message, ActionId_t actionId,
store_address_t data);
static void setCompletionReply(CommandMessage* message, ActionId_t fid,
bool success, ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
static void clear(CommandMessage* message); static void setStepReply(CommandMessage* message, ActionId_t fid, uint8_t step,
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
static uint8_t getStep(const CommandMessage* message);
static ReturnValue_t getReturnCode(const CommandMessage* message);
static void setDataReply(CommandMessage* message, ActionId_t actionId, store_address_t data);
static void setCompletionReply(CommandMessage* message, ActionId_t fid, bool success,
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
static void clear(CommandMessage* message);
}; };
#endif /* FSFW_ACTION_ACTIONMESSAGE_H_ */ #endif /* FSFW_ACTION_ACTIONMESSAGE_H_ */

View File

@ -1,125 +1,115 @@
#include "fsfw/action.h" #include "fsfw/action.h"
#include "fsfw/objectmanager/ObjectManager.h" #include "fsfw/objectmanager/ObjectManager.h"
CommandActionHelper::CommandActionHelper(CommandsActionsIF *setOwner) : CommandActionHelper::CommandActionHelper(CommandsActionsIF *setOwner)
owner(setOwner), queueToUse(NULL), ipcStore( : owner(setOwner), queueToUse(NULL), ipcStore(NULL), commandCount(0), lastTarget(0) {}
NULL), commandCount(0), lastTarget(0) {
}
CommandActionHelper::~CommandActionHelper() { CommandActionHelper::~CommandActionHelper() {}
}
ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo, ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo, ActionId_t actionId,
ActionId_t actionId, SerializeIF *data) { SerializeIF *data) {
HasActionsIF *receiver = ObjectManager::instance()->get<HasActionsIF>(commandTo); HasActionsIF *receiver = ObjectManager::instance()->get<HasActionsIF>(commandTo);
if (receiver == NULL) { if (receiver == NULL) {
return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS; return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS;
} }
store_address_t storeId; store_address_t storeId;
uint8_t *storePointer; uint8_t *storePointer;
size_t maxSize = data->getSerializedSize(); size_t maxSize = data->getSerializedSize();
ReturnValue_t result = ipcStore->getFreeElement(&storeId, maxSize, ReturnValue_t result = ipcStore->getFreeElement(&storeId, maxSize, &storePointer);
&storePointer); if (result != HasReturnvaluesIF::RETURN_OK) {
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
size_t size = 0;
result = data->serialize(&storePointer, &size, maxSize,
SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return sendCommand(receiver->getCommandQueue(), actionId, storeId);
}
ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo,
ActionId_t actionId, const uint8_t *data, uint32_t size) {
// if (commandCount != 0) {
// return CommandsFunctionsIF::ALREADY_COMMANDING;
// }
HasActionsIF *receiver = ObjectManager::instance()->get<HasActionsIF>(commandTo);
if (receiver == NULL) {
return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS;
}
store_address_t storeId;
ReturnValue_t result = ipcStore->addData(&storeId, data, size);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return sendCommand(receiver->getCommandQueue(), actionId, storeId);
}
ReturnValue_t CommandActionHelper::sendCommand(MessageQueueId_t queueId,
ActionId_t actionId, store_address_t storeId) {
CommandMessage command;
ActionMessage::setCommand(&command, actionId, storeId);
ReturnValue_t result = queueToUse->sendMessage(queueId, &command);
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeId);
}
lastTarget = queueId;
commandCount++;
return result; return result;
}
size_t size = 0;
result = data->serialize(&storePointer, &size, maxSize, SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return sendCommand(receiver->getCommandQueue(), actionId, storeId);
}
ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo, ActionId_t actionId,
const uint8_t *data, uint32_t size) {
// if (commandCount != 0) {
// return CommandsFunctionsIF::ALREADY_COMMANDING;
// }
HasActionsIF *receiver = ObjectManager::instance()->get<HasActionsIF>(commandTo);
if (receiver == NULL) {
return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS;
}
store_address_t storeId;
ReturnValue_t result = ipcStore->addData(&storeId, data, size);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return sendCommand(receiver->getCommandQueue(), actionId, storeId);
}
ReturnValue_t CommandActionHelper::sendCommand(MessageQueueId_t queueId, ActionId_t actionId,
store_address_t storeId) {
CommandMessage command;
ActionMessage::setCommand(&command, actionId, storeId);
ReturnValue_t result = queueToUse->sendMessage(queueId, &command);
if (result != HasReturnvaluesIF::RETURN_OK) {
ipcStore->deleteData(storeId);
}
lastTarget = queueId;
commandCount++;
return result;
} }
ReturnValue_t CommandActionHelper::initialize() { ReturnValue_t CommandActionHelper::initialize() {
ipcStore = ObjectManager::instance()->get<StorageManagerIF>(objects::IPC_STORE); ipcStore = ObjectManager::instance()->get<StorageManagerIF>(objects::IPC_STORE);
if (ipcStore == NULL) { if (ipcStore == NULL) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
queueToUse = owner->getCommandQueuePtr(); queueToUse = owner->getCommandQueuePtr();
if (queueToUse == NULL) { if (queueToUse == NULL) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t CommandActionHelper::handleReply(CommandMessage *reply) { ReturnValue_t CommandActionHelper::handleReply(CommandMessage *reply) {
if (reply->getSender() != lastTarget) { if (reply->getSender() != lastTarget) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
switch (reply->getCommand()) { switch (reply->getCommand()) {
case ActionMessage::COMPLETION_SUCCESS: case ActionMessage::COMPLETION_SUCCESS:
commandCount--; commandCount--;
owner->completionSuccessfulReceived(ActionMessage::getActionId(reply)); owner->completionSuccessfulReceived(ActionMessage::getActionId(reply));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
case ActionMessage::COMPLETION_FAILED: case ActionMessage::COMPLETION_FAILED:
commandCount--; commandCount--;
owner->completionFailedReceived(ActionMessage::getActionId(reply), owner->completionFailedReceived(ActionMessage::getActionId(reply),
ActionMessage::getReturnCode(reply)); ActionMessage::getReturnCode(reply));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
case ActionMessage::STEP_SUCCESS: case ActionMessage::STEP_SUCCESS:
owner->stepSuccessfulReceived(ActionMessage::getActionId(reply), owner->stepSuccessfulReceived(ActionMessage::getActionId(reply),
ActionMessage::getStep(reply)); ActionMessage::getStep(reply));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
case ActionMessage::STEP_FAILED: case ActionMessage::STEP_FAILED:
commandCount--; commandCount--;
owner->stepFailedReceived(ActionMessage::getActionId(reply), owner->stepFailedReceived(ActionMessage::getActionId(reply), ActionMessage::getStep(reply),
ActionMessage::getStep(reply), ActionMessage::getReturnCode(reply));
ActionMessage::getReturnCode(reply)); return HasReturnvaluesIF::RETURN_OK;
return HasReturnvaluesIF::RETURN_OK;
case ActionMessage::DATA_REPLY: case ActionMessage::DATA_REPLY:
extractDataForOwner(ActionMessage::getActionId(reply), extractDataForOwner(ActionMessage::getActionId(reply), ActionMessage::getStoreId(reply));
ActionMessage::getStoreId(reply)); return HasReturnvaluesIF::RETURN_OK;
return HasReturnvaluesIF::RETURN_OK;
default: default:
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
uint8_t CommandActionHelper::getCommandCount() const { uint8_t CommandActionHelper::getCommandCount() const { return commandCount; }
return commandCount;
}
void CommandActionHelper::extractDataForOwner(ActionId_t actionId, store_address_t storeId) { void CommandActionHelper::extractDataForOwner(ActionId_t actionId, store_address_t storeId) {
const uint8_t * data = NULL; const uint8_t *data = NULL;
size_t size = 0; size_t size = 0;
ReturnValue_t result = ipcStore->getData(storeId, &data, &size); ReturnValue_t result = ipcStore->getData(storeId, &data, &size);
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return; return;
} }
owner->dataReceived(actionId, data, size); owner->dataReceived(actionId, data, size);
ipcStore->deleteData(storeId); ipcStore->deleteData(storeId);
} }

View File

@ -2,35 +2,35 @@
#define COMMANDACTIONHELPER_H_ #define COMMANDACTIONHELPER_H_
#include "ActionMessage.h" #include "ActionMessage.h"
#include "fsfw/ipc/MessageQueueIF.h"
#include "fsfw/objectmanager/ObjectManagerIF.h" #include "fsfw/objectmanager/ObjectManagerIF.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "fsfw/serialize/SerializeIF.h" #include "fsfw/serialize/SerializeIF.h"
#include "fsfw/storagemanager/StorageManagerIF.h" #include "fsfw/storagemanager/StorageManagerIF.h"
#include "fsfw/ipc/MessageQueueIF.h"
class CommandsActionsIF; class CommandsActionsIF;
class CommandActionHelper { class CommandActionHelper {
friend class CommandsActionsIF; friend class CommandsActionsIF;
public:
CommandActionHelper(CommandsActionsIF* owner); public:
virtual ~CommandActionHelper(); CommandActionHelper(CommandsActionsIF* owner);
ReturnValue_t commandAction(object_id_t commandTo, virtual ~CommandActionHelper();
ActionId_t actionId, const uint8_t* data, uint32_t size); ReturnValue_t commandAction(object_id_t commandTo, ActionId_t actionId, const uint8_t* data,
ReturnValue_t commandAction(object_id_t commandTo, uint32_t size);
ActionId_t actionId, SerializeIF* data); ReturnValue_t commandAction(object_id_t commandTo, ActionId_t actionId, SerializeIF* data);
ReturnValue_t initialize(); ReturnValue_t initialize();
ReturnValue_t handleReply(CommandMessage* reply); ReturnValue_t handleReply(CommandMessage* reply);
uint8_t getCommandCount() const; uint8_t getCommandCount() const;
private:
CommandsActionsIF* owner; private:
MessageQueueIF* queueToUse; CommandsActionsIF* owner;
StorageManagerIF* ipcStore; MessageQueueIF* queueToUse;
uint8_t commandCount; StorageManagerIF* ipcStore;
MessageQueueId_t lastTarget; uint8_t commandCount;
void extractDataForOwner(ActionId_t actionId, store_address_t storeId); MessageQueueId_t lastTarget;
ReturnValue_t sendCommand(MessageQueueId_t queueId, ActionId_t actionId, void extractDataForOwner(ActionId_t actionId, store_address_t storeId);
store_address_t storeId); ReturnValue_t sendCommand(MessageQueueId_t queueId, ActionId_t actionId, store_address_t storeId);
}; };
#endif /* COMMANDACTIONHELPER_H_ */ #endif /* COMMANDACTIONHELPER_H_ */

View File

@ -1,9 +1,9 @@
#ifndef FSFW_ACTION_COMMANDSACTIONSIF_H_ #ifndef FSFW_ACTION_COMMANDSACTIONSIF_H_
#define FSFW_ACTION_COMMANDSACTIONSIF_H_ #define FSFW_ACTION_COMMANDSACTIONSIF_H_
#include "CommandActionHelper.h"
#include "../returnvalues/HasReturnvaluesIF.h"
#include "../ipc/MessageQueueIF.h" #include "../ipc/MessageQueueIF.h"
#include "../returnvalues/HasReturnvaluesIF.h"
#include "CommandActionHelper.h"
/** /**
* Interface to separate commanding actions of other objects. * Interface to separate commanding actions of other objects.
@ -15,23 +15,21 @@
* - replyReceived(id, step, cause) (if cause == OK, it's a success). * - replyReceived(id, step, cause) (if cause == OK, it's a success).
*/ */
class CommandsActionsIF { class CommandsActionsIF {
friend class CommandActionHelper; friend class CommandActionHelper;
public:
static const uint8_t INTERFACE_ID = CLASS_ID::COMMANDS_ACTIONS_IF; public:
static const ReturnValue_t OBJECT_HAS_NO_FUNCTIONS = MAKE_RETURN_CODE(1); static const uint8_t INTERFACE_ID = CLASS_ID::COMMANDS_ACTIONS_IF;
static const ReturnValue_t ALREADY_COMMANDING = MAKE_RETURN_CODE(2); static const ReturnValue_t OBJECT_HAS_NO_FUNCTIONS = MAKE_RETURN_CODE(1);
virtual ~CommandsActionsIF() {} static const ReturnValue_t ALREADY_COMMANDING = MAKE_RETURN_CODE(2);
virtual MessageQueueIF* getCommandQueuePtr() = 0; virtual ~CommandsActionsIF() {}
protected: virtual MessageQueueIF* getCommandQueuePtr() = 0;
virtual void stepSuccessfulReceived(ActionId_t actionId, uint8_t step) = 0;
virtual void stepFailedReceived(ActionId_t actionId, uint8_t step, protected:
ReturnValue_t returnCode) = 0; virtual void stepSuccessfulReceived(ActionId_t actionId, uint8_t step) = 0;
virtual void dataReceived(ActionId_t actionId, const uint8_t* data, virtual void stepFailedReceived(ActionId_t actionId, uint8_t step, ReturnValue_t returnCode) = 0;
uint32_t size) = 0; virtual void dataReceived(ActionId_t actionId, const uint8_t* data, uint32_t size) = 0;
virtual void completionSuccessfulReceived(ActionId_t actionId) = 0; virtual void completionSuccessfulReceived(ActionId_t actionId) = 0;
virtual void completionFailedReceived(ActionId_t actionId, virtual void completionFailedReceived(ActionId_t actionId, ReturnValue_t returnCode) = 0;
ReturnValue_t returnCode) = 0;
}; };
#endif /* FSFW_ACTION_COMMANDSACTIONSIF_H_ */ #endif /* FSFW_ACTION_COMMANDSACTIONSIF_H_ */

View File

@ -1,11 +1,11 @@
#ifndef FSFW_ACTION_HASACTIONSIF_H_ #ifndef FSFW_ACTION_HASACTIONSIF_H_
#define FSFW_ACTION_HASACTIONSIF_H_ #define FSFW_ACTION_HASACTIONSIF_H_
#include "../ipc/MessageQueueIF.h"
#include "../returnvalues/HasReturnvaluesIF.h"
#include "ActionHelper.h" #include "ActionHelper.h"
#include "ActionMessage.h" #include "ActionMessage.h"
#include "SimpleActionHelper.h" #include "SimpleActionHelper.h"
#include "../returnvalues/HasReturnvaluesIF.h"
#include "../ipc/MessageQueueIF.h"
/** /**
* @brief * @brief
@ -34,30 +34,29 @@
* @ingroup interfaces * @ingroup interfaces
*/ */
class HasActionsIF { class HasActionsIF {
public: public:
static const uint8_t INTERFACE_ID = CLASS_ID::HAS_ACTIONS_IF; static const uint8_t INTERFACE_ID = CLASS_ID::HAS_ACTIONS_IF;
static const ReturnValue_t IS_BUSY = MAKE_RETURN_CODE(1); static const ReturnValue_t IS_BUSY = MAKE_RETURN_CODE(1);
static const ReturnValue_t INVALID_PARAMETERS = MAKE_RETURN_CODE(2); static const ReturnValue_t INVALID_PARAMETERS = MAKE_RETURN_CODE(2);
static const ReturnValue_t EXECUTION_FINISHED = MAKE_RETURN_CODE(3); static const ReturnValue_t EXECUTION_FINISHED = MAKE_RETURN_CODE(3);
static const ReturnValue_t INVALID_ACTION_ID = MAKE_RETURN_CODE(4); static const ReturnValue_t INVALID_ACTION_ID = MAKE_RETURN_CODE(4);
virtual ~HasActionsIF() { } virtual ~HasActionsIF() {}
/** /**
* Function to get the MessageQueueId_t of the implementing object * Function to get the MessageQueueId_t of the implementing object
* @return MessageQueueId_t of the object * @return MessageQueueId_t of the object
*/ */
virtual MessageQueueId_t getCommandQueue() const = 0; virtual MessageQueueId_t getCommandQueue() const = 0;
/** /**
* Execute or initialize the execution of a certain function. * Execute or initialize the execution of a certain function.
* The ActionHelpers will execute this function and behave differently * The ActionHelpers will execute this function and behave differently
* depending on the returnvalue. * depending on the returnvalue.
* *
* @return * @return
* -@c EXECUTION_FINISHED Finish reply will be generated * -@c EXECUTION_FINISHED Finish reply will be generated
* -@c Not RETURN_OK Step failure reply will be generated * -@c Not RETURN_OK Step failure reply will be generated
*/ */
virtual ReturnValue_t executeAction(ActionId_t actionId, virtual ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
MessageQueueId_t commandedBy, const uint8_t* data, size_t size) = 0; const uint8_t* data, size_t size) = 0;
}; };
#endif /* FSFW_ACTION_HASACTIONSIF_H_ */ #endif /* FSFW_ACTION_HASACTIONSIF_H_ */

View File

@ -1,74 +1,67 @@
#include "fsfw/action.h" #include "fsfw/action.h"
SimpleActionHelper::SimpleActionHelper(HasActionsIF* setOwner, SimpleActionHelper::SimpleActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue)
MessageQueueIF* useThisQueue) : : ActionHelper(setOwner, useThisQueue), isExecuting(false) {}
ActionHelper(setOwner, useThisQueue), isExecuting(false) {
}
SimpleActionHelper::~SimpleActionHelper() { SimpleActionHelper::~SimpleActionHelper() {}
}
void SimpleActionHelper::step(ReturnValue_t result) { void SimpleActionHelper::step(ReturnValue_t result) {
// STEP_OFFESET is subtracted to compensate for adding offset in base // STEP_OFFESET is subtracted to compensate for adding offset in base
// method, which is not necessary here. // method, which is not necessary here.
ActionHelper::step(stepCount - STEP_OFFSET, lastCommander, lastAction, ActionHelper::step(stepCount - STEP_OFFSET, lastCommander, lastAction, result);
result); if (result != HasReturnvaluesIF::RETURN_OK) {
if (result != HasReturnvaluesIF::RETURN_OK) { resetHelper();
resetHelper(); }
}
} }
void SimpleActionHelper::finish(ReturnValue_t result) { void SimpleActionHelper::finish(ReturnValue_t result) {
ActionHelper::finish(lastCommander, lastAction, result); ActionHelper::finish(lastCommander, lastAction, result);
resetHelper(); resetHelper();
} }
ReturnValue_t SimpleActionHelper::reportData(SerializeIF* data) { ReturnValue_t SimpleActionHelper::reportData(SerializeIF* data) {
return ActionHelper::reportData(lastCommander, lastAction, data); return ActionHelper::reportData(lastCommander, lastAction, data);
} }
void SimpleActionHelper::resetHelper() { void SimpleActionHelper::resetHelper() {
stepCount = 0; stepCount = 0;
isExecuting = false; isExecuting = false;
lastAction = 0; lastAction = 0;
lastCommander = 0; lastCommander = 0;
} }
void SimpleActionHelper::prepareExecution(MessageQueueId_t commandedBy, void SimpleActionHelper::prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId,
ActionId_t actionId, store_address_t dataAddress) { store_address_t dataAddress) {
CommandMessage reply; CommandMessage reply;
if (isExecuting) { if (isExecuting) {
ipcStore->deleteData(dataAddress);
ActionMessage::setStepReply(&reply, actionId, 0,
HasActionsIF::IS_BUSY);
queueToUse->sendMessage(commandedBy, &reply);
}
const uint8_t* dataPtr = NULL;
size_t size = 0;
ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
ActionMessage::setStepReply(&reply, actionId, 0, result);
queueToUse->sendMessage(commandedBy, &reply);
return;
}
lastCommander = commandedBy;
lastAction = actionId;
result = owner->executeAction(actionId, commandedBy, dataPtr, size);
ipcStore->deleteData(dataAddress); ipcStore->deleteData(dataAddress);
switch (result) { ActionMessage::setStepReply(&reply, actionId, 0, HasActionsIF::IS_BUSY);
queueToUse->sendMessage(commandedBy, &reply);
}
const uint8_t* dataPtr = NULL;
size_t size = 0;
ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size);
if (result != HasReturnvaluesIF::RETURN_OK) {
ActionMessage::setStepReply(&reply, actionId, 0, result);
queueToUse->sendMessage(commandedBy, &reply);
return;
}
lastCommander = commandedBy;
lastAction = actionId;
result = owner->executeAction(actionId, commandedBy, dataPtr, size);
ipcStore->deleteData(dataAddress);
switch (result) {
case HasReturnvaluesIF::RETURN_OK: case HasReturnvaluesIF::RETURN_OK:
isExecuting = true; isExecuting = true;
stepCount++; stepCount++;
break; break;
case HasActionsIF::EXECUTION_FINISHED: case HasActionsIF::EXECUTION_FINISHED:
ActionMessage::setCompletionReply(&reply, actionId, ActionMessage::setCompletionReply(&reply, actionId, true, HasReturnvaluesIF::RETURN_OK);
true, HasReturnvaluesIF::RETURN_OK); queueToUse->sendMessage(commandedBy, &reply);
queueToUse->sendMessage(commandedBy, &reply); break;
break;
default: default:
ActionMessage::setStepReply(&reply, actionId, 0, result); ActionMessage::setStepReply(&reply, actionId, 0, result);
queueToUse->sendMessage(commandedBy, &reply); queueToUse->sendMessage(commandedBy, &reply);
break; break;
} }
} }

View File

@ -8,23 +8,24 @@
* at a time but remembers last commander and last action which * at a time but remembers last commander and last action which
* simplifies usage * simplifies usage
*/ */
class SimpleActionHelper: public ActionHelper { class SimpleActionHelper : public ActionHelper {
public: public:
SimpleActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue); SimpleActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue);
virtual ~SimpleActionHelper(); virtual ~SimpleActionHelper();
void step(ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); void step(ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
void finish(ReturnValue_t result = HasReturnvaluesIF::RETURN_OK); void finish(ReturnValue_t result = HasReturnvaluesIF::RETURN_OK);
ReturnValue_t reportData(SerializeIF* data); ReturnValue_t reportData(SerializeIF* data);
protected: protected:
void prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId, void prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId,
store_address_t dataAddress); store_address_t dataAddress);
virtual void resetHelper(); virtual void resetHelper();
private:
bool isExecuting; private:
MessageQueueId_t lastCommander = MessageQueueIF::NO_QUEUE; bool isExecuting;
ActionId_t lastAction = 0; MessageQueueId_t lastCommander = MessageQueueIF::NO_QUEUE;
uint8_t stepCount = 0; ActionId_t lastAction = 0;
uint8_t stepCount = 0;
}; };
#endif /* SIMPLEACTIONHELPER_H_ */ #endif /* SIMPLEACTIONHELPER_H_ */

View File

@ -1,60 +1,57 @@
#include "fsfw/ipc/CommandMessage.h"
#include "fsfw/storagemanager/storeAddress.h"
#include "fsfw/cfdp/CFDPHandler.h" #include "fsfw/cfdp/CFDPHandler.h"
#include "fsfw/cfdp/CFDPMessage.h"
#include "fsfw/tmtcservices/AcceptsTelemetryIF.h" #include "fsfw/cfdp/CFDPMessage.h"
#include "fsfw/ipc/CommandMessage.h"
#include "fsfw/ipc/QueueFactory.h" #include "fsfw/ipc/QueueFactory.h"
#include "fsfw/objectmanager/ObjectManager.h" #include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/storagemanager/storeAddress.h"
#include "fsfw/tmtcservices/AcceptsTelemetryIF.h"
object_id_t CFDPHandler::packetSource = 0; object_id_t CFDPHandler::packetSource = 0;
object_id_t CFDPHandler::packetDestination = 0; object_id_t CFDPHandler::packetDestination = 0;
CFDPHandler::CFDPHandler(object_id_t setObjectId, CFDPDistributor* dist) : SystemObject(setObjectId) { CFDPHandler::CFDPHandler(object_id_t setObjectId, CFDPDistributor* dist)
requestQueue = QueueFactory::instance()->createMessageQueue(CFDP_HANDLER_MAX_RECEPTION); : SystemObject(setObjectId) {
distributor = dist; requestQueue = QueueFactory::instance()->createMessageQueue(CFDP_HANDLER_MAX_RECEPTION);
distributor = dist;
} }
CFDPHandler::~CFDPHandler() {} CFDPHandler::~CFDPHandler() {}
ReturnValue_t CFDPHandler::initialize() { ReturnValue_t CFDPHandler::initialize() {
ReturnValue_t result = SystemObject::initialize(); ReturnValue_t result = SystemObject::initialize();
if (result != RETURN_OK) { if (result != RETURN_OK) {
return result; return result;
} }
this->distributor->registerHandler(this); this->distributor->registerHandler(this);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t CFDPHandler::handleRequest(store_address_t storeId) { ReturnValue_t CFDPHandler::handleRequest(store_address_t storeId) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "CFDPHandler::handleRequest" << std::endl; sif::debug << "CFDPHandler::handleRequest" << std::endl;
#else #else
sif::printDebug("CFDPHandler::handleRequest\n"); sif::printDebug("CFDPHandler::handleRequest\n");
#endif /* !FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* !FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif #endif
//TODO read out packet from store using storeId // TODO read out packet from store using storeId
return RETURN_OK; return RETURN_OK;
} }
ReturnValue_t CFDPHandler::performOperation(uint8_t opCode) { ReturnValue_t CFDPHandler::performOperation(uint8_t opCode) {
ReturnValue_t status = RETURN_OK; ReturnValue_t status = RETURN_OK;
CommandMessage currentMessage; CommandMessage currentMessage;
for (status = this->requestQueue->receiveMessage(&currentMessage); status == RETURN_OK; for (status = this->requestQueue->receiveMessage(&currentMessage); status == RETURN_OK;
status = this->requestQueue->receiveMessage(&currentMessage)) { status = this->requestQueue->receiveMessage(&currentMessage)) {
store_address_t storeId = CFDPMessage::getStoreId(&currentMessage); store_address_t storeId = CFDPMessage::getStoreId(&currentMessage);
this->handleRequest(storeId); this->handleRequest(storeId);
} }
return RETURN_OK; return RETURN_OK;
} }
uint16_t CFDPHandler::getIdentifier() { uint16_t CFDPHandler::getIdentifier() { return 0; }
return 0;
}
MessageQueueId_t CFDPHandler::getRequestQueue() { MessageQueueId_t CFDPHandler::getRequestQueue() { return this->requestQueue->getId(); }
return this->requestQueue->getId();
}

View File

@ -1,62 +1,63 @@
#ifndef FSFW_CFDP_CFDPHANDLER_H_ #ifndef FSFW_CFDP_CFDPHANDLER_H_
#define FSFW_CFDP_CFDPHANDLER_H_ #define FSFW_CFDP_CFDPHANDLER_H_
#include "fsfw/tmtcservices/AcceptsTelecommandsIF.h" #include "fsfw/ipc/MessageQueueIF.h"
#include "fsfw/objectmanager/SystemObject.h" #include "fsfw/objectmanager/SystemObject.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "fsfw/tasks/ExecutableObjectIF.h" #include "fsfw/tasks/ExecutableObjectIF.h"
#include "fsfw/tcdistribution/CFDPDistributor.h" #include "fsfw/tcdistribution/CFDPDistributor.h"
#include "fsfw/tmtcservices/AcceptsTelecommandsIF.h"
#include "fsfw/ipc/MessageQueueIF.h" namespace Factory {
namespace Factory{
void setStaticFrameworkObjectIds(); void setStaticFrameworkObjectIds();
} }
class CFDPHandler : class CFDPHandler : public ExecutableObjectIF,
public ExecutableObjectIF, public AcceptsTelecommandsIF,
public AcceptsTelecommandsIF, public SystemObject,
public SystemObject, public HasReturnvaluesIF {
public HasReturnvaluesIF { friend void(Factory::setStaticFrameworkObjectIds)();
friend void (Factory::setStaticFrameworkObjectIds)();
public:
CFDPHandler(object_id_t setObjectId, CFDPDistributor* distributor);
/**
* The destructor is empty.
*/
virtual ~CFDPHandler();
virtual ReturnValue_t handleRequest(store_address_t storeId); public:
CFDPHandler(object_id_t setObjectId, CFDPDistributor* distributor);
/**
* The destructor is empty.
*/
virtual ~CFDPHandler();
virtual ReturnValue_t initialize() override; virtual ReturnValue_t handleRequest(store_address_t storeId);
virtual uint16_t getIdentifier() override;
MessageQueueId_t getRequestQueue() override;
ReturnValue_t performOperation(uint8_t opCode) override;
protected:
/**
* This is a complete instance of the telecommand reception queue
* of the class. It is initialized on construction of the class.
*/
MessageQueueIF* requestQueue = nullptr;
CFDPDistributor* distributor = nullptr; virtual ReturnValue_t initialize() override;
virtual uint16_t getIdentifier() override;
MessageQueueId_t getRequestQueue() override;
ReturnValue_t performOperation(uint8_t opCode) override;
/** protected:
* The current CFDP packet to be processed. /**
* It is deleted after handleRequest was executed. * This is a complete instance of the telecommand reception queue
*/ * of the class. It is initialized on construction of the class.
CFDPPacketStored currentPacket; */
MessageQueueIF* requestQueue = nullptr;
static object_id_t packetSource; CFDPDistributor* distributor = nullptr;
static object_id_t packetDestination; /**
private: * The current CFDP packet to be processed.
/** * It is deleted after handleRequest was executed.
* This constant sets the maximum number of packets accepted per call. */
* Remember that one packet must be completely handled in one CFDPPacketStored currentPacket;
* #handleRequest call.
*/ static object_id_t packetSource;
static const uint8_t CFDP_HANDLER_MAX_RECEPTION = 100;
static object_id_t packetDestination;
private:
/**
* This constant sets the maximum number of packets accepted per call.
* Remember that one packet must be completely handled in one
* #handleRequest call.
*/
static const uint8_t CFDP_HANDLER_MAX_RECEPTION = 100;
}; };
#endif /* FSFW_CFDP_CFDPHANDLER_H_ */ #endif /* FSFW_CFDP_CFDPHANDLER_H_ */

View File

@ -1,21 +1,17 @@
#include "CFDPMessage.h" #include "CFDPMessage.h"
CFDPMessage::CFDPMessage() { CFDPMessage::CFDPMessage() {}
}
CFDPMessage::~CFDPMessage() { CFDPMessage::~CFDPMessage() {}
}
void CFDPMessage::setCommand(CommandMessage *message, void CFDPMessage::setCommand(CommandMessage *message, store_address_t cfdpPacket) {
store_address_t cfdpPacket) { message->setParameter(cfdpPacket.raw);
message->setParameter(cfdpPacket.raw);
} }
store_address_t CFDPMessage::getStoreId(const CommandMessage *message) { store_address_t CFDPMessage::getStoreId(const CommandMessage *message) {
store_address_t storeAddressCFDPPacket; store_address_t storeAddressCFDPPacket;
storeAddressCFDPPacket = message->getParameter(); storeAddressCFDPPacket = message->getParameter();
return storeAddressCFDPPacket; return storeAddressCFDPPacket;
} }
void CFDPMessage::clear(CommandMessage *message) { void CFDPMessage::clear(CommandMessage *message) {}
}

View File

@ -6,18 +6,18 @@
#include "fsfw/storagemanager/StorageManagerIF.h" #include "fsfw/storagemanager/StorageManagerIF.h"
class CFDPMessage { class CFDPMessage {
private: private:
CFDPMessage(); CFDPMessage();
public:
static const uint8_t MESSAGE_ID = messagetypes::CFDP;
virtual ~CFDPMessage(); public:
static void setCommand(CommandMessage* message, static const uint8_t MESSAGE_ID = messagetypes::CFDP;
store_address_t cfdpPacket);
static store_address_t getStoreId(const CommandMessage* message); virtual ~CFDPMessage();
static void setCommand(CommandMessage* message, store_address_t cfdpPacket);
static void clear(CommandMessage* message); static store_address_t getStoreId(const CommandMessage* message);
static void clear(CommandMessage* message);
}; };
#endif /* FSFW_CFDP_CFDPMESSAGE_H_ */ #endif /* FSFW_CFDP_CFDPMESSAGE_H_ */

View File

@ -6,78 +6,73 @@
namespace cfdp { namespace cfdp {
struct FileSize: public SerializeIF { struct FileSize : public SerializeIF {
public: public:
FileSize(): largeFile(false) {}; FileSize() : largeFile(false){};
FileSize(uint64_t fileSize, bool isLarge = false) { FileSize(uint64_t fileSize, bool isLarge = false) { setFileSize(fileSize, isLarge); };
setFileSize(fileSize, isLarge);
};
ReturnValue_t serialize(bool isLarge, uint8_t **buffer, size_t *size,size_t maxSize, ReturnValue_t serialize(bool isLarge, uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) { Endianness streamEndianness) {
this->largeFile = isLarge; this->largeFile = isLarge;
return serialize(buffer, size, maxSize, streamEndianness); return serialize(buffer, size, maxSize, streamEndianness);
}
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const override {
if (not largeFile) {
uint32_t fileSizeTyped = fileSize;
return SerializeAdapter::serialize(&fileSizeTyped, buffer, size, maxSize, streamEndianness);
} }
return SerializeAdapter::serialize(&fileSize, buffer, size, maxSize, streamEndianness);
}
ReturnValue_t serialize(uint8_t **buffer, size_t *size,size_t maxSize, size_t getSerializedSize() const override {
Endianness streamEndianness) const override { if (largeFile) {
if(not largeFile) { return 8;
uint32_t fileSizeTyped = fileSize; } else {
return SerializeAdapter::serialize(&fileSizeTyped, buffer, size, maxSize, return 4;
streamEndianness); }
}
} ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
return SerializeAdapter::serialize(&fileSize, buffer, size, maxSize, streamEndianness); Endianness streamEndianness) override {
if (largeFile) {
return SerializeAdapter::deSerialize(&size, buffer, size, streamEndianness);
} else {
uint32_t sizeTmp = 0;
ReturnValue_t result =
SerializeAdapter::deSerialize(&sizeTmp, buffer, size, streamEndianness);
if (result == HasReturnvaluesIF::RETURN_OK) {
fileSize = sizeTmp;
}
return result;
} }
}
size_t getSerializedSize() const override { ReturnValue_t setFileSize(uint64_t fileSize, bool largeFile) {
if (largeFile) { if (not largeFile and fileSize > UINT32_MAX) {
return 8; // TODO: emit warning here
} else { return HasReturnvaluesIF::RETURN_FAILED;
return 4;
}
} }
this->fileSize = fileSize;
this->largeFile = largeFile;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, bool isLargeFile() const { return largeFile; }
Endianness streamEndianness) override { uint64_t getSize(bool *largeFile = nullptr) const {
if(largeFile) { if (largeFile != nullptr) {
return SerializeAdapter::deSerialize(&size, buffer, size, streamEndianness); *largeFile = this->largeFile;
} else {
uint32_t sizeTmp = 0;
ReturnValue_t result = SerializeAdapter::deSerialize(&sizeTmp, buffer, size,
streamEndianness);
if(result == HasReturnvaluesIF::RETURN_OK) {
fileSize = sizeTmp;
}
return result;
}
} }
return fileSize;
}
ReturnValue_t setFileSize(uint64_t fileSize, bool largeFile) { private:
if (not largeFile and fileSize > UINT32_MAX) { uint64_t fileSize = 0;
// TODO: emit warning here bool largeFile = false;
return HasReturnvaluesIF::RETURN_FAILED;
}
this->fileSize = fileSize;
this->largeFile = largeFile;
return HasReturnvaluesIF::RETURN_OK;
}
bool isLargeFile() const {
return largeFile;
}
uint64_t getSize(bool* largeFile = nullptr) const {
if(largeFile != nullptr) {
*largeFile = this->largeFile;
}
return fileSize;
}
private:
uint64_t fileSize = 0;
bool largeFile = false;
}; };
} } // namespace cfdp
#endif /* FSFW_SRC_FSFW_CFDP_FILESIZE_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_FILESIZE_H_ */

View File

@ -2,10 +2,12 @@
#define FSFW_SRC_FSFW_CFDP_PDU_DEFINITIONS_H_ #define FSFW_SRC_FSFW_CFDP_PDU_DEFINITIONS_H_
#include <fsfw/serialize/SerializeIF.h> #include <fsfw/serialize/SerializeIF.h>
#include <cstdint>
#include <cstddef> #include <cstddef>
#include "fsfw/returnvalues/HasReturnvaluesIF.h" #include <cstdint>
#include "fsfw/returnvalues/FwClassIds.h" #include "fsfw/returnvalues/FwClassIds.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
namespace cfdp { namespace cfdp {
@ -14,140 +16,122 @@ static constexpr uint8_t VERSION_BITS = 0b00100000;
static constexpr uint8_t CFDP_CLASS_ID = CLASS_ID::CFDP; static constexpr uint8_t CFDP_CLASS_ID = CLASS_ID::CFDP;
static constexpr ReturnValue_t INVALID_TLV_TYPE = static constexpr ReturnValue_t INVALID_TLV_TYPE =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 1); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 1);
static constexpr ReturnValue_t INVALID_DIRECTIVE_FIELDS = static constexpr ReturnValue_t INVALID_DIRECTIVE_FIELDS =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 2); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 2);
static constexpr ReturnValue_t INVALID_PDU_DATAFIELD_LEN = static constexpr ReturnValue_t INVALID_PDU_DATAFIELD_LEN =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 3); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 3);
static constexpr ReturnValue_t INVALID_ACK_DIRECTIVE_FIELDS = static constexpr ReturnValue_t INVALID_ACK_DIRECTIVE_FIELDS =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 4); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 4);
//! Can not parse options. This can also occur because there are options //! Can not parse options. This can also occur because there are options
//! available but the user did not pass a valid options array //! available but the user did not pass a valid options array
static constexpr ReturnValue_t METADATA_CANT_PARSE_OPTIONS = static constexpr ReturnValue_t METADATA_CANT_PARSE_OPTIONS =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 5); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 5);
static constexpr ReturnValue_t NAK_CANT_PARSE_OPTIONS = static constexpr ReturnValue_t NAK_CANT_PARSE_OPTIONS =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 6); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 6);
static constexpr ReturnValue_t FINISHED_CANT_PARSE_FS_RESPONSES = static constexpr ReturnValue_t FINISHED_CANT_PARSE_FS_RESPONSES =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 6); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 6);
static constexpr ReturnValue_t FILESTORE_REQUIRES_SECOND_FILE = static constexpr ReturnValue_t FILESTORE_REQUIRES_SECOND_FILE =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 8); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 8);
//! Can not parse filestore response because user did not pass a valid instance //! Can not parse filestore response because user did not pass a valid instance
//! or remaining size is invalid //! or remaining size is invalid
static constexpr ReturnValue_t FILESTORE_RESPONSE_CANT_PARSE_FS_MESSAGE = static constexpr ReturnValue_t FILESTORE_RESPONSE_CANT_PARSE_FS_MESSAGE =
HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 9); HasReturnvaluesIF::makeReturnCode(CFDP_CLASS_ID, 9);
//! Checksum types according to the SANA Checksum Types registry //! Checksum types according to the SANA Checksum Types registry
//! https://sanaregistry.org/r/checksum_identifiers/ //! https://sanaregistry.org/r/checksum_identifiers/
enum ChecksumType { enum ChecksumType {
// Modular legacy checksum // Modular legacy checksum
MODULAR = 0, MODULAR = 0,
CRC_32_PROXIMITY_1 = 1, CRC_32_PROXIMITY_1 = 1,
CRC_32C = 2, CRC_32C = 2,
CRC_32 = 3, CRC_32 = 3,
NULL_CHECKSUM = 15 NULL_CHECKSUM = 15
}; };
enum PduType: bool { enum PduType : bool { FILE_DIRECTIVE = 0, FILE_DATA = 1 };
FILE_DIRECTIVE = 0,
FILE_DATA = 1 enum TransmissionModes : bool { ACKNOWLEDGED = 0, UNACKNOWLEDGED = 1 };
enum SegmentMetadataFlag : bool { NOT_PRESENT = 0, PRESENT = 1 };
enum Direction : bool { TOWARDS_RECEIVER = 0, TOWARDS_SENDER = 1 };
enum SegmentationControl : bool {
NO_RECORD_BOUNDARIES_PRESERVATION = 0,
RECORD_BOUNDARIES_PRESERVATION = 1
}; };
enum TransmissionModes: bool { enum WidthInBytes : uint8_t {
ACKNOWLEDGED = 0, // Only those are supported for now
UNACKNOWLEDGED = 1 ONE_BYTE = 1,
TWO_BYTES = 2,
FOUR_BYTES = 4,
}; };
enum SegmentMetadataFlag: bool { enum FileDirectives : uint8_t {
NOT_PRESENT = 0, INVALID_DIRECTIVE = 0x0f,
PRESENT = 1 EOF_DIRECTIVE = 0x04,
FINISH = 0x05,
ACK = 0x06,
METADATA = 0x07,
NAK = 0x08,
PROMPT = 0x09,
KEEP_ALIVE = 0x0c
}; };
enum Direction: bool { enum ConditionCode : uint8_t {
TOWARDS_RECEIVER = 0, NO_CONDITION_FIELD = 0xff,
TOWARDS_SENDER = 1 NO_ERROR = 0b0000,
}; POSITIVE_ACK_LIMIT_REACHED = 0b0001,
KEEP_ALIVE_LIMIT_REACHED = 0b0010,
enum SegmentationControl: bool { INVALID_TRANSMISSION_MODE = 0b0011,
NO_RECORD_BOUNDARIES_PRESERVATION = 0, FILESTORE_REJECTION = 0b0100,
RECORD_BOUNDARIES_PRESERVATION = 1 FILE_CHECKSUM_FAILURE = 0b0101,
}; FILE_SIZE_ERROR = 0b0110,
NAK_LIMIT_REACHED = 0b0111,
enum WidthInBytes: uint8_t { INACTIVITY_DETECTED = 0b1000,
// Only those are supported for now CHECK_LIMIT_REACHED = 0b1010,
ONE_BYTE = 1, UNSUPPORTED_CHECKSUM_TYPE = 0b1011,
TWO_BYTES = 2, SUSPEND_REQUEST_RECEIVED = 0b1110,
FOUR_BYTES = 4, CANCEL_REQUEST_RECEIVED = 0b1111
};
enum FileDirectives: uint8_t {
INVALID_DIRECTIVE = 0x0f,
EOF_DIRECTIVE = 0x04,
FINISH = 0x05,
ACK = 0x06,
METADATA = 0x07,
NAK = 0x08,
PROMPT = 0x09,
KEEP_ALIVE = 0x0c
};
enum ConditionCode: uint8_t {
NO_CONDITION_FIELD = 0xff,
NO_ERROR = 0b0000,
POSITIVE_ACK_LIMIT_REACHED = 0b0001,
KEEP_ALIVE_LIMIT_REACHED = 0b0010,
INVALID_TRANSMISSION_MODE = 0b0011,
FILESTORE_REJECTION = 0b0100,
FILE_CHECKSUM_FAILURE = 0b0101,
FILE_SIZE_ERROR = 0b0110,
NAK_LIMIT_REACHED = 0b0111,
INACTIVITY_DETECTED = 0b1000,
CHECK_LIMIT_REACHED = 0b1010,
UNSUPPORTED_CHECKSUM_TYPE = 0b1011,
SUSPEND_REQUEST_RECEIVED = 0b1110,
CANCEL_REQUEST_RECEIVED = 0b1111
}; };
enum AckTransactionStatus { enum AckTransactionStatus {
UNDEFINED = 0b00, UNDEFINED = 0b00,
ACTIVE = 0b01, ACTIVE = 0b01,
TERMINATED = 0b10, TERMINATED = 0b10,
UNRECOGNIZED = 0b11 UNRECOGNIZED = 0b11
}; };
enum FinishedDeliveryCode { enum FinishedDeliveryCode { DATA_COMPLETE = 0, DATA_INCOMPLETE = 1 };
DATA_COMPLETE = 0,
DATA_INCOMPLETE = 1
};
enum FinishedFileStatus { enum FinishedFileStatus {
DISCARDED_DELIBERATELY = 0, DISCARDED_DELIBERATELY = 0,
DISCARDED_FILESTORE_REJECTION = 1, DISCARDED_FILESTORE_REJECTION = 1,
RETAINED_IN_FILESTORE = 2, RETAINED_IN_FILESTORE = 2,
FILE_STATUS_UNREPORTED = 3 FILE_STATUS_UNREPORTED = 3
}; };
enum PromptResponseRequired: bool { enum PromptResponseRequired : bool { PROMPT_NAK = 0, PROMPT_KEEP_ALIVE = 1 };
PROMPT_NAK = 0,
PROMPT_KEEP_ALIVE = 1
};
enum TlvTypes: uint8_t { enum TlvTypes : uint8_t {
FILESTORE_REQUEST = 0x00, FILESTORE_REQUEST = 0x00,
FILESTORE_RESPONSE = 0x01, FILESTORE_RESPONSE = 0x01,
MSG_TO_USER = 0x02, MSG_TO_USER = 0x02,
FAULT_HANDLER = 0x04, FAULT_HANDLER = 0x04,
FLOW_LABEL = 0x05, FLOW_LABEL = 0x05,
ENTITY_ID = 0x06, ENTITY_ID = 0x06,
INVALID_TLV = 0xff, INVALID_TLV = 0xff,
}; };
enum RecordContinuationState { enum RecordContinuationState {
NO_START_NO_END = 0b00, NO_START_NO_END = 0b00,
CONTAINS_START_NO_END = 0b01, CONTAINS_START_NO_END = 0b01,
CONTAINS_END_NO_START = 0b10, CONTAINS_END_NO_START = 0b10,
CONTAINS_START_AND_END = 0b11 CONTAINS_START_AND_END = 0b11
}; };
} } // namespace cfdp
#endif /* FSFW_SRC_FSFW_CFDP_PDU_DEFINITIONS_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_DEFINITIONS_H_ */

View File

@ -1,52 +1,45 @@
#include "AckInfo.h" #include "AckInfo.h"
AckInfo::AckInfo(cfdp::FileDirectives ackedDirective, cfdp::ConditionCode ackedConditionCode, AckInfo::AckInfo(cfdp::FileDirectives ackedDirective, cfdp::ConditionCode ackedConditionCode,
cfdp::AckTransactionStatus transactionStatus, uint8_t directiveSubtypeCode): cfdp::AckTransactionStatus transactionStatus, uint8_t directiveSubtypeCode)
ackedDirective(ackedDirective), ackedConditionCode(ackedConditionCode), : ackedDirective(ackedDirective),
transactionStatus(transactionStatus), directiveSubtypeCode(directiveSubtypeCode) { ackedConditionCode(ackedConditionCode),
if (ackedDirective == cfdp::FileDirectives::FINISH) { transactionStatus(transactionStatus),
this->directiveSubtypeCode = 0b0001; directiveSubtypeCode(directiveSubtypeCode) {
} else { if (ackedDirective == cfdp::FileDirectives::FINISH) {
this->directiveSubtypeCode = 0b0000; this->directiveSubtypeCode = 0b0001;
} } else {
this->directiveSubtypeCode = 0b0000;
}
} }
cfdp::ConditionCode AckInfo::getAckedConditionCode() const { cfdp::ConditionCode AckInfo::getAckedConditionCode() const { return ackedConditionCode; }
return ackedConditionCode;
}
void AckInfo::setAckedConditionCode(cfdp::ConditionCode ackedConditionCode) { void AckInfo::setAckedConditionCode(cfdp::ConditionCode ackedConditionCode) {
this->ackedConditionCode = ackedConditionCode; this->ackedConditionCode = ackedConditionCode;
if (ackedDirective == cfdp::FileDirectives::FINISH) { if (ackedDirective == cfdp::FileDirectives::FINISH) {
this->directiveSubtypeCode = 0b0001; this->directiveSubtypeCode = 0b0001;
} else { } else {
this->directiveSubtypeCode = 0b0000; this->directiveSubtypeCode = 0b0000;
} }
} }
cfdp::FileDirectives AckInfo::getAckedDirective() const { cfdp::FileDirectives AckInfo::getAckedDirective() const { return ackedDirective; }
return ackedDirective;
}
void AckInfo::setAckedDirective(cfdp::FileDirectives ackedDirective) { void AckInfo::setAckedDirective(cfdp::FileDirectives ackedDirective) {
this->ackedDirective = ackedDirective; this->ackedDirective = ackedDirective;
} }
uint8_t AckInfo::getDirectiveSubtypeCode() const { uint8_t AckInfo::getDirectiveSubtypeCode() const { return directiveSubtypeCode; }
return directiveSubtypeCode;
}
void AckInfo::setDirectiveSubtypeCode(uint8_t directiveSubtypeCode) { void AckInfo::setDirectiveSubtypeCode(uint8_t directiveSubtypeCode) {
this->directiveSubtypeCode = directiveSubtypeCode; this->directiveSubtypeCode = directiveSubtypeCode;
} }
cfdp::AckTransactionStatus AckInfo::getTransactionStatus() const { cfdp::AckTransactionStatus AckInfo::getTransactionStatus() const { return transactionStatus; }
return transactionStatus;
}
AckInfo::AckInfo() { AckInfo::AckInfo() {}
}
void AckInfo::setTransactionStatus(cfdp::AckTransactionStatus transactionStatus) { void AckInfo::setTransactionStatus(cfdp::AckTransactionStatus transactionStatus) {
this->transactionStatus = transactionStatus; this->transactionStatus = transactionStatus;
} }

View File

@ -4,30 +4,28 @@
#include "../definitions.h" #include "../definitions.h"
class AckInfo { class AckInfo {
public: public:
AckInfo(); AckInfo();
AckInfo(cfdp::FileDirectives ackedDirective, cfdp::ConditionCode ackedConditionCode, AckInfo(cfdp::FileDirectives ackedDirective, cfdp::ConditionCode ackedConditionCode,
cfdp::AckTransactionStatus transactionStatus, uint8_t directiveSubtypeCode = 0); cfdp::AckTransactionStatus transactionStatus, uint8_t directiveSubtypeCode = 0);
cfdp::ConditionCode getAckedConditionCode() const; cfdp::ConditionCode getAckedConditionCode() const;
void setAckedConditionCode(cfdp::ConditionCode ackedConditionCode); void setAckedConditionCode(cfdp::ConditionCode ackedConditionCode);
cfdp::FileDirectives getAckedDirective() const; cfdp::FileDirectives getAckedDirective() const;
void setAckedDirective(cfdp::FileDirectives ackedDirective); void setAckedDirective(cfdp::FileDirectives ackedDirective);
uint8_t getDirectiveSubtypeCode() const; uint8_t getDirectiveSubtypeCode() const;
void setDirectiveSubtypeCode(uint8_t directiveSubtypeCode); void setDirectiveSubtypeCode(uint8_t directiveSubtypeCode);
cfdp::AckTransactionStatus getTransactionStatus() const; cfdp::AckTransactionStatus getTransactionStatus() const;
void setTransactionStatus(cfdp::AckTransactionStatus transactionStatus); void setTransactionStatus(cfdp::AckTransactionStatus transactionStatus);
private: private:
cfdp::FileDirectives ackedDirective = cfdp::FileDirectives::INVALID_DIRECTIVE; cfdp::FileDirectives ackedDirective = cfdp::FileDirectives::INVALID_DIRECTIVE;
cfdp::ConditionCode ackedConditionCode = cfdp::ConditionCode::NO_CONDITION_FIELD; cfdp::ConditionCode ackedConditionCode = cfdp::ConditionCode::NO_CONDITION_FIELD;
cfdp::AckTransactionStatus transactionStatus = cfdp::AckTransactionStatus::UNDEFINED; cfdp::AckTransactionStatus transactionStatus = cfdp::AckTransactionStatus::UNDEFINED;
uint8_t directiveSubtypeCode = 0; uint8_t directiveSubtypeCode = 0;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_ACKINFO_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_ACKINFO_H_ */

View File

@ -1,38 +1,37 @@
#include "AckPduDeserializer.h" #include "AckPduDeserializer.h"
AckPduDeserializer::AckPduDeserializer(const uint8_t *pduBuf, size_t maxSize, AckInfo& info): AckPduDeserializer::AckPduDeserializer(const uint8_t* pduBuf, size_t maxSize, AckInfo& info)
FileDirectiveDeserializer(pduBuf, maxSize), info(info) { : FileDirectiveDeserializer(pduBuf, maxSize), info(info) {}
}
ReturnValue_t AckPduDeserializer::parseData() { ReturnValue_t AckPduDeserializer::parseData() {
ReturnValue_t result = FileDirectiveDeserializer::parseData(); ReturnValue_t result = FileDirectiveDeserializer::parseData();
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
size_t currentIdx = FileDirectiveDeserializer::getHeaderSize(); size_t currentIdx = FileDirectiveDeserializer::getHeaderSize();
if (currentIdx + 2 > this->maxSize) { if (currentIdx + 2 > this->maxSize) {
return SerializeIF::BUFFER_TOO_SHORT; return SerializeIF::BUFFER_TOO_SHORT;
} }
if(not checkAndSetCodes(rawPtr[currentIdx], rawPtr[currentIdx + 1])) { if (not checkAndSetCodes(rawPtr[currentIdx], rawPtr[currentIdx + 1])) {
return cfdp::INVALID_ACK_DIRECTIVE_FIELDS; return cfdp::INVALID_ACK_DIRECTIVE_FIELDS;
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
bool AckPduDeserializer::checkAndSetCodes(uint8_t firstByte, uint8_t secondByte) { bool AckPduDeserializer::checkAndSetCodes(uint8_t firstByte, uint8_t secondByte) {
uint8_t ackedDirective = static_cast<cfdp::FileDirectives>(firstByte >> 4); uint8_t ackedDirective = static_cast<cfdp::FileDirectives>(firstByte >> 4);
if(ackedDirective != cfdp::FileDirectives::EOF_DIRECTIVE and if (ackedDirective != cfdp::FileDirectives::EOF_DIRECTIVE and
ackedDirective != cfdp::FileDirectives::FINISH) { ackedDirective != cfdp::FileDirectives::FINISH) {
return false; return false;
} }
this->info.setAckedDirective(static_cast<cfdp::FileDirectives>(ackedDirective)); this->info.setAckedDirective(static_cast<cfdp::FileDirectives>(ackedDirective));
uint8_t directiveSubtypeCode = firstByte & 0x0f; uint8_t directiveSubtypeCode = firstByte & 0x0f;
if(directiveSubtypeCode != 0b0000 and directiveSubtypeCode != 0b0001) { if (directiveSubtypeCode != 0b0000 and directiveSubtypeCode != 0b0001) {
return false; return false;
} }
this->info.setDirectiveSubtypeCode(directiveSubtypeCode); this->info.setDirectiveSubtypeCode(directiveSubtypeCode);
this->info.setAckedConditionCode(static_cast<cfdp::ConditionCode>(secondByte >> 4)); this->info.setAckedConditionCode(static_cast<cfdp::ConditionCode>(secondByte >> 4));
this->info.setTransactionStatus(static_cast<cfdp::AckTransactionStatus>(secondByte & 0x0f)); this->info.setTransactionStatus(static_cast<cfdp::AckTransactionStatus>(secondByte & 0x0f));
return true; return true;
} }

View File

@ -1,26 +1,23 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_ACKPDUDESERIALIZER_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_ACKPDUDESERIALIZER_H_
#define FSFW_SRC_FSFW_CFDP_PDU_ACKPDUDESERIALIZER_H_ #define FSFW_SRC_FSFW_CFDP_PDU_ACKPDUDESERIALIZER_H_
#include "fsfw/cfdp/pdu/FileDirectiveDeserializer.h"
#include "AckInfo.h" #include "AckInfo.h"
#include "fsfw/cfdp/pdu/FileDirectiveDeserializer.h"
class AckPduDeserializer: public FileDirectiveDeserializer { class AckPduDeserializer : public FileDirectiveDeserializer {
public: public:
AckPduDeserializer(const uint8_t* pduBuf, size_t maxSize, AckInfo& info); AckPduDeserializer(const uint8_t* pduBuf, size_t maxSize, AckInfo& info);
/** /**
* *
* @return * @return
* - cfdp::INVALID_DIRECTIVE_FIELDS: Invalid fields * - cfdp::INVALID_DIRECTIVE_FIELDS: Invalid fields
*/ */
ReturnValue_t parseData(); ReturnValue_t parseData();
private:
bool checkAndSetCodes(uint8_t rawAckedByte, uint8_t rawAckedConditionCode);
AckInfo& info;
private:
bool checkAndSetCodes(uint8_t rawAckedByte, uint8_t rawAckedConditionCode);
AckInfo& info;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_ACKPDUDESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_ACKPDUDESERIALIZER_H_ */

View File

@ -1,38 +1,36 @@
#include "AckPduSerializer.h" #include "AckPduSerializer.h"
AckPduSerializer::AckPduSerializer(AckInfo& ackInfo, PduConfig &pduConf): AckPduSerializer::AckPduSerializer(AckInfo &ackInfo, PduConfig &pduConf)
FileDirectiveSerializer(pduConf, cfdp::FileDirectives::ACK, 2), : FileDirectiveSerializer(pduConf, cfdp::FileDirectives::ACK, 2), ackInfo(ackInfo) {}
ackInfo(ackInfo) {
}
size_t AckPduSerializer::getSerializedSize() const { size_t AckPduSerializer::getSerializedSize() const {
return FileDirectiveSerializer::getWholePduSize(); return FileDirectiveSerializer::getWholePduSize();
} }
ReturnValue_t AckPduSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize, ReturnValue_t AckPduSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const { Endianness streamEndianness) const {
ReturnValue_t result = FileDirectiveSerializer::serialize(buffer, size, maxSize, ReturnValue_t result =
streamEndianness); FileDirectiveSerializer::serialize(buffer, size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
cfdp::FileDirectives ackedDirective = ackInfo.getAckedDirective(); cfdp::FileDirectives ackedDirective = ackInfo.getAckedDirective();
uint8_t directiveSubtypeCode = ackInfo.getDirectiveSubtypeCode(); uint8_t directiveSubtypeCode = ackInfo.getDirectiveSubtypeCode();
cfdp::ConditionCode ackedConditionCode = ackInfo.getAckedConditionCode(); cfdp::ConditionCode ackedConditionCode = ackInfo.getAckedConditionCode();
cfdp::AckTransactionStatus transactionStatus = ackInfo.getTransactionStatus(); cfdp::AckTransactionStatus transactionStatus = ackInfo.getTransactionStatus();
if(ackedDirective != cfdp::FileDirectives::FINISH and if (ackedDirective != cfdp::FileDirectives::FINISH and
ackedDirective != cfdp::FileDirectives::EOF_DIRECTIVE) { ackedDirective != cfdp::FileDirectives::EOF_DIRECTIVE) {
// TODO: better returncode // TODO: better returncode
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
if(*size + 2 > maxSize) { if (*size + 2 > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT; return SerializeIF::BUFFER_TOO_SHORT;
} }
**buffer = ackedDirective << 4 | directiveSubtypeCode; **buffer = ackedDirective << 4 | directiveSubtypeCode;
*buffer += 1; *buffer += 1;
*size += 1; *size += 1;
**buffer = ackedConditionCode << 4 | transactionStatus; **buffer = ackedConditionCode << 4 | transactionStatus;
*buffer += 1; *buffer += 1;
*size += 1; *size += 1;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -5,26 +5,26 @@
#include "FileDirectiveDeserializer.h" #include "FileDirectiveDeserializer.h"
#include "FileDirectiveSerializer.h" #include "FileDirectiveSerializer.h"
class AckPduSerializer: public FileDirectiveSerializer { class AckPduSerializer : public FileDirectiveSerializer {
public: public:
/** /**
* @brief Serializer to pack ACK PDUs * @brief Serializer to pack ACK PDUs
* @details * @details
* Please note that only Finished PDUs and EOF are acknowledged. * Please note that only Finished PDUs and EOF are acknowledged.
* @param ackedDirective * @param ackedDirective
* @param ackedConditionCode * @param ackedConditionCode
* @param transactionStatus * @param transactionStatus
* @param pduConf * @param pduConf
*/ */
AckPduSerializer(AckInfo& ackInfo, PduConfig& pduConf); AckPduSerializer(AckInfo& ackInfo, PduConfig& pduConf);
size_t getSerializedSize() const override; size_t getSerializedSize() const override;
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override; Endianness streamEndianness) const override;
private: private:
AckInfo& ackInfo; AckInfo& ackInfo;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_ACKPDUSERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_ACKPDUSERIALIZER_H_ */

View File

@ -1,58 +1,47 @@
#include "EofInfo.h" #include "EofInfo.h"
EofInfo::EofInfo(cfdp::ConditionCode conditionCode, uint32_t checksum, cfdp::FileSize fileSize, EofInfo::EofInfo(cfdp::ConditionCode conditionCode, uint32_t checksum, cfdp::FileSize fileSize,
EntityIdTlv* faultLoc): conditionCode(conditionCode), checksum(checksum), EntityIdTlv* faultLoc)
fileSize(fileSize), faultLoc(faultLoc) { : conditionCode(conditionCode), checksum(checksum), fileSize(fileSize), faultLoc(faultLoc) {}
}
EofInfo::EofInfo(EntityIdTlv *faultLoc): conditionCode(cfdp::ConditionCode::NO_CONDITION_FIELD), EofInfo::EofInfo(EntityIdTlv* faultLoc)
checksum(0), fileSize(0), faultLoc(faultLoc) { : conditionCode(cfdp::ConditionCode::NO_CONDITION_FIELD),
} checksum(0),
fileSize(0),
faultLoc(faultLoc) {}
uint32_t EofInfo::getChecksum() const { uint32_t EofInfo::getChecksum() const { return checksum; }
return checksum;
}
cfdp::ConditionCode EofInfo::getConditionCode() const { cfdp::ConditionCode EofInfo::getConditionCode() const { return conditionCode; }
return conditionCode;
}
EntityIdTlv* EofInfo::getFaultLoc() const { EntityIdTlv* EofInfo::getFaultLoc() const { return faultLoc; }
return faultLoc;
}
cfdp::FileSize& EofInfo::getFileSize() { cfdp::FileSize& EofInfo::getFileSize() { return fileSize; }
return fileSize;
}
void EofInfo::setChecksum(uint32_t checksum) { void EofInfo::setChecksum(uint32_t checksum) { this->checksum = checksum; }
this->checksum = checksum;
}
void EofInfo::setConditionCode(cfdp::ConditionCode conditionCode) { void EofInfo::setConditionCode(cfdp::ConditionCode conditionCode) {
this->conditionCode = conditionCode; this->conditionCode = conditionCode;
} }
void EofInfo::setFaultLoc(EntityIdTlv *faultLoc) { void EofInfo::setFaultLoc(EntityIdTlv* faultLoc) { this->faultLoc = faultLoc; }
this->faultLoc = faultLoc;
}
size_t EofInfo::getSerializedSize(bool fssLarge) { size_t EofInfo::getSerializedSize(bool fssLarge) {
// Condition code + spare + 4 byte checksum // Condition code + spare + 4 byte checksum
size_t size = 5; size_t size = 5;
if(fssLarge) { if (fssLarge) {
size += 8; size += 8;
} else { } else {
size += 4; size += 4;
} }
// Do not account for fault location if the condition code is NO_ERROR. We assume that // Do not account for fault location if the condition code is NO_ERROR. We assume that
// a serializer will not serialize the fault location here. // a serializer will not serialize the fault location here.
if(getFaultLoc() != nullptr and getConditionCode() != cfdp::ConditionCode::NO_ERROR) { if (getFaultLoc() != nullptr and getConditionCode() != cfdp::ConditionCode::NO_ERROR) {
size+= faultLoc->getSerializedSize(); size += faultLoc->getSerializedSize();
} }
return size; return size;
} }
ReturnValue_t EofInfo::setFileSize(size_t fileSize, bool isLarge) { ReturnValue_t EofInfo::setFileSize(size_t fileSize, bool isLarge) {
return this->fileSize.setFileSize(fileSize, isLarge); return this->fileSize.setFileSize(fileSize, isLarge);
} }

View File

@ -1,33 +1,33 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_EOFINFO_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_EOFINFO_H_
#define FSFW_SRC_FSFW_CFDP_PDU_EOFINFO_H_ #define FSFW_SRC_FSFW_CFDP_PDU_EOFINFO_H_
#include "fsfw/cfdp/tlv/EntityIdTlv.h"
#include "../definitions.h"
#include "../FileSize.h" #include "../FileSize.h"
#include "../definitions.h"
#include "fsfw/cfdp/tlv/EntityIdTlv.h"
struct EofInfo { struct EofInfo {
public: public:
EofInfo(EntityIdTlv* faultLoc = nullptr); EofInfo(EntityIdTlv* faultLoc = nullptr);
EofInfo(cfdp::ConditionCode conditionCode, uint32_t checksum, cfdp::FileSize fileSize, EofInfo(cfdp::ConditionCode conditionCode, uint32_t checksum, cfdp::FileSize fileSize,
EntityIdTlv* faultLoc = nullptr); EntityIdTlv* faultLoc = nullptr);
size_t getSerializedSize(bool fssLarge = false); size_t getSerializedSize(bool fssLarge = false);
uint32_t getChecksum() const; uint32_t getChecksum() const;
cfdp::ConditionCode getConditionCode() const; cfdp::ConditionCode getConditionCode() const;
EntityIdTlv* getFaultLoc() const; EntityIdTlv* getFaultLoc() const;
cfdp::FileSize& getFileSize(); cfdp::FileSize& getFileSize();
void setChecksum(uint32_t checksum); void setChecksum(uint32_t checksum);
void setConditionCode(cfdp::ConditionCode conditionCode); void setConditionCode(cfdp::ConditionCode conditionCode);
void setFaultLoc(EntityIdTlv *faultLoc); void setFaultLoc(EntityIdTlv* faultLoc);
ReturnValue_t setFileSize(size_t size, bool isLarge); ReturnValue_t setFileSize(size_t size, bool isLarge);
private:
cfdp::ConditionCode conditionCode; private:
uint32_t checksum; cfdp::ConditionCode conditionCode;
cfdp::FileSize fileSize; uint32_t checksum;
EntityIdTlv* faultLoc = nullptr; cfdp::FileSize fileSize;
EntityIdTlv* faultLoc = nullptr;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_EOFINFO_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_EOFINFO_H_ */

View File

@ -1,68 +1,69 @@
#include "EofPduDeserializer.h" #include "EofPduDeserializer.h"
#include "fsfw/FSFW.h" #include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h" #include "fsfw/serviceinterface.h"
EofPduDeserializer::EofPduDeserializer(const uint8_t *pduBuf, size_t maxSize, EofInfo& eofInfo): EofPduDeserializer::EofPduDeserializer(const uint8_t* pduBuf, size_t maxSize, EofInfo& eofInfo)
FileDirectiveDeserializer(pduBuf, maxSize), info(eofInfo) { : FileDirectiveDeserializer(pduBuf, maxSize), info(eofInfo) {}
}
ReturnValue_t EofPduDeserializer::parseData() { ReturnValue_t EofPduDeserializer::parseData() {
ReturnValue_t result = FileDirectiveDeserializer::parseData(); ReturnValue_t result = FileDirectiveDeserializer::parseData();
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
const uint8_t* bufPtr = rawPtr; const uint8_t* bufPtr = rawPtr;
size_t expectedFileFieldLen = 4; size_t expectedFileFieldLen = 4;
if(this->getLargeFileFlag()) { if (this->getLargeFileFlag()) {
expectedFileFieldLen = 8; expectedFileFieldLen = 8;
} }
size_t currentIdx = FileDirectiveDeserializer::getHeaderSize(); size_t currentIdx = FileDirectiveDeserializer::getHeaderSize();
size_t deserLen = maxSize; size_t deserLen = maxSize;
if(maxSize < currentIdx + 5 + expectedFileFieldLen) { if (maxSize < currentIdx + 5 + expectedFileFieldLen) {
return SerializeIF::STREAM_TOO_SHORT; return SerializeIF::STREAM_TOO_SHORT;
} }
bufPtr += currentIdx; bufPtr += currentIdx;
deserLen -= currentIdx; deserLen -= currentIdx;
info.setConditionCode(static_cast<cfdp::ConditionCode>(*bufPtr >> 4)); info.setConditionCode(static_cast<cfdp::ConditionCode>(*bufPtr >> 4));
bufPtr += 1; bufPtr += 1;
deserLen -= 1; deserLen -= 1;
uint32_t checksum = 0; uint32_t checksum = 0;
auto endianness = getEndianness(); auto endianness = getEndianness();
result = SerializeAdapter::deSerialize(&checksum, &bufPtr, &deserLen, endianness); result = SerializeAdapter::deSerialize(&checksum, &bufPtr, &deserLen, endianness);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
info.setChecksum(checksum); info.setChecksum(checksum);
if(this->getLargeFileFlag()) { if (this->getLargeFileFlag()) {
uint64_t fileSizeValue = 0; uint64_t fileSizeValue = 0;
result = SerializeAdapter::deSerialize(&fileSizeValue, &bufPtr, &deserLen, endianness); result = SerializeAdapter::deSerialize(&fileSizeValue, &bufPtr, &deserLen, endianness);
info.setFileSize(fileSizeValue, true); info.setFileSize(fileSizeValue, true);
} } else {
else { uint32_t fileSizeValue = 0;
uint32_t fileSizeValue = 0; result = SerializeAdapter::deSerialize(&fileSizeValue, &bufPtr, &deserLen, endianness);
result = SerializeAdapter::deSerialize(&fileSizeValue, &bufPtr, &deserLen, endianness); info.setFileSize(fileSizeValue, false);
info.setFileSize(fileSizeValue, false); }
} if (result != HasReturnvaluesIF::RETURN_OK) {
if(result != HasReturnvaluesIF::RETURN_OK) { return result;
return result; }
} if (info.getConditionCode() != cfdp::ConditionCode::NO_ERROR) {
if(info.getConditionCode() != cfdp::ConditionCode::NO_ERROR) { EntityIdTlv* tlvPtr = info.getFaultLoc();
EntityIdTlv* tlvPtr = info.getFaultLoc(); if (tlvPtr == nullptr) {
if(tlvPtr == nullptr) {
#if FSFW_VERBOSE_LEVEL >= 1 #if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "EofPduDeserializer::parseData: Ca not deserialize fault location," sif::warning << "EofPduDeserializer::parseData: Ca not deserialize fault location,"
" given TLV pointer invalid" << std::endl; " given TLV pointer invalid"
<< std::endl;
#else #else
sif::printWarning("EofPduDeserializer::parseData: Ca not deserialize fault location," sif::printWarning(
" given TLV pointer invalid"); "EofPduDeserializer::parseData: Ca not deserialize fault location,"
" given TLV pointer invalid");
#endif #endif
#endif /* FSFW_VERBOSE_LEVEL >= 1 */ #endif /* FSFW_VERBOSE_LEVEL >= 1 */
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
}
result = tlvPtr->deSerialize(&bufPtr, &deserLen, endianness);
} }
return result; result = tlvPtr->deSerialize(&bufPtr, &deserLen, endianness);
}
return result;
} }

View File

@ -1,18 +1,17 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_EOFPDUDESERIALIZER_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_EOFPDUDESERIALIZER_H_
#define FSFW_SRC_FSFW_CFDP_PDU_EOFPDUDESERIALIZER_H_ #define FSFW_SRC_FSFW_CFDP_PDU_EOFPDUDESERIALIZER_H_
#include "fsfw/cfdp/pdu/FileDirectiveDeserializer.h"
#include "EofInfo.h" #include "EofInfo.h"
#include "fsfw/cfdp/pdu/FileDirectiveDeserializer.h"
class EofPduDeserializer: public FileDirectiveDeserializer { class EofPduDeserializer : public FileDirectiveDeserializer {
public: public:
EofPduDeserializer(const uint8_t* pduBuf, size_t maxSize, EofInfo& eofInfo); EofPduDeserializer(const uint8_t* pduBuf, size_t maxSize, EofInfo& eofInfo);
virtual ReturnValue_t parseData() override; virtual ReturnValue_t parseData() override;
private:
EofInfo& info; private:
EofInfo& info;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_EOFPDUDESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_EOFPDUDESERIALIZER_H_ */

View File

@ -1,46 +1,44 @@
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
#include "EofPduSerializer.h" #include "EofPduSerializer.h"
EofPduSerializer::EofPduSerializer(PduConfig &conf, EofInfo& info): #include "fsfw/FSFW.h"
FileDirectiveSerializer(conf, cfdp::FileDirectives::EOF_DIRECTIVE, 9), info(info) { #include "fsfw/serviceinterface.h"
setDirectiveDataFieldLen(info.getSerializedSize(getLargeFileFlag()));
EofPduSerializer::EofPduSerializer(PduConfig &conf, EofInfo &info)
: FileDirectiveSerializer(conf, cfdp::FileDirectives::EOF_DIRECTIVE, 9), info(info) {
setDirectiveDataFieldLen(info.getSerializedSize(getLargeFileFlag()));
} }
size_t EofPduSerializer::getSerializedSize() const { size_t EofPduSerializer::getSerializedSize() const {
return FileDirectiveSerializer::getWholePduSize(); return FileDirectiveSerializer::getWholePduSize();
} }
ReturnValue_t EofPduSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize, ReturnValue_t EofPduSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const { Endianness streamEndianness) const {
ReturnValue_t result = FileDirectiveSerializer::serialize(buffer, size, maxSize, ReturnValue_t result =
streamEndianness); FileDirectiveSerializer::serialize(buffer, size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if(*size + 1 > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT;
}
**buffer = info.getConditionCode() << 4;
*buffer += 1;
*size += 1;
uint32_t checksum = info.getChecksum();
result = SerializeAdapter::serialize(&checksum, buffer, size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if(info.getFileSize().isLargeFile()) {
uint64_t fileSizeValue = info.getFileSize().getSize();
result = SerializeAdapter::serialize(&fileSizeValue, buffer, size,
maxSize, streamEndianness);
}
else {
uint32_t fileSizeValue = info.getFileSize().getSize();
result = SerializeAdapter::serialize(&fileSizeValue, buffer, size,
maxSize, streamEndianness);
}
if(info.getFaultLoc() != nullptr and info.getConditionCode() != cfdp::ConditionCode::NO_ERROR) {
result = info.getFaultLoc()->serialize(buffer, size, maxSize, streamEndianness);
}
return result; return result;
}
if (*size + 1 > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT;
}
**buffer = info.getConditionCode() << 4;
*buffer += 1;
*size += 1;
uint32_t checksum = info.getChecksum();
result = SerializeAdapter::serialize(&checksum, buffer, size, maxSize, streamEndianness);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if (info.getFileSize().isLargeFile()) {
uint64_t fileSizeValue = info.getFileSize().getSize();
result = SerializeAdapter::serialize(&fileSizeValue, buffer, size, maxSize, streamEndianness);
} else {
uint32_t fileSizeValue = info.getFileSize().getSize();
result = SerializeAdapter::serialize(&fileSizeValue, buffer, size, maxSize, streamEndianness);
}
if (info.getFaultLoc() != nullptr and info.getConditionCode() != cfdp::ConditionCode::NO_ERROR) {
result = info.getFaultLoc()->serialize(buffer, size, maxSize, streamEndianness);
}
return result;
} }

View File

@ -1,22 +1,21 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_EOFPDUSERIALIZER_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_EOFPDUSERIALIZER_H_
#define FSFW_SRC_FSFW_CFDP_PDU_EOFPDUSERIALIZER_H_ #define FSFW_SRC_FSFW_CFDP_PDU_EOFPDUSERIALIZER_H_
#include "EofInfo.h"
#include "fsfw/cfdp/pdu/FileDirectiveSerializer.h" #include "fsfw/cfdp/pdu/FileDirectiveSerializer.h"
#include "fsfw/cfdp/tlv/EntityIdTlv.h" #include "fsfw/cfdp/tlv/EntityIdTlv.h"
#include "EofInfo.h"
class EofPduSerializer: public FileDirectiveSerializer { class EofPduSerializer : public FileDirectiveSerializer {
public: public:
EofPduSerializer(PduConfig &conf, EofInfo& info); EofPduSerializer(PduConfig& conf, EofInfo& info);
size_t getSerializedSize() const override; size_t getSerializedSize() const override;
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override; Endianness streamEndianness) const override;
private:
EofInfo& info; private:
EofInfo& info;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_EOFPDUSERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_EOFPDUSERIALIZER_H_ */

View File

@ -1,52 +1,48 @@
#include "FileDataDeserializer.h" #include "FileDataDeserializer.h"
FileDataDeserializer::FileDataDeserializer(const uint8_t *pduBuf, size_t maxSize, FileDataDeserializer::FileDataDeserializer(const uint8_t* pduBuf, size_t maxSize,
FileDataInfo& info): FileDataInfo& info)
HeaderDeserializer(pduBuf, maxSize), info(info) { : HeaderDeserializer(pduBuf, maxSize), info(info) {}
}
ReturnValue_t FileDataDeserializer::parseData() { ReturnValue_t FileDataDeserializer::parseData() {
ReturnValue_t result = HeaderDeserializer::parseData(); ReturnValue_t result = HeaderDeserializer::parseData();
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
}
size_t currentIdx = HeaderDeserializer::getHeaderSize();
const uint8_t* buf = rawPtr + currentIdx;
size_t remSize = HeaderDeserializer::getWholePduSize() - currentIdx;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;
}
if (hasSegmentMetadataFlag()) {
info.setSegmentMetadataFlag(true);
info.setRecordContinuationState(static_cast<cfdp::RecordContinuationState>((*buf >> 6) & 0b11));
size_t segmentMetadataLen = *buf & 0b00111111;
info.setSegmentMetadataLen(segmentMetadataLen);
if (remSize < segmentMetadataLen + 1) {
return SerializeIF::STREAM_TOO_SHORT;
} }
size_t currentIdx = HeaderDeserializer::getHeaderSize(); if (segmentMetadataLen > 0) {
const uint8_t* buf = rawPtr + currentIdx; buf += 1;
size_t remSize = HeaderDeserializer::getWholePduSize() - currentIdx; remSize -= 1;
if (remSize < 1) { info.setSegmentMetadata(buf);
return SerializeIF::STREAM_TOO_SHORT; buf += segmentMetadataLen;
remSize -= segmentMetadataLen;
} }
if(hasSegmentMetadataFlag()) { }
info.setSegmentMetadataFlag(true); result = info.getOffset().deSerialize(&buf, &remSize, this->getEndianness());
info.setRecordContinuationState(static_cast<cfdp::RecordContinuationState>( if (result != HasReturnvaluesIF::RETURN_OK) {
(*buf >> 6) & 0b11)); return result;
size_t segmentMetadataLen = *buf & 0b00111111; }
info.setSegmentMetadataLen(segmentMetadataLen); if (remSize > 0) {
if(remSize < segmentMetadataLen + 1) { info.setFileData(buf, remSize);
return SerializeIF::STREAM_TOO_SHORT; }
} return HasReturnvaluesIF::RETURN_OK;
if(segmentMetadataLen > 0) {
buf += 1;
remSize -= 1;
info.setSegmentMetadata(buf);
buf += segmentMetadataLen;
remSize -= segmentMetadataLen;
}
}
result = info.getOffset().deSerialize(&buf, &remSize, this->getEndianness());
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if(remSize > 0) {
info.setFileData(buf, remSize);
}
return HasReturnvaluesIF::RETURN_OK;
} }
SerializeIF::Endianness FileDataDeserializer::getEndianness() const { SerializeIF::Endianness FileDataDeserializer::getEndianness() const { return endianness; }
return endianness;
}
void FileDataDeserializer::setEndianness(SerializeIF::Endianness endianness) { void FileDataDeserializer::setEndianness(SerializeIF::Endianness endianness) {
this->endianness = endianness; this->endianness = endianness;
} }

View File

@ -1,22 +1,21 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_
#define FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_ #define FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_
#include "../definitions.h"
#include "FileDataInfo.h" #include "FileDataInfo.h"
#include "HeaderDeserializer.h" #include "HeaderDeserializer.h"
#include "../definitions.h"
class FileDataDeserializer: public HeaderDeserializer { class FileDataDeserializer : public HeaderDeserializer {
public: public:
FileDataDeserializer(const uint8_t* pduBuf, size_t maxSize, FileDataInfo& info); FileDataDeserializer(const uint8_t* pduBuf, size_t maxSize, FileDataInfo& info);
ReturnValue_t parseData(); ReturnValue_t parseData();
SerializeIF::Endianness getEndianness() const; SerializeIF::Endianness getEndianness() const;
void setEndianness(SerializeIF::Endianness endianness = SerializeIF::Endianness::NETWORK); void setEndianness(SerializeIF::Endianness endianness = SerializeIF::Endianness::NETWORK);
private: private:
SerializeIF::Endianness endianness = SerializeIF::Endianness::NETWORK;
SerializeIF::Endianness endianness = SerializeIF::Endianness::NETWORK; FileDataInfo& info;
FileDataInfo& info;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_ */

View File

@ -1,104 +1,93 @@
#include "FileDataInfo.h" #include "FileDataInfo.h"
FileDataInfo::FileDataInfo(cfdp::FileSize &offset, const uint8_t *fileData, size_t fileSize): FileDataInfo::FileDataInfo(cfdp::FileSize &offset, const uint8_t *fileData, size_t fileSize)
offset(offset), fileData(fileData), fileSize(fileSize) { : offset(offset), fileData(fileData), fileSize(fileSize) {}
}
FileDataInfo::FileDataInfo(cfdp::FileSize &offset): offset(offset) { FileDataInfo::FileDataInfo(cfdp::FileSize &offset) : offset(offset) {}
}
void FileDataInfo::setSegmentMetadataFlag(bool enable) { void FileDataInfo::setSegmentMetadataFlag(bool enable) {
if(enable) { if (enable) {
segmentMetadataFlag = cfdp::SegmentMetadataFlag::PRESENT; segmentMetadataFlag = cfdp::SegmentMetadataFlag::PRESENT;
} else { } else {
segmentMetadataFlag = cfdp::SegmentMetadataFlag::NOT_PRESENT; segmentMetadataFlag = cfdp::SegmentMetadataFlag::NOT_PRESENT;
} }
} }
size_t FileDataInfo::getSerializedSize(bool largeFile) const { size_t FileDataInfo::getSerializedSize(bool largeFile) const {
size_t sz = 0; size_t sz = 0;
if(segmentMetadataFlag == cfdp::SegmentMetadataFlag::PRESENT) { if (segmentMetadataFlag == cfdp::SegmentMetadataFlag::PRESENT) {
sz += 1 + segmentMetadataLen; sz += 1 + segmentMetadataLen;
} }
if(largeFile) { if (largeFile) {
sz += 8; sz += 8;
} else { } else {
sz += 4; sz += 4;
} }
sz += fileSize; sz += fileSize;
return sz; return sz;
} }
cfdp::SegmentMetadataFlag FileDataInfo::getSegmentMetadataFlag() const { cfdp::SegmentMetadataFlag FileDataInfo::getSegmentMetadataFlag() const {
return this->segmentMetadataFlag; return this->segmentMetadataFlag;
} }
bool FileDataInfo::hasSegmentMetadata() const { bool FileDataInfo::hasSegmentMetadata() const {
if(segmentMetadataFlag == cfdp::SegmentMetadataFlag::PRESENT) { if (segmentMetadataFlag == cfdp::SegmentMetadataFlag::PRESENT) {
return true; return true;
} }
return false; return false;
} }
cfdp::RecordContinuationState FileDataInfo::getRecordContinuationState() const { cfdp::RecordContinuationState FileDataInfo::getRecordContinuationState() const {
return this->recContState; return this->recContState;
} }
size_t FileDataInfo::getSegmentMetadataLen() const { size_t FileDataInfo::getSegmentMetadataLen() const { return segmentMetadataLen; }
return segmentMetadataLen;
}
ReturnValue_t FileDataInfo::addSegmentMetadataInfo(cfdp::RecordContinuationState recContState, ReturnValue_t FileDataInfo::addSegmentMetadataInfo(cfdp::RecordContinuationState recContState,
const uint8_t* segmentMetadata, size_t segmentMetadataLen) { const uint8_t *segmentMetadata,
this->segmentMetadataFlag = cfdp::SegmentMetadataFlag::PRESENT; size_t segmentMetadataLen) {
this->recContState = recContState; this->segmentMetadataFlag = cfdp::SegmentMetadataFlag::PRESENT;
if(segmentMetadataLen > 63) { this->recContState = recContState;
return HasReturnvaluesIF::RETURN_FAILED; if (segmentMetadataLen > 63) {
} return HasReturnvaluesIF::RETURN_FAILED;
this->segmentMetadata = segmentMetadata; }
this->segmentMetadataLen = segmentMetadataLen; this->segmentMetadata = segmentMetadata;
return HasReturnvaluesIF::RETURN_OK; this->segmentMetadataLen = segmentMetadataLen;
return HasReturnvaluesIF::RETURN_OK;
} }
const uint8_t* FileDataInfo::getFileData(size_t *fileSize) const { const uint8_t *FileDataInfo::getFileData(size_t *fileSize) const {
if(fileSize != nullptr) { if (fileSize != nullptr) {
*fileSize = this->fileSize; *fileSize = this->fileSize;
} }
return fileData; return fileData;
} }
const uint8_t* FileDataInfo::getSegmentMetadata(size_t *segmentMetadataLen) { const uint8_t *FileDataInfo::getSegmentMetadata(size_t *segmentMetadataLen) {
if(segmentMetadataLen != nullptr) { if (segmentMetadataLen != nullptr) {
*segmentMetadataLen = this->segmentMetadataLen; *segmentMetadataLen = this->segmentMetadataLen;
} }
return segmentMetadata; return segmentMetadata;
} }
cfdp::FileSize& FileDataInfo::getOffset() { cfdp::FileSize &FileDataInfo::getOffset() { return offset; }
return offset;
}
void FileDataInfo::setRecordContinuationState(cfdp::RecordContinuationState recContState) { void FileDataInfo::setRecordContinuationState(cfdp::RecordContinuationState recContState) {
this->recContState = recContState; this->recContState = recContState;
} }
void FileDataInfo::setSegmentMetadataLen(size_t len) { void FileDataInfo::setSegmentMetadataLen(size_t len) { this->segmentMetadataLen = len; }
this->segmentMetadataLen = len;
}
void FileDataInfo::setSegmentMetadata(const uint8_t *ptr) { void FileDataInfo::setSegmentMetadata(const uint8_t *ptr) { this->segmentMetadata = ptr; }
this->segmentMetadata = ptr;
}
void FileDataInfo::setFileData(const uint8_t *fileData, size_t fileSize) { void FileDataInfo::setFileData(const uint8_t *fileData, size_t fileSize) {
this->fileData = fileData; this->fileData = fileData;
this->fileSize = fileSize; this->fileSize = fileSize;
} }
cfdp::SegmentationControl FileDataInfo::getSegmentationControl() const { cfdp::SegmentationControl FileDataInfo::getSegmentationControl() const { return segCtrl; }
return segCtrl;
}
void FileDataInfo::setSegmentationControl(cfdp::SegmentationControl segCtrl) { void FileDataInfo::setSegmentationControl(cfdp::SegmentationControl segCtrl) {
this->segCtrl = segCtrl; this->segCtrl = segCtrl;
} }

View File

@ -1,45 +1,44 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_FILEDATAINFO_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_FILEDATAINFO_H_
#define FSFW_SRC_FSFW_CFDP_PDU_FILEDATAINFO_H_ #define FSFW_SRC_FSFW_CFDP_PDU_FILEDATAINFO_H_
#include <fsfw/cfdp/definitions.h>
#include <fsfw/cfdp/FileSize.h> #include <fsfw/cfdp/FileSize.h>
#include <fsfw/cfdp/definitions.h>
class FileDataInfo { class FileDataInfo {
public: public:
FileDataInfo(cfdp::FileSize& offset); FileDataInfo(cfdp::FileSize& offset);
FileDataInfo(cfdp::FileSize& offset, const uint8_t* fileData, size_t fileSize); FileDataInfo(cfdp::FileSize& offset, const uint8_t* fileData, size_t fileSize);
size_t getSerializedSize(bool largeFile = false) const; size_t getSerializedSize(bool largeFile = false) const;
cfdp::FileSize& getOffset(); cfdp::FileSize& getOffset();
const uint8_t* getFileData(size_t* fileSize = nullptr) const; const uint8_t* getFileData(size_t* fileSize = nullptr) const;
void setFileData(const uint8_t* fileData, size_t fileSize); void setFileData(const uint8_t* fileData, size_t fileSize);
cfdp::SegmentMetadataFlag getSegmentMetadataFlag() const; cfdp::SegmentMetadataFlag getSegmentMetadataFlag() const;
cfdp::SegmentationControl getSegmentationControl() const; cfdp::SegmentationControl getSegmentationControl() const;
cfdp::RecordContinuationState getRecordContinuationState() const; cfdp::RecordContinuationState getRecordContinuationState() const;
void setRecordContinuationState(cfdp::RecordContinuationState recContState); void setRecordContinuationState(cfdp::RecordContinuationState recContState);
void setSegmentationControl(cfdp::SegmentationControl segCtrl); void setSegmentationControl(cfdp::SegmentationControl segCtrl);
size_t getSegmentMetadataLen() const; size_t getSegmentMetadataLen() const;
void setSegmentMetadataLen(size_t len); void setSegmentMetadataLen(size_t len);
void setSegmentMetadata(const uint8_t* ptr); void setSegmentMetadata(const uint8_t* ptr);
bool hasSegmentMetadata() const; bool hasSegmentMetadata() const;
void setSegmentMetadataFlag(bool enable); void setSegmentMetadataFlag(bool enable);
ReturnValue_t addSegmentMetadataInfo(cfdp::RecordContinuationState recContState, ReturnValue_t addSegmentMetadataInfo(cfdp::RecordContinuationState recContState,
const uint8_t* segmentMetadata, size_t segmentMetadataLen); const uint8_t* segmentMetadata, size_t segmentMetadataLen);
const uint8_t* getSegmentMetadata(size_t* segmentMetadataLen = nullptr); const uint8_t* getSegmentMetadata(size_t* segmentMetadataLen = nullptr);
private: private:
cfdp::SegmentMetadataFlag segmentMetadataFlag = cfdp::SegmentMetadataFlag::NOT_PRESENT; cfdp::SegmentMetadataFlag segmentMetadataFlag = cfdp::SegmentMetadataFlag::NOT_PRESENT;
cfdp::SegmentationControl segCtrl = cfdp::SegmentationControl segCtrl = cfdp::SegmentationControl::NO_RECORD_BOUNDARIES_PRESERVATION;
cfdp::SegmentationControl::NO_RECORD_BOUNDARIES_PRESERVATION; cfdp::FileSize& offset;
cfdp::FileSize& offset; const uint8_t* fileData = nullptr;
const uint8_t* fileData = nullptr; size_t fileSize = 0;
size_t fileSize = 0; cfdp::RecordContinuationState recContState = cfdp::RecordContinuationState::NO_START_NO_END;
cfdp::RecordContinuationState recContState = cfdp::RecordContinuationState::NO_START_NO_END; size_t segmentMetadataLen = 0;
size_t segmentMetadataLen = 0; const uint8_t* segmentMetadata = nullptr;
const uint8_t* segmentMetadata = nullptr;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDATAINFO_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDATAINFO_H_ */

View File

@ -1,54 +1,55 @@
#include "FileDataSerializer.h" #include "FileDataSerializer.h"
#include <cstring> #include <cstring>
FileDataSerializer::FileDataSerializer(PduConfig& conf, FileDataInfo& info): FileDataSerializer::FileDataSerializer(PduConfig& conf, FileDataInfo& info)
HeaderSerializer(conf, cfdp::PduType::FILE_DATA, 0, info.getSegmentMetadataFlag()), : HeaderSerializer(conf, cfdp::PduType::FILE_DATA, 0, info.getSegmentMetadataFlag()),
info(info) { info(info) {
update(); update();
} }
void FileDataSerializer::update() { void FileDataSerializer::update() {
this->setSegmentMetadataFlag(info.getSegmentMetadataFlag()); this->setSegmentMetadataFlag(info.getSegmentMetadataFlag());
this->setSegmentationControl(info.getSegmentationControl()); this->setSegmentationControl(info.getSegmentationControl());
setPduDataFieldLen(info.getSerializedSize(this->getLargeFileFlag())); setPduDataFieldLen(info.getSerializedSize(this->getLargeFileFlag()));
} }
ReturnValue_t FileDataSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize, ReturnValue_t FileDataSerializer::serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const { Endianness streamEndianness) const {
ReturnValue_t result = HeaderSerializer::serialize(buffer, size, maxSize, streamEndianness); ReturnValue_t result = HeaderSerializer::serialize(buffer, size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
if(*size + this->getSerializedSize() > maxSize) { if (*size + this->getSerializedSize() > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT; return SerializeIF::BUFFER_TOO_SHORT;
} }
const uint8_t* readOnlyPtr = nullptr; const uint8_t* readOnlyPtr = nullptr;
if (this->hasSegmentMetadataFlag()) { if (this->hasSegmentMetadataFlag()) {
size_t segmentMetadataLen = info.getSegmentMetadataLen(); size_t segmentMetadataLen = info.getSegmentMetadataLen();
**buffer = info.getRecordContinuationState() << 6 | segmentMetadataLen; **buffer = info.getRecordContinuationState() << 6 | segmentMetadataLen;
*buffer += 1; *buffer += 1;
*size += 1; *size += 1;
readOnlyPtr = info.getSegmentMetadata(); readOnlyPtr = info.getSegmentMetadata();
std::memcpy(*buffer, readOnlyPtr, segmentMetadataLen); std::memcpy(*buffer, readOnlyPtr, segmentMetadataLen);
*buffer += segmentMetadataLen; *buffer += segmentMetadataLen;
*size += segmentMetadataLen; *size += segmentMetadataLen;
} }
cfdp::FileSize& offset = info.getOffset(); cfdp::FileSize& offset = info.getOffset();
result = offset.serialize(this->getLargeFileFlag(), buffer, size, maxSize, streamEndianness); result = offset.serialize(this->getLargeFileFlag(), buffer, size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
size_t fileSize = 0; size_t fileSize = 0;
readOnlyPtr = info.getFileData(&fileSize); readOnlyPtr = info.getFileData(&fileSize);
if(*size + fileSize > maxSize) { if (*size + fileSize > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT; return SerializeIF::BUFFER_TOO_SHORT;
} }
std::memcpy(*buffer, readOnlyPtr, fileSize); std::memcpy(*buffer, readOnlyPtr, fileSize);
*buffer += fileSize; *buffer += fileSize;
*size += fileSize; *size += fileSize;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
size_t FileDataSerializer::getSerializedSize() const { size_t FileDataSerializer::getSerializedSize() const {
return HeaderSerializer::getSerializedSize() + info.getSerializedSize(this->getLargeFileFlag()); return HeaderSerializer::getSerializedSize() + info.getSerializedSize(this->getLargeFileFlag());
} }

View File

@ -1,23 +1,23 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_FILEDATASERIALIZER_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_FILEDATASERIALIZER_H_
#define FSFW_SRC_FSFW_CFDP_PDU_FILEDATASERIALIZER_H_ #define FSFW_SRC_FSFW_CFDP_PDU_FILEDATASERIALIZER_H_
#include "FileDataInfo.h"
#include "../definitions.h" #include "../definitions.h"
#include "FileDataInfo.h"
#include "HeaderSerializer.h" #include "HeaderSerializer.h"
class FileDataSerializer: public HeaderSerializer { class FileDataSerializer : public HeaderSerializer {
public: public:
FileDataSerializer(PduConfig& conf, FileDataInfo& info); FileDataSerializer(PduConfig& conf, FileDataInfo& info);
void update(); void update();
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override; Endianness streamEndianness) const override;
size_t getSerializedSize() const override; size_t getSerializedSize() const override;
private: private:
FileDataInfo& info; FileDataInfo& info;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDATADESERIALIZER_H_ */

View File

@ -1,55 +1,49 @@
#include "FileDirectiveDeserializer.h" #include "FileDirectiveDeserializer.h"
FileDirectiveDeserializer::FileDirectiveDeserializer(const uint8_t *pduBuf, size_t maxSize): FileDirectiveDeserializer::FileDirectiveDeserializer(const uint8_t *pduBuf, size_t maxSize)
HeaderDeserializer(pduBuf, maxSize) { : HeaderDeserializer(pduBuf, maxSize) {}
}
cfdp::FileDirectives FileDirectiveDeserializer::getFileDirective() const { cfdp::FileDirectives FileDirectiveDeserializer::getFileDirective() const { return fileDirective; }
return fileDirective;
}
ReturnValue_t FileDirectiveDeserializer::parseData() { ReturnValue_t FileDirectiveDeserializer::parseData() {
ReturnValue_t result = HeaderDeserializer::parseData(); ReturnValue_t result = HeaderDeserializer::parseData();
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
if(this->getPduDataFieldLen() < 1) { if (this->getPduDataFieldLen() < 1) {
return cfdp::INVALID_PDU_DATAFIELD_LEN; return cfdp::INVALID_PDU_DATAFIELD_LEN;
} }
if(FileDirectiveDeserializer::getWholePduSize() > maxSize) { if (FileDirectiveDeserializer::getWholePduSize() > maxSize) {
return SerializeIF::STREAM_TOO_SHORT; return SerializeIF::STREAM_TOO_SHORT;
} }
size_t currentIdx = HeaderDeserializer::getHeaderSize(); size_t currentIdx = HeaderDeserializer::getHeaderSize();
if(not checkFileDirective(rawPtr[currentIdx])) { if (not checkFileDirective(rawPtr[currentIdx])) {
return cfdp::INVALID_DIRECTIVE_FIELDS; return cfdp::INVALID_DIRECTIVE_FIELDS;
} }
setFileDirective(static_cast<cfdp::FileDirectives>(rawPtr[currentIdx])); setFileDirective(static_cast<cfdp::FileDirectives>(rawPtr[currentIdx]));
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
size_t FileDirectiveDeserializer::getHeaderSize() const { size_t FileDirectiveDeserializer::getHeaderSize() const {
// return size of header plus the directive byte // return size of header plus the directive byte
return HeaderDeserializer::getHeaderSize() + 1; return HeaderDeserializer::getHeaderSize() + 1;
} }
bool FileDirectiveDeserializer::checkFileDirective(uint8_t rawByte) { bool FileDirectiveDeserializer::checkFileDirective(uint8_t rawByte) {
if(rawByte < cfdp::FileDirectives::EOF_DIRECTIVE or if (rawByte < cfdp::FileDirectives::EOF_DIRECTIVE or
(rawByte > cfdp::FileDirectives::PROMPT and (rawByte > cfdp::FileDirectives::PROMPT and rawByte != cfdp::FileDirectives::KEEP_ALIVE)) {
rawByte != cfdp::FileDirectives::KEEP_ALIVE)) { // Invalid directive field. TODO: Custom returnvalue
// Invalid directive field. TODO: Custom returnvalue return false;
return false; }
} return true;
return true;
} }
void FileDirectiveDeserializer::setFileDirective(cfdp::FileDirectives fileDirective) { void FileDirectiveDeserializer::setFileDirective(cfdp::FileDirectives fileDirective) {
this->fileDirective = fileDirective; this->fileDirective = fileDirective;
} }
void FileDirectiveDeserializer::setEndianness(SerializeIF::Endianness endianness) { void FileDirectiveDeserializer::setEndianness(SerializeIF::Endianness endianness) {
this->endianness = endianness; this->endianness = endianness;
} }
SerializeIF::Endianness FileDirectiveDeserializer::getEndianness() const { SerializeIF::Endianness FileDirectiveDeserializer::getEndianness() const { return endianness; }
return endianness;
}

View File

@ -11,31 +11,29 @@
* This is a zero-copy implementation and #parseData needs to be called to ensure the data is * This is a zero-copy implementation and #parseData needs to be called to ensure the data is
* valid. * valid.
*/ */
class FileDirectiveDeserializer: public HeaderDeserializer { class FileDirectiveDeserializer : public HeaderDeserializer {
public: public:
FileDirectiveDeserializer(const uint8_t* pduBuf, size_t maxSize); FileDirectiveDeserializer(const uint8_t* pduBuf, size_t maxSize);
/** /**
* This needs to be called before accessing the PDU fields to avoid segmentation faults. * This needs to be called before accessing the PDU fields to avoid segmentation faults.
* @return * @return
*/ */
virtual ReturnValue_t parseData(); virtual ReturnValue_t parseData();
size_t getHeaderSize() const; size_t getHeaderSize() const;
cfdp::FileDirectives getFileDirective() const; cfdp::FileDirectives getFileDirective() const;
void setEndianness(SerializeIF::Endianness endianness); void setEndianness(SerializeIF::Endianness endianness);
SerializeIF::Endianness getEndianness() const; SerializeIF::Endianness getEndianness() const;
protected: protected:
bool checkFileDirective(uint8_t rawByte); bool checkFileDirective(uint8_t rawByte);
private: private:
void setFileDirective(cfdp::FileDirectives fileDirective); void setFileDirective(cfdp::FileDirectives fileDirective);
cfdp::FileDirectives fileDirective = cfdp::FileDirectives::INVALID_DIRECTIVE; cfdp::FileDirectives fileDirective = cfdp::FileDirectives::INVALID_DIRECTIVE;
SerializeIF::Endianness endianness = SerializeIF::Endianness::NETWORK; SerializeIF::Endianness endianness = SerializeIF::Endianness::NETWORK;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDIRECTIVEDESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDIRECTIVEDESERIALIZER_H_ */

View File

@ -1,39 +1,38 @@
#include "FileDirectiveSerializer.h" #include "FileDirectiveSerializer.h"
FileDirectiveSerializer::FileDirectiveSerializer(PduConfig &pduConf, FileDirectiveSerializer::FileDirectiveSerializer(PduConfig &pduConf,
cfdp::FileDirectives directiveCode, size_t directiveParamFieldLen): cfdp::FileDirectives directiveCode,
HeaderSerializer(pduConf, cfdp::PduType::FILE_DIRECTIVE, directiveParamFieldLen + 1), size_t directiveParamFieldLen)
directiveCode(directiveCode) { : HeaderSerializer(pduConf, cfdp::PduType::FILE_DIRECTIVE, directiveParamFieldLen + 1),
} directiveCode(directiveCode) {}
size_t FileDirectiveSerializer::getSerializedSize() const { size_t FileDirectiveSerializer::getSerializedSize() const {
return HeaderSerializer::getSerializedSize() + 1; return HeaderSerializer::getSerializedSize() + 1;
} }
ReturnValue_t FileDirectiveSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize, ReturnValue_t FileDirectiveSerializer::serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const { Endianness streamEndianness) const {
if(buffer == nullptr or size == nullptr) { if (buffer == nullptr or size == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
if(FileDirectiveSerializer::getWholePduSize() > maxSize) { if (FileDirectiveSerializer::getWholePduSize() > maxSize) {
return BUFFER_TOO_SHORT; return BUFFER_TOO_SHORT;
} }
ReturnValue_t result = HeaderSerializer::serialize(buffer, size, maxSize, streamEndianness); ReturnValue_t result = HeaderSerializer::serialize(buffer, size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
if(*size >= maxSize) { if (*size >= maxSize) {
return BUFFER_TOO_SHORT; return BUFFER_TOO_SHORT;
} }
**buffer = directiveCode; **buffer = directiveCode;
*buffer += 1; *buffer += 1;
*size += 1; *size += 1;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
void FileDirectiveSerializer::setDirectiveDataFieldLen(size_t len) { void FileDirectiveSerializer::setDirectiveDataFieldLen(size_t len) {
// Set length of data field plus 1 byte for the directive octet // Set length of data field plus 1 byte for the directive octet
HeaderSerializer::setPduDataFieldLen(len + 1); HeaderSerializer::setPduDataFieldLen(len + 1);
} }

View File

@ -3,26 +3,26 @@
#include "fsfw/cfdp/pdu/HeaderSerializer.h" #include "fsfw/cfdp/pdu/HeaderSerializer.h"
class FileDirectiveSerializer: public HeaderSerializer { class FileDirectiveSerializer : public HeaderSerializer {
public: public:
FileDirectiveSerializer(PduConfig& pduConf, cfdp::FileDirectives directiveCode, FileDirectiveSerializer(PduConfig& pduConf, cfdp::FileDirectives directiveCode,
size_t directiveParamFieldLen); size_t directiveParamFieldLen);
/** /**
* This only returns the size of the PDU header + 1 for the directive code octet. * This only returns the size of the PDU header + 1 for the directive code octet.
* Use FileDirectiveSerializer::getWholePduSize to get the full packet length, assuming * Use FileDirectiveSerializer::getWholePduSize to get the full packet length, assuming
* the length fields was set correctly * the length fields was set correctly
* @return * @return
*/ */
size_t getSerializedSize() const override; size_t getSerializedSize() const override;
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override; Endianness streamEndianness) const override;
void setDirectiveDataFieldLen(size_t len); void setDirectiveDataFieldLen(size_t len);
private:
cfdp::FileDirectives directiveCode = cfdp::FileDirectives::INVALID_DIRECTIVE;
private:
cfdp::FileDirectives directiveCode = cfdp::FileDirectives::INVALID_DIRECTIVE;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDIRECTIVESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FILEDIRECTIVESERIALIZER_H_ */

View File

@ -1,111 +1,102 @@
#include "FinishedInfo.h" #include "FinishedInfo.h"
FinishedInfo::FinishedInfo() { FinishedInfo::FinishedInfo() {}
}
FinishedInfo::FinishedInfo(cfdp::ConditionCode conditionCode, FinishedInfo::FinishedInfo(cfdp::ConditionCode conditionCode,
cfdp::FinishedDeliveryCode deliveryCode, cfdp::FinishedFileStatus fileStatus): cfdp::FinishedDeliveryCode deliveryCode,
conditionCode(conditionCode), deliveryCode(deliveryCode), fileStatus(fileStatus) { cfdp::FinishedFileStatus fileStatus)
} : conditionCode(conditionCode), deliveryCode(deliveryCode), fileStatus(fileStatus) {}
size_t FinishedInfo::getSerializedSize() const { size_t FinishedInfo::getSerializedSize() const {
size_t size = 1; size_t size = 1;
if(hasFsResponses()) { if (hasFsResponses()) {
for(size_t idx = 0; idx < fsResponsesLen; idx++) { for (size_t idx = 0; idx < fsResponsesLen; idx++) {
size += fsResponses[idx]->getSerializedSize(); size += fsResponses[idx]->getSerializedSize();
}
} }
if(this->faultLocation != nullptr) { }
size += faultLocation->getSerializedSize(); if (this->faultLocation != nullptr) {
} size += faultLocation->getSerializedSize();
return size; }
return size;
} }
bool FinishedInfo::hasFsResponses() const { bool FinishedInfo::hasFsResponses() const {
if(fsResponses != nullptr and fsResponsesLen > 0) { if (fsResponses != nullptr and fsResponsesLen > 0) {
return true; return true;
} }
return false; return false;
} }
bool FinishedInfo::canHoldFsResponses() const { bool FinishedInfo::canHoldFsResponses() const {
if(fsResponses != nullptr and fsResponsesMaxLen > 0) { if (fsResponses != nullptr and fsResponsesMaxLen > 0) {
return true; return true;
} }
return false; return false;
} }
ReturnValue_t FinishedInfo::setFilestoreResponsesArray(FilestoreResponseTlv** fsResponses, ReturnValue_t FinishedInfo::setFilestoreResponsesArray(FilestoreResponseTlv** fsResponses,
size_t* fsResponsesLen, const size_t* maxFsResponsesLen) { size_t* fsResponsesLen,
this->fsResponses = fsResponses; const size_t* maxFsResponsesLen) {
if(fsResponsesLen != nullptr) { this->fsResponses = fsResponses;
this->fsResponsesLen = *fsResponsesLen; if (fsResponsesLen != nullptr) {
if(this->fsResponsesMaxLen < *fsResponsesLen) { this->fsResponsesLen = *fsResponsesLen;
this->fsResponsesMaxLen = this->fsResponsesLen; if (this->fsResponsesMaxLen < *fsResponsesLen) {
} this->fsResponsesMaxLen = this->fsResponsesLen;
} }
if(maxFsResponsesLen != nullptr) { }
this->fsResponsesMaxLen = *maxFsResponsesLen; if (maxFsResponsesLen != nullptr) {
} this->fsResponsesMaxLen = *maxFsResponsesLen;
return HasReturnvaluesIF::RETURN_OK; }
return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t FinishedInfo::getFilestoreResonses(FilestoreResponseTlv ***fsResponses, ReturnValue_t FinishedInfo::getFilestoreResonses(FilestoreResponseTlv*** fsResponses,
size_t *fsResponsesLen, size_t* fsResponsesMaxLen) { size_t* fsResponsesLen,
if(fsResponses == nullptr) { size_t* fsResponsesMaxLen) {
return HasReturnvaluesIF::RETURN_FAILED; if (fsResponses == nullptr) {
} return HasReturnvaluesIF::RETURN_FAILED;
*fsResponses = this->fsResponses; }
if(fsResponsesLen != nullptr) { *fsResponses = this->fsResponses;
*fsResponsesLen = this->fsResponsesLen; if (fsResponsesLen != nullptr) {
} *fsResponsesLen = this->fsResponsesLen;
if(fsResponsesMaxLen != nullptr) { }
*fsResponsesMaxLen = this->fsResponsesMaxLen; if (fsResponsesMaxLen != nullptr) {
} *fsResponsesMaxLen = this->fsResponsesMaxLen;
return HasReturnvaluesIF::RETURN_OK; }
return HasReturnvaluesIF::RETURN_OK;
} }
void FinishedInfo::setFaultLocation(EntityIdTlv *faultLocation) { void FinishedInfo::setFaultLocation(EntityIdTlv* faultLocation) {
this->faultLocation = faultLocation; this->faultLocation = faultLocation;
} }
ReturnValue_t FinishedInfo::getFaultLocation(EntityIdTlv** faultLocation) { ReturnValue_t FinishedInfo::getFaultLocation(EntityIdTlv** faultLocation) {
if(this->faultLocation == nullptr) { if (this->faultLocation == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
*faultLocation = this->faultLocation; *faultLocation = this->faultLocation;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
cfdp::ConditionCode FinishedInfo::getConditionCode() const { cfdp::ConditionCode FinishedInfo::getConditionCode() const { return conditionCode; }
return conditionCode;
}
void FinishedInfo::setConditionCode(cfdp::ConditionCode conditionCode) { void FinishedInfo::setConditionCode(cfdp::ConditionCode conditionCode) {
this->conditionCode = conditionCode; this->conditionCode = conditionCode;
} }
cfdp::FinishedDeliveryCode FinishedInfo::getDeliveryCode() const { cfdp::FinishedDeliveryCode FinishedInfo::getDeliveryCode() const { return deliveryCode; }
return deliveryCode;
}
void FinishedInfo::setDeliveryCode(cfdp::FinishedDeliveryCode deliveryCode) { void FinishedInfo::setDeliveryCode(cfdp::FinishedDeliveryCode deliveryCode) {
this->deliveryCode = deliveryCode; this->deliveryCode = deliveryCode;
} }
cfdp::FinishedFileStatus FinishedInfo::getFileStatus() const { cfdp::FinishedFileStatus FinishedInfo::getFileStatus() const { return fileStatus; }
return fileStatus;
}
void FinishedInfo::setFilestoreResponsesArrayLen(size_t fsResponsesLen) { void FinishedInfo::setFilestoreResponsesArrayLen(size_t fsResponsesLen) {
this->fsResponsesLen = fsResponsesLen; this->fsResponsesLen = fsResponsesLen;
} }
size_t FinishedInfo::getFsResponsesLen() const { size_t FinishedInfo::getFsResponsesLen() const { return fsResponsesLen; }
return fsResponsesLen;
}
void FinishedInfo::setFileStatus(cfdp::FinishedFileStatus fileStatus) { void FinishedInfo::setFileStatus(cfdp::FinishedFileStatus fileStatus) {
this->fileStatus = fileStatus; this->fileStatus = fileStatus;
} }

View File

@ -1,45 +1,45 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_FINISHINFO_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_FINISHINFO_H_
#define FSFW_SRC_FSFW_CFDP_PDU_FINISHINFO_H_ #define FSFW_SRC_FSFW_CFDP_PDU_FINISHINFO_H_
#include "../definitions.h"
#include "fsfw/cfdp/tlv/EntityIdTlv.h" #include "fsfw/cfdp/tlv/EntityIdTlv.h"
#include "fsfw/cfdp/tlv/FilestoreResponseTlv.h" #include "fsfw/cfdp/tlv/FilestoreResponseTlv.h"
#include "../definitions.h"
class FinishedInfo { class FinishedInfo {
public: public:
FinishedInfo(); FinishedInfo();
FinishedInfo(cfdp::ConditionCode conditionCode, cfdp::FinishedDeliveryCode deliveryCode, FinishedInfo(cfdp::ConditionCode conditionCode, cfdp::FinishedDeliveryCode deliveryCode,
cfdp::FinishedFileStatus fileStatus); cfdp::FinishedFileStatus fileStatus);
size_t getSerializedSize() const; size_t getSerializedSize() const;
bool hasFsResponses() const; bool hasFsResponses() const;
bool canHoldFsResponses() const; bool canHoldFsResponses() const;
ReturnValue_t setFilestoreResponsesArray(FilestoreResponseTlv** fsResponses, ReturnValue_t setFilestoreResponsesArray(FilestoreResponseTlv** fsResponses,
size_t* fsResponsesLen, const size_t* maxFsResponseLen); size_t* fsResponsesLen, const size_t* maxFsResponseLen);
void setFaultLocation(EntityIdTlv* entityId); void setFaultLocation(EntityIdTlv* entityId);
ReturnValue_t getFilestoreResonses(FilestoreResponseTlv ***fsResponses, ReturnValue_t getFilestoreResonses(FilestoreResponseTlv*** fsResponses, size_t* fsResponsesLen,
size_t *fsResponsesLen, size_t* fsResponsesMaxLen); size_t* fsResponsesMaxLen);
size_t getFsResponsesLen() const; size_t getFsResponsesLen() const;
void setFilestoreResponsesArrayLen(size_t fsResponsesLen); void setFilestoreResponsesArrayLen(size_t fsResponsesLen);
ReturnValue_t getFaultLocation(EntityIdTlv** entityId); ReturnValue_t getFaultLocation(EntityIdTlv** entityId);
cfdp::ConditionCode getConditionCode() const; cfdp::ConditionCode getConditionCode() const;
void setConditionCode(cfdp::ConditionCode conditionCode); void setConditionCode(cfdp::ConditionCode conditionCode);
cfdp::FinishedDeliveryCode getDeliveryCode() const; cfdp::FinishedDeliveryCode getDeliveryCode() const;
void setDeliveryCode(cfdp::FinishedDeliveryCode deliveryCode); void setDeliveryCode(cfdp::FinishedDeliveryCode deliveryCode);
cfdp::FinishedFileStatus getFileStatus() const; cfdp::FinishedFileStatus getFileStatus() const;
void setFileStatus(cfdp::FinishedFileStatus fileStatus); void setFileStatus(cfdp::FinishedFileStatus fileStatus);
private: private:
cfdp::ConditionCode conditionCode = cfdp::ConditionCode::NO_CONDITION_FIELD; cfdp::ConditionCode conditionCode = cfdp::ConditionCode::NO_CONDITION_FIELD;
cfdp::FinishedDeliveryCode deliveryCode = cfdp::FinishedDeliveryCode::DATA_COMPLETE; cfdp::FinishedDeliveryCode deliveryCode = cfdp::FinishedDeliveryCode::DATA_COMPLETE;
cfdp::FinishedFileStatus fileStatus = cfdp::FinishedFileStatus::DISCARDED_DELIBERATELY; cfdp::FinishedFileStatus fileStatus = cfdp::FinishedFileStatus::DISCARDED_DELIBERATELY;
FilestoreResponseTlv** fsResponses = nullptr; FilestoreResponseTlv** fsResponses = nullptr;
size_t fsResponsesLen = 0; size_t fsResponsesLen = 0;
size_t fsResponsesMaxLen = 0; size_t fsResponsesMaxLen = 0;
EntityIdTlv* faultLocation = nullptr; EntityIdTlv* faultLocation = nullptr;
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FINISHINFO_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FINISHINFO_H_ */

View File

@ -1,90 +1,88 @@
#include "FinishedPduDeserializer.h" #include "FinishedPduDeserializer.h"
FinishPduDeserializer::FinishPduDeserializer(const uint8_t *pduBuf, size_t maxSize, FinishPduDeserializer::FinishPduDeserializer(const uint8_t* pduBuf, size_t maxSize,
FinishedInfo &info): FileDirectiveDeserializer(pduBuf, maxSize), finishedInfo(info) { FinishedInfo& info)
} : FileDirectiveDeserializer(pduBuf, maxSize), finishedInfo(info) {}
ReturnValue_t FinishPduDeserializer::parseData() { ReturnValue_t FinishPduDeserializer::parseData() {
ReturnValue_t result = FileDirectiveDeserializer::parseData(); ReturnValue_t result = FileDirectiveDeserializer::parseData();
if(result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
size_t currentIdx = FileDirectiveDeserializer::getHeaderSize();
const uint8_t* buf = rawPtr + currentIdx;
size_t remSize = FileDirectiveDeserializer::getWholePduSize() - currentIdx;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;
}
uint8_t firstByte = *buf;
cfdp::ConditionCode condCode = static_cast<cfdp::ConditionCode>((firstByte >> 4) & 0x0f);
finishedInfo.setConditionCode(condCode);
finishedInfo.setDeliveryCode(static_cast<cfdp::FinishedDeliveryCode>(firstByte >> 2 & 0b1));
finishedInfo.setFileStatus(static_cast<cfdp::FinishedFileStatus>(firstByte & 0b11));
buf += 1;
remSize -= 1;
currentIdx += 1;
if (remSize > 0) {
result = parseTlvs(remSize, currentIdx, buf, condCode);
}
return result;
}
FinishedInfo& FinishPduDeserializer::getInfo() { return finishedInfo; }
ReturnValue_t FinishPduDeserializer::parseTlvs(size_t remLen, size_t currentIdx, const uint8_t* buf,
cfdp::ConditionCode conditionCode) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
size_t fsResponsesIdx = 0;
auto endianness = getEndianness();
FilestoreResponseTlv** fsResponseArray = nullptr;
size_t fsResponseMaxArrayLen = 0;
EntityIdTlv* faultLocation = nullptr;
cfdp::TlvTypes nextTlv = cfdp::TlvTypes::INVALID_TLV;
while (remLen > 0) {
// Simply forward parse the TLV type. Every TLV type except the last one must be a Filestore
// Response TLV, and even the last one can be a Filestore Response TLV if the fault
// location is omitted
if (currentIdx + 2 > maxSize) {
return SerializeIF::STREAM_TOO_SHORT;
}
nextTlv = static_cast<cfdp::TlvTypes>(*buf);
if (nextTlv == cfdp::TlvTypes::FILESTORE_RESPONSE) {
if (fsResponseArray == nullptr) {
if (not finishedInfo.canHoldFsResponses()) {
return cfdp::FINISHED_CANT_PARSE_FS_RESPONSES;
}
result =
finishedInfo.getFilestoreResonses(&fsResponseArray, nullptr, &fsResponseMaxArrayLen);
}
if (fsResponsesIdx == fsResponseMaxArrayLen) {
return cfdp::FINISHED_CANT_PARSE_FS_RESPONSES;
}
result = fsResponseArray[fsResponsesIdx]->deSerialize(&buf, &remLen, endianness);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
}
fsResponsesIdx += 1;
} else if (nextTlv == cfdp::TlvTypes::ENTITY_ID) {
// This needs to be the last TLV and it should not be here if the condition code
// is "No Error" or "Unsupported Checksum Type"
if (conditionCode == cfdp::ConditionCode::NO_ERROR or
conditionCode == cfdp::ConditionCode::UNSUPPORTED_CHECKSUM_TYPE) {
return cfdp::INVALID_TLV_TYPE;
}
result = finishedInfo.getFaultLocation(&faultLocation);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
result = faultLocation->deSerialize(&buf, &remLen, endianness);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
} else {
return cfdp::INVALID_TLV_TYPE;
} }
size_t currentIdx = FileDirectiveDeserializer::getHeaderSize(); }
const uint8_t* buf = rawPtr + currentIdx; finishedInfo.setFilestoreResponsesArrayLen(fsResponsesIdx);
size_t remSize = FileDirectiveDeserializer::getWholePduSize() - currentIdx; return result;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;
}
uint8_t firstByte = *buf;
cfdp::ConditionCode condCode = static_cast<cfdp::ConditionCode>((firstByte >> 4) & 0x0f);
finishedInfo.setConditionCode(condCode);
finishedInfo.setDeliveryCode(static_cast<cfdp::FinishedDeliveryCode>(firstByte >> 2 & 0b1));
finishedInfo.setFileStatus(static_cast<cfdp::FinishedFileStatus>(firstByte & 0b11));
buf += 1;
remSize -= 1;
currentIdx += 1;
if(remSize > 0) {
result = parseTlvs(remSize, currentIdx, buf, condCode);
}
return result;
}
FinishedInfo& FinishPduDeserializer::getInfo() {
return finishedInfo;
}
ReturnValue_t FinishPduDeserializer::parseTlvs(size_t remLen, size_t currentIdx,
const uint8_t* buf, cfdp::ConditionCode conditionCode) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
size_t fsResponsesIdx = 0;
auto endianness = getEndianness();
FilestoreResponseTlv** fsResponseArray = nullptr;
size_t fsResponseMaxArrayLen = 0;
EntityIdTlv* faultLocation = nullptr;
cfdp::TlvTypes nextTlv = cfdp::TlvTypes::INVALID_TLV;
while(remLen > 0) {
// Simply forward parse the TLV type. Every TLV type except the last one must be a Filestore
// Response TLV, and even the last one can be a Filestore Response TLV if the fault
// location is omitted
if (currentIdx + 2 > maxSize) {
return SerializeIF::STREAM_TOO_SHORT;
}
nextTlv = static_cast<cfdp::TlvTypes>(*buf);
if (nextTlv == cfdp::TlvTypes::FILESTORE_RESPONSE) {
if(fsResponseArray == nullptr) {
if(not finishedInfo.canHoldFsResponses()) {
return cfdp::FINISHED_CANT_PARSE_FS_RESPONSES;
}
result = finishedInfo.getFilestoreResonses(&fsResponseArray, nullptr,
&fsResponseMaxArrayLen);
}
if(fsResponsesIdx == fsResponseMaxArrayLen) {
return cfdp::FINISHED_CANT_PARSE_FS_RESPONSES;
}
result = fsResponseArray[fsResponsesIdx]->deSerialize(&buf, &remLen, endianness);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
fsResponsesIdx += 1;
} else if(nextTlv == cfdp::TlvTypes::ENTITY_ID) {
// This needs to be the last TLV and it should not be here if the condition code
// is "No Error" or "Unsupported Checksum Type"
if(conditionCode == cfdp::ConditionCode::NO_ERROR or
conditionCode == cfdp::ConditionCode::UNSUPPORTED_CHECKSUM_TYPE) {
return cfdp::INVALID_TLV_TYPE;
}
result = finishedInfo.getFaultLocation(&faultLocation);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
result = faultLocation->deSerialize(&buf, &remLen, endianness);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
} else {
return cfdp::INVALID_TLV_TYPE;
}
}
finishedInfo.setFilestoreResponsesArrayLen(fsResponsesIdx);
return result;
} }

View File

@ -1,21 +1,22 @@
#ifndef FSFW_SRC_FSFW_CFDP_PDU_FINISHEDPDUDESERIALIZER_H_ #ifndef FSFW_SRC_FSFW_CFDP_PDU_FINISHEDPDUDESERIALIZER_H_
#define FSFW_SRC_FSFW_CFDP_PDU_FINISHEDPDUDESERIALIZER_H_ #define FSFW_SRC_FSFW_CFDP_PDU_FINISHEDPDUDESERIALIZER_H_
#include "fsfw/cfdp/pdu/FinishedInfo.h"
#include "fsfw/cfdp/pdu/FileDirectiveDeserializer.h" #include "fsfw/cfdp/pdu/FileDirectiveDeserializer.h"
#include "fsfw/cfdp/pdu/FinishedInfo.h"
class FinishPduDeserializer: public FileDirectiveDeserializer { class FinishPduDeserializer : public FileDirectiveDeserializer {
public: public:
FinishPduDeserializer(const uint8_t *pduBuf, size_t maxSize, FinishedInfo& info); FinishPduDeserializer(const uint8_t* pduBuf, size_t maxSize, FinishedInfo& info);
ReturnValue_t parseData() override; ReturnValue_t parseData() override;
FinishedInfo& getInfo(); FinishedInfo& getInfo();
private:
FinishedInfo& finishedInfo;
ReturnValue_t parseTlvs(size_t remLen, size_t currentIdx, const uint8_t* buf, private:
cfdp::ConditionCode conditionCode); FinishedInfo& finishedInfo;
ReturnValue_t parseTlvs(size_t remLen, size_t currentIdx, const uint8_t* buf,
cfdp::ConditionCode conditionCode);
}; };
#endif /* FSFW_SRC_FSFW_CFDP_PDU_FINISHEDPDUDESERIALIZER_H_ */ #endif /* FSFW_SRC_FSFW_CFDP_PDU_FINISHEDPDUDESERIALIZER_H_ */

Some files were not shown because too many files have changed in this diff Show More