From e32da2ba4af387a1c7ce41cc194b1e0bacb0adf4 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Thu, 4 Dec 2014 20:12:23 +0100 Subject: [PATCH 01/19] No more waiting in lcdRefresh() for SPI transfer to finish --- radio/src/Makefile | 2 + radio/src/targets/taranis/lcd_driver.cpp | 69 +++++++++++++++--------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/radio/src/Makefile b/radio/src/Makefile index 606e77e94..d2b63b1a5 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -1217,6 +1217,8 @@ endif MAJ_VER = ${shell sh -c "grep \"MAJ_VERS\" opentx.h | cut -d\ -f3 | egrep -o \"[[:digit:]]\""} MIN_VER = ${shell sh -c "grep \"MIN_VERS\" opentx.h | cut -d\ -f3"} +.SECONDARY: + # Default target. all: begin gccversion sizebefore build sizeafter end diff --git a/radio/src/targets/taranis/lcd_driver.cpp b/radio/src/targets/taranis/lcd_driver.cpp index 77b667970..b05274575 100644 --- a/radio/src/targets/taranis/lcd_driver.cpp +++ b/radio/src/targets/taranis/lcd_driver.cpp @@ -17,9 +17,11 @@ #if defined(REVPLUS) #define CONTRAST_OFS 160 #define RESET_WAIT_DELAY_MS 300 //wait time after LCD reset before first command + #define WAIT_FOR_DMA_END() { while(lcd_busy) {}; } #else #define CONTRAST_OFS 5 #define RESET_WAIT_DELAY_MS 1300 //wait time after LCD reset before first command + #define WAIT_FOR_DMA_END() #endif #if defined(BOOT) @@ -64,24 +66,25 @@ void initLcdSpi() configure_pins( PIN_LCD_RST, PIN_OUTPUT | PIN_PORTD | PIN_PUSHPULL | PIN_OS25 | PIN_NO_PULLUP ) ; configure_pins( PIN_LCD_A0, PIN_OUTPUT | PIN_PORTC | PIN_PUSHPULL | PIN_OS50 | PIN_NO_PULLUP ) ; configure_pins( PIN_LCD_MOSI|PIN_LCD_CLK, PIN_PORTC | PIN_PUSHPULL | PIN_OS50 | PIN_NO_PULLUP | PIN_PER_6 | PIN_PERIPHERAL ) ; -} -void setupSPIdma() -{ - RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN ; // Enable DMA1 clock - // Chan 0, 8-bit wide, Medium priority, memory increments - DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA + + // NVIC_SetPriority( DMA1_Stream7_IRQn, 8 ) ; + NVIC_EnableIRQ(DMA1_Stream7_IRQn) ; + DMA1->HIFCR |= DMA_HIFCR_CTCIF7; //clear interrupt flag + DMA1->LISR |= DMA_HISR_TCIF7; //enable DMA TX end interrupt + + RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN ; // Enable DMA1 clock + + DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA DMA1->HIFCR = DMA_HIFCR_CTCIF7 | DMA_HIFCR_CHTIF7 | DMA_HIFCR_CTEIF7 | DMA_HIFCR_CDMEIF7 | DMA_HIFCR_CFEIF7 ; // Write ones to clear bits DMA1_Stream7->CR = DMA_SxCR_PL_0 | DMA_SxCR_MINC | DMA_SxCR_DIR_0 ; DMA1_Stream7->PAR = (uint32_t) &SPI3->DR ; DMA1_Stream7->M0AR = (uint32_t)displayBuf; DMA1_Stream7->FCR = 0x05 ; //DMA_SxFCR_DMDIS | DMA_SxFCR_FTH_0 ; DMA1_Stream7->NDTR = LCD_W*LCD_H/8*4 ; - -// NVIC_SetPriority( DMA1_Stream7_IRQn, 2 ) ; // Lower priority interrupt -// NVIC_EnableIRQ(DMA1_Stream7_IRQn) ; } + static void LCD_Init() { WriteCommand(0x2F); //Internal pump control @@ -174,35 +177,47 @@ void Set_Address(u8 x, u8 y) LCD_CLK_HIGH(); #if defined(REVPLUS) + +volatile bool lcd_busy; + void lcdRefresh(bool wait) { + //wait if previous DMA transfer still active + WAIT_FOR_DMA_END(); + lcd_busy = true; + Set_Address(0, 0); LCD_NCS_LOW(); LCD_A0_HIGH(); - setupSPIdma() ; - DMA1_Stream7->CR |= DMA_SxCR_EN ; // Enable DMA + DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA + DMA1->HIFCR = DMA_HIFCR_CTCIF7 | DMA_HIFCR_CHTIF7 | DMA_HIFCR_CTEIF7 | DMA_HIFCR_CDMEIF7 | DMA_HIFCR_CFEIF7 ; // Write ones to clear bits + DMA1_Stream7->CR |= DMA_SxCR_EN | DMA_SxCR_TCIE; // Enable DMA & tXe interrupt SPI3->CR2 |= SPI_CR2_TXDMAEN ; +} - while ( ( DMA1->HISR & DMA_HISR_TCIF7 ) == 0 ) { - // wait - } - +// #if !defined(SIMU) +extern "C" void DMA1_Stream7_IRQHandler() +{ + //clear interrupt flag + DMA1_Stream7->CR &= ~DMA_SxCR_TCIE ; // Stop interrupt + DMA1->HIFCR |= DMA_HIFCR_CTCIF7; // Clear interrupt flag SPI3->CR2 &= ~SPI_CR2_TXDMAEN ; - DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA - - while ( ( SPI3->SR & SPI_SR_TXE ) == 0 ) { - // wait - } // Last byte being sent + DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA while ( SPI3->SR & SPI_SR_BSY ) { - // wait + /* Wait for SPI to finis sending data + The DMA Tx End interrupt comes two bytes before the end of SPI transmission, + therefore we have to wait here. + */ } - LCD_NCS_HIGH(); + lcd_busy = false; } -#else +// #endif // #if !defined(SIMU) + +#else // #if defined(REVPLUS) void lcdRefresh() { for (uint32_t y=0; y Date: Thu, 4 Dec 2014 21:11:27 +0100 Subject: [PATCH 02/19] Dual-buffer for LCD --- radio/src/Makefile | 4 ++-- radio/src/lcd.h | 6 ++++++ radio/src/lcd_common.cpp | 11 ++++++++++- radio/src/targets/taranis/lcd_driver.cpp | 13 +++++++++---- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/radio/src/Makefile b/radio/src/Makefile index d2b63b1a5..8237687e9 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -678,11 +678,11 @@ ifeq ($(PCB), TARANIS) CPPDEFS = -DREV4a SWR = YES else ifeq ($(PCBREV), REVPLUS) - CPPDEFS = -DREVPLUS + CPPDEFS = -DREVPLUS -DLCD_DUAL_BUFFER HAPTIC = YES SWR = NO else ifeq ($(PCBREV), REV9E) - CPPDEFS = -DREVPLUS -DREV9E + CPPDEFS = -DREVPLUS -DREV9E -DLCD_DUAL_BUFFER HAPTIC = YES SWR = YES else diff --git a/radio/src/lcd.h b/radio/src/lcd.h index c6f71e7e1..b28d18647 100644 --- a/radio/src/lcd.h +++ b/radio/src/lcd.h @@ -161,7 +161,13 @@ #define DISPLAY_BUF_SIZE (LCD_W*((LCD_H+7)/8)) #endif +#if defined(LCD_DUAL_BUFFER) +extern display_t displayBuf1[DISPLAY_BUF_SIZE]; +extern display_t displayBuf2[DISPLAY_BUF_SIZE]; +extern display_t * displayBuf; +#else extern display_t displayBuf[DISPLAY_BUF_SIZE]; +#endif extern coord_t lcdLastPos; extern coord_t lcdNextPos; diff --git a/radio/src/lcd_common.cpp b/radio/src/lcd_common.cpp index cd05611a1..e3adc9cc4 100644 --- a/radio/src/lcd_common.cpp +++ b/radio/src/lcd_common.cpp @@ -36,11 +36,20 @@ #include "opentx.h" + +#if defined(LCD_DUAL_BUFFER) +display_t displayBuf1[DISPLAY_BUF_SIZE]; +display_t displayBuf2[DISPLAY_BUF_SIZE]; +display_t * displayBuf = displayBuf1; +#define DISPLAY_BUFER_SIZE sizeof(displayBuf1) +#else display_t displayBuf[DISPLAY_BUF_SIZE]; +#define DISPLAY_BUFER_SIZE sizeof(displayBuf) +#endif void lcd_clear() { - memset(displayBuf, 0, sizeof(displayBuf)); + memset(displayBuf, 0, DISPLAY_BUFER_SIZE); } coord_t lcdLastPos; diff --git a/radio/src/targets/taranis/lcd_driver.cpp b/radio/src/targets/taranis/lcd_driver.cpp index b05274575..c16584189 100644 --- a/radio/src/targets/taranis/lcd_driver.cpp +++ b/radio/src/targets/taranis/lcd_driver.cpp @@ -193,11 +193,17 @@ void lcdRefresh(bool wait) DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA DMA1->HIFCR = DMA_HIFCR_CTCIF7 | DMA_HIFCR_CHTIF7 | DMA_HIFCR_CTEIF7 | DMA_HIFCR_CDMEIF7 | DMA_HIFCR_CFEIF7 ; // Write ones to clear bits + +#if defined(LCD_DUAL_BUFFER) + //switch LCD buffer + DMA1_Stream7->M0AR = (uint32_t)displayBuf; + displayBuf = (displayBuf == displayBuf1) ? displayBuf2 : displayBuf1; +#endif + DMA1_Stream7->CR |= DMA_SxCR_EN | DMA_SxCR_TCIE; // Enable DMA & tXe interrupt SPI3->CR2 |= SPI_CR2_TXDMAEN ; } -// #if !defined(SIMU) extern "C" void DMA1_Stream7_IRQHandler() { //clear interrupt flag @@ -207,15 +213,14 @@ extern "C" void DMA1_Stream7_IRQHandler() DMA1_Stream7->CR &= ~DMA_SxCR_EN ; // Disable DMA while ( SPI3->SR & SPI_SR_BSY ) { - /* Wait for SPI to finis sending data - The DMA Tx End interrupt comes two bytes before the end of SPI transmission, + /* Wait for SPI to finish sending data + The DMA TX End interrupt comes two bytes before the end of SPI transmission, therefore we have to wait here. */ } LCD_NCS_HIGH(); lcd_busy = false; } -// #endif // #if !defined(SIMU) #else // #if defined(REVPLUS) void lcdRefresh() From 271dae71f76949849c7abc8962516dcc8ed31777 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Thu, 4 Dec 2014 21:23:09 +0100 Subject: [PATCH 03/19] Try to keep perMain() execution period of 20ms --- radio/src/tasks_arm.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 828e851b9..cafc9c1fb 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -147,6 +147,8 @@ void mixerTask(void * pdata) } } +#define MENU_TASK_PERIOD_TICKS 10 // 20ms + extern void opentxClose(); extern void opentxInit(); void menusTask(void * pdata) @@ -154,9 +156,16 @@ void menusTask(void * pdata) opentxInit(); while (pwrCheck() != e_power_off) { + U64 start = CoGetOSTime(); perMain(); // TODO remove completely massstorage from sky9x firmware - CoTickDelay(5); // 5*2ms for now + U32 runtime = (U32)(CoGetOSTime() - start); + if (runtime >= MENU_TASK_PERIOD_TICKS) { + //no delay + } + else { + CoTickDelay(MENU_TASK_PERIOD_TICKS - runtime); // 5*2ms for now + } } lcd_clear(); From 5566439e7663499fc9bbee92561865617d6cbf54 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Thu, 4 Dec 2014 22:17:06 +0100 Subject: [PATCH 04/19] Fixed simu build --- radio/src/lcd.h | 2 ++ radio/src/lcd_common.cpp | 2 -- radio/src/simu.cpp | 2 +- radio/src/tasks_arm.cpp | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/radio/src/lcd.h b/radio/src/lcd.h index b28d18647..8f5a16801 100644 --- a/radio/src/lcd.h +++ b/radio/src/lcd.h @@ -168,9 +168,11 @@ extern display_t * displayBuf; #else extern display_t displayBuf[DISPLAY_BUF_SIZE]; #endif + extern coord_t lcdLastPos; extern coord_t lcdNextPos; +#define DISPLAY_BUFER_SIZE (sizeof(display_t)*DISPLAY_BUF_SIZE) #define DISPLAY_END (displayBuf + DISPLAY_BUF_SIZE) #define ASSERT_IN_DISPLAY(p) assert((p) >= displayBuf && (p) < DISPLAY_END) diff --git a/radio/src/lcd_common.cpp b/radio/src/lcd_common.cpp index e3adc9cc4..c46f8fa4f 100644 --- a/radio/src/lcd_common.cpp +++ b/radio/src/lcd_common.cpp @@ -41,10 +41,8 @@ display_t displayBuf1[DISPLAY_BUF_SIZE]; display_t displayBuf2[DISPLAY_BUF_SIZE]; display_t * displayBuf = displayBuf1; -#define DISPLAY_BUFER_SIZE sizeof(displayBuf1) #else display_t displayBuf[DISPLAY_BUF_SIZE]; -#define DISPLAY_BUFER_SIZE sizeof(displayBuf) #endif void lcd_clear() diff --git a/radio/src/simu.cpp b/radio/src/simu.cpp index a0618d3e7..abdcd25b7 100644 --- a/radio/src/simu.cpp +++ b/radio/src/simu.cpp @@ -85,7 +85,7 @@ Open9xSim::Open9xSim(FXApp* a): FXMainWindow(a, "OpenTX Simu", NULL, NULL, DECOR_ALL, 20, 90, 0, 0) { firstTime = true; - memset(displayBuf, 0, sizeof(displayBuf)); + memset(displayBuf, 0, DISPLAY_BUFER_SIZE); bmp = new FXPPMImage(getApp(),NULL,IMAGE_OWNED|IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP, W2, H2); FXHorizontalFrame *hf11=new FXHorizontalFrame(this,LAYOUT_CENTER_X); diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index cafc9c1fb..8472ac56f 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -117,6 +117,8 @@ uint32_t stack_free(uint32_t tid) return i*4; } +#if !defined(SIMU) + void mixerTask(void * pdata) { s_pulses_paused = true; @@ -209,3 +211,5 @@ void tasksStart() CoStartOS(); } + +#endif // #if !defined(SIMU) \ No newline at end of file From 5c8e8ebbe5c280588023901467b555834720622e Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Fri, 5 Dec 2014 22:21:29 +0100 Subject: [PATCH 05/19] Separated perMain() for ARM and other platforms --- radio/src/eeprom_common.h | 2 + radio/src/opentx.cpp | 442 ++++++-------------- radio/src/opentx.h | 5 + radio/src/targets/simu/simpgmspace.cpp | 6 + radio/src/targets/simu/simpgmspace.h | 1 - radio/src/targets/taranis/board_taranis.cpp | 89 +++- radio/src/targets/taranis/board_taranis.h | 5 + radio/src/tasks_arm.cpp | 137 +++++- 8 files changed, 378 insertions(+), 309 deletions(-) diff --git a/radio/src/eeprom_common.h b/radio/src/eeprom_common.h index 1997c930e..463c1fd65 100644 --- a/radio/src/eeprom_common.h +++ b/radio/src/eeprom_common.h @@ -46,6 +46,8 @@ #define WRITE_DELAY_10MS 200 #endif +#define TIME_TO_WRITE() (s_eeDirtyMsk && (tmr10ms_t)(get_tmr10ms() - s_eeDirtyTime10ms) >= (tmr10ms_t)WRITE_DELAY_10MS) + extern uint8_t s_eeDirtyMsk; extern tmr10ms_t s_eeDirtyTime10ms; diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 86c99c6f4..c539e5780 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -87,56 +87,6 @@ uint8_t g_tmr1Latency_min; uint16_t lastMixerDuration; #endif -#if defined(PCBTARANIS) -uint8_t currentTrainerMode = 0xff; - -void checkTrainerSettings() -{ - uint8_t requiredTrainerMode = g_model.trainerMode; - if (requiredTrainerMode != currentTrainerMode) { - switch (currentTrainerMode) { - case TRAINER_MODE_MASTER: - stop_trainer_capture(); - break; - case TRAINER_MODE_SLAVE: - stop_trainer_ppm(); - break; - case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: - stop_cppm_on_heartbeat_capture() ; - break; - case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: - stop_sbus_on_heartbeat_capture() ; - break; - case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: - uart3Stop(); - } - - currentTrainerMode = requiredTrainerMode; - switch (requiredTrainerMode) { - case TRAINER_MODE_SLAVE: - init_trainer_ppm(); - break; - case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: - init_cppm_on_heartbeat_capture() ; - break; - case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: - init_sbus_on_heartbeat_capture() ; - break; - case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: - if (g_eeGeneral.uart3Mode == UART_MODE_SBUS_TRAINER) { - uart3SbusInit(); - break; - } - // no break - default: - // master is default - init_trainer_capture(); - break; - } - } -} -#endif - uint8_t unexpectedShutdown = 0; /* AVR: mixer duration in 1/16ms */ @@ -209,7 +159,6 @@ void per10ms() watchdogTimeout -= 1; wdt_reset(); // Retrigger hardware watchdog } - Tenms |= 1 ; // 10 mS has passed #endif if (lightOffCounter) lightOffCounter--; @@ -1699,10 +1648,6 @@ uint8_t mSwitchDuration[1+NUM_ROTARY_ENCODERS] = { 0 }; #define CFN_PRESSLONG_DURATION 100 #endif -#if defined(CPUARM) -uint8_t currentSpeakerVolume = 255; -uint8_t requiredSpeakerVolume; -#endif uint8_t s_mixer_first_run_done = false; @@ -1995,7 +1940,6 @@ void doMixerCalculations() s_mixer_first_run_done = true; } -#define TIME_TO_WRITE() (s_eeDirtyMsk && (tmr10ms_t)(get_tmr10ms() - s_eeDirtyTime10ms) >= (tmr10ms_t)WRITE_DELAY_10MS) #if defined(NAVIGATION_STICKS) @@ -2129,205 +2073,11 @@ void opentxClose() #endif -#if defined(USB_JOYSTICK) && defined(PCBTARANIS) && !defined(SIMU) -extern USB_OTG_CORE_HANDLE USB_OTG_dev; - -/* - Prepare and send new USB data packet - - The format of HID_Buffer is defined by - USB endpoint description can be found in - file usb_hid_joystick.c, variable HID_JOYSTICK_ReportDesc -*/ -void usbJoystickUpdate(void) -{ - static uint8_t HID_Buffer[HID_IN_PACKET]; - - //buttons - HID_Buffer[0] = 0; //buttons - for (int i = 0; i < 8; ++i) { - if ( channelOutputs[i+8] > 0 ) { - HID_Buffer[0] |= (1 << i); - } - } - - //analog values - //uint8_t * p = HID_Buffer + 1; - for (int i = 0; i < 8; ++i) { - int16_t value = channelOutputs[i] / 8; - if ( value > 127 ) value = 127; - else if ( value < -127 ) value = -127; - HID_Buffer[i+1] = static_cast(value); - } - - USBD_HID_SendReport (&USB_OTG_dev, HID_Buffer, HID_IN_PACKET ); -} - -#endif //#if defined(USB_JOYSTICK) && defined(PCBTARANIS) && !defined(SIMU) - - -void perMain() -{ -#if defined(SIMU) - doMixerCalculations(); -#elif !defined(CPUARM) - uint16_t t0 = getTmr16KHz(); - int16_t delta = (nextMixerEndTime - lastMixerDuration) - t0; - if (delta > 0 && delta < MAX_MIXER_DELTA) { -#if defined(PCBSTD) && defined(ROTARY_ENCODER_NAVIGATION) - rotencPoll(); -#endif - - // @@@ open.20.fsguruh - // SLEEP(); // wouldn't that make sense? should save a lot of battery power!!! -/* for future use; currently very very beta... */ -#if defined(POWER_SAVE) - ADCSRA&=0x7F; // disable ADC for power saving - ACSR&=0xF7; // disable ACIE Interrupts - ACSR|=0x80; // disable Analog Comparator - // maybe we disable here a lot more hardware components in future to save even more power - - - - MCUCR|=0x20; // enable Sleep (bit5) - // MCUCR|=0x28; // enable Sleep (bit5) enable ADC Noise Reduction (bit3) - // first tests showed: simple sleep would reduce cpu current from 40.5mA to 32.0mA - // noise reduction sleep would reduce it down to 28.5mA; However this would break pulses in theory - // however with standard module, it will need about 95mA. Therefore the drop to 88mA is not much noticable - do { - asm volatile(" sleep \n\t"); // if _SLEEP() is not defined use this - t0=getTmr16KHz(); - delta= (nextMixerEndTime - lastMixerDuration) - t0; - } while ((delta>0) && (delta maxMixerDuration) maxMixerDuration = t0; -#endif - -// TODO same code here + integrate the timer which could be common -#if defined(CPUARM) - if (!Tenms) return; - Tenms = 0 ; -#endif - -#if defined(PCBSKY9X) - Current_accumulator += Current_analogue ; - static uint32_t OneSecTimer; - if (++OneSecTimer >= 100) { - OneSecTimer -= 100 ; - Current_used += Current_accumulator / 100 ; // milliAmpSeconds (but scaled) - Current_accumulator = 0 ; - } -#endif - -#if defined(CPUARM) - if (currentSpeakerVolume != requiredSpeakerVolume) { - currentSpeakerVolume = requiredSpeakerVolume; - setVolume(currentSpeakerVolume); - } -#endif - -#if defined(MODULE_ALWAYS_SEND_PULSES) - if (startupWarningState < STARTUP_WARNING_DONE) { - // don't do menu's until throttle and switch warnings are handled - return; - } -#endif - - if (!usbPlugged()) { - // TODO merge these 2 branches -#if defined(PCBSKY9X) - if (Eeprom32_process_state != E32_IDLE) - ee32_process(); - else if (TIME_TO_WRITE()) - eeCheck(false); -#elif defined(CPUARM) - if (theFile.isWriting()) - theFile.nextWriteStep(); - else if (TIME_TO_WRITE()) - eeCheck(false); -#else - if (!eeprom_buffer_size) { - if (theFile.isWriting()) - theFile.nextWriteStep(); - else if (TIME_TO_WRITE()) - eeCheck(false); - } -#endif - } - -#if defined(SDCARD) - sdMountPoll(); - writeLogs(); -#endif - -#if defined(CPUARM) && defined(SIMU) - checkTrims(); -#endif - -#if defined(CPUARM) - uint8_t evt = getEvent(false); -#else - uint8_t evt = getEvent(); - evt = checkTrim(evt); -#endif - - if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on - - checkBacklight(); - -#if !defined(CPUARM) && (defined(FRSKY) || defined(MAVLINK)) - telemetryWakeup(); -#endif - -#if defined(PCBTARANIS) - checkTrainerSettings(); -#endif - -#if defined(PCBTARANIS) && !defined(SIMU) - static bool usbStarted = false; - if (!usbStarted && usbPlugged()) { -#if defined(USB_MASS_STORAGE) - opentxClose(); -#endif - usbStart(); -#if defined(USB_MASS_STORAGE) - usbPluggedIn(); -#endif - usbStarted = true; - } - -#if defined(USB_JOYSTICK) - if (usbStarted) { - if (!usbPlugged()) { - //disable USB - usbStop(); - usbStarted = false; - } - else { - usbJoystickUpdate(); - } - } -#endif - -#endif //#if defined(PCBTARANIS) && !defined(SIMU) #if defined(NAVIGATION_STICKS) +uint8_t getSticksNavigationEvent() +{ + uint8_t evt = 0; if (StickScrollAllowed) { if ( StickScrollTimer ) { static uint8_t repeater; @@ -2389,60 +2139,12 @@ void perMain() StickScrollTimer = 0; // Seconds } StickScrollAllowed = 1 ; + return evt; +} #endif -#if defined(USB_MASS_STORAGE) - if (usbPlugged()) { - lcd_clear(); - menuMainView(0); - } - else -#endif - { - const char *warn = s_warning; - uint8_t menu = s_menu_count; - - if (!LUA_STANDALONE_SCRIPT_RUNNING()) { - lcd_clear(); - if (menuEvent) { - m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; - m_posHorz = 0; - evt = menuEvent; - menuEvent = 0; - AUDIO_MENUS(); - } - g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); - } - -#if defined(LUA) - luaTask(evt); -#endif - - if (!LUA_STANDALONE_SCRIPT_RUNNING()) { - if (warn) DISPLAY_WARNING(evt); -#if defined(NAVIGATION_MENUS) - if (menu) { - const char * result = displayMenu(evt); - if (result) { - menuHandler(result); - putEvent(EVT_MENU_UP); - } - } -#endif - } - } - - drawStatusLine(); - - lcdRefresh(LCD_REFRESH_DONT_WAIT); - - if (SLAVE_MODE()) { - JACK_PPM_OUT(); - } - else { - JACK_PPM_IN(); - } - +void batteryCheck() +{ static uint8_t counter = 0; if (g_menuStack[g_menuStackPtr] == menuGeneralDiagAna) { g_vbat100mV = 0; @@ -2506,8 +2208,138 @@ void perMain() #endif } } + } +#if !defined(CPUARM) +void perMain() +{ +#if defined(SIMU) + doMixerCalculations(); +#endif + uint16_t t0 = getTmr16KHz(); + int16_t delta = (nextMixerEndTime - lastMixerDuration) - t0; + if (delta > 0 && delta < MAX_MIXER_DELTA) { +#if defined(PCBSTD) && defined(ROTARY_ENCODER_NAVIGATION) + rotencPoll(); +#endif + + // @@@ open.20.fsguruh + // SLEEP(); // wouldn't that make sense? should save a lot of battery power!!! +/* for future use; currently very very beta... */ +#if defined(POWER_SAVE) + ADCSRA&=0x7F; // disable ADC for power saving + ACSR&=0xF7; // disable ACIE Interrupts + ACSR|=0x80; // disable Analog Comparator + // maybe we disable here a lot more hardware components in future to save even more power + + + + MCUCR|=0x20; // enable Sleep (bit5) + // MCUCR|=0x28; // enable Sleep (bit5) enable ADC Noise Reduction (bit3) + // first tests showed: simple sleep would reduce cpu current from 40.5mA to 32.0mA + // noise reduction sleep would reduce it down to 28.5mA; However this would break pulses in theory + // however with standard module, it will need about 95mA. Therefore the drop to 88mA is not much noticable + do { + asm volatile(" sleep \n\t"); // if _SLEEP() is not defined use this + t0=getTmr16KHz(); + delta= (nextMixerEndTime - lastMixerDuration) - t0; + } while ((delta>0) && (delta maxMixerDuration) maxMixerDuration = t0; + +#if defined(MODULE_ALWAYS_SEND_PULSES) + if (startupWarningState < STARTUP_WARNING_DONE) { + // don't do menu's until throttle and switch warnings are handled + return; + } +#endif + + if (!eeprom_buffer_size) { + if (theFile.isWriting()) + theFile.nextWriteStep(); + else if (TIME_TO_WRITE()) + eeCheck(false); + } + +#if defined(SDCARD) + sdMountPoll(); + writeLogs(); +#endif + + uint8_t evt = getEvent(); + evt = checkTrim(evt); + + if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on + checkBacklight(); + +#if (defined(FRSKY) || defined(MAVLINK)) + telemetryWakeup(); +#endif + +#if defined(NAVIGATION_STICKS) + uint8_t sticks_evt = getSticksNavigationEvent(); + if (sticks_evt) evt = sticks_evt; +#endif + + const char *warn = s_warning; + uint8_t menu = s_menu_count; + + lcd_clear(); + if (menuEvent) { + m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; + m_posHorz = 0; + evt = menuEvent; + menuEvent = 0; + AUDIO_MENUS(); + } + g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + + + if (warn) DISPLAY_WARNING(evt); +#if defined(NAVIGATION_MENUS) + if (menu) { + const char * result = displayMenu(evt); + if (result) { + menuHandler(result); + putEvent(EVT_MENU_UP); + } + } +#endif + + drawStatusLine(); + + lcdRefresh(); + + if (SLAVE_MODE()) { + JACK_PPM_OUT(); + } + else { + JACK_PPM_IN(); + } + + batteryCheck(); +} +#endif // #if !defined(CPUARM) + + + int16_t g_ppmIns[NUM_TRAINER]; uint8_t ppmInState = 0; // 0=unsync 1..8= wait for value i-1 uint8_t ppmInValid = 0; diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 84394dd05..3ae32273d 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -726,7 +726,12 @@ void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms); void evalMixes(uint8_t tick10ms); void doMixerCalculations(); +#if defined(CPUARM) +void perMainArm(); +void checkTrims(); +#else void perMain(); +#endif NOINLINE void per10ms(); getvalue_t getValue(mixsrc_t i); diff --git a/radio/src/targets/simu/simpgmspace.cpp b/radio/src/targets/simu/simpgmspace.cpp index 71142b53c..487f661f6 100644 --- a/radio/src/targets/simu/simpgmspace.cpp +++ b/radio/src/targets/simu/simpgmspace.cpp @@ -339,7 +339,13 @@ void *main_thread(void *) s_current_protocol[0] = 0; while (main_thread_running) { +#if defined(CPUARM) + doMixerCalculations(); + checkTrims(); + perMainArm(); +#else perMain(); +#endif sleep(10/*ms*/); } diff --git a/radio/src/targets/simu/simpgmspace.h b/radio/src/targets/simu/simpgmspace.h index 1c7e00585..b6a1be62e 100644 --- a/radio/src/targets/simu/simpgmspace.h +++ b/radio/src/targets/simu/simpgmspace.h @@ -336,7 +336,6 @@ extern void rxPdcUsart( void (*pChProcess)(uint8_t x) ); #define asm(x) #if defined(CPUARM) -extern volatile uint32_t Tenms; extern uint32_t Master_frequency; #define NVIC_EnableIRQ(x) #define NVIC_DisableIRQ(x) diff --git a/radio/src/targets/taranis/board_taranis.cpp b/radio/src/targets/taranis/board_taranis.cpp index d162e7a83..3ef899069 100644 --- a/radio/src/targets/taranis/board_taranis.cpp +++ b/radio/src/targets/taranis/board_taranis.cpp @@ -46,8 +46,6 @@ extern "C" { } #endif -volatile uint32_t Tenms ; // TODO to remove everywhere / use a #define - void watchdogInit(unsigned int duration) { IWDG->KR = 0x5555 ; // Unlock registers @@ -150,3 +148,90 @@ void boardInit() } #endif + + +#if defined(USB_JOYSTICK) && !defined(SIMU) +extern USB_OTG_CORE_HANDLE USB_OTG_dev; + +/* + Prepare and send new USB data packet + + The format of HID_Buffer is defined by + USB endpoint description can be found in + file usb_hid_joystick.c, variable HID_JOYSTICK_ReportDesc +*/ +void usbJoystickUpdate(void) +{ + static uint8_t HID_Buffer[HID_IN_PACKET]; + + //buttons + HID_Buffer[0] = 0; //buttons + for (int i = 0; i < 8; ++i) { + if ( channelOutputs[i+8] > 0 ) { + HID_Buffer[0] |= (1 << i); + } + } + + //analog values + //uint8_t * p = HID_Buffer + 1; + for (int i = 0; i < 8; ++i) { + int16_t value = channelOutputs[i] / 8; + if ( value > 127 ) value = 127; + else if ( value < -127 ) value = -127; + HID_Buffer[i+1] = static_cast(value); + } + + USBD_HID_SendReport (&USB_OTG_dev, HID_Buffer, HID_IN_PACKET ); +} + +#endif //#if defined(USB_JOYSTICK) && defined(PCBTARANIS) && !defined(SIMU) + + +uint8_t currentTrainerMode = 0xff; + +void checkTrainerSettings() +{ + uint8_t requiredTrainerMode = g_model.trainerMode; + if (requiredTrainerMode != currentTrainerMode) { + switch (currentTrainerMode) { + case TRAINER_MODE_MASTER: + stop_trainer_capture(); + break; + case TRAINER_MODE_SLAVE: + stop_trainer_ppm(); + break; + case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: + stop_cppm_on_heartbeat_capture() ; + break; + case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: + stop_sbus_on_heartbeat_capture() ; + break; + case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: + uart3Stop(); + } + + currentTrainerMode = requiredTrainerMode; + switch (requiredTrainerMode) { + case TRAINER_MODE_SLAVE: + init_trainer_ppm(); + break; + case TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE: + init_cppm_on_heartbeat_capture() ; + break; + case TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE: + init_sbus_on_heartbeat_capture() ; + break; + case TRAINER_MODE_MASTER_BATTERY_COMPARTMENT: + if (g_eeGeneral.uart3Mode == UART_MODE_SBUS_TRAINER) { + uart3SbusInit(); + break; + } + // no break + default: + // master is default + init_trainer_capture(); + break; + } + } +} + diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index d09e81782..b8d4719b4 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -277,5 +277,10 @@ void uart3Stop(void); #define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE) +#if defined(USB_JOYSTICK) && !defined(SIMU) +void usbJoystickUpdate(void); +#endif + +void checkTrainerSettings(); #endif diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 8472ac56f..10f1871c5 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -36,6 +36,140 @@ #include "opentx.h" + +static uint8_t currentSpeakerVolume = 255; +uint8_t requiredSpeakerVolume; + +extern void batteryCheck(); + +void perMainArm() +{ +#if defined(PCBSKY9X) + Current_accumulator += Current_analogue ; + static uint32_t OneSecTimer; + if (++OneSecTimer >= 100) { + OneSecTimer -= 100 ; + Current_used += Current_accumulator / 100 ; // milliAmpSeconds (but scaled) + Current_accumulator = 0 ; + } +#endif + + if (currentSpeakerVolume != requiredSpeakerVolume) { + currentSpeakerVolume = requiredSpeakerVolume; + setVolume(currentSpeakerVolume); + } + + if (!usbPlugged()) { + // TODO merge these 2 branches +#if defined(PCBSKY9X) + if (Eeprom32_process_state != E32_IDLE) + ee32_process(); + else if (TIME_TO_WRITE()) + eeCheck(false); +#else + if (theFile.isWriting()) + theFile.nextWriteStep(); + else if (TIME_TO_WRITE()) + eeCheck(false); +#endif + } + + sdMountPoll(); + writeLogs(); + + uint8_t evt = getEvent(false); + if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on + checkBacklight(); + +#if defined(PCBTARANIS) + checkTrainerSettings(); +#endif + +#if defined(PCBTARANIS) && !defined(SIMU) + static bool usbStarted = false; + if (!usbStarted && usbPlugged()) { +#if defined(USB_MASS_STORAGE) + opentxClose(); +#endif + usbStart(); +#if defined(USB_MASS_STORAGE) + usbPluggedIn(); +#endif + usbStarted = true; + } + +#if defined(USB_JOYSTICK) + if (usbStarted) { + if (!usbPlugged()) { + //disable USB + usbStop(); + usbStarted = false; + } + else { + usbJoystickUpdate(); + } + } +#endif +#endif //#if defined(PCBTARANIS) && !defined(SIMU) + +#if defined(NAVIGATION_STICKS) + uint8_t sticks_evt = getSticksNavigationEvent(); + if (sticks_evt) evt = sticks_evt; +#endif + +#if defined(USB_MASS_STORAGE) + if (usbPlugged()) { + lcd_clear(); + menuMainView(0); + } + else +#endif + { + const char *warn = s_warning; + uint8_t menu = s_menu_count; + + if (!LUA_STANDALONE_SCRIPT_RUNNING()) { + lcd_clear(); + if (menuEvent) { + m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; + m_posHorz = 0; + evt = menuEvent; + menuEvent = 0; + AUDIO_MENUS(); + } + g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + } + +#if defined(LUA) + luaTask(evt); +#endif + + if (!LUA_STANDALONE_SCRIPT_RUNNING()) { + if (warn) DISPLAY_WARNING(evt); + if (menu) { + const char * result = displayMenu(evt); + if (result) { + menuHandler(result); + putEvent(EVT_MENU_UP); + } + } + } + } + + drawStatusLine(); + + lcdRefresh(); + + if (SLAVE_MODE()) { + JACK_PPM_OUT(); + } + else { + JACK_PPM_IN(); + } + + batteryCheck(); +} + #define MENUS_STACK_SIZE 2000 #define MIXER_STACK_SIZE 500 #define AUDIO_STACK_SIZE 500 @@ -153,13 +287,14 @@ void mixerTask(void * pdata) extern void opentxClose(); extern void opentxInit(); + void menusTask(void * pdata) { opentxInit(); while (pwrCheck() != e_power_off) { U64 start = CoGetOSTime(); - perMain(); + perMainArm(); // TODO remove completely massstorage from sky9x firmware U32 runtime = (U32)(CoGetOSTime() - start); if (runtime >= MENU_TASK_PERIOD_TICKS) { From fac8571c29f1df8fe9e0365f57e11119dc33b1c1 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 6 Dec 2014 13:13:51 +0100 Subject: [PATCH 06/19] Refactoring of perMainArm(), hw specific stuff moved to targets --- radio/src/opentx.cpp | 7 -- radio/src/targets/sky9x/board_sky9x.cpp | 32 ++++++++ radio/src/targets/sky9x/board_sky9x.h | 7 +- radio/src/targets/taranis/board_taranis.h | 4 +- radio/src/tasks_arm.cpp | 95 +++++++++++------------ 5 files changed, 83 insertions(+), 62 deletions(-) diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index c539e5780..b0dd96212 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -1352,13 +1352,6 @@ uint8_t checkTrim(uint8_t event) #endif } -#if defined(PCBSKY9X) && !defined(REVA) -uint16_t Current_analogue; -uint16_t Current_max; -uint32_t Current_accumulator; -uint32_t Current_used; -#endif - #if !defined(SIMU) static uint16_t s_anaFilt[NUMBER_ANALOG]; #endif diff --git a/radio/src/targets/sky9x/board_sky9x.cpp b/radio/src/targets/sky9x/board_sky9x.cpp index 40cb33aa1..dcbdac3bf 100644 --- a/radio/src/targets/sky9x/board_sky9x.cpp +++ b/radio/src/targets/sky9x/board_sky9x.cpp @@ -42,6 +42,13 @@ volatile uint32_t Tenms ; volatile uint8_t lcdLock; volatile uint32_t lcdInputs; +#if !defined(REVA) +uint16_t Current_analogue; +uint16_t Current_max; +uint32_t Current_accumulator; +uint32_t Current_used; +#endif + extern "C" void sam_boot( void ) ; // Prototype @@ -672,3 +679,28 @@ void setSticksGain(uint8_t gains) padc->ADC_CGR = gain; padc->ADC_COR = offset; } + + +#if !defined(REVA) +void calcConsumption() +{ + Current_accumulator += Current_analogue ; + static uint32_t OneSecTimer; + if (++OneSecTimer >= 100) { + OneSecTimer -= 100 ; + Current_used += Current_accumulator / 100 ; // milliAmpSeconds (but scaled) + Current_accumulator = 0 ; + } +} +#endif + +void checkTrainerSettings() +{ + if (SLAVE_MODE()) { + PIOC->PIO_PDR = PIO_PC22; + } + else { + PIOC->PIO_PER = PIO_PC22; + } +} + diff --git a/radio/src/targets/sky9x/board_sky9x.h b/radio/src/targets/sky9x/board_sky9x.h index 5d7e360bc..f0e6078a4 100644 --- a/radio/src/targets/sky9x/board_sky9x.h +++ b/radio/src/targets/sky9x/board_sky9x.h @@ -168,11 +168,14 @@ extern uint16_t Current_max; extern uint32_t Current_accumulator; extern uint32_t Current_used; extern uint16_t sessionTimer; +void calcConsumption(); #endif #define SLAVE_MODE() (pwrCheck() == e_power_trainer) -#define JACK_PPM_OUT() PIOC->PIO_PDR = PIO_PC22 -#define JACK_PPM_IN() PIOC->PIO_PER = PIO_PC22 +// #define JACK_PPM_OUT() PIOC->PIO_PDR = PIO_PC22 +// #define JACK_PPM_IN() PIOC->PIO_PER = PIO_PC22 +void checkTrainerSettings(); + void setSticksGain(uint8_t gains); diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index b8d4719b4..98170bfe6 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -82,8 +82,8 @@ extern "C" { #define TIMER_MULT_APB1 2 #define TIMER_MULT_APB2 2 -#define JACK_PPM_OUT() -#define JACK_PPM_IN() +// #define JACK_PPM_OUT() +// #define JACK_PPM_IN() #define PIN_MODE_MASK 0x0003 #define PIN_INPUT 0x0000 diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 10f1871c5..8883b6fcc 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -42,49 +42,8 @@ uint8_t requiredSpeakerVolume; extern void batteryCheck(); -void perMainArm() +void handleUsbConnection() { -#if defined(PCBSKY9X) - Current_accumulator += Current_analogue ; - static uint32_t OneSecTimer; - if (++OneSecTimer >= 100) { - OneSecTimer -= 100 ; - Current_used += Current_accumulator / 100 ; // milliAmpSeconds (but scaled) - Current_accumulator = 0 ; - } -#endif - - if (currentSpeakerVolume != requiredSpeakerVolume) { - currentSpeakerVolume = requiredSpeakerVolume; - setVolume(currentSpeakerVolume); - } - - if (!usbPlugged()) { - // TODO merge these 2 branches -#if defined(PCBSKY9X) - if (Eeprom32_process_state != E32_IDLE) - ee32_process(); - else if (TIME_TO_WRITE()) - eeCheck(false); -#else - if (theFile.isWriting()) - theFile.nextWriteStep(); - else if (TIME_TO_WRITE()) - eeCheck(false); -#endif - } - - sdMountPoll(); - writeLogs(); - - uint8_t evt = getEvent(false); - if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on - checkBacklight(); - -#if defined(PCBTARANIS) - checkTrainerSettings(); -#endif - #if defined(PCBTARANIS) && !defined(SIMU) static bool usbStarted = false; if (!usbStarted && usbPlugged()) { @@ -111,12 +70,55 @@ void perMainArm() } #endif #endif //#if defined(PCBTARANIS) && !defined(SIMU) +} +void checkSpeakerVolume() +{ + if (currentSpeakerVolume != requiredSpeakerVolume) { + currentSpeakerVolume = requiredSpeakerVolume; + setVolume(currentSpeakerVolume); + } +} + +void checkEeprom() +{ + if (!usbPlugged()) { + // TODO merge these 2 branches +#if defined(PCBSKY9X) + if (Eeprom32_process_state != E32_IDLE) + ee32_process(); + else if (TIME_TO_WRITE()) + eeCheck(false); +#else + if (theFile.isWriting()) + theFile.nextWriteStep(); + else if (TIME_TO_WRITE()) + eeCheck(false); +#endif + } +} + +void perMainArm() +{ +#if defined(PCBSKY9X) && !defined(REVA) + calcConsumption(); +#endif + checkSpeakerVolume(); + checkEeprom(); + sdMountPoll(); + writeLogs(); + handleUsbConnection(); + checkTrainerSettings(); + + uint8_t evt = getEvent(false); + if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on + checkBacklight(); #if defined(NAVIGATION_STICKS) uint8_t sticks_evt = getSticksNavigationEvent(); if (sticks_evt) evt = sticks_evt; #endif + #if defined(USB_MASS_STORAGE) if (usbPlugged()) { lcd_clear(); @@ -157,16 +159,7 @@ void perMainArm() } drawStatusLine(); - lcdRefresh(); - - if (SLAVE_MODE()) { - JACK_PPM_OUT(); - } - else { - JACK_PPM_IN(); - } - batteryCheck(); } From 8aeb35c91268b23d6bbc272c0286172f3076ae00 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 6 Dec 2014 15:12:18 +0100 Subject: [PATCH 07/19] Refactoring continued: Try to run lua scripts before the first LCD function --- radio/src/Makefile | 2 +- radio/src/{lua.cpp => lua_api.cpp} | 262 ++++++++++++++++------ radio/src/lua_api.h | 128 +++++++++++ radio/src/opentx.cpp | 4 +- radio/src/opentx.h | 83 +------ radio/src/targets/taranis/keys_driver.cpp | 5 +- radio/src/tasks_arm.cpp | 62 +++-- radio/src/tests/gtests.h | 2 + radio/src/tests/lua.cpp | 1 + radio/src/tests/mixer.cpp | 12 + 10 files changed, 371 insertions(+), 190 deletions(-) rename radio/src/{lua.cpp => lua_api.cpp} (90%) create mode 100644 radio/src/lua_api.h diff --git a/radio/src/Makefile b/radio/src/Makefile index 8237687e9..79b768a07 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -781,7 +781,7 @@ ifeq ($(PCB), TARANIS) endif CPPDEFS += -DLUA INCDIRS += lua/src - CPPSRC += lua.cpp + CPPSRC += lua_api.cpp LUASRC = lua/src/lapi.c lua/src/lcode.c lua/src/lctype.c lua/src/ldebug.c lua/src/ldo.c lua/src/ldump.c lua/src/lfunc.c lua/src/lgc.c lua/src/llex.c lua/src/lmem.c \ lua/src/lobject.c lua/src/lopcodes.c lua/src/lparser.c lua/src/lstate.c lua/src/lstring.c lua/src/ltable.c lua/src/lrotable.c lua/src/ltm.c lua/src/lundump.c lua/src/lvm.c lua/src/lzio.c \ lua/src/lbaselib.c lua/src/linit.c lua/src/lmathlib.c lua/src/lbitlib.c lua/src/loadlib.c lua/src/lauxlib.c lua/src/ltablib.c lua/src/lcorolib.c diff --git a/radio/src/lua.cpp b/radio/src/lua_api.cpp similarity index 90% rename from radio/src/lua.cpp rename to radio/src/lua_api.cpp index 8890e6392..3a267e03a 100644 --- a/radio/src/lua.cpp +++ b/radio/src/lua_api.cpp @@ -72,6 +72,7 @@ ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS] = { {0} }; ScriptInternalData standaloneScript = { SCRIPT_NOFILE, 0 }; uint16_t maxLuaInterval = 0; uint16_t maxLuaDuration = 0; +bool luaLcdAllowed; #define PERMANENT_SCRIPTS_MAX_INSTRUCTIONS (10000/100) #define MANUAL_SCRIPTS_MAX_INSTRUCTIONS (20000/100) @@ -341,14 +342,20 @@ static int luaGetGeneralSettings(lua_State *L) return 1; } +static int luaLcdLock(lua_State *L) +{ + return 0; +} + static int luaLcdClear(lua_State *L) { - lcd_clear(); + if (luaLcdAllowed) lcd_clear(); return 0; } static int luaLcdDrawPoint(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); lcd_plot(x, y); @@ -357,6 +364,7 @@ static int luaLcdDrawPoint(lua_State *L) static int luaLcdDrawLine(lua_State *L) { + if (! luaLcdAllowed) return 0; int x1 = luaL_checkinteger(L, 1); int y1 = luaL_checkinteger(L, 2); int x2 = luaL_checkinteger(L, 3); @@ -375,6 +383,7 @@ static int luaLcdGetLastPos(lua_State *L) static int luaLcdDrawText(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); const char * s = luaL_checkstring(L, 3); @@ -385,6 +394,7 @@ static int luaLcdDrawText(lua_State *L) static int luaLcdDrawTimer(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int seconds = luaL_checkinteger(L, 3); @@ -395,6 +405,7 @@ static int luaLcdDrawTimer(lua_State *L) static int luaLcdDrawNumber(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int n = luaL_checkinteger(L, 3); @@ -405,6 +416,7 @@ static int luaLcdDrawNumber(lua_State *L) static int luaLcdDrawChannel(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int channel = -1; @@ -427,6 +439,7 @@ static int luaLcdDrawChannel(lua_State *L) static int luaLcdDrawSwitch(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int s = luaL_checkinteger(L, 3); @@ -437,6 +450,7 @@ static int luaLcdDrawSwitch(lua_State *L) static int luaLcdDrawSource(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int s = luaL_checkinteger(L, 3); @@ -447,6 +461,7 @@ static int luaLcdDrawSource(lua_State *L) static int luaLcdDrawPixmap(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); const char * filename = luaL_checkstring(L, 3); @@ -460,6 +475,7 @@ static int luaLcdDrawPixmap(lua_State *L) static int luaLcdDrawRectangle(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -470,6 +486,7 @@ static int luaLcdDrawRectangle(lua_State *L) static int luaLcdDrawFilledRectangle(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -481,6 +498,7 @@ static int luaLcdDrawFilledRectangle(lua_State *L) static int luaLcdDrawGauge(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -498,6 +516,7 @@ static int luaLcdDrawGauge(lua_State *L) static int luaLcdDrawScreenTitle(lua_State *L) { + if (! luaLcdAllowed) return 0; const char * str = luaL_checkstring(L, 1); int idx = luaL_checkinteger(L, 2); int cnt = luaL_checkinteger(L, 3); @@ -511,6 +530,7 @@ static int luaLcdDrawScreenTitle(lua_State *L) static int luaLcdDrawCombobox(lua_State *L) { + if (! luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -1411,6 +1431,7 @@ const luaL_Reg modelLib[] = { }; const luaL_Reg lcdLib[] = { + { "lock", luaLcdLock }, { "clear", luaLcdClear }, { "getLastPos", luaLcdGetLastPos }, { "drawPoint", luaLcdDrawPoint }, @@ -1810,96 +1831,99 @@ void luaDoOneRunStandalone(uint8_t evt) } } -void luaDoOneRunPermanentScript(uint8_t evt, int i) +bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) { ScriptInternalData & sid = scriptInternalData[i]; - if (sid.state == SCRIPT_OK) { - SET_LUA_INSTRUCTIONS_COUNT(PERMANENT_SCRIPTS_MAX_INSTRUCTIONS); - int inputsCount = 0; + if (sid.state != SCRIPT_OK) return false; + + SET_LUA_INSTRUCTIONS_COUNT(PERMANENT_SCRIPTS_MAX_INSTRUCTIONS); + int inputsCount = 0; #if defined(SIMU) || defined(DEBUG) - const char *filename; + const char *filename; #endif - ScriptInputsOutputs * sio = NULL; + ScriptInputsOutputs * sio = NULL; #if SCRIPT_MIX_FIRST > 0 - if (sid.reference >= SCRIPT_MIX_FIRST && sid.reference <= SCRIPT_MIX_LAST) { + if (sid.reference >= SCRIPT_MIX_FIRST && sid.reference <= SCRIPT_MIX_LAST) { #else - if (sid.reference <= SCRIPT_MIX_LAST) { + if (sid.reference <= SCRIPT_MIX_LAST) { #endif - ScriptData & sd = g_model.scriptsData[sid.reference-SCRIPT_MIX_FIRST]; - sio = &scriptInputsOutputs[sid.reference-SCRIPT_MIX_FIRST]; - inputsCount = sio->inputsCount; + if (runGuiScripts) return false; //mixer script is not gui script + ScriptData & sd = g_model.scriptsData[sid.reference-SCRIPT_MIX_FIRST]; + sio = &scriptInputsOutputs[sid.reference-SCRIPT_MIX_FIRST]; + inputsCount = sio->inputsCount; #if defined(SIMU) || defined(DEBUG) - filename = sd.file; + filename = sd.file; #endif - lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); - for (int j=0; jinputsCount; j++) { - if (sio->inputs[j].type == 1) - luaGetValueAndPush((uint8_t)sd.inputs[j]); - else - lua_pushinteger(L, sd.inputs[j] + sio->inputs[j].def); - } + lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); + for (int j=0; jinputsCount; j++) { + if (sio->inputs[j].type == 1) + luaGetValueAndPush((uint8_t)sd.inputs[j]); + else + lua_pushinteger(L, sd.inputs[j] + sio->inputs[j].def); } - else if (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST) { - CustomFunctionData & fn = g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST]; - if (!getSwitch(fn.swtch)) { - return; - } + } + else if (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST) { + CustomFunctionData & fn = g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST]; + if (runGuiScripts) return false; //function script is not gui script + if (!getSwitch(fn.swtch)) return false; #if defined(SIMU) || defined(DEBUG) - filename = fn.play.name; + filename = fn.play.name; #endif + lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); + } + else { +#if defined(SIMU) || defined(DEBUG) + TelemetryScriptData & script = g_model.frsky.screens[sid.reference-SCRIPT_TELEMETRY_FIRST].script; + filename = script.file; +#endif + if (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view) { + if (!runGuiScripts) return false; //telemetry script gui part, do not run this time lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); + lua_pushinteger(L, evt); + inputsCount = 1; + } + else if (sid.background) { + if (runGuiScripts) return false; //telemetry script non-gui part, do not run this time + lua_rawgeti(L, LUA_REGISTRYINDEX, sid.background); } else { -#if defined(SIMU) || defined(DEBUG) - TelemetryScriptData & script = g_model.frsky.screens[sid.reference-SCRIPT_TELEMETRY_FIRST].script; - filename = script.file; -#endif - if (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view) { - lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); - lua_pushinteger(L, evt); - inputsCount = 1; - } - else if (sid.background) { - lua_rawgeti(L, LUA_REGISTRYINDEX, sid.background); - } - else { - return; - } + return false; } + } - if (lua_pcall(L, inputsCount, sio ? sio->outputsCount : 0, 0) == 0) { - if (sio) { - for (int j=sio->outputsCount-1; j>=0; j--) { - if (!lua_isnumber(L, -1)) { - sid.state = (instructionsPercent > 100 ? SCRIPT_KILLED : SCRIPT_SYNTAX_ERROR); - TRACE("Script %8s disabled", filename); - break; - } - sio->outputs[j].value = lua_tointeger(L, -1); - lua_pop(L, 1); + if (lua_pcall(L, inputsCount, sio ? sio->outputsCount : 0, 0) == 0) { + if (sio) { + for (int j=sio->outputsCount-1; j>=0; j--) { + if (!lua_isnumber(L, -1)) { + sid.state = (instructionsPercent > 100 ? SCRIPT_KILLED : SCRIPT_SYNTAX_ERROR); + TRACE("Script %8s disabled", filename); + break; } - } - } - else { - if (instructionsPercent > 100) { - TRACE("Script %8s killed", filename); - sid.state = SCRIPT_KILLED; - } - else { - TRACE("Script %8s error: %s", filename, lua_tostring(L, -1)); - sid.state = SCRIPT_SYNTAX_ERROR; - } - } - - if (sid.state != SCRIPT_OK) { - luaFree(sid); - } - else { - if (instructionsPercent > sid.instructions) { - sid.instructions = instructionsPercent; + sio->outputs[j].value = lua_tointeger(L, -1); + lua_pop(L, 1); } } } + else { + if (instructionsPercent > 100) { + TRACE("Script %8s killed", filename); + sid.state = SCRIPT_KILLED; + } + else { + TRACE("Script %8s error: %s", filename, lua_tostring(L, -1)); + sid.state = SCRIPT_SYNTAX_ERROR; + } + } + + if (sid.state != SCRIPT_OK) { + luaFree(sid); + } + else { + if (instructionsPercent > sid.instructions) { + sid.instructions = instructionsPercent; + } + } + return true; } void luaDoGc() @@ -1924,6 +1948,103 @@ void luaDoGc() } } + +uint32_t lastRunTime; + +void luaRunNonGuiScripts() +{ + //execute one cycle of all Lua cripts that don't use LCD (model scripts, function scripts) + + uint32_t t0 = get_tmr10ms(); + static uint32_t lastLuaTime = 0; + uint16_t interval = (lastLuaTime == 0 ? 0 : (t0 - lastLuaTime)); + lastLuaTime = t0; + if (interval > maxLuaInterval) { + maxLuaInterval = interval; + } + + luaLcdAllowed = false; + if (luaState == INTERPRETER_PANIC) return; + if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) return; + + if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) { + luaState = 0; + luaInit(); + if (luaState == INTERPRETER_PANIC) return; + luaLoadPermanentScripts(); + if (luaState == INTERPRETER_PANIC) return; + } + for (int i=0; i maxLuaDuration) { + maxLuaDuration = lastRunTime; + } + + return scriptWasRun; +} + +#if 0 void luaTask(uint8_t evt) { uint32_t t0 = get_tmr10ms(); @@ -1977,6 +2098,7 @@ void luaTask(uint8_t evt) maxLuaDuration = t0; } } +#endif int luaGetMemUsed() { diff --git a/radio/src/lua_api.h b/radio/src/lua_api.h new file mode 100644 index 000000000..6df19f118 --- /dev/null +++ b/radio/src/lua_api.h @@ -0,0 +1,128 @@ +/* + * Authors (alphabetical order) + * - Andre Bernet + * - Andreas Weitl + * - Bertrand Songis + * - Bryan J. Rentoul (Gruvin) + * - Cameron Weeks + * - Erez Raviv + * - Gabriel Birkus + * - Jean-Pierre Parisy + * - Karl Szmutny + * - Michael Blandford + * - Michal Hlavinka + * - Pat Mackenzie + * - Philip Moss + * - Rob Thomson + * - Romolo Manfredini + * - Thomas Husterer + * + * opentx is based on code named + * gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/, + * er9x by Erez Raviv: http://code.google.com/p/er9x/, + * and the original (and ongoing) project by + * Thomas Husterer, th9x: http://code.google.com/p/th9x/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifndef luaapi_h +#define luaapi_h + + + +#if defined(LUA) + struct ScriptInput { + const char *name; + uint8_t type; + int16_t min; + int16_t max; + int16_t def; + }; + struct ScriptOutput { + const char *name; + int16_t value; + }; + enum ScriptState { + SCRIPT_OK, + SCRIPT_NOFILE, + SCRIPT_SYNTAX_ERROR, + SCRIPT_PANIC, + SCRIPT_KILLED + }; + enum ScriptReference { + SCRIPT_MIX_FIRST, + SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1, + SCRIPT_FUNC_FIRST, + SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+NUM_CFN-1, + SCRIPT_TELEMETRY_FIRST, + SCRIPT_TELEMETRY_LAST=SCRIPT_TELEMETRY_FIRST+MAX_SCRIPTS, // telem0 and telem1 .. telem7 + }; + struct ScriptInternalData { + uint8_t reference; + uint8_t state; + int run; + int background; + uint8_t instructions; + }; + struct ScriptInputsOutputs { + uint8_t inputsCount; + ScriptInput inputs[MAX_SCRIPT_INPUTS]; + uint8_t outputsCount; + ScriptOutput outputs[MAX_SCRIPT_OUTPUTS]; + }; + #define INTERPRETER_RUNNING_STANDALONE_SCRIPT 1 + #define INTERPRETER_RELOAD_PERMANENT_SCRIPTS 2 + #define INTERPRETER_PANIC 255 + extern uint8_t luaState; + extern uint8_t luaScriptsCount; + extern ScriptInternalData standaloneScript; + extern ScriptInternalData scriptInternalData[MAX_SCRIPTS]; + extern ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS]; + void luaClose(); + // void luaInit(); + // void luaTask(uint8_t evt); + void luaExec(const char *filename); + int luaGetMemUsed(); + #define luaGetCpuUsed(idx) scriptInternalData[idx].instructions + #define LUA_LOAD_MODEL_SCRIPTS() luaState |= INTERPRETER_RELOAD_PERMANENT_SCRIPTS + #define LUA_LOAD_MODEL_SCRIPT(idx) luaState |= INTERPRETER_RELOAD_PERMANENT_SCRIPTS + #define LUA_STANDALONE_SCRIPT_RUNNING() (luaState == INTERPRETER_RUNNING_STANDALONE_SCRIPT) + // Lua PROTECT/UNPROTECT + #include + struct our_longjmp { + struct our_longjmp *previous; + jmp_buf b; + volatile int status; /* error code */ + }; + extern struct our_longjmp * global_lj; + #define PROTECT_LUA() { struct our_longjmp lj; \ + lj.previous = global_lj; /* chain new error handler */ \ + global_lj = &lj; \ + if (setjmp(lj.b) == 0) + #define UNPROTECT_LUA() global_lj = lj.previous; } /* restore old error handler */ + + void luaRunNonGuiScripts(); + bool luaRunGuiScripts(uint8_t evt); + + extern uint16_t maxLuaInterval; + extern uint16_t maxLuaDuration; + + +#else // #if defined(LUA) + #define LUA_LOAD_MODEL_SCRIPTS() + #define LUA_LOAD_MODEL_SCRIPT(idx) + #define LUA_STANDALONE_SCRIPT_RUNNING() (0) + #define luaRunNonGuiScripts() + #define luaRunGuiScripts(evt) (false) +#endif + +#endif // #ifndef luaapi_h diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index b0dd96212..7453e0822 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -2136,7 +2136,7 @@ uint8_t getSticksNavigationEvent() } #endif -void batteryCheck() +void checkBattery() { static uint8_t counter = 0; if (g_menuStack[g_menuStackPtr] == menuGeneralDiagAna) { @@ -2327,7 +2327,7 @@ void perMain() JACK_PPM_IN(); } - batteryCheck(); + checkBattery(); } #endif // #if !defined(CPUARM) diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 3ae32273d..1e25023fe 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -922,11 +922,6 @@ extern uint8_t g_tmr1Latency_min; extern uint16_t lastMixerDuration; #endif -#if defined(LUA) -extern uint16_t maxLuaInterval; -extern uint16_t maxLuaDuration; -#endif - #if defined(CPUARM) #define DURATION_MS_PREC2(x) ((x)/20) #else @@ -1536,82 +1531,6 @@ enum AUDIO_SOUNDS { #include "rtc.h" #endif -#if defined(LUA) - struct ScriptInput { - const char *name; - uint8_t type; - int16_t min; - int16_t max; - int16_t def; - }; - struct ScriptOutput { - const char *name; - int16_t value; - }; - enum ScriptState { - SCRIPT_OK, - SCRIPT_NOFILE, - SCRIPT_SYNTAX_ERROR, - SCRIPT_PANIC, - SCRIPT_KILLED - }; - enum ScriptReference { - SCRIPT_MIX_FIRST, - SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1, - SCRIPT_FUNC_FIRST, - SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+NUM_CFN-1, - SCRIPT_TELEMETRY_FIRST, - SCRIPT_TELEMETRY_LAST=SCRIPT_TELEMETRY_FIRST+MAX_SCRIPTS, // telem0 and telem1 .. telem7 - }; - struct ScriptInternalData { - uint8_t reference; - uint8_t state; - int run; - int background; - uint8_t instructions; - }; - struct ScriptInputsOutputs { - uint8_t inputsCount; - ScriptInput inputs[MAX_SCRIPT_INPUTS]; - uint8_t outputsCount; - ScriptOutput outputs[MAX_SCRIPT_OUTPUTS]; - }; - #define INTERPRETER_RUNNING_STANDALONE_SCRIPT 1 - #define INTERPRETER_RELOAD_PERMANENT_SCRIPTS 2 - #define INTERPRETER_PANIC 255 - extern uint8_t luaState; - extern uint8_t luaScriptsCount; - extern ScriptInternalData standaloneScript; - extern ScriptInternalData scriptInternalData[MAX_SCRIPTS]; - extern ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS]; - void luaClose(); - void luaInit(); - void luaTask(uint8_t evt); - void luaExec(const char *filename); - int luaGetMemUsed(); - #define luaGetCpuUsed(idx) scriptInternalData[idx].instructions - #define LUA_LOAD_MODEL_SCRIPTS() luaState |= INTERPRETER_RELOAD_PERMANENT_SCRIPTS - #define LUA_LOAD_MODEL_SCRIPT(idx) luaState |= INTERPRETER_RELOAD_PERMANENT_SCRIPTS - #define LUA_STANDALONE_SCRIPT_RUNNING() (luaState == INTERPRETER_RUNNING_STANDALONE_SCRIPT) - // Lua PROTECT/UNPROTECT - #include - struct our_longjmp { - struct our_longjmp *previous; - jmp_buf b; - volatile int status; /* error code */ - }; - extern struct our_longjmp * global_lj; - #define PROTECT_LUA() { struct our_longjmp lj; \ - lj.previous = global_lj; /* chain new error handler */ \ - global_lj = &lj; \ - if (setjmp(lj.b) == 0) - #define UNPROTECT_LUA() global_lj = lj.previous; } /* restore old error handler */ -#else - #define LUA_LOAD_MODEL_SCRIPTS() - #define LUA_LOAD_MODEL_SCRIPT(idx) - #define LUA_STANDALONE_SCRIPT_RUNNING() (0) -#endif - #if defined(CPUARM) extern uint8_t requiredSpeakerVolume; #endif @@ -1826,4 +1745,6 @@ void varioWakeup(); extern void usbPluggedIn(); #endif +#include "lua_api.h" + #endif diff --git a/radio/src/targets/taranis/keys_driver.cpp b/radio/src/targets/taranis/keys_driver.cpp index 7437d2cfb..6c36d5e46 100644 --- a/radio/src/targets/taranis/keys_driver.cpp +++ b/radio/src/targets/taranis/keys_driver.cpp @@ -41,11 +41,8 @@ uint32_t readKeys() { register uint32_t d = GPIOD->IDR; -#if !defined(REV9E) register uint32_t e = GPIOE->IDR; -#else register uint32_t f = GPIOF->IDR; -#endif register uint32_t result = 0; #if defined(REV9E) @@ -55,7 +52,7 @@ uint32_t readKeys() #endif result |= 0x02 << KEY_ENTER; -#if !defined(REV9E) +#if !defined(REV9E) || defined(SIMU) if (e & PIN_BUTTON_PLUS) result |= 0x02 << KEY_PLUS; if (e & PIN_BUTTON_MINUS) diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 8883b6fcc..6e2ae7dd8 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -36,11 +36,10 @@ #include "opentx.h" - static uint8_t currentSpeakerVolume = 255; uint8_t requiredSpeakerVolume; -extern void batteryCheck(); +extern void checkBattery(); void handleUsbConnection() { @@ -109,6 +108,7 @@ void perMainArm() writeLogs(); handleUsbConnection(); checkTrainerSettings(); + checkBattery(); uint8_t evt = getEvent(false); if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on @@ -118,49 +118,47 @@ void perMainArm() if (sticks_evt) evt = sticks_evt; #endif - #if defined(USB_MASS_STORAGE) if (usbPlugged()) { + // disable access to menus lcd_clear(); menuMainView(0); + lcdRefresh(); + return; } - else #endif - { + + // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) + luaRunNonGuiScripts(); + + // draw LCD from menus or from Lua script + if (luaRunGuiScripts(evt)) { // either stand-alone or telemetry scripts + // let Lua manage LCD fully + } + else { + // normal GUI from menus const char *warn = s_warning; uint8_t menu = s_menu_count; - - if (!LUA_STANDALONE_SCRIPT_RUNNING()) { - lcd_clear(); - if (menuEvent) { - m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; - m_posHorz = 0; - evt = menuEvent; - menuEvent = 0; - AUDIO_MENUS(); - } - g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + lcd_clear(); + if (menuEvent) { + m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; + m_posHorz = 0; + evt = menuEvent; + menuEvent = 0; + AUDIO_MENUS(); } - -#if defined(LUA) - luaTask(evt); -#endif - - if (!LUA_STANDALONE_SCRIPT_RUNNING()) { - if (warn) DISPLAY_WARNING(evt); - if (menu) { - const char * result = displayMenu(evt); - if (result) { - menuHandler(result); - putEvent(EVT_MENU_UP); - } + g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + if (warn) DISPLAY_WARNING(evt); + if (menu) { + const char * result = displayMenu(evt); + if (result) { + menuHandler(result); + putEvent(EVT_MENU_UP); } } + drawStatusLine(); } - - drawStatusLine(); lcdRefresh(); - batteryCheck(); } #define MENUS_STACK_SIZE 2000 diff --git a/radio/src/tests/gtests.h b/radio/src/tests/gtests.h index 43c780b4a..eff8915d7 100644 --- a/radio/src/tests/gtests.h +++ b/radio/src/tests/gtests.h @@ -42,8 +42,10 @@ inline void MIXER_RESET() inline void TELEMETRY_RESET() { +#if defined(FRSKY) memclear(&frskyData, sizeof(frskyData)); TELEMETRY_RSSI() = 100; +#endif } void MIXER_RESET(); diff --git a/radio/src/tests/lua.cpp b/radio/src/tests/lua.cpp index f4e1c37e1..625a51960 100644 --- a/radio/src/tests/lua.cpp +++ b/radio/src/tests/lua.cpp @@ -48,6 +48,7 @@ extern const char * zchar2string(const char * zstring, int size); #define EXPECT_ZSTREQ(c_string, z_string) EXPECT_STREQ(c_string, zchar2string(z_string, sizeof(z_string))) +extern void luaInit(); void luaExecStr(const char * str) { diff --git a/radio/src/tests/mixer.cpp b/radio/src/tests/mixer.cpp index 7655e3ac9..2c09886fc 100644 --- a/radio/src/tests/mixer.cpp +++ b/radio/src/tests/mixer.cpp @@ -391,7 +391,11 @@ TEST(Trims, CopySticksToOffset) MODEL_RESET(); modelDefault(0); anaInValues[ELE_STICK] = -100; +#if defined(CPUARM) + doMixerCalculations(); +#else perMain(); +#endif copySticksToOffset(1); EXPECT_EQ(g_model.limitData[1].offset, -97); } @@ -618,11 +622,19 @@ TEST(Mixer, RecursiveAddChannelAfterInactivePhase) g_model.mixData[2].srcRaw = MIXSRC_MAX; g_model.mixData[2].weight = 100; simuSetSwitch(3, -1); +#if defined(CPUARM) + doMixerCalculations(); +#else perMain(); +#endif EXPECT_EQ(chans[0], CHANNEL_MAX/2); EXPECT_EQ(chans[1], CHANNEL_MAX); simuSetSwitch(3, 0); +#if defined(CPUARM) + doMixerCalculations(); +#else perMain(); +#endif EXPECT_EQ(chans[0], CHANNEL_MAX/2); EXPECT_EQ(chans[1], CHANNEL_MAX); } From e4de0da0bb2a10e6ca7c29641ffedbac38972e6f Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 6 Dec 2014 15:55:07 +0100 Subject: [PATCH 08/19] Fixed Companion build --- companion/src/firmwares/opentx/opentxTaranisSimulator.cpp | 2 +- companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp | 2 +- radio/src/opentx.cpp | 3 --- radio/src/targets/taranis/keys_driver.cpp | 3 +++ radio/src/tasks_arm.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp b/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp index 1c1f9fbd9..47425b24d 100644 --- a/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp +++ b/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp @@ -136,7 +136,7 @@ inline int geteepromsize() { #include "radio/src/translations/tts_sk.cpp" #include "radio/src/translations/tts_pl.cpp" #include "radio/src/translations/tts_hu.cpp" -#include "radio/src/lua.cpp" +#include "radio/src/lua_api.cpp" #include "radio/src/lua/src/lapi.c" #include "radio/src/lua/src/lcode.c" #include "radio/src/lua/src/lctype.c" diff --git a/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp b/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp index 2b83d85ee..795d09407 100755 --- a/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp +++ b/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp @@ -138,7 +138,7 @@ inline int geteepromsize() { #include "radio/src/translations/tts_sk.cpp" #include "radio/src/translations/tts_pl.cpp" #include "radio/src/translations/tts_hu.cpp" -#include "radio/src/lua.cpp" +#include "radio/src/lua_api.cpp" #include "radio/src/lua/src/lapi.c" #include "radio/src/lua/src/lcode.c" #include "radio/src/lua/src/lctype.c" diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 7453e0822..cf26d952f 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -2720,9 +2720,6 @@ void opentxInit(OPENTX_INIT_ARGS) #endif #if defined(VOICE) -#if defined(CPUARM) - currentSpeakerVolume = requiredSpeakerVolume = g_eeGeneral.speakerVolume+VOLUME_LEVEL_DEF; -#endif setVolume(g_eeGeneral.speakerVolume+VOLUME_LEVEL_DEF); #endif diff --git a/radio/src/targets/taranis/keys_driver.cpp b/radio/src/targets/taranis/keys_driver.cpp index 6c36d5e46..5ce8d234b 100644 --- a/radio/src/targets/taranis/keys_driver.cpp +++ b/radio/src/targets/taranis/keys_driver.cpp @@ -45,6 +45,9 @@ uint32_t readKeys() register uint32_t f = GPIOF->IDR; register uint32_t result = 0; + (void)e; + (void)f; + #if defined(REV9E) if (f & PIN_BUTTON_ENTER) #else diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 6e2ae7dd8..613ac08da 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -37,7 +37,7 @@ #include "opentx.h" static uint8_t currentSpeakerVolume = 255; -uint8_t requiredSpeakerVolume; +uint8_t requiredSpeakerVolume = 255; extern void checkBattery(); From f2ed441916cda13f01bac86d424d1f0c625f79b3 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 6 Dec 2014 18:19:32 +0100 Subject: [PATCH 09/19] LCD_DUAL_BUFFER optional (speed vs RAM) --- radio/src/Makefile | 19 +++++++++++++++++-- radio/src/lcd.h | 16 +++++++++++----- radio/src/lcd_common.cpp | 3 ++- radio/src/lcd_default.cpp | 7 +++++++ radio/src/lcd_taranis.cpp | 19 ++++++++----------- radio/src/targets/taranis/lcd_driver.cpp | 7 +++++++ 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/radio/src/Makefile b/radio/src/Makefile index 79b768a07..07facfafd 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -314,6 +314,15 @@ TRACE_SD_CARD = NO TRACE_FATFS = NO TRACE_AUDIO = NO +# Enable double buffering for LCD. Only for TARANIS PLUS and 9XE targets. +# Activating requires about 6kB of RAM, but it enables menus task to +# immediately start to compose a new LCD image while the current one is +# being written to LCD by DMA. +# If this option is NO, then first function that affects LCD buffer is +# delayed until DMA transfer is finished. +# Values = NO, YES +LCD_DUAL_BUFFER = NO + #------- END BUILD OPTIONS --------------------------- # Define programs and commands. @@ -678,11 +687,17 @@ ifeq ($(PCB), TARANIS) CPPDEFS = -DREV4a SWR = YES else ifeq ($(PCBREV), REVPLUS) - CPPDEFS = -DREVPLUS -DLCD_DUAL_BUFFER + CPPDEFS = -DREVPLUS + ifeq ($(LCD_DUAL_BUFFER), YES) + CPPDEFS += -DLCD_DUAL_BUFFER + endif HAPTIC = YES SWR = NO else ifeq ($(PCBREV), REV9E) - CPPDEFS = -DREVPLUS -DREV9E -DLCD_DUAL_BUFFER + CPPDEFS = -DREVPLUS -DREV9E + ifeq ($(LCD_DUAL_BUFFER), YES) + CPPDEFS += -DLCD_DUAL_BUFFER + endif HAPTIC = YES SWR = YES else diff --git a/radio/src/lcd.h b/radio/src/lcd.h index 8f5a16801..8f5474c1d 100644 --- a/radio/src/lcd.h +++ b/radio/src/lcd.h @@ -161,12 +161,18 @@ #define DISPLAY_BUF_SIZE (LCD_W*((LCD_H+7)/8)) #endif -#if defined(LCD_DUAL_BUFFER) -extern display_t displayBuf1[DISPLAY_BUF_SIZE]; -extern display_t displayBuf2[DISPLAY_BUF_SIZE]; -extern display_t * displayBuf; +#if defined(PCBTARANIS) && defined(REVPLUS) && defined(LCD_DUAL_BUFFER) + extern display_t displayBuf1[DISPLAY_BUF_SIZE]; + extern display_t displayBuf2[DISPLAY_BUF_SIZE]; + extern display_t * displayBuf; #else -extern display_t displayBuf[DISPLAY_BUF_SIZE]; + extern display_t displayBuf[DISPLAY_BUF_SIZE]; +#endif + +#if defined(PCBTARANIS) && defined(REVPLUS) && !defined(LCD_DUAL_BUFFER) + void lcdWaitDmaEnd(); +#else + #define lcdWaitDmaEnd() #endif extern coord_t lcdLastPos; diff --git a/radio/src/lcd_common.cpp b/radio/src/lcd_common.cpp index c46f8fa4f..c03f0ccd3 100644 --- a/radio/src/lcd_common.cpp +++ b/radio/src/lcd_common.cpp @@ -37,7 +37,7 @@ #include "opentx.h" -#if defined(LCD_DUAL_BUFFER) +#if defined(PCBTARANIS) && defined(REVPLUS) && defined(LCD_DUAL_BUFFER) display_t displayBuf1[DISPLAY_BUF_SIZE]; display_t displayBuf2[DISPLAY_BUF_SIZE]; display_t * displayBuf = displayBuf1; @@ -47,6 +47,7 @@ display_t displayBuf[DISPLAY_BUF_SIZE]; void lcd_clear() { + lcdWaitDmaEnd(); memset(displayBuf, 0, DISPLAY_BUFER_SIZE); } diff --git a/radio/src/lcd_default.cpp b/radio/src/lcd_default.cpp index f4777abb4..5e09f5430 100644 --- a/radio/src/lcd_default.cpp +++ b/radio/src/lcd_default.cpp @@ -41,6 +41,7 @@ #if !defined(CPUARM) void lcd_putcAtt(coord_t x, uint8_t y, const unsigned char c, LcdFlags flags) { + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; const pm_uchar *q = &font_5x7[(c-0x20)*5]; @@ -204,6 +205,7 @@ void lcd_mask(uint8_t *p, uint8_t mask, LcdFlags att) void lcd_plot(coord_t x, coord_t y, LcdFlags att) { + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; if (p= LCD_H) return; if (x+w > LCD_W) { w = LCD_W - x; } + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; uint8_t msk = BITMASK(y%8); while (w--) { @@ -239,6 +242,7 @@ void lcd_vlineStip(coord_t x, int8_t y, int8_t h, uint8_t pat) if (pat==DOTTED && !(y%2)) pat = ~pat; + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; y = (y & 0x07); if (y) { @@ -277,6 +281,7 @@ void lcd_vlineStip(coord_t x, scoord_t y, scoord_t h, uint8_t pat, LcdFlags att) if (pat==DOTTED && !(y%2)) pat = ~pat; + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; y = (y & 0x07); if (y) { @@ -303,6 +308,7 @@ void lcd_vlineStip(coord_t x, scoord_t y, scoord_t h, uint8_t pat, LcdFlags att) void lcd_invert_line(int8_t y) { + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[y * LCD_W]; for (coord_t x=0; x=LCD_W || y<0 || y>=LCD_H) - return; + if (x<0 || x>=LCD_W || y<0 || y>=LCD_H) return; + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 2 * LCD_W + x ]; uint8_t mask = PIXEL_GREY_MASK(y, att); if (p= LCD_H) return; if (x+w > LCD_W) { - if (x >= LCD_W ) { - return; - } + if (x >= LCD_W ) return; w = LCD_W - x; } + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 2 * LCD_W + x ]; uint8_t mask = PIXEL_GREY_MASK(y, att); while (w--) { @@ -123,6 +122,7 @@ void lcd_vlineStip(coord_t x, scoord_t y, scoord_t h, uint8_t pat, LcdFlags att) void lcd_invert_line(int8_t line) { + lcdWaitDmaEnd(); uint8_t *p = &displayBuf[line * 4 * LCD_W]; for (coord_t x=0; x LCD_W) { - if (x >= LCD_W ) { - return; - } + if (x >= LCD_W ) return; width = LCD_W-x; } uint8_t rows = (*q++ + 1) / 2; + lcdWaitDmaEnd(); for (uint8_t row=0; row= DISPLAY_END) { - return; - } + if ((p) >= DISPLAY_END) return; uint8_t b = *q++; if (y & 1) { *p = (*p & 0x0f) + ((b & 0x0f) << 4); diff --git a/radio/src/targets/taranis/lcd_driver.cpp b/radio/src/targets/taranis/lcd_driver.cpp index c16584189..17686e2b3 100644 --- a/radio/src/targets/taranis/lcd_driver.cpp +++ b/radio/src/targets/taranis/lcd_driver.cpp @@ -180,6 +180,13 @@ void Set_Address(u8 x, u8 y) volatile bool lcd_busy; +#if !defined(LCD_DUAL_BUFFER) +void lcdWaitDmaEnd() +{ + WAIT_FOR_DMA_END(); +} +#endif + void lcdRefresh(bool wait) { //wait if previous DMA transfer still active From 8854ccd2313f8d4cce49564d1e5985d627b82c00 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 6 Dec 2014 18:35:39 +0100 Subject: [PATCH 10/19] Companion fix --- radio/src/lcd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radio/src/lcd.h b/radio/src/lcd.h index 8f5474c1d..7cff29233 100644 --- a/radio/src/lcd.h +++ b/radio/src/lcd.h @@ -169,7 +169,7 @@ extern display_t displayBuf[DISPLAY_BUF_SIZE]; #endif -#if defined(PCBTARANIS) && defined(REVPLUS) && !defined(LCD_DUAL_BUFFER) +#if defined(PCBTARANIS) && defined(REVPLUS) && !defined(LCD_DUAL_BUFFER) && !defined(SIMU) void lcdWaitDmaEnd(); #else #define lcdWaitDmaEnd() From e9f8b14f8b75dffcea713cf97b9ac718f450c89f Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 6 Dec 2014 19:16:05 +0100 Subject: [PATCH 11/19] Bootloader for 9XE --- radio/src/bootloader/Makefile | 8 ++++++++ radio/src/targets/taranis/board_taranis.h | 6 +++--- radio/src/targets/taranis/keys_driver.cpp | 2 +- radio/src/targets/taranis/lcd_driver.cpp | 1 - 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/radio/src/bootloader/Makefile b/radio/src/bootloader/Makefile index 4d4081b50..1093e59f8 100644 --- a/radio/src/bootloader/Makefile +++ b/radio/src/bootloader/Makefile @@ -124,6 +124,10 @@ else CPPDEFS += -DREV4a else ifeq ($(PCBREV), REVPLUS) CPPDEFS += -DREVPLUS + HAPTIC = YES + else ifeq ($(PCBREV), REV9E) + CPPDEFS += -DREVPLUS -DREV9E + HAPTIC = YES else CPPDEFS += -DREV4 endif @@ -178,6 +182,10 @@ CPPSRC = ../targets/taranis/lcd_driver.cpp \ ../strhelpers.cpp \ boot.cpp +ifeq ($(PCBREV), REV9E) + CPPSRC += ../targets/taranis/rotenc_driver.cpp +endif + # List ASM source files here ASRC = ../targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx/Source/Templates/gcc_ride7/startup_stm32f2xx.s diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index 98170bfe6..0830bb5a4 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -174,9 +174,9 @@ uint32_t readTrims(void); #if defined(REV9E) // Rotary Encoder driver -void rotencInit(); -void rotencEnd(); -void checkRotaryEncoder(); +void rotencInit(void); +void rotencEnd(void); +void checkRotaryEncoder(void); #endif // WDT driver diff --git a/radio/src/targets/taranis/keys_driver.cpp b/radio/src/targets/taranis/keys_driver.cpp index 5ce8d234b..5a00ca274 100644 --- a/radio/src/targets/taranis/keys_driver.cpp +++ b/radio/src/targets/taranis/keys_driver.cpp @@ -145,7 +145,7 @@ void readKeysAndTrims() #if defined(REV9E) static rotenc_t rePreviousValue; - rotenc_t reNewValue = (x9de_rotenc / ROTARY_ENCODER_GRANULARITY); + rotenc_t reNewValue = (x9de_rotenc / 2); int8_t scrollRE = reNewValue - rePreviousValue; if (scrollRE) { rePreviousValue = reNewValue; diff --git a/radio/src/targets/taranis/lcd_driver.cpp b/radio/src/targets/taranis/lcd_driver.cpp index 17686e2b3..148b84096 100644 --- a/radio/src/targets/taranis/lcd_driver.cpp +++ b/radio/src/targets/taranis/lcd_driver.cpp @@ -44,7 +44,6 @@ static void Delay(uint32_t ms) } #if defined(REVPLUS) -static void LCD_Hardware_Init() ; // New hardware SPI driver for LCD void initLcdSpi() From e55b7504c30622d9717f0f95d77ab103a397b94b Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 09:41:31 +0100 Subject: [PATCH 12/19] Cosmetic changes (thanks to Bertrand's review) --- radio/src/lcd_common.cpp | 8 ++++---- radio/src/lua_api.cpp | 31 ++++++++++++++++--------------- radio/src/tasks_arm.cpp | 9 ++++----- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/radio/src/lcd_common.cpp b/radio/src/lcd_common.cpp index c03f0ccd3..c0639df80 100644 --- a/radio/src/lcd_common.cpp +++ b/radio/src/lcd_common.cpp @@ -38,11 +38,11 @@ #if defined(PCBTARANIS) && defined(REVPLUS) && defined(LCD_DUAL_BUFFER) -display_t displayBuf1[DISPLAY_BUF_SIZE]; -display_t displayBuf2[DISPLAY_BUF_SIZE]; -display_t * displayBuf = displayBuf1; + display_t displayBuf1[DISPLAY_BUF_SIZE]; + display_t displayBuf2[DISPLAY_BUF_SIZE]; + display_t * displayBuf = displayBuf1; #else -display_t displayBuf[DISPLAY_BUF_SIZE]; + display_t displayBuf[DISPLAY_BUF_SIZE]; #endif void lcd_clear() diff --git a/radio/src/lua_api.cpp b/radio/src/lua_api.cpp index 3a267e03a..07483b2e3 100644 --- a/radio/src/lua_api.cpp +++ b/radio/src/lua_api.cpp @@ -355,7 +355,7 @@ static int luaLcdClear(lua_State *L) static int luaLcdDrawPoint(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); lcd_plot(x, y); @@ -364,7 +364,7 @@ static int luaLcdDrawPoint(lua_State *L) static int luaLcdDrawLine(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x1 = luaL_checkinteger(L, 1); int y1 = luaL_checkinteger(L, 2); int x2 = luaL_checkinteger(L, 3); @@ -383,7 +383,7 @@ static int luaLcdGetLastPos(lua_State *L) static int luaLcdDrawText(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); const char * s = luaL_checkstring(L, 3); @@ -394,7 +394,7 @@ static int luaLcdDrawText(lua_State *L) static int luaLcdDrawTimer(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int seconds = luaL_checkinteger(L, 3); @@ -405,7 +405,7 @@ static int luaLcdDrawTimer(lua_State *L) static int luaLcdDrawNumber(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int n = luaL_checkinteger(L, 3); @@ -416,7 +416,7 @@ static int luaLcdDrawNumber(lua_State *L) static int luaLcdDrawChannel(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int channel = -1; @@ -439,7 +439,7 @@ static int luaLcdDrawChannel(lua_State *L) static int luaLcdDrawSwitch(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int s = luaL_checkinteger(L, 3); @@ -450,7 +450,7 @@ static int luaLcdDrawSwitch(lua_State *L) static int luaLcdDrawSource(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int s = luaL_checkinteger(L, 3); @@ -461,7 +461,7 @@ static int luaLcdDrawSource(lua_State *L) static int luaLcdDrawPixmap(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); const char * filename = luaL_checkstring(L, 3); @@ -475,7 +475,7 @@ static int luaLcdDrawPixmap(lua_State *L) static int luaLcdDrawRectangle(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -486,7 +486,7 @@ static int luaLcdDrawRectangle(lua_State *L) static int luaLcdDrawFilledRectangle(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -498,7 +498,7 @@ static int luaLcdDrawFilledRectangle(lua_State *L) static int luaLcdDrawGauge(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -516,7 +516,7 @@ static int luaLcdDrawGauge(lua_State *L) static int luaLcdDrawScreenTitle(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; const char * str = luaL_checkstring(L, 1); int idx = luaL_checkinteger(L, 2); int cnt = luaL_checkinteger(L, 3); @@ -530,7 +530,7 @@ static int luaLcdDrawScreenTitle(lua_State *L) static int luaLcdDrawCombobox(lua_State *L) { - if (! luaLcdAllowed) return 0; + if (!luaLcdAllowed) return 0; int x = luaL_checkinteger(L, 1); int y = luaL_checkinteger(L, 2); int w = luaL_checkinteger(L, 3); @@ -1990,7 +1990,8 @@ void luaRunNonGuiScripts() lastRunTime = get_tmr10ms() - t0; } -bool luaRunGuiScripts(uint8_t evt){ +bool luaRunGuiScripts(uint8_t evt) +{ //execute one cycle of Lua scripts that use LCD //either stand-alone or other (telemetry scripts) diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 613ac08da..48b931ac8 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -288,11 +288,10 @@ void menusTask(void * pdata) perMainArm(); // TODO remove completely massstorage from sky9x firmware U32 runtime = (U32)(CoGetOSTime() - start); - if (runtime >= MENU_TASK_PERIOD_TICKS) { - //no delay - } - else { - CoTickDelay(MENU_TASK_PERIOD_TICKS - runtime); // 5*2ms for now + // deduct the thread run-time from the wait, if run-time was more than + // desired period, then skip the wait all together + if (runtime < MENU_TASK_PERIOD_TICKS) { + CoTickDelay(MENU_TASK_PERIOD_TICKS - runtime); } } From afedd86fae7eec51d3715d6a336cd6f91b8d5b3e Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 09:58:46 +0100 Subject: [PATCH 13/19] perMain() moved into main_avr.cpp and main_arm.cpp --- .../opentx/opentxGruvin9xsimulator.cpp | 1 + .../firmwares/opentx/opentxM128simulator.cpp | 1 + .../firmwares/opentx/opentxM64simulator.cpp | 1 + .../firmwares/opentx/opentxSky9xsimulator.cpp | 1 + .../opentx/opentxTaranisSimulator.cpp | 1 + .../opentx/opentxTaranisX9ESimulator.cpp | 1 + radio/src/Makefile | 14 +- radio/src/main_arm.cpp | 162 +++++++++++++++++ radio/src/main_avr.cpp | 165 ++++++++++++++++++ radio/src/opentx.cpp | 129 -------------- radio/src/tasks_arm.cpp | 125 ------------- 11 files changed, 340 insertions(+), 261 deletions(-) create mode 100644 radio/src/main_arm.cpp create mode 100644 radio/src/main_avr.cpp diff --git a/companion/src/firmwares/opentx/opentxGruvin9xsimulator.cpp b/companion/src/firmwares/opentx/opentxGruvin9xsimulator.cpp index 83151bb8e..4a938ec5c 100644 --- a/companion/src/firmwares/opentx/opentxGruvin9xsimulator.cpp +++ b/companion/src/firmwares/opentx/opentxGruvin9xsimulator.cpp @@ -63,6 +63,7 @@ namespace Open9xGruvin9x { #include "radio/src/eeprom_common.cpp" #include "radio/src/eeprom_rlc.cpp" #include "radio/src/opentx.cpp" +#include "radio/src/main_avr.cpp" #include "radio/src/strhelpers.cpp" #include "radio/src/switches.cpp" #include "radio/src/functions.cpp" diff --git a/companion/src/firmwares/opentx/opentxM128simulator.cpp b/companion/src/firmwares/opentx/opentxM128simulator.cpp index e53261380..2a4d9ab95 100644 --- a/companion/src/firmwares/opentx/opentxM128simulator.cpp +++ b/companion/src/firmwares/opentx/opentxM128simulator.cpp @@ -64,6 +64,7 @@ namespace OpenTxM128 { #include "radio/src/eeprom_common.cpp" #include "radio/src/eeprom_rlc.cpp" #include "radio/src/opentx.cpp" +#include "radio/src/main_avr.cpp" #include "radio/src/strhelpers.cpp" #include "radio/src/switches.cpp" #include "radio/src/functions.cpp" diff --git a/companion/src/firmwares/opentx/opentxM64simulator.cpp b/companion/src/firmwares/opentx/opentxM64simulator.cpp index a566a4ff2..d35c19ee2 100644 --- a/companion/src/firmwares/opentx/opentxM64simulator.cpp +++ b/companion/src/firmwares/opentx/opentxM64simulator.cpp @@ -69,6 +69,7 @@ namespace OpenTxM64 { #include "radio/src/eeprom_common.cpp" #include "radio/src/eeprom_rlc.cpp" #include "radio/src/opentx.cpp" +#include "radio/src/main_avr.cpp" #include "radio/src/strhelpers.cpp" #include "radio/src/switches.cpp" #include "radio/src/functions.cpp" diff --git a/companion/src/firmwares/opentx/opentxSky9xsimulator.cpp b/companion/src/firmwares/opentx/opentxSky9xsimulator.cpp index 37828abcd..bbb65e624 100644 --- a/companion/src/firmwares/opentx/opentxSky9xsimulator.cpp +++ b/companion/src/firmwares/opentx/opentxSky9xsimulator.cpp @@ -76,6 +76,7 @@ namespace Open9xSky9x { #include "radio/src/eeprom_raw.cpp" #include "radio/src/eeprom_conversions.cpp" #include "radio/src/opentx.cpp" +#include "radio/src/main_arm.cpp" #include "radio/src/strhelpers.cpp" #include "radio/src/switches.cpp" #include "radio/src/functions.cpp" diff --git a/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp b/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp index 47425b24d..1377f5f08 100644 --- a/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp +++ b/companion/src/firmwares/opentx/opentxTaranisSimulator.cpp @@ -79,6 +79,7 @@ inline int geteepromsize() { #include "radio/src/eeprom_conversions.cpp" #include "radio/src/eeprom_rlc.cpp" #include "radio/src/opentx.cpp" +#include "radio/src/main_arm.cpp" #include "radio/src/strhelpers.cpp" #include "radio/src/switches.cpp" #include "radio/src/functions.cpp" diff --git a/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp b/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp index 795d09407..b185c9464 100755 --- a/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp +++ b/companion/src/firmwares/opentx/opentxTaranisX9ESimulator.cpp @@ -80,6 +80,7 @@ inline int geteepromsize() { #include "radio/src/eeprom_conversions.cpp" #include "radio/src/eeprom_rlc.cpp" #include "radio/src/opentx.cpp" +#include "radio/src/main_arm.cpp" #include "radio/src/strhelpers.cpp" #include "radio/src/switches.cpp" #include "radio/src/functions.cpp" diff --git a/radio/src/Makefile b/radio/src/Makefile index 07facfafd..a59cae3ad 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -362,7 +362,7 @@ ifeq ($(PCB), $(filter $(PCB), STD 9X 9XR)) THR_TRACE = NO INCDIRS += targets/stock CPPDEFS = -DF_CPU=$(F_CPU)UL -DPCBSTD -DCPUM64 -DEEPROM_VARIANT=$(shell echo ${EEPROM_VARIANT} | bc) - BOARDSRC = targets/stock/board_stock.cpp + BOARDSRC = main_avr.cpp targets/stock/board_stock.cpp EXTRABOARDSRC = targets/stock/lcd_driver.cpp targets/common_avr/telemetry_driver.cpp EEPROMSRC = eeprom_common.cpp eeprom_rlc.cpp LCDSRC = lcd_common.cpp lcd_default.cpp @@ -426,7 +426,7 @@ ifeq ($(PCB), $(filter $(PCB), STD128 9X128 9XR128)) EEPROM_VARIANT += ${M128_VARIANT} CPPDEFS = -DF_CPU=$(F_CPU)UL -DPCBSTD -DCPUM128 -DEEPROM_VARIANT=$(shell echo ${EEPROM_VARIANT} | bc) INCDIRS += targets/stock - BOARDSRC = targets/stock/board_stock.cpp + BOARDSRC = main_avr.cpp targets/stock/board_stock.cpp EXTRABOARDSRC = targets/stock/lcd_driver.cpp targets/common_avr/telemetry_driver.cpp EEPROMSRC = eeprom_common.cpp eeprom_rlc.cpp LCDSRC = lcd_common.cpp lcd_default.cpp @@ -486,7 +486,7 @@ ifeq ($(PCB), $(filter $(PCB), 9X2561)) EEPROM_VARIANT += ${M2561_VARIANT} CPPDEFS = -DF_CPU=$(F_CPU)UL -DPCBSTD -DCPUM2561 -DEEPROM_VARIANT=$(shell echo ${EEPROM_VARIANT} | bc) INCDIRS += targets/stock - BOARDSRC = targets/stock/board_stock.cpp + BOARDSRC = main_avr.cpp targets/stock/board_stock.cpp EXTRABOARDSRC = targets/stock/lcd_driver.cpp targets/common_avr/telemetry_driver.cpp EEPROMSRC = eeprom_common.cpp eeprom_rlc.cpp LCDSRC = lcd_common.cpp lcd_default.cpp @@ -547,7 +547,7 @@ ifeq ($(PCB), GRUVIN9X) BUZZER = NO THR_TRACE = YES INCDIRS += targets/gruvin9x targets/stock FatFs FatFs/option - BOARDSRC = targets/gruvin9x/board_gruvin9x.cpp + BOARDSRC = main_avr.cpp targets/gruvin9x/board_gruvin9x.cpp EXTRABOARDSRC = targets/stock/lcd_driver.cpp targets/common_avr/telemetry_driver.cpp EEPROMSRC = eeprom_common.cpp eeprom_rlc.cpp LCDSRC = lcd_common.cpp lcd_default.cpp @@ -586,7 +586,7 @@ ifeq ($(PCB), MEGA2560) SDCARD = YES THR_TRACE = YES INCDIRS += targets/mega2560 targets/stock FatFs FatFs/option - BOARDSRC = targets/mega2560/board_mega2560.cpp + BOARDSRC = main_avr.cpp targets/mega2560/board_mega2560.cpp EXTRABOARDSRC = targets/mega2560/lcd_driver_ST7565R.cpp targets/common_avr/telemetry_driver.cpp EEPROMSRC = eeprom_common.cpp eeprom_rlc.cpp LCDSRC = lcd_common.cpp lcd_default.cpp @@ -640,7 +640,7 @@ ifeq ($(PCB), $(filter $(PCB), SKY9X 9XRPRO)) CPPDEFS += -DSPORT_FILE_LOG endif INCDIRS += targets/sky9x CoOS/kernel CoOS/portable - BOARDSRC = targets/sky9x/board_sky9x.cpp + BOARDSRC = main_arm.cpp targets/sky9x/board_sky9x.cpp EXTRABOARDSRC = targets/sky9x/lcd_driver.cpp SRC += targets/sky9x/core_cm3.c targets/sky9x/board_lowlevel.c targets/sky9x/crt.c targets/sky9x/vectors_sam3s.c SRC += CoOS/kernel/core.c CoOS/kernel/hook.c CoOS/kernel/task.c CoOS/kernel/event.c CoOS/kernel/time.c CoOS/kernel/timer.c CoOS/kernel/flag.c CoOS/kernel/mutex.c CoOS/kernel/serviceReq.c CoOS/portable/GCC/port.c CoOS/portable/arch.c @@ -744,7 +744,7 @@ ifeq ($(PCB), TARANIS) EXTRAINCDIRS += targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx/Include targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/include EXTRAINCDIRS += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_OTG_Driver/inc targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Core/inc EXTRAINCDIRS += targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/inc targets/taranis/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/hid/inc - BOARDSRC = targets/taranis/board_taranis.cpp + BOARDSRC = main_arm.cpp targets/taranis/board_taranis.cpp EXTRABOARDSRC += targets/taranis/configure_pins.cpp targets/taranis/lcd_driver.cpp targets/taranis/flash_driver.cpp targets/taranis/aspi.c targets/taranis/i2c.c targets/taranis/delays.c SRC += CoOS/kernel/core.c CoOS/kernel/hook.c CoOS/kernel/task.c CoOS/kernel/event.c CoOS/kernel/time.c CoOS/kernel/timer.c CoOS/kernel/flag.c CoOS/kernel/mutex.c CoOS/kernel/serviceReq.c CoOS/portable/GCC/port.c CoOS/portable/arch.c SRC += targets/taranis/pwr_driver.c targets/taranis/usb_driver.c diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp new file mode 100644 index 000000000..9707d123a --- /dev/null +++ b/radio/src/main_arm.cpp @@ -0,0 +1,162 @@ +/* + * Authors (alphabetical order) + * - Andre Bernet + * - Andreas Weitl + * - Bertrand Songis + * - Bryan J. Rentoul (Gruvin) + * - Cameron Weeks + * - Erez Raviv + * - Gabriel Birkus + * - Jean-Pierre Parisy + * - Karl Szmutny + * - Michael Blandford + * - Michal Hlavinka + * - Pat Mackenzie + * - Philip Moss + * - Rob Thomson + * - Romolo Manfredini + * - Thomas Husterer + * + * opentx is based on code named + * gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/, + * er9x by Erez Raviv: http://code.google.com/p/er9x/, + * and the original (and ongoing) project by + * Thomas Husterer, th9x: http://code.google.com/p/th9x/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include "opentx.h" + +static uint8_t currentSpeakerVolume = 255; +uint8_t requiredSpeakerVolume = 255; + +extern void checkBattery(); + +void handleUsbConnection() +{ +#if defined(PCBTARANIS) && !defined(SIMU) + static bool usbStarted = false; + if (!usbStarted && usbPlugged()) { +#if defined(USB_MASS_STORAGE) + opentxClose(); +#endif + usbStart(); +#if defined(USB_MASS_STORAGE) + usbPluggedIn(); +#endif + usbStarted = true; + } + +#if defined(USB_JOYSTICK) + if (usbStarted) { + if (!usbPlugged()) { + //disable USB + usbStop(); + usbStarted = false; + } + else { + usbJoystickUpdate(); + } + } +#endif +#endif //#if defined(PCBTARANIS) && !defined(SIMU) +} + +void checkSpeakerVolume() +{ + if (currentSpeakerVolume != requiredSpeakerVolume) { + currentSpeakerVolume = requiredSpeakerVolume; + setVolume(currentSpeakerVolume); + } +} + +void checkEeprom() +{ + if (!usbPlugged()) { + // TODO merge these 2 branches +#if defined(PCBSKY9X) + if (Eeprom32_process_state != E32_IDLE) + ee32_process(); + else if (TIME_TO_WRITE()) + eeCheck(false); +#else + if (theFile.isWriting()) + theFile.nextWriteStep(); + else if (TIME_TO_WRITE()) + eeCheck(false); +#endif + } +} + +void perMainArm() +{ +#if defined(PCBSKY9X) && !defined(REVA) + calcConsumption(); +#endif + checkSpeakerVolume(); + checkEeprom(); + sdMountPoll(); + writeLogs(); + handleUsbConnection(); + checkTrainerSettings(); + checkBattery(); + + uint8_t evt = getEvent(false); + if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on + checkBacklight(); +#if defined(NAVIGATION_STICKS) + uint8_t sticks_evt = getSticksNavigationEvent(); + if (sticks_evt) evt = sticks_evt; +#endif + +#if defined(USB_MASS_STORAGE) + if (usbPlugged()) { + // disable access to menus + lcd_clear(); + menuMainView(0); + lcdRefresh(); + return; + } +#endif + + // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) + luaRunNonGuiScripts(); + + // draw LCD from menus or from Lua script + if (luaRunGuiScripts(evt)) { // either stand-alone or telemetry scripts + // let Lua manage LCD fully + } + else { + // normal GUI from menus + const char *warn = s_warning; + uint8_t menu = s_menu_count; + lcd_clear(); + if (menuEvent) { + m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; + m_posHorz = 0; + evt = menuEvent; + menuEvent = 0; + AUDIO_MENUS(); + } + g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + if (warn) DISPLAY_WARNING(evt); + if (menu) { + const char * result = displayMenu(evt); + if (result) { + menuHandler(result); + putEvent(EVT_MENU_UP); + } + } + drawStatusLine(); + } + lcdRefresh(); +} diff --git a/radio/src/main_avr.cpp b/radio/src/main_avr.cpp new file mode 100644 index 000000000..e0a58f2c5 --- /dev/null +++ b/radio/src/main_avr.cpp @@ -0,0 +1,165 @@ +/* + * Authors (alphabetical order) + * - Andre Bernet + * - Andreas Weitl + * - Bertrand Songis + * - Bryan J. Rentoul (Gruvin) + * - Cameron Weeks + * - Erez Raviv + * - Gabriel Birkus + * - Jean-Pierre Parisy + * - Karl Szmutny + * - Michael Blandford + * - Michal Hlavinka + * - Pat Mackenzie + * - Philip Moss + * - Rob Thomson + * - Romolo Manfredini + * - Thomas Husterer + * + * opentx is based on code named + * gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/, + * er9x by Erez Raviv: http://code.google.com/p/er9x/, + * and the original (and ongoing) project by + * Thomas Husterer, th9x: http://code.google.com/p/th9x/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include "opentx.h" + +extern void checkBattery(); +extern uint8_t checkTrim(uint8_t event); + +void perMain() +{ +#if defined(SIMU) + doMixerCalculations(); +#endif + uint16_t t0 = getTmr16KHz(); + int16_t delta = (nextMixerEndTime - lastMixerDuration) - t0; + if (delta > 0 && delta < MAX_MIXER_DELTA) { +#if defined(PCBSTD) && defined(ROTARY_ENCODER_NAVIGATION) + rotencPoll(); +#endif + + // @@@ open.20.fsguruh + // SLEEP(); // wouldn't that make sense? should save a lot of battery power!!! +/* for future use; currently very very beta... */ +#if defined(POWER_SAVE) + ADCSRA&=0x7F; // disable ADC for power saving + ACSR&=0xF7; // disable ACIE Interrupts + ACSR|=0x80; // disable Analog Comparator + // maybe we disable here a lot more hardware components in future to save even more power + + + + MCUCR|=0x20; // enable Sleep (bit5) + // MCUCR|=0x28; // enable Sleep (bit5) enable ADC Noise Reduction (bit3) + // first tests showed: simple sleep would reduce cpu current from 40.5mA to 32.0mA + // noise reduction sleep would reduce it down to 28.5mA; However this would break pulses in theory + // however with standard module, it will need about 95mA. Therefore the drop to 88mA is not much noticable + do { + asm volatile(" sleep \n\t"); // if _SLEEP() is not defined use this + t0=getTmr16KHz(); + delta= (nextMixerEndTime - lastMixerDuration) - t0; + } while ((delta>0) && (delta maxMixerDuration) maxMixerDuration = t0; + +#if defined(MODULE_ALWAYS_SEND_PULSES) + if (startupWarningState < STARTUP_WARNING_DONE) { + // don't do menu's until throttle and switch warnings are handled + return; + } +#endif + + if (!eeprom_buffer_size) { + if (theFile.isWriting()) + theFile.nextWriteStep(); + else if (TIME_TO_WRITE()) + eeCheck(false); + } + +#if defined(SDCARD) + sdMountPoll(); + writeLogs(); +#endif + + uint8_t evt = getEvent(); + evt = checkTrim(evt); + + if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on + checkBacklight(); + +#if (defined(FRSKY) || defined(MAVLINK)) + telemetryWakeup(); +#endif + +#if defined(NAVIGATION_STICKS) + uint8_t sticks_evt = getSticksNavigationEvent(); + if (sticks_evt) evt = sticks_evt; +#endif + + const char *warn = s_warning; + uint8_t menu = s_menu_count; + + lcd_clear(); + if (menuEvent) { + m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; + m_posHorz = 0; + evt = menuEvent; + menuEvent = 0; + AUDIO_MENUS(); + } + g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); + + + if (warn) DISPLAY_WARNING(evt); +#if defined(NAVIGATION_MENUS) + if (menu) { + const char * result = displayMenu(evt); + if (result) { + menuHandler(result); + putEvent(EVT_MENU_UP); + } + } +#endif + + drawStatusLine(); + + lcdRefresh(); + + if (SLAVE_MODE()) { + JACK_PPM_OUT(); + } + else { + JACK_PPM_IN(); + } + + checkBattery(); +} diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index cf26d952f..6751626e9 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -2204,135 +2204,6 @@ void checkBattery() } -#if !defined(CPUARM) -void perMain() -{ -#if defined(SIMU) - doMixerCalculations(); -#endif - uint16_t t0 = getTmr16KHz(); - int16_t delta = (nextMixerEndTime - lastMixerDuration) - t0; - if (delta > 0 && delta < MAX_MIXER_DELTA) { -#if defined(PCBSTD) && defined(ROTARY_ENCODER_NAVIGATION) - rotencPoll(); -#endif - - // @@@ open.20.fsguruh - // SLEEP(); // wouldn't that make sense? should save a lot of battery power!!! -/* for future use; currently very very beta... */ -#if defined(POWER_SAVE) - ADCSRA&=0x7F; // disable ADC for power saving - ACSR&=0xF7; // disable ACIE Interrupts - ACSR|=0x80; // disable Analog Comparator - // maybe we disable here a lot more hardware components in future to save even more power - - - - MCUCR|=0x20; // enable Sleep (bit5) - // MCUCR|=0x28; // enable Sleep (bit5) enable ADC Noise Reduction (bit3) - // first tests showed: simple sleep would reduce cpu current from 40.5mA to 32.0mA - // noise reduction sleep would reduce it down to 28.5mA; However this would break pulses in theory - // however with standard module, it will need about 95mA. Therefore the drop to 88mA is not much noticable - do { - asm volatile(" sleep \n\t"); // if _SLEEP() is not defined use this - t0=getTmr16KHz(); - delta= (nextMixerEndTime - lastMixerDuration) - t0; - } while ((delta>0) && (delta maxMixerDuration) maxMixerDuration = t0; - -#if defined(MODULE_ALWAYS_SEND_PULSES) - if (startupWarningState < STARTUP_WARNING_DONE) { - // don't do menu's until throttle and switch warnings are handled - return; - } -#endif - - if (!eeprom_buffer_size) { - if (theFile.isWriting()) - theFile.nextWriteStep(); - else if (TIME_TO_WRITE()) - eeCheck(false); - } - -#if defined(SDCARD) - sdMountPoll(); - writeLogs(); -#endif - - uint8_t evt = getEvent(); - evt = checkTrim(evt); - - if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on - checkBacklight(); - -#if (defined(FRSKY) || defined(MAVLINK)) - telemetryWakeup(); -#endif - -#if defined(NAVIGATION_STICKS) - uint8_t sticks_evt = getSticksNavigationEvent(); - if (sticks_evt) evt = sticks_evt; -#endif - - const char *warn = s_warning; - uint8_t menu = s_menu_count; - - lcd_clear(); - if (menuEvent) { - m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; - m_posHorz = 0; - evt = menuEvent; - menuEvent = 0; - AUDIO_MENUS(); - } - g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); - - - if (warn) DISPLAY_WARNING(evt); -#if defined(NAVIGATION_MENUS) - if (menu) { - const char * result = displayMenu(evt); - if (result) { - menuHandler(result); - putEvent(EVT_MENU_UP); - } - } -#endif - - drawStatusLine(); - - lcdRefresh(); - - if (SLAVE_MODE()) { - JACK_PPM_OUT(); - } - else { - JACK_PPM_IN(); - } - - checkBattery(); -} -#endif // #if !defined(CPUARM) - - - int16_t g_ppmIns[NUM_TRAINER]; uint8_t ppmInState = 0; // 0=unsync 1..8= wait for value i-1 uint8_t ppmInValid = 0; diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 48b931ac8..26fe67097 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -36,131 +36,6 @@ #include "opentx.h" -static uint8_t currentSpeakerVolume = 255; -uint8_t requiredSpeakerVolume = 255; - -extern void checkBattery(); - -void handleUsbConnection() -{ -#if defined(PCBTARANIS) && !defined(SIMU) - static bool usbStarted = false; - if (!usbStarted && usbPlugged()) { -#if defined(USB_MASS_STORAGE) - opentxClose(); -#endif - usbStart(); -#if defined(USB_MASS_STORAGE) - usbPluggedIn(); -#endif - usbStarted = true; - } - -#if defined(USB_JOYSTICK) - if (usbStarted) { - if (!usbPlugged()) { - //disable USB - usbStop(); - usbStarted = false; - } - else { - usbJoystickUpdate(); - } - } -#endif -#endif //#if defined(PCBTARANIS) && !defined(SIMU) -} - -void checkSpeakerVolume() -{ - if (currentSpeakerVolume != requiredSpeakerVolume) { - currentSpeakerVolume = requiredSpeakerVolume; - setVolume(currentSpeakerVolume); - } -} - -void checkEeprom() -{ - if (!usbPlugged()) { - // TODO merge these 2 branches -#if defined(PCBSKY9X) - if (Eeprom32_process_state != E32_IDLE) - ee32_process(); - else if (TIME_TO_WRITE()) - eeCheck(false); -#else - if (theFile.isWriting()) - theFile.nextWriteStep(); - else if (TIME_TO_WRITE()) - eeCheck(false); -#endif - } -} - -void perMainArm() -{ -#if defined(PCBSKY9X) && !defined(REVA) - calcConsumption(); -#endif - checkSpeakerVolume(); - checkEeprom(); - sdMountPoll(); - writeLogs(); - handleUsbConnection(); - checkTrainerSettings(); - checkBattery(); - - uint8_t evt = getEvent(false); - if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on - checkBacklight(); -#if defined(NAVIGATION_STICKS) - uint8_t sticks_evt = getSticksNavigationEvent(); - if (sticks_evt) evt = sticks_evt; -#endif - -#if defined(USB_MASS_STORAGE) - if (usbPlugged()) { - // disable access to menus - lcd_clear(); - menuMainView(0); - lcdRefresh(); - return; - } -#endif - - // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) - luaRunNonGuiScripts(); - - // draw LCD from menus or from Lua script - if (luaRunGuiScripts(evt)) { // either stand-alone or telemetry scripts - // let Lua manage LCD fully - } - else { - // normal GUI from menus - const char *warn = s_warning; - uint8_t menu = s_menu_count; - lcd_clear(); - if (menuEvent) { - m_posVert = menuEvent == EVT_ENTRY_UP ? g_menuPos[g_menuStackPtr] : 0; - m_posHorz = 0; - evt = menuEvent; - menuEvent = 0; - AUDIO_MENUS(); - } - g_menuStack[g_menuStackPtr]((warn || menu) ? 0 : evt); - if (warn) DISPLAY_WARNING(evt); - if (menu) { - const char * result = displayMenu(evt); - if (result) { - menuHandler(result); - putEvent(EVT_MENU_UP); - } - } - drawStatusLine(); - } - lcdRefresh(); -} - #define MENUS_STACK_SIZE 2000 #define MIXER_STACK_SIZE 500 #define AUDIO_STACK_SIZE 500 From f8203b72f70967d93260ea90faf8a8e62c3b3c93 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 10:12:26 +0100 Subject: [PATCH 14/19] perMainArm() renamed to perMain() --- radio/src/main_arm.cpp | 2 +- radio/src/opentx.h | 22 ++++++++++------------ radio/src/targets/simu/simpgmspace.cpp | 4 +--- radio/src/tasks_arm.cpp | 2 +- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index 9707d123a..d8427d854 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -97,7 +97,7 @@ void checkEeprom() } } -void perMainArm() +void perMain() { #if defined(PCBSKY9X) && !defined(REVA) calcConsumption(); diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 1e25023fe..2d6297d3e 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -727,11 +727,9 @@ void evalMixes(uint8_t tick10ms); void doMixerCalculations(); #if defined(CPUARM) -void perMainArm(); -void checkTrims(); -#else -void perMain(); + void checkTrims(); #endif +void perMain(); NOINLINE void per10ms(); getvalue_t getValue(mixsrc_t i); @@ -747,15 +745,15 @@ void logicalSwitchesTimerTick(); void logicalSwitchesReset(); #if defined(CPUARM) -void evalLogicalSwitches(bool isCurrentPhase=true); -void logicalSwitchesCopyState(uint8_t src, uint8_t dst); -#define LS_RECURSIVE_EVALUATION_RESET() + void evalLogicalSwitches(bool isCurrentPhase=true); + void logicalSwitchesCopyState(uint8_t src, uint8_t dst); + #define LS_RECURSIVE_EVALUATION_RESET() #else -#define evalLogicalSwitches(xxx) -#define GETSWITCH_RECURSIVE_TYPE uint16_t -extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_used; -extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_value; -#define LS_RECURSIVE_EVALUATION_RESET() s_last_switch_used = 0 + #define evalLogicalSwitches(xxx) + #define GETSWITCH_RECURSIVE_TYPE uint16_t + extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_used; + extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_value; + #define LS_RECURSIVE_EVALUATION_RESET() s_last_switch_used = 0 #endif #if defined(PCBTARANIS) diff --git a/radio/src/targets/simu/simpgmspace.cpp b/radio/src/targets/simu/simpgmspace.cpp index 487f661f6..c9ea5d762 100644 --- a/radio/src/targets/simu/simpgmspace.cpp +++ b/radio/src/targets/simu/simpgmspace.cpp @@ -342,10 +342,8 @@ void *main_thread(void *) #if defined(CPUARM) doMixerCalculations(); checkTrims(); - perMainArm(); -#else - perMain(); #endif + perMain(); sleep(10/*ms*/); } diff --git a/radio/src/tasks_arm.cpp b/radio/src/tasks_arm.cpp index 26fe67097..2e25744e7 100644 --- a/radio/src/tasks_arm.cpp +++ b/radio/src/tasks_arm.cpp @@ -160,7 +160,7 @@ void menusTask(void * pdata) while (pwrCheck() != e_power_off) { U64 start = CoGetOSTime(); - perMainArm(); + perMain(); // TODO remove completely massstorage from sky9x firmware U32 runtime = (U32)(CoGetOSTime() - start); // deduct the thread run-time from the wait, if run-time was more than From 6f46ef23da135e533525b1b3f2ce9be13ef022ee Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 10:24:27 +0100 Subject: [PATCH 15/19] Do lcdWaitDmaEnd() in perMain() --- radio/src/lcd_common.cpp | 1 - radio/src/lcd_default.cpp | 7 ------- radio/src/lcd_taranis.cpp | 4 ---- radio/src/main_arm.cpp | 7 +++++++ 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/radio/src/lcd_common.cpp b/radio/src/lcd_common.cpp index c0639df80..01de783b3 100644 --- a/radio/src/lcd_common.cpp +++ b/radio/src/lcd_common.cpp @@ -47,7 +47,6 @@ void lcd_clear() { - lcdWaitDmaEnd(); memset(displayBuf, 0, DISPLAY_BUFER_SIZE); } diff --git a/radio/src/lcd_default.cpp b/radio/src/lcd_default.cpp index 5e09f5430..f4777abb4 100644 --- a/radio/src/lcd_default.cpp +++ b/radio/src/lcd_default.cpp @@ -41,7 +41,6 @@ #if !defined(CPUARM) void lcd_putcAtt(coord_t x, uint8_t y, const unsigned char c, LcdFlags flags) { - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; const pm_uchar *q = &font_5x7[(c-0x20)*5]; @@ -205,7 +204,6 @@ void lcd_mask(uint8_t *p, uint8_t mask, LcdFlags att) void lcd_plot(coord_t x, coord_t y, LcdFlags att) { - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; if (p= LCD_H) return; if (x+w > LCD_W) { w = LCD_W - x; } - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; uint8_t msk = BITMASK(y%8); while (w--) { @@ -242,7 +239,6 @@ void lcd_vlineStip(coord_t x, int8_t y, int8_t h, uint8_t pat) if (pat==DOTTED && !(y%2)) pat = ~pat; - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; y = (y & 0x07); if (y) { @@ -281,7 +277,6 @@ void lcd_vlineStip(coord_t x, scoord_t y, scoord_t h, uint8_t pat, LcdFlags att) if (pat==DOTTED && !(y%2)) pat = ~pat; - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 8 * LCD_W + x ]; y = (y & 0x07); if (y) { @@ -308,7 +303,6 @@ void lcd_vlineStip(coord_t x, scoord_t y, scoord_t h, uint8_t pat, LcdFlags att) void lcd_invert_line(int8_t y) { - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[y * LCD_W]; for (coord_t x=0; x=LCD_W || y<0 || y>=LCD_H) return; - lcdWaitDmaEnd(); uint8_t *p = &displayBuf[ y / 2 * LCD_W + x ]; uint8_t mask = PIXEL_GREY_MASK(y, att); if (p Date: Sun, 7 Dec 2014 17:05:16 +0100 Subject: [PATCH 16/19] Refactored luaTask() based on script type --- radio/src/lua_api.cpp | 142 +++++------------------------------------ radio/src/lua_api.h | 15 +++-- radio/src/main_arm.cpp | 12 ++-- 3 files changed, 31 insertions(+), 138 deletions(-) diff --git a/radio/src/lua_api.cpp b/radio/src/lua_api.cpp index 07483b2e3..e8abbbac5 100644 --- a/radio/src/lua_api.cpp +++ b/radio/src/lua_api.cpp @@ -1831,7 +1831,7 @@ void luaDoOneRunStandalone(uint8_t evt) } } -bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) +bool luaDoOneRunPermanentScript(uint8_t evt, int i, uint8_t scriptType) { ScriptInternalData & sid = scriptInternalData[i]; if (sid.state != SCRIPT_OK) return false; @@ -1843,11 +1843,10 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) #endif ScriptInputsOutputs * sio = NULL; #if SCRIPT_MIX_FIRST > 0 - if (sid.reference >= SCRIPT_MIX_FIRST && sid.reference <= SCRIPT_MIX_LAST) { + if ((scriptType & RUN_MIX_SCRIPT) && (sid.reference >= SCRIPT_MIX_FIRST && sid.reference <= SCRIPT_MIX_LAST)) { #else - if (sid.reference <= SCRIPT_MIX_LAST) { + if ((scriptType & RUN_MIX_SCRIPT) && (sid.reference <= SCRIPT_MIX_LAST)) { #endif - if (runGuiScripts) return false; //mixer script is not gui script ScriptData & sd = g_model.scriptsData[sid.reference-SCRIPT_MIX_FIRST]; sio = &scriptInputsOutputs[sid.reference-SCRIPT_MIX_FIRST]; inputsCount = sio->inputsCount; @@ -1862,9 +1861,8 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) lua_pushinteger(L, sd.inputs[j] + sio->inputs[j].def); } } - else if (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST) { + else if ((scriptType & RUN_FUNC_SCRIPT) && (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST)) { CustomFunctionData & fn = g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST]; - if (runGuiScripts) return false; //function script is not gui script if (!getSwitch(fn.swtch)) return false; #if defined(SIMU) || defined(DEBUG) filename = fn.play.name; @@ -1876,14 +1874,13 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) TelemetryScriptData & script = g_model.frsky.screens[sid.reference-SCRIPT_TELEMETRY_FIRST].script; filename = script.file; #endif - if (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view) { - if (!runGuiScripts) return false; //telemetry script gui part, do not run this time + if ((scriptType & RUN_TELEM_FG_SCRIPT) && + (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view)) { lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); lua_pushinteger(L, evt); inputsCount = 1; } - else if (sid.background) { - if (runGuiScripts) return false; //telemetry script non-gui part, do not run this time + else if ((scriptType & RUN_TELEM_BG_SCRIPT) && (sid.background)) { lua_rawgeti(L, LUA_REGISTRYINDEX, sid.background); } else { @@ -1948,61 +1945,16 @@ void luaDoGc() } } - -uint32_t lastRunTime; - -void luaRunNonGuiScripts() +bool luaTask(uint8_t evt, uint8_t scriptType, bool allowLcdUsage) { - //execute one cycle of all Lua cripts that don't use LCD (model scripts, function scripts) - - uint32_t t0 = get_tmr10ms(); - static uint32_t lastLuaTime = 0; - uint16_t interval = (lastLuaTime == 0 ? 0 : (t0 - lastLuaTime)); - lastLuaTime = t0; - if (interval > maxLuaInterval) { - maxLuaInterval = interval; - } - - luaLcdAllowed = false; - if (luaState == INTERPRETER_PANIC) return; - if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) return; - - if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) { - luaState = 0; - luaInit(); - if (luaState == INTERPRETER_PANIC) return; - luaLoadPermanentScripts(); - if (luaState == INTERPRETER_PANIC) return; - } - for (int i=0; i maxLuaDuration) { - maxLuaDuration = lastRunTime; - } - return scriptWasRun; } -#if 0 -void luaTask(uint8_t evt) -{ - uint32_t t0 = get_tmr10ms(); - static uint32_t lastLuaTime = 0; - uint16_t interval = (lastLuaTime == 0 ? 0 : (t0 - lastLuaTime)); - lastLuaTime = t0; - if (interval > maxLuaInterval) { - maxLuaInterval = interval; - } - - if (luaState == INTERPRETER_PANIC) { - return; - } - - if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) { - PROTECT_LUA() { - luaDoOneRunStandalone(evt); - } - else { - luaDisable(); - return; - } - UNPROTECT_LUA(); - } - else { - // model scripts - if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) { - luaState = 0; - luaInit(); - if (luaState == INTERPRETER_PANIC) return; - luaLoadPermanentScripts(); - if (luaState == INTERPRETER_PANIC) return; - } - - for (int i=0; i maxLuaDuration) { - maxLuaDuration = t0; - } -} -#endif - int luaGetMemUsed() { return (lua_gc(L, LUA_GCCOUNT, 0) << 10) + lua_gc(L, LUA_GCCOUNTB, 0); diff --git a/radio/src/lua_api.h b/radio/src/lua_api.h index 6df19f118..d3e041253 100644 --- a/radio/src/lua_api.h +++ b/radio/src/lua_api.h @@ -38,6 +38,12 @@ #define luaapi_h +#define RUN_MIX_SCRIPT (1 << 0) +#define RUN_FUNC_SCRIPT (1 << 1) +#define RUN_TELEM_BG_SCRIPT (1 << 2) +#define RUN_TELEM_FG_SCRIPT (1 << 3) +#define RUN_STNDAL_SCRIPT (1 << 4) + #if defined(LUA) struct ScriptInput { @@ -88,8 +94,7 @@ extern ScriptInternalData scriptInternalData[MAX_SCRIPTS]; extern ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS]; void luaClose(); - // void luaInit(); - // void luaTask(uint8_t evt); + bool luaTask(uint8_t evt, uint8_t scriptType, bool allowLcdUsage); void luaExec(const char *filename); int luaGetMemUsed(); #define luaGetCpuUsed(idx) scriptInternalData[idx].instructions @@ -110,19 +115,15 @@ if (setjmp(lj.b) == 0) #define UNPROTECT_LUA() global_lj = lj.previous; } /* restore old error handler */ - void luaRunNonGuiScripts(); - bool luaRunGuiScripts(uint8_t evt); - extern uint16_t maxLuaInterval; extern uint16_t maxLuaDuration; - - #else // #if defined(LUA) #define LUA_LOAD_MODEL_SCRIPTS() #define LUA_LOAD_MODEL_SCRIPT(idx) #define LUA_STANDALONE_SCRIPT_RUNNING() (0) #define luaRunNonGuiScripts() #define luaRunGuiScripts(evt) (false) + #define luaAllowLcdUsage(allow) #endif #endif // #ifndef luaapi_h diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index 406a0ff0f..df789d239 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -129,7 +129,7 @@ void perMain() #endif // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) - luaRunNonGuiScripts(); + luaTask(0, RUN_MIX_SCRIPT | RUN_FUNC_SCRIPT | RUN_TELEM_BG_SCRIPT, false); // wait for LCD DMA to finish before continuing, because code from this point // is allowed to change the contents of LCD buffer @@ -139,10 +139,12 @@ void perMain() lcdWaitDmaEnd(); // draw LCD from menus or from Lua script - if (luaRunGuiScripts(evt)) { // either stand-alone or telemetry scripts - // let Lua manage LCD fully - } - else { + // run Lua scripts that use LCD + bool scriptWasRun = luaTask(evt, RUN_TELEM_FG_SCRIPT | RUN_STNDAL_SCRIPT, true); + + // TODO luaTask timing must be done here + + if (!scriptWasRun) { // normal GUI from menus const char *warn = s_warning; uint8_t menu = s_menu_count; From f0cd4fbe8db7684d2c9ee94a0362a56e038aca86 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 17:12:51 +0100 Subject: [PATCH 17/19] Compilation fixes (thanks to Jenkins) --- radio/src/lua_api.h | 4 +--- radio/src/main_arm.cpp | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/radio/src/lua_api.h b/radio/src/lua_api.h index d3e041253..f461329fb 100644 --- a/radio/src/lua_api.h +++ b/radio/src/lua_api.h @@ -121,9 +121,7 @@ #define LUA_LOAD_MODEL_SCRIPTS() #define LUA_LOAD_MODEL_SCRIPT(idx) #define LUA_STANDALONE_SCRIPT_RUNNING() (0) - #define luaRunNonGuiScripts() - #define luaRunGuiScripts(evt) (false) - #define luaAllowLcdUsage(allow) + #define luaTask(evt, scriptType, allowLcdUsage) (false) #endif #endif // #ifndef luaapi_h diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index df789d239..862af5d33 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -129,7 +129,7 @@ void perMain() #endif // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) - luaTask(0, RUN_MIX_SCRIPT | RUN_FUNC_SCRIPT | RUN_TELEM_BG_SCRIPT, false); + (void)luaTask(0, RUN_MIX_SCRIPT | RUN_FUNC_SCRIPT | RUN_TELEM_BG_SCRIPT, false); // wait for LCD DMA to finish before continuing, because code from this point // is allowed to change the contents of LCD buffer From 55f0781bc9918366333bbeeb0d72df15debd217f Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 17:23:44 +0100 Subject: [PATCH 18/19] Lua task timing done in perMain() --- radio/src/lcd.h | 4 ++-- radio/src/lua_api.h | 1 - radio/src/main_arm.cpp | 23 ++++++++++++++++++++--- radio/src/targets/taranis/lcd_driver.cpp | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/radio/src/lcd.h b/radio/src/lcd.h index 7cff29233..666651f86 100644 --- a/radio/src/lcd.h +++ b/radio/src/lcd.h @@ -170,9 +170,9 @@ #endif #if defined(PCBTARANIS) && defined(REVPLUS) && !defined(LCD_DUAL_BUFFER) && !defined(SIMU) - void lcdWaitDmaEnd(); + void lcdRefreshWait(); #else - #define lcdWaitDmaEnd() + #define lcdRefreshWait() #endif extern coord_t lcdLastPos; diff --git a/radio/src/lua_api.h b/radio/src/lua_api.h index f461329fb..0dd0bc8ad 100644 --- a/radio/src/lua_api.h +++ b/radio/src/lua_api.h @@ -121,7 +121,6 @@ #define LUA_LOAD_MODEL_SCRIPTS() #define LUA_LOAD_MODEL_SCRIPT(idx) #define LUA_STANDALONE_SCRIPT_RUNNING() (0) - #define luaTask(evt, scriptType, allowLcdUsage) (false) #endif #endif // #ifndef luaapi_h diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index 862af5d33..9a9f4e741 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -128,23 +128,40 @@ void perMain() } #endif +#if defined(LUA) + + uint32_t t0 = get_tmr10ms(); + static uint32_t lastLuaTime = 0; + uint16_t interval = (lastLuaTime == 0 ? 0 : (t0 - lastLuaTime)); + lastLuaTime = t0; + if (interval > maxLuaInterval) { + maxLuaInterval = interval; + } + // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) - (void)luaTask(0, RUN_MIX_SCRIPT | RUN_FUNC_SCRIPT | RUN_TELEM_BG_SCRIPT, false); + luaTask(0, RUN_MIX_SCRIPT | RUN_FUNC_SCRIPT | RUN_TELEM_BG_SCRIPT, false); // wait for LCD DMA to finish before continuing, because code from this point // is allowed to change the contents of LCD buffer // // WARNING: make sure no code above this line does any change to the LCD display buffer! // - lcdWaitDmaEnd(); + lcdRefreshWait(); // draw LCD from menus or from Lua script // run Lua scripts that use LCD bool scriptWasRun = luaTask(evt, RUN_TELEM_FG_SCRIPT | RUN_STNDAL_SCRIPT, true); - // TODO luaTask timing must be done here + t0 = get_tmr10ms() - t0; + if (t0 > maxLuaDuration) { + maxLuaDuration = t0; + } if (!scriptWasRun) { +#else + lcdRefreshWait(); // WARNING: make sure no code above this line does any change to the LCD display buffer! + { +#endif // normal GUI from menus const char *warn = s_warning; uint8_t menu = s_menu_count; diff --git a/radio/src/targets/taranis/lcd_driver.cpp b/radio/src/targets/taranis/lcd_driver.cpp index 148b84096..533c83fb0 100644 --- a/radio/src/targets/taranis/lcd_driver.cpp +++ b/radio/src/targets/taranis/lcd_driver.cpp @@ -180,7 +180,7 @@ void Set_Address(u8 x, u8 y) volatile bool lcd_busy; #if !defined(LCD_DUAL_BUFFER) -void lcdWaitDmaEnd() +void lcdRefreshWait() { WAIT_FOR_DMA_END(); } From 36a7d4732fef345df44b85e2f4486b008cf91e43 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Mon, 8 Dec 2014 17:51:53 +0100 Subject: [PATCH 19/19] luaDoOneRunPermanentScript() param optimization, todo for luaLcdLock() removal --- radio/src/lua_api.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/radio/src/lua_api.cpp b/radio/src/lua_api.cpp index e8abbbac5..712876b63 100644 --- a/radio/src/lua_api.cpp +++ b/radio/src/lua_api.cpp @@ -344,6 +344,8 @@ static int luaGetGeneralSettings(lua_State *L) static int luaLcdLock(lua_State *L) { + // disabled in opentx 2.1 + // TODO: remove this function completely in opentx 2.2 return 0; } @@ -1831,7 +1833,7 @@ void luaDoOneRunStandalone(uint8_t evt) } } -bool luaDoOneRunPermanentScript(uint8_t evt, int i, uint8_t scriptType) +bool luaDoOneRunPermanentScript(uint8_t evt, int i, uint32_t scriptType) { ScriptInternalData & sid = scriptInternalData[i]; if (sid.state != SCRIPT_OK) return false;