2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-09-18 10:35:22 -03:00
|
|
|
|
2017-11-02 10:45:18 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
|
2016-01-05 06:28:27 -04:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH || \
|
2016-10-17 15:02:48 -03:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DARK || \
|
2020-01-10 15:25:03 -04:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI || \
|
2021-09-07 02:17:32 -03:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIGATOR || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_OBAL_V1
|
2014-09-18 10:35:22 -03:00
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
#include <assert.h>
|
2014-09-18 10:35:22 -03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-09-18 10:35:22 -03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <unistd.h>
|
2014-09-18 10:35:22 -03:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include "GPIO.h"
|
|
|
|
#include "Util_RPI.h"
|
2015-03-28 10:46:08 -03:00
|
|
|
|
2018-07-10 22:28:49 -03:00
|
|
|
#define GPIO_RPI_MAX_NUMBER_PINS 32
|
2016-06-20 11:10:59 -03:00
|
|
|
|
2014-09-18 10:35:22 -03:00
|
|
|
using namespace Linux;
|
|
|
|
|
2020-03-01 19:50:34 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2016-06-20 11:10:59 -03:00
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
// Range based in the first memory address of the first register and the last memory addres
|
|
|
|
// for the GPIO section (0x7E20'00B4 - 0x7E20'0000).
|
|
|
|
const uint8_t GPIO_RPI::_gpio_registers_memory_range = 0xB4;
|
|
|
|
const char* GPIO_RPI::_system_memory_device_path = "/dev/mem";
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
GPIO_RPI::GPIO_RPI()
|
2016-06-20 11:10:59 -03:00
|
|
|
{
|
|
|
|
}
|
2014-09-18 10:35:22 -03:00
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
void GPIO_RPI::set_gpio_mode_alt(int pin, int alternative)
|
|
|
|
{
|
|
|
|
// Each register can contain 10 pins
|
|
|
|
const uint8_t pins_per_register = 10;
|
|
|
|
// Calculates the position of the 3 bit mask in the 32 bits register
|
|
|
|
const uint8_t tree_bits_position_in_register = (pin%pins_per_register)*3;
|
|
|
|
/** Creates a mask to enable the alternative function based in the following logic:
|
|
|
|
*
|
|
|
|
* | Alternative Function | 3 bits value |
|
|
|
|
* |:--------------------:|:------------:|
|
|
|
|
* | Function 0 | 0b100 |
|
|
|
|
* | Function 1 | 0b101 |
|
|
|
|
* | Function 2 | 0b110 |
|
|
|
|
* | Function 3 | 0b111 |
|
|
|
|
* | Function 4 | 0b011 |
|
|
|
|
* | Function 5 | 0b010 |
|
|
|
|
*/
|
|
|
|
const uint8_t alternative_value =
|
|
|
|
(alternative < 4 ? (alternative + 4) : (alternative == 4 ? 3 : 2));
|
|
|
|
// 0b00'000'000'000'000'000'000'ALT'000'000'000 enables alternative for the 4th pin
|
|
|
|
const uint32_t mask_with_alt = static_cast<uint32_t>(alternative_value) << tree_bits_position_in_register;
|
|
|
|
const uint32_t mask = 0b111 << tree_bits_position_in_register;
|
|
|
|
// Clear all bits in our position and apply our mask with alt values
|
|
|
|
uint32_t register_value = _gpio[pin / pins_per_register];
|
|
|
|
register_value &= ~mask;
|
|
|
|
_gpio[pin / pins_per_register] = register_value | mask_with_alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_RPI::set_gpio_mode_in(int pin)
|
|
|
|
{
|
|
|
|
// Each register can contain 10 pins
|
|
|
|
const uint8_t pins_per_register = 10;
|
|
|
|
// Calculates the position of the 3 bit mask in the 32 bits register
|
|
|
|
const uint8_t tree_bits_position_in_register = (pin%pins_per_register)*3;
|
|
|
|
// Create a mask that only removes the bits in this specific GPIO pin, E.g:
|
|
|
|
// 0b11'111'111'111'111'111'111'000'111'111'111 for the 4th pin
|
|
|
|
const uint32_t mask = ~(0b111<<tree_bits_position_in_register);
|
|
|
|
// Apply mask
|
|
|
|
_gpio[pin / pins_per_register] &= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_RPI::set_gpio_mode_out(int pin)
|
|
|
|
{
|
|
|
|
// Each register can contain 10 pins
|
|
|
|
const uint8_t pins_per_register = 10;
|
|
|
|
// Calculates the position of the 3 bit mask in the 32 bits register
|
|
|
|
const uint8_t tree_bits_position_in_register = (pin%pins_per_register)*3;
|
|
|
|
// Create a mask to enable the bit that sets output functionality
|
|
|
|
// 0b00'000'000'000'000'000'000'001'000'000'000 enables output for the 4th pin
|
|
|
|
const uint32_t mask_with_bit = 0b001 << tree_bits_position_in_register;
|
|
|
|
const uint32_t mask = 0b111 << tree_bits_position_in_register;
|
|
|
|
// Clear all bits in our position and apply our mask with alt values
|
|
|
|
uint32_t register_value = _gpio[pin / pins_per_register];
|
|
|
|
register_value &= ~mask;
|
|
|
|
_gpio[pin / pins_per_register] = register_value | mask_with_bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_RPI::set_gpio_high(int pin)
|
|
|
|
{
|
|
|
|
// Calculate index of the array for the register GPSET0 (0x7E20'001C)
|
|
|
|
constexpr uint32_t gpset0_memory_offset_value = 0x1c;
|
|
|
|
constexpr uint32_t gpset0_index_value = gpset0_memory_offset_value / sizeof(*_gpio);
|
|
|
|
_gpio[gpset0_index_value] = 1 << pin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_RPI::set_gpio_low(int pin)
|
|
|
|
{
|
|
|
|
// Calculate index of the array for the register GPCLR0 (0x7E20'0028)
|
|
|
|
constexpr uint32_t gpclr0_memory_offset_value = 0x28;
|
|
|
|
constexpr uint32_t gpclr0_index_value = gpclr0_memory_offset_value / sizeof(*_gpio);
|
|
|
|
_gpio[gpclr0_index_value] = 1 << pin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GPIO_RPI::get_gpio_logic_state(int pin)
|
|
|
|
{
|
|
|
|
// Calculate index of the array for the register GPLEV0 (0x7E20'0034)
|
|
|
|
constexpr uint32_t gplev0_memory_offset_value = 0x34;
|
|
|
|
constexpr uint32_t gplev0_index_value = gplev0_memory_offset_value / sizeof(*_gpio);
|
|
|
|
return _gpio[gplev0_index_value] & (1 << pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GPIO_RPI::get_address(GPIO_RPI::Address address, GPIO_RPI::PeripheralOffset offset) const
|
|
|
|
{
|
|
|
|
return static_cast<uint32_t>(address) + static_cast<uint32_t>(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile uint32_t* GPIO_RPI::get_memory_pointer(uint32_t address, uint32_t range) const
|
|
|
|
{
|
|
|
|
auto pointer = mmap(
|
|
|
|
nullptr, // Any adddress in our space will do
|
|
|
|
range, // Map length
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC, // Enable reading & writing to mapped memory
|
|
|
|
MAP_SHARED|MAP_LOCKED, // Shared with other processes
|
|
|
|
_system_memory_device, // File to map
|
|
|
|
address // Offset to GPIO peripheral
|
|
|
|
);
|
|
|
|
|
|
|
|
if (pointer == MAP_FAILED) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<volatile uint32_t*>(pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GPIO_RPI::openMemoryDevice()
|
|
|
|
{
|
|
|
|
_system_memory_device = open(_system_memory_device_path, O_RDWR|O_SYNC|O_CLOEXEC);
|
|
|
|
if (_system_memory_device < 0) {
|
|
|
|
AP_HAL::panic("Can't open %s", GPIO_RPI::_system_memory_device_path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPIO_RPI::closeMemoryDevice()
|
|
|
|
{
|
|
|
|
close(_system_memory_device);
|
|
|
|
// Invalidate device variable
|
|
|
|
_system_memory_device = -1;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_RPI::init()
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
2020-03-24 17:09:18 -03:00
|
|
|
const int rpi_version = UtilRPI::from(hal.util)->get_rpi_version();
|
|
|
|
|
|
|
|
GPIO_RPI::Address peripheral_base;
|
2020-01-10 09:53:38 -04:00
|
|
|
if(rpi_version == 1) {
|
2020-03-24 17:09:18 -03:00
|
|
|
peripheral_base = Address::BCM2708_PERIPHERAL_BASE;
|
2020-01-10 09:53:38 -04:00
|
|
|
} else if (rpi_version == 2) {
|
2020-03-24 17:09:18 -03:00
|
|
|
peripheral_base = Address::BCM2709_PERIPHERAL_BASE;
|
2020-01-10 09:53:38 -04:00
|
|
|
} else {
|
2020-03-24 17:09:18 -03:00
|
|
|
peripheral_base = Address::BCM2711_PERIPHERAL_BASE;
|
2020-01-10 09:53:38 -04:00
|
|
|
}
|
2016-06-20 11:10:59 -03:00
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
if (!openMemoryDevice()) {
|
|
|
|
AP_HAL::panic("Failed to initialize memory device.");
|
|
|
|
return;
|
2014-11-05 09:56:12 -04:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
const uint32_t gpio_address = get_address(peripheral_base, PeripheralOffset::GPIO);
|
2014-11-05 09:56:12 -04:00
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
_gpio = get_memory_pointer(gpio_address, _gpio_registers_memory_range);
|
|
|
|
if (!_gpio) {
|
|
|
|
AP_HAL::panic("Failed to get GPIO memory map.");
|
2014-11-05 09:56:12 -04:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:09:18 -03:00
|
|
|
// No need to keep mem_fd open after mmap
|
|
|
|
closeMemoryDevice();
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_RPI::pinMode(uint8_t pin, uint8_t output)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
if (output == HAL_GPIO_INPUT) {
|
2020-03-24 17:09:18 -03:00
|
|
|
set_gpio_mode_in(pin);
|
2014-09-18 10:35:22 -03:00
|
|
|
} else {
|
2020-03-24 17:09:18 -03:00
|
|
|
set_gpio_mode_in(pin);
|
|
|
|
set_gpio_mode_out(pin);
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_RPI::pinMode(uint8_t pin, uint8_t output, uint8_t alt)
|
2015-08-17 23:32:29 -03:00
|
|
|
{
|
|
|
|
if (output == HAL_GPIO_INPUT) {
|
2020-03-24 17:09:18 -03:00
|
|
|
set_gpio_mode_in(pin);
|
2015-08-17 23:32:29 -03:00
|
|
|
} else if (output == HAL_GPIO_ALT) {
|
2020-03-24 17:09:18 -03:00
|
|
|
set_gpio_mode_in(pin);
|
|
|
|
set_gpio_mode_alt(pin, alt);
|
2015-08-17 23:32:29 -03:00
|
|
|
} else {
|
2020-03-24 17:09:18 -03:00
|
|
|
set_gpio_mode_in(pin);
|
|
|
|
set_gpio_mode_out(pin);
|
2015-08-17 23:32:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint8_t GPIO_RPI::read(uint8_t pin)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
2018-07-10 22:28:49 -03:00
|
|
|
if (pin >= GPIO_RPI_MAX_NUMBER_PINS) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-24 17:09:18 -03:00
|
|
|
return static_cast<uint8_t>(get_gpio_logic_state(pin));
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_RPI::write(uint8_t pin, uint8_t value)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
2020-03-24 17:09:18 -03:00
|
|
|
if (value != 0) {
|
|
|
|
set_gpio_high(pin);
|
2014-09-18 10:35:22 -03:00
|
|
|
} else {
|
2020-03-24 17:09:18 -03:00
|
|
|
set_gpio_low(pin);
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void GPIO_RPI::toggle(uint8_t pin)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
write(pin, !read(pin));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alternative interface: */
|
2016-06-20 11:10:59 -03:00
|
|
|
AP_HAL::DigitalSource* GPIO_RPI::channel(uint16_t n)
|
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
return new DigitalSource(n);
|
2014-09-18 10:35:22 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool GPIO_RPI::usb_connected(void)
|
2014-09-18 10:35:22 -03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:33:45 -04:00
|
|
|
#endif
|