diff --git a/src/open9x.cpp b/src/open9x.cpp index e1699de06..7c572664c 100644 --- a/src/open9x.cpp +++ b/src/open9x.cpp @@ -50,8 +50,8 @@ gtime_t g_unixTime; // Global date/time register, incremented each second in per EEGeneral g_eeGeneral; ModelData g_model; -uint8_t g_tmr1Latency_max; -uint8_t g_tmr1Latency_min; +uint8_t g_tmr1Latency_max; +uint8_t g_tmr1Latency_min; uint16_t g_timeMain; #ifdef DEBUG uint16_t g_time_per10; @@ -318,13 +318,13 @@ int16_t Expo::expo(int16_t x) #endif -void applyExpos(int16_t *anas, uint8_t phase) +void applyExpos(int16_t *anas, uint8_t phase) { static int16_t anas2[NUM_STICKS]; // values before expo, to ensure same expo base when multiple expo lines are used memcpy(anas2, anas, sizeof(anas2)); - if (phase == 255) - phase = getFlightPhase(); + if (phase == 255) + phase = getFlightPhase(); int8_t cur_chn = -1; for (uint8_t i=0; i=0 && k<8) { // && (event & _MSK_KEY_REPT)) //LH_DWN LH_UP LV_DWN LV_UP RV_DWN RV_UP RH_DWN RH_UP uint8_t idx = CONVERT_MODE(1+k/2) - 1; - uint8_t phase = getTrimFlightPhase(idx, getFlightPhase()); + uint8_t phase = getTrimFlightPhase(idx, getFlightPhase()); int16_t before = getTrimValue(phase, idx); int8_t v = (s==0) ? min(32, abs(before)/4+1) : 1 << (s-1); // 1=>1 2=>2 3=>4 4=>8 bool thro = (idx==THR_STICK && g_model.thrTrim); @@ -896,16 +896,16 @@ void getADC_filt() #else void getADC_filt() { - static uint16_t t_ana[2][8]; - for (uint8_t adc_input=0; adc_input<8; adc_input++) { + static uint16_t t_ana[2][8]; + for (uint8_t adc_input=0; adc_input<8; adc_input++) { ADMUX=adc_input|ADC_VREF_TYPE; // Start the AD conversion ADCSRA|=0x40; - - // Do this while waiting - s_anaFilt[adc_input] = (s_anaFilt[adc_input]/2 + t_ana[1][adc_input]) & 0xFFFE; //gain of 2 on last conversion - clear last bit - t_ana[1][adc_input] = (t_ana[1][adc_input] + t_ana[0][adc_input]) >> 1; - + + // Do this while waiting + s_anaFilt[adc_input] = (s_anaFilt[adc_input]/2 + t_ana[1][adc_input]) & 0xFFFE; //gain of 2 on last conversion - clear last bit + t_ana[1][adc_input] = (t_ana[1][adc_input] + t_ana[0][adc_input]) >> 1; + // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; @@ -942,20 +942,20 @@ void getADC_osmp() #else void getADC_osmp() { - uint16_t temp_ana; - - for (uint8_t adc_input=0; adc_input<8; adc_input++) { - temp_ana = 0; - ADMUX = adc_input|ADC_VREF_TYPE; + uint16_t temp_ana; + + for (uint8_t adc_input=0; adc_input<8; adc_input++) { + temp_ana = 0; + ADMUX = adc_input|ADC_VREF_TYPE; for (uint8_t i=0; i<4;i++) { // Going from 10bits to 11 bits. Addition = n. Loop 4^n times // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; - temp_ana += ADCW; + temp_ana += ADCW; } - s_anaFilt[adc_input] = temp_ana / 2; // divide by 2^n to normalize result. + s_anaFilt[adc_input] = temp_ana / 2; // divide by 2^n to normalize result. } } #endif @@ -975,7 +975,7 @@ void getADC_single() #else void getADC_single() { - for (uint8_t adc_input=0; adc_input<8; adc_input++) { + for (uint8_t adc_input=0; adc_input<8; adc_input++) { ADMUX=adc_input|ADC_VREF_TYPE; // Start the AD conversion ADCSRA|=0x40; @@ -987,7 +987,7 @@ void getADC_single() } #endif -#if not defined(PCBARM) +#if not defined(PCBARM) void getADC_bandgap() { #if defined (PCBV4) @@ -1110,17 +1110,17 @@ int32_t act [MAX_MIXERS] = {0}; uint8_t swOn [MAX_MIXERS] = {0}; uint8_t mixWarning; -FORCEINLINE void evalTrims(uint8_t phase) +FORCEINLINE void evalTrims(uint8_t phase) { for (uint8_t i=0; i throttle trim if applicable int32_t vv = 2*RESX; int16_t trim = getTrimValue(getTrimFlightPhase(i, phase), i); if (i==THR_STICK && g_model.thrTrim) { - if (g_eeGeneral.throttleReversed) + if (g_eeGeneral.throttleReversed) trim = -trim; - int16_t v = anas[i]; - vv = ((int32_t)trim-TRIM_MIN)*(RESX-v)/(2*RESX); + int16_t v = anas[i]; + vv = ((int32_t)trim-TRIM_MIN)*(RESX-v)/(2*RESX); } else if (trimsCheckTimer > 0) { trim = 0; @@ -1131,7 +1131,7 @@ FORCEINLINE void evalTrims(uint8_t phase) } bool s_noTrainerInput = false; -uint8_t evalSticks(uint8_t phase) +uint8_t evalSticks(uint8_t phase) { #ifdef HELI uint16_t d = 0; @@ -1162,10 +1162,10 @@ uint8_t evalSticks(uint8_t phase) if(v < -RESX) v = -RESX; if(v > RESX) v = RESX; - - if (g_eeGeneral.throttleReversed && ch==THR_STICK) - v = -v; - + + if (g_eeGeneral.throttleReversed && ch==THR_STICK) + v = -v; + calibratedStick[ch] = v; //for show in expo uint8_t tmp = (uint16_t)abs(v) / 16; if (tmp <= 1) anaCenter |= (tmp==0 ? 1< NUM_POTS) { val = RESX + g_chans512[g_model.thrTraceSrc-NUM_POTS-1]; @@ -1769,27 +1769,27 @@ void perMain() } // Timers end - if (s_fade_flight_phases) { - for (uint8_t p=0; p delta) - fp_act[p] += delta; - else { - fp_act[p] = MAX_ACT; - s_fade_flight_phases -= (1< delta) - fp_act[p] -= delta; - else { - fp_act[p] = 0; - s_fade_flight_phases -= (1< delta) + fp_act[p] += delta; + else { + fp_act[p] = MAX_ACT; + s_fade_flight_phases -= (1< delta) + fp_act[p] -= delta; + else { + fp_act[p] = 0; + s_fade_flight_phases -= (1< 20) - { - for (int xx=0; xx<20; xx++) // scroll one char left - userDataDisplayBuf[xx] = userDataDisplayBuf[xx+1]; - displayBufferIndex = 20; - } - userDataDisplayBuf[displayBufferIndex] = userDataRxBuffer[byt]; - - // Write the raw byte out to log file, if open - if (testLogOpen && (g_oLogFile.fs != 0)) - f_putc(userDataRxBuffer[byt], &g_oLogFile); - - } -#endif - + char userDataRxBuffer[21]; // Temp buffer used to collect fr-sky user data + + // retrieve bytes from user data buffer and insert into display string, + // scrolling at the 21 character mark (edge of screen) + uint8_t numbytes = frskyGetUserData(userDataRxBuffer, 21); // Get as many bytes as we can + + static uint8_t displayBufferIndex; + for (uint8_t byt=0; byt < numbytes; byt++) + { + displayBufferIndex++; + if (displayBufferIndex > 20) + { + for (int xx=0; xx<20; xx++) // scroll one char left + userDataDisplayBuf[xx] = userDataDisplayBuf[xx+1]; + displayBufferIndex = 20; + } + userDataDisplayBuf[displayBufferIndex] = userDataRxBuffer[byt]; + + // Write the raw byte out to log file, if open + if (testLogOpen && (g_oLogFile.fs != 0)) + f_putc(userDataRxBuffer[byt], &g_oLogFile); + + } +#endif + lcd_clear(); uint8_t evt = getEvent(); evt = checkTrim(evt); @@ -1853,23 +1853,23 @@ void perMain() g_menuStack[g_menuStackPtr](evt); refreshDisplay(); -#if defined(PCBV4) +#if defined(PCBV4) + // PPM signal on phono-jack. In or out? ... + if(checkSlaveMode()) { + PORTG |= (1<4000 && val < 16000) // G: Prioritize reset pulse. (Needed when less than 8 incoming pulses) + if (val>4000 && val < 16000) // G: Prioritize reset pulse. (Needed when less than 8 incoming pulses) ppmInState = 1; // triggered else { @@ -2038,7 +2038,7 @@ ISR(TIMER3_CAPT_vect) // G: High frequency noise can cause stack overflo with IS lastCapt = capture; - cli(); // disable other interrupts for stack pops before this function's RETI + cli(); // disable other interrupts for stack pops before this function's RETI #if defined (PCBV4) TIMSK3 |= (1< 0) { - UDR0 = frskyTxBuffer[--frskyTxBufferCount]; - } - else { - UCSR0B &= ~(1 << UDRIE0); // disable UDRE0 interrupt - } -} -#endif - -#if defined (DSM2_SERIAL) -FORCEINLINE void DSM2_USART0_vect() +/* + USART0 Transmit Data Register Emtpy ISR + Used to transmit FrSky data packets and DSM2 protocol +*/ + +#if defined (FRSKY) +FORCEINLINE void FRSKY_USART0_vect() { - UDR0 = *((uint16_t*)pulses2MHzRPtr); + if (frskyTxBufferCount > 0) { + UDR0 = frskyTxBuffer[--frskyTxBufferCount]; + } + else { + UCSR0B &= ~(1 << UDRIE0); // disable UDRE0 interrupt + } +} +#endif + +#if defined (DSM2_SERIAL) +FORCEINLINE void DSM2_USART0_vect() +{ + UDR0 = *((uint16_t*)pulses2MHzRPtr); + + pulses2MHzRPtr += sizeof(uint16_t); + if (pulses2MHzRPtr == pulses2MHzWPtr) { + UCSR0B &= ~(1 << UDRIE0); // disable UDRE0 interrupt + } +} +#endif + +#ifndef SIMU +#if defined (FRSKY) or defined(DSM2_SERIAL) +ISR(USART0_UDRE_vect) +{ +#if defined (FRSKY) and defined (DSM2_SERIAL) + if (g_model.protocol == PROTO_DSM2) { + DSM2_USART0_vect(); + } + else { + FRSKY_USART0_vect(); + } +#elif defined (FRSKY) + FRSKY_USART0_vect(); +#else + DSM2_USART0_vect(); +#endif +} +#endif +#endif - pulses2MHzRPtr += sizeof(uint16_t); - if (pulses2MHzRPtr == pulses2MHzWPtr) { - UCSR0B &= ~(1 << UDRIE0); // disable UDRE0 interrupt - } -} -#endif - -#ifndef SIMU -#if defined (FRSKY) or defined(DSM2_SERIAL) -ISR(USART0_UDRE_vect) -{ -#if defined (FRSKY) and defined (DSM2_SERIAL) - if (g_model.protocol == PROTO_DSM2) { - DSM2_USART0_vect(); - } - else { - FRSKY_USART0_vect(); - } -#elif defined (FRSKY) - FRSKY_USART0_vect(); -#else - DSM2_USART0_vect(); -#endif -} -#endif -#endif - #if defined (PCBV4) /*---------------------------------------------------------*/ /* User Provided Date/Time Function for FatFs module */ @@ -2153,14 +2153,14 @@ uint32_t o9x_get_fattime(void) // TODO why not in ff.cpp? void instantTrim() { - uint8_t phase = getFlightPhase(); - + uint8_t phase = getFlightPhase(); + for (uint8_t i=0; i