STM32和gcc中的常量字符串

时间:2018-08-10 08:52:36

标签: string gcc stm32

我在使用gcc在Flash中使用字符串/指向字符串的指针数组时遇到了问题。控制器为STM32F100VBT6,编译器为Atollic TrueStudio v9.0.1。

关闭优化后,一切正常,但是将优化设置为-s或其他任何设置时,LCD上都会显示垃圾。我可以看到,尽管映射文件正确显示了字符串以及指向字符串的指针数组都在“ rodata”中,但所传递的字符串的指针值是错误的。 这是部分代码...

#define MENU_CONFF_STR_COUNT            (12)
static const char g_strMnuPrev[]         = "[ ]Previous Menu";
static const char g_strMnuConf_F_1[]    = "[ ]PWD Enable?";
static const char g_strMnuConf_F_2[]    = "[ ]System Type";
static const char g_strMnuConf_F_3[]    = "[ ]Set Yr/Mon";
static const char g_strMnuConf_F_4[]    = "[ ]Set Serial";
static const char g_strMnuConf_F_5[]    = "[ ]Set M1";
static const char g_strMnuConf_F_6[]    = "[ ]Set M2";
static const char g_strMnuConf_F_7[]    = "[ ]Set IP-FSW";
static const char g_strMnuConf_F_8[]    = "[ ]Set IP-P1";
static const char g_strMnuConf_F_9[]    = "[ ]Set IP-P2";
static const char g_strMnuConf_F_10[]   = "[ ]Set TCON";
static const char g_strMnuConf_F_11[]   = "[ ]T Calib";
static const char* const g_a_strMnuConfF_P[MENU_CONFF_STR_COUNT] = {
    g_strMnuConf_F_1, g_strMnuConf_F_2 ,g_strMnuConf_F_3,
    g_strMnuConf_F_4, g_strMnuConf_F_5,
    g_strMnuConf_F_6, g_strMnuConf_F_7, g_strMnuConf_F_8,   
    g_strMnuConf_F_9, g_strMnuConf_F_10,
    g_strMnuConf_F_11,  g_strMnuPrev
};
// some code using string using function --> void vLCD_WriteStr_P( const char* pStr_P, INT8U x, INT8U y, BOOL fill );
vLCD_WriteStr_P( (const char*)g_strMnuConf_F_10, 0, LCD_ROW_1, TRUE );
//
// some code using array of pointer to strings -> INT8U ucHandle_Menu( const char* const* mnu_array, INT8U mnu_count )
for( ;; ) {
    switch( ucHandle_Menu( &g_a_strMnuConfF_P[0], MENU_CONFF_STR_COUNT ) ) {
    //....code handling switch()
    }
}

完全相同的代码也可以与Keil完美配合。 那么我对gcc(Atollic Crossstudio)做错了什么?

这是vLCD_WriteStr_P()的代码,

void vLCD_WriteStr_P( const char* pStr_P, INT8U x, INT8U y, BOOL fill )
{
    char ch;

    vLCD_SetXY( x, y );
    for( ;; ) {
        ch = *pStr_P++;
        if( !ch ) {
            break;
        }
        x++;                                                /* col++                                */
        vLCD_CmdData( LCD_DATA, ch );
    }

    if( fill ) {                                            /* fill rest of the row with 'blanks'   */
        while( LCD_COL_MAX > x ) {
            vLCD_CmdData( LCD_DATA, ' ' );
            x++;
        }
    }
}

0 个答案:

没有答案