Friday, February 26, 2016

C - structures, initialized and uninitialized variables


  1. Structure alignment
  2. Trailing structure padding
  3. Trailing padding in bit fields


Following is the best link to study "C Structures"

Lost art of C structure packing: http://www.catb.org/esr/structure-packing/

Initialized and uninitialized variables:

All local variables & pointers are "uninitialized"
Local variables declared as "static" are initialized to zero or NULL

All global variables & pointers are initialized

Difference between global variable and static global variable:

  • Normal global variable can be used in other C files by use of "extern"
  • Static global variable cannot be used outside the file
Multiple file C project:
  1. Architecture of such project should be like
    1. user_main.c
    2. user_header.h
    3. file1.c
    4. file1.h
    5. file2.c
    6. file2.h
    7. file3.c
    8. file3.h
  2. Variables declared by individual c file (file1.c, file2.c file3.c) should be defined in c files itself
  3. Variable declarations should be included as part of header files(file1.h file2.h file3.h) as "extern" if these varaibles are needed outside the c file.
  4. user_header.h should include all these header files(file1.h file2.h file3.h) in it, so as get access of all variables defined the source files(file1.c, file2.c file3.c)
  5. In case of functions, function definition in c files & function prototype in header files
Multiple file C project is best for situations where
  • You want to group methods depending on there functionality. And you are the one whole and sole developer for the project. Where you don't have to share any of your code to others.
Shared objects:
  • Situations where you have to share your code to others, but you want to keep your code functionality hidden
  • The executable prepared by using shared object is small in size, but needs the .so or .dll at the time of compilation and execution
  • The application(exe) is already prepared that uses shared object & we need to optimize the algorithm inside the dll with same function prototype, then we just need to compile new shared object and replace the old. By this exe doesn't need to be recompiled.
  • These techniques of modularization of code by shared objects is used for most softwares installed on PC
  • Code Architectures come into play as, the function prototypes need to be design as part of  main source code with lot of future vision and modular scope in mind. Hence to be a code architect, you need to have lot of experience at first.
  • If we need to link new shared object to exe, then the exe need to be recompiled with new dll linked to it.
Static objects:
  • Executable prepared with static linking, the size is larger as compared to shared object
  • They are with .a extensions in Linux
  • When a executable is prepared with linking to these object file, it is called static linking
  • The concept is used in preparing mobile application eg. Apple applications, where the rule is to prepare app with static linking, if it is intended to be available on AppStore
  • It provides security kind of feature, as at runtime exe doesn't depends on any external file like in case of shared objects
At runtime, the memory allocation (RAM) takes place in almost the same size in shared object and static linking. Just a difference of few bytes, as in shared objects the address of calling the function from required .so/.dll comes into picture
Plugins:
They are basically shared objects only, just the extension names are different.

Tuesday, February 23, 2016

Function pointer use & What are callback functions ??

Function pointers are basically used in callback function, to call a particular function when a event occurs.
Callback functions are ISR(interrupt service routines) when it comes to non-os based embedded systems.

Example of using funtion pointer in C program:

---------------------
user_main.c
---------------------
*
 * user_main.c
 *
 *  Created on: 23-Feb-2016
 *      Author: root
 */


#include <stdio.h>
#include <user_include.h>

int main(int argc, char *argv[]){
Demo d;
d.input_string = "Jon snow";
d.print_data_pointer = data_print_1;
printer_function(&d);
d.print_data_pointer = data_print_2;
printer_function(&d);
return 0;
}


void data_print_1(char *inp){
printf("\n You Know Nothing,... %s ", inp);
}

void data_print_2(char *inp){
printf("\n You know everything, ... %s", inp);
}

void printer_function(Demo *temp){
temp->print_data_pointer(temp->input_string);
}


----------------------
user_include.h
----------------------
/*
 * user_include.h
 *
 *  Created on: 23-Feb-2016
 *      Author: root
 */

#ifndef USER_INCLUDE_H_
#define USER_INCLUDE_H_

/*
 * typedef
 */
typedef signed char int8;
typedef unsigned char uint8;
typedef signed int int32;
typedef unsigned int uint32;

typedef struct{
void (*print_data_pointer)(char *);
char *input_string;
}Demo;

/*
 * Function declaration
 */
void data_print_1(char *);
void data_print_2(char *);
void printer_function(Demo *);

#endif /* USER_INCLUDE_H_ */


****** OUTPUT ******
You Know Nothing,... Jon snow 
You know everything, ... Jon snow

Monday, February 22, 2016

Latch and Flip flop difference

Flip flops are used to store informaion

Category:
  1. Asynchronous/Transparent flipflops (Latch)
  2. Synchronous fliplfops
Types:
  1. SR (Set Reset)
  2. D (Data or delay)
  3. T (Toggle)
  4. JK
Underneath hardware:
  1. NOR based (eg. SR latch)
  2. NAND based (eg. SR' latch)
Methods of describing
  • Characteristic table / Truth table
R
S
Q(next)
Action
0
0
Q
Hold state
0
1
1
Set
1
0
0
Reset
1
1
0
Not allowed as Q = Q’ = 0

  • Excitation table
Q
R
S
Q(next)
0
x
0
0
0
0
1
1
1
1
0
0
1
0
x
1

Latches and flip-flops are the basic elements for storing information. One latch or flip-flop can store one bit of information. The main difference between latches and flip-flops is that for latches, their outputs are constantly affected by their inputs as long as the enable signal is asserted. In other words, when they are enabled, their content changes immediately when their inputs change. Flip-flops, on the other hand, have their content change only either at the rising or falling edge of the enable signal. This enable signal is usually the controlling clock signal. After the rising or falling edge of the clock, the flip-flop content remains constant even if the input changes.



NOTE:

  1. When the device is latch/Gate we define it by Truth table
  2. When the device is flip-flop we define it by Excitation table
States of output:
  1. HIGH
  2. LOW
  3. No change
  4. Indeterminate
  5. Race around
State of Input:
  1. HIGH
  2. LOW
  3. Don't care


RS flipflop (mostly used as LATCH)

R
S
Q
Q’
Comment
0
0
0
1
No Change
0
1
1
0
Q Change to 1 as ‘S’ is 1 (Set State)
0
0
1
0
No Change
0
0
1
0
No Change
1
0
0
1
Q Change to 0 as ‘R’ is 1 (Reset State)
0
0
0
1
No change
1
1
0
0
Not allowed. In this state Q = Q’
0
0
?
?
If both R and S change from 11 to 00 then its indeterminate state

SR flipflop is the basic building block for computer memories

D-flipflop (mostly used as LATCH)
Major drawback of SR latch (indeterminate state and Not allowed state) are overcome by D-flipflop
D-Type-edge-trig-symbol.gif
D is a synchronous input
S' and R' are asynchronous inputs

JK-flipflop (mostly a flipflop +ve edged or -ve edged)
JK-MS-symbol.giftable-5-4-1.gif


J
K
Q(next)
Q’(next)
Action
0
0
Q
Q
Hold state
0
1
J
K
J
1
0
J
K
K
1
1
Q’
Q
Toggle

Sunday, February 7, 2016

CAN protocol information


  • CAN control area network
  • Introduced by "Robert Bosch"
  • It has 3 layers of OSI model:
    • Physical layer (CAN Transceiver SN65HVD233)
    • Data link layer (CAN Controller MCP2515)
    • Application layer (Higher level protocols are implemented
Specifications:

ISO 11898
Specification is related to only following two layers
  1. Physical layer
  2. Data link layer
Physical connection:
2 wire interface
uC <---SPI 4---> CAN controller <---Tx & Rx ---> CAN Transceiver <--CANH & CANL
two 120ohm termination resistors are connected on the basis of transmission line theory.

CAN packet description:
  • Standard
SOF(1) + ID(11) + RTR(1) + IDE(1) + RB0(1) + DLC(4) + DataFrame(8*n where 0<=n<=8) +  CRC(15) + CRCdel(1) + ACK slot(1) + ACKdel(1) + EOF(7) + IFS(3)
  • Extended
SOF(1) + ID(11) + SRR(1) + IDE(1) + ID(18) + RTR(1) + RB1(1) + RB0(1) + DLC(4) + DataFrame(8*n where 0<=n<=8) +  CRC(15) + CRCdel(1) + ACK slot(1) + ACKdel(1) + EOF(7) + IFS(3)

Dominant bit = 0 logic
Recessive bit = 1 logic

Bit stuffing - if there are more than 5 consecutive dominant bits then bit stuffing mechanism is used to send a recessive bit so as to maintain the synchronization

CAN frame types:
  1. standard
  2. extended
  3. remote request
  4. errror (active & passive)
    • CRC error
    • acknowledgement error
    • form error
    • bit error
    • stuff error
  5. overload
CANOpen
Its a application layer protocol


  1. Communication profile (CiA DS-301): The communication profile provides the means to configure devices and communication data and defines how data is communicated between devices.
  2. Device profile (CiA DSP-401): 

  • Device profiles add device-specific behavior for (classes of) devices (e.g. digital I/O, analog I/O,  motion controllers, encoders, etc.)
  • Tuesday, February 2, 2016

    Design patterns

    Proper code design has following advantages:

    1. speed up development process by providing tested, proven development paradigms
    2. to prevent major issues in later stage of project
    3. improves code readability for coders familiar with pattern
    4. helps developers to communicate using well known terms
    5. makes the design robust
    Design patterns types:
    1. Creational
      • Class creational pattern (use inheritance effectively)
      • Object creational pattern (use delegation effectively)
    2. Structural
    3. Behavioral

    Monday, February 1, 2016

    Basic concepts when it comes to OOPs...


    "classes and objects" is the base when it comes to OOP
    classes in C++ are very similar to structures in C

    Important points
    1. class members
    2. access modifiers
    3. constructor (eg. classname(){...})
    4. destructor (eg. ~classname(){...})
    5. copy constructor
    6. friend function
    7. inline function
    8. 'this' pointer

    Concepts / Aspects / Features :
    1. Inheritance (parent/base class & child/derived class)
    2. Overloading - (The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.)
      • function overloading
      • operator overloading (important aspect of C++)
      • overload resolution
    3. Polymorphism (functions with same name but different data types of input parameters and/or return types)
      • static resolution / static linkage / early binding (the call of the function is being set once by the compiler as the version defined in the base class)
      • Virtual function - keyword 'virtual': (the compiler looks at the contents of the pointer instead of it's type) - dynamic linkage / late binding
        • Pure Virtual Function (eg. virtual int area() = 0; //The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.)
    4. Abstraction
      • interfaces
      • implementations
    5. Encapsulation (public, private, protected)
      • data encapsulation (data hiding)
      • data abstraction
    6. Interfaces
      • abstract class (class with at least one Pure Virtual function.  The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface.  )
    A step further...
    1. namespaces
      • defining namespaces
      • using namespaces
      • Dis-contiguous namespaces
      • Nested namespaces
    2. templates (A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.)
      • function template: typename
      • class template: class
    3. dynamic memory
      • new (eg. float *f1 = new float;) - The malloc() function from C, still exists in C++, but it is recommended to avoid using malloc() function. The main advantage of new over malloc() is that new doesn't just allocate memory, it constructs objects which is prime purpose of C++.
      • delete
    4. exception handling
      • try
      • throw
      • catch
    5. files and streaming
      • ofstream - create and write to file
      • ifstream - read information from file
      • fstream - capability of both ofstream and ifstream
      • open()
      • close()
    6. signal  handling
      • signal() - to trap unexpected events
      • raise() - generate signals
    7. multi threading
      • create thread
      • terminate thread
      • passing arguments to thread
      • join thread
      • detach thread

    PROFILE

    My photo
    India
    Design Engineer ( IFM Engineering Private Limited )

    Followers