Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review the Linux kernel driver #7

Merged
merged 1 commit into from
Mar 22, 2024
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
4 changes: 2 additions & 2 deletions src/utility/linux_kernel/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <unistd.h> // close()
#include <fcntl.h> // open()
#include <sys/ioctl.h> // ioctl()
#include <errno.h> // errno, strerror()
#include <string.h> // std::string, strcpy()
#include <errno.h> // errno
#include <string.h> // strerror(), memset(), strcpy()
#include <map>
#include "linux/gpio.h"
#include "gpio.h"
Expand Down
4 changes: 2 additions & 2 deletions src/utility/linux_kernel/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define CIRQUEPINNACLE_UTILITY_LINUX_KERNEL_GPIO_H_
#ifndef ARDUINO

#include <stdexcept>
#include <cstdint>
#include <cstdint> // uintXX_t
#include <stdexcept> // std::exception, std::string
#include "linux/gpio.h" // gpiochip_info

#ifdef __cplusplus
Expand Down
30 changes: 21 additions & 9 deletions src/utility/linux_kernel/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
*/
#ifndef ARDUINO

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h> // free(), malloc()
#include <stddef.h> // size_t
#include <stdint.h> // uintXX_t
#include <stdio.h> // sprintf
#include <unistd.h> // close()
#include <fcntl.h> // open()
#include <sys/ioctl.h> // ioctl()
#include <errno.h> // errno
#include <string.h> // strerror()
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "i2c.h"
Expand Down Expand Up @@ -51,7 +55,9 @@ namespace cirque_pinnacle_arduino_wrappers {
sprintf(filename, "/dev/i2c-%d", busNumber);
file = open(filename, O_RDWR);
if (file < 0) {
throw I2CException("Can't open I2C bus. Check access rights.");
std::string msg = "[TwoWire::begin] Can't open I2C bus; ";
msg += strerror(errno);
throw I2CException(msg);
}
bus_fd = file;
}
Expand All @@ -65,7 +71,9 @@ namespace cirque_pinnacle_arduino_wrappers {
void TwoWire::beginTransmission(uint8_t address)
{
if (ioctl(bus_fd, I2C_SLAVE, address) < 0) {
throw I2CException("Could not select I2C slave address.");
std::string msg = "[TwoWire::beginTransmission] Could not select I2C slave address; ";
msg += strerror(errno);
throw I2CException(msg);
}
xBuffIndex = 0;
xBuffLen = 0;
Expand All @@ -76,7 +84,9 @@ namespace cirque_pinnacle_arduino_wrappers {
(void)sendStop; // param not used in this implementation

if (::write(bus_fd, xBuff, xBuffLen) != xBuffLen) {
throw I2CException("Could not write data to I2C bus.");
std::string msg = "[TwoWire::endTransmission] Could not write data to I2C bus; ";
msg += strerror(errno);
throw I2CException(msg);
}
return xBuffLen;
}
Expand All @@ -102,7 +112,9 @@ namespace cirque_pinnacle_arduino_wrappers {
xBuffIndex = 0;
int retVal = ::read(bus_fd, xBuff, quantity);
if (retVal < 0) {
throw I2CException("Could not read data from I2C bus.");
std::string msg = "[TwoWire::endTransmission] Could not read data from I2C bus; ";
msg += strerror(errno);
throw I2CException(msg);
}
xBuffLen = quantity;
return xBuffLen;
Expand Down
4 changes: 2 additions & 2 deletions src/utility/linux_kernel/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define CIRQUEPINNACLE_UTILITY_LINUX_KERNEL_I2C_H_
#ifndef ARDUINO

#include <cstdint>
#include <stdexcept>
#include <cstdint> // uintXX_t
#include <stdexcept> // std::exception, std::string

#ifdef __cplusplus
extern "C" {
Expand Down
50 changes: 35 additions & 15 deletions src/utility/linux_kernel/spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* SOFTWARE.
*/
#ifndef ARDUINO
#include <fcntl.h>
#include <stdint.h> // uintXX_ts
#include <unistd.h> // close()
#include <fcntl.h> // open()
#include <sys/ioctl.h> // ioctl()
#include <errno.h> // errno,
#include <string.h> // memset() strerror()
#include <linux/spi/spidev.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "spi.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -61,7 +61,11 @@ namespace cirque_pinnacle_arduino_wrappers {

fd = open(device, O_RDWR);
if (fd < 0) {
throw SPIException("can't open device");
std::string msg = "[SPIClass::begin] Could not open device ";
msg += device;
msg += "; ";
msg += strerror(errno);
throw SPIException(msg);
}
spiIsInitialized = true;

Expand All @@ -70,35 +74,47 @@ namespace cirque_pinnacle_arduino_wrappers {
// spi mode
ret = ioctl(fd, SPI_IOC_WR_MODE, &settings.mode);
if (ret == -1) {
throw SPIException("SPI can't set mode");
std::string msg = "[SPIClass::begin] Could not set write mode; ";
msg += strerror(errno);
throw SPIException(msg);
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &settings.mode);
if (ret == -1) {
throw SPIException("SPI can't read mode");
std::string msg = "[SPIClass::begin] Could not set read mode; ";
msg += strerror(errno);
throw SPIException(msg);
}

// bits per word
uint8_t bits = PINNACLE_SPI_BITS_PER_WORD;
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) {
throw SPIException("SPI can't set bits per word");
std::string msg = "[SPIClass::begin] Could not set write bits per word; ";
msg += strerror(errno);
throw SPIException(msg);
}

ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1) {
throw SPIException("SPI can't read bits per word");
std::string msg = "[SPIClass::begin] Could not set read bits per word; ";
msg += strerror(errno);
throw SPIException(msg);
}

// max speed Hz
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &settings.clock);
if (ret == -1) {
throw SPIException("SPI can't set max speed Hz");
std::string msg = "[SPIClass::begin] Could not set write max speed Hz; ";
msg += strerror(errno);
throw SPIException(msg);
}

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &settings.clock);
if (ret == -1) {
throw SPIException("SPI can't read max speed Hz");
std::string msg = "[SPIClass::begin] Could not set read max speed Hz; ";
msg += strerror(errno);
throw SPIException(msg);
}

_spi_speed = settings.clock;
Expand All @@ -120,7 +136,9 @@ namespace cirque_pinnacle_arduino_wrappers {
int ret;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
throw SPIException("SPI ioctl() transfer failed.");
std::string msg = "[SPIClass::transfer(tx)] Could not transfer buffer; ";
msg += strerror(errno);
throw SPIException(msg);
}
return rx;
}
Expand All @@ -140,7 +158,9 @@ namespace cirque_pinnacle_arduino_wrappers {
int ret;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
throw SPIException("SPI ioctl() transfer failed.");
std::string msg = "[SPIClass::transfer(tx, rx, len)] Could not transfer buffer; ";
msg += strerror(errno);
throw SPIException(msg);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utility/linux_kernel/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#define CIRQUEPINNACLE_UTILITY_LINUX_KERNEL_SPI_H_
#ifndef ARDUINO

#include <cstdint>
#include <stdexcept>
#include <cstdint> // uintXX_t
#include <stdexcept> // std::exception, std::string
#include <linux/spi/spidev.h>

#ifdef __cplusplus
Expand Down