Skip to main content

[Basic] C program and SRAM, flash, stack, heap ... Why microcontrollers use different types of memory like that?

Preface

When you build the source code in C/C++, you might see some output messages as the blow:

Flash used: 1166 of 262144 bytes (0.4%).

SRAM used: 2505 of 65536 bytes (3.8%). Stack: 2048 bytes. Heap: 128 bytes.

--------------- Rebuild Succeeded: 10/01/2024 17:46:40 ---------------

Figure 1: The output message from PSoC Creator



Figure 2: The output message from Arduino IDE

Have you asked yourself what are technical jargon such as flash, stack, and heap?

In short, they are kinds of memory for machines.

Microcontrollers use different types of memory (Flash, SRAM, EEPROM, etc.) because each type serves a specific purpose, balancing factors like cost, speed, power consumption, and data persistence.

Key Differences Between These Memory Types:



The explanation for the output message in the figure 1:

Flash used: 1222 of 262144 bytes (0.5%)

The program's code occupies 1222 bytes of the total available 262144 bytes in Flash memory. This is a very small portion, showing efficient memory usage

SRAM used: 2521 of 65536 bytes (3.8%)

The program's data consumes 2521 bytes out of 65536 bytes of SRAM (Static Random-Access Memory). This includes global variables and dynamically allocated memory.

Stack: 2048 bytes

The stack, which is used for function calls, local variables, and return addresses, is allocated 2048 bytes. 

Heap: 128 bytes

The heap, used for dynamic memory allocation (via malloc or similar functions), is set to 128 bytes.


How do we match which part of the source code to which memory? 

Let's dive into the following example:



To be continue





Comments

Popular posts from this blog

How to test frame buffer in linux

The intended goal of this article is to provide the user with enough information so they can display an image onto a screen of their choosing. How to re-build frame buffer testing application. Base knowledge http://trac.gateworks.com/wiki/OpenEmbedded/Video_Out Frame buffer test application - rebuild fb-test-app step 1: Download soure code ( git installed on your machine and account in git-hub is required) https://github.com/prpplague/fb-test-app Command in git: git init git clone https://github.com/prpplague/fb-test-app.git step 2: Change makefile vi Makefile //open Makefile by vim editor :set nu //show line number replace from line 6 to line 12 by folow code: CC=arm-linux-gnueabi-gcc CFLAGS=-02 -Wall Save change command :wq step 3: Rebuid Type “make” command step 4: Copy execute file to target system by filezilla or something like that step 5: Remote target by root account, change mode of execute file (what copied from host)

[Git] Handle trailing space when patching with git

After complete coding one module in development branch. Next phase is merging. Obviously, you can merge source code automatically without no errors happen. However, life is not dream. Create a patch git diff HEAD > newTariff.patch Apply patch git apply newTariff.patch then problem happens Problem git apply newTariff.patch:106: trailing whitespace. patch does not apply Route cause: Nguyên nhân: Do trong source code có những dấu space thừa (ô vuông màu đỏ hình dưới) Some whitespaces is existed in your patch. (red area in below pictures) Fix Clean white space and patch again. Cách khắc phục Xóa những dấu space này đi và thực hiện patch lại Make up after complete coding. Find and clean whitespace before create patch file. Sửa sau khi code xong Kiểm tra sau khi code xong có lỗi này không git diff HEAD --check Prevent whitespace by manually when typing source code Sửa ngay khi đang code =>Bật chức năng hiển thị các dấu whitespace, hoặc remove trailing

The best 12 AI Tools in 2023

The new life challenges require new tools.  For decades, AI technology has been a fuzzy field for unprofessional users. However, it is not rocket science anymore at this time.  There are the best 12 AI tools in 2023 that will keep you up and take some advantages in your business. Here, I put everything on your table. Enjoy Solves anything - ChatGPT ( https://chat.openai.com/auth/login ) Write anything - Writesonic ( https://writesonic.com/ ) Generates Art - Midjourney ( https://www.midjourney.com/explore ) Generates Code - Replit ( https://replit.com /) Generates Video - Synthesia ( https://www.synthesia.io/ ) Generates Music - Soundraw ( https://soundraw.io/ ) Generates Tiktoks - Fliki ( https://fliki.ai/ ) Generates Avatars - Starrytars ( https://starryai.com/en/starrytars ) Generates PPTs - SlidesAI ( https://www.slidesai.io/ ) Edit Pictures - Remini ( https://remini.ai/ ) Edit Video - Pictory ( https://pictory.ai/ ) Summarize Notes - Wordtune( https://www.wordtune.com /)