123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include <AP_HAL/AP_HAL.h>
- #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
- #include "AP_RangeFinder_BBB_PRU.h"
- #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;
- AP_RangeFinder_BBB_PRU::AP_RangeFinder_BBB_PRU(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
- AP_RangeFinder_Backend(_state, _params)
- {
- }
- bool AP_RangeFinder_BBB_PRU::detect()
- {
- bool result = true;
- uint32_t mem_fd;
- uint32_t *ctrl;
- void *ram;
- mem_fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC);
- 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);
-
- *ctrl = 0;
- hal.scheduler->delay(1);
-
- FILE *file = fopen("/lib/firmware/rangefinderprutext.bin", "rb");
- if (file == nullptr) {
- result = false;
- }
- if (fread(ram, PRU0_IRAM_SIZE, 1, file) != 1) {
- result = false;
- }
- fclose(file);
- munmap(ram, PRU0_IRAM_SIZE);
- ram = mmap(0, PRU0_DRAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_DRAM_BASE);
-
- file = fopen("/lib/firmware/rangefinderprudata.bin", "rb");
- if (file == nullptr) {
- result = false;
- }
- if (fread(ram, PRU0_DRAM_SIZE, 1, file) != 1) {
- result = false;
- }
- fclose(file);
- munmap(ram, PRU0_DRAM_SIZE);
-
- ram = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_DRAM_BASE);
- close(mem_fd);
-
- *ctrl = 2;
- rangerpru = (volatile struct range*)ram;
- return result;
- }
- void AP_RangeFinder_BBB_PRU::update(void)
- {
- state.status = (RangeFinder::RangeFinder_Status)rangerpru->status;
- state.distance_cm = rangerpru->distance;
- state.last_reading_ms = AP_HAL::millis();
- }
- #endif
|