Monday, January 3, 2022

Types of Makefile and build system

 Different type of Makefile

  • Default/legacy make file -> GNU Makefile
  • Cmake 
  • Autotool -> Makefile.am -> Makefile.in
  • Linux kernel special Makefile
  • Android.mk

Build system/tool

  • Ninja and Meson
  • Yocto
  • Cerbero

Friday, July 9, 2021

synchronization between kernel thread and ISR

Why we need volatile

 The volatile keyword is a type qualifier which tells compiler to not to optimize/cache any operation with these variable.


Value of this variable can change between two read or two write by external forces or event


The volatile keyword is mainly used where we directly deal with GPIO, interrupt or flag Register. It is also used with a global variable or buffer which shared between the threads.


some good program to be written in c or c++ programming

 - Write a program to Draw circle 

- Write a program for Matrix multiplication

- Write a program to reverse bitstream. (bit by bit reverse in  1 byte of data)

https://stackoverflow.com/questions/22004690/print-odd-and-even-numbers-using-2-threads-using-mutex-in-c



virtual memory, virtual base class and virtual function

 Virtual Memory

Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of main memory.

This enables a computer to be able to compensate shortages of physical memory by transferring pages of data from random access memory to disk storage.

More at https://www.geeksforgeeks.org/virtual-memory-in-operating-system/

Virtual Base class

When a class is made a virtual base class, C++ takes care to see that only one copy of that class is inherited regardless of how many inheritance path exits between the virtual base class and derived class.

Virtual function

This is example of runtime polymorphism


(Function overloading & Operator overloading are example of compile time polymorphism)

In c++ we can point any derived object via its base pointer.
But that pointer can not be used directly to access all members of derived class. by default that base  pointer will points to base class members only.

when we have same function name used in base class & derived class then if we mark base class function as virtual function then c++ decides which function to call at runtime based on the type of object pointed by base pointer. 

Pure Virtual functions

Its normal that we mark one function as virtual in base class and redefine it in derived class.

Many times virtual function in base class are just place holder and do nothing.

virtual void display() = 0;

such function called pure virtual function.

Such base class can not declare their own object.

so these classes are also called abstract base class.

sizeof operator implementation

 #define my_sizeof(type) (char *)(&type+1)-(char*)(&type)


This can not be implemented by function. For any function its argument
type needs to be always fixed.

Little and Big Endian

 Little and big endian are two ways of storing multibyte data-types.



Suppose integer is stored as 4 bytes then a variable x with value 0x01234567 will be stored as following in Big Endian

Big Endian:

  01,                   23,                45,             67

 0x100             0x101            0x102         0x103


Little Endian

 67,                   45,                23,             01

 0x100             0x101            0x102         0x103


Sample C program to check this.,


int main() 
    unsigned int i = 1; 
    char *c = (char*)&i; 
    if (*c) 
        cout<<"Little endian"
    else
        cout<<"Big endian"
    return 0; 

Tuesday, May 4, 2021

100 Embedded interview questions

 https://aticleworld.com/embedded-c-interview-questions-2/

Sunday, April 25, 2021

Constant pointer vs Pointer to constant

 const int* ptr;

is a pointer to constant (content). You are allowed to modify the pointer. e.g. ptr = NULL, ptr++, but modification of the content is not possible.


int * const ptr;

Is a constant pointer. The opposite is possible. You are not allowed to modify the pointer, but you are allowed to modify what it points to e.g. *ptr += 5.

Fun with array pointers and function pointers

1-D and 2-D array pointers.   

// Pointer to an integer

int *p; 

// Pointer to an array of 5 integers

int (*ptr)[5];

// Pointer to an array of 5x2 

int (*ptr1)[5][2];

int arr[5];

int arr1[5][2];

    // Points to 0th element of the arr.

    p = arr;

    // Points to the whole array arr.

    ptr = &arr;

// Points to the while 5x2 array

ptr1 = &arry1;   

==========

Function Pointers


void fun(int a)

{

    printf("Value of a is %d\n", a);

}

  

int main()

{

    // fun_ptr is a pointer to function fun() 

    void (*fun_ptr)(int) = &fun;    

//  OR

void (*fun_ptr)(int) = fun;

  

    /* The above line is equivalent of following two

       void (*fun_ptr)(int);

       fun_ptr = &fun; 

    */

  

    // Invoking fun() using fun_ptr

    (*fun_ptr)(10);

//OR

fun_ptr(10);

    return 0;

}

Saturday, April 24, 2021

Video Formats and chroma sub-sampling

 color space system.

-> RGB

-> YCbCR  (YPbPr in analog system)

-> HSB hue satuaration brightness

-> CMYK

=================

4:2:2 (Horizontal sub sampling) 8 bytes used for 4 pixel.

Y   Y  Y  Y

cb     cb

cr     cr

4:1:1 (Horizontal sub sampling)  6 bytes used for 4 pixel.

Y   Y  Y  Y

cb     

cr     

4:2:0 (Horizontal + Vertical sub sampling) 6 bytes used for 4 pixel.

y    Y

cb, 

cr,

y    y

================

YUV formats (8 bits only)

1) Planner

-> All Y would be in one array/plane, then all U would be in one array/plane and then V would be in once array/plane

-> Here all 3 array can be at different offset in single buffer also. 

I420, I422

-> reversed planners 

YV12 is exactly like I420, but the order of the U and V planes is reversed

2) Semi Planner

-> Semi planar formats have two planes instead of three, one plane for luminance, and one plane for both chrominance components. 

-> NV12, NV16

-> reveresed semi planners

-> NV21, NV61

3)Packed formats

In packed formats, you usually have only one plane. (This is something like RGB)

( packet formats cannot normally deal with vertical sub-sampling.)  

UYVY or Y422

YUY2 or V422 (reversed format)

Thursday, April 15, 2021

Gstreamer and OMX-IL state

 Gstreamer state handling


NULL -> Default state. NO resources are allocated in this state.

READY -> Element has allocated its all global resources. Element open the devices allocate the buffers. But stream is still not openened. NO stream specific allocation.

PAUSED -> Elemened has openned the stream but its actively processing stream. Audio/Video o/p pushed into buffers and queued to play it right after state change. Video sink element display first frame and paused.

Playing -> same as paused except clock is running and data is being processing  


OMX-IL state

Loaded: 

WaitingForResources

IDLE

Executiting

paused 

Saturday, March 27, 2021

Communication method between user space and kernel space and how those are implemented

 There are couple of well known methods to communicate from user space to kernel space.

  • Virtual file system like  /proc, /sys, configfs, /debugfs
  • Standard system call like read(),write(), open(), close(), fork()
  • ioctl for char drivers. With copy_to_user and copy_from_user
  • netlink socket
  • Use udev and uevent based event mechanisim from kernel space to user space.  Read more https://stackoverflow.com/a/23149574/775964
  • Sending signal from kernal space to user space thread. Full example at https://embetronicx.com/tutorials/linux/device-drivers/sending-signal-from-linux-device-driver-to-user-space/
  • Using common memory which can be used by user space/kernel space both like ION memory or DMABuf  read more here https://lwn.net/Articles/480055

Monday, November 23, 2020

Things to know about inline function

Below are some good points about inline function to keep in mind.

  1. Usage of inlne funcation , May increase function size so that it may not fit on the cache, causing lots of cahce miss.
  2. You can not use static variable in Inline funcation. If you use them then compiler will not treat this as inline funcation.
  3. You can not use recursion with inline funcation
  4. If funcation return value depends on any used loop, swicth or goto statement in your funcation then compiler will not treat this as inline funcation
Using inline will help, Just to save execuatation time by avoiding call/stack of funcation

inline funcation are good than macro becuase, macro does not perform any type checking/usual error checking while compilation.  

Macro are like find and replace while preprocessing steps. 


Saturday, November 21, 2020

Difference between softirq, tasklet, workqueue and threaded_irq

 While Linux interrupt handling, To perform "bottom half",  There are couple of methods used as below.

  • Tasklet

    • It can not sleep.
    • It run in software interrupt context
    • These are atomic. These are guaranteed to never run on more than one CPU of a given processor, for a given tasklet. In other terms they do not run concurrent, (If you have multiple different tasklet in single driver then they can run concurrent)
    • They may be scheduled to run multiple times, but tasklet scheduling is not cumulative; the tasklet runs only once, even if it is requested repeatedly before it is launched
    • However, another interrupt can certainly be delivered while the tasklet is running, so locking between the tasklet and the interrupt handler may still be required
    • Tasklets can be statically allocated using DECLARE_TASKLET(name, func, data) or can also be allocated dynamically and initialized at runtime using tasklet_init(name, func, data)
    • Tasklets are actually run from a softirq

Saturday, October 3, 2020

Different type of operators and type casting in C programming

  •  Airthmetic Operations
                + , - , *, / , %
  • Relation Operations
                <. <=, >, >=, ==, !=
  • Logical Operations
                && logical AND
                ||   logical OR
                !    logical NOT
  • Assignment operations
                a = a+ 1  is called a += 1

Data Types and storage class in C programming

 ANSI C support different 3 types of data types.

  1. Primary data types
    • int, (short,long,signed,unsigned) 
    • char, (signed,unsigned)
    • float,  (float,double,long double)
    • void 
  2. Derived data types
    • arrays
    • strings
    • structures
  3. User-Defined data type
    • typedef 
      • typedef <old name> <new name>;
    • enum identiifers
      • enum day [monday, tuesday, ... sunday];

Basic information about C programming

 C Tokens

Just like any other programming C programming has 6 different tokens
  1. Keywords (there are 32 keywords in C89, lile int,flot)
  2. Identifiers (main)
  3. Constants
  4. Strings
  5. Special symbols
  6. Operators

Constant has further few more types.

1) Integer constants

C program compilation flow

 Here is the brief summary of detail C program compilation process.


Source File (.c file)

            |

Preprocessing (Include all header file code in main source code, Expand all Macro and #define) (.i file)

            |

Compilation (Convert the program into assembly code) (.s file)

            |

Assembling (Convert the assembly code into machine code called object) (.obj or .o file)

            |

Linking  (Links different object file/module and system library together and create final executable object code) (.exe or .out file)       


History of C programming

  • ALGOL (The root of all mordern programming language)  #1960
  • BCPL (Basic combined programming language)  #1967
  • B (It was used to create early version of UNIX in Bell Lab) #1970
  • Traditional C  #1972
  • K&R C  launched book #1978. C started some acceptence
  • C89 / ANSI C (Americal National Standard Institute) launched in #1989 also known as C89
  • C90 (ANSI & ISO commite approved) #1990
Meanwhile Sun Microsystem launched JAVA based on C & C++

In 1999, Standardization commite, decided to keep adding new feature of C++/Java in C and they launched

  • C99  in #1999
  • C11 in  #2011
  • C18 in  #2018
  • C2X in progress

Most famous compilers like gcc, LLVM clang, IAR, Microsoft Visual C++ VS 2019 support all this standars. (Atleast for x86 platform)