2012-12-12 18:04:27 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
#include "Scheduler.h"
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2014-11-15 20:04:55 -04:00
|
|
|
#include <fenv.h>
|
2015-06-04 16:05:42 -03:00
|
|
|
#include "fenv_polyfill.h"
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
|
2013-09-30 07:35:42 -03:00
|
|
|
AP_HAL::Proc SITLScheduler::_failsafe = NULL;
|
2012-12-12 18:04:27 -04:00
|
|
|
volatile bool SITLScheduler::_timer_suspended = false;
|
2013-01-03 21:32:52 -04:00
|
|
|
volatile bool SITLScheduler::_timer_event_missed = false;
|
2013-04-17 08:33:50 -03:00
|
|
|
|
2013-09-30 07:35:42 -03:00
|
|
|
AP_HAL::MemberProc SITLScheduler::_timer_proc[SITL_SCHEDULER_MAX_TIMER_PROCS] = {NULL};
|
2012-12-12 18:04:27 -04:00
|
|
|
uint8_t SITLScheduler::_num_timer_procs = 0;
|
|
|
|
bool SITLScheduler::_in_timer_proc = false;
|
2013-04-17 08:33:50 -03:00
|
|
|
|
2013-09-30 07:35:42 -03:00
|
|
|
AP_HAL::MemberProc SITLScheduler::_io_proc[SITL_SCHEDULER_MAX_TIMER_PROCS] = {NULL};
|
2013-04-17 08:33:50 -03:00
|
|
|
uint8_t SITLScheduler::_num_io_procs = 0;
|
|
|
|
bool SITLScheduler::_in_io_proc = false;
|
|
|
|
|
2012-12-13 18:57:01 -04:00
|
|
|
struct timeval SITLScheduler::_sketch_start_time;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2014-11-15 20:04:55 -04:00
|
|
|
SITLScheduler::SITLScheduler(SITL_State *sitlState) :
|
2015-03-21 11:27:25 -03:00
|
|
|
_sitlState(sitlState),
|
|
|
|
stopped_clock_usec(0)
|
|
|
|
{
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::init(void *unused)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
gettimeofday(&_sketch_start_time,NULL);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
uint64_t SITLScheduler::_micros64()
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
struct timeval tp;
|
|
|
|
gettimeofday(&tp,NULL);
|
|
|
|
uint64_t ret = 1.0e6*((tp.tv_sec + (tp.tv_usec*1.0e-6)) -
|
2014-08-19 18:57:45 -03:00
|
|
|
(_sketch_start_time.tv_sec +
|
|
|
|
(_sketch_start_time.tv_usec*1.0e-6)));
|
|
|
|
return ret;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
uint64_t SITLScheduler::micros64()
|
2014-08-19 18:57:45 -03:00
|
|
|
{
|
2015-03-21 11:27:25 -03:00
|
|
|
if (stopped_clock_usec) {
|
|
|
|
return stopped_clock_usec;
|
|
|
|
}
|
2014-08-19 18:57:45 -03:00
|
|
|
return _micros64();
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
uint32_t SITLScheduler::micros()
|
2012-12-13 18:57:01 -04:00
|
|
|
{
|
2014-08-19 18:57:45 -03:00
|
|
|
return micros64() & 0xFFFFFFFF;
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
uint64_t SITLScheduler::millis64()
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-03-21 11:27:25 -03:00
|
|
|
if (stopped_clock_usec) {
|
|
|
|
return stopped_clock_usec/1000;
|
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
struct timeval tp;
|
|
|
|
gettimeofday(&tp,NULL);
|
|
|
|
uint64_t ret = 1.0e3*((tp.tv_sec + (tp.tv_usec*1.0e-6)) -
|
2014-08-19 18:57:45 -03:00
|
|
|
(_sketch_start_time.tv_sec +
|
|
|
|
(_sketch_start_time.tv_usec*1.0e-6)));
|
|
|
|
return ret;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
uint32_t SITLScheduler::millis()
|
2014-08-19 18:57:45 -03:00
|
|
|
{
|
|
|
|
return millis64() & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::delay_microseconds(uint16_t usec)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
uint64_t start = micros64();
|
2015-03-21 11:27:25 -03:00
|
|
|
uint64_t dtime;
|
2015-05-04 21:59:07 -03:00
|
|
|
while ((dtime=(micros64() - start) < usec)) {
|
2015-03-21 11:27:25 -03:00
|
|
|
if (stopped_clock_usec) {
|
2015-05-02 08:41:13 -03:00
|
|
|
_sitlState->wait_clock(start+usec);
|
2015-03-21 11:27:25 -03:00
|
|
|
} else {
|
|
|
|
usleep(usec - dtime);
|
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2012-12-19 22:16:10 -04:00
|
|
|
void SITLScheduler::delay(uint16_t ms)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
while (ms > 0) {
|
2015-03-21 11:27:25 -03:00
|
|
|
delay_microseconds(1000);
|
|
|
|
ms--;
|
2012-12-12 18:04:27 -04:00
|
|
|
if (_min_delay_cb_ms <= ms) {
|
|
|
|
if (_delay_cb) {
|
|
|
|
_delay_cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SITLScheduler::register_delay_callback(AP_HAL::Proc proc,
|
2015-05-04 21:59:07 -03:00
|
|
|
uint16_t min_time_ms)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
_delay_cb = proc;
|
|
|
|
_min_delay_cb_ms = min_time_ms;
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::register_timer_process(AP_HAL::MemberProc proc)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_timer_procs; i++) {
|
|
|
|
if (_timer_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_timer_procs < SITL_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
_timer_proc[_num_timer_procs] = proc;
|
|
|
|
_num_timer_procs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::register_io_process(AP_HAL::MemberProc proc)
|
2013-04-17 08:33:50 -03:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_io_procs; i++) {
|
|
|
|
if (_io_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_io_procs < SITL_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
_io_proc[_num_io_procs] = proc;
|
|
|
|
_num_io_procs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::register_timer_failsafe(AP_HAL::Proc failsafe, uint32_t period_us)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
_failsafe = failsafe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SITLScheduler::suspend_timer_procs() {
|
|
|
|
_timer_suspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SITLScheduler::resume_timer_procs() {
|
|
|
|
_timer_suspended = false;
|
2013-01-03 21:32:52 -04:00
|
|
|
if (_timer_event_missed) {
|
|
|
|
_timer_event_missed = false;
|
|
|
|
_run_timer_procs(false);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 17:50:32 -04:00
|
|
|
bool SITLScheduler::in_timerprocess() {
|
2013-04-17 08:33:50 -03:00
|
|
|
return _in_timer_proc || _in_io_proc;
|
2013-01-10 17:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SITLScheduler::system_initializing() {
|
|
|
|
return !_initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SITLScheduler::system_initialized() {
|
|
|
|
if (_initialized) {
|
|
|
|
panic(
|
2015-10-24 18:45:41 -03:00
|
|
|
"PANIC: scheduler system initialized called more than once");
|
2013-01-10 17:50:32 -04:00
|
|
|
}
|
2015-04-27 00:08:50 -03:00
|
|
|
int exceptions = FE_OVERFLOW | FE_DIVBYZERO;
|
|
|
|
#ifndef __i386__
|
|
|
|
// i386 with gcc doesn't work with FE_INVALID
|
|
|
|
exceptions |= FE_INVALID;
|
|
|
|
#endif
|
2014-12-01 21:57:13 -04:00
|
|
|
if (_sitlState->_sitl == NULL || _sitlState->_sitl->float_exception) {
|
2015-04-27 00:08:50 -03:00
|
|
|
feenableexcept(exceptions);
|
2014-11-15 20:04:55 -04:00
|
|
|
} else {
|
2015-04-27 00:08:50 -03:00
|
|
|
feclearexcept(exceptions);
|
2014-11-15 20:04:55 -04:00
|
|
|
}
|
2013-01-10 17:50:32 -04:00
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
2013-01-03 21:32:52 -04:00
|
|
|
void SITLScheduler::sitl_end_atomic() {
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_nested_atomic_ctr == 0)
|
2015-10-25 16:50:51 -03:00
|
|
|
hal.uartA->println("NESTED ATOMIC ERROR");
|
2013-01-03 21:32:52 -04:00
|
|
|
else
|
|
|
|
_nested_atomic_ctr--;
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2013-01-03 21:32:52 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::reboot(bool hold_in_bootloader)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-10-25 16:50:51 -03:00
|
|
|
hal.uartA->println("REBOOT NOT IMPLEMENTED\r\n");
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::_run_timer_procs(bool called_from_isr)
|
2012-12-13 18:57:01 -04:00
|
|
|
{
|
|
|
|
if (_in_timer_proc) {
|
|
|
|
// the timer calls took longer than the period of the
|
|
|
|
// timer. This is bad, and may indicate a serious
|
|
|
|
// driver failure. We can't just call the drivers
|
|
|
|
// again, as we could run out of stack. So we only
|
|
|
|
// call the _failsafe call. It's job is to detect if
|
|
|
|
// the drivers or the main loop are indeed dead and to
|
|
|
|
// activate whatever failsafe it thinks may help if
|
|
|
|
// need be. We assume the failsafe code can't
|
|
|
|
// block. If it does then we will recurse and die when
|
|
|
|
// we run out of stack
|
|
|
|
if (_failsafe != NULL) {
|
2013-09-30 07:35:42 -03:00
|
|
|
_failsafe();
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_in_timer_proc = true;
|
|
|
|
|
|
|
|
if (!_timer_suspended) {
|
|
|
|
// now call the timer based drivers
|
|
|
|
for (int i = 0; i < _num_timer_procs; i++) {
|
2015-05-24 07:40:10 -03:00
|
|
|
if (_timer_proc[i]) {
|
2013-09-30 07:35:42 -03:00
|
|
|
_timer_proc[i]();
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
}
|
2013-01-03 21:32:52 -04:00
|
|
|
} else if (called_from_isr) {
|
|
|
|
_timer_event_missed = true;
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// and the failsafe, if one is setup
|
|
|
|
if (_failsafe != NULL) {
|
2015-03-24 12:02:47 -03:00
|
|
|
_failsafe();
|
2012-12-13 18:57:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_in_timer_proc = false;
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
void SITLScheduler::_run_io_procs(bool called_from_isr)
|
2013-04-17 08:33:50 -03:00
|
|
|
{
|
|
|
|
if (_in_io_proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_in_io_proc = true;
|
|
|
|
|
|
|
|
if (!_timer_suspended) {
|
|
|
|
// now call the IO based drivers
|
|
|
|
for (int i = 0; i < _num_io_procs; i++) {
|
2015-05-24 07:40:10 -03:00
|
|
|
if (_io_proc[i]) {
|
2013-09-30 07:35:42 -03:00
|
|
|
_io_proc[i]();
|
2013-04-17 08:33:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (called_from_isr) {
|
|
|
|
_timer_event_missed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_in_io_proc = false;
|
|
|
|
}
|
|
|
|
|
2015-10-22 14:15:06 -03:00
|
|
|
void SITLScheduler::panic(const prog_char_t *errormsg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, errormsg);
|
2015-10-25 16:53:31 -03:00
|
|
|
hal.console->vprintf(errormsg, ap);
|
2015-10-22 14:15:06 -03:00
|
|
|
va_end(ap);
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("\n");
|
2015-10-22 14:15:06 -03:00
|
|
|
|
2012-12-17 22:42:38 -04:00
|
|
|
for(;;);
|
|
|
|
}
|
|
|
|
|
2015-03-21 11:27:25 -03:00
|
|
|
/*
|
|
|
|
set simulation timestamp
|
|
|
|
*/
|
|
|
|
void SITLScheduler::stop_clock(uint64_t time_usec)
|
|
|
|
{
|
2015-04-12 17:05:55 -03:00
|
|
|
stopped_clock_usec = time_usec;
|
2015-03-21 11:27:25 -03:00
|
|
|
_run_io_procs(false);
|
|
|
|
}
|
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
#endif
|