2013-09-22 23:53:48 -03:00
|
|
|
// Scheduler.cpp
|
|
|
|
//
|
|
|
|
// Flymaple Scheduler.
|
|
|
|
// We use systick interrupt for the 1kHz ordinary timers.
|
|
|
|
// We use a slightly higher priority HardwareTimer 2 for the failsafe callbacks
|
2016-05-12 14:04:28 -03:00
|
|
|
// so a hung timer won't prevent the failsafe timer interrupt running
|
2013-09-24 08:55:30 -03:00
|
|
|
//
|
|
|
|
// Use of noInterrupts()/interrupts() on FLymaple ARM processor.
|
|
|
|
// Please see the notes in FlymaplePortingNotes.txt in this directory for
|
|
|
|
// information about disabling interrupts on Flymaple
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-09-24 08:55:30 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_FLYMAPLE
|
2013-09-22 23:53:48 -03:00
|
|
|
|
|
|
|
#include "Scheduler.h"
|
|
|
|
|
2015-10-22 14:15:05 -03:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2013-09-22 23:53:48 -03:00
|
|
|
#include "FlymapleWirish.h"
|
|
|
|
|
2013-10-06 22:56:23 -03:00
|
|
|
// Flymaple: Force init to be called *first*, i.e. before static object allocation.
|
|
|
|
// Otherwise, statically allocated objects (eg SerialUSB) that need libmaple may fail.
|
|
|
|
__attribute__((constructor)) void premain() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2013-09-22 23:53:48 -03:00
|
|
|
// Not declared in any libmaple headers :-(
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
void systick_attach_callback(void (*callback)(void));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use Maple hardware timer for 1khz failsafe timer
|
|
|
|
// Caution, this must agree with the interrupt number passed to
|
|
|
|
// nvic_irq_set_priority
|
|
|
|
static HardwareTimer _failsafe_timer(2);
|
|
|
|
|
|
|
|
using namespace AP_HAL_FLYMAPLE_NS;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-09-30 04:00:47 -03:00
|
|
|
AP_HAL::Proc FLYMAPLEScheduler::_failsafe = NULL;
|
2013-09-22 23:53:48 -03:00
|
|
|
volatile bool FLYMAPLEScheduler::_timer_suspended = false;
|
|
|
|
volatile bool FLYMAPLEScheduler::_timer_event_missed = false;
|
|
|
|
volatile bool FLYMAPLEScheduler::_in_timer_proc = false;
|
2013-09-30 04:00:47 -03:00
|
|
|
AP_HAL::MemberProc FLYMAPLEScheduler::_timer_proc[FLYMAPLE_SCHEDULER_MAX_TIMER_PROCS] = {NULL};
|
2013-09-22 23:53:48 -03:00
|
|
|
uint8_t FLYMAPLEScheduler::_num_timer_procs = 0;
|
|
|
|
|
|
|
|
FLYMAPLEScheduler::FLYMAPLEScheduler() :
|
|
|
|
_delay_cb(NULL),
|
|
|
|
_min_delay_cb_ms(65535),
|
|
|
|
_initialized(false)
|
|
|
|
{}
|
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
void FLYMAPLEScheduler::init()
|
2013-09-22 23:53:48 -03:00
|
|
|
{
|
|
|
|
delay_us(2000000); // Wait for startup so we have time to connect a new USB console
|
|
|
|
// 1kHz interrupts from systick for normal timers
|
|
|
|
systick_attach_callback(_timer_procs_timer_event);
|
|
|
|
|
|
|
|
// Set up Maple hardware timer for 1khz failsafe timer
|
|
|
|
// ref: http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer
|
|
|
|
_failsafe_timer.pause();
|
|
|
|
_failsafe_timer.setPeriod(1000); // 1000us = 1kHz
|
|
|
|
_failsafe_timer.setChannelMode(TIMER_CH1, TIMER_OUTPUT_COMPARE);// Set up an interrupt on channel 1
|
|
|
|
_failsafe_timer.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
|
|
|
|
_failsafe_timer.attachInterrupt(TIMER_CH1, _failsafe_timer_event);
|
|
|
|
_failsafe_timer.refresh();// Refresh the timer's count, prescale, and overflow
|
|
|
|
_failsafe_timer.resume(); // Start the timer counting
|
|
|
|
// We run this timer at a higher priority, so that a broken timer handler (ie one that hangs)
|
|
|
|
// will not prevent the failsafe timer interrupt.
|
|
|
|
// Caution: the timer number must agree with the HardwareTimer number
|
|
|
|
nvic_irq_set_priority(NVIC_TIMER2, 0x14);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function may calls the _delay_cb to use up time
|
|
|
|
void FLYMAPLEScheduler::delay(uint16_t ms)
|
|
|
|
{
|
2015-11-11 10:22:49 -04:00
|
|
|
uint32_t start = AP_HAL::micros();
|
2013-09-22 23:53:48 -03:00
|
|
|
|
|
|
|
while (ms > 0) {
|
2015-11-11 10:22:49 -04:00
|
|
|
while ((AP_HAL::micros() - start) >= 1000) {
|
2013-09-22 23:53:48 -03:00
|
|
|
ms--;
|
|
|
|
if (ms == 0) break;
|
|
|
|
start += 1000;
|
|
|
|
}
|
|
|
|
if (_min_delay_cb_ms <= ms) {
|
|
|
|
if (_delay_cb) {
|
|
|
|
_delay_cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::delay_microseconds(uint16_t us)
|
|
|
|
{
|
|
|
|
delay_us(us);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::register_delay_callback(AP_HAL::Proc proc, uint16_t min_time_ms)
|
|
|
|
{
|
|
|
|
_delay_cb = proc;
|
|
|
|
_min_delay_cb_ms = min_time_ms;
|
|
|
|
}
|
|
|
|
|
2013-09-30 04:00:47 -03:00
|
|
|
void FLYMAPLEScheduler::register_timer_process(AP_HAL::MemberProc proc)
|
2013-09-22 23:53:48 -03:00
|
|
|
{
|
|
|
|
for (int i = 0; i < _num_timer_procs; i++) {
|
|
|
|
if (_timer_proc[i] == proc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_num_timer_procs < FLYMAPLE_SCHEDULER_MAX_TIMER_PROCS) {
|
|
|
|
/* this write to _timer_proc can be outside the critical section
|
|
|
|
* because that memory won't be used until _num_timer_procs is
|
|
|
|
* incremented. */
|
|
|
|
_timer_proc[_num_timer_procs] = proc;
|
|
|
|
/* _num_timer_procs is used from interrupt, and multiple bytes long. */
|
|
|
|
noInterrupts();
|
|
|
|
_num_timer_procs++;
|
|
|
|
interrupts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 04:00:47 -03:00
|
|
|
void FLYMAPLEScheduler::register_io_process(AP_HAL::MemberProc k)
|
2013-09-22 23:53:48 -03:00
|
|
|
{
|
|
|
|
// IO processes not supported on FLYMAPLE
|
|
|
|
}
|
|
|
|
|
2013-09-30 04:00:47 -03:00
|
|
|
void FLYMAPLEScheduler::register_timer_failsafe(AP_HAL::Proc failsafe, uint32_t period_us)
|
2013-09-22 23:53:48 -03:00
|
|
|
{
|
|
|
|
/* XXX Assert period_us == 1000 */
|
|
|
|
_failsafe = failsafe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::suspend_timer_procs()
|
|
|
|
{
|
|
|
|
_timer_suspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::resume_timer_procs()
|
|
|
|
{
|
|
|
|
_timer_suspended = false;
|
|
|
|
if (_timer_event_missed == true) {
|
|
|
|
_run_timer_procs(false);
|
|
|
|
_timer_event_missed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FLYMAPLEScheduler::in_timerprocess() {
|
|
|
|
return _in_timer_proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::_timer_procs_timer_event() {
|
|
|
|
_run_timer_procs(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by HardwareTimer when a failsafe timer event occurs
|
|
|
|
void FLYMAPLEScheduler::_failsafe_timer_event()
|
|
|
|
{
|
|
|
|
// run the failsafe, if one is setup
|
|
|
|
if (_failsafe != NULL)
|
2013-09-30 04:00:47 -03:00
|
|
|
_failsafe();
|
2013-09-22 23:53:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::_run_timer_procs(bool called_from_isr)
|
|
|
|
{
|
|
|
|
_in_timer_proc = true;
|
|
|
|
|
|
|
|
if (!_timer_suspended) {
|
|
|
|
// now call the timer based drivers
|
|
|
|
for (int i = 0; i < _num_timer_procs; i++) {
|
2015-05-26 02:16:51 -03:00
|
|
|
if (_timer_proc[i]) {
|
2013-09-30 04:00:47 -03:00
|
|
|
_timer_proc[i]();
|
2013-09-22 23:53:48 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (called_from_isr) {
|
|
|
|
_timer_event_missed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_in_timer_proc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::system_initialized()
|
|
|
|
{
|
|
|
|
if (_initialized) {
|
2015-11-11 10:22:49 -04:00
|
|
|
AP_HAL::panic("PANIC: scheduler::system_initialized called"
|
2015-10-24 18:45:41 -03:00
|
|
|
"more than once");
|
2013-09-22 23:53:48 -03:00
|
|
|
}
|
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLYMAPLEScheduler::reboot(bool hold_in_bootloader) {
|
2015-10-25 16:50:51 -03:00
|
|
|
hal.uartA->println("GOING DOWN FOR A REBOOT\r\n");
|
2013-09-22 23:53:48 -03:00
|
|
|
hal.scheduler->delay(100);
|
|
|
|
nvic_sys_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|