14 #define assert_vpin(v_, max_, ...) do { \ 16 hal.console->printf("warning (%s): vpin %u out of range [0, %u)\n",\ 17 __PRETTY_FUNCTION__, v_, max_); \ 22 using namespace Linux;
26 #define UINT32_MAX_STR "4294967295" 30 char export_gpio[
sizeof(
"export")];
35 #define GPIO_BASE_PATH "/sys/class/gpio/" 36 #define GPIO_PATH_MAX (sizeof(GPIO_BASE_PATH) + sizeof(gpio_params) - 1) 54 if (::pread(
_value_fd, &char_value, 1, 0) < 0) {
55 hal.
console->
printf(
"DigitalSource_Sysfs: Unable to read pin %u value.\n",
59 return char_value -
'0';
64 if (::pwrite(
_value_fd, value ==
HIGH ?
"1" :
"0", 1, 0) < 0) {
65 hal.
console->
printf(
"DigitalSource_Sysfs: Unable to write pin %u value.\n",
73 gpio_sysfs->_pinMode(
_pin, output);
90 _pinMode(pin_table[vpin], output);
95 const char *dir = output ?
"out" :
"in";
102 hal.
console->
printf(
"GPIO_Sysfs: Unable to set pin %u mode.\n", pin);
113 || (fd =
open(path, flags | O_CLOEXEC)) < 0) {
114 hal.
console->
printf(
"GPIO_Sysfs: Unable to get value file descriptor for pin %u.\n",
126 const unsigned pin = pin_table[vpin];
127 int fd = _open_pin_value(pin, O_RDONLY);
134 if (::pread(fd, &char_value, 1, 0) < 0) {
140 return char_value -
'0';
143 hal.
console->
printf(
"GPIO_Sysfs: Unable to read pin %u value.\n", vpin);
151 const unsigned pin = pin_table[vpin];
152 int fd = _open_pin_value(pin, O_WRONLY);
158 if (::pwrite(fd, value ==
HIGH ?
"1" :
"0", 1, 0) < 0) {
166 hal.
console->
printf(
"GPIO_Sysfs: Unable to write pin %u value.\n", vpin);
183 const unsigned pin = pin_table[vpin];
186 if (_export_pin(vpin)) {
187 value_fd = _open_pin_value(pin, O_RDWR);
211 const unsigned int pin = pin_table[vpin];
220 if (access(gpio_path, F_OK) == 0) {
231 if (dprintf(fd,
"%u", pin) < 0) {
242 hal.
console->
printf(
"GPIO_Sysfs: Unable to export pin %u.\n", pin);
AP_HAL::DigitalSource * channel(uint16_t vpin) override
void toggle(uint8_t vpin) override
static bool _export_pin(uint8_t vpin)
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
AP_HAL::UARTDriver * console
#define assert_vpin(v_, max_,...)
bool usb_connected() override
static GPIO_Sysfs * from(AP_HAL::GPIO *gpio)
DigitalSource_Sysfs(unsigned pin, int value_fd)
void write(uint8_t value)
static Util * from(AP_HAL::Util *util)
virtual void printf(const char *,...) FMT_PRINTF(2
static const AP_HAL::HAL & hal
int close(int fileno)
POSIX Close a file with fileno handel.
void pinMode(uint8_t vpin, uint8_t output) override
uint8_t read(uint8_t vpin) override
void mode(uint8_t output)
int write_file(const char *path, const char *fmt,...) FMT_PRINTF(3
bool attach_interrupt(uint8_t interrupt_num, AP_HAL::Proc p, uint8_t mode) override
int snprintf(char *str, size_t size, const char *fmt,...)
#define error(fmt, args ...)
int _open_pin_value(unsigned int pin, int flags)
void write(uint8_t vpin, uint8_t value) override
int8_t analogPinToDigitalPin(uint8_t vpin) override
void _pinMode(unsigned int pin, uint8_t output)