2016-02-02 18:09:39 -04:00
|
|
|
#pragma once
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2016-02-02 23:42:47 -04:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_Linux.h"
|
2020-03-11 21:37:54 -03:00
|
|
|
|
2014-08-20 18:49:49 -03:00
|
|
|
#include "Semaphores.h"
|
2016-02-02 18:12:07 -04:00
|
|
|
#include "Thread.h"
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
#define LINUX_SCHEDULER_MAX_TIMER_PROCS 10
|
2015-08-03 13:06:12 -03:00
|
|
|
#define LINUX_SCHEDULER_MAX_TIMESLICED_PROCS 10
|
2015-01-26 02:45:17 -04:00
|
|
|
#define LINUX_SCHEDULER_MAX_IO_PROCS 10
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2016-07-19 10:01:47 -03:00
|
|
|
#define AP_LINUX_SENSORS_STACK_SIZE 256 * 1024
|
|
|
|
#define AP_LINUX_SENSORS_SCHED_POLICY SCHED_FIFO
|
|
|
|
#define AP_LINUX_SENSORS_SCHED_PRIO 12
|
|
|
|
|
2016-07-29 16:14:02 -03:00
|
|
|
namespace Linux {
|
|
|
|
|
|
|
|
class Scheduler : public AP_HAL::Scheduler {
|
2013-09-22 03:01:24 -03:00
|
|
|
public:
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler();
|
2015-11-11 10:02:43 -04:00
|
|
|
|
|
|
|
static Scheduler *from(AP_HAL::Scheduler *scheduler) {
|
|
|
|
return static_cast<Scheduler*>(scheduler);
|
|
|
|
}
|
|
|
|
|
2019-02-21 19:08:12 -04:00
|
|
|
void init() override;
|
|
|
|
void delay(uint16_t ms) override;
|
|
|
|
void delay_microseconds(uint16_t us) override;
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2019-02-21 19:08:12 -04:00
|
|
|
void register_timer_process(AP_HAL::MemberProc) override;
|
|
|
|
void register_io_process(AP_HAL::MemberProc) override;
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2017-09-17 22:02:54 -03:00
|
|
|
bool in_main_thread() const override;
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2019-02-21 19:08:12 -04:00
|
|
|
void register_timer_failsafe(AP_HAL::Proc, uint32_t period_us) override;
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2020-12-23 07:27:27 -04:00
|
|
|
void set_system_initialized() override;
|
|
|
|
bool is_system_initialized() override { return _initialized; };
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2019-02-21 19:08:12 -04:00
|
|
|
void reboot(bool hold_in_bootloader) override;
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2019-02-21 19:08:12 -04:00
|
|
|
void stop_clock(uint64_t time_usec) override;
|
2013-12-29 18:31:33 -04:00
|
|
|
|
2015-11-11 10:02:43 -04:00
|
|
|
uint64_t stopped_clock_usec() const { return _stopped_clock_usec; }
|
|
|
|
|
2016-02-01 18:17:54 -04:00
|
|
|
void microsleep(uint32_t usec);
|
|
|
|
|
2016-10-26 18:58:57 -03:00
|
|
|
void teardown();
|
|
|
|
|
2018-07-06 21:25:39 -03:00
|
|
|
/*
|
|
|
|
create a new thread
|
|
|
|
*/
|
|
|
|
bool thread_create(AP_HAL::MemberProc, const char *name, uint32_t stack_size, priority_base base, int8_t priority) override;
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
private:
|
2016-02-03 01:13:57 -04:00
|
|
|
class SchedulerThread : public PeriodicThread {
|
2016-02-02 19:07:23 -04:00
|
|
|
public:
|
|
|
|
SchedulerThread(Thread::task_t t, Scheduler &sched)
|
2016-02-03 01:13:57 -04:00
|
|
|
: PeriodicThread(t)
|
2016-02-02 19:07:23 -04:00
|
|
|
, _sched(sched)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool _run() override;
|
|
|
|
|
|
|
|
Scheduler &_sched;
|
|
|
|
};
|
|
|
|
|
2016-11-15 22:20:31 -04:00
|
|
|
void init_realtime();
|
|
|
|
|
2016-02-02 23:42:47 -04:00
|
|
|
void _wait_all_threads();
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2016-04-23 02:01:22 -03:00
|
|
|
void _debug_stack();
|
|
|
|
|
2013-09-30 07:36:12 -03:00
|
|
|
AP_HAL::Proc _failsafe;
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
bool _initialized;
|
2016-02-02 23:42:47 -04:00
|
|
|
pthread_barrier_t _initialized_barrier;
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2013-09-30 07:36:12 -03:00
|
|
|
AP_HAL::MemberProc _timer_proc[LINUX_SCHEDULER_MAX_TIMER_PROCS];
|
2013-09-28 01:59:00 -03:00
|
|
|
uint8_t _num_timer_procs;
|
|
|
|
volatile bool _in_timer_proc;
|
|
|
|
|
2015-01-26 02:45:17 -04:00
|
|
|
AP_HAL::MemberProc _io_proc[LINUX_SCHEDULER_MAX_IO_PROCS];
|
2013-09-28 01:59:00 -03:00
|
|
|
uint8_t _num_io_procs;
|
|
|
|
|
2016-02-02 19:07:23 -04:00
|
|
|
SchedulerThread _timer_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_timer_task, void), *this};
|
|
|
|
SchedulerThread _io_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_io_task, void), *this};
|
|
|
|
SchedulerThread _rcin_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_rcin_task, void), *this};
|
|
|
|
SchedulerThread _uart_thread{FUNCTOR_BIND_MEMBER(&Scheduler::_uart_task, void), *this};
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2016-02-02 18:12:07 -04:00
|
|
|
void _timer_task();
|
|
|
|
void _io_task();
|
|
|
|
void _rcin_task();
|
|
|
|
void _uart_task();
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2016-02-02 18:12:07 -04:00
|
|
|
void _run_io();
|
|
|
|
void _run_uarts();
|
2013-12-31 05:05:07 -04:00
|
|
|
|
2015-11-11 10:02:43 -04:00
|
|
|
uint64_t _stopped_clock_usec;
|
2016-04-23 02:01:22 -03:00
|
|
|
uint64_t _last_stack_debug_msec;
|
2017-07-31 18:41:43 -03:00
|
|
|
pthread_t _main_ctx;
|
2014-08-20 18:49:49 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
Semaphore _io_semaphore;
|
2013-09-22 03:01:24 -03:00
|
|
|
};
|
2016-07-29 16:14:02 -03:00
|
|
|
|
|
|
|
}
|