2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-09-18 10:35:22 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || \
|
2015-09-06 18:35:13 -03:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD || \
|
2016-12-29 10:47:55 -04:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI || \
|
2017-12-13 15:35:47 -04:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
|
2014-09-18 10:35:22 -03:00
|
|
|
|
|
|
|
#include "GPIO.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2021-09-17 22:14:53 -03:00
|
|
|
#define LOW 0
|
|
|
|
#define HIGH 1
|
|
|
|
|
2014-09-18 10:35:22 -03:00
|
|
|
using namespace Linux;
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
GPIO_BBB::GPIO_BBB()
|
2014-09-18 10:35:22 -03:00
|
|
|
{}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_BBB::init()
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
#if LINUX_GPIO_NUM_BANKS == 4
|
|
|
|
int mem_fd;
|
|
|
|
|
|
|
|
/* open /dev/mem */
|
2016-10-30 10:22:29 -03:00
|
|
|
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC|O_CLOEXEC)) < 0) {
|
2014-09-18 10:35:22 -03:00
|
|
|
printf("can't open /dev/mem \n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
2020-03-01 11:37:42 -04:00
|
|
|
/*
|
|
|
|
Enable all GPIO clocks
|
|
|
|
Without this, access to deactivated banks (i.e. those with no clock source set up) will (logically) fail with SIGBUS
|
|
|
|
*/
|
|
|
|
volatile unsigned *cm_per = (volatile unsigned *)mmap(0, CM_PER_BASE_SIZE, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_SHARED, mem_fd, CM_PER_BASE);
|
|
|
|
if ((char *)cm_per == MAP_FAILED) {
|
|
|
|
AP_HAL::panic("unable to map CM_PER registers");
|
|
|
|
}
|
|
|
|
off_t cm_offsets[LINUX_GPIO_NUM_BANKS-1] = { CM_PER_GPIO1_CLKCTRL, CM_PER_GPIO2_CLKCTRL, CM_PER_GPIO3_CLKCTRL };
|
|
|
|
for (uint8_t i=0; i<LINUX_GPIO_NUM_BANKS-1; i++) {
|
|
|
|
unsigned reg_value = *(cm_per + cm_offsets[i]);
|
|
|
|
*(cm_per + cm_offsets[i]) = (reg_value & ~0b11) | 0b10;
|
|
|
|
}
|
|
|
|
munmap((void *)cm_per, CM_PER_BASE_SIZE);
|
|
|
|
|
2014-09-18 10:35:22 -03:00
|
|
|
/* mmap GPIO */
|
|
|
|
off_t offsets[LINUX_GPIO_NUM_BANKS] = { GPIO0_BASE, GPIO1_BASE, GPIO2_BASE, GPIO3_BASE };
|
|
|
|
for (uint8_t i=0; i<LINUX_GPIO_NUM_BANKS; i++) {
|
|
|
|
gpio_bank[i].base = (volatile unsigned *)mmap(0, GPIO_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, offsets[i]);
|
|
|
|
if ((char *)gpio_bank[i].base == MAP_FAILED) {
|
2015-11-19 23:10:58 -04:00
|
|
|
AP_HAL::panic("unable to map GPIO bank");
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
gpio_bank[i].oe = gpio_bank[i].base + GPIO_OE;
|
|
|
|
gpio_bank[i].in = gpio_bank[i].base + GPIO_IN;
|
|
|
|
gpio_bank[i].out = gpio_bank[i].base + GPIO_OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(mem_fd);
|
|
|
|
#endif // LINUX_GPIO_NUM_BANKS
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_BBB::pinMode(uint8_t pin, uint8_t output)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
uint8_t bank = pin/32;
|
|
|
|
uint8_t bankpin = pin & 0x1F;
|
|
|
|
if (bank >= LINUX_GPIO_NUM_BANKS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (output == HAL_GPIO_INPUT) {
|
|
|
|
*gpio_bank[bank].oe |= (1U<<bankpin);
|
|
|
|
} else {
|
|
|
|
*gpio_bank[bank].oe &= ~(1U<<bankpin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint8_t GPIO_BBB::read(uint8_t pin) {
|
2014-09-18 10:35:22 -03:00
|
|
|
|
|
|
|
uint8_t bank = pin/32;
|
|
|
|
uint8_t bankpin = pin & 0x1F;
|
|
|
|
if (bank >= LINUX_GPIO_NUM_BANKS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return *gpio_bank[bank].in & (1U<<bankpin) ? HIGH : LOW;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_BBB::write(uint8_t pin, uint8_t value)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
uint8_t bank = pin/32;
|
|
|
|
uint8_t bankpin = pin & 0x1F;
|
|
|
|
if (bank >= LINUX_GPIO_NUM_BANKS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value == LOW) {
|
|
|
|
*gpio_bank[bank].out &= ~(1U<<bankpin);
|
|
|
|
} else {
|
|
|
|
*gpio_bank[bank].out |= 1U<<bankpin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_BBB::toggle(uint8_t pin)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
write(pin, !read(pin));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alternative interface: */
|
2015-10-20 18:13:25 -03:00
|
|
|
AP_HAL::DigitalSource* GPIO_BBB::channel(uint16_t n) {
|
2024-05-26 22:24:12 -03:00
|
|
|
return NEW_NOTHROW DigitalSource(n);
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool GPIO_BBB::usb_connected(void)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF ||
|
2015-09-06 18:35:13 -03:00
|
|
|
// CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD ||
|
2016-12-29 10:47:55 -04:00
|
|
|
// CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI ||
|
2017-12-13 15:35:47 -04:00
|
|
|
// CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE ||
|
|
|
|
// CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
|