This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with more member in a structure

Hi there,
I'm using STM32F103 with Keil ver 4.72
I defined a structure like this:

typedef struct
{
 uint16_t X;
 uint16_t Y;
 uint16_t Width;
 uint16_t Height;
 uint16_t BackColor;
 uint16_t TextColor;
 uint16_t TextBackColor;
} button_t;


I use the struct in main file like this:


button_t btn1;

int main()
{
 btn1.X = 100;
 btn1.Y = 100;
 .
 .
 .
 while(1)
 {
 }
}


When I use this struct in program, every things works well.
The problem is when I add more members into the structure, like this:

typedef struct
{
 uint16_t X;
 uint16_t Y;
 uint16_t Width;
 uint16_t Height;
 uint16_t BackColor;
 uint16_t TextColor;
 uint16_t TextBackColor;

 uint16_t TLX;
 uint16_t TLY;
 uint16_t TRX;
 uint16_t TRY;
 uint16_t BLX;
 uint16_t BLY;
 uint16_t BRX;
 uint16_t BRY;
};


When I do that, program compiles without any problem, but when I download it into the MCU, it doesn't work.
I increased Heap Size from 0x0200 to 0x0800, but took the same result. I set the Heap Size in startup.s file. I'm not sure if it's a right way to set that.

Parents
  • Thank you,
    How can I attach the project here?
    I defined the struct in main.c

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    **
    **      http://www.kavirElectronic.ir & www.kavirelectronic.ir/eshop
    **      support Team:
    **      http://www.IranMicro.ir  & http://www.IranMicro.ir/forum
    **
    **       kavirelectronic.com@gmail.com
    **      0352-7730313  0352-7734146
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    #include "usart.h"
    #include "lcd.h"
    #include <stdio.h>
    #include <string.h>
    #include "stm32f10x.h"
    
    #include "font.h"
    //#include "button.h"
    
    typedef struct
    {
            uint16_t LocationX;
            uint16_t LocationY;
            uint16_t SizeWidth;
            uint16_t SizeHeight;
            uint16_t BackColor;
            uint16_t TextColor;
            uint16_t TextBackColor;
    
    //      uint16_t PosTLX;
    //      uint16_t PosTLY;
    //      uint16_t PosTRX;
    //      uint16_t PosTRY;
    //      uint16_t PosBLX;
    //      uint16_t PosBLY;
    //      uint16_t PosBRX;
    //      uint16_t PosBRY;
    
            char *Text;
    } button_t;
    
    
    button_t Button1;
    
    int main()
    {
            //char str[9];
            u16 x,y;
            //u8 bufff[]="!!!abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    u8 bufff[] = "!!!abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
            Usart_Configuration(115200);
            printf("Â´Â®ØŸØ¹Â³ÙØªÂ¼Â»Â¯Â³Ø©Â¹Â¦\r\n");
    
            Button1.LocationX = 300;
            Button1.LocationY = 300;
            Button1.SizeHeight = 100;
            Button1.SizeWidth = 200;
            Button1.BackColor = 0x0503;
            Button1.TextColor = 0x0000;
            Button1.TextBackColor = 0x0000;
            //Button1.Text = "Aghil";
            //ButtonSetText(&Button1, "Key1");
            //strcpy(Button1.Text, "Key 1");
    //      Button1.Text[0] = 'A';
    //      Button1.Text[1] = 'g';
    //      Button1.Text[2] = 'h';
    //      Button1.Text[3] = 'H';
    //      Button1.Text[4] = NULL;
    
    
            LCD_Init();
            LCD_Clear(0xfff8); //0xf800 //0x0f0000
    
            //LCD_Highlight(0xff0000);
    
            x=y=0;
            //LCD_DisplayString2(0,0,bufff,0,0xffff);
            //LCD_DisplayString2(100,100,Button1.Text,0,0xffff);
    //      for (i=0; i<0xFFFF; i+=100)
    //      {
    //              sprintf(str,"%05u", i);
    //              LCD_DisplayString(0,0,str,0xffff,0x0);
    //              LCD_DisplayString(0,32,bufff,i,i);
    //      }
    
            //LCD_DisplayString2(200,200,"Aghil",0xffff,0x0);
            //LCD_PutChar2(200, 200, 'A', 0xffff, 0x0);
    
            //_DrawRect(200, 200, 300, 300, 0);
            //LCD_FillArea(201, 201, 299, 299, 0x1818);
    //      Button1.FontHeight = acFont32_GetHeight();
    //      Button1.FontWidthMin = acFont32_GetMinWidth();
    //      Button1.FontWidthMax = acFont32_GetMaxWidth();
            //ButtonShow(&Button1);
    
            while(1);
    //      while(1){
    //
    //              while(!(USART1->SR&USART_FLAG_RXNE));
    //              if(x >799)                     /* ¼ى²éت£سà ؟ص¼نتا·ٌ×م¹»         */
    //          {
    //                  x = 0;
    //              y = y + 16;                        /* ¸ؤ±ندشت¾×ّ±ê                 */
    //              if(y > 479)                       /* ×Ù×ّ±ê³¬³٠                  */
    //              {
    //                          y = 0;
    //              }
    //              }
    
    //              LCD_PutChar(x,y,USART1->DR,0,0xffff);
    //              x += 8;
    //      }
    
    
    }
    
    

    As you can see in the code, I just assigned some value to some of structure members, and I don't use it in other places. When I Uncommecnt variables shown in structure, program doesn't run on MCU. Also I can't debug in that case. When I stop debug mode, an error occurs and tells about JTAG cable. When I Comment those varibles, program run properly and I can debug properly, too.
    I can send you project files, just tell me how to share them.

Reply
  • Thank you,
    How can I attach the project here?
    I defined the struct in main.c

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    **
    **      http://www.kavirElectronic.ir & www.kavirelectronic.ir/eshop
    **      support Team:
    **      http://www.IranMicro.ir  & http://www.IranMicro.ir/forum
    **
    **       kavirelectronic.com@gmail.com
    **      0352-7730313  0352-7734146
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
    #include "usart.h"
    #include "lcd.h"
    #include <stdio.h>
    #include <string.h>
    #include "stm32f10x.h"
    
    #include "font.h"
    //#include "button.h"
    
    typedef struct
    {
            uint16_t LocationX;
            uint16_t LocationY;
            uint16_t SizeWidth;
            uint16_t SizeHeight;
            uint16_t BackColor;
            uint16_t TextColor;
            uint16_t TextBackColor;
    
    //      uint16_t PosTLX;
    //      uint16_t PosTLY;
    //      uint16_t PosTRX;
    //      uint16_t PosTRY;
    //      uint16_t PosBLX;
    //      uint16_t PosBLY;
    //      uint16_t PosBRX;
    //      uint16_t PosBRY;
    
            char *Text;
    } button_t;
    
    
    button_t Button1;
    
    int main()
    {
            //char str[9];
            u16 x,y;
            //u8 bufff[]="!!!abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    u8 bufff[] = "!!!abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
            Usart_Configuration(115200);
            printf("Â´Â®ØŸØ¹Â³ÙØªÂ¼Â»Â¯Â³Ø©Â¹Â¦\r\n");
    
            Button1.LocationX = 300;
            Button1.LocationY = 300;
            Button1.SizeHeight = 100;
            Button1.SizeWidth = 200;
            Button1.BackColor = 0x0503;
            Button1.TextColor = 0x0000;
            Button1.TextBackColor = 0x0000;
            //Button1.Text = "Aghil";
            //ButtonSetText(&Button1, "Key1");
            //strcpy(Button1.Text, "Key 1");
    //      Button1.Text[0] = 'A';
    //      Button1.Text[1] = 'g';
    //      Button1.Text[2] = 'h';
    //      Button1.Text[3] = 'H';
    //      Button1.Text[4] = NULL;
    
    
            LCD_Init();
            LCD_Clear(0xfff8); //0xf800 //0x0f0000
    
            //LCD_Highlight(0xff0000);
    
            x=y=0;
            //LCD_DisplayString2(0,0,bufff,0,0xffff);
            //LCD_DisplayString2(100,100,Button1.Text,0,0xffff);
    //      for (i=0; i<0xFFFF; i+=100)
    //      {
    //              sprintf(str,"%05u", i);
    //              LCD_DisplayString(0,0,str,0xffff,0x0);
    //              LCD_DisplayString(0,32,bufff,i,i);
    //      }
    
            //LCD_DisplayString2(200,200,"Aghil",0xffff,0x0);
            //LCD_PutChar2(200, 200, 'A', 0xffff, 0x0);
    
            //_DrawRect(200, 200, 300, 300, 0);
            //LCD_FillArea(201, 201, 299, 299, 0x1818);
    //      Button1.FontHeight = acFont32_GetHeight();
    //      Button1.FontWidthMin = acFont32_GetMinWidth();
    //      Button1.FontWidthMax = acFont32_GetMaxWidth();
            //ButtonShow(&Button1);
    
            while(1);
    //      while(1){
    //
    //              while(!(USART1->SR&USART_FLAG_RXNE));
    //              if(x >799)                     /* ¼ى²éت£سà ؟ص¼نتا·ٌ×م¹»         */
    //          {
    //                  x = 0;
    //              y = y + 16;                        /* ¸ؤ±ندشت¾×ّ±ê                 */
    //              if(y > 479)                       /* ×Ù×ّ±ê³¬³٠                  */
    //              {
    //                          y = 0;
    //              }
    //              }
    
    //              LCD_PutChar(x,y,USART1->DR,0,0xffff);
    //              x += 8;
    //      }
    
    
    }
    
    

    As you can see in the code, I just assigned some value to some of structure members, and I don't use it in other places. When I Uncommecnt variables shown in structure, program doesn't run on MCU. Also I can't debug in that case. When I stop debug mode, an error occurs and tells about JTAG cable. When I Comment those varibles, program run properly and I can debug properly, too.
    I can send you project files, just tell me how to share them.

Children
  • Hello,

    char *Text;

    allocates enough memory for a POINTER, not for the string.
    If you do

          Button1.Text[0] = 'A';
          Button1.Text[1] = 'g';
          Button1.Text[2] = 'h';
          Button1.Text[3] = 'H';
          Button1.Text[4] = NULL;
    


    and I assume the pointer is 32bit, access to [0].[3] might not do what you want, but also does not corrupt the memory. But Button1.Text[4] would destroy some memory.

    do

    typedef struct
    {
            uint16_t LocationX;
            uint16_t LocationY;
            uint16_t SizeWidth;
            uint16_t SizeHeight;
            uint16_t BackColor;
            uint16_t TextColor;
            uint16_t TextBackColor;
    
    //      uint16_t PosTLX;
    //      uint16_t PosTLY;
    //      uint16_t PosTRX;
    //      uint16_t PosTRY;
    //      uint16_t PosBLX;
    //      uint16_t PosBLY;
    //      uint16_t PosBRX;
    //      uint16_t PosBRY;
    
            char Text[20]; // max. required number of charactern required in app
    } button_t;
    

    instead

  • Thank you G. Bohlen,
    You are right but i think it isn't my problem. That part of code you mentioned is comment as below, and I didn't use Textarray anywhere.

    //      Button1.Text[0] = 'A';
    //      Button1.Text[1] = 'g';
    //      Button1.Text[2] = 'h';
    //      Button1.Text[3] = 'H';
    //      Button1.Text[4] = NULL;
    

    Also if I declare an array with length of for example 20, program does not run on MCU because of the problem that is the issue of this topic.

  • You've probably muffed up your memory allocation.

    Sensible debugging should point you in the right direction.

    You fo know how to debug don't you?

  • You keep saying that, but it is plainly not true!

    Of course the code runs - but, when it runs, it crashes!

    Your job in debugging is to first find where it crashes; and then work out why it crashes.

    "That part of code you mentioned is comment"

    So don't post code that's full of irrelevant junk!

    Clean it up before posting - you may well find your own error in the process!

  • Fair enough, but do you understand why that code is bad?

    Are you sure that you haven't made a similar mistake - or mistakes - elsewhere ... ?

  • Thank you,
    The example uses TFT LCD via FSMC interface in the project. When I debug program by JLINK, while writing data to FSMC bus (address 0x60020000), debugger crashes (shows an error about JTAG cable). I really don't know why. I just used another example and there wasn't this problem in it. I uploaded the example here
    www.filedropper.com/28stm32showchartft7inch
    Program compiles successfully, but crashes on hardware.

  • As I said before:

    You've probably muffed up your memory allocation.

    With all you've written since I could change that to::

    You've almost definitely muffed up your memory allocation.

    Check it!