Skip to content

Commit

Permalink
Switch the detection of i2c and spi buses on udev.
Browse files Browse the repository at this point in the history
  • Loading branch information
epsilonrt committed Oct 7, 2018
1 parent 08b5fef commit c4b7c95
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 114 deletions.
2 changes: 1 addition & 1 deletion include/piduino/arduino/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TwoWire : public Piduino::I2cDev {
TwoWire() : Piduino::I2cDev() {}
virtual ~TwoWire() {}
void begin() {
setBus (defaultBus());
setBus (Info::defaultBus());
open ();
}
void begin (int id) {
Expand Down
16 changes: 10 additions & 6 deletions include/piduino/i2cdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef PIDUINO_I2CDEV_H
#define PIDUINO_I2CDEV_H

#include <map>
#include <deque>
#include <string>
#include <piduino/iodevice.h>

Expand All @@ -37,7 +37,7 @@ namespace Piduino {
class Info {
public:
static const int MaxBuses = 32;
Info(int id = 0) {
Info (int id = 0) {
setId (id);
}
inline int id() const {
Expand All @@ -57,8 +57,15 @@ namespace Piduino {
bool operator!= (const Info & other) {
return (_path != other._path) ;
}

static std::string busPath (int id);

static Info defaultBus ();
/**
Returns a list of available serial ports on the system.
*/
static std::deque<Info> availableBuses ();


private:
int _id;
std::string _path;
Expand Down Expand Up @@ -116,9 +123,6 @@ namespace Piduino {

virtual void flush ();

static std::map<int, Info> availableBuses ();
static Info defaultBus ();

protected:
class Private;
I2cDev (Private &dd);
Expand Down
47 changes: 23 additions & 24 deletions include/piduino/spidev.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef PIDUINO_SPIDEV_H
#define PIDUINO_SPIDEV_H

#include <vector>
#include <deque>
#include <map>
#include <string>
#include <piduino/iodevice.h>
Expand Down Expand Up @@ -81,27 +81,27 @@ namespace Piduino {

Cs() : _id (-1), _pin (0), _mode (Pin::ModeUnknown),
_driverControl (true), _activeLevel (false) {}

inline int id() const {
return _id;
}

inline Pin * pin() const {
return _pin;
}

inline Pin::Mode mode() const {
return _mode;
}

inline bool driverControl() const {
return _driverControl;
}

inline bool activeLevel() const {
return _activeLevel;
}

bool setDriverControl (bool enable = false, bool activeLevel = false);
bool get() const;
void set (bool value);
Expand Down Expand Up @@ -133,22 +133,22 @@ namespace Piduino {
public:
static const int MaxBuses = 32;
static const int MaxCs = 32;

Info (int bus = 0, int cs = 0) {
setId (bus, cs);
}

void setId (int bus, int cs = 0);
bool setPath (const std::string & path);

inline int busId() const {
return _bus;
}

inline int csId() const {
return _cs;
}

inline const std::string & path() const {
return _path;
}
Expand All @@ -169,6 +169,17 @@ namespace Piduino {
return (_path != other._path) ;
}

/**
* @brief Liste des bus disponibles sur le systèmes
*/
static std::deque<SpiDev::Info> availableBuses ();

/**
* @brief Information sur le bus SPI par défaut disponible sur la carte Pi
* Dépend du modèle de carte (informations stockées dans la base de données)
*/
static Info defaultBus ();

/**
* @brief Chemin système correspondant à un bus
* @param bus identifiant du bus
Expand Down Expand Up @@ -214,7 +225,7 @@ namespace Piduino {
}

bool operator!= (const Settings & other) {
return !(*this == other);
return ! (*this == other);
}

/*
Expand Down Expand Up @@ -470,18 +481,6 @@ namespace Piduino {
*/
bool bitOrder() const;

/**
* @brief Liste des bus disponibles sur le systèmes
* Dépend du modèle de carte (informations stockées dans la base de données)
*/
static std::vector<SpiDev::Info> availableBuses ();

/**
* @brief Information sur le bus SPI par défaut disponible sur la carte Pi
* Dépend du modèle de carte (informations stockées dans la base de données)
*/
static Info defaultBus ();

protected:
class Private;
SpiDev (Private &dd);
Expand Down
2 changes: 1 addition & 1 deletion src/arduino/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Piduino {
}
else {

cs = & SpiDev::defaultBus().cs();
cs = & SpiDev::Info::defaultBus().cs();
}
if (gpio.open()) {

Expand Down
2 changes: 1 addition & 1 deletion src/arduino/spi/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// Initialize the SPI library
void SPIClass::begin() {

setBus (defaultBus());
setBus (Info::defaultBus());
setSettings (SPISettings());
open ();
}
Expand Down
93 changes: 57 additions & 36 deletions src/i2c/i2cdev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <libudev.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
Expand Down Expand Up @@ -83,15 +84,15 @@ namespace Piduino {
// -----------------------------------------------------------------------------

// ---------------------------------------------------------------------------
bool
bool
I2cDev::Info::setPath (const std::string & p) {

for (int i = 0; i < MaxBuses; i++) {
std::string bp = busPath(i);
std::string bp = busPath (i);

if (bp == p) {
setId(i);

setId (i);
return true;
}
}
Expand All @@ -107,6 +108,53 @@ namespace Piduino {
return std::string (path);
}

// ---------------------------------------------------------------------------
std::deque<I2cDev::Info>
I2cDev::Info::availableBuses () {
std::deque<I2cDev::Info> buses;
struct udev *udev;

udev = udev_new();
if (udev) {
struct udev_enumerate * enumerate;
struct udev_list_entry * devices, * dev_list_entry;
struct udev_device * dev;

enumerate = udev_enumerate_new (udev);
udev_enumerate_add_match_subsystem (enumerate, "i2c-dev");
udev_enumerate_scan_devices (enumerate);
devices = udev_enumerate_get_list_entry (enumerate);

udev_list_entry_foreach (dev_list_entry, devices) {

dev = udev_device_new_from_syspath (
udev, udev_list_entry_get_name (dev_list_entry));

if (!dev) {
break;
}

const char * path = udev_device_get_devnode (dev);
if (path) {
Info bus;
if (bus.setPath (path)) {
buses.push_back(bus);
}
}
}
udev_unref (udev);
}

return buses;
}

// ---------------------------------------------------------------------------
I2cDev::Info
I2cDev::Info::defaultBus () {

return Info (db.board().defaultI2cBus());
}

// -----------------------------------------------------------------------------
//
// I2cDev Class
Expand Down Expand Up @@ -165,7 +213,7 @@ namespace Piduino {
d->flush();
d->rxbuf.clear();

d->fd = ::open (d->bus.path().c_str(), d->modeToPosixFlags(mode));
d->fd = ::open (d->bus.path().c_str(), d->modeToPosixFlags (mode));
if (d->fd < 0) {

d->setError();
Expand Down Expand Up @@ -262,8 +310,8 @@ namespace Piduino {
void
I2cDev::setBusPath (const char * path) {
PIMP_D (I2cDev);
setBusPath (std::string(path));

setBusPath (std::string (path));
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -436,33 +484,6 @@ namespace Piduino {
d->flush();
}
}


// ---------------------------------------------------------------------------
std::map<int, I2cDev::Info>
I2cDev::availableBuses () {
std::map<int, I2cDev::Info> buses;

for (int id = 0; id < db.board().soc().i2cCount() ; id++) {
std::string path = Info::busPath (id);

if (System::fileExist (path)) {
Info bus(id);

buses[id] = bus;
}

}
return buses;
}

// ---------------------------------------------------------------------------
I2cDev::Info
I2cDev::defaultBus () {

return Info(db.board().defaultI2cBus());
}

}

/* ========================================================================== */
5 changes: 0 additions & 5 deletions src/serial/serialportinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#if 0
#include <cstdio>
#include <stdlib.h>
#endif

#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
Expand Down
Loading

0 comments on commit c4b7c95

Please sign in to comment.