2013-09-28 03:28:36 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
#include "Scheduler.h"
|
2013-09-28 01:59:00 -03:00
|
|
|
#include "Storage.h"
|
2013-09-28 21:13:51 -03:00
|
|
|
#include "UARTDriver.h"
|
2013-09-22 03:01:24 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
2013-09-28 01:59:00 -03:00
|
|
|
#include <poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2013-09-28 21:32:51 -03:00
|
|
|
#include <stdio.h>
|
2013-10-07 21:09:29 -03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/mman.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-10-01 00:05:04 -03:00
|
|
|
#define APM_LINUX_TIMER_PRIORITY 13
|
|
|
|
#define APM_LINUX_UART_PRIORITY 12
|
|
|
|
#define APM_LINUX_MAIN_PRIORITY 11
|
|
|
|
#define APM_LINUX_IO_PRIORITY 10
|
2013-09-28 21:16:07 -03:00
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
LinuxScheduler::LinuxScheduler()
|
|
|
|
{}
|
|
|
|
|
2013-09-28 01:59:00 -03:00
|
|
|
typedef void *(*pthread_startroutine_t)(void *);
|
|
|
|
|
2013-10-07 21:09:29 -03:00
|
|
|
/*
|
|
|
|
setup for realtime. Lock all of memory in the thread and pre-fault
|
|
|
|
the given stack size, so stack faults don't cause timing jitter
|
|
|
|
*/
|
|
|
|
void LinuxScheduler::_setup_realtime(uint32_t size)
|
|
|
|
{
|
|
|
|
uint8_t dummy[size];
|
|
|
|
mlockall(MCL_CURRENT|MCL_FUTURE);
|
|
|
|
memset(dummy, 0, sizeof(dummy));
|
|
|
|
}
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
void LinuxScheduler::init(void* machtnichts)
|
|
|
|
{
|
2013-10-06 06:57:24 -03:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &_sketch_start_time);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2013-10-07 21:09:29 -03:00
|
|
|
_setup_realtime(32768);
|
|
|
|
|
2013-09-28 01:59:00 -03:00
|
|
|
pthread_attr_t thread_attr;
|
|
|
|
struct sched_param param;
|
|
|
|
|
2013-09-28 21:16:07 -03:00
|
|
|
memset(¶m, 0, sizeof(param));
|
|
|
|
|
|
|
|
param.sched_priority = APM_LINUX_MAIN_PRIORITY;
|
|
|
|
sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|
|
|
|
2013-09-28 01:59:00 -03:00
|
|
|
param.sched_priority = APM_LINUX_TIMER_PRIORITY;
|
2013-09-28 05:38:50 -03:00
|
|
|
pthread_attr_init(&thread_attr);
|
2013-09-28 01:59:00 -03:00
|
|
|
(void)pthread_attr_setschedparam(&thread_attr, ¶m);
|
|
|
|
pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
|
|
|
|
|
|
|
|
pthread_create(&_timer_thread_ctx, &thread_attr, (pthread_startroutine_t)&Linux::LinuxScheduler::_timer_thread, this);
|
|
|
|
|
|
|
|
// the UART thread runs at a medium priority
|
|
|
|
pthread_attr_init(&thread_attr);
|
|
|
|
param.sched_priority = APM_LINUX_UART_PRIORITY;
|
|
|
|
(void)pthread_attr_setschedparam(&thread_attr, ¶m);
|
|
|
|
pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
|
|
|
|
|
|
|
|
pthread_create(&_uart_thread_ctx, &thread_attr, (pthread_startroutine_t)&Linux::LinuxScheduler::_uart_thread, this);
|
|
|
|
|
|
|
|
// the IO thread runs at lower priority
|
|
|
|
pthread_attr_init(&thread_attr);
|
|
|
|
param.sched_priority = APM_LINUX_IO_PRIORITY;
|
|
|
|
(void)pthread_attr_setschedparam(&thread_attr, ¶m);
|
|
|
|
pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);
|
|
|
|
|
|
|
|
pthread_create(&_io_thread_ctx, &thread_attr, (pthread_startroutine_t)&Linux::LinuxScheduler::_io_thread, this);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2013-10-07 21:09:29 -03:00
|
|
|
void LinuxScheduler::_microsleep(uint32_t usec)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = usec*1000UL;
|
|
|
|
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) ;
|
|
|
|
}
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
void LinuxScheduler::delay(uint16_t ms)
|
|
|
|
{
|
2014-02-26 04:33:39 -04:00
|
|
|
if (stopped_clock_usec) {
|
|
|
|
stopped_clock_usec += 1000UL*ms;
|
2014-01-04 20:39:29 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-09-28 21:32:51 -03:00
|
|
|
uint32_t start = millis();
|
|
|
|
|
|
|
|
while ((millis() - start) < ms) {
|
|
|
|
// this yields the CPU to other apps
|
2013-10-07 21:09:29 -03:00
|
|
|
_microsleep(1000);
|
2013-09-28 21:32:51 -03:00
|
|
|
if (_min_delay_cb_ms <= ms) {
|
|
|
|
if (_delay_cb) {
|
|
|
|
_delay_cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LinuxScheduler::millis()
|
|
|
|
{
|
2014-02-26 04:33:39 -04:00
|
|
|
if (stopped_clock_usec) {
|
|
|
|
return stopped_clock_usec/1000;
|
2013-12-31 05:05:07 -04:00
|
|
|
}
|
2013-10-06 06:57:24 -03:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return 1.0e3*((ts.tv_sec + (ts.tv_nsec*1.0e-9)) -
|
2013-09-22 03:01:24 -03:00
|
|
|
(_sketch_start_time.tv_sec +
|
2013-10-06 06:57:24 -03:00
|
|
|
(_sketch_start_time.tv_nsec*1.0e-9)));
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LinuxScheduler::micros()
|
|
|
|
{
|
2014-02-26 04:33:39 -04:00
|
|
|
if (stopped_clock_usec) {
|
|
|
|
return stopped_clock_usec;
|
2013-12-31 05:05:07 -04:00
|
|
|
}
|
2013-10-06 06:57:24 -03:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return 1.0e6*((ts.tv_sec + (ts.tv_nsec*1.0e-9)) -
|
2013-09-22 03:01:24 -03:00
|
|
|
(_sketch_start_time.tv_sec +
|
2013-10-06 06:57:24 -03:00
|
|
|
(_sketch_start_time.tv_nsec*1.0e-9)));
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxScheduler::delay_microseconds(uint16_t us)
|
|
|
|
{
|
2013-10-07 21:09:29 -03:00
|
|
|
_microsleep(us);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2013-09-28 01:59:00 -03:00
|
|
|
void LinuxScheduler::register_delay_callback(AP_HAL::Proc proc,
|
|
|
|
uint16_t min_time_ms)
|
|
|
|
{
|
|
|
|
_delay_cb = proc;
|
|
|
|
_min_delay_cb_ms = min_time_ms;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2013-09-30 07:36:12 -03:00
|
|
|
void LinuxScheduler::register_timer_process(AP_HAL::MemberProc proc)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_timer_procs; i++) {
|
|
|
|
if (_timer_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_timer_procs < LINUX_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
_timer_proc[_num_timer_procs] = proc;
|
|
|
|
_num_timer_procs++;
|
|
|
|
} else {
|
|
|
|
hal.console->printf("Out of timer processes\n");
|
|
|
|
}
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2013-09-30 07:36:12 -03:00
|
|
|
void LinuxScheduler::register_io_process(AP_HAL::MemberProc proc)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < _num_io_procs; i++) {
|
|
|
|
if (_io_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_io_procs < LINUX_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
_io_proc[_num_io_procs] = proc;
|
|
|
|
_num_io_procs++;
|
|
|
|
} else {
|
|
|
|
hal.console->printf("Out of IO processes\n");
|
|
|
|
}
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2013-09-30 23:09:46 -03:00
|
|
|
void LinuxScheduler::register_timer_failsafe(AP_HAL::Proc failsafe, uint32_t period_us)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
_failsafe = failsafe;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
void LinuxScheduler::suspend_timer_procs()
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
_timer_suspended = true;
|
2013-09-28 21:48:22 -03:00
|
|
|
while (_in_timer_proc) {
|
|
|
|
usleep(1);
|
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
void LinuxScheduler::resume_timer_procs()
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
_timer_suspended = false;
|
|
|
|
if (_timer_event_missed == true) {
|
|
|
|
_run_timers(false);
|
|
|
|
_timer_event_missed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxScheduler::_run_timers(bool called_from_timer_thread)
|
|
|
|
{
|
|
|
|
if (_in_timer_proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_in_timer_proc = true;
|
|
|
|
|
|
|
|
if (!_timer_suspended) {
|
|
|
|
// now call the timer based drivers
|
|
|
|
for (int i = 0; i < _num_timer_procs; i++) {
|
|
|
|
if (_timer_proc[i] != NULL) {
|
2013-09-30 23:09:46 -03:00
|
|
|
_timer_proc[i]();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (called_from_timer_thread) {
|
|
|
|
_timer_event_missed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and the failsafe, if one is setup
|
|
|
|
if (_failsafe != NULL) {
|
2013-09-30 23:09:46 -03:00
|
|
|
_failsafe();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
_in_timer_proc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *LinuxScheduler::_timer_thread(void)
|
|
|
|
{
|
2013-10-07 21:09:29 -03:00
|
|
|
_setup_realtime(32768);
|
2013-10-26 04:25:27 -03:00
|
|
|
while (system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
while (true) {
|
2014-05-21 07:09:57 -03:00
|
|
|
_microsleep(1000);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
// run registered timers
|
|
|
|
_run_timers(true);
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxScheduler::_run_io(void)
|
|
|
|
{
|
|
|
|
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++) {
|
|
|
|
if (_io_proc[i] != NULL) {
|
2013-09-30 23:09:46 -03:00
|
|
|
_io_proc[i]();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_in_io_proc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *LinuxScheduler::_uart_thread(void)
|
|
|
|
{
|
2013-10-07 21:09:29 -03:00
|
|
|
_setup_realtime(32768);
|
2013-10-26 04:25:27 -03:00
|
|
|
while (system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
while (true) {
|
2013-10-08 07:11:59 -03:00
|
|
|
_microsleep(10000);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
// process any pending serial bytes
|
|
|
|
((LinuxUARTDriver *)hal.uartA)->_timer_tick();
|
|
|
|
((LinuxUARTDriver *)hal.uartB)->_timer_tick();
|
|
|
|
((LinuxUARTDriver *)hal.uartC)->_timer_tick();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *LinuxScheduler::_io_thread(void)
|
|
|
|
{
|
2013-10-07 21:09:29 -03:00
|
|
|
_setup_realtime(32768);
|
2013-10-26 04:25:27 -03:00
|
|
|
while (system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
while (true) {
|
2013-10-08 07:11:59 -03:00
|
|
|
_microsleep(20000);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
// process any pending storage writes
|
|
|
|
((LinuxStorage *)hal.storage)->_timer_tick();
|
|
|
|
|
|
|
|
// run registered IO processes
|
|
|
|
_run_io();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxScheduler::panic(const prog_char_t *errormsg)
|
|
|
|
{
|
|
|
|
write(1, errormsg, strlen(errormsg));
|
|
|
|
write(1, "\n", 1);
|
|
|
|
hal.scheduler->delay_microseconds(10000);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxScheduler::in_timerprocess()
|
|
|
|
{
|
|
|
|
return _in_timer_proc;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxScheduler::begin_atomic()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void LinuxScheduler::end_atomic()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool LinuxScheduler::system_initializing() {
|
2013-09-28 01:59:00 -03:00
|
|
|
return !_initialized;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxScheduler::system_initialized()
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
if (_initialized) {
|
|
|
|
panic("PANIC: scheduler::system_initialized called more than once");
|
|
|
|
}
|
|
|
|
_initialized = true;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2013-09-28 01:59:00 -03:00
|
|
|
void LinuxScheduler::reboot(bool hold_in_bootloader)
|
|
|
|
{
|
2013-09-22 03:01:24 -03:00
|
|
|
for(;;);
|
|
|
|
}
|
2013-09-28 03:28:36 -03:00
|
|
|
|
2014-02-26 04:33:39 -04:00
|
|
|
void LinuxScheduler::stop_clock(uint64_t time_usec)
|
2013-12-29 18:31:33 -04:00
|
|
|
{
|
2014-02-26 04:33:39 -04:00
|
|
|
stopped_clock_usec = time_usec;
|
2013-12-31 05:05:07 -04:00
|
|
|
}
|
|
|
|
|
2013-09-28 03:28:36 -03:00
|
|
|
#endif // CONFIG_HAL_BOARD
|