2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
#ifndef __AP_HAL_LINUX_SCHEDULER_H__
|
|
|
|
#define __AP_HAL_LINUX_SCHEDULER_H__
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_Linux.h"
|
2014-08-20 18:49:49 -03:00
|
|
|
#include "Semaphores.h"
|
2013-09-28 03:28:36 -03:00
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2013-09-22 03:01:24 -03:00
|
|
|
#include <sys/time.h>
|
2013-09-28 01:59:00 -03:00
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
class Linux::Scheduler : public AP_HAL::Scheduler {
|
2015-03-18 16:50:44 -03:00
|
|
|
|
|
|
|
typedef void *(*pthread_startroutine_t)(void *);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
void init();
|
2013-09-22 03:01:24 -03:00
|
|
|
void delay(uint16_t ms);
|
|
|
|
void delay_microseconds(uint16_t us);
|
|
|
|
void register_delay_callback(AP_HAL::Proc,
|
|
|
|
uint16_t min_time_ms);
|
|
|
|
|
2013-09-30 23:09:46 -03:00
|
|
|
void register_timer_process(AP_HAL::MemberProc);
|
2015-08-03 13:06:12 -03:00
|
|
|
bool register_timer_process(AP_HAL::MemberProc, uint8_t);
|
2013-09-30 23:09:46 -03:00
|
|
|
void register_io_process(AP_HAL::MemberProc);
|
2013-09-22 03:01:24 -03:00
|
|
|
void suspend_timer_procs();
|
|
|
|
void resume_timer_procs();
|
|
|
|
|
|
|
|
bool in_timerprocess();
|
|
|
|
|
2013-09-30 07:36:12 -03:00
|
|
|
void register_timer_failsafe(AP_HAL::Proc, uint32_t period_us);
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
void begin_atomic();
|
|
|
|
void end_atomic();
|
|
|
|
|
|
|
|
bool system_initializing();
|
|
|
|
void system_initialized();
|
|
|
|
|
|
|
|
void reboot(bool hold_in_bootloader);
|
|
|
|
|
2014-02-26 04:33:39 -04:00
|
|
|
void stop_clock(uint64_t time_usec);
|
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; }
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
private:
|
2013-09-28 01:59:00 -03:00
|
|
|
void _timer_handler(int signum);
|
2013-10-07 21:09:29 -03:00
|
|
|
void _microsleep(uint32_t usec);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
AP_HAL::Proc _delay_cb;
|
|
|
|
uint16_t _min_delay_cb_ms;
|
|
|
|
|
2013-09-30 07:36:12 -03:00
|
|
|
AP_HAL::Proc _failsafe;
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
bool _initialized;
|
|
|
|
volatile bool _timer_pending;
|
|
|
|
|
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-08-03 13:06:12 -03:00
|
|
|
uint8_t _timeslices_count;
|
|
|
|
|
|
|
|
struct timesliced_proc {
|
|
|
|
AP_HAL::MemberProc proc;
|
|
|
|
uint8_t timeslot;
|
|
|
|
uint8_t freq_div;
|
|
|
|
};
|
|
|
|
timesliced_proc _timesliced_proc[LINUX_SCHEDULER_MAX_TIMESLICED_PROCS];
|
|
|
|
uint8_t _num_timesliced_procs;
|
|
|
|
uint8_t _max_freq_div;
|
2013-09-28 01:59:00 -03:00
|
|
|
|
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;
|
|
|
|
volatile bool _in_io_proc;
|
|
|
|
|
|
|
|
volatile bool _timer_event_missed;
|
|
|
|
|
|
|
|
pthread_t _timer_thread_ctx;
|
|
|
|
pthread_t _io_thread_ctx;
|
2014-08-14 13:48:17 -03:00
|
|
|
pthread_t _rcin_thread_ctx;
|
2013-09-28 01:59:00 -03:00
|
|
|
pthread_t _uart_thread_ctx;
|
2014-08-23 04:52:43 -03:00
|
|
|
pthread_t _tonealarm_thread_ctx;
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2015-03-18 17:23:37 -03:00
|
|
|
static void *_timer_thread(void* arg);
|
|
|
|
static void *_io_thread(void* arg);
|
|
|
|
static void *_rcin_thread(void* arg);
|
|
|
|
static void *_uart_thread(void* arg);
|
2015-12-13 23:19:54 -04:00
|
|
|
static void _run_uarts(void);
|
2015-03-18 17:23:37 -03:00
|
|
|
static void *_tonealarm_thread(void* arg);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
void _run_timers(bool called_from_timer_thread);
|
|
|
|
void _run_io(void);
|
2015-03-18 17:59:29 -03:00
|
|
|
void _create_realtime_thread(pthread_t *ctx, int rtprio, const char *name,
|
2015-03-18 16:50:44 -03:00
|
|
|
pthread_startroutine_t start_routine);
|
2015-08-03 13:06:12 -03:00
|
|
|
bool _register_timesliced_proc(AP_HAL::MemberProc, uint8_t);
|
2013-12-31 05:05:07 -04:00
|
|
|
|
2015-11-11 10:02:43 -04:00
|
|
|
uint64_t _stopped_clock_usec;
|
2014-08-20 18:49:49 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
Semaphore _timer_semaphore;
|
|
|
|
Semaphore _io_semaphore;
|
2013-09-22 03:01:24 -03:00
|
|
|
};
|
|
|
|
|
2013-09-28 03:28:36 -03:00
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
#endif // __AP_HAL_LINUX_SCHEDULER_H__
|