From 381af741fb4d4844ce6660d7ecf623a07246ebf2 Mon Sep 17 00:00:00 2001 From: "bsongis@gmail.com" Date: Tue, 25 Jun 2013 19:31:20 +0000 Subject: [PATCH] [Taranis] USB deconnection fix [Taranis / sky9x] Less maths for audio (no more sin) --- src/audio_arm.cpp | 48 +++++++--- src/audio_arm.h | 2 +- src/opentx.cpp | 124 +++++++++++++------------- src/simpgmspace.cpp | 1 + src/simpgmspace.h | 1 - src/targets/gruvin9x/board_gruvin9x.h | 3 + src/targets/sky9x/board_sky9x.h | 28 +++--- src/targets/sky9x/diskio.cpp | 8 ++ src/targets/taranis/board_taranis.h | 1 + src/targets/taranis/diskio.cpp | 1 - src/targets/taranis/usbd_usr.cpp | 6 -- 11 files changed, 131 insertions(+), 92 deletions(-) diff --git a/src/audio_arm.cpp b/src/audio_arm.cpp index 2c4a1c8b0..fff6b9d8d 100644 --- a/src/audio_arm.cpp +++ b/src/audio_arm.cpp @@ -39,14 +39,25 @@ extern OS_MutexID audioMutex; -const unsigned int toneVolumes[] = { - 4000, - 8000, - 16000, - 24000, - 32000 +const int16_t sineValues[] = +{ + 0, 64, 128, 191, 254, 316, 376, 435, 493, 548, + 601, 652, 700, 746, 789, 828, 864, 897, 926, 952, + 973, 991, 1005, 1015, 1021, 1024, 1021, 1015, 1005, 991, + 973, 952, 926, 897, 864, 828, 789, 746, 700, 652, + 601, 548, 493, 435, 376, 316, 254, 191, 128, 64, + 0, -64, -128, -191, -254, -316, -376, -435, -493, -548, + -601, -652, -700, -746, -789, -828, -864, -897, -926, -952, + -973, -991, -1005, -1015, -1021, -1024, -1021, -1015, -1005, -991, + -973, -952, -926, -897, -864, -828, -789, -746, -700, -652, + -601, -548, -493, -435, -376, -316, -254, -191, -128, -64, }; +#if 1 + const unsigned int toneVolumes[] = { 2, 4, 8, 12, 16 }; +#else + const unsigned int toneVolumes[] = { 4000, 8000, 16000, 24000, 32000 }; +#endif #if defined(SDCARD) const char * audioFilenames[] = { @@ -423,7 +434,7 @@ int AudioQueue::mixWav(AudioContext &context, AudioBuffer *buffer, int volume, u int AudioQueue::mixBeep(AudioContext &context, AudioBuffer *buffer, int volume, unsigned int fade) { AudioFragment & fragment = context.fragment; - unsigned int duration = 0; + int duration = 0; int result = 0; if (fragment.tone.duration > 0) { @@ -432,9 +443,14 @@ int AudioQueue::mixBeep(AudioContext &context, AudioBuffer *buffer, int volume, int periods = DIM(context.state.tone.points) / ((AUDIO_SAMPLE_RATE / fragment.tone.freq) + 1); context.state.tone.count = (periods * AUDIO_SAMPLE_RATE) / fragment.tone.freq; if (context.state.tone.idx >= context.state.tone.count) context.state.tone.idx = 0; +#if 1 + for (unsigned int i=0; i(AUDIO_BUFFER_DURATION, fragment.tone.duration); - for (unsigned int i=0; idata[i], context.state.tone.points[context.state.tone.idx], fade); context.state.tone.idx = context.state.tone.idx + 1; - if (context.state.tone.idx >= context.state.tone.count) context.state.tone.idx = 0; + if (context.state.tone.idx >= context.state.tone.count) { + context.state.tone.idx = 0; + if (end && i+DIM(context.state.tone.points) > points) break; + } } fragment.tone.duration -= duration; diff --git a/src/audio_arm.h b/src/audio_arm.h index 92c23d5fd..20ecb3088 100644 --- a/src/audio_arm.h +++ b/src/audio_arm.h @@ -104,7 +104,7 @@ class AudioContext { #endif struct { - uint16_t points[100]; + int16_t points[100]; uint8_t count; uint8_t idx; uint16_t freq; diff --git a/src/opentx.cpp b/src/opentx.cpp index eb76b8e4e..00be07a67 100644 --- a/src/opentx.cpp +++ b/src/opentx.cpp @@ -1877,7 +1877,7 @@ uint8_t checkTrim(uint8_t event) setTrimValue(phase, idx, after); #endif -#if defined (AUDIO) +#if defined(AUDIO) // toneFreq higher/lower according to trim position // limit the frequency, range -125 to 125 = toneFreq: 19 to 101 if (after > TRIM_MAX) @@ -3501,6 +3501,63 @@ uint8_t calcStickScroll( uint8_t index ) } #endif +void opentxStart() +{ + doSplash(); + +#if defined(PCBSKY9X) && defined(SDCARD) && !defined(SIMU) + for (int i=0; i<500 && !Card_initialized; i++) { + CoTickDelay(1); // 2ms + } +#endif + +#if defined(CPUARM) + eeLoadModel(g_eeGeneral.currModel); +#endif + + checkAlarm(); + checkAll(); + + if (g_eeGeneral.chkSum != evalChkSum()) { + chainMenu(menuFirstCalib); + } +} + +#if defined(CPUARM) || defined(PCBGRUVIN9X) +void opentxClose() +{ +#if defined(FRSKY) + FRSKY_End(); +#endif + +#if defined(SDCARD) + closeLogs(); + sdDone(); +#endif + +#if defined(HAPTIC) + hapticOff(); +#endif + + saveTimers(); + +#if defined(PCBSKY9X) + uint32_t mAhUsed = g_eeGeneral.mAhUsed + Current_used * (488 + g_eeGeneral.currentCalib) / 8192 / 36; + if (g_eeGeneral.mAhUsed != mAhUsed) + g_eeGeneral.mAhUsed = mAhUsed; +#endif + +#if defined(CPUARM) && !defined(REVA) + if (sessionTimer > 0) + g_eeGeneral.globalTimer += sessionTimer; +#endif + + g_eeGeneral.unexpectedShutdown = 0; + eeDirty(EE_GENERAL); + eeCheck(true); +} +#endif + void perMain() { #if defined(SIMU) @@ -3642,10 +3699,9 @@ void perMain() #if defined(PCBTARANIS) static bool usbStarted = false; if (!usbStarted && usbPlugged()) { - usbStarted = true; - sdDone(); - eeCheck(true); + opentxClose(); usbStart(); + usbStarted = true; } #endif @@ -4083,28 +4139,6 @@ void saveTimers() volatile rotenc_t g_rotenc[1] = {0}; #endif -void opentxStart() -{ - doSplash(); - -#if defined(PCBSKY9X) && defined(SDCARD) && !defined(SIMU) - for (int i=0; i<500 && !Card_initialized; i++) { - CoTickDelay(1); // 2ms - } -#endif - -#if defined(CPUARM) - eeLoadModel(g_eeGeneral.currModel); -#endif - - checkAlarm(); - checkAll(); - - if (g_eeGeneral.chkSum != evalChkSum()) { - chainMenu(menuFirstCalib); - } -} - #ifndef SIMU #if defined(CPUARM) @@ -4310,33 +4344,7 @@ void menusTask(void * pdata) lcd_clear(); displayPopup(STR_SHUTDOWN); -#if defined(FRSKY) - FRSKY_End(); -#endif - -#if defined(SDCARD) - closeLogs(); -#endif - -#if defined(HAPTIC) - hapticOff(); -#endif - - saveTimers(); - -#if defined(PCBSKY9X) - uint32_t mAhUsed = g_eeGeneral.mAhUsed + Current_used * (488 + g_eeGeneral.currentCalib) / 8192 / 36; - if (g_eeGeneral.mAhUsed != mAhUsed) - g_eeGeneral.mAhUsed = mAhUsed; -#endif - - if (sessionTimer > 0) - g_eeGeneral.globalTimer += sessionTimer; - - g_eeGeneral.unexpectedShutdown = 0; - - eeDirty(EE_GENERAL); - eeCheck(true); + opentxClose(); lcd_clear(); lcdRefresh(); @@ -4481,15 +4489,9 @@ int main(void) #if defined(PCBGRUVIN9X) // Time to switch off - lcd_clear() ; + lcd_clear(); displayPopup(STR_SHUTDOWN); - saveTimers(); - g_eeGeneral.unexpectedShutdown=0; - eeDirty(EE_GENERAL); - eeCheck(true); -#if defined(SDCARD) - closeLogs(); -#endif + opentxClose(); lcd_clear() ; lcdRefresh() ; pwrOff(); // Only turn power off if necessary diff --git a/src/simpgmspace.cpp b/src/simpgmspace.cpp index d35f2297f..43e1b52e2 100644 --- a/src/simpgmspace.cpp +++ b/src/simpgmspace.cpp @@ -591,6 +591,7 @@ void lcdRefresh() } #if defined(PCBTARANIS) +void usbStart() { } ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct) { return SUCCESS; } ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct) { return SUCCESS; } void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct) { } diff --git a/src/simpgmspace.h b/src/simpgmspace.h index 4a6d3ec82..c17742987 100644 --- a/src/simpgmspace.h +++ b/src/simpgmspace.h @@ -384,7 +384,6 @@ inline void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) { #define GPIO_ResetBits(GPIOx, pin) GPIOx->BSRRL &= ~pin #define GPIO_ReadInputDataBit(GPIOx, pin) (GPIOx->BSRRL & pin) #define RCC_AHB1PeriphClockCmd(...) -#define GPIO_ReadInputDataBit(...) true #endif #define configure_pins(...) diff --git a/src/targets/gruvin9x/board_gruvin9x.h b/src/targets/gruvin9x/board_gruvin9x.h index 27f71c8f9..f74695e86 100644 --- a/src/targets/gruvin9x/board_gruvin9x.h +++ b/src/targets/gruvin9x/board_gruvin9x.h @@ -181,4 +181,7 @@ void pwrOff(); #define EEPROM_MASSSTORAGE() (0) +// Haptic driver +#define hapticOff() // TODO hapticOn() cleaner ... + #endif diff --git a/src/targets/sky9x/board_sky9x.h b/src/targets/sky9x/board_sky9x.h index a2e0986a3..7710309fc 100644 --- a/src/targets/sky9x/board_sky9x.h +++ b/src/targets/sky9x/board_sky9x.h @@ -175,22 +175,26 @@ void disable_ssc(); #define disable_dsm2(port) disable_ssc() // SD driver -#if !defined(SIMU) -#define sdPoll10ms() -void sdMountPoll(); -extern "C" { -void init_SDcard(); -void sdInit(); -uint32_t sd_card_ready(); -uint32_t sdMounted(); -} +#if defined(SIMU) + #define sdInit() + #define sdDone() +#else + #define sdPoll10ms() + void sdMountPoll(); + extern "C" { + void init_SDcard(); + void sdInit(); + void sdDone(); + uint32_t sd_card_ready(); + uint32_t sdMounted(); + } #endif // WDT driver #if !defined(SIMU) -#define wdt_disable() -#define wdt_enable(x) WDT->WDT_MR = 0x3FFF207F -#define wdt_reset() WDT->WDT_CR = 0xA5000001 + #define wdt_disable() + #define wdt_enable(x) WDT->WDT_MR = 0x3FFF207F + #define wdt_reset() WDT->WDT_CR = 0xA5000001 #endif // Backlight driver diff --git a/src/targets/sky9x/diskio.cpp b/src/targets/sky9x/diskio.cpp index 746dadba7..d7f60e900 100644 --- a/src/targets/sky9x/diskio.cpp +++ b/src/targets/sky9x/diskio.cpp @@ -1089,6 +1089,14 @@ void sdInit() Card_initialized = 1; } +void sdDone() +{ + if (sdMounted()) { + audioQueue.stopSD(); + f_mount(0, 0); // unmount SD + } +} + // Checks for card ready for read/write // returns 1 for YES, 0 for NO uint32_t sd_card_ready( void ) diff --git a/src/targets/taranis/board_taranis.h b/src/targets/taranis/board_taranis.h index b28fc473e..c03b93d88 100644 --- a/src/targets/taranis/board_taranis.h +++ b/src/targets/taranis/board_taranis.h @@ -93,6 +93,7 @@ void delay_01us(uint16_t nb); // SD driver #if defined(SIMU) #define sdInit() + #define sdDone() #else #define SD_IS_HC() (1) #define SD_GET_SIZE_MB() (0) diff --git a/src/targets/taranis/diskio.cpp b/src/targets/taranis/diskio.cpp index c3b2f2e29..5503b3c5e 100644 --- a/src/targets/taranis/diskio.cpp +++ b/src/targets/taranis/diskio.cpp @@ -980,7 +980,6 @@ void sdDone() { if (sdMounted()) { audioQueue.stopSD(); - closeLogs(); #if defined(DEBUG) f_close(&g_telemetryFile); #endif diff --git a/src/targets/taranis/usbd_usr.cpp b/src/targets/taranis/usbd_usr.cpp index bf12deb9c..32fedc43c 100644 --- a/src/targets/taranis/usbd_usr.cpp +++ b/src/targets/taranis/usbd_usr.cpp @@ -119,12 +119,6 @@ void USBD_USR_DeviceConnected (void) */ void USBD_USR_DeviceDisconnected (void) { - // TODO: Separate the shutdown operations in main() into a shutdown() function in opentx.cpp, - // and call that before the NVIC_SystemReset()? - g_eeGeneral.unexpectedShutdown = 0; - eeDirty(EE_GENERAL); - eeCheck(true); - NVIC_SystemReset(); }