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

    Friday, January 29, 2016

    Operating system: Process vs Threads


    Differences
    1. Creation of thread faster than creation of process
    2. Switching between threads faster than switching between processes
    3. Variables can be shared between threads of same process hence data sharing in threads is easy as compared to process. Processes don't share a common memory space hence schemes like Pipes and Queues need to be used for data communication

    Applications

    Threads
    1. Almost all softwares that we run on PC, like microsoft office, adobe pdf reader, eclipse etc. they create a single process and inside process they create multiple threads - GUI handling, back end processing, event handling etc.
    2. This is because processes as slower in all aspect as mentioned above and eventually application will be slow performing. Hence in these cases multi-thread creation is a better option as compared to multi-process creation
    3. There are two types of threads:
      • Kernel level thread (Thread model - One to one)
        • Advantages: 
          • Best suited when thread blocking comes into play
          • One blocked thread does not block the whole process. Multithreading and multiprocessing hardware is actually used in this case
        • Disadvantage:
          • Slow as compared to User level threads
          • Overhead of thread management
        • Example: PThread
      • User level thread (Thread model - Many to one)
        • Adavantges:
          • Best suited for non-blocking thread
          • Fast execution as compared to kernel threads
          • Fast thread creation, switching, synchronization etc.
          • Kernel is not aware of threads created in user level
        • Disadvantages:
          • Mutlithreading and multiprocessing hardware is not used
          • If one thread blocks the process is blocked
    4. Protecting shared resources
      • Mutex(Mutual Exclusion) - Thread resource sharing serialization
      • Probable problems - 
        • Deadlock - Thread1 acquire lockA, and demands lockB. Thread2 acquire lockB and demand lockA. This is the scenario where deadlock occurs.
        • Race condition - Unsynchronizaed access to shared resource can cause inconsitency in the shared data
        • Priority Inversion - When a LOW priority thread acquires a lockA, and is doing some operation. At teh same time if HIGH priority thread want to acquire the same lockA, then is halts at the point waiting to get lockA. Here, even though the demanding thread is of HIGH priority it has to wait until LOW priority thread release the lockA. Thus the problem of Priority Inversion. In the mean time, if a MEDIUM priority thread want to execute and that doesnt need lockA, then it takes over and LOW thread is halted, which is even a more twist in the story. To avoid this, Priority inheritance and Priority Ceiling are used.
    5. Thread synchronization primitives
      • Join
      • Condition Variable
      • Barrier
      • Spinlocks
      • Semaphores
    Processes
    1. Multiple process creation is a better option in scenarios where completely separate memory areas for task as required. Example: The chromium project. its a web browser, where separate processes are created for every tab you create. This is to protect the overall application from bugs and glitches in the rendering engine. This brings to web browsing the benefits that memory protection and access control brought to operating systems.
    2. If your application will consist of separate, individually-usable components that communicate through well-defined protocols, each of which performs jobs that can individually succeed or fail without complicating the logic of the other components, then it's perfectly reasonable to write an application that utilizes multiple processes.

    PROFILE

    My photo
    India
    Design Engineer ( IFM Engineering Private Limited )

    Followers