2015-04-06 13:13:11 -03:00
|
|
|
#include "GPIO_Sysfs.h"
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
#include "Util.h"
|
|
|
|
|
2015-04-06 13:13:11 -03:00
|
|
|
#define LOW 0
|
|
|
|
#define HIGH 1
|
|
|
|
#define assert_vpin(v_, max_, ...) do { \
|
|
|
|
if (v_ >= max_) { \
|
|
|
|
hal.console->printf("warning (%s): vpin %u out of range [0, %u)\n",\
|
|
|
|
__PRETTY_FUNCTION__, v_, max_); \
|
|
|
|
return __VA_ARGS__; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
|
|
|
static const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
#define UINT32_MAX_STR "4294967295"
|
|
|
|
|
|
|
|
/* Trick to use minimum stack space for each of the params */
|
|
|
|
union gpio_params {
|
|
|
|
char export_gpio[sizeof("export")];
|
|
|
|
char direction[sizeof("gpio" UINT32_MAX_STR "/direction")];
|
|
|
|
char value[sizeof("gpio" UINT32_MAX_STR "/value")];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define GPIO_BASE_PATH "/sys/class/gpio/"
|
|
|
|
#define GPIO_PATH_MAX (sizeof(GPIO_BASE_PATH) + sizeof(gpio_params) - 1)
|
|
|
|
|
2015-04-06 13:13:11 -03:00
|
|
|
DigitalSource_Sysfs::DigitalSource_Sysfs(unsigned pin, int value_fd)
|
|
|
|
: _pin(pin)
|
|
|
|
, _value_fd(value_fd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DigitalSource_Sysfs::~DigitalSource_Sysfs()
|
|
|
|
{
|
|
|
|
if (_value_fd >= 0) {
|
|
|
|
::close(_value_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t DigitalSource_Sysfs::read()
|
|
|
|
{
|
|
|
|
char char_value;
|
2015-11-18 12:30:17 -04:00
|
|
|
if (::pread(_value_fd, &char_value, 1, 0) < 0) {
|
|
|
|
hal.console->printf("DigitalSource_Sysfs: Unable to read pin %u value.\n",
|
2015-04-06 13:13:11 -03:00
|
|
|
_pin);
|
|
|
|
return LOW;
|
|
|
|
}
|
2015-11-18 12:30:17 -04:00
|
|
|
return char_value - '0';
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DigitalSource_Sysfs::write(uint8_t value)
|
|
|
|
{
|
2015-11-18 12:30:17 -04:00
|
|
|
if (::pwrite(_value_fd, value == HIGH ? "1" : "0", 1, 0) < 0) {
|
|
|
|
hal.console->printf("DigitalSource_Sysfs: Unable to write pin %u value.\n",
|
2015-04-06 13:13:11 -03:00
|
|
|
_pin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DigitalSource_Sysfs::mode(uint8_t output)
|
|
|
|
{
|
|
|
|
auto gpio_sysfs = GPIO_Sysfs::from(hal.gpio);
|
|
|
|
gpio_sysfs->_pinMode(_pin, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DigitalSource_Sysfs::toggle()
|
|
|
|
{
|
|
|
|
write(!read());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_Sysfs::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_Sysfs::pinMode(uint8_t vpin, uint8_t output)
|
|
|
|
{
|
|
|
|
assert_vpin(vpin, n_pins);
|
|
|
|
|
2015-11-19 12:35:56 -04:00
|
|
|
_export_pin(vpin);
|
|
|
|
_pinMode(pin_table[vpin], output);
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_Sysfs::_pinMode(unsigned int pin, uint8_t output)
|
|
|
|
{
|
2015-11-18 12:30:17 -04:00
|
|
|
const char *dir = output ? "out" : "in";
|
|
|
|
char path[GPIO_PATH_MAX];
|
2015-04-06 13:13:11 -03:00
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
int r = snprintf(path, GPIO_PATH_MAX, GPIO_BASE_PATH "gpio%u/direction",
|
|
|
|
pin);
|
|
|
|
if (r < 0 || r >= (int)GPIO_PATH_MAX
|
|
|
|
|| Util::from(hal.util)->write_file(path, "%s", dir) < 0) {
|
|
|
|
hal.console->printf("GPIO_Sysfs: Unable to set pin %u mode.\n", pin);
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int GPIO_Sysfs::_open_pin_value(unsigned int pin, int flags)
|
|
|
|
{
|
2015-11-18 12:30:17 -04:00
|
|
|
char path[GPIO_PATH_MAX];
|
2015-04-06 13:13:11 -03:00
|
|
|
int fd;
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
int r = snprintf(path, GPIO_PATH_MAX, GPIO_BASE_PATH "gpio%u/value", pin);
|
|
|
|
if (r < 0 || r >= (int)GPIO_PATH_MAX
|
|
|
|
|| (fd = open(path, flags | O_CLOEXEC)) < 0) {
|
|
|
|
hal.console->printf("GPIO_Sysfs: Unable to get value file descriptor for pin %u.\n",
|
|
|
|
pin);
|
2015-04-06 13:13:11 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t GPIO_Sysfs::read(uint8_t vpin)
|
|
|
|
{
|
|
|
|
assert_vpin(vpin, n_pins, LOW);
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
const unsigned pin = pin_table[vpin];
|
2015-04-06 13:13:11 -03:00
|
|
|
int fd = _open_pin_value(pin, O_RDONLY);
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
2015-04-06 13:13:11 -03:00
|
|
|
|
|
|
|
char char_value;
|
2015-11-18 12:30:17 -04:00
|
|
|
if (::pread(fd, &char_value, 1, 0) < 0) {
|
|
|
|
goto error;
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
return char_value - '0';
|
|
|
|
|
|
|
|
error:
|
|
|
|
hal.console->printf("GPIO_Sysfs: Unable to read pin %u value.\n", vpin);
|
|
|
|
return LOW;
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_Sysfs::write(uint8_t vpin, uint8_t value)
|
|
|
|
{
|
|
|
|
assert_vpin(vpin, n_pins);
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
const unsigned pin = pin_table[vpin];
|
2015-04-06 13:13:11 -03:00
|
|
|
int fd = _open_pin_value(pin, O_WRONLY);
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
2015-04-06 13:13:11 -03:00
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
if (::pwrite(fd, value == HIGH ? "1" : "0", 1, 0) < 0) {
|
|
|
|
goto error;
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2015-11-18 12:30:17 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
hal.console->printf("GPIO_Sysfs: Unable to write pin %u value.\n", vpin);
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_Sysfs::toggle(uint8_t vpin)
|
|
|
|
{
|
|
|
|
write(vpin, !read(vpin));
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL::DigitalSource* GPIO_Sysfs::channel(uint16_t vpin)
|
|
|
|
{
|
|
|
|
assert_vpin(vpin, n_pins, nullptr);
|
|
|
|
|
2015-11-18 12:30:17 -04:00
|
|
|
const unsigned pin = pin_table[vpin];
|
2015-04-06 13:13:11 -03:00
|
|
|
int value_fd = -1;
|
|
|
|
|
2015-11-19 12:35:56 -04:00
|
|
|
if (_export_pin(vpin)) {
|
2015-11-18 12:30:17 -04:00
|
|
|
value_fd = _open_pin_value(pin, O_RDWR);
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Even if we couldn't open the fd, return a new DigitalSource and let
|
|
|
|
* reads and writes fail later due to invalid. Otherwise we
|
|
|
|
* could crash in undesired places */
|
|
|
|
return new DigitalSource_Sysfs(pin, value_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GPIO_Sysfs::attach_interrupt(uint8_t interrupt_num, AP_HAL::Proc p,
|
|
|
|
uint8_t mode)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GPIO_Sysfs::usb_connected(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-19 12:35:56 -04:00
|
|
|
bool GPIO_Sysfs::_export_pin(uint8_t vpin)
|
2015-04-06 13:13:11 -03:00
|
|
|
{
|
2015-12-30 17:01:17 -04:00
|
|
|
assert_vpin(vpin, n_pins, false);
|
2015-04-06 13:13:11 -03:00
|
|
|
|
2015-12-30 17:01:17 -04:00
|
|
|
const unsigned int pin = pin_table[vpin];
|
|
|
|
char gpio_path[GPIO_PATH_MAX];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
int r = snprintf(gpio_path, GPIO_PATH_MAX, GPIO_BASE_PATH "gpio%u", pin);
|
|
|
|
if (r < 0 || r >= (int) GPIO_PATH_MAX) {
|
|
|
|
goto fail_snprintf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(gpio_path, F_OK) == 0) {
|
|
|
|
// Already exported
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(GPIO_BASE_PATH "export", O_WRONLY | O_CLOEXEC);
|
2015-04-06 13:13:11 -03:00
|
|
|
if (fd < 0) {
|
2015-12-30 17:01:17 -04:00
|
|
|
goto fail_open;
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
2015-12-30 17:01:17 -04:00
|
|
|
// Try to export
|
|
|
|
if (dprintf(fd, "%u", pin) < 0) {
|
|
|
|
goto fail_export;
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2015-12-30 17:01:17 -04:00
|
|
|
return true;
|
2015-04-06 13:13:11 -03:00
|
|
|
|
2015-12-30 17:01:17 -04:00
|
|
|
fail_export:
|
|
|
|
close(fd);
|
|
|
|
fail_open:
|
|
|
|
fail_snprintf:
|
|
|
|
hal.console->printf("GPIO_Sysfs: Unable to export pin %u.\n", pin);
|
|
|
|
return false;
|
2015-04-06 13:13:11 -03:00
|
|
|
}
|