MEGA2560 the first PPM pulse in the frame longer. #2938 is missing in next

This commit is contained in:
Ingwie
2016-05-26 12:34:28 +02:00
parent 116802c946
commit d1d3f7fa0a
+51 -58
View File
@@ -19,7 +19,6 @@
*/ */
#include "../opentx.h" #include "../opentx.h"
uint16_t nextMixerEndTime = 0; uint16_t nextMixerEndTime = 0;
#define SCHEDULE_MIXER_END(delay) nextMixerEndTime = getTmr16KHz() + (delay) - 2*16/*2ms*/ #define SCHEDULE_MIXER_END(delay) nextMixerEndTime = getTmr16KHz() + (delay) - 2*16/*2ms*/
@@ -94,28 +93,39 @@ uint8_t *pulses2MHzWPtr = pulses2MHz;
#define CTRL_REP_1CMD -3 #define CTRL_REP_1CMD -3
#define CTRL_REP_2CMD -6 #define CTRL_REP_2CMD -6
#define SETUP_PULSES_DURATION 1000/*500us*/ #define SETUP_PULSES_DURATION 1000 // 500us
uint8_t g_ppmPulsePolarity = 0; // Needed for Bit-bang PPM.
// TIMER1_COMPA_vect used for PPM and DSM2=SERIAL // TIMER1_COMPA_vect used for PPM and DSM2=SERIAL.
uint8_t g_ppmPulsePolarity = 0; ISR(TIMER1_COMPA_vect) // 2MHz pulse generation (BLOCKING ISR).
ISR(TIMER1_COMPA_vect) //2MHz pulse generation (BLOCKING ISR)
{ {
uint8_t dt = TCNT1L; // record Timer1 latency for DEBUG stats display uint8_t dt = TCNT1L; // Record Timer1 latency for DEBUG stats display.
// Call setupPulses only after REST pulse had been sent. // Call setupPulses only after "rest" period has elapsed.
// Must do this before toggle PORTB to keep timing accurate // Must do this before toggle PORTB to keep timing accurate.
if (IS_DSM2_SERIAL_PROTOCOL(s_current_protocol[0]) || *((uint16_t*)pulses2MHzRPtr) == 0) { if (IS_DSM2_SERIAL_PROTOCOL(s_current_protocol[0]) || *((uint16_t*)pulses2MHzRPtr) == 0) {
if (!IS_DSM2_SERIAL_PROTOCOL(s_current_protocol[0])) { if (!IS_DSM2_SERIAL_PROTOCOL(s_current_protocol[0])) {
OCR1A = SETUP_PULSES_DURATION; OCR1A = SETUP_PULSES_DURATION;
#if defined(CPUM2560) // CPUM2560 hardware toggled PPM out.
OCR1B = OCR1A;
if (g_model.pulsePol) {
TCCR1A = (TCCR1A | (1<<COM1B1)) & ~(1<<COM1B0); // Set idle level.
} }
setupPulses(); // does not sei() for setupPulsesPPM else {
TCCR1A |= 3<<COM1B0;
}
TCCR1C = 1<<FOC1B; // Strobe FOC1B.
TCCR1A = (TCCR1A | (1<<COM1B0)) & ~(1<<COM1B1); // Toggle OC1B on next match.
#endif
}
setupPulses(); // Does not sei() for setupPulsesPPM.
heartbeat |= HEART_TIMER_PULSES; heartbeat |= HEART_TIMER_PULSES;
return; return;
} }
#if !defined(CPUM2560)
// Original bitbang for PPM
if (s_current_protocol[0] != PROTO_NONE) { if (s_current_protocol[0] != PROTO_NONE) {
#if !defined(CPUM2560)
// Original Bit-bang for PPM.
if (g_ppmPulsePolarity) { if (g_ppmPulsePolarity) {
PORTB |= (1<<OUT_B_PPM); // GCC optimisation should result in a single SBI instruction PORTB |= (1<<OUT_B_PPM); // GCC optimisation should result in a single SBI instruction
g_ppmPulsePolarity = 0; g_ppmPulsePolarity = 0;
@@ -124,27 +134,20 @@ ISR(TIMER1_COMPA_vect) //2MHz pulse generation (BLOCKING ISR)
PORTB &= ~(1<<OUT_B_PPM); PORTB &= ~(1<<OUT_B_PPM);
g_ppmPulsePolarity = 1; g_ppmPulsePolarity = 1;
} }
}
#else // defined(CPUM2560) #else // defined(CPUM2560)
// CPUM2560 zero jitter hardware toggled PPM_out // CPUM2560 hardware toggled PPM out.
OCR1B = *((uint16_t*)pulses2MHzRPtr); // duplicate capture (Timer1 in CTC mode, so restricted to OCR1A for int vector) if (*(uint16_t*)(pulses2MHzRPtr + sizeof(uint16_t)) == 0) {
// Look one step ahead to see if we are currently the "rest" period.
// Toggle bit: Can't read PPM_OUT I/O pin when OC1B is connected (on the ATmega2560 -- can on ATmega64A!) OCR1B = 0xffff; // Prevent next compare match hence toggle.
// so need to use pusePol register to keep track of PPM_out polarity.
if (s_current_protocol[0] != PROTO_NONE) {
if (g_ppmPulsePolarity) {
TCCR1A = (3<<COM1B0); // SET the state of PB6(OC1B)/PPM_out on next TCNT1==OCR1B
g_ppmPulsePolarity = 0;
} }
else { else {
TCCR1A = (2<<COM1B0); // CLEAR the state of PB6(OC1B)/PPM_out on next TCNT1==OCR1B OCR1B = *((uint16_t*) pulses2MHzRPtr);
g_ppmPulsePolarity = 1;
}
} }
#endif #endif
}
OCR1A = *((uint16_t*)pulses2MHzRPtr); // Schedule next Timer1 interrupt vector (to this function) OCR1A = *((uint16_t*) pulses2MHzRPtr); // Schedule next Timer1 interrupt vector (to this function).
pulses2MHzRPtr += sizeof(uint16_t); // non PPM protocols use uint8_t pulse buffer pulses2MHzRPtr += sizeof(uint16_t); // Non PPM protocols use uint8_t pulse buffer.
if (dt > g_tmr1Latency_max) g_tmr1Latency_max = dt; if (dt > g_tmr1Latency_max) g_tmr1Latency_max = dt;
if (dt < g_tmr1Latency_min) g_tmr1Latency_min = dt; if (dt < g_tmr1Latency_min) g_tmr1Latency_min = dt;
@@ -188,6 +191,7 @@ void setupPulsesPPM(uint8_t proto)
*ptr = 0; *ptr = 0;
} }
#if defined(PXX) #if defined(PXX)
const pm_uint16_t CRCTable[] PROGMEM = const pm_uint16_t CRCTable[] PROGMEM =
{ {
@@ -232,7 +236,7 @@ uint8_t PcmOnesCount;
void crc(uint8_t data) void crc(uint8_t data)
{ {
PcmCrc = (PcmCrc<<8)^pgm_read_word(&CRCTable[((PcmCrc>>8)^data) & 0xFF]); PcmCrc = (PcmCrc<<8) ^ pgm_read_word(&CRCTable[((PcmCrc >> 8) ^ data) & 0xFF]);
} }
@@ -557,9 +561,6 @@ void setupPulsesDSM2()
/****** END DSM2=PPM ********/ /****** END DSM2=PPM ********/
#if defined(IRPROTOS) #if defined(IRPROTOS)
static void _send_u8(uint8_t u8) static void _send_u8(uint8_t u8)
{ {
@@ -653,26 +654,31 @@ void picco_sendBn(uint8_t bits, uint8_t n)
while (n--) picco_sendB1(bits & (1<<n)); while (n--) picco_sendB1(bits & (1<<n));
} }
#define BITS 10 #define BITS 10
#define BITS2 (BITS-1) #define BITS2 (BITS-1)
NOINLINE uint8_t reduce7u(int16_t v, uint8_t sfr) NOINLINE uint8_t reduce7u(int16_t v, uint8_t sfr)
{ {
v += (1<<BITS2); v += (1<<BITS2);
if(v < 0) v=0; if (v < 0) v=0;
if(v >= (1<<BITS)) v=(1<<BITS)-1; if (v >= (1<<BITS)) v=(1<<BITS)-1;
return v>>sfr; return v >> sfr;
} }
NOINLINE int8_t reduce7s(int16_t v, uint8_t sfr, uint8_t sf2, int8_t ofs2) NOINLINE int8_t reduce7s(int16_t v, uint8_t sfr, uint8_t sf2, int8_t ofs2)
{ {
v += (1<<BITS2)+sf2; v += (1<<BITS2) + sf2;
if(v&(1<<BITS)) v = (1<<BITS)-1; //no overflow if (v & (1<<BITS)) {
v = (1<<BITS)-1; // no overflow
}
int8_t i8 = (uint16_t)v>>sfr; int8_t i8 = (uint16_t)v>>sfr;
if(i8<=0) i8=1; if (i8 <= 0) {
i8-=ofs2; i8 = 1;
if(i8>=ofs2) i8=ofs2-1;//no overflow }
i8 -= ofs2;
if (i8 >= ofs2) {
i8 = ofs2-1; //no overflow
}
return i8; return i8;
} }
@@ -771,7 +777,6 @@ void setupPulses()
#endif #endif
switch (required_protocol) { switch (required_protocol) {
#if defined(DSM2_PPM) // For DSM2=SERIAL, the default: case is executed, below #if defined(DSM2_PPM) // For DSM2=SERIAL, the default: case is executed, below
case PROTO_DSM2_LP45: case PROTO_DSM2_LP45:
case PROTO_DSM2_DSM2: case PROTO_DSM2_DSM2:
@@ -869,7 +874,6 @@ void setupPulses()
} }
switch(required_protocol) { switch(required_protocol) {
#if defined(PXX) #if defined(PXX)
case PROTO_PXX: case PROTO_PXX:
// schedule next Mixer calculations // schedule next Mixer calculations
@@ -920,7 +924,6 @@ void setupPulses()
#if defined(DEBUG) && !defined(VOICE) #if defined(DEBUG) && !defined(VOICE)
PORTH &= ~0x80; // PORTH:7 HIGH->LOW signals end of setupPulses() PORTH &= ~0x80; // PORTH:7 HIGH->LOW signals end of setupPulses()
#endif #endif
} }
#ifndef SIMU #ifndef SIMU
@@ -929,7 +932,6 @@ void setupPulses()
ISR(TIMER1_CAPT_vect) // 2MHz pulse generation ISR(TIMER1_CAPT_vect) // 2MHz pulse generation
{ {
#if defined (CPUM2560) #if defined (CPUM2560)
/*** G9X V4 hardware toggled PPM_out avoids any chance of output timing jitter ***/ /*** G9X V4 hardware toggled PPM_out avoids any chance of output timing jitter ***/
// OCR1B output pin (PPM_OUT) is pre-SET in setupPulses -- on every new // OCR1B output pin (PPM_OUT) is pre-SET in setupPulses -- on every new
@@ -943,15 +945,12 @@ ISR(TIMER1_CAPT_vect) // 2MHz pulse generation
// pin on OCR1A. Oh well.) // pin on OCR1A. Oh well.)
#else // manual bit-bang mode #else // manual bit-bang mode
uint8_t x; uint8_t x;
PORTB ^= (1<<OUT_B_PPM); // Toggle PPM_OUT PORTB ^= (1<<OUT_B_PPM); // Toggle PPM_OUT
x = *pulses2MHzRPtr++; // Byte size x = *pulses2MHzRPtr++; // Byte size
ICR1 = x; ICR1 = x;
if (x > 200) PORTB |= (1<<OUT_B_PPM); // Make sure pulses are the correct way up. if (x > 200) PORTB |= (1<<OUT_B_PPM); // Make sure pulses are the correct way up.
#endif #endif
} }
#if defined(PXX) #if defined(PXX)
@@ -960,23 +959,18 @@ ISR(TIMER1_COMPB_vect) // PXX main interrupt
uint8_t x; uint8_t x;
PORTB ^= (1<<OUT_B_PPM); PORTB ^= (1<<OUT_B_PPM);
x = *pulses2MHzRPtr; // Byte size x = *pulses2MHzRPtr; // Byte size
if ((x & 1) == 0) if ((x & 1) == 0) {
{
OCR1B += 32; OCR1B += 32;
} }
else else {
{
OCR1B += 16; OCR1B += 16;
} }
if ((x >>= 1) == 0) if ((x >>= 1) == 0) {
{ if (*(++pulses2MHzRPtr) == 0) {
if (*(++pulses2MHzRPtr) == 0)
{
OCR1B = OCR1C + 2000; // 1mS on from OCR1B OCR1B = OCR1C + 2000; // 1mS on from OCR1B
} }
} }
else else {
{
*pulses2MHzRPtr = x; *pulses2MHzRPtr = x;
} }
@@ -1002,7 +996,6 @@ ISR(TIMER1_COMPC_vect) // DSM2_PPM or PXX end of frame
} }
heartbeat |= HEART_TIMER_PULSES; heartbeat |= HEART_TIMER_PULSES;
#endif #endif
#if defined(DSM2_PPM) && defined(PXX) #if defined(DSM2_PPM) && defined(PXX)
@@ -1071,7 +1064,7 @@ void set_timer3_ppm()
ISR(TIMER3_COMPA_vect) //2MHz pulse generation ISR(TIMER3_COMPA_vect) //2MHz pulse generation
{ {
static uint8_t pulsePol; static uint8_t pulsePol;
static uint16_t *pulse2MHzPPM16RPtr = (uint16_t*) &pulses2MHz[PULSES_SIZE/2]; static uint16_t * pulse2MHzPPM16RPtr = (uint16_t*) &pulses2MHz[PULSES_SIZE/2];
if (pulsePol) { if (pulsePol) {
PORTE |= 0x80; // (1<<OUT_B_PPM); PORTE |= 0x80; // (1<<OUT_B_PPM);