2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2015-10-19 11:26:18 -03:00
|
|
|
#include <assert.h>
|
2018-06-27 00:42:30 -03:00
|
|
|
#include <errno.h>
|
2019-05-09 05:48:37 -03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2015-10-19 11:26:18 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
|
|
|
#include "HAL_SITL_Class.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
#include "Scheduler.h"
|
2012-12-13 18:57:01 -04:00
|
|
|
#include "AnalogIn.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
#include "UARTDriver.h"
|
2020-08-03 00:24:26 -03:00
|
|
|
#include "I2CDevice.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
#include "Storage.h"
|
2012-12-17 22:33:46 -04:00
|
|
|
#include "RCInput.h"
|
2012-12-17 23:56:21 -04:00
|
|
|
#include "RCOutput.h"
|
2016-07-21 21:58:02 -03:00
|
|
|
#include "GPIO.h"
|
2012-12-13 18:57:01 -04:00
|
|
|
#include "SITL_State.h"
|
2012-12-18 21:13:01 -04:00
|
|
|
#include "Util.h"
|
2019-08-15 09:33:00 -03:00
|
|
|
#include "DSP.h"
|
2020-05-31 09:32:19 -03:00
|
|
|
#include "CANSocketIface.h"
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2019-07-03 22:38:24 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL_Empty/AP_HAL_Empty.h>
|
|
|
|
#include <AP_HAL_Empty/AP_HAL_Empty_Private.h>
|
2019-05-09 05:48:37 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2019-06-26 23:38:13 -03:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2012-12-17 22:33:46 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2019-01-21 00:21:42 -04:00
|
|
|
static Storage sitlStorage;
|
2012-12-13 18:57:01 -04:00
|
|
|
static SITL_State sitlState;
|
2016-01-10 02:23:32 -04:00
|
|
|
static Scheduler sitlScheduler(&sitlState);
|
2020-09-12 16:04:38 -03:00
|
|
|
#if !defined(HAL_BUILD_AP_PERIPH)
|
2016-01-10 02:23:32 -04:00
|
|
|
static RCInput sitlRCInput(&sitlState);
|
|
|
|
static RCOutput sitlRCOutput(&sitlState);
|
2016-07-21 21:58:02 -03:00
|
|
|
static GPIO sitlGPIO(&sitlState);
|
2020-09-12 16:04:38 -03:00
|
|
|
#else
|
|
|
|
static Empty::RCInput sitlRCInput;
|
|
|
|
static Empty::RCOutput sitlRCOutput;
|
|
|
|
static Empty::GPIO sitlGPIO;
|
|
|
|
#endif
|
|
|
|
static AnalogIn sitlAnalogIn(&sitlState);
|
2019-08-15 09:33:00 -03:00
|
|
|
static DSP dspDriver;
|
|
|
|
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2012-12-18 06:14:32 -04:00
|
|
|
// use the Empty HAL for hardware we don't emulate
|
2015-12-07 15:01:34 -04:00
|
|
|
static Empty::SPIDeviceManager emptySPI;
|
2015-11-23 15:15:41 -04:00
|
|
|
static Empty::OpticalFlow emptyOpticalFlow;
|
2019-03-25 21:14:30 -03:00
|
|
|
static Empty::Flash emptyFlash;
|
2012-12-17 22:33:46 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
static UARTDriver sitlUart0Driver(0, &sitlState);
|
|
|
|
static UARTDriver sitlUart1Driver(1, &sitlState);
|
|
|
|
static UARTDriver sitlUart2Driver(2, &sitlState);
|
|
|
|
static UARTDriver sitlUart3Driver(3, &sitlState);
|
|
|
|
static UARTDriver sitlUart4Driver(4, &sitlState);
|
2016-04-19 00:48:18 -03:00
|
|
|
static UARTDriver sitlUart5Driver(5, &sitlState);
|
2018-06-27 08:34:24 -03:00
|
|
|
static UARTDriver sitlUart6Driver(6, &sitlState);
|
2019-07-12 01:58:43 -03:00
|
|
|
static UARTDriver sitlUart7Driver(7, &sitlState);
|
2020-11-26 02:49:00 -04:00
|
|
|
static UARTDriver sitlUart8Driver(8, &sitlState);
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2020-09-12 16:04:38 -03:00
|
|
|
#if defined(HAL_BUILD_AP_PERIPH)
|
|
|
|
static Empty::I2CDeviceManager i2c_mgr_instance;
|
|
|
|
#else
|
2020-08-03 00:24:26 -03:00
|
|
|
static I2CDeviceManager i2c_mgr_instance;
|
2020-09-12 16:04:38 -03:00
|
|
|
#endif
|
2016-01-10 02:23:32 -04:00
|
|
|
static Util utilInstance(&sitlState);
|
2012-12-18 21:13:01 -04:00
|
|
|
|
2020-05-31 09:32:19 -03:00
|
|
|
|
|
|
|
#if HAL_NUM_CAN_IFACES
|
|
|
|
static HALSITL::CANIface* canDrivers[HAL_NUM_CAN_IFACES];
|
|
|
|
#endif
|
|
|
|
|
2021-06-02 08:48:38 -03:00
|
|
|
static Empty::QSPIDeviceManager qspi_mgr_instance;
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
HAL_SITL::HAL_SITL() :
|
2012-12-11 21:30:12 -04:00
|
|
|
AP_HAL::HAL(
|
2017-01-09 08:23:23 -04:00
|
|
|
&sitlUart0Driver, /* uartA */
|
|
|
|
&sitlUart1Driver, /* uartB */
|
|
|
|
&sitlUart2Driver, /* uartC */
|
|
|
|
&sitlUart3Driver, /* uartD */
|
|
|
|
&sitlUart4Driver, /* uartE */
|
|
|
|
&sitlUart5Driver, /* uartF */
|
2018-06-27 08:34:24 -03:00
|
|
|
&sitlUart6Driver, /* uartG */
|
2019-07-12 01:58:43 -03:00
|
|
|
&sitlUart7Driver, /* uartH */
|
2020-11-26 02:49:00 -04:00
|
|
|
&sitlUart8Driver, /* uartI */
|
2015-12-01 15:57:35 -04:00
|
|
|
&i2c_mgr_instance,
|
2017-01-09 08:23:23 -04:00
|
|
|
&emptySPI, /* spi */
|
2021-06-02 08:48:38 -03:00
|
|
|
&qspi_mgr_instance,
|
2017-01-09 08:23:23 -04:00
|
|
|
&sitlAnalogIn, /* analogin */
|
2019-01-21 00:21:42 -04:00
|
|
|
&sitlStorage, /* storage */
|
2017-01-09 08:23:23 -04:00
|
|
|
&sitlUart0Driver, /* console */
|
|
|
|
&sitlGPIO, /* gpio */
|
|
|
|
&sitlRCInput, /* rcinput */
|
|
|
|
&sitlRCOutput, /* rcoutput */
|
|
|
|
&sitlScheduler, /* scheduler */
|
|
|
|
&utilInstance, /* util */
|
2019-08-15 09:33:00 -03:00
|
|
|
&emptyOpticalFlow, /* onboard optical flow */
|
|
|
|
&emptyFlash, /* flash driver */
|
|
|
|
&dspDriver, /* dsp driver */
|
2020-05-31 09:32:19 -03:00
|
|
|
#if HAL_NUM_CAN_IFACES
|
|
|
|
(AP_HAL::CANIface**)canDrivers
|
|
|
|
#else
|
|
|
|
nullptr
|
|
|
|
#endif
|
|
|
|
), /* CAN */
|
2012-12-13 18:57:01 -04:00
|
|
|
_sitl_state(&sitlState)
|
2012-12-11 21:30:12 -04:00
|
|
|
{}
|
|
|
|
|
2018-06-27 00:42:30 -03:00
|
|
|
static char *new_argv[100];
|
|
|
|
|
2019-05-09 05:48:37 -03:00
|
|
|
/*
|
|
|
|
save watchdog data
|
|
|
|
*/
|
2019-05-09 07:46:52 -03:00
|
|
|
static bool watchdog_save(const uint32_t *data, uint32_t nwords)
|
2019-05-09 05:48:37 -03:00
|
|
|
{
|
|
|
|
int fd = ::open("persistent.dat", O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
2019-05-09 07:46:52 -03:00
|
|
|
bool ret = false;
|
2019-05-09 05:48:37 -03:00
|
|
|
if (fd != -1) {
|
2019-05-11 20:10:31 -03:00
|
|
|
if (::write(fd, data, nwords*4) == (ssize_t)(nwords*4)) {
|
2019-05-09 07:46:52 -03:00
|
|
|
ret = true;
|
|
|
|
}
|
2019-05-09 05:48:37 -03:00
|
|
|
::close(fd);
|
|
|
|
}
|
2019-05-09 07:46:52 -03:00
|
|
|
return ret;
|
2019-05-09 05:48:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
load watchdog data
|
|
|
|
*/
|
|
|
|
static bool watchdog_load(uint32_t *data, uint32_t nwords)
|
|
|
|
{
|
|
|
|
int fd = ::open("persistent.dat", O_RDONLY, 0644);
|
|
|
|
bool ret = false;
|
|
|
|
if (fd != -1) {
|
2019-05-11 20:10:31 -03:00
|
|
|
ret = (::read(fd, data, nwords*4) == (ssize_t)(nwords*4));
|
2019-05-09 05:48:37 -03:00
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
implement watchdoh reset via SIGALRM
|
|
|
|
*/
|
|
|
|
static void sig_alrm(int signum)
|
|
|
|
{
|
|
|
|
static char env[] = "SITL_WATCHDOG_RESET=1";
|
|
|
|
putenv(env);
|
|
|
|
printf("GOT SIGALRM\n");
|
|
|
|
execv(new_argv[0], new_argv);
|
|
|
|
}
|
|
|
|
|
2019-09-22 21:39:42 -03:00
|
|
|
void HAL_SITL::exit_signal_handler(int signum)
|
|
|
|
{
|
|
|
|
HALSITL::Scheduler::_should_exit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_SITL::setup_signal_handlers() const
|
|
|
|
{
|
|
|
|
struct sigaction sa = { };
|
|
|
|
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
|
|
|
sa.sa_handler = HAL_SITL::exit_signal_handler;
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
}
|
|
|
|
|
2019-09-28 04:39:30 -03:00
|
|
|
/*
|
|
|
|
fill 8k of stack with NaN. This allows us to find uses of
|
|
|
|
uninitialised memory without valgrind
|
|
|
|
*/
|
|
|
|
static void fill_stack_nan(void)
|
|
|
|
{
|
|
|
|
float stk[2048];
|
|
|
|
fill_nanf(stk, ARRAY_SIZE(stk));
|
|
|
|
}
|
|
|
|
|
2015-10-19 12:59:47 -03:00
|
|
|
void HAL_SITL::run(int argc, char * const argv[], Callbacks* callbacks) const
|
2012-12-13 18:57:01 -04:00
|
|
|
{
|
2015-10-19 12:59:47 -03:00
|
|
|
assert(callbacks);
|
|
|
|
|
2020-11-08 17:48:25 -04:00
|
|
|
utilInstance.init(argc, argv);
|
2012-12-13 18:57:01 -04:00
|
|
|
_sitl_state->init(argc, argv);
|
2019-09-22 21:39:42 -03:00
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
scheduler->init();
|
2020-12-10 20:18:38 -04:00
|
|
|
serial(0)->begin(115200);
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
rcin->init();
|
|
|
|
rcout->init();
|
2012-12-17 22:33:46 -04:00
|
|
|
|
2017-01-09 08:23:23 -04:00
|
|
|
// spi->init();
|
2015-12-02 11:14:20 -04:00
|
|
|
analogin->init();
|
2015-10-19 11:26:18 -03:00
|
|
|
|
2019-05-09 05:48:37 -03:00
|
|
|
if (getenv("SITL_WATCHDOG_RESET")) {
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::watchdog_reset);
|
2019-05-09 05:48:37 -03:00
|
|
|
if (watchdog_load((uint32_t *)&utilInstance.persistent_data, (sizeof(utilInstance.persistent_data)+3)/4)) {
|
2020-12-10 20:18:38 -04:00
|
|
|
serial(0)->printf("Loaded watchdog data");
|
2020-03-25 08:26:15 -03:00
|
|
|
utilInstance.last_persistent_data = utilInstance.persistent_data;
|
2019-05-09 05:48:37 -03:00
|
|
|
}
|
2015-10-19 11:26:18 -03:00
|
|
|
}
|
2018-06-27 00:42:30 -03:00
|
|
|
|
2019-05-11 05:47:00 -03:00
|
|
|
// form a new argv, removing problem parameters. This is used for reboot
|
2018-06-27 00:42:30 -03:00
|
|
|
uint8_t new_argv_offset = 0;
|
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(new_argv) && i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-w")) {
|
|
|
|
// don't wipe params on reboot
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
new_argv[new_argv_offset++] = argv[i];
|
|
|
|
}
|
2019-05-09 05:48:37 -03:00
|
|
|
|
2019-09-28 04:39:30 -03:00
|
|
|
fill_stack_nan();
|
|
|
|
|
2019-05-09 05:48:37 -03:00
|
|
|
callbacks->setup();
|
2020-12-23 07:28:14 -04:00
|
|
|
scheduler->set_system_initialized();
|
2019-05-09 05:48:37 -03:00
|
|
|
|
2021-05-17 03:51:37 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2019-05-15 03:55:05 -03:00
|
|
|
if (getenv("SITL_WATCHDOG_RESET")) {
|
|
|
|
const AP_HAL::Util::PersistentData &pd = util->persistent_data;
|
2020-04-29 21:40:46 -03:00
|
|
|
AP::logger().WriteCritical("WDOG", "TimeUS,Task,IErr,IErrCnt,IErrLn,MavMsg,MavCmd,SemLine", "QbIHHHHH",
|
2019-05-15 03:55:05 -03:00
|
|
|
AP_HAL::micros64(),
|
|
|
|
pd.scheduler_task,
|
|
|
|
pd.internal_errors,
|
|
|
|
pd.internal_error_count,
|
2020-04-29 21:40:46 -03:00
|
|
|
pd.internal_error_last_line,
|
2019-05-15 03:55:05 -03:00
|
|
|
pd.last_mavlink_msgid,
|
|
|
|
pd.last_mavlink_cmd,
|
|
|
|
pd.semaphore_line);
|
|
|
|
}
|
2020-09-12 16:04:38 -03:00
|
|
|
#endif
|
2019-05-15 03:55:05 -03:00
|
|
|
|
2019-05-09 05:48:37 -03:00
|
|
|
bool using_watchdog = AP_BoardConfig::watchdog_enabled();
|
|
|
|
if (using_watchdog) {
|
|
|
|
signal(SIGALRM, sig_alrm);
|
|
|
|
alarm(2);
|
|
|
|
}
|
2019-09-22 21:39:42 -03:00
|
|
|
setup_signal_handlers();
|
2019-05-09 05:48:37 -03:00
|
|
|
|
|
|
|
uint32_t last_watchdog_save = AP_HAL::millis();
|
2020-11-08 18:43:41 -04:00
|
|
|
uint8_t fill_count = 0;
|
2019-05-09 05:48:37 -03:00
|
|
|
|
|
|
|
while (!HALSITL::Scheduler::_should_reboot) {
|
2019-09-22 21:39:42 -03:00
|
|
|
if (HALSITL::Scheduler::_should_exit) {
|
|
|
|
::fprintf(stderr, "Exitting\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-11-08 18:43:41 -04:00
|
|
|
if (fill_count++ % 10 == 0) {
|
|
|
|
// only fill every 10 loops. This still gives us a lot of
|
|
|
|
// protection, but saves a lot of CPU
|
|
|
|
fill_stack_nan();
|
|
|
|
}
|
2019-05-09 05:48:37 -03:00
|
|
|
callbacks->loop();
|
|
|
|
HALSITL::Scheduler::_run_io_procs();
|
|
|
|
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - last_watchdog_save >= 100 && using_watchdog) {
|
|
|
|
// save persistent data every 100ms
|
|
|
|
last_watchdog_save = now;
|
|
|
|
watchdog_save((uint32_t *)&utilInstance.persistent_data, (sizeof(utilInstance.persistent_data)+3)/4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (using_watchdog) {
|
|
|
|
// note that this only works for a speedup of 1
|
|
|
|
alarm(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 05:02:18 -03:00
|
|
|
actually_reboot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_SITL::actually_reboot()
|
|
|
|
{
|
2018-06-27 00:42:30 -03:00
|
|
|
execv(new_argv[0], new_argv);
|
|
|
|
AP_HAL::panic("PANIC: REBOOT FAILED: %s", strerror(errno));
|
2015-10-19 11:26:18 -03:00
|
|
|
}
|
|
|
|
|
2015-10-16 17:22:11 -03:00
|
|
|
const AP_HAL::HAL& AP_HAL::get_HAL() {
|
|
|
|
static const HAL_SITL hal;
|
|
|
|
return hal;
|
|
|
|
}
|
2012-12-11 21:30:12 -04:00
|
|
|
|
2017-01-09 08:23:23 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL
|