DSM2 serial wrong function switch used.

DSM2 ppm port start. Not ready yet.
This commit is contained in:
bsongis
2012-01-19 19:22:24 +00:00
parent c67ba218fa
commit 23e49d4f7c
+121 -17
View File
@@ -21,6 +21,10 @@
#include "open9x.h" #include "open9x.h"
#if defined(DSM2_PPM) || defined(PXX)
uint8_t s_current_protocol = 255;
#endif
#ifdef DSM2_SERIAL #ifdef DSM2_SERIAL
inline void DSM2_EnableTXD(void) inline void DSM2_EnableTXD(void)
{ {
@@ -135,17 +139,17 @@ ISR(TIMER1_COMPA_vect) //2MHz pulse generation
#endif #endif
if (++pulses2MHzRPtr == pulses2MHzWPtr) { if (++pulses2MHzRPtr == pulses2MHzWPtr) {
//currpulse=0;
pulsePol = g_model.pulsePol;//0; pulsePol = g_model.pulsePol;
// channel = 0 ;
// PulseTotal = 0 ;
#if defined(PCBV3) #if defined(PCBV3)
TIMSK1 &= ~(1<<OCIE1A); //stop reentrance TIMSK1 &= ~(1<<OCIE1A); //stop reentrance
#else #else
TIMSK &= ~(1<<OCIE1A); //stop reentrance TIMSK &= ~(1<<OCIE1A); //stop reentrance
#endif #endif
sei(); sei();
setupPulses(); setupPulses();
#if !defined(PCBV3) && defined(DPPMPB7_HARDWARE) #if !defined(PCBV3) && defined(DPPMPB7_HARDWARE)
@@ -157,6 +161,7 @@ ISR(TIMER1_COMPA_vect) //2MHz pulse generation
#endif #endif
cli(); cli();
#if defined(PCBV3) #if defined(PCBV3)
TIMSK1 |= (1<<OCIE1A); TIMSK1 |= (1<<OCIE1A);
#else #else
@@ -174,14 +179,6 @@ ISR(TIMER1_COMPA_vect) //2MHz pulse generation
#endif #endif
//inline int16_t reduceRange(int16_t x) // for in case we want to have room for subtrims
//{
// return x-(x/4); //512+128 =? 640, 640 - 640/4 == 640 * 3/4 => 480 (just below 500msec - it can still reach 500 with offset)
//}
//int16_t PPM_range = 512*2; //range of 0.7..1.7msec
//uint16_t PPM_gap = 300 * 2; //Stoplen *2
//uint16_t PPM_frame ;
FORCEINLINE void setupPulsesPPM() // changed 10/05/2010 by dino Issue 128 FORCEINLINE void setupPulsesPPM() // changed 10/05/2010 by dino Issue 128
{ {
#define PPM_CENTER 1200*2 #define PPM_CENTER 1200*2
@@ -249,16 +246,13 @@ normal:
DSM2_Channel[i*2] = (byte)(i<<2) | highByte(pulse); DSM2_Channel[i*2] = (byte)(i<<2) | highByte(pulse);
DSM2_Channel[i*2+1] = lowByte(pulse); DSM2_Channel[i*2+1] = lowByte(pulse);
*/ */
#define DSM2_CHANS 6 #define DSM2_CHANS 6
inline void __attribute__ ((always_inline)) setupPulsesDsm2() FORCEINLINE void setupPulsesDsm2()
{ {
if (isFunctionActive(FUNC_LOGS)) {*pulses2MHzWPtr++ = 0x80;} *pulses2MHzWPtr++ = (isFunctionActive(FUNC_MODELMATCH) ? 0x80 : 0x00);
//elseif (somerangetestvariable) {*pulses2MHzWPtr++ = 0x20;}
else *pulses2MHzWPtr++ = 0x00;
*pulses2MHzWPtr++ = g_eeGeneral.currModel; *pulses2MHzWPtr++ = g_eeGeneral.currModel;
for (uint8_t i=0; i<DSM2_CHANS; i++) { for (uint8_t i=0; i<DSM2_CHANS; i++) {
uint16_t pulse = limit(0, (g_chans512[i]>>1)+512, 1023); uint16_t pulse = limit(0, (g_chans512[i]>>1)+512, 1023);
@@ -303,6 +297,52 @@ void DSM2_Init(void)
#endif // SIMU #endif // SIMU
} }
#endif
#if defined(DSM2_PPM)
static uint8_t *Dsm2_pulsePtr = (uint8_t *)pulses2MHz;
void setupPulsesDsm2(uint8_t chns)
{
static uint8_t dsmDat[2+6*2]= {0xFF,0x00, 0x00,0xAA, 0x05,0xFF, 0x09,0xFF, 0x0D,0xFF, 0x13,0x54, 0x14,0xAA};
uint8_t counter;
// If more channels needed make sure the pulses union/array is large enough
if (dsmDat[0] & BadData) //first time through, setup header
{
switch(g_model.ppmNCH)
{
case LPXDSM2:
dsmDat[0]= 0x80;
break;
case DSM2only:
dsmDat[0]=0x90;
break;
default:
dsmDat[0]=0x98; //dsmx, bind mode
break;
}
}
if ((dsmDat[0]&BindBit)&&(!keyState(SW_Trainer))) dsmDat[0]&=~BindBit; //clear bind bit if trainer not pulled
if ((!(dsmDat[0]&BindBit))&&getSwitch(MAX_DRSWITCH-1,0,0)) dsmDat[0]|=RangeCheckBit; //range check function
else dsmDat[0]&=~RangeCheckBit;
dsmDat[1]=g_eeGeneral.currModel+1; //DSM2 Header second byte for model match
for(uint8_t i=0; i<chns; i++)
{
uint16_t pulse = limit(0, (g_chans512[i]>>1)+512,1023);
dsmDat[2+2*i] = (i<<2) | ((pulse>>8)&0x03);
dsmDat[3+2*i] = pulse & 0xff;
}
for ( counter = 0; counter < 14; counter += 1 )
{
sendByteDsm2(dsmDat[counter]);
}
pulses2MHzptr-=1; //remove last stopbits and
_send_1( 255 ); //prolong them
_send_1(0); //end of pulse stream
Dsm2_pulsePtr = pulses2MHz.pbyte;
}
#endif #endif
@@ -466,6 +506,45 @@ inline void __attribute__ ((always_inline)) setupPulsesTracerCtp1009()
void setupPulses() void setupPulses()
{ {
#if defined(DSM2_PPM) || defined(PXX)
if (s_current_protocol != g_model.protocol) {
s_current_protocol = g_model.protocol;
// switch mode here
switch (g_model.protocol) {
#if defined(DSM2_PPM)
case PROTO_DSM2:
TCCR1B = 0; // Stop counter
OCR1B = 200; // 100 uS
TCNT1 = 300; // Past the OCR1B value
ICR1 = 44000; // Next frame starts in 22 mS
TIMSK &= ~0x3C; // All interrupts off
TIFR = 0x3C; // Clear all pending interrupts
TIMSK |= 0x28; // Enable CAPT and COMPB
TCCR1A = (0 << WGM10);
TCCR1B = (3 << WGM12) | (2 << CS10); // CTC ICR, 16MHz / 8
break;
#endif
#if defined(PXX)
case PROTO_PXX:
// do nothing, not yet implemented
break;
#endif
default:
TCCR1B = 0; // Stop counter
OCR1A = 40000; // Next frame starts in 20 mS
TCNT1 = 0;
TIMSK &= ~0x3C; // All interrupts off
TIFR = 0x3C; // Clear all pending interrupts
TIMSK |= 0x10; // Enable COMPA
TCCR1A = (0 << WGM10);
TCCR1B = (1 << WGM12) | (2 << CS10); // CTC OCRA, 16MHz / 8
break;
}
}
#endif
pulses2MHzWPtr = pulses2MHz; pulses2MHzWPtr = pulses2MHz;
pulses2MHzRPtr = pulses2MHz; pulses2MHzRPtr = pulses2MHz;
@@ -497,3 +576,28 @@ void setupPulses()
break; break;
} }
} }
#if defined(DSM2_PPM) || defined(PXX)
ISR(TIMER1_CAPT_vect) // 2MHz pulse generation
{
uint8_t x ;
PORTB ^= (1<<OUT_B_PPM);
x = *Dsm2_pulsePtr++; // Byte size
ICR1 = x ;
if (x > 200) PORTB |= (1<<OUT_B_PPM); // Make sure pulses are the correct way up
heartbeat |= HEART_TIMER2Mhz;
}
ISR(TIMER1_COMPB_vect) // DSM2 end of frame
{
ICR1 = 41536 ; // next frame starts in 22ms 41536 = 2*(22000 - 14*11*8)
if (OCR1B < 255) {
OCR1B = 39000; // delay setup pulses by 19.5ms to reduce system latency
}
else {
OCR1B = 200;
sei();
setupPulses();
}
}
#endif