Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -193,51 +193,51 @@ function detect_machine {
soc="BCM2837"
fi
;;
sun4i|Sun4iw1p1)
*sun4i*|*Sun4iw1p1*)
soc="A10"
;;
sun5i|Sun4iw2p1)
*sun5i*|*Sun4iw2p1*)
soc="A13"
;;
Sun4iw2p2)
*Sun4iw2p2*)
soc="A12"
;;
Sun4iw2p3)
*Sun4iw2p3*)
soc="A10s"
;;
sun6i|Sun8iw1p1)
*sun6i*|*Sun8iw1p1*)
soc="A31"
;;
Sun8iw1p2)
*Sun8iw1p2*)
soc="A31s"
;;
sun7i|Sun8iw2p1)
*sun7i*|*Sun8iw2p1*)
soc="A20"
if [[ $machine == "Banana Pi"* ]]; then
tp="BananaPi"
elif [[ $machine == "Banana Pro"* ]]; then
tp="BananaPro"
fi
;;
sun8i|Sun8iw7p1)
*sun8i*|*Sun8iw7p1*)
soc="H3"
;;
Sun8iw3p1)
*Sun8iw3p1*)
soc="A23"
;;
Sun8iw5p1)
*Sun8iw5p1*)
soc="A33"
;;
Sun8iw6p1)
*Sun8iw6p1*)
soc="A83t"
;;
sun9i|Sun9iw1p1)
*sun9i*|*Sun9iw1p1*)
soc="A80"
;;
Sun9iw1p2)
*Sun9iw1p2*)
soc="A80t"
;;
sun50i|Sun50iw1p1)
*sun50i*|*Sun50iw1p1*)
soc="A64"
;;
'Generic AM33XX'*)
Expand Down
34 changes: 19 additions & 15 deletions drivers/BCM/BCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ BCMClass::~BCMClass()
}
}

uint8_t BCMClass::init()
{
if (!bcm2835_init()) {
logError("Failed to initialized bcm2835.\n");
exit(1);
}
initialized = 1;

return 1;
}

void BCMClass::pinMode(uint8_t gpio, uint8_t mode)
{
if (!initialized) {
if (!bcm2835_init()) {
logError("Failed to initialized bcm2835.\n");
exit(1);
}
initialized = 1;
init();
}

bcm2835_gpio_fsel(gpio, mode);
Expand All @@ -50,11 +57,7 @@ void BCMClass::pinMode(uint8_t gpio, uint8_t mode)
void BCMClass::digitalWrite(uint8_t gpio, uint8_t value)
{
if (!initialized) {
if (!bcm2835_init()) {
logError("Failed to initialized bcm2835.\n");
exit(1);
}
initialized = 1;
init();
}

bcm2835_gpio_write(gpio, value);
Expand All @@ -65,12 +68,13 @@ void BCMClass::digitalWrite(uint8_t gpio, uint8_t value)
uint8_t BCMClass::digitalRead(uint8_t gpio)
{
if (!initialized) {
if (!bcm2835_init()) {
logError("Failed to initialized bcm2835.\n");
exit(1);
}
initialized = 1;
init();
}

return bcm2835_gpio_lev(gpio);
}

uint8_t BCMClass::isInitialized()
{
return initialized;
}
12 changes: 12 additions & 0 deletions drivers/BCM/BCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class BCMClass
* @brief BCMClass destructor.
*/
~BCMClass();
/**
* @brief Initializes BCM.
*
* @return 1 if successful, else exits the program.
*/
uint8_t init();
/**
* @brief Configures the specified pin to behave either as an input or an output.
*
Expand Down Expand Up @@ -65,6 +71,12 @@ class BCMClass
* @return The GPIO pin number.
*/
inline uint8_t digitalPinToInterrupt(uint8_t gpio);
/**
* @brief Checks if SPI was initialized.
*
* @return 1 if initialized, else 0.
*/
uint8_t isInitialized();

private:
static uint8_t initialized; //!< @brief BCM initialized flag.
Expand Down
3 changes: 3 additions & 0 deletions drivers/BCM/SPIBCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ uint8_t SPIBCMClass::initialized = 0;
void SPIBCMClass::begin()
{
if (!initialized) {
if (!BCM.isInitialized()) {
BCM.init();
}
if (!bcm2835_spi_begin()) {
logError("You need root privilege to use SPI.\n");
exit(1);
Expand Down
125 changes: 102 additions & 23 deletions drivers/Linux/SPIDEV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ SPIDEVClass SPIDEV = SPIDEVClass();
uint8_t SPIDEVClass::initialized = 0;
int SPIDEVClass::fd = -1;
std::string SPIDEVClass::device = SPI_SPIDEV_DEVICE;
uint8_t SPIDEVClass::mode = SPI_MODE0;
uint32_t SPIDEVClass::speed = SPI_CLOCK_BASE;
uint32_t SPIDEVClass::speed_temp = SPI_CLOCK_BASE;
struct spi_ioc_transfer SPIDEVClass::tr = {0,0,0,0,0,8,1,0,0,0}; // 8 bits_per_word, 1 cs_change
uint8_t SPIDEVClass::bit_order = MSBFIRST;
struct spi_ioc_transfer SPIDEVClass::tr = {0,0,0,0,0,8,0,0,0,0}; // 8 bits_per_word, 0 cs_change

SPIDEVClass::SPIDEVClass()
{
Expand Down Expand Up @@ -78,15 +79,21 @@ void SPIDEVClass::end()
}
}

void SPIDEVClass::setBitOrder(uint8_t bit_order)
void SPIDEVClass::setBitOrder(uint8_t border)
{
pthread_mutex_lock(&spiMutex);

/*
* bit order
*/
int lsb_setting = bit_order;
int ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb_setting);
bit_order = border;
int ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
if (ret == -1) {
logError("Can't set SPI bit order.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &bit_order);
if (ret == -1) {
logError("Can't set SPI bit order.\n");
abort();
Expand All @@ -102,7 +109,14 @@ void SPIDEVClass::setDataMode(uint8_t data_mode)
/*
* spi mode
*/
int ret = ioctl(fd, SPI_IOC_WR_MODE, &data_mode);
mode = data_mode;
int ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
logError("Can't set SPI mode.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1) {
logError("Can't set SPI mode.\n");
abort();
Expand All @@ -113,6 +127,10 @@ void SPIDEVClass::setDataMode(uint8_t data_mode)

void SPIDEVClass::setClockDivider(uint16_t divider)
{
if (divider == 0) {
return;
}

pthread_mutex_lock(&spiMutex);

/*
Expand All @@ -125,6 +143,12 @@ void SPIDEVClass::setClockDivider(uint16_t divider)
abort();
}

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1) {
logError("Can't set SPI max speed hz.\n");
abort();
}

pthread_mutex_unlock(&spiMutex);
}

Expand Down Expand Up @@ -195,29 +219,63 @@ void SPIDEVClass::beginTransaction(SPISettings settings)
/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE, &settings.dmode);
if (ret == -1) {
logError("Can't set spi mode.\n");
abort();
if (settings.dmode != mode) {
mode = settings.dmode;

ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
logError("Can't set spi mode.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1) {
logError("Can't set spi mode.\n");
abort();
}
}

// Save the current speed
speed_temp = speed;
speed = settings.clock;
/*
* speed
*/
if (settings.clock != speed) {
speed = settings.clock;

ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1) {
logError("Can't set SPI max speed hz.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1) {
logError("Can't set SPI max speed hz.\n");
abort();
}
}

/*
* bit order
*/
ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &settings.border);
if (ret == -1) {
logError("Can't set bits per word.\n");
abort();
if (settings.border != bit_order) {
bit_order = settings.border;

ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
if (ret == -1) {
logError("Can't set SPI bit order.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &bit_order);
if (ret == -1) {
logError("Can't set SPI bit order.\n");
abort();
}
}
}

void SPIDEVClass::endTransaction()
{
speed = speed_temp;
pthread_mutex_unlock(&spiMutex);
}

Expand Down Expand Up @@ -248,38 +306,59 @@ void SPIDEVClass::init()
/*
* spi mode
*/
uint8_t mode = SPI_MODE0;
int ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
logError("Can't set SPI mode.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1) {
logError("Can't set SPI mode.\n");
abort();
}

/*
* bits per word
*/
uint8_t bits = 0; // 0 corresponds to 8 bits per word
uint8_t bits = 8; // 8 bits per word
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) {
logError("Can't set SPI bits per word.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1) {
logError("Can't set SPI bits per word.\n");
abort();
}

/*
* max speed hz
*/
speed = SPI_CLOCK_BASE;
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1) {
logError("Can't set SPI max speed hz.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1) {
logError("Can't set SPI max speed hz.\n");
abort();
}

/*
* bit order
*/
int lsb_setting = MSBFIRST;
ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb_setting);
ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
if (ret == -1) {
logError("Can't set SPI bit order.\n");
abort();
}

ret = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &bit_order);
if (ret == -1) {
logError("Can't set SPI bit order.\n");
abort();
Expand Down
3 changes: 2 additions & 1 deletion drivers/Linux/SPIDEV.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ class SPIDEVClass
static uint8_t initialized; //!< @brief SPI initialized flag.
static int fd; //!< @brief SPI device file descriptor.
static std::string device; //!< @brief Default SPI device.
static uint8_t mode; //!< @brief SPI mode.
static uint32_t speed; //!< @brief SPI speed.
static uint32_t speed_temp; //!< @brief Used when doing transaction.
static uint8_t bit_order; //!< @brief SPI bit order.
static struct spi_ioc_transfer tr; //!< @brief Auxiliar struct for data transfer.

static void init();
Expand Down
Loading