From f2154feda1ad0056f0eae3dc10e8c8d38c53c310 Mon Sep 17 00:00:00 2001 From: bsongis Date: Tue, 2 Oct 2012 19:11:22 +0000 Subject: [PATCH] [sky9x] Stack probe [sky9x] No sprintf (needed heap) [sky9x] Bug when retrieving available SD audio files --- src/Makefile | 12 +---- src/frsky.cpp | 17 ------- src/lcd.cpp | 30 +++++++---- src/open9x.cpp | 74 +++++++++++---------------- src/open9x.h | 5 -- src/sky9x/audio.cpp | 6 +++ src/sky9x/massstorage.cpp | 101 ++++++++++++++++++++++++++++++++----- src/sky9x/sam3s4c_flash.ld | 7 +-- src/statistics_views.cpp | 47 +++++++---------- 9 files changed, 168 insertions(+), 131 deletions(-) diff --git a/src/Makefile b/src/Makefile index c8d9045a3..cfd2e738d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -128,10 +128,6 @@ PPM_LIMITS_UNIT = PERCENT # Values = YES, NO DECIMALS = YES -# DISPLAY_USER_DATA to display on screen data from FrSky module (testing/demo purpose) -# Values = YES, NO -DISPLAY_USER_DATA = NO - # FrSky Hub # Values = YES, NO FRSKY_HUB = YES @@ -438,7 +434,6 @@ ifeq ($(PCB), X9D) SRC += sky9x/usb/device/core/USBD_UDP.c sky9x/usb/device/core/USBDDriver.c SRC += sky9x/usb/device/massstorage/MSDDriver.c sky9x/usb/device/massstorage/MSDDStateMachine.c sky9x/usb/device/massstorage/MSDLun.c sky9x/usb/device/massstorage/MSDDriverDescriptors.c sky9x/usb/device/massstorage/SBCMethods.c SRC += sky9x/usb/common/core/USBEndpointDescriptor.c sky9x/usb/common/core/USBGenericRequest.c sky9x/usb/common/core/USBFeatureRequest.c sky9x/usb/common/core/USBInterfaceRequest.c sky9x/usb/common/core/USBGetDescriptorRequest.c sky9x/usb/common/core/USBSetAddressRequest.c sky9x/usb/common/core/USBSetConfigurationRequest.c sky9x/usb/common/core/USBConfigurationDescriptor.c sky9x/usb/common/core/USBGenericDescriptor.c - SRC += sky9x/syscalls.c SRC += sky9x/MEDSdcard.c EEPROMSRC = eeprom_arm.cpp PULSESSRC = pulses_arm.cpp @@ -458,6 +453,7 @@ ifeq ($(PCB), X9D) ifeq ($(DEBUG), YES) CPPSRC += sky9x/debug.cpp + SRC += sky9x/syscalls.c endif endif @@ -472,7 +468,6 @@ ifeq ($(PCB), SKY9X) SRC += sky9x/usb/device/core/USBD_UDP.c sky9x/usb/device/core/USBDDriver.c SRC += sky9x/usb/device/massstorage/MSDDriver.c sky9x/usb/device/massstorage/MSDDStateMachine.c sky9x/usb/device/massstorage/MSDLun.c sky9x/usb/device/massstorage/MSDDriverDescriptors.c sky9x/usb/device/massstorage/SBCMethods.c SRC += sky9x/usb/common/core/USBEndpointDescriptor.c sky9x/usb/common/core/USBGenericRequest.c sky9x/usb/common/core/USBFeatureRequest.c sky9x/usb/common/core/USBInterfaceRequest.c sky9x/usb/common/core/USBGetDescriptorRequest.c sky9x/usb/common/core/USBSetAddressRequest.c sky9x/usb/common/core/USBSetConfigurationRequest.c sky9x/usb/common/core/USBConfigurationDescriptor.c sky9x/usb/common/core/USBGenericDescriptor.c - SRC += sky9x/syscalls.c SRC += sky9x/MEDSdcard.c EEPROMSRC = eeprom_arm.cpp PULSESSRC = pulses_arm.cpp @@ -493,6 +488,7 @@ ifeq ($(PCB), SKY9X) ifeq ($(DEBUG), YES) CPPSRC += sky9x/debug.cpp + SRC += sky9x/syscalls.c endif endif @@ -614,10 +610,6 @@ ifeq ($(TEMPLATES), YES) CPPSRC += templates.cpp endif -ifeq ($(DISPLAY_USER_DATA), YES) - CPPDEFS += -DDISPLAY_USER_DATA -endif - ifeq ($(PXX), YES) CPPDEFS += -DPXX endif diff --git a/src/frsky.cpp b/src/frsky.cpp index 520e52f28..1c1a5f003 100644 --- a/src/frsky.cpp +++ b/src/frsky.cpp @@ -67,23 +67,6 @@ FrskyData frskyData; uint8_t barsThresholds[THLD_MAX]; #endif -#ifdef DISPLAY_USER_DATA -/* - Copies all available bytes (up to max bufsize) from frskyUserData circular - buffer into supplied *buffer. Returns number of bytes copied (or zero) -*/ -uint8_t frskyGetUserData(char *buffer, uint8_t bufSize) -{ - uint8_t i = 0; - while (!frskyUserData.isEmpty()) - { - buffer[i] = frskyUserData.get(); - i++; - } - return i; -} -#endif - uint16_t getChannelRatio(uint8_t channel) { return (uint16_t)g_model.frsky.channels[channel].ratio << g_model.frsky.channels[channel].multiplier; diff --git a/src/lcd.cpp b/src/lcd.cpp index 610490786..4bd03a255 100644 --- a/src/lcd.cpp +++ b/src/lcd.cpp @@ -38,6 +38,7 @@ uint8_t displayBuf[DISPLAY_W*DISPLAY_H/8]; #define DISPLAY_END (displayBuf+sizeof(displayBuf)) +#define ASSERT_IN_DISPLAY(p) assert((p) >= displayBuf && (p) < DISPLAY_END) #ifdef SIMU bool lcd_refresh = true; @@ -60,6 +61,7 @@ void lcd_img(uint8_t x, uint8_t y, const pm_uchar * img, uint8_t idx, uint8_t mo uint8_t *p = &displayBuf[ (y / 8 + yb) * DISPLAY_W + x ]; for (uint8_t i=0; i> (8-ym8)))) + (b >> (8-ym8)); } + if (ym8) { + uint8_t *r = p + DISPLAY_W; + if (r> (8-ym8)))) + (b >> (8-ym8)); + } #ifdef BOLD_FONT if (mode & BOLD) { + ASSERT_IN_DISPLAY(p+1); if (inv) *(p+1) &= (b << ym8); else @@ -342,7 +352,7 @@ void lcd_outdezNAtt(uint8_t x, uint8_t y, int16_t val, LcdFlags flags, uint8_t l void lcd_mask(uint8_t *p, uint8_t mask, uint8_t att) { - assert(p >= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); if (att & BLACK) *p |= mask; @@ -354,7 +364,7 @@ void lcd_mask(uint8_t *p, uint8_t mask, uint8_t att) void lcd_plot(uint8_t x,uint8_t y, uint8_t att) { - uint8_t *p = &displayBuf[ y / 8 * DISPLAY_W + x ]; + uint8_t *p = &displayBuf[ y / 8 * DISPLAY_W + x ]; if (p= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); uint8_t msk = ~(BITMASK(y)-1); h -= 8-y; if (h < 0) @@ -409,13 +419,13 @@ void lcd_vlineStip(uint8_t x, int8_t y, int8_t h, uint8_t pat, uint8_t att) p += DISPLAY_W; } while (h>=8) { - assert(p >= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); lcd_mask(p, pat, att); p += DISPLAY_W; h -= 8; } if (h>0) { - assert(p >= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); lcd_mask(p, (BITMASK(h)-1) & pat, att); } } @@ -433,13 +443,13 @@ void lcd_vlineStip(uint8_t x, int8_t y, int8_t h, uint8_t pat) uint8_t *p = &displayBuf[ y / 8 * DISPLAY_W + x ]; y = y % 8; if (y) { - assert(p >= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); *p ^= ~(BITMASK(y)-1) & pat; p += DISPLAY_W; h -= 8-y; } while (h>0) { - assert(p >= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); *p ^= pat; p += DISPLAY_W; h -= 8; @@ -447,7 +457,7 @@ void lcd_vlineStip(uint8_t x, int8_t y, int8_t h, uint8_t pat) if (h < 0) h += 8; if (h) { p -= DISPLAY_W; - assert(p >= displayBuf && p < DISPLAY_END); + ASSERT_IN_DISPLAY(p); *p ^= ~(BITMASK(h)-1) & pat; } } diff --git a/src/open9x.cpp b/src/open9x.cpp index 4049c4438..4f3aad83c 100644 --- a/src/open9x.cpp +++ b/src/open9x.cpp @@ -35,7 +35,7 @@ #if defined(PCBSKY9X) #define MIXER_STACK_SIZE 500 -#define MENUS_STACK_SIZE 2500 +#define MENUS_STACK_SIZE 1000 #define AUDIO_STACK_SIZE 500 #define BT_STACK_SIZE 500 #define DEBUG_STACK_SIZE 500 @@ -2293,10 +2293,6 @@ void perOut(uint8_t tick10ms) mixWarning = lv_mixWarning; } -#ifdef DISPLAY_USER_DATA -char userDataDisplayBuf[TELEM_SCREEN_BUFFER_SIZE]; -#endif - #if defined(SIMU) #define TIME_TO_WRITE s_eeDirtyMsk #else @@ -2672,27 +2668,6 @@ void perMain() writeLogs(); #endif -#if defined(FRSKY) && defined(DISPLAY_USER_DATA) - char userDataRxBuffer[21]; // Temp buffer used to collect fr-sky user data - - // retrieve bytes from user data buffer and insert into display string, - // scrolling at the 21 character mark (edge of screen) - uint8_t numbytes = frskyGetUserData(userDataRxBuffer, 21); // Get as many bytes as we can - - static uint8_t displayBufferIndex; - for (uint8_t byt=0; byt < numbytes; byt++) - { - displayBufferIndex++; - if (displayBufferIndex > 20) - { - for (int xx=0; xx<20; xx++) // scroll one char left - userDataDisplayBuf[xx] = userDataDisplayBuf[xx+1]; - displayBufferIndex = 20; - } - userDataDisplayBuf[displayBufferIndex] = userDataRxBuffer[byt]; - } -#endif - lcd_clear(); #if defined(PCBSKY9X) && defined(SIMU) @@ -3186,8 +3161,35 @@ ISR(INT6_vect) } #endif //PCBGRUVIN9X+ROTARY_ENCODERS -#if !defined(PCBSKY9X) +#if defined(PCBSKY9X) +void stack_paint() +{ + for (uint16_t i=0; i q ) + *p-- = 0x55 ; +} uint16_t stack_free() { @@ -3200,7 +3202,6 @@ uint16_t stack_free() } return p - &__bss_end ; } - #endif #if defined(PCBGRUVIN9X) @@ -3382,22 +3383,7 @@ int main(void) lcd_init(); -#if !defined(PCBSKY9X) - // Init Stack while interrupts are disabled -#define STACKPTR _SFR_IO16(0x3D) - { - unsigned char *p ; - unsigned char *q ; - - p = (unsigned char *) STACKPTR ; - q = &__bss_end ; - p -= 2 ; - while ( p > q ) - { - *p-- = 0x55 ; - } - } -#endif + stack_paint(); g_menuStack[0] = menuMainView; g_menuStack[1] = menuProcModelSelect; diff --git a/src/open9x.h b/src/open9x.h index 1975d5397..46a62eeef 100644 --- a/src/open9x.h +++ b/src/open9x.h @@ -945,11 +945,6 @@ inline bool isFunctionActive(uint8_t func) return activeFunctions & (1 << (func-FUNC_TRAINER)); } -#ifdef DISPLAY_USER_DATA -#define TELEM_SCREEN_BUFFER_SIZE 21 -extern char userDataDisplayBuf[TELEM_SCREEN_BUFFER_SIZE]; // text buffer for frsky telem. user data experiments -#endif - #if defined(ROTARY_ENCODERS) // Global rotary encoder registers -- 8-bit, 0-255 #if defined(PCBSKY9X) diff --git a/src/sky9x/audio.cpp b/src/sky9x/audio.cpp index b9d42e7a7..496d387d9 100644 --- a/src/sky9x/audio.cpp +++ b/src/sky9x/audio.cpp @@ -72,6 +72,12 @@ extern "C" { void retrieveAvailableAudioFiles() { FILINFO info; +#if _USE_LFN + TCHAR lfn[_MAX_LFN + 1]; + info.lfname = lfn; + info.lfsize = sizeof(lfn); +#endif + char filename[32] = SYSTEM_SOUNDS_PATH "/"; #if !defined WIN32 && defined __GNUC__ diff --git a/src/sky9x/massstorage.cpp b/src/sky9x/massstorage.cpp index c1636fdf8..3506bd770 100644 --- a/src/sky9x/massstorage.cpp +++ b/src/sky9x/massstorage.cpp @@ -78,17 +78,90 @@ Media medias[MAX_LUNS]; /** Device LUNs. */ MSDLun luns[MAX_LUNS]; -char *bytes2Str(char *str, uint32_t bytes) +int itoa(int val, char* buf) { - if (bytes < 1024*100) - sprintf(str, "%lu.%luK", bytes / 1024, (bytes % 1024) / 103); - else if (bytes < 1024*1024) - sprintf(str, "%luK", bytes / 1024); - else if (bytes < 1024*1024*100) - sprintf(str, "%lu.%luM", bytes / (1024*1024), (bytes % (1024*1024)) / 104858); - else - sprintf(str, "%luM", bytes / (1024*1024)); - return str; + const unsigned int radix = 10; + + char* p; + unsigned int a; //every digit + int len; + char* b; //start of the digit char + char temp; + unsigned int u; + + p = buf; + +#if 0 + if (val < 0) + { + *p++ = '-'; + val = 0 - val; + } +#endif + + u = (unsigned int)val; + + b = p; + + do + { + a = u % radix; + u /= radix; + + *p++ = a + '0'; + + } while (u > 0); + + len = (int)(p - buf); + + *p-- = 0; + + //swap + do + { + temp = *p; + *p = *b; + *b = temp; + --p; + ++b; + + } while (b < p); + + return len; +} + +uint8_t write_bytes(char *str, uint32_t bytes) +{ + uint8_t len; + if (bytes < 1024*100) { + len = itoa(bytes / 1024, str); + str[len] = '.'; + str[len+1] = '0' + ((bytes % 1024) / 103); + str[len+2] = 'K'; + str[len+3] = '\0'; + len += 3; + } + else if (bytes < 1024*1024) { + len = itoa(bytes / 1024, str); + str[len] = 'K'; + str[len+1] = '\0'; + len += 1; + } + else if (bytes < 1024*1024*100) { + len = itoa(bytes / (1024*1024), str); + str[len] = '.'; + str[len+1] = '0' + ((bytes % (1024*1024)) / 104858); + str[len+2] = 'M'; + str[len+3] = '\0'; + len += 3; + } + else { + len = itoa(bytes / (1024*1024), str); + str[len] = 'M'; + str[len+1] = '\0'; + len += 1; + } + return len; } static void MSDCallbacks_Data( unsigned char flowDirection, unsigned int dataLength, @@ -99,10 +172,10 @@ static void MSDCallbacks_Data( unsigned char flowDirection, unsigned int dataLen else msdWriteTotal += dataLength; - char rdStr[10]; - char wrStr[10]; - - sprintf(statusLineMsg, "USB Rd:%s Wr:%s", bytes2Str(rdStr, msdReadTotal), bytes2Str(wrStr, msdWriteTotal)); + strcpy(statusLineMsg, "USB Rd:"); + uint8_t len = write_bytes(statusLineMsg+7, msdReadTotal); + strcpy(statusLineMsg+7+len, " Wr:"); + write_bytes(statusLineMsg+7+len+4, msdWriteTotal); showStatusLine(); } diff --git a/src/sky9x/sam3s4c_flash.ld b/src/sky9x/sam3s4c_flash.ld index 8563d14e6..fd8241a61 100644 --- a/src/sky9x/sam3s4c_flash.ld +++ b/src/sky9x/sam3s4c_flash.ld @@ -128,12 +128,13 @@ SECTIONS *(.bss.*) . = ALIGN(4); /* Align the end of the section */ - *(.ram.b) - _end = .; - __end = .; } > RAM _ebss = .; /* Provide the name for the end of this section */ + . = ALIGN(4); /* Align the end of the section */ + __end = .; + _end = .; + /* * The ".stack" section is our stack. * Here this section starts at the end of the ram segment. diff --git a/src/statistics_views.cpp b/src/statistics_views.cpp index a196f5f1f..40b030d14 100644 --- a/src/statistics_views.cpp +++ b/src/statistics_views.cpp @@ -117,43 +117,34 @@ void menuProcDebug(uint8_t event) break; } -#if !defined(PCBSKY9X) +#if defined(PCBSKY9X) +#if defined(REVB) + lcd_putsLeft(1*FH, STR_CPU_CURRENT); + putsTelemetryValue(MENU_DEBUG_COL_OFS, 1*FH, getCurrent(), UNIT_MILLIAMPS, 0); + uint32_t current_scale = 488 + g_eeGeneral.currentCalib; + putsTelemetryValue(20*FW+2, 1*FH, Current_max*10*current_scale/8192, UNIT_RAW, 0); + lcd_putsLeft(2*FH, STR_CPU_MAH); + putsTelemetryValue(MENU_DEBUG_COL_OFS, 2*FH, g_eeGeneral.mAhUsed + Current_used*current_scale/8192/36, UNIT_MAH, PREC1); + putsTime(17*FW, 2*FH, g_eeGeneral.globalTimer + sessionTimer, 0, 0); +#endif + lcd_putsLeft(3*FH, STR_CPU_TEMP); + putsTelemetryValue(MENU_DEBUG_COL_OFS, 3*FH, getTemperature(), UNIT_DEGREES, 0); + putsTelemetryValue(20*FW+2, 3*FH, maxTemperature+g_eeGeneral.temperatureCalib, UNIT_DEGREES, 0); + lcd_putsLeft(4*FH, STR_TMAINMAXMS); + lcd_outdezAtt(MENU_DEBUG_COL_OFS, 4*FH, (g_timeMainMax)/20, PREC2); +#else lcd_putsLeft(1*FH, STR_TMR1LATMAXUS); lcd_outdez8(MENU_DEBUG_COL_OFS , 1*FH, g_tmr1Latency_max/2 ); lcd_putsLeft(2*FH, STR_TMR1LATMINUS); lcd_outdez8(MENU_DEBUG_COL_OFS , 2*FH, g_tmr1Latency_min/2 ); lcd_putsLeft(3*FH, STR_TMR1JITTERUS); lcd_outdez8(MENU_DEBUG_COL_OFS , 3*FH, (g_tmr1Latency_max - g_tmr1Latency_min) /2 ); -#endif - -#if defined(PCBSKY9X) - lcd_putsLeft(5*FH, STR_TMAINMAXMS); - lcd_outdezAtt(MENU_DEBUG_COL_OFS, 5*FH, (g_timeMainMax)/20, PREC2); -#else lcd_putsLeft(4*FH, STR_TMAINMAXMS); lcd_outdezAtt(MENU_DEBUG_COL_OFS, 4*FH, (g_timeMainMax*100)/16, PREC2); #endif -#if defined(PCBSKY9X) -#if defined(REVB) - lcd_putsLeft(2*FH, STR_CPU_CURRENT); - putsTelemetryValue(MENU_DEBUG_COL_OFS, 2*FH, getCurrent(), UNIT_MILLIAMPS, 0); - uint32_t current_scale = 488 + g_eeGeneral.currentCalib; - putsTelemetryValue(20*FW+2, 2*FH, Current_max*10*current_scale/8192, UNIT_RAW, 0); - lcd_putsLeft(3*FH, STR_CPU_MAH); - putsTelemetryValue(MENU_DEBUG_COL_OFS, 3*FH, g_eeGeneral.mAhUsed + Current_used*current_scale/8192/36, UNIT_MAH, PREC1); - putsTime(17*FW, 3*FH, g_eeGeneral.globalTimer + sessionTimer, 0, 0); -#endif + lcd_putsLeft(5*FH, STR_FREESTACKMINB); + lcd_outdezAtt(14*FW, 5*FH, stack_free(), UNSIGN) ; - lcd_putsLeft(4*FH, STR_CPU_TEMP); - putsTelemetryValue(MENU_DEBUG_COL_OFS, 4*FH, getTemperature(), UNIT_DEGREES, 0); - putsTelemetryValue(20*FW+2, 4*FH, maxTemperature+g_eeGeneral.temperatureCalib, UNIT_DEGREES, 0); -#endif - -#if !defined(PCBSKY9X) - lcd_puts( 0*FW, 5*FH, STR_FREESTACKMINB); - lcd_outdezAtt(14*FW, 5*FH, stack_free(), UNSIGN) ; -#endif - - lcd_puts( 3*FW, 7*FH, STR_MENUTORESET); + lcd_puts(3*FW, 7*FH, STR_MENUTORESET); }