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:
- A pointer is created for BlackImage
- The size of memory to be allocated for Blackimage is decided based of the display dimensions
- 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:
- If I do not have enough memory - where do I look to find this out? (I am using Keil uVision IDE)
- 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?
- 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?
- 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.