1

I'm kinda new to C but feel I have a good basic understanding and hence have posted the question here.

I am trying to port over some demo code for a Waveshare E-paper display to use for an application in my Graduate Project. For now I just wanted to prove the demo code works but will most likely use aspects of it later on.

The issue I am currently having is with use of the following lines:

// Create a new image cache
UBYTE *BlackImage; 
UWORD Imagesize = ((EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1)) * EPD_HEIGHT;
if((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL)
    {
    printf("Failed to apply for memory...\r\n");
    return -1;
}

I am pretty sure my understanding of this is correct, but the essentially:

  1. A pointer is created for BlackImage
  2. The size of memory to be allocated for Blackimage is decided based of the display dimensions
  3. In the if statement, BlackImage is assigned the memory size from the above line, and rerturns the pointer value for BlackImage. If this pointer value is NULL (i.e. 0) then the memory has not been allocated.

My code is always failing at 'applying this memory'. I assume the reasons are that I do not have enough memory available.. but here are my questions:

  1. If I do not have enough memory - where do I look to find this out? (I am using Keil uVision IDE)
  2. Can I solve this by shifting memory allocations round? Does malloc store in a specific type of memory? I know the term heap is essientially useable memory, but what type of memory does this correlate to in the uC datasheet?
  3. Are there alternative ways to do this? The demo code also includes an array stored in a seperate C file (ImageData.c attached), so why would you use malloc here and not do it the same way?
  4. What are the best solutions around this?

Thanks in advance for any help! Please correct me where I have stated anything incorrect, I'm here to learn! :D

EDIT: Additional Information below.

I am using an STM32L152RB (STM32L-Discovery) which has 16kB of RAM. Also see variables below:

// Display resolution
#define EPD_WIDTH       122
#define EPD_HEIGHT      250

FULL demo code can be found here: https://www.waveshare.com/wiki/File:2.13inch_e-paper_code(V2).7z

All I have done is port the pins over and removed the printf/debug lines as I don't have an USB to TTL converter at the moment.

norand
  • 13
  • 1
  • 5
  • 2
    How big is `ImageSize`? How much memory do you have in the microcontroller. You generally don't have much control over the heap that `malloc()` uses, so in microcontrollers you often have to do your own memory management. – Barmar Feb 20 '20 at 00:57
  • *In the if statement, `BlackImage` is assigned the memory size from the above line, * is incorrect. BlackImage is assigned the pointer that is returned by `malloc(ImageSize)`, and then that pointer is compared to `NULL` to see if the allocation succeeded. In no case is the memory size assigned to `BlackImage`. – Ken White Feb 20 '20 at 01:00
  • 3
    Unless you've explicitly configured it (both size & placement) in your project, you don't have a heap to `malloc` from - so it will always fail. – brhans Feb 20 '20 at 01:37
  • @Barmar I have 16kB RAM and am using STM32L152, question updated to reflect. – norand Feb 20 '20 at 14:19
  • @brhans I have added a link to the demo code - Where and how do you configure this? Isn't the heap configured as part of the startup_stm32xxxx.asm? – norand Feb 20 '20 at 14:19
  • 1
    It's not in the source code. It'll be in your linker configuration & project settings. – brhans Feb 20 '20 at 14:28

1 Answers1

1
  1. do not use malloc dynamic allocation in the embedded projects.
  2. if you decide to malloc (assuming the standard STM generated startup) you need to change the linker script where the heap size is defined. To check the available heap memory (still considering STM glibc implementatn) you need to amend the sbrk implementation. change the static heap position to the global variable and using provided by the linker script symbols check how many memory left.
0___________
  • 60,014
  • 4
  • 34
  • 74