2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-12 18:01:40 -04:00
|
|
|
#if (CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2)
|
2012-08-27 20:45:27 -03:00
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AP_HAL_AVR.h"
|
2012-08-27 20:45:27 -03:00
|
|
|
#include "RCInput.h"
|
|
|
|
#include "utility/ISRRegistry.h"
|
|
|
|
using namespace AP_HAL;
|
|
|
|
using namespace AP_HAL_AVR;
|
|
|
|
|
|
|
|
extern const HAL& hal;
|
|
|
|
|
|
|
|
/* private variables to communicate with input capture isr */
|
|
|
|
volatile uint16_t APM2RCInput::_pulse_capt[AVR_RC_INPUT_NUM_CHANNELS] = {0};
|
2014-03-25 00:39:41 -03:00
|
|
|
volatile uint8_t APM2RCInput::_num_channels = 0;
|
|
|
|
volatile bool APM2RCInput::_new_input = false;
|
2012-08-27 20:45:27 -03:00
|
|
|
|
|
|
|
/* private callback for input capture ISR */
|
|
|
|
void APM2RCInput::_timer5_capt_cb(void) {
|
|
|
|
static uint16_t icr5_prev;
|
|
|
|
static uint8_t channel_ctr;
|
|
|
|
|
|
|
|
const uint16_t icr5_current = ICR5;
|
|
|
|
uint16_t pulse_width;
|
|
|
|
if (icr5_current < icr5_prev) {
|
|
|
|
/* ICR5 rolls over at TOP=40000 */
|
|
|
|
pulse_width = icr5_current + 40000 - icr5_prev;
|
|
|
|
} else {
|
|
|
|
pulse_width = icr5_current - icr5_prev;
|
|
|
|
}
|
|
|
|
|
2015-02-08 02:09:17 -04:00
|
|
|
if (pulse_width > AVR_RC_INPUT_MIN_SYNC_PULSE_WIDTH*2) {
|
2013-04-29 02:35:59 -03:00
|
|
|
// sync pulse detected. Pass through values if at least a minimum number of channels received
|
|
|
|
if( channel_ctr >= AVR_RC_INPUT_MIN_CHANNELS ) {
|
2014-03-25 00:39:41 -03:00
|
|
|
_num_channels = channel_ctr;
|
|
|
|
_new_input = true;
|
2013-04-29 02:35:59 -03:00
|
|
|
}
|
2012-08-27 20:45:27 -03:00
|
|
|
channel_ctr = 0;
|
|
|
|
} else {
|
|
|
|
if (channel_ctr < AVR_RC_INPUT_NUM_CHANNELS) {
|
|
|
|
_pulse_capt[channel_ctr] = pulse_width;
|
|
|
|
channel_ctr++;
|
2012-08-27 22:28:09 -03:00
|
|
|
if (channel_ctr == AVR_RC_INPUT_NUM_CHANNELS) {
|
2014-03-25 00:39:41 -03:00
|
|
|
_num_channels = AVR_RC_INPUT_NUM_CHANNELS;
|
|
|
|
_new_input = true;
|
2012-08-27 22:28:09 -03:00
|
|
|
}
|
2012-08-27 20:45:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
icr5_prev = icr5_current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void APM2RCInput::init(void* _isrregistry) {
|
|
|
|
ISRRegistry* isrregistry = (ISRRegistry*) _isrregistry;
|
|
|
|
isrregistry->register_signal(ISR_REGISTRY_TIMER5_CAPT, _timer5_capt_cb);
|
|
|
|
|
2012-12-06 16:45:56 -04:00
|
|
|
/* initialize overrides */
|
|
|
|
clear_overrides();
|
2012-08-27 20:45:27 -03:00
|
|
|
/* Arduino pin 48 is ICP5 / PL1, timer 5 input capture */
|
2014-06-01 20:26:19 -03:00
|
|
|
hal.gpio->pinMode(48, HAL_GPIO_INPUT);
|
2012-08-27 20:45:27 -03:00
|
|
|
/**
|
|
|
|
* WGM: 1 1 1 1. Fast WPM, TOP is in OCR5A
|
|
|
|
* COM all disabled
|
|
|
|
* CS51: prescale by 8 => 0.5us tick
|
|
|
|
* ICES5: input capture on rising edge
|
|
|
|
* OCR5A: 40000, 0.5us tick => 2ms period / 50hz freq for outbound
|
|
|
|
* fast PWM.
|
|
|
|
*/
|
2013-01-30 15:00:36 -04:00
|
|
|
|
|
|
|
uint8_t oldSREG = SREG;
|
|
|
|
cli();
|
|
|
|
|
|
|
|
/* Timer cleanup before configuring */
|
|
|
|
TCNT5 = 0;
|
|
|
|
TIFR5 = 0;
|
|
|
|
|
|
|
|
/* Set timer 8x prescaler fast PWM mode toggle compare at OCRA with rising edge input capture */
|
2012-08-27 20:45:27 -03:00
|
|
|
TCCR5A = _BV(WGM50) | _BV(WGM51);
|
2013-01-30 15:00:36 -04:00
|
|
|
TCCR5B |= _BV(WGM53) | _BV(WGM52) | _BV(CS51) | _BV(ICES5);
|
|
|
|
OCR5A = 40000 - 1; // -1 to correct for wrap
|
2012-08-27 20:45:27 -03:00
|
|
|
|
2013-01-30 15:00:36 -04:00
|
|
|
/* OCR5B and OCR5C will be used by RCOutput_APM2. Init to 0xFFFF to prevent premature PWM output */
|
2012-08-27 20:45:27 -03:00
|
|
|
OCR5B = 0xFFFF;
|
|
|
|
OCR5C = 0xFFFF;
|
|
|
|
|
|
|
|
/* Enable input capture interrupt */
|
|
|
|
TIMSK5 |= _BV(ICIE5);
|
2013-01-30 15:00:36 -04:00
|
|
|
|
|
|
|
SREG = oldSREG;
|
2012-08-27 20:45:27 -03:00
|
|
|
}
|
|
|
|
|
2015-02-08 18:56:32 -04:00
|
|
|
bool APM2RCInput::new_input()
|
|
|
|
{
|
|
|
|
if (_new_input) {
|
|
|
|
_new_input = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 00:39:41 -03:00
|
|
|
uint8_t APM2RCInput::num_channels() { return _num_channels; }
|
2012-08-27 20:45:27 -03:00
|
|
|
|
2012-08-27 22:28:09 -03:00
|
|
|
/* constrain captured pulse to be between min and max pulsewidth. */
|
|
|
|
static inline uint16_t constrain_pulse(uint16_t p) {
|
|
|
|
if (p > RC_INPUT_MAX_PULSEWIDTH) return RC_INPUT_MAX_PULSEWIDTH;
|
|
|
|
if (p < RC_INPUT_MIN_PULSEWIDTH) return RC_INPUT_MIN_PULSEWIDTH;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-08-27 20:45:27 -03:00
|
|
|
|
|
|
|
uint16_t APM2RCInput::read(uint8_t ch) {
|
2012-12-06 16:45:56 -04:00
|
|
|
/* constrain ch */
|
2012-12-06 17:38:04 -04:00
|
|
|
if (ch >= AVR_RC_INPUT_NUM_CHANNELS) return 0;
|
2012-12-06 16:45:56 -04:00
|
|
|
/* grab channel from isr's memory in critical section*/
|
2013-01-30 15:00:36 -04:00
|
|
|
uint8_t oldSREG = SREG;
|
2012-08-27 20:45:27 -03:00
|
|
|
cli();
|
|
|
|
uint16_t capt = _pulse_capt[ch];
|
2013-01-30 15:00:36 -04:00
|
|
|
SREG = oldSREG;
|
2012-08-27 22:28:09 -03:00
|
|
|
/* scale _pulse_capt from 0.5us units to 1us units. */
|
2012-12-06 16:45:56 -04:00
|
|
|
uint16_t pulse = constrain_pulse(capt >> 1);
|
|
|
|
/* Check for override */
|
|
|
|
uint16_t over = _override[ch];
|
|
|
|
return (over == 0) ? pulse : over;
|
2012-08-27 20:45:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t APM2RCInput::read(uint16_t* periods, uint8_t len) {
|
2012-12-06 16:45:56 -04:00
|
|
|
/* constrain len */
|
|
|
|
if (len > AVR_RC_INPUT_NUM_CHANNELS) { len = AVR_RC_INPUT_NUM_CHANNELS; }
|
|
|
|
/* grab channels from isr's memory in critical section */
|
2013-01-30 15:00:36 -04:00
|
|
|
uint8_t oldSREG = SREG;
|
2012-08-27 20:45:27 -03:00
|
|
|
cli();
|
|
|
|
for (int i = 0; i < len; i++) {
|
2012-08-27 22:28:09 -03:00
|
|
|
periods[i] = _pulse_capt[i];
|
2012-08-27 20:45:27 -03:00
|
|
|
}
|
2013-01-30 15:00:36 -04:00
|
|
|
SREG = oldSREG;
|
2012-08-27 22:28:09 -03:00
|
|
|
/* Outside of critical section, do the math (in place) to scale and
|
|
|
|
* constrain the pulse. */
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
/* scale _pulse_capt from 0.5us units to 1us units. */
|
|
|
|
periods[i] = constrain_pulse(periods[i] >> 1);
|
2012-12-06 16:45:56 -04:00
|
|
|
/* check for override */
|
|
|
|
if (_override[i] != 0) {
|
|
|
|
periods[i] = _override[i];
|
|
|
|
}
|
2012-08-27 22:28:09 -03:00
|
|
|
}
|
2014-03-25 00:39:41 -03:00
|
|
|
return _num_channels;
|
2012-08-27 20:45:27 -03:00
|
|
|
}
|
|
|
|
|
2012-12-06 16:45:56 -04:00
|
|
|
bool APM2RCInput::set_overrides(int16_t *overrides, uint8_t len) {
|
|
|
|
bool res = false;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
res |= set_override(i, overrides[i]);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool APM2RCInput::set_override(uint8_t channel, int16_t override) {
|
|
|
|
if (override < 0) return false; /* -1: no change. */
|
|
|
|
if (channel < AVR_RC_INPUT_NUM_CHANNELS) {
|
|
|
|
_override[channel] = override;
|
|
|
|
if (override != 0) {
|
2014-03-25 00:39:41 -03:00
|
|
|
_new_input = true;
|
2012-12-06 16:45:56 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void APM2RCInput::clear_overrides() {
|
|
|
|
for (int i = 0; i < AVR_RC_INPUT_NUM_CHANNELS; i++) {
|
|
|
|
_override[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-12-12 18:01:40 -04:00
|
|
|
|
|
|
|
#endif
|