2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.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 "Scheduler.h"
|
2013-09-28 01:59:00 -03:00
|
|
|
#include "Storage.h"
|
2014-08-14 13:48:17 -03:00
|
|
|
#include "RCInput.h"
|
2013-09-28 21:13:51 -03:00
|
|
|
#include "UARTDriver.h"
|
2014-08-23 04:52:43 -03:00
|
|
|
#include "Util.h"
|
2014-11-07 06:17:50 -04:00
|
|
|
#include "SPIUARTDriver.h"
|
2015-08-17 23:39:02 -03:00
|
|
|
#include "RPIOUARTDriver.h"
|
2013-09-22 03:01:24 -03:00
|
|
|
#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;
|
|
|
|
|
2014-08-23 04:52:43 -03:00
|
|
|
#define APM_LINUX_TIMER_PRIORITY 15
|
|
|
|
#define APM_LINUX_UART_PRIORITY 14
|
|
|
|
#define APM_LINUX_RCIN_PRIORITY 13
|
|
|
|
#define APM_LINUX_MAIN_PRIORITY 12
|
|
|
|
#define APM_LINUX_TONEALARM_PRIORITY 11
|
|
|
|
#define APM_LINUX_IO_PRIORITY 10
|
2013-09-28 21:16:07 -03:00
|
|
|
|
2015-11-01 07:24:27 -04:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2
|
2015-04-16 12:19:19 -03:00
|
|
|
#define APM_LINUX_UART_PERIOD 10000
|
2015-08-17 23:39:02 -03:00
|
|
|
#define APM_LINUX_RCIN_PERIOD 500
|
2015-04-16 12:19:19 -03:00
|
|
|
#define APM_LINUX_TONEALARM_PERIOD 10000
|
|
|
|
#define APM_LINUX_IO_PERIOD 20000
|
|
|
|
#else
|
|
|
|
#define APM_LINUX_UART_PERIOD 10000
|
|
|
|
#define APM_LINUX_RCIN_PERIOD 10000
|
|
|
|
#define APM_LINUX_TONEALARM_PERIOD 10000
|
|
|
|
#define APM_LINUX_IO_PERIOD 20000
|
2015-11-01 07:24:27 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2
|
2015-04-16 12:19:19 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler::Scheduler()
|
2013-09-22 03:01:24 -03:00
|
|
|
{}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::_create_realtime_thread(pthread_t *ctx, int rtprio,
|
2015-03-18 17:59:29 -03:00
|
|
|
const char *name,
|
2015-03-18 16:50:44 -03:00
|
|
|
pthread_startroutine_t start_routine)
|
|
|
|
{
|
|
|
|
struct sched_param param = { .sched_priority = rtprio };
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
2015-04-13 20:25:35 -03:00
|
|
|
/*
|
|
|
|
we need to run as root to get realtime scheduling. Allow it to
|
|
|
|
run as non-root for debugging purposes, plus to allow the Replay
|
|
|
|
tool to run
|
|
|
|
*/
|
|
|
|
if (geteuid() == 0) {
|
|
|
|
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
|
|
|
|
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
|
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
|
|
|
}
|
2015-03-18 16:50:44 -03:00
|
|
|
r = pthread_create(ctx, &attr, start_routine, this);
|
|
|
|
if (r != 0) {
|
2015-03-18 17:59:29 -03:00
|
|
|
hal.console->printf("Error creating thread '%s': %s\n",
|
|
|
|
name, strerror(r));
|
2015-10-24 18:45:41 -03:00
|
|
|
panic("Failed to create thread");
|
2015-03-18 16:50:44 -03:00
|
|
|
}
|
|
|
|
pthread_attr_destroy(&attr);
|
2015-03-18 18:00:24 -03:00
|
|
|
|
|
|
|
if (name) {
|
|
|
|
pthread_setname_np(*ctx, name);
|
|
|
|
}
|
2015-03-18 16:50:44 -03:00
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::init(void* machtnichts)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2015-03-18 16:36:44 -03:00
|
|
|
mlockall(MCL_CURRENT|MCL_FUTURE);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2015-03-18 16:36:44 -03:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &_sketch_start_time);
|
2013-10-07 21:09:29 -03:00
|
|
|
|
2015-03-18 16:50:44 -03:00
|
|
|
struct sched_param param = { .sched_priority = APM_LINUX_MAIN_PRIORITY };
|
2013-09-28 21:16:07 -03:00
|
|
|
sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|
|
|
|
2015-03-18 17:59:29 -03:00
|
|
|
struct {
|
|
|
|
pthread_t *ctx;
|
|
|
|
int rtprio;
|
|
|
|
const char *name;
|
|
|
|
pthread_startroutine_t start_routine;
|
|
|
|
} *iter, table[] = {
|
|
|
|
{ .ctx = &_timer_thread_ctx,
|
|
|
|
.rtprio = APM_LINUX_TIMER_PRIORITY,
|
|
|
|
.name = "sched-timer",
|
2015-10-20 18:13:25 -03:00
|
|
|
.start_routine = &Linux::Scheduler::_timer_thread,
|
2015-03-18 17:59:29 -03:00
|
|
|
},
|
|
|
|
{ .ctx = &_uart_thread_ctx,
|
|
|
|
.rtprio = APM_LINUX_UART_PRIORITY,
|
|
|
|
.name = "sched-uart",
|
2015-10-20 18:13:25 -03:00
|
|
|
.start_routine = &Linux::Scheduler::_uart_thread,
|
2015-03-18 17:59:29 -03:00
|
|
|
},
|
|
|
|
{ .ctx = &_rcin_thread_ctx,
|
|
|
|
.rtprio = APM_LINUX_RCIN_PRIORITY,
|
|
|
|
.name = "sched-rcin",
|
2015-10-20 18:13:25 -03:00
|
|
|
.start_routine = &Linux::Scheduler::_rcin_thread,
|
2015-03-18 17:59:29 -03:00
|
|
|
},
|
|
|
|
{ .ctx = &_tonealarm_thread_ctx,
|
|
|
|
.rtprio = APM_LINUX_TONEALARM_PRIORITY,
|
|
|
|
.name = "sched-tonealarm",
|
2015-10-20 18:13:25 -03:00
|
|
|
.start_routine = &Linux::Scheduler::_tonealarm_thread,
|
2015-03-18 17:59:29 -03:00
|
|
|
},
|
|
|
|
{ .ctx = &_io_thread_ctx,
|
|
|
|
.rtprio = APM_LINUX_IO_PRIORITY,
|
|
|
|
.name = "sched-io",
|
2015-10-20 18:13:25 -03:00
|
|
|
.start_routine = &Linux::Scheduler::_io_thread,
|
2015-03-18 17:59:29 -03:00
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2015-04-13 20:25:35 -03:00
|
|
|
if (geteuid() != 0) {
|
|
|
|
printf("WARNING: running as non-root. Will not use realtime scheduling\n");
|
|
|
|
}
|
|
|
|
|
2015-03-18 17:59:29 -03:00
|
|
|
for (iter = table; iter->ctx; iter++)
|
|
|
|
_create_realtime_thread(iter->ctx, iter->rtprio, iter->name,
|
|
|
|
iter->start_routine);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::_microsleep(uint32_t usec)
|
2013-10-07 21:09:29 -03:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = usec*1000UL;
|
|
|
|
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) ;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::delay(uint16_t ms)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2014-02-26 04:33:39 -04:00
|
|
|
if (stopped_clock_usec) {
|
2014-01-04 20:39:29 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-08-19 19:00:21 -03:00
|
|
|
uint64_t start = millis64();
|
2015-08-17 23:39:02 -03:00
|
|
|
|
2014-08-19 19:00:21 -03:00
|
|
|
while ((millis64() - start) < ms) {
|
2013-09-28 21:32:51 -03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint64_t Scheduler::millis64()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
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);
|
2015-08-17 23:39:02 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint64_t Scheduler::micros64()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
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);
|
2015-08-17 23:39:02 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint32_t Scheduler::millis()
|
2014-08-19 19:00:21 -03:00
|
|
|
{
|
|
|
|
return millis64() & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint32_t Scheduler::micros()
|
2014-08-19 19:00:21 -03:00
|
|
|
{
|
|
|
|
return micros64() & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::delay_microseconds(uint16_t us)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2014-11-15 21:30:50 -04:00
|
|
|
if (stopped_clock_usec) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-07 21:09:29 -03:00
|
|
|
_microsleep(us);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::register_delay_callback(AP_HAL::Proc proc,
|
2013-09-28 01:59:00 -03:00
|
|
|
uint16_t min_time_ms)
|
|
|
|
{
|
|
|
|
_delay_cb = proc;
|
|
|
|
_min_delay_cb_ms = min_time_ms;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::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
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 02:45:17 -04:00
|
|
|
if (_num_io_procs < LINUX_SCHEDULER_MAX_IO_PROCS) {
|
2013-09-28 01:59:00 -03:00
|
|
|
_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
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::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
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::suspend_timer_procs()
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2014-08-20 18:49:49 -03:00
|
|
|
if (!_timer_semaphore.take(0)) {
|
|
|
|
printf("Failed to take timer semaphore\n");
|
2013-09-28 21:48:22 -03:00
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::resume_timer_procs()
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2014-08-20 18:49:49 -03:00
|
|
|
_timer_semaphore.give();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::_run_timers(bool called_from_timer_thread)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
if (_in_timer_proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_in_timer_proc = true;
|
|
|
|
|
2014-08-20 18:49:49 -03:00
|
|
|
if (!_timer_semaphore.take(0)) {
|
|
|
|
printf("Failed to take timer semaphore in _run_timers\n");
|
|
|
|
}
|
|
|
|
// now call the timer based drivers
|
|
|
|
for (int i = 0; i < _num_timer_procs; i++) {
|
2015-05-26 02:14:50 -03:00
|
|
|
if (_timer_proc[i]) {
|
2014-08-20 18:49:49 -03:00
|
|
|
_timer_proc[i]();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
}
|
2015-08-17 23:39:02 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
|
|
|
//SPI UART use SPI
|
2015-10-20 18:13:25 -03:00
|
|
|
if (!((RPIOUARTDriver *)hal.uartC)->isExternal() )
|
2015-08-17 23:39:02 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
((RPIOUARTDriver *)hal.uartC)->_timer_tick();
|
2015-08-17 23:39:02 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-20 18:49:49 -03:00
|
|
|
_timer_semaphore.give();
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void *Scheduler::_timer_thread(void* arg)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler* sched = (Scheduler *)arg;
|
2015-03-18 17:23:37 -03:00
|
|
|
|
|
|
|
while (sched->system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
2013-10-26 04:25:27 -03:00
|
|
|
}
|
2014-08-19 07:03:15 -03:00
|
|
|
/*
|
|
|
|
this aims to run at an average of 1kHz, so that it can be used
|
|
|
|
to drive 1kHz processes without drift
|
|
|
|
*/
|
2015-03-18 17:23:37 -03:00
|
|
|
uint64_t next_run_usec = sched->micros64() + 1000;
|
2013-09-28 01:59:00 -03:00
|
|
|
while (true) {
|
2015-03-18 17:23:37 -03:00
|
|
|
uint64_t dt = next_run_usec - sched->micros64();
|
2014-08-19 07:03:15 -03:00
|
|
|
if (dt > 2000) {
|
|
|
|
// we've lost sync - restart
|
2015-03-18 17:23:37 -03:00
|
|
|
next_run_usec = sched->micros64();
|
2014-08-19 07:03:15 -03:00
|
|
|
} else {
|
2015-03-18 17:23:37 -03:00
|
|
|
sched->_microsleep(dt);
|
2014-08-19 07:03:15 -03:00
|
|
|
}
|
|
|
|
next_run_usec += 1000;
|
2013-09-28 01:59:00 -03:00
|
|
|
// run registered timers
|
2015-03-18 17:23:37 -03:00
|
|
|
sched->_run_timers(true);
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::_run_io(void)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2014-12-07 20:24:39 -04:00
|
|
|
if (!_io_semaphore.take(0)) {
|
2013-09-28 01:59:00 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-20 18:49:49 -03:00
|
|
|
// now call the IO based drivers
|
|
|
|
for (int i = 0; i < _num_io_procs; i++) {
|
2015-05-26 02:14:50 -03:00
|
|
|
if (_io_proc[i]) {
|
2014-08-20 18:49:49 -03:00
|
|
|
_io_proc[i]();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:24:39 -04:00
|
|
|
_io_semaphore.give();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void *Scheduler::_rcin_thread(void *arg)
|
2014-08-14 13:48:17 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler* sched = (Scheduler *)arg;
|
2015-03-18 17:23:37 -03:00
|
|
|
|
|
|
|
while (sched->system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
2014-08-14 13:48:17 -03:00
|
|
|
}
|
|
|
|
while (true) {
|
2015-04-16 12:19:19 -03:00
|
|
|
sched->_microsleep(APM_LINUX_RCIN_PERIOD);
|
2015-10-20 18:13:25 -03:00
|
|
|
RCInput::from(hal.rcin)->_timer_tick();
|
2014-08-14 13:48:17 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void *Scheduler::_uart_thread(void* arg)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler* sched = (Scheduler *)arg;
|
2015-03-18 17:23:37 -03:00
|
|
|
|
|
|
|
while (sched->system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
2013-10-26 04:25:27 -03:00
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
while (true) {
|
2015-04-16 12:19:19 -03:00
|
|
|
sched->_microsleep(APM_LINUX_UART_PERIOD);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
// process any pending serial bytes
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::from(hal.uartA)->_timer_tick();
|
|
|
|
UARTDriver::from(hal.uartB)->_timer_tick();
|
2015-08-17 23:39:02 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
|
|
|
//SPI UART not use SPI
|
2015-10-20 18:13:25 -03:00
|
|
|
if (RPIOUARTDriver::from(hal.uartC)->isExternal()) {
|
|
|
|
RPIOUARTDriver::from(hal.uartC)->_timer_tick();
|
2015-08-17 23:39:02 -03:00
|
|
|
}
|
|
|
|
#else
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::from(hal.uartC)->_timer_tick();
|
2015-08-17 23:39:02 -03:00
|
|
|
#endif
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::from(hal.uartE)->_timer_tick();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void *Scheduler::_tonealarm_thread(void* arg)
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler* sched = (Scheduler *)arg;
|
2015-03-18 17:23:37 -03:00
|
|
|
|
|
|
|
while (sched->system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
while (true) {
|
2015-04-16 12:19:19 -03:00
|
|
|
sched->_microsleep(APM_LINUX_TONEALARM_PERIOD);
|
2014-08-23 04:52:43 -03:00
|
|
|
|
|
|
|
// process tone command
|
2015-10-20 18:13:25 -03:00
|
|
|
Util::from(hal.util)->_toneAlarm_timer_tick();
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void *Scheduler::_io_thread(void* arg)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
Scheduler* sched = (Scheduler *)arg;
|
2015-03-18 17:23:37 -03:00
|
|
|
|
|
|
|
while (sched->system_initializing()) {
|
|
|
|
poll(NULL, 0, 1);
|
2013-10-26 04:25:27 -03:00
|
|
|
}
|
2013-09-28 01:59:00 -03:00
|
|
|
while (true) {
|
2015-04-16 12:19:19 -03:00
|
|
|
sched->_microsleep(APM_LINUX_IO_PERIOD);
|
2013-09-28 01:59:00 -03:00
|
|
|
|
|
|
|
// process any pending storage writes
|
2015-10-20 18:13:25 -03:00
|
|
|
Storage::from(hal.storage)->_timer_tick();
|
2013-09-28 01:59:00 -03:00
|
|
|
|
2015-04-16 12:19:19 -03:00
|
|
|
// run registered IO procepsses
|
2015-03-18 17:23:37 -03:00
|
|
|
sched->_run_io();
|
2013-09-28 01:59:00 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-26 08:25:44 -03:00
|
|
|
void Scheduler::panic(const char *errormsg, ...)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2015-10-22 14:15:05 -03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, errormsg);
|
|
|
|
vdprintf(1, errormsg, ap);
|
|
|
|
va_end(ap);
|
2013-09-28 01:59:00 -03:00
|
|
|
write(1, "\n", 1);
|
2015-10-22 14:15:05 -03:00
|
|
|
|
2015-07-06 19:50:56 -03:00
|
|
|
hal.rcin->deinit();
|
2013-09-28 01:59:00 -03:00
|
|
|
hal.scheduler->delay_microseconds(10000);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool Scheduler::in_timerprocess()
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
|
|
|
return _in_timer_proc;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::begin_atomic()
|
2013-09-22 03:01:24 -03:00
|
|
|
{}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::end_atomic()
|
2013-09-22 03:01:24 -03:00
|
|
|
{}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool Scheduler::system_initializing() {
|
2013-09-28 01:59:00 -03:00
|
|
|
return !_initialized;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::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
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::reboot(bool hold_in_bootloader)
|
2013-09-28 01:59:00 -03:00
|
|
|
{
|
2014-10-19 22:40:08 -03:00
|
|
|
exit(1);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2013-09-28 03:28:36 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Scheduler::stop_clock(uint64_t time_usec)
|
2013-12-29 18:31:33 -04:00
|
|
|
{
|
2015-04-20 01:04:23 -03:00
|
|
|
if (time_usec >= stopped_clock_usec) {
|
|
|
|
stopped_clock_usec = time_usec;
|
|
|
|
_run_io();
|
|
|
|
}
|
2013-12-31 05:05:07 -04:00
|
|
|
}
|
|
|
|
|
2013-09-28 03:28:36 -03:00
|
|
|
#endif // CONFIG_HAL_BOARD
|