ADIS16448: Add string based constructor for Accelerometer

Signed-off-by: Adelin Dobre <adelin.dobre1994@gmail.com>
This commit is contained in:
Adelin Dobre 2018-07-04 16:46:56 +03:00 committed by Stefan Andritoiu
parent b2c46f602e
commit 4aa25f2281
2 changed files with 88 additions and 20 deletions

View File

@ -11,7 +11,7 @@
// //
// This library runs on an Intel Edison and uses mraa to acquire data // This library runs on an Intel Edison and uses mraa to acquire data
// from an ADIS16448. This data is then scaled and printed onto the terminal. // from an ADIS16448. This data is then scaled and printed onto the terminal.
// //
// This software has been tested to connect to an ADIS16448 through a level shifter // This software has been tested to connect to an ADIS16448 through a level shifter
// such as the TI TXB0104. The SPI lines (DIN, DOUT, SCLK, /CS) are all wired through // such as the TI TXB0104. The SPI lines (DIN, DOUT, SCLK, /CS) are all wired through
// the level shifter and the ADIS16448 is also being powered by the Intel Edison. // the level shifter and the ADIS16448 is also being powered by the Intel Edison.
@ -44,6 +44,7 @@
#include <functional> #include <functional>
#include <string.h> #include <string.h>
#include "upm_string_parser.hpp"
#include "adis16448.hpp" #include "adis16448.hpp"
using namespace upm; using namespace upm;
@ -55,24 +56,81 @@ using namespace upm;
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
ADIS16448::ADIS16448(int bus, int rst) ADIS16448::ADIS16448(int bus, int rst)
{ {
// Configure I/O // Configure I/O
//Initialize RST pin //Initialize RST pin
if ( !(_rst = mraa_gpio_init(rst)) ) if ( !(_rst = mraa_gpio_init(rst)) )
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?"); ": mraa_gpio_init() failed, invalid pin?");
return; return;
} }
mraa_gpio_dir(_rst, MRAA_GPIO_IN); //Set direction as INPUT mraa_gpio_dir(_rst, MRAA_GPIO_IN); //Set direction as INPUT
// Configure SPI // Configure SPI
if ( !(_spi = mraa_spi_init(bus)) ) if ( !(_spi = mraa_spi_init(bus)) )
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_init() failed"); ": mraa_spi_init() failed");
return; return;
} }
configSPI(); configSPI();
}
ADIS16448::ADIS16448(std::string initStr) : mraaIo(initStr)
{
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
std::vector<std::string> upmTokens;
if (!mraaIo.getLeftoverStr().empty()) {
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
}
// Configure I/O
//Initialize RST pin
if(!descs->gpios)
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
}
else
{
if( !(_rst = descs->gpios[0]) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
}
}
mraa_gpio_dir(_rst, MRAA_GPIO_IN); //Set direction as INPUT
// Configure SPI
if(!descs->spis)
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_init() failed");
}
else
{
if( !(_spi = descs->spis[0]) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_init() failed");
}
}
configSPI();
std::string::size_type sz;
for (std::string tok : upmTokens) {
if(tok.substr(0,9) == "regWrite:") {
uint8_t regAddr = std::stoi(tok.substr(9),&sz,0);
tok = tok.substr(9);
uint16_t regData = std::stoi(tok.substr(sz+1),nullptr,0);
regWrite(regAddr, regData);
}
}
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
@ -114,7 +172,7 @@ void ADIS16448::resetDUT()
void ADIS16448::configSPI() { void ADIS16448::configSPI() {
mraa_spi_frequency(_spi, 1000000); //Set SPI frequency to 1MHz mraa_spi_frequency(_spi, 1000000); //Set SPI frequency to 1MHz
if ( mraa_spi_mode(_spi, MRAA_SPI_MODE3) != MRAA_SUCCESS ) if ( mraa_spi_mode(_spi, MRAA_SPI_MODE3) != MRAA_SUCCESS )
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_mode() failed"); ": mraa_spi_mode() failed");
@ -122,7 +180,7 @@ void ADIS16448::configSPI() {
} }
//Set # of bits per word //Set # of bits per word
if ( mraa_spi_bit_per_word(_spi, 16) != MRAA_SUCCESS ) if ( mraa_spi_bit_per_word(_spi, 16) != MRAA_SUCCESS )
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_bit_per_word() failed"); ": mraa_spi_bit_per_word() failed");
@ -162,7 +220,8 @@ int16_t ADIS16448::regRead(uint8_t regAddr)
// regData - data to be written to the register // regData - data to be written to the register
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
void ADIS16448::regWrite(uint8_t regAddr,uint16_t regData) void ADIS16448::regWrite(uint8_t regAddr,uint16_t regData)
{ { std::vector<std::string> upmTokens;
configSPI(); configSPI();
// Separate the 16 bit command word into two bytes // Separate the 16 bit command word into two bytes
uint16_t addr = (((regAddr & 0x7F) | 0x80) << 8); //Check that the address is 7 bits, flip the sign bit uint16_t addr = (((regAddr & 0x7F) | 0x80) << 8); //Check that the address is 7 bits, flip the sign bit

View File

@ -39,6 +39,7 @@
#include <string> #include <string>
#include <mraa/spi.h> #include <mraa/spi.h>
#include <mraa/gpio.h> #include <mraa/gpio.h>
#include <mraa/initio.hpp>
// User Register Memory Map from Table 6 of the Datasheet // User Register Memory Map from Table 6 of the Datasheet
#define FLASH_CNT 0x00 //Flash memory write count #define FLASH_CNT 0x00 //Flash memory write count
@ -113,6 +114,13 @@ namespace upm {
*/ */
ADIS16448(int bus, int rst); ADIS16448(int bus, int rst);
/**
* Instantiates ADIS16448 Accelerometer object based on a given string.
*
* @param initStr string containing specific information for ADIS16448 initialization.
*/
ADIS16448(std::string initStr);
/** /**
* Destructor * Destructor
*/ */
@ -165,6 +173,7 @@ namespace upm {
private: private:
mraa::MraaIo mraaIo;
mraa_spi_context _spi; mraa_spi_context _spi;
mraa_gpio_context _rst; mraa_gpio_context _rst;