Friday, June 18, 2021

Flash and SRAM

 Hi,

Every embedded firmware engineer shall know a fundamental that, what goes to which area.

In this blog I will be touching two things one is Flash and SRAM.

So, what will be placed into SRAM and which part of the code written by us goes into the Flash area of MCU.

Consider below example code

unsigned int gl_Count = 0;

void get_count (void)

{

    return gl_Count;

}

void increment_count(void)

{

    gl_Count++;

}

In the above code when we declare a variable globally then that will be placed into SRAM and that is not placed into Flash. There might be some compiler constructs which might allow you to do so. However in this case there are no constructs hence the gl_Count will be allocated from SRAM.

Where as the funciton get_count and increment_count will be placed in the Flash space of the MCU.

When we implement / write a code, that will be placed in the Flash Area of the Microcontroller.  After writing the code it shall be cross compiled so that the generated output can be burned into the MCU's Flash using an external programmer / JTAG.

Hope this helps.

Thursday, June 10, 2021

Read a binary file using Python

 Hi,

Today, I have to write a python script to verify the data present in it. 

We are expecting a fixed pattern from 0 to 63, until end of the file. So the snipper can be seen below.


The size of file is around 272 KB, to check if there is any value missing would be nightmare.

Hence I wrote a small python script which checks the data integrity and prints the data mismatch string on console.

import os

f = open("try.bin", 'rb')

print("Reading to buffer")

count = 0

pages = 0;


while True:

    count = 0

    buffer = f.read(64)

    if not buffer:

        break

    while(count < 64):

        if(buffer[count] != count):

            print("data mismatch")

        count = count + 1

    pages = pages + 1

    if((pages % 16) == 0):

        print("\n", end = '')

print ("No of pages", pages)

f.close()


Hope this helps for you. 

This is being tested with Python 3.7.9

Thank you for reading.