Util.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #pragma once
  18. #include <AP_HAL/AP_HAL.h>
  19. #include "AP_HAL_ChibiOS_Namespace.h"
  20. #include "AP_HAL_ChibiOS.h"
  21. #include <ch.h>
  22. class ChibiOS::Util : public AP_HAL::Util {
  23. public:
  24. static Util *from(AP_HAL::Util *util) {
  25. return static_cast<Util*>(util);
  26. }
  27. bool run_debug_shell(AP_HAL::BetterStream *stream) override { return false; }
  28. uint32_t available_memory() override;
  29. // Special Allocation Routines
  30. void *malloc_type(size_t size, AP_HAL::Util::Memory_Type mem_type) override;
  31. void free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) override;
  32. #ifdef ENABLE_HEAP
  33. // heap functions, note that a heap once alloc'd cannot be dealloc'd
  34. virtual void *allocate_heap_memory(size_t size) override;
  35. virtual void *heap_realloc(void *heap, void *ptr, size_t new_size) override;
  36. #endif // ENABLE_HEAP
  37. /*
  38. return state of safety switch, if applicable
  39. */
  40. enum safety_state safety_switch_state(void) override;
  41. // IMU temperature control
  42. void set_imu_temp(float current) override;
  43. void set_imu_target_temp(int8_t *target) override;
  44. // get system ID as a string
  45. bool get_system_id(char buf[40]) override;
  46. bool get_system_id_unformatted(uint8_t buf[], uint8_t &len) override;
  47. #ifdef HAL_PWM_ALARM
  48. bool toneAlarm_init() override;
  49. void toneAlarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms) override;
  50. #endif
  51. #ifdef USE_POSIX
  52. /*
  53. initialise (or re-initialise) filesystem storage
  54. */
  55. bool fs_init(void) override;
  56. #endif
  57. // return true if the reason for the reboot was a watchdog reset
  58. bool was_watchdog_reset() const override;
  59. private:
  60. #ifdef HAL_PWM_ALARM
  61. struct ToneAlarmPwmGroup {
  62. pwmchannel_t chan;
  63. PWMConfig pwm_cfg;
  64. PWMDriver* pwm_drv;
  65. };
  66. static ToneAlarmPwmGroup _toneAlarm_pwm_group;
  67. #endif
  68. #if HAL_HAVE_IMU_HEATER
  69. struct {
  70. int8_t *target;
  71. float integrator;
  72. uint16_t count;
  73. float sum;
  74. uint32_t last_update_ms;
  75. float output;
  76. } heater;
  77. #endif
  78. /*
  79. set HW RTC in UTC microseconds
  80. */
  81. void set_hw_rtc(uint64_t time_utc_usec) override;
  82. /*
  83. get system clock in UTC microseconds
  84. */
  85. uint64_t get_hw_rtc() const override;
  86. #if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT)
  87. bool flash_bootloader() override;
  88. #endif
  89. #ifdef ENABLE_HEAP
  90. static memory_heap_t scripting_heap;
  91. #endif // ENABLE_HEAP
  92. // stm32F4 and F7 have 20 total RTC backup registers. We use the first one for boot type
  93. // flags, so 19 available for persistent data
  94. static_assert(sizeof(persistent_data) <= 19*4, "watchdog persistent data too large");
  95. };