Wednesday, June 21, 2023

Macros in C

Recently we were developing some source code say for feature X.

As part of the development we have written the header file something like below

#ifndef __2023_06_21_001_H__

#define __2023_06_21_001_H__

#if (CUSTOM_FEATURE_X == CUSTOM_FEATURE_VAL)

#define CUSTOM_FEATURE_X     (1u)

#define CUSTOM_FEATURE_VAL   (1u)

typedef struct

{

    unsigned int val;

}feature_x_t;

#endif

#endif /* __2023_06_21_001_H__ */


In a C source file, we have included the above header file. In C source file, we have declared a variable of type feature_x_t as following:

feature_x_t gl_feature_str;




In addition by mistake we have defined the both macro values inside the the header file itself as shown in snippet. We were wondering how the preprocessor, could find the feature_x_t variable type declaration which is being used in C source file.


After a day, what I understood is, it seems as both were not defined, #if (CUSTOM_FEATURE_X == CUSTOM_FEATURE_VAL), condition is passing, and the C Preprocessor processes the further declarations from the header file. Hence the variable type feature_x_t could be found.

Friday, February 17, 2023

CFLAGS with -Wall or -Werror in Modux Toolbox 3.0

In this post we will discuss about CFLAGS with -Wall and -Werror option in Modus Toolbox 3.0 (MTB3.0).

The user need to integrate few C source and header files. Let us assume that the filenames are source_1.c, source_2.c, source_1.h and source_2.h.

To integrate the source files into the MTB3.0 project the files were copied to the existing folder and MTB IDE could include the source files to the project automatically.

The project also contains MTB Shared library which will be generated while creating the project.

Now let us assume a function named my_function defined in source_1.c.

The user calls my_function from one of the source file present inside the mtb_shared folder. However the prototype for the function is not included into the source file which is available in mtb_shared.

As the Makefile contains CFLAGS with -Werror, the compiler stops generating the final output such as hex or bin files.

When we have added the prototype for my_function in the source file present in mtb_shared, then the IDE could generate the hex or bin files. 

The possible solutions to it are, 

  1. To change the CFLAGS Setting from -Werror to -Wall.
  2. Include the prototype for the functions into source files present in mtb_shared.
As removing -Werror might stop generating some other errors such as overflow calculations for example.

Hence it is advised to include the function prototypes.


Tuesday, October 26, 2021

USB Basics - 1

This post describes about benefits of USB


  1. USB allows usage of one standard connector. No need of different types of connector like DB-9, Parallel connector. (I am talking about the older days such as 90's and still this holds good).
  2. USB reduces the size of connector drastically. Compared to DB-9 and parallel connector USB connector size reduced significantly.
  3. USB eliminates restarting the Host PC after installation of a driver when the connected device uses the standard drivers being used which are shipped as part Windows / Linux / Mac OS.
  4. Before USB invented, to install a new hardware onto a Windows PC, one need to restart it. USB can help the device to work without restarting it. We call it as plug and play. Buy any new USB based device such as pen drive, camera, mouse, keyboard and etc... just connect them to the USB port available on Host PC, it just works like that.

Thursday, September 23, 2021

Different Current ratings provided on USB Port for Charging

Universal Serial Bus in short USB is capable of providing different current ratings over VBUS.

A USB connector will have 4 basic signals called as VBUS, D+, D- and GND.

VBUS will be +5V w.r.t. the GND(Ground) signal.

D+ and D- are used for data communication between Host and Device.

For example when we connect a pen drive to the USB Port of a Host PC, the circuitry on the pen drive is powered using the VBUS and GND signals.

Then the data exchange happens over D+ and D- to see the files stored on the Pen Drive.

Now, when it comes to charging, USB is keep evolving w.r.t Data throughput and power supply.

For example these are some of the Specs from USB committee, USB 1.1, USB2.0, USB3.1, USB3.2 and USB4.

When a device is USB1.1 capable then it supports 5V@100mA.
A USB 2.0 device can support 5V@500mA. USB3.x device can support upto 5V@900mA. USB4 uses a special connector called as Type-C connector which is capable of providing upto 5V@3A

The others are 5V@1.5A and 5V@2.1A. These are called as either (CDP or DCP). When we refer CDP it means the device can charge and enumerate. When we say DCP the device can be only charged on that port and no enumeration of the device is allowed.

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.


Saturday, January 23, 2021

Data Memory Optimization  for Micro controllers

 
Look at the linker map file for the items which are consuming more space.
 
Pack the structures, so that padding is removed.
 
Use bit fields wherever possible.

Use local variables as much as possible.

Do not allocate data memory as large chunks.

Try to reuse the Data memory wherever possible.