added gpio sysfs if
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
target_sources(${TARGET_NAME} PRIVATE
|
||||
SpiTest.cpp
|
||||
RPiGPIO.cpp
|
||||
)
|
||||
|
||||
|
||||
|
123
bsp_rpi/boardtest/RPiGPIO.cpp
Normal file
123
bsp_rpi/boardtest/RPiGPIO.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include "RPiGPIO.h"
|
||||
|
||||
#include <fsfw/serviceinterface/ServiceInterface.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
int RPiGPIO::enablePin(int pin) {
|
||||
char buffer[BUFFER_MAX];
|
||||
ssize_t bytes_written;
|
||||
int fd;
|
||||
|
||||
fd = open("/sys/class/gpio/export", O_WRONLY);
|
||||
if (fd == -1) {
|
||||
sif::error << "Failed to open export of pin " << pin << " for writing!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
|
||||
write(fd, buffer, bytes_written);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RPiGPIO::disablePin(int pin) {
|
||||
char buffer[BUFFER_MAX];
|
||||
ssize_t bytes_written;
|
||||
int fd;
|
||||
|
||||
fd = open("/sys/class/gpio/unexport", O_WRONLY);
|
||||
if (fd == -1) {
|
||||
sif::error << "Failed to open unexport of pin " << pin << " for writing!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
|
||||
write(fd, buffer, bytes_written);
|
||||
close(fd);
|
||||
return(0);
|
||||
}
|
||||
|
||||
int RPiGPIO::pinDirection(int pin, Directions dir) {
|
||||
|
||||
char path[DIRECTION_MAX];
|
||||
|
||||
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
|
||||
int fd = open(path, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
sif::error << "Failed to open gpio " << pin << " direction for writing!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
if(dir == Directions::IN) {
|
||||
result = write(fd, "in", IN_WRITE_SIZE);
|
||||
}
|
||||
else {
|
||||
result = write(fd, "out", OUT_WRITE_SIZE);
|
||||
}
|
||||
|
||||
if (result != 0) {
|
||||
sif::error << "Failed to set direction!" << std::endl;
|
||||
return -2;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RPiGPIO::readPin(int pin) {
|
||||
char path[VALUE_MAX];
|
||||
char value_str[3];
|
||||
|
||||
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
sif::error << "RPiGPIO::readPin: Failed to open GPIO pin " << pin << "!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (read(fd, value_str, 3) == -1) {
|
||||
sif::error << "Failed to read value!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
char* endPtr = nullptr;
|
||||
|
||||
return strtol(value_str, &endPtr, 10);
|
||||
}
|
||||
|
||||
int RPiGPIO::writePin(int pin, States state) {
|
||||
|
||||
char path[VALUE_MAX];
|
||||
int fd;
|
||||
|
||||
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
|
||||
fd = open(path, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
sif::error << "RPiGPIO::writePin: Failed to open GPIO pin " << pin << "!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
if(state == States::LOW) {
|
||||
result = write(fd, "0", 1);
|
||||
}
|
||||
else {
|
||||
result = write(fd, "1", 1);
|
||||
}
|
||||
|
||||
|
||||
if (result != 0) {
|
||||
sif::error << "Failed to write pin " << pin << " value to " << static_cast<int>(state)
|
||||
<< "!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
41
bsp_rpi/boardtest/RPiGPIO.h
Normal file
41
bsp_rpi/boardtest/RPiGPIO.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef BSP_RPI_BOARDTEST_RPIGPIO_H_
|
||||
#define BSP_RPI_BOARDTEST_RPIGPIO_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* @brief Really simple C++ GPIO wrapper for the Raspberry Pi, using the sysfs interface.
|
||||
* Use BCM pins notation (https://pinout.xyz/#)
|
||||
*
|
||||
*/
|
||||
class RPiGPIO {
|
||||
public:
|
||||
enum Directions {
|
||||
IN = 0,
|
||||
OUT = 1
|
||||
};
|
||||
|
||||
enum States {
|
||||
LOW = 0,
|
||||
HIGH = 1
|
||||
};
|
||||
|
||||
static int enablePin(int pin);
|
||||
static int disablePin(int pin);
|
||||
static int pinDirection(int pin, Directions dir);
|
||||
static int readPin(int pin);
|
||||
static int writePin(int pin, States state);
|
||||
|
||||
private:
|
||||
|
||||
|
||||
static constexpr uint8_t BUFFER_MAX = 3;
|
||||
static constexpr uint8_t DIRECTION_MAX = 35;
|
||||
static constexpr uint8_t VALUE_MAX = 30;
|
||||
|
||||
static constexpr uint8_t IN_WRITE_SIZE = 3;
|
||||
static constexpr uint8_t OUT_WRITE_SIZE = 4;
|
||||
};
|
||||
|
||||
|
||||
#endif /* BSP_RPI_BOARDTEST_RPIGPIO_H_ */
|
@ -1,13 +1,31 @@
|
||||
#include "SpiTest.h"
|
||||
|
||||
#include <fsfw/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include <fsfw/serviceinterface/ServiceInterface.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <wiringPi.h>
|
||||
|
||||
|
||||
|
||||
SpiTest::SpiTest(object_id_t objectId): SystemObject(objectId) {
|
||||
wiringPiSetupGpio();
|
||||
sif::info << "Setting up Raspberry Pi WiringPi library." << std::endl;
|
||||
// wiringPiSetupGpio();
|
||||
|
||||
// pinMode(SS_MGM_0_LIS3, OUTPUT);
|
||||
// pinMode(SS_MGM_1_RM, OUTPUT);
|
||||
// pinMode(SS_GYRO_0_ADIS, OUTPUT);
|
||||
// pinMode(SS_GYRO_1_L3G, OUTPUT);
|
||||
// pinMode(SS_GYRO_2_L3G, OUTPUT);
|
||||
// pinMode(SS_MGM_2_LIS3, OUTPUT);
|
||||
// pinMode(SS_MGM_3_RM, OUTPUT);
|
||||
//
|
||||
// digitalWrite(SS_MGM_0_LIS3, HIGH);
|
||||
// digitalWrite(SS_MGM_1_RM, HIGH);
|
||||
// digitalWrite(SS_GYRO_0_ADIS, HIGH);
|
||||
// digitalWrite(SS_GYRO_1_L3G, HIGH);
|
||||
// digitalWrite(SS_GYRO_2_L3G, HIGH);
|
||||
// digitalWrite(SS_MGM_2_LIS3, HIGH);
|
||||
// digitalWrite(SS_MGM_3_RM, HIGH);
|
||||
|
||||
int spiFd = open(spiDeviceName.c_str(), O_RDWR);
|
||||
if (spiFd < 0){
|
||||
@ -40,4 +58,4 @@ ReturnValue_t SpiTest::initialize() {
|
||||
//transferHandle.speed_hz = 976000;
|
||||
//transferHandle.len = 2;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,27 @@
|
||||
#include <linux/spi/spidev.h>
|
||||
#include <string>
|
||||
|
||||
class SpiTest: public ExecutableObjectIF, SystemObject {
|
||||
class SpiTest:
|
||||
public SystemObject,
|
||||
public ExecutableObjectIF {
|
||||
public:
|
||||
SpiTest(object_id_t objectId);
|
||||
|
||||
ReturnValue_t performOperation(uint8_t opCode) override;
|
||||
ReturnValue_t initialize() override;
|
||||
private:
|
||||
// These chip selects (BCM number) will be pulled high if not used
|
||||
// ACS board specific.
|
||||
enum SpiChipSelects {
|
||||
SS_MGM_0_LIS3 = 0, //!< MGM 0, LIS3MDLTR, U6, A side
|
||||
SS_MGM_1_RM = 1, //!< MGM 1, RM3100, U7, A side
|
||||
SS_GYRO_0_ADIS = 2, //!< Gyro 0, ADIS16485, U3, A side
|
||||
SS_GYRO_1_L3G = 3, //!< Gyro 1, L3GD20H, U4, A side
|
||||
SS_GYRO_2_L3G = 4, //!< Gyro 2, L3GD20h, U5, B side
|
||||
SS_MGM_2_LIS3 = 17, //!< MGM 2, LIS3MDLTR, U8, B side
|
||||
SS_MGM_3_RM = 27, //!< MGM 3, RM3100, U9, B side
|
||||
};
|
||||
|
||||
const std::string spiDeviceName = "/dev/spidev0.0";
|
||||
int spiFd = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user