Skip to content

Commit

Permalink
improve MRAA exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Mar 27, 2024
1 parent 0fc9c28 commit d4732bb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
24 changes: 16 additions & 8 deletions utility/MRAA/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <map>
#include <mraa.h> // mraa_strresult()
#include "gpio.h"

// cache for mraa::Gpio instances
Expand All @@ -25,15 +26,22 @@ GPIO::~GPIO()

void GPIO::open(rf24_gpio_pin_t port, mraa::Dir DDR)
{
mraa::Result status;

// check that mraa::Gpio context doesn't already exist
std::map<rf24_gpio_pin_t, mraa::Gpio*>::iterator i = gpio_cache.find(port);
if (i == gpio_cache.end()) {
mraa::Gpio* gpio_inst = new mraa::Gpio(port);
gpio_cache[port] = gpio_inst;
gpio_inst->dir(DDR);
status = gpio_inst->dir(DDR);
}
else {
i->second->dir(DDR);
status = i->second->dir(DDR);
}
if (status != mraa::SUCCESS) {
std::string msg = "[GPIO::open] Could not set the pin direction; ";
msg += mraa_strresult((mraa_result_t)status);
throw GPIOException(msg);
}
}

Expand All @@ -60,17 +68,17 @@ int GPIO::read(rf24_gpio_pin_t port)

void GPIO::write(rf24_gpio_pin_t port, int value)
{
mraa::Result result = mraa::Result::ERROR_UNSPECIFIED; // a default
// get cache gpio instance
std::map<rf24_gpio_pin_t, mraa::Gpio*>::iterator i = gpio_cache.find(port);
if (i != gpio_cache.end()) {
result = i->second->write(value);
mraa::Result result = i->second->write(value);
if (result != mraa::Result::SUCCESS) {
std::string msg = "[GPIO::write] Could not set pin output value; ";
msg += mraa_strresult((mraa_result_t)result);
throw GPIOException(msg);
}
}
else {
throw GPIOException("[GPIO::write] pin was not initialized with GPIO::open()");
}

if (result != mraa::Result::SUCCESS) {
throw GPIOException("GPIO::write() failed");
}
}
29 changes: 25 additions & 4 deletions utility/MRAA/spi.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <mraa.h> // mraa_strresult(), mraa_result_t
#include "spi.h"
#include "mraa.h"

SPI::SPI()
{
Expand All @@ -12,11 +12,32 @@ void SPI::begin(int busNo, uint32_t spi_speed)
// init mraa spi bus, it will handle chip select internally. For CS pin wiring user must check SPI details in hardware manual
mspi = new mraa::Spi(busNo);

mspi->mode(mraa::SPI_MODE0);
mspi->bitPerWord(8);
mraa::Result result;

result = mspi->mode(mraa::SPI_MODE0);
if (result != mraa::Result::SUCCESS) {
std::string msg = "[SPI::begin] Could not set bus mode;";
msg += mraa_strresult((mraa_result_t)result);
throw SPIException(msg);
return;
}

result = mspi->bitPerWord(8);
if (result != mraa::Result::SUCCESS) {
std::string msg = "[SPI::begin] Could not set bus bits per word;";
msg += mraa_strresult((mraa_result_t)result);
throw SPIException(msg);
return;
}

// Prophet: this will try to set 8MHz, however MRAA will reset to max platform speed and syslog a message of it
mspi->frequency(spi_speed);
result = mspi->frequency(spi_speed);
if (result != mraa::Result::SUCCESS) {
std::string msg = "[SPI::begin] Could not set bus frequency;";
msg += mraa_strresult((mraa_result_t)result);
throw SPIException(msg);
return;
}
}

void SPI::end()
Expand Down
23 changes: 20 additions & 3 deletions utility/MRAA/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
* Class declaration for SPI helper files
*/

#include "mraa.h"
#include "mraa.hpp"
#include <stdexcept> // std::exception, std::string
#include <mraa.hpp> // mraa::

#include "../../RF24_config.h" // This is cyclical and should be fixed
/** @brief The default SPI speed (in Hz) */
#ifndef RF24_SPI_SPEED
#define RF24_SPI_SPEED 10000000
#endif

/** Specific exception for SPI errors */
class SPIException : public std::runtime_error
{
public:
explicit SPIException(const std::string& msg)
: std::runtime_error(msg)
{
}
};

class SPI
{
Expand All @@ -34,12 +47,16 @@ class SPI

void end();

// not actually used in Linux
void setBitOrder(uint8_t bit_order);

// not actually used in Linux
void setDataMode(uint8_t data_mode);

// not actually used in Linux
void setClockDivider(uint32_t spi_speed);

// not actually used in Linux
void chipSelect(int csn_pin);
};

Expand Down

0 comments on commit d4732bb

Please sign in to comment.