diff --git a/utility/MRAA/gpio.cpp b/utility/MRAA/gpio.cpp index 543228fb..ab41421a 100644 --- a/utility/MRAA/gpio.cpp +++ b/utility/MRAA/gpio.cpp @@ -4,6 +4,7 @@ */ #include +#include // mraa_strresult() #include "gpio.h" // cache for mraa::Gpio instances @@ -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::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); } } @@ -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::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"); - } } diff --git a/utility/MRAA/spi.cpp b/utility/MRAA/spi.cpp index 1e5c0634..a689576d 100644 --- a/utility/MRAA/spi.cpp +++ b/utility/MRAA/spi.cpp @@ -1,6 +1,6 @@ +#include // mraa_strresult(), mraa_result_t #include "spi.h" -#include "mraa.h" SPI::SPI() { @@ -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() diff --git a/utility/MRAA/spi.h b/utility/MRAA/spi.h index f5fb246c..3475b363 100644 --- a/utility/MRAA/spi.h +++ b/utility/MRAA/spi.h @@ -10,10 +10,23 @@ * Class declaration for SPI helper files */ -#include "mraa.h" -#include "mraa.hpp" +#include // std::exception, std::string +#include // 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 { @@ -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); };