diff --git a/src/Makefile b/src/Makefile index b88bd8804..70bfbad1e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -347,6 +347,10 @@ ifeq ($(PCB), ARM) ifeq ($(SDCARD), YES) SRC += FatFs/ff.c FatFs/fattime.c FatFs/option/ccsbcs.c ersky9x/diskio.c endif + + ifeq ($(ROTARY_ENCODERS), YES) + CPPDEFS += -DROTARY_ENCODERS + endif endif ifeq ($(PCB), V4) diff --git a/src/board_ersky9x.cpp b/src/board_ersky9x.cpp index ebff7e3a0..aeeb782ea 100644 --- a/src/board_ersky9x.cpp +++ b/src/board_ersky9x.cpp @@ -415,6 +415,46 @@ void end_ppm_capture() NVIC_DisableIRQ(TC3_IRQn) ; } +static void init_rotary_encoder() +{ + configure_pins( PIO_PC19 | PIO_PC21, PIN_ENABLE | PIN_INPUT | PIN_PORTC | PIN_PULLUP ) ; // 19 and 21 are rotary encoder + configure_pins( PIO_PB6, PIN_ENABLE | PIN_INPUT | PIN_PORTB | PIN_PULLUP ) ; // rotary encoder switch + PIOC->PIO_IER = PIO_PC19 | PIO_PC21 ; + NVIC_EnableIRQ(PIOC_IRQn) ; +} + +static void stop_rotary_encoder() +{ + NVIC_DisableIRQ(PIOC_IRQn) ; + PIOC->PIO_IDR = PIO_PC19 | PIO_PC21 ; +} + +volatile uint32_t Rotary_position ; +extern "C" void PIOC_IRQHandler() +{ + register uint32_t dummy; + + dummy = PIOC->PIO_ISR ; // Read and clear status register + (void) dummy ; // Discard value - prevents compiler warning + + dummy = PIOC->PIO_PDSR ; // Read Rotary encoder (PC19, PC21) + dummy >>= 19 ; + dummy &= 0x05 ; // pick out the three bits + if ( dummy != ( Rotary_position & 0x05 ) ) + { + if ( ( Rotary_position & 0x01 ) ^ ( ( dummy & 0x04) >> 2 ) ) + { + incRotaryEncoder(0, -1); + } + else + { + incRotaryEncoder(0, +1); + } + Rotary_position &= ~0x45 ; + Rotary_position |= dummy ; + } +} + extern "C" void TC2_IRQHandler() { register uint32_t dummy; @@ -733,6 +773,8 @@ void board_init() eeprom_init(); + init_rotary_encoder(); + init_SDcard(); } @@ -980,6 +1022,10 @@ void readKeysAndTrims() { register uint32_t i; +#if defined(ROTARY_ENCODERS) + keys[BTN_RE1].input(PIOB->PIO_PDSR & 0x40, BTN_RE1); +#endif + uint8_t enuk = KEY_MENU; uint8_t in = ~read_keys(); for (i = 1; i < 7; i++) { @@ -1106,6 +1152,7 @@ void usb_mode() // This might be replaced by a software reset // Any interrupts that have been enabled must be disabled here // BEFORE calling sam_boot() + stop_rotary_encoder(); endPdcUsartReceive() ; // Terminate any serial reception end_ppm_capture() ; end_spi() ; diff --git a/src/board_gruvin9x.cpp b/src/board_gruvin9x.cpp index d80f8f075..ee4c82ee8 100644 --- a/src/board_gruvin9x.cpp +++ b/src/board_gruvin9x.cpp @@ -106,11 +106,11 @@ inline void board_init() EICRA = (1<> 1) & 0x01; res = ((resh << 8) | resl); if((res == 0x180) | (res == 0x1C0)){ //button REb filter - keys[BTN_REa].input((res & 0x1C0) == 0x1C0, BTN_REa); + keys[BTN_RE1].input((res & 0x1C0) == 0x1C0, BTN_RE1); } else if((res & 0x100) == 0x100){ //rotary filter vpotToChange = res & 0xEF; vpot_mod_state = 1; @@ -275,9 +275,9 @@ void readKeysAndTrims() uint8_t enuk = KEY_MENU; #if !defined(EXTRA_ROTARY_ENCODERS) - keys[BTN_REa].input(~PIND & 0x20, BTN_REa); + keys[BTN_RE1].input(~PIND & 0x20, BTN_RE1); #endif //!EXTRA_ROTARY_ENCODERS - keys[BTN_REb].input(~PIND & 0x10, BTN_REb); + keys[BTN_RE2].input(~PIND & 0x10, BTN_RE2); uint8_t tin = ~PINL; uint8_t in; diff --git a/src/eeprom_avr.cpp b/src/eeprom_avr.cpp index acf81f7f2..f2b132ce2 100644 --- a/src/eeprom_avr.cpp +++ b/src/eeprom_avr.cpp @@ -39,9 +39,7 @@ uint8_t s_write_err = 0; // error reasons uint8_t s_sync_write = false; uint8_t s_eeDirtyMsk; -#if defined(PCBV4) && !defined(REV0) uint16_t s_eeDirtyTime10ms; -#endif RlcFile theFile; //used for any file operation EeFs eeFs; @@ -49,9 +47,7 @@ EeFs eeFs; void eeDirty(uint8_t msk) { s_eeDirtyMsk |= msk; -#if defined(PCBV4) && !defined(REV0) s_eeDirtyTime10ms = get_tmr10ms(); -#endif } uint16_t eeprom_pointer; @@ -164,13 +160,15 @@ static void EeFsFlush() uint16_t EeFsGetFree() { - uint16_t ret = 0; + int16_t ret = 0; uint8_t i = eeFs.freeList; - while( i ){ + while (i) { ret += BS-1; i = EeFsGetLink(i); } - return ret; + ret += eeFs.files[FILE_TMP].size; + ret -= eeFs.files[FILE_MODEL(g_eeGeneral.currModel)].size; + return (ret > 0 ? ret : 0); } /// free one or more blocks @@ -867,6 +865,7 @@ void eeLoadModel(uint8_t id) } if (pulsesStarted()) { + checkLowEEPROM(); checkTHR(); checkSwitches(); resumePulses(); diff --git a/src/eeprom_avr.h b/src/eeprom_avr.h index 9c3dded96..dec185d07 100644 --- a/src/eeprom_avr.h +++ b/src/eeprom_avr.h @@ -38,8 +38,10 @@ #if defined(PCBV4) && !defined(REV0) #define WRITE_DELAY_10MS 500 -extern uint16_t s_eeDirtyTime10ms; +#else +#define WRITE_DELAY_10MS 200 #endif +extern uint16_t s_eeDirtyTime10ms; // // bs=16 128 blocks verlust link:128 16files:16*8 128 sum 256 diff --git a/src/general_menus.cpp b/src/general_menus.cpp index b0a9ac2b5..0a819dc0f 100644 --- a/src/general_menus.cpp +++ b/src/general_menus.cpp @@ -760,11 +760,11 @@ void menuProcDiagKeys(uint8_t event) lcd_putcAtt(5*FW+2, y, t+'0', t); } -#if defined (PCBV4) +#if defined (ROTARY_ENCODERS) for(uint8_t i=0; i<2; i++) { uint8_t y = i*FH + FH; lcd_putsiAtt(14*FW, y, STR_RE1RE2, i, 0); - lcd_outdezNAtt(18*FW, y, g_rotenc[i], LEFT|(keyState((EnumKeys)(BTN_REa+i)) ? INVERS : 0)); + lcd_outdezNAtt(18*FW, y, g_rotenc[i], LEFT|(keyState((EnumKeys)(BTN_RE1+i)) ? INVERS : 0)); } #endif diff --git a/src/main_views.cpp b/src/main_views.cpp index 5832be35d..77d593d21 100644 --- a/src/main_views.cpp +++ b/src/main_views.cpp @@ -178,8 +178,8 @@ void menuMainView(uint8_t event) killEvents(KEY_DOWN); break; #if defined(ROTARY_ENCODERS) - case EVT_KEY_LONG(BTN_REa): - case EVT_KEY_LONG(BTN_REb): + case EVT_KEY_LONG(BTN_RE1): + case EVT_KEY_LONG(BTN_RE2): if (navigationRotaryEncoder(event)) { killEvents(event); if (s_inflight_value && !s_inflight_enable) { @@ -189,8 +189,8 @@ void menuMainView(uint8_t event) } } // no break - case EVT_KEY_BREAK(BTN_REa): - case EVT_KEY_BREAK(BTN_REb): + case EVT_KEY_BREAK(BTN_RE1): + case EVT_KEY_BREAK(BTN_RE2): s_inflight_enable = false; break; #endif diff --git a/src/menus.cpp b/src/menus.cpp index 3dd8d3efa..300d567fd 100644 --- a/src/menus.cpp +++ b/src/menus.cpp @@ -227,12 +227,12 @@ bool check(uint8_t event, uint8_t curr, const MenuFuncP *menuTab, uint8_t menuTa #if defined(ROTARY_ENCODERS) if (!(s_warning || s_sdcard_error || s_menu_count)) check_rotary_encoder(); - if (m_posVert < 0 && (event==EVT_KEY_FIRST(BTN_REa) || event==EVT_KEY_FIRST(BTN_REb) || event==EVT_KEY_FIRST(KEY_MENU))) { + if (m_posVert < 0 && (event==EVT_KEY_FIRST(BTN_RE1) || event==EVT_KEY_FIRST(BTN_RE2) || event==EVT_KEY_FIRST(KEY_MENU))) { popMenu(); killEvents(event); return false; } - if (g_eeGeneral.reNavigation && event == EVT_KEY_BREAK(BTN_REa+g_eeGeneral.reNavigation-1)) { + if (g_eeGeneral.reNavigation && event == EVT_KEY_BREAK(BTN_RE1+g_eeGeneral.reNavigation-1)) { if (s_editMode > 0 && (maxcol & ZCHAR)) { if (m_posHorz < maxcol-ZCHAR) { m_posHorz++; @@ -670,7 +670,7 @@ void checkInFlightIncDecModel(uint8_t event, int8_t *value, int8_t i_min, int8_t { *value = (((uint8_t)(*value)) & ((1 << bitshift) - 1)) + ((i_shift + checkIncDecModel(event, (((uint8_t)(*value)) >> bitshift)-i_shift, i_min, i_max)) << bitshift); - if (g_eeGeneral.reNavigation && event == EVT_KEY_LONG(BTN_REa+g_eeGeneral.reNavigation-1)) { + if (g_eeGeneral.reNavigation && event == EVT_KEY_LONG(BTN_RE1+g_eeGeneral.reNavigation-1)) { if (value == s_inflight_value) { s_inflight_value = NULL; } diff --git a/src/menus.h b/src/menus.h index f1984c42e..91255bc9f 100644 --- a/src/menus.h +++ b/src/menus.h @@ -82,8 +82,8 @@ void menuProcDebug(uint8_t event); #if defined(ROTARY_ENCODERS) extern int8_t scrollRE; extern int16_t p1valdiff; -#define IS_RE_NAVIGATION_EVT_TYPE(event, type) (g_eeGeneral.reNavigation && event==type(BTN_REa + g_eeGeneral.reNavigation - 1)) -#define IS_RE_NAVIGATION_EVT(event) (g_eeGeneral.reNavigation && (event&EVT_KEY_MASK)==(BTN_REa + g_eeGeneral.reNavigation - 1)) +#define IS_RE_NAVIGATION_EVT_TYPE(event, type) (g_eeGeneral.reNavigation && event==type(BTN_RE1 + g_eeGeneral.reNavigation - 1)) +#define IS_RE_NAVIGATION_EVT(event) (g_eeGeneral.reNavigation && (event&EVT_KEY_MASK)==(BTN_RE1 + g_eeGeneral.reNavigation - 1)) #else #define IS_RE_NAVIGATION_EVT_TYPE(event, type) (0) #define IS_RE_NAVIGATION_EVT(event) (0) diff --git a/src/model_menus.cpp b/src/model_menus.cpp index 7a6e2e50c..15a29c10d 100644 --- a/src/model_menus.cpp +++ b/src/model_menus.cpp @@ -269,13 +269,13 @@ void menuProcModelSelect(uint8_t event) } break; #if defined(ROTARY_ENCODERS) - case EVT_KEY_BREAK(BTN_REa): - case EVT_KEY_BREAK(BTN_REb): + case EVT_KEY_BREAK(BTN_RE1): + case EVT_KEY_BREAK(BTN_RE2): if (navigationRotaryEncoder(_event)) s_editMode = (s_editMode == 0 && sub == g_eeGeneral.currModel) ? -1 : 0; break; - case EVT_KEY_LONG(BTN_REa): - case EVT_KEY_LONG(BTN_REb): + case EVT_KEY_LONG(BTN_RE1): + case EVT_KEY_LONG(BTN_RE2): if (!navigationRotaryEncoder(_event)) break; #endif @@ -518,13 +518,13 @@ void EditName(uint8_t x, uint8_t y, char *name, uint8_t size, uint8_t event, boo if (next= WRITE_DELAY_10MS) -#else +#if defined(SIMU) #define TIME_TO_WRITE s_eeDirtyMsk +#else +#define TIME_TO_WRITE (s_eeDirtyMsk && (get_tmr10ms() - s_eeDirtyTime10ms) >= WRITE_DELAY_10MS) #endif #ifdef BOLD_FONT @@ -2858,14 +2863,15 @@ void moveTrimsToOffsets() // copy state of 3 primary to subtrim AUDIO_WARNING2(); } -#if defined (PCBV4) +#if defined(ROTARY_ENCODERS) // Rotary encoder interrupts volatile uint8_t g_rotenc[2] = {0}; #endif #ifndef SIMU -#if defined (PCBV4) +#if defined(PCBV4) && defined(ROTARY_ENCODERS) + #if !defined(EXTRA_ROTARY_ENCODERS) ISR(INT2_vect) { @@ -2877,7 +2883,7 @@ ISR(INT3_vect) uint8_t input = PIND & 0b00001100; if (input == 0 || input == 0b00001100) incRotaryEncoder(0, +1); } -#endif //EXTRA_ROTARY_ENCODERS +#endif //!EXTRA_ROTARY_ENCODERS ISR(INT5_vect) { @@ -2889,7 +2895,7 @@ ISR(INT6_vect) uint8_t input = PINE & 0b01100000; if (input == 0 || input == 0b01100000) incRotaryEncoder(1, -1); } -#endif //PCBV4 +#endif //PCBV4+ROTARY_ENCODERS #if !defined(PCBARM) extern unsigned char __bss_end ; diff --git a/src/open9x.h b/src/open9x.h index 142706fd0..9895aa8b1 100644 --- a/src/open9x.h +++ b/src/open9x.h @@ -310,8 +310,8 @@ enum EnumKeys { TRM_RH_DWN , TRM_RH_UP , #if defined(ROTARY_ENCODERS) - BTN_REa, - BTN_REb, + BTN_RE1, + BTN_RE2, #endif //SW_NC , //SW_ON , @@ -546,7 +546,7 @@ int16_t getRotaryEncoder(uint8_t idx); void incRotaryEncoder(uint8_t idx, int8_t inc); inline bool navigationRotaryEncoder(uint8_t event) { - return g_eeGeneral.reNavigation == ((event & EVT_KEY_MASK) - BTN_REa + 1); + return g_eeGeneral.reNavigation == ((event & EVT_KEY_MASK) - BTN_RE1 + 1); } #endif @@ -820,7 +820,7 @@ inline bool isFunctionActive(uint8_t func) extern char userDataDisplayBuf[TELEM_SCREEN_BUFFER_SIZE]; // text buffer for frsky telem. user data experiments #endif -#if defined (PCBV4) +#if defined (ROTARY_ENCODERS) // Global rotary encoder registers -- 8-bit, 0-255 extern volatile uint8_t g_rotenc[2]; #endif diff --git a/src/translations.cpp b/src/translations.cpp index d14ab71e4..eb3120ced 100644 --- a/src/translations.cpp +++ b/src/translations.cpp @@ -46,6 +46,7 @@ const pm_char STR_OPEN9X[] PROGMEM = ISTR(VBEEPMODE) #if defined(ROTARY_ENCODERS) ISTR(VRENAVIG) + ISTR(RE1RE2) #endif ISTR(VFILTERADC) ISTR(TRNMODE) @@ -88,7 +89,6 @@ const pm_char STR_OPEN9X[] PROGMEM = ISTR(DSM2MODE) #endif #if defined(PCBV4) - ISTR(RE1RE2) ISTR(DATETIME) #endif ; diff --git a/src/translations.h b/src/translations.h index 3bcaf9995..b73247688 100644 --- a/src/translations.h +++ b/src/translations.h @@ -71,7 +71,8 @@ extern const pm_char STR_OPEN9X[]; #endif #if defined(ROTARY_ENCODERS) #define OFS_VRENAVIG (OFS_VBEEPMODE + sizeof(TR_VBEEPMODE)) -#define OFS_VFILTERADC (OFS_VRENAVIG + sizeof(TR_VRENAVIG)) +#define OFS_RE1RE2 (OFS_VRENAVIG + sizeof(TR_VRENAVIG)) +#define OFS_VFILTERADC (OFS_RE1RE2 + sizeof(TR_RE1RE2)) #else #define OFS_VFILTERADC (OFS_VBEEPMODE + sizeof(TR_VBEEPMODE)) #endif @@ -127,8 +128,7 @@ extern const pm_char STR_OPEN9X[]; #define OFS_ENDDSM2 (OFS_VTMRMODES + sizeof(TR_VTMRMODES)) #endif #if defined(PCBV4) -#define OFS_RE1RE2 (OFS_ENDDSM2) -#define OFS_DATETIME (OFS_RE1RE2 + sizeof(TR_RE1RE2)) +#define OFS_DATETIME (OFS_ENDDSM2) #endif #define STR_OFFON (STR_OPEN9X + OFS_OFFON) @@ -138,7 +138,6 @@ extern const pm_char STR_OPEN9X[]; #define STR_VBEEPLEN (STR_OPEN9X + OFS_VBEEPLEN) #endif #define STR_VBEEPMODE (STR_OPEN9X + OFS_VBEEPMODE) -#define STR_VRENAVIG (STR_OPEN9X + OFS_VRENAVIG) #define STR_VFILTERADC (STR_OPEN9X + OFS_VFILTERADC) #define STR_TRNMODE (STR_OPEN9X + OFS_TRNMODE) #define STR_TRNCHN (STR_OPEN9X + OFS_TRNCHN) @@ -188,8 +187,12 @@ extern const pm_char STR_OPEN9X[]; #define STR_DSM2MODE (STR_OPEN9X + OFS_DSM2MODE) #endif -#if defined(PCBV4) +#if defined(ROTARY_ENCODERS) +#define STR_VRENAVIG (STR_OPEN9X + OFS_VRENAVIG) #define STR_RE1RE2 (STR_OPEN9X + OFS_RE1RE2) +#endif + +#if defined(PCBV4) #define STR_DATETIME (STR_OPEN9X + OFS_DATETIME) #endif