2016-02-17 21:25:25 -04:00
|
|
|
#pragma once
|
2012-12-18 20:09:24 -04:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
2021-10-27 05:06:41 -03:00
|
|
|
#include <AP_Common/AP_Common.h> // for FMT_PRINTF
|
2012-12-18 20:09:24 -04:00
|
|
|
#include "AP_HAL_Namespace.h"
|
|
|
|
|
2020-12-30 02:23:27 -04:00
|
|
|
class ExpandingString;
|
|
|
|
|
2012-12-18 20:09:24 -04:00
|
|
|
class AP_HAL::Util {
|
|
|
|
public:
|
2013-09-21 00:28:46 -03:00
|
|
|
int snprintf(char* str, size_t size,
|
2021-08-03 23:44:31 -03:00
|
|
|
const char *format, ...) FMT_PRINTF(4, 5);
|
2012-12-18 20:09:24 -04:00
|
|
|
|
2013-09-21 00:28:46 -03:00
|
|
|
int vsnprintf(char* str, size_t size,
|
|
|
|
const char *format, va_list ap);
|
2012-12-18 20:09:24 -04:00
|
|
|
|
2021-10-24 18:09:01 -03:00
|
|
|
virtual void set_soft_armed(const bool b);
|
2015-01-28 19:15:48 -04:00
|
|
|
bool get_soft_armed() const { return soft_armed; }
|
|
|
|
|
2019-06-17 23:57:39 -03:00
|
|
|
// return the time that the armed state last changed
|
|
|
|
uint32_t get_last_armed_change() const { return last_armed_change_ms; };
|
|
|
|
|
2019-04-11 06:50:56 -03:00
|
|
|
// return true if the reason for the reboot was a watchdog reset
|
|
|
|
virtual bool was_watchdog_reset() const { return false; }
|
|
|
|
|
2019-04-19 21:24:06 -03:00
|
|
|
// return true if safety was off and this was a watchdog reset
|
2019-05-09 04:48:55 -03:00
|
|
|
bool was_watchdog_safety_off() const {
|
|
|
|
return was_watchdog_reset() && persistent_data.safety_state == SAFETY_ARMED;
|
|
|
|
}
|
2019-04-19 22:24:56 -03:00
|
|
|
|
|
|
|
// return true if this is a watchdog reset boot and we were armed
|
2019-05-09 04:48:55 -03:00
|
|
|
bool was_watchdog_armed() const { return was_watchdog_reset() && persistent_data.armed; }
|
2019-04-21 00:08:29 -03:00
|
|
|
|
2017-04-19 01:16:05 -03:00
|
|
|
virtual const char* get_custom_log_directory() const { return nullptr; }
|
2016-10-30 02:24:21 -03:00
|
|
|
virtual const char* get_custom_terrain_directory() const { return nullptr; }
|
2018-03-28 03:19:37 -03:00
|
|
|
virtual const char *get_custom_storage_directory() const { return nullptr; }
|
2015-06-28 16:09:20 -03:00
|
|
|
|
2016-01-06 18:09:09 -04:00
|
|
|
// get path to custom defaults file for AP_Param
|
|
|
|
virtual const char* get_custom_defaults_file() const {
|
|
|
|
return HAL_PARAM_DEFAULTS_PATH;
|
|
|
|
}
|
2016-05-23 17:14:54 -03:00
|
|
|
|
2020-08-23 03:21:47 -03:00
|
|
|
// set command line parameters to the eeprom on start
|
|
|
|
virtual void set_cmdline_parameters() {};
|
|
|
|
|
2013-02-07 00:03:34 -04:00
|
|
|
// run a debug shall on the given stream if possible. This is used
|
|
|
|
// to support dropping into a debug shell to run firmware upgrade
|
|
|
|
// commands
|
|
|
|
virtual bool run_debug_shell(AP_HAL::BetterStream *stream) = 0;
|
|
|
|
|
2020-04-28 03:31:57 -03:00
|
|
|
enum safety_state : uint8_t {
|
2013-10-05 02:46:35 -03:00
|
|
|
SAFETY_NONE, SAFETY_DISARMED, SAFETY_ARMED
|
|
|
|
};
|
|
|
|
|
2019-05-09 04:48:55 -03:00
|
|
|
/*
|
|
|
|
persistent data structure. This data is restored on boot if
|
|
|
|
there has been a watchdog reset. The data in this structure
|
|
|
|
should only be read if was_watchdog_reset() is true
|
|
|
|
Note that on STM32 this structure is limited to 76 bytes
|
|
|
|
*/
|
|
|
|
struct PersistentData {
|
|
|
|
float roll_rad, pitch_rad, yaw_rad; // attitude
|
|
|
|
int32_t home_lat, home_lon, home_alt_cm; // home position
|
2020-04-28 03:31:57 -03:00
|
|
|
uint32_t fault_addr;
|
|
|
|
uint32_t fault_icsr;
|
|
|
|
uint32_t fault_lr;
|
2019-05-09 08:14:40 -03:00
|
|
|
uint32_t internal_errors;
|
2020-04-29 21:40:46 -03:00
|
|
|
uint16_t internal_error_count;
|
|
|
|
uint16_t internal_error_last_line;
|
2020-04-28 03:31:57 -03:00
|
|
|
uint32_t spi_count;
|
|
|
|
uint32_t i2c_count;
|
|
|
|
uint32_t i2c_isr_count;
|
2019-05-09 04:48:55 -03:00
|
|
|
uint16_t waypoint_num;
|
2019-05-11 05:18:51 -03:00
|
|
|
uint16_t last_mavlink_msgid;
|
|
|
|
uint16_t last_mavlink_cmd;
|
|
|
|
uint16_t semaphore_line;
|
2019-07-16 01:45:34 -03:00
|
|
|
uint16_t fault_line;
|
|
|
|
uint8_t fault_type;
|
|
|
|
uint8_t fault_thd_prio;
|
2020-04-28 03:31:57 -03:00
|
|
|
char thread_name4[4];
|
|
|
|
int8_t scheduler_task;
|
|
|
|
bool armed; // true if vehicle was armed
|
|
|
|
enum safety_state safety_state;
|
2019-05-09 04:48:55 -03:00
|
|
|
};
|
|
|
|
struct PersistentData persistent_data;
|
2020-03-25 08:07:50 -03:00
|
|
|
// last_persistent_data is only filled in if we've suffered a watchdog reset
|
|
|
|
struct PersistentData last_persistent_data;
|
2019-05-09 04:48:55 -03:00
|
|
|
|
2013-10-05 02:46:35 -03:00
|
|
|
/*
|
|
|
|
return state of safety switch, if applicable
|
|
|
|
*/
|
|
|
|
virtual enum safety_state safety_switch_state(void) { return SAFETY_NONE; }
|
2013-10-23 09:26:36 -03:00
|
|
|
|
|
|
|
/*
|
2018-02-13 21:39:37 -04:00
|
|
|
set HW RTC in UTC microseconds
|
2013-10-23 09:26:36 -03:00
|
|
|
*/
|
2021-11-22 22:40:38 -04:00
|
|
|
virtual void set_hw_rtc(uint64_t time_utc_usec) = 0;
|
2013-11-25 20:57:40 -04:00
|
|
|
|
2016-04-12 01:39:29 -03:00
|
|
|
/*
|
2018-02-13 21:39:37 -04:00
|
|
|
get system clock in UTC microseconds
|
2016-04-12 01:39:29 -03:00
|
|
|
*/
|
2021-11-22 22:40:38 -04:00
|
|
|
virtual uint64_t get_hw_rtc() const = 0;
|
2016-04-12 01:39:29 -03:00
|
|
|
|
2019-10-24 23:14:09 -03:00
|
|
|
enum class FlashBootloader {
|
|
|
|
OK=0,
|
|
|
|
NO_CHANGE=1,
|
|
|
|
FAIL=2,
|
|
|
|
NOT_AVAILABLE=3,
|
|
|
|
};
|
|
|
|
|
2018-06-27 02:59:24 -03:00
|
|
|
// overwrite bootloader (probably with one from ROMFS)
|
2019-10-24 23:14:09 -03:00
|
|
|
virtual FlashBootloader flash_bootloader() { return FlashBootloader::NOT_AVAILABLE; }
|
2018-06-27 02:59:24 -03:00
|
|
|
|
2013-11-25 20:57:40 -04:00
|
|
|
/*
|
|
|
|
get system identifier (eg. serial number)
|
|
|
|
return false if a system identifier is not available
|
|
|
|
|
|
|
|
Buf should be filled with a printable string and must be null
|
|
|
|
terminated
|
|
|
|
*/
|
|
|
|
virtual bool get_system_id(char buf[40]) { return false; }
|
2018-09-28 12:31:25 -03:00
|
|
|
virtual bool get_system_id_unformatted(uint8_t buf[], uint8_t &len) { return false; }
|
2013-12-27 23:51:01 -04:00
|
|
|
|
2014-02-22 17:15:39 -04:00
|
|
|
/**
|
|
|
|
return commandline arguments, if available
|
|
|
|
*/
|
|
|
|
virtual void commandline_arguments(uint8_t &argc, char * const *&argv) { argc = 0; }
|
2016-05-23 17:14:54 -03:00
|
|
|
|
2021-04-03 19:01:40 -03:00
|
|
|
virtual bool toneAlarm_init(uint8_t types) { return false;}
|
2018-07-27 04:12:17 -03:00
|
|
|
virtual void toneAlarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms) {}
|
2015-01-28 19:15:48 -04:00
|
|
|
|
2015-06-17 04:04:15 -03:00
|
|
|
/*
|
|
|
|
return a stream for access to a system shell, if available
|
|
|
|
*/
|
2018-03-22 03:53:32 -03:00
|
|
|
virtual AP_HAL::BetterStream *get_shell_stream() { return nullptr; }
|
2015-06-17 04:04:15 -03:00
|
|
|
|
2015-10-02 06:29:27 -03:00
|
|
|
/* Support for an imu heating system */
|
2016-07-01 02:35:07 -03:00
|
|
|
virtual void set_imu_temp(float current) {}
|
2015-10-02 06:29:27 -03:00
|
|
|
|
2016-06-15 05:00:55 -03:00
|
|
|
/* Support for an imu heating system */
|
2016-07-01 02:35:07 -03:00
|
|
|
virtual void set_imu_target_temp(int8_t *target) {}
|
2016-06-15 05:00:55 -03:00
|
|
|
|
2016-11-23 05:30:32 -04:00
|
|
|
// allocate and free DMA-capable memory if possible. Otherwise return normal memory
|
2018-01-08 05:47:27 -04:00
|
|
|
enum Memory_Type {
|
|
|
|
MEM_DMA_SAFE,
|
|
|
|
MEM_FAST
|
|
|
|
};
|
2018-01-16 17:02:13 -04:00
|
|
|
virtual void *malloc_type(size_t size, Memory_Type mem_type) { return calloc(1, size); }
|
2018-01-08 05:47:27 -04:00
|
|
|
virtual void free_type(void *ptr, size_t size, Memory_Type mem_type) { return free(ptr); }
|
|
|
|
|
2018-12-08 22:34:13 -04:00
|
|
|
#ifdef ENABLE_HEAP
|
|
|
|
// heap functions, note that a heap once alloc'd cannot be dealloc'd
|
|
|
|
virtual void *allocate_heap_memory(size_t size) = 0;
|
|
|
|
virtual void *heap_realloc(void *heap, void *ptr, size_t new_size) = 0;
|
2020-03-17 05:47:38 -03:00
|
|
|
#if USE_LIBC_REALLOC
|
2020-03-16 04:21:16 -03:00
|
|
|
virtual void *std_realloc(void *ptr, size_t new_size) { return realloc(ptr, new_size); }
|
2020-03-17 05:47:38 -03:00
|
|
|
#else
|
|
|
|
virtual void *std_realloc(void *ptr, size_t new_size) = 0;
|
|
|
|
#endif // USE_LIBC_REALLOC
|
2018-12-08 22:34:13 -04:00
|
|
|
#endif // ENABLE_HEAP
|
|
|
|
|
2020-03-16 04:21:16 -03:00
|
|
|
|
2018-01-08 05:47:27 -04:00
|
|
|
/**
|
|
|
|
how much free memory do we have in bytes. If unknown return 4096
|
|
|
|
*/
|
|
|
|
virtual uint32_t available_memory(void) { return 4096; }
|
2018-09-28 12:31:25 -03:00
|
|
|
|
2019-04-01 05:38:35 -03:00
|
|
|
// attempt to trap the processor, presumably to enter an attached debugger
|
|
|
|
virtual bool trap() const { return false; }
|
|
|
|
|
2020-03-27 17:28:44 -03:00
|
|
|
// request information on running threads
|
2020-12-30 02:23:27 -04:00
|
|
|
virtual void thread_info(ExpandingString &str) {}
|
2020-03-27 17:28:44 -03:00
|
|
|
|
2020-11-21 11:59:41 -04:00
|
|
|
// request information on dma contention
|
2020-12-30 02:23:27 -04:00
|
|
|
virtual void dma_info(ExpandingString &str) {}
|
2020-11-21 11:59:41 -04:00
|
|
|
|
2021-01-28 13:38:17 -04:00
|
|
|
// request information on memory allocation
|
|
|
|
virtual void mem_info(ExpandingString &str) {}
|
|
|
|
|
2021-01-16 18:22:11 -04:00
|
|
|
// load persistent parameters from bootloader sector
|
|
|
|
virtual bool load_persistent_params(ExpandingString &str) const { return false; }
|
|
|
|
|
2022-01-08 06:56:52 -04:00
|
|
|
#if HAL_UART_STATS_ENABLED
|
2020-12-05 15:16:05 -04:00
|
|
|
// request information on uart I/O
|
|
|
|
virtual void uart_info(ExpandingString &str) {}
|
2022-01-08 06:56:52 -04:00
|
|
|
#endif
|
2022-02-10 10:41:44 -04:00
|
|
|
// request information on timer frequencies
|
|
|
|
virtual void timer_info(ExpandingString &str) {}
|
2020-12-05 15:16:05 -04:00
|
|
|
|
2021-06-24 13:47:40 -03:00
|
|
|
// generate Random values
|
|
|
|
virtual bool get_random_vals(uint8_t* data, size_t size) { return false; }
|
|
|
|
|
2021-10-06 02:36:39 -03:00
|
|
|
// generate Random values, will block until enough entropy is available
|
|
|
|
virtual bool get_true_random_vals(uint8_t* data, size_t size, uint32_t timeout_us) { return false; }
|
|
|
|
|
2021-09-06 01:46:19 -03:00
|
|
|
// log info on stack usage
|
|
|
|
virtual void log_stack_info(void) {}
|
2021-10-23 03:33:59 -03:00
|
|
|
|
2022-08-15 04:22:48 -03:00
|
|
|
#if AP_CRASHDUMP_ENABLED
|
2021-11-03 13:46:59 -03:00
|
|
|
virtual size_t last_crash_dump_size() const { return 0; }
|
|
|
|
virtual void* last_crash_dump_ptr() const { return nullptr; }
|
2022-08-15 04:22:48 -03:00
|
|
|
#endif
|
|
|
|
|
2015-01-28 19:15:48 -04:00
|
|
|
protected:
|
2015-08-22 05:47:45 -03:00
|
|
|
// we start soft_armed false, so that actuators don't send any
|
|
|
|
// values until the vehicle code has fully started
|
|
|
|
bool soft_armed = false;
|
2019-06-17 23:57:39 -03:00
|
|
|
uint32_t last_armed_change_ms;
|
2012-12-18 20:09:24 -04:00
|
|
|
};
|