This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PM: How to manage all the SRAM available for me in the right way without crashing my code

Part Number: TM4C123GH6PM

Hello, so recently I've been working on a project and I successfully implemented multiple UARTs using Direct Memory Access and other drivers like timers, I/O pins and others. 
in the start up I had 0 heap which caused the uC not to work then I changed it as shown in the next code section then it worked during callocating (dynamic memory allocation using calloc) so I changed the configuration to the following 

; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
;
;******************************************************************************
Stack   EQU     0x00000800

;******************************************************************************
;
; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
;
;******************************************************************************
Heap    EQU     0x00000400

then my program worked fine, but I realized my GPRS module keeps resetting on its own multiple times so I doubted it's a memory issue 
in KEIL u4 compiler I could see the following information after I compile my code 

Program Size: Code=7168 RO-data=744 RW-data=312 ZI-data=4856  

so I tried to increase the following section as shown

; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
;
;******************************************************************************
Stack   EQU     0x000002710

;******************************************************************************
;
; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
;
;******************************************************************************
Heap    EQU     0x00001338

so I wanted to allocate 5kB for the heap and 10kB for the stack, but once I implemented that then it was the havoc and program acted in a completely mess and wasn't stable like it is behaving completely wrong. 

Questions are : 
1- how to manage my memory and the whole 32KB SRAM in the right way? ( I am using SRAM to save data when there is no internet connection) 
2- is changing those 2 lines in the startup file enough or they should be followed with other changes?
3- after compiling the program as shown in the screenshot, RAM data = ZI + RW  ,and ROM data = RO data, or I'm wrong here? 

  • Are you using TI-RTOS?
  • user5175619 said:
    1- how to manage my memory and the whole 32KB SRAM in the right way?

    First don't use the heap if at all possible. Heap will fragment* unless used in ways that don't actually require a heap exists.

    If you must use heap, give it as much memory as you can. Libraries that use heap often do not fare well when they fail to allocate memory.

    user5175619 said:
    - after compiling the program as shown in the screenshot, RAM data = ZI + RW  ,and ROM data = RO data, or I'm wrong here? 

    I have no idea what ZI is but no this will not include all the data. In particular it will not include any measurements of how much heap and stack are required. It may include the heap and stack allocated, it may not. You need to check your compiler documentation.

    Robert

    * Although some types are more resistant to fragmentation than others.

  • No I am not using RTOS I forgot to mention and will add it to my topic now
  • I avoided fragmentation by freeing the heap right after manipulating data inside before I allocate anything else, I allocated more but still crashing. I used <stdlib.h> library for that matter
  • user5175619 said:
    I avoided fragmentation by freeing the heap right after manipulating data inside before I allocate anything else,

    Then why on earth are you using the heap?

    Robert

  • Because I have a function for receiving resposne from GPRS module and I have to search in a buffer for "OK" to proceed to the next command and this "OK"'s location varies from response to another, for example on the first one I receive it on the 5th, 6th location, but on others I receive it on the 950th location. so it's not wise to statically allocate char arr[950] I think.
    So basically I send the size of the required space as an argument in the function, and that function will allocate it. as you know I can't decide the size of array inside a function argument statically

  • user5175619 said:
    Because I have a function for receiving resposne from GPRS module and I have to search in a buffer for "OK" to proceed to the next command and this "OK"'s location varies from response to another, for example on the first one I receive it on the 5th, 6th location, but on others I receive it on the 950th location. so it's not wise to statically allocate char arr[950] I think.

    If you need more than 3 bytes to just recognize OK in a stream of characters you're being sloppy*. Ask yourself the question, why are you storing characters that don't match?

    Robert

    * I think you can do it in one but I'd have to spend time to make sure

  • user5175619 said:
    So basically I send the size of the required space as an argument in the function, and that function will allocate it. as you know I can't decide the size of array inside a function argument statically

    That's not entirely true. There are two ways of having a variable sized array. Support is not universal for either but at least one is standard (implementation of the standard lags).

    Robert