2015-07-06 16:24:06 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
HC-SR04 Ultrasonic Distance Sensor connected to BeagleBone Black
|
|
|
|
by Mirko Denecke <mirkix@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_RangeFinder_BBB_PRU.h"
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#if AP_RANGEFINDER_BBB_PRU_ENABLED
|
|
|
|
|
2015-07-06 16:24:06 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
volatile struct range *rangerpru;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Stop PRU, load firmware (check if firmware is present), start PRU.
|
|
|
|
If we get a result the sensor seems to be there.
|
|
|
|
*/
|
2017-08-07 00:41:01 -03:00
|
|
|
bool AP_RangeFinder_BBB_PRU::detect()
|
2015-07-06 16:24:06 -03:00
|
|
|
{
|
2021-10-28 13:21:38 -03:00
|
|
|
//The constructor is called when the detect() method returns true, more on this in the header file
|
2015-07-06 16:24:06 -03:00
|
|
|
bool result = true;
|
|
|
|
uint32_t mem_fd;
|
|
|
|
uint32_t *ctrl;
|
|
|
|
void *ram;
|
|
|
|
|
2016-10-30 10:42:48 -03:00
|
|
|
mem_fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC);
|
2015-07-06 16:24:06 -03:00
|
|
|
ctrl = (uint32_t*)mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_CTRL_BASE);
|
|
|
|
ram = mmap(0, PRU0_IRAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_IRAM_BASE);
|
|
|
|
|
|
|
|
// Reset PRU 0
|
|
|
|
*ctrl = 0;
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2016-01-18 15:54:43 -04:00
|
|
|
// Load firmware (.text)
|
2015-07-06 16:24:06 -03:00
|
|
|
FILE *file = fopen("/lib/firmware/rangefinderprutext.bin", "rb");
|
2019-08-22 16:04:17 -03:00
|
|
|
if (file == nullptr) {
|
2015-07-06 16:24:06 -03:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:04:17 -03:00
|
|
|
if (fread(ram, PRU0_IRAM_SIZE, 1, file) != 1) {
|
2015-07-06 16:24:06 -03:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
munmap(ram, PRU0_IRAM_SIZE);
|
|
|
|
|
2016-01-18 15:54:43 -04:00
|
|
|
ram = mmap(0, PRU0_DRAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_DRAM_BASE);
|
|
|
|
|
|
|
|
// Load firmware (.data)
|
|
|
|
file = fopen("/lib/firmware/rangefinderprudata.bin", "rb");
|
2019-08-22 16:04:17 -03:00
|
|
|
if (file == nullptr) {
|
2016-01-18 15:54:43 -04:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
2019-08-22 16:04:17 -03:00
|
|
|
if (fread(ram, PRU0_DRAM_SIZE, 1, file) != 1) {
|
2016-01-18 15:54:43 -04:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
munmap(ram, PRU0_DRAM_SIZE);
|
|
|
|
|
2015-07-06 16:24:06 -03:00
|
|
|
// Map PRU RAM
|
|
|
|
ram = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_DRAM_BASE);
|
|
|
|
close(mem_fd);
|
|
|
|
|
|
|
|
// Start PRU 0
|
|
|
|
*ctrl = 2;
|
|
|
|
|
|
|
|
rangerpru = (volatile struct range*)ram;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
update the state of the sensor
|
|
|
|
*/
|
|
|
|
void AP_RangeFinder_BBB_PRU::update(void)
|
|
|
|
{
|
2019-11-01 02:10:52 -03:00
|
|
|
state.status = (RangeFinder::Status)rangerpru->status;
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = rangerpru->distance * 0.01f;
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
2015-07-06 16:24:06 -03:00
|
|
|
}
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif // AP_RANGEFINDER_BBB_PRU_ENABLED
|