ardupilot/libraries/AP_HAL_PX4/GPIO.cpp

308 lines
8.1 KiB
C++
Raw Normal View History

#include <AP_HAL/AP_HAL.h>
2013-07-11 00:01:59 -03:00
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
#include "GPIO.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
/* PX4 headers */
#include <drivers/drv_led.h>
#include <drivers/drv_tone_alarm.h>
#include <drivers/drv_gpio.h>
#include <modules/px4iofirmware/protocol.h>
#include <arch/board/board.h>
#include <board_config.h>
2013-07-11 00:01:59 -03:00
2013-08-30 02:05:29 -03:00
#define LOW 0
#define HIGH 1
2013-07-11 00:01:59 -03:00
extern const AP_HAL::HAL& hal;
using namespace PX4;
PX4GPIO::PX4GPIO()
{}
void PX4GPIO::init()
{
#ifdef CONFIG_ARCH_BOARD_PX4FMU_V1
_led_fd = open(LED0_DEVICE_PATH, O_RDWR);
2013-07-11 00:01:59 -03:00
if (_led_fd == -1) {
AP_HAL::panic("Unable to open " LED0_DEVICE_PATH);
2013-07-11 00:01:59 -03:00
}
if (ioctl(_led_fd, LED_OFF, LED_BLUE) != 0) {
hal.console->printf("GPIO: Unable to setup GPIO LED BLUE\n");
}
if (ioctl(_led_fd, LED_OFF, LED_RED) != 0) {
hal.console->printf("GPIO: Unable to setup GPIO LED RED\n");
}
#endif
_tone_alarm_fd = open(TONEALARM0_DEVICE_PATH, O_WRONLY);
2013-07-11 00:01:59 -03:00
if (_tone_alarm_fd == -1) {
AP_HAL::panic("Unable to open " TONEALARM0_DEVICE_PATH);
2013-07-11 00:01:59 -03:00
}
_gpio_fmu_fd = open(PX4FMU_DEVICE_PATH, 0);
if (_gpio_fmu_fd == -1) {
AP_HAL::panic("Unable to open GPIO");
2013-07-11 00:01:59 -03:00
}
#ifdef CONFIG_ARCH_BOARD_PX4FMU_V1
if (ioctl(_gpio_fmu_fd, GPIO_CLEAR, GPIO_EXT_1) != 0) {
2013-07-11 00:01:59 -03:00
hal.console->printf("GPIO: Unable to setup GPIO_1\n");
}
#endif
2013-07-11 00:01:59 -03:00
2015-11-24 22:06:43 -04:00
#ifdef PX4IO_DEVICE_PATH
// also try to setup for the relay pins on the IO board
_gpio_io_fd = open(PX4IO_DEVICE_PATH, O_RDWR);
if (_gpio_io_fd == -1) {
hal.console->printf("GPIO: Unable to open px4io\n");
}
2015-11-24 22:06:43 -04:00
#endif
2013-07-11 00:01:59 -03:00
}
void PX4GPIO::pinMode(uint8_t pin, uint8_t output)
{
switch (pin) {
case PX4_GPIO_FMU_SERVO_PIN(0) ... PX4_GPIO_FMU_SERVO_PIN(5):
uint32_t pinmask = 1U<<(pin-PX4_GPIO_FMU_SERVO_PIN(0));
if (output) {
uint8_t old_value = read(pin);
if (old_value) {
ioctl(_gpio_fmu_fd, GPIO_SET_OUTPUT_HIGH, pinmask);
} else {
ioctl(_gpio_fmu_fd, GPIO_SET_OUTPUT_LOW, pinmask);
}
} else {
ioctl(_gpio_fmu_fd, GPIO_SET_INPUT, pinmask);
}
break;
}
}
2013-07-11 00:01:59 -03:00
int8_t PX4GPIO::analogPinToDigitalPin(uint8_t pin)
{
switch (pin) {
case PX4_GPIO_FMU_SERVO_PIN(0) ... PX4_GPIO_FMU_SERVO_PIN(5):
// the only pins that can be mapped are the FMU servo rail pins */
return pin;
}
2013-07-11 00:01:59 -03:00
return -1;
}
uint8_t PX4GPIO::read(uint8_t pin) {
switch (pin) {
#ifdef GPIO_EXT_1
case PX4_GPIO_EXT_FMU_RELAY1_PIN: {
uint32_t relays = 0;
ioctl(_gpio_fmu_fd, GPIO_GET, (unsigned long)&relays);
return (relays & GPIO_EXT_1)?HIGH:LOW;
}
#endif
#ifdef GPIO_EXT_2
case PX4_GPIO_EXT_FMU_RELAY2_PIN: {
uint32_t relays = 0;
ioctl(_gpio_fmu_fd, GPIO_GET, (unsigned long)&relays);
return (relays & GPIO_EXT_2)?HIGH:LOW;
}
#endif
#ifdef PX4IO_P_SETUP_RELAYS_POWER1
case PX4_GPIO_EXT_IO_RELAY1_PIN: {
uint32_t relays = 0;
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd == -1) {
return LOW;
}
ioctl(_gpio_io_fd, GPIO_GET, (unsigned long)&relays);
return (relays & PX4IO_P_SETUP_RELAYS_POWER1)?HIGH:LOW;
}
#endif
#ifdef PX4IO_P_SETUP_RELAYS_POWER2
case PX4_GPIO_EXT_IO_RELAY2_PIN: {
uint32_t relays = 0;
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd == -1) {
return LOW;
}
ioctl(_gpio_io_fd, GPIO_GET, (unsigned long)&relays);
return (relays & PX4IO_P_SETUP_RELAYS_POWER2)?HIGH:LOW;
}
#endif
#ifdef PX4IO_P_SETUP_RELAYS_ACC1
case PX4_GPIO_EXT_IO_ACC1_PIN: {
uint32_t relays = 0;
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd == -1) {
return LOW;
}
ioctl(_gpio_io_fd, GPIO_GET, (unsigned long)&relays);
return (relays & PX4IO_P_SETUP_RELAYS_ACC1)?HIGH:LOW;
}
#endif
#ifdef PX4IO_P_SETUP_RELAYS_ACC2
case PX4_GPIO_EXT_IO_ACC2_PIN: {
uint32_t relays = 0;
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd == -1) {
return LOW;
}
ioctl(_gpio_io_fd, GPIO_GET, (unsigned long)&relays);
return (relays & PX4IO_P_SETUP_RELAYS_ACC2)?HIGH:LOW;
}
#endif
case PX4_GPIO_FMU_SERVO_PIN(0) ... PX4_GPIO_FMU_SERVO_PIN(5): {
uint32_t relays = 0;
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd == -1) {
return LOW;
}
ioctl(_gpio_fmu_fd, GPIO_GET, (unsigned long)&relays);
return (relays & (1U<<(pin-PX4_GPIO_FMU_SERVO_PIN(0))))?HIGH:LOW;
}
}
return LOW;
2013-07-11 00:01:59 -03:00
}
void PX4GPIO::write(uint8_t pin, uint8_t value)
{
switch (pin) {
#ifdef CONFIG_ARCH_BOARD_PX4FMU_V1
2013-08-08 10:14:06 -03:00
case HAL_GPIO_A_LED_PIN: // Arming LED
2013-07-11 00:01:59 -03:00
if (value == LOW) {
ioctl(_led_fd, LED_OFF, LED_RED);
} else {
ioctl(_led_fd, LED_ON, LED_RED);
}
break;
2013-08-08 10:14:06 -03:00
case HAL_GPIO_B_LED_PIN: // not used yet
2013-07-11 00:01:59 -03:00
break;
2013-08-08 10:14:06 -03:00
case HAL_GPIO_C_LED_PIN: // GPS LED
2013-07-11 00:01:59 -03:00
if (value == LOW) {
ioctl(_led_fd, LED_OFF, LED_BLUE);
} else {
ioctl(_led_fd, LED_ON, LED_BLUE);
}
break;
#endif
2013-07-11 00:01:59 -03:00
case PX4_GPIO_PIEZO_PIN: // Piezo beeper
2013-07-11 00:01:59 -03:00
if (value == LOW) { // this is inverted
ioctl(_tone_alarm_fd, TONE_SET_ALARM, 3); // Alarm on !!
//::write(_tone_alarm_fd, &user_tune, sizeof(user_tune));
} else {
ioctl(_tone_alarm_fd, TONE_SET_ALARM, 0); // Alarm off !!
}
break;
#ifdef GPIO_EXT_1
case PX4_GPIO_EXT_FMU_RELAY1_PIN:
ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, GPIO_EXT_1);
break;
#endif
#ifdef GPIO_EXT_2
case PX4_GPIO_EXT_FMU_RELAY2_PIN:
ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, GPIO_EXT_2);
break;
#endif
#ifdef PX4IO_P_SETUP_RELAYS_POWER1
case PX4_GPIO_EXT_IO_RELAY1_PIN:
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd != -1) {
ioctl(_gpio_io_fd, value==LOW?GPIO_CLEAR:GPIO_SET, PX4IO_P_SETUP_RELAYS_POWER1);
}
break;
#endif
#ifdef PX4IO_P_SETUP_RELAYS_POWER2
case PX4_GPIO_EXT_IO_RELAY2_PIN:
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd != -1) {
ioctl(_gpio_io_fd, value==LOW?GPIO_CLEAR:GPIO_SET, PX4IO_P_SETUP_RELAYS_POWER2);
}
break;
#endif
#ifdef PX4IO_P_SETUP_RELAYS_ACC1
case PX4_GPIO_EXT_IO_ACC1_PIN:
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd != -1) {
ioctl(_gpio_io_fd, value==LOW?GPIO_CLEAR:GPIO_SET, PX4IO_P_SETUP_RELAYS_ACC1);
}
2013-07-11 00:01:59 -03:00
break;
#endif
#ifdef PX4IO_P_SETUP_RELAYS_ACC2
case PX4_GPIO_EXT_IO_ACC2_PIN:
2015-11-24 22:06:43 -04:00
if (_gpio_io_fd != -1) {
ioctl(_gpio_io_fd, value==LOW?GPIO_CLEAR:GPIO_SET, PX4IO_P_SETUP_RELAYS_ACC2);
}
break;
#endif
case PX4_GPIO_FMU_SERVO_PIN(0) ... PX4_GPIO_FMU_SERVO_PIN(5):
ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, 1U<<(pin-PX4_GPIO_FMU_SERVO_PIN(0)));
break;
2013-07-11 00:01:59 -03:00
}
}
2013-08-08 10:14:06 -03:00
void PX4GPIO::toggle(uint8_t pin)
{
write(pin, !read(pin));
2013-08-08 10:14:06 -03:00
}
2013-07-11 00:01:59 -03:00
/* Alternative interface: */
AP_HAL::DigitalSource* PX4GPIO::channel(uint16_t n) {
return new PX4DigitalSource(0);
}
/* Interrupt interface: */
bool PX4GPIO::attach_interrupt(uint8_t interrupt_num, AP_HAL::Proc p, uint8_t mode)
{
return true;
}
/*
return true when USB connected
*/
bool PX4GPIO::usb_connected(void)
{
/*
we use a combination of voltage on the USB connector and the
open of the /dev/ttyACM0 character device. This copes with
systems where the VBUS may go high even with no USB connected
(such as AUAV-X2)
*/
return stm32_gpioread(GPIO_OTGFS_VBUS) && _usb_connected;
}
2013-07-11 00:01:59 -03:00
PX4DigitalSource::PX4DigitalSource(uint8_t v) :
_v(v)
{}
void PX4DigitalSource::mode(uint8_t output)
{}
uint8_t PX4DigitalSource::read() {
return _v;
}
void PX4DigitalSource::write(uint8_t value) {
_v = value;
}
2013-08-08 10:14:06 -03:00
void PX4DigitalSource::toggle() {
_v = !_v;
}
2013-07-11 00:01:59 -03:00
#endif // CONFIG_HAL_BOARD