diff --git a/src/board_gruvin9x.cpp b/src/board_gruvin9x.cpp index 5492d4cff..d80f8f075 100644 --- a/src/board_gruvin9x.cpp +++ b/src/board_gruvin9x.cpp @@ -119,6 +119,11 @@ inline void board_init() OCR4A = 0x7d; TCCR4B = (1 << WGM42) | (3<=0 && k<8); + + if (trim == trim_evt) { + s_evt = 0; + return evt; + } + else { + return 0; + } +} +#else uint8_t getEvent() { uint8_t evt = s_evt; s_evt = 0; return evt; } +#endif #if defined(PCBARM) #define KEY_LONG_DELAY 32 @@ -105,7 +123,7 @@ void Key::input(bool val, EnumKeys enuk) m_state >>= 1; m_cnt = 0; } - //fallthrough + // no break case 1: if( (m_cnt & (m_state-1)) == 0) putEvent(EVT_KEY_REPT(enuk)); break; @@ -133,7 +151,6 @@ void killEvents(uint8_t event) if(event < (int)DIM(keys)) keys[event].killEvents(); } -//uint16_t g_anaIns[8]; volatile uint16_t g_tmr10ms; volatile uint8_t g_blinkTmr10ms; @@ -141,6 +158,9 @@ void per10ms() { g_tmr10ms++; g_blinkTmr10ms++; + + if (s_noHi) s_noHi--; + if (trimsCheckTimer) trimsCheckTimer --; #if defined (PCBARM) Tenms |= 1 ; // 10 mS has passed diff --git a/src/eeprom_avr.cpp b/src/eeprom_avr.cpp index ea4f1cbfb..8c6d73870 100644 --- a/src/eeprom_avr.cpp +++ b/src/eeprom_avr.cpp @@ -110,7 +110,7 @@ void eeWriteBlockCmp(const void *i_pointer_ram, uint16_t i_pointer_eeprom, size_ #endif if (s_sync_write) { - while (eeprom_buffer_size > 0) wdt_reset(); + while (eeprom_buffer_size > 0) WDT_RESET_STOCK(); } } @@ -545,8 +545,6 @@ const pm_char * eeBackupModel(uint8_t i_fileSrc) } #endif - wdt_reset(); - // check and create folder here strcpy_P(buf, STR_MODELS_PATH); #ifndef SIMU @@ -559,8 +557,6 @@ const pm_char * eeBackupModel(uint8_t i_fileSrc) } #endif - wdt_reset(); - buf[sizeof(MODELS_PATH)-1] = '/'; eeLoadModelName(i_fileSrc, &buf[sizeof(MODELS_PATH)]); buf[sizeof(MODELS_PATH)+FILENAME_MAXLEN] = '\0'; @@ -599,8 +595,6 @@ const pm_char * eeBackupModel(uint8_t i_fileSrc) return SDCARD_ERROR(result); } - wdt_reset(); - result = f_write(&archiveFile, &g_eeGeneral.myVers, 1, &written); if (result != FR_OK) { return SDCARD_ERROR(result); @@ -614,7 +608,6 @@ const pm_char * eeBackupModel(uint8_t i_fileSrc) if (result != FR_OK) { return SDCARD_ERROR(result); } - wdt_reset(); } f_close(&archiveFile); @@ -632,8 +625,6 @@ const pm_char * eeRestoreModel(uint8_t i_fileDst, char *model_name) return SDCARD_ERROR(result); } - wdt_reset(); - strcpy_P(buf, STR_MODELS_PATH); buf[sizeof(MODELS_PATH)-1] = '/'; strcpy(&buf[sizeof(MODELS_PATH)], model_name); @@ -671,7 +662,6 @@ const pm_char * eeRestoreModel(uint8_t i_fileDst, char *model_name) s_sync_write = false; return STR_EEPROMOVERFLOW; } - wdt_reset(); } } while (read == 15); @@ -805,7 +795,7 @@ void RlcFile::nextRlcWriteStep() void RlcFile::flush() { - while (eeprom_buffer_size > 0) wdt_reset(); + while (eeprom_buffer_size > 0) WDT_RESET_STOCK(); s_sync_write = true; while (m_write_len && !s_write_err) diff --git a/src/gruvin9x/logs.cpp b/src/gruvin9x/logs.cpp index cb67882b5..9029ab7c1 100644 --- a/src/gruvin9x/logs.cpp +++ b/src/gruvin9x/logs.cpp @@ -54,8 +54,6 @@ const pm_char * openLogs() return SDCARD_ERROR(result); } - wdt_reset(); - strcpy_P(buf, STR_LOGS_PATH); result = f_opendir(&folder, buf); @@ -66,8 +64,6 @@ const pm_char * openLogs() return SDCARD_ERROR(result); } - wdt_reset(); - buf[sizeof(LOGS_PATH)-1] = '/'; memcpy(&buf[sizeof(LOGS_PATH)], g_model.name, sizeof(g_model.name)); buf[sizeof(LOGS_PATH)+FILENAME_MAXLEN] = '\0'; diff --git a/src/model_menus.cpp b/src/model_menus.cpp index 63ab921a9..24a24cfd6 100644 --- a/src/model_menus.cpp +++ b/src/model_menus.cpp @@ -150,7 +150,6 @@ bool listSDcardModels() FRESULT res = f_opendir(&dir, MODELS_PATH); /* Open the directory */ if (res == FR_OK) { for (;;) { - wdt_reset(); res = f_readdir(&dir, &fno); /* Read a directory item */ if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ if (fno.fname[0] == '.') continue; /* Ignore dot entry */ @@ -1229,8 +1228,28 @@ bool reachExpoMixCountLimit(uint8_t expo) return false; } +#if defined(PCBV4) +inline void pauseMixerCalculations() +{ + cli(); + TIMSK5 &= ~(1<srcRaw = (s_currCh > 4 ? s_currCh : channel_order(s_currCh)); mix->weight = 100; } + resumeMixerCalculations(); STORE_MODELVARS; } void copyExpoMix(uint8_t expo, uint8_t idx) { + pauseMixerCalculations(); if (expo) { ExpoData *expo = expoaddress(idx); memmove(expo+1, expo, (MAX_EXPOS-(idx+1))*sizeof(ExpoData)); @@ -1274,6 +1297,7 @@ void copyExpoMix(uint8_t expo, uint8_t idx) MixData *mix = mixaddress(idx); memmove(mix+1, mix, (MAX_MIXERS-(idx+1))*sizeof(MixData)); } + resumeMixerCalculations(); STORE_MODELVARS; } @@ -1283,11 +1307,13 @@ void memswap(void *a, void *b, uint8_t size) uint8_t *y = (uint8_t*)b; uint8_t temp ; + pauseMixerCalculations(); while (size--) { temp = *x; *x++ = *y; *y++ = temp; } + resumeMixerCalculations(); } bool swapExpo(uint8_t &idx, uint8_t up) @@ -1923,7 +1949,9 @@ void menuProcLimits(uint8_t _event) if (event==EVT_KEY_LONG(KEY_MENU)) { s_noHi = NO_HI_LEN; killEvents(event); + pauseMixerCalculations(); moveTrimsToOffsets(); // if highlighted and menu pressed - move trims to offsets + resumeMixerCalculations(); } } return; @@ -1961,11 +1989,21 @@ void menuProcLimits(uint8_t _event) lcd_outdezAtt( 8*FW, y, ld->offset, attr|PREC1); #endif if (active) { - ld->offset = checkIncDec(event, ld->offset, -1000, 1000, EE_MODEL|NO_INCDEC_MARKS); + int16_t new_offset = checkIncDec(event, ld->offset, -1000, 1000, EE_MODEL|NO_INCDEC_MARKS); +#if defined(PCBV4) + if (checkIncDec_Ret) +#endif + { + pauseMixerCalculations(); + ld->offset = new_offset; + resumeMixerCalculations(); + } } else if (attr && event==EVT_KEY_LONG(KEY_MENU)) { int16_t zero = g_chans512[k]; + pauseMixerCalculations(); ld->offset = (ld->revert) ? -zero : zero; + resumeMixerCalculations(); s_editMode = 0; STORE_MODELVARS; } diff --git a/src/open9x.cpp b/src/open9x.cpp index b651d3526..1c3f12368 100644 --- a/src/open9x.cpp +++ b/src/open9x.cpp @@ -55,9 +55,10 @@ ModelData g_model; uint8_t g_tmr1Latency_max; uint8_t g_tmr1Latency_min; #endif -uint16_t g_timeMain; -#ifdef DEBUG -uint16_t g_time_per10; + +uint16_t g_timeMainMax; +#if defined(PCBV4) +uint8_t g_timeMainLast; #endif #ifdef AUDIO @@ -771,7 +772,7 @@ void clearKeyEvents() #ifdef SIMU while (keyDown() && main_thread_running) sleep(1/*ms*/); #else - while (keyDown()) wdt_reset(); // loop until all keys are up + while (keyDown()) WDT_RESET_STOCK(); // loop until all keys are up #endif memset(keys, 0, sizeof(keys)); putEvent(0); @@ -902,7 +903,7 @@ void checkTHR() checkBacklight(); - wdt_reset(); + WDT_RESET_STOCK(); } } @@ -945,7 +946,7 @@ void checkSwitches() checkBacklight(); - wdt_reset(); + WDT_RESET_STOCK(); #ifdef SIMU if (!main_thread_running) return; @@ -972,7 +973,7 @@ void alert(const pm_char * t, const pm_char *s) checkBacklight(); - wdt_reset(); + WDT_RESET_STOCK(); } } @@ -1003,12 +1004,21 @@ void message(const pm_char *title, const pm_char *t, const char *last) clearKeyEvents(); } +#if defined(PCBV4) +void checkTrims() +{ + uint8_t event = getEvent(true); +#else uint8_t checkTrim(uint8_t event) { +#endif int8_t k = (event & EVT_KEY_MASK) - TRM_BASE; int8_t s = g_model.trimInc; - if (k>=0 && k<8) { // && (event & _MSK_KEY_REPT)) +#if !defined(PCBV4) + if (k>=0 && k<8) +#endif + { // && (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(s_perout_flight_phase, idx); @@ -1063,9 +1073,13 @@ uint8_t checkTrim(uint8_t event) AUDIO_TRIM(); #endif } +#if !defined(PCBV4) return 0; +#endif } +#if !defined(PCBV4) return event; +#endif } #ifdef SIMU @@ -1245,7 +1259,6 @@ void getADC_bandgap() #endif // SIMU uint8_t g_vbat100mV = 0; - volatile uint8_t tick10ms = 0; uint16_t g_LightOffCounter; @@ -1797,7 +1810,7 @@ void perOut() sDelay[i] = md->delayUp * 100; } if (sDelay[i]) { // perform delay - if(tick10ms) sDelay[i]--; + if(tick10ms) sDelay[i]-=tick10ms; if (!md->swtch) { v = -1024; } @@ -1818,7 +1831,7 @@ void perOut() sDelay[i] = md->delayDown * 100; } if (sDelay[i]) { // perform delay - if(tick10ms) sDelay[i]--; + if(tick10ms) sDelay[i]-=tick10ms; if (!md->swtch) v = +1024; has_delay = true; } @@ -1852,7 +1865,7 @@ void perOut() //act[i] += diff>0 ? (32768)/((int16_t)100*md->speedUp) : -(32768)/((int16_t)100*md->speedDown); //-100..100 => 32768 -> 100*83886/256 = 32768, For MAX we divide by 2 since it's asymmetrical if (tick10ms) { - int32_t rate = (int32_t)DEL_MULT*2048*100; + int32_t rate = (int32_t)DEL_MULT*2048*100*tick10ms; if(md->weight) rate /= abs(md->weight); act[i] = (diff>0) ? ((md->speedUp>0) ? act[i]+(rate)/((int16_t)50*md->speedUp) : (int32_t)v*DEL_MULT) : @@ -1925,12 +1938,27 @@ char userDataDisplayBuf[TELEM_SCREEN_BUFFER_SIZE]; #endif int32_t sum_chans512[NUM_CHNOUT] = {0}; -void perMain() +inline void doMixerCalculations(uint16_t tmr10ms, uint8_t tick10ms) { - static uint16_t lastTMR; - uint16_t tmr10ms = get_tmr10ms(); - tick10ms = (tmr10ms != lastTMR); - lastTMR = tmr10ms; + if (g_eeGeneral.filterInput == 1) { + getADC_filt() ; + } + else if ( g_eeGeneral.filterInput == 2) { + getADC_osmp() ; + } + else { + getADC_single() ; + } + +#if defined(PCBARM) && defined(REVB) + Current_analogue = ( Current_analogue * 31 + s_anaFilt[8] ) >> 5 ; +#elif defined(PCBV4) + // For PCB V4, use our own 1.2V, external reference (connected to ADC3) + ADCSRB &= ~(1<= 0) s_traceCnt++; // TODO to be checked } } - // Throttle trace end // Timers start for (uint8_t i=0; i<2; i++) { @@ -2102,7 +2113,7 @@ void perMain() lastSwPos[i] = swPos; } - if ( (s_timerVal_10ms[i] += 1 ) >= 100 ) { + if ( (s_timerVal_10ms[i] += tick10ms ) >= 100 ) { s_timerVal_10ms[i] -= 100 ; if (tv) s_timerVal[i] = tv - s_timerVal[i]; @@ -2176,16 +2187,16 @@ void perMain() for (uint8_t p=0; p delta) - fp_act[p] += delta; + if (MAX_ACT - fp_act[p] > delta * tick10ms) + fp_act[p] += delta * tick10ms; else { fp_act[p] = MAX_ACT; s_fade_flight_phases -= (1< delta) - fp_act[p] -= delta; + if (fp_act[p] > delta * tick10ms) + fp_act[p] -= delta * tick10ms; else { fp_act[p] = 0; s_fade_flight_phases -= (1<50) { inacCounter++; @@ -2243,8 +2287,13 @@ void perMain() #endif lcd_clear(); + +#if defined(PCBV4) + uint8_t evt = getEvent(false); +#else uint8_t evt = getEvent(); evt = checkTrim(evt); +#endif // TODO port lightOnStickMove from er9x + flash saving, call checkBacklight() if(g_LightOffCounter) g_LightOffCounter--; @@ -2320,6 +2369,7 @@ void perMain() break; } } + int16_t g_ppmIns[8]; uint8_t ppmInState = 0; //0=unsync 1..8= wait for value i-1 @@ -2351,6 +2401,43 @@ uint16_t getTmr16KHz() } } +#if defined (PCBV4) +ISR(TIMER5_COMPA_vect, ISR_NOBLOCK) // mixer interrupt +{ + cli(); + TIMSK5 &= ~(1< g_timeMainMax) g_timeMainMax = t0 ; + + cli(); + TIMSK5 |= (1<> 5 ; -#elif defined(PCBV4) - // For PCB V4, use our own 1.2V, external reference (connected to ADC3) - ADCSRB &= ~(1< g_timeMain) g_timeMain = t0 ; + if (t0 > g_timeMainMax) g_timeMainMax = t0 ; +#endif + } #if defined(PCBARM) || defined(PCBV4) diff --git a/src/open9x.h b/src/open9x.h index 3699b922c..494a5d3f9 100644 --- a/src/open9x.h +++ b/src/open9x.h @@ -41,6 +41,12 @@ #include #include +#if defined(PCBSTD) +#define WDT_RESET_STOCK() wdt_reset() +#else +#define WDT_RESET_STOCK() +#endif + #if defined(PCBARM) #include "board_ersky9x.h" #endif @@ -478,7 +484,11 @@ extern char idx2char(int8_t idx); void clearKeyEvents(); void pauseEvents(uint8_t enuk); void killEvents(uint8_t enuk); +#if defined(PCBV4) +uint8_t getEvent(bool trim); +#else uint8_t getEvent(); +#endif void putEvent(uint8_t evt); uint8_t keyDown(); @@ -559,9 +569,9 @@ void resetAll(); extern uint8_t g_tmr1Latency_max; extern uint8_t g_tmr1Latency_min; -extern uint16_t g_timeMain; -#ifdef DEBUG -extern uint16_t g_time_per10; +extern uint16_t g_timeMainMax; +#if defined(PCBV4) +extern uint8_t g_timeMainLast; #endif #define MAXTRACE 120 @@ -748,7 +758,6 @@ extern uint8_t ppmInState; //0=unsync 1..8= wait for value i-1 extern int16_t g_ppmIns[8]; extern int16_t ex_chans[NUM_CHNOUT]; // Outputs (before LIMITS) of the last perMain extern int16_t g_chans512[NUM_CHNOUT]; -extern volatile uint8_t tick10ms; extern uint16_t BandGap; extern uint16_t expou(uint16_t x, uint16_t k); diff --git a/src/pulses_avr.cpp b/src/pulses_avr.cpp index 5c6b97b3e..86cc92976 100644 --- a/src/pulses_avr.cpp +++ b/src/pulses_avr.cpp @@ -204,6 +204,12 @@ void setupPulsesPPM(uint8_t proto) uint8_t p = (proto == PROTO_PPM16 ? 16 : 8) + (g_model.ppmNCH * 2); //Channels *2 uint16_t q = (g_model.ppmDelay*50+300)*2; // Stoplen *2 uint32_t rest = 22500u*2 - q; // Minimum Framelen=22.5ms + +#if defined(PCBV4) + OCR5A = (uint16_t)0x7d * (45+g_model.ppmFrameLength-g_timeMainLast-2/*1ms*/); + TCNT5 = 0; +#endif + rest += (int32_t(g_model.ppmFrameLength))*1000; for (uint8_t i=(proto==PROTO_PPM16) ? p-8 : 0; i