AP_RangeFinder_BBB_PRU.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. HC-SR04 Ultrasonic Distance Sensor connected to BeagleBone Black
  13. by Mirko Denecke <mirkix@gmail.com>
  14. */
  15. #include <AP_HAL/AP_HAL.h>
  16. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
  17. #include "AP_RangeFinder_BBB_PRU.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/mman.h>
  25. #include <sys/types.h>
  26. extern const AP_HAL::HAL& hal;
  27. volatile struct range *rangerpru;
  28. /*
  29. The constructor also initialises the rangefinder. Note that this
  30. constructor is not called until detect() returns true, so we
  31. already know that we should setup the rangefinder
  32. */
  33. AP_RangeFinder_BBB_PRU::AP_RangeFinder_BBB_PRU(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
  34. AP_RangeFinder_Backend(_state, _params)
  35. {
  36. }
  37. /*
  38. Stop PRU, load firmware (check if firmware is present), start PRU.
  39. If we get a result the sensor seems to be there.
  40. */
  41. bool AP_RangeFinder_BBB_PRU::detect()
  42. {
  43. bool result = true;
  44. uint32_t mem_fd;
  45. uint32_t *ctrl;
  46. void *ram;
  47. mem_fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC);
  48. ctrl = (uint32_t*)mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_CTRL_BASE);
  49. ram = mmap(0, PRU0_IRAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_IRAM_BASE);
  50. // Reset PRU 0
  51. *ctrl = 0;
  52. hal.scheduler->delay(1);
  53. // Load firmware (.text)
  54. FILE *file = fopen("/lib/firmware/rangefinderprutext.bin", "rb");
  55. if (file == nullptr) {
  56. result = false;
  57. }
  58. if (fread(ram, PRU0_IRAM_SIZE, 1, file) != 1) {
  59. result = false;
  60. }
  61. fclose(file);
  62. munmap(ram, PRU0_IRAM_SIZE);
  63. ram = mmap(0, PRU0_DRAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_DRAM_BASE);
  64. // Load firmware (.data)
  65. file = fopen("/lib/firmware/rangefinderprudata.bin", "rb");
  66. if (file == nullptr) {
  67. result = false;
  68. }
  69. if (fread(ram, PRU0_DRAM_SIZE, 1, file) != 1) {
  70. result = false;
  71. }
  72. fclose(file);
  73. munmap(ram, PRU0_DRAM_SIZE);
  74. // Map PRU RAM
  75. ram = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, PRU0_DRAM_BASE);
  76. close(mem_fd);
  77. // Start PRU 0
  78. *ctrl = 2;
  79. rangerpru = (volatile struct range*)ram;
  80. return result;
  81. }
  82. /*
  83. update the state of the sensor
  84. */
  85. void AP_RangeFinder_BBB_PRU::update(void)
  86. {
  87. state.status = (RangeFinder::RangeFinder_Status)rangerpru->status;
  88. state.distance_cm = rangerpru->distance;
  89. state.last_reading_ms = AP_HAL::millis();
  90. }
  91. #endif // CONFIG_HAL_BOARD_SUBTYPE