Wednesday, June 27, 2012

Understanding Embedded System Protocol - Basics

UART Protocol
  •   UART stands for Universal Asynchronous Receiver Transmitter.
  •   It is a way to transmit data from one device to another.
  •   The 9 pin serial port that are still standard on most computers is an example of a UART.
  •  The baud rate is the number of bits that can be sent in one second. This is NOT the same as the amount of data that can be sent
  •  The data rate is always less than the maximum transmission rate.
  •   Most of the time, you need to send 11 bits to be able to receiver one byte of data.
  • The parity bit is a bit used to check the integrity of the data. Usually it counts the number of ones (or zeros it’s the same thing), and indicates whether the number is even or odd. This ensures that if there is an error in ONE bit, the receiver will be able to detect it.
  •   Overall, the system works like this (one way transmission): A wire connects from the transmission output pin of the transmitter to the reception input pin of the receiver.
  • Once the wires are established and the baud rate is determined on BOTH parties, the transmission can begin.
  •   Each section of the grid represents one time unit equal to the inverse of the baud rate. For example, if we were at 2400 bps, then each section of the grid in the above picture represents 1sec/2400 = 416.7 usec.
  •  When a transmission begins, the transmitter must first pull the line down to a zero, for one baud period. Then it sends the data, with the least significant bit (lsb) of the byte first. Next the transmitter sends the parity, and lastly, pulls the line back to a high, the stop bit, to indicate that the transmission has ended. Another transmission cannot begin until at least the end of the stop bit. (There can be one or more start/stop bits).

I2C Protocol 

  • Most electronics today uses I2c to reduce design cost and pin count, uses the I2C bus for inter-IC.
  •  I2C is a communications bus invented by Philips.Ø
  • The I2C bus requires two wires to be connected between the master and slaves, as well as a return (GND) wire.
  •   There are provisions in the I2C standard for multi-master environments, as well as 10 bit addressing.
  •   The most simple and commonly used configuration is the single master, 7 bit addressing.
  •  I2C is synchronous form of communication.
  • The master device sends both the data signal, as well as the clock signal, with the exception during data reads. The receiver syncs up with the clock signal, and samples the data on the rising edge of the clock. Ultimately, this means that the transmitter and receiver do not need to know the clock rate because a clock signal is sent along with data.
  •   In I2C more than 1 receiver (slave) can be selected. This is the advantage over UART.
  •   The convention is to name the data line SDA, and the clock line SCL. 
  •  
  • Hardware:
  •  Obviously, you’ll need to have two devices: a master I2C device and a slave I2C device.
  • Second, the I2C bus is an open drain pull-down bus. What this means is that the bus needs to be pulled up to a nominal voltage (Vdd) when no devices are occupying the wires. This assures that when the bus is idle, the voltages on data and clock are always at Vdd.
  •  Usually 3.3 to -2v or +5 to -5 V.
  • Communications:
  • Let’s say there is one master device and one slave device on an I2C bus line with pull-up resistors pulling to 3.3V.
  • When the bus is idle, both the SCL and SDA lines are pulled high by the pull-up resistors.
  •   If the master wants to send a byte of data, it must first initiate a start bit (Label S), where the SDA line is pulled to a logical 0, while maintaining a logical 1 on the SCL line.
  •   Next it starts sending a clock signal. Because the SCL is pulled high at the start bit, the master must pull SCL down for half a clock cycle, before the first rising edge.
  • Data is only sampled on the rising edge, at all other times, the master is allowed to transition the SDA between a logical 0 and a logical 1.
  •   On the first rising edge, the SDA line must have stabilized.
  •  The slave device samples the data (Label B1). This process is repeated until the 8th rising edge, or the 8th bit.(7bits for address and 1 bit specifies read(1)/write(0))
  •  Right after the 8th rising edge, the master device releases control of the SDA line. It has finished sending its data, and is now awaiting a response from the slave device.
  •  The slave device has one clock cycle to transition the SDA clock to a logical 0 to acknowledge the reception of the data byte. (/ACK bit)(if ack is 0 slave says data received properly, if 1 ,something went wrong).
  •  The master then sends a 9th rising edge on the SCL line, and samples the SDA line.
  •   Lastly the SCL is held high, and subsequently the SDA line is pulled high to signal a stop bit (P).
  •  
  • I2C Packet
  •   3bytes are send 8bits-device address, wordaddress-8bits, data- 8bits
  •  The first 7 bits is the slave device address. In order for the I2C bus to allow for multiple slaves, the master must indicate which device the message is intended.
  •   Obviously there cannot be multiple slaves with the same address. This would create a collision on the I2C bus.
  •  The 8th bit of the first byte is the read/write indicator.
  •   If the bit is a 0, the master wants to write a byte, if it is a 1, the master intends to read.
  •  Next, on the 9th bit of the first byte, the master waits for an acknowledge bit from the slave.
  • If the /ACK is a 1, a non-acknowledge, then the master thinks there is no slave with the sent address. It terminates the packet with a STOP bit. However, if a slave does send back a /ACK with logical 0, then the master device begins sending the second byte.
  •  Whenever I want to write to the device, I must specify which one of the 256 memory locations I wish to write to. This is what the second byte of the packet is usually used for.
  • On the 9th byte of the second byte the slave device must acknowledge that the specified address exists as one of the slave device’s internal addresses.
  •  Lastly, the master device sends the data it intends to write. This is usually referred to as the data byte.
  •   Finally the slave device acknowledges the received byte, and the communications is terminated by a stop bit.
  • Other pattern packet
  •  
  • I²C is limited to 1Mbps in Fast Mode+ and to 3.4 Mbps in High Speed Mode.

SPI Protocol

  •   This protocol is basically limited to 127 devices, and a memory device address space of 256 bytes.
  •   In SPI, there is no set order on how the data is accessed.
  •   I can set up the data access in any which way I want.
  •   I can have the memory address width to be 8 or 16 bits for example, and set the data width to be 8 or 16 or even 32 bits.
  • There is one big advantages going for the SPI protocol compared to I2c is that it is fast. Any times you have a large amount of access, for example storing/retrieving a large table
  •  The protocol usually allows up to 10Mb/s whereas the I2C usually tapers out at around 400Kb/s.
  •  
  • Physical Connections
  •  
  •   3 common signals + 1 device specific (CS).
  •  SCLK is the serial clock given by the micro controller. It is used by the slave devices to sample the incoming data and output the outgoing data.
  • There are two data lines going to and from the microcontroller.(MOSI –MISO)or (SDO-SDI).
  • Unlike I2C, which selects the device according to the chip address in the first byte of a data packet, device selection in SPI is accomplished by the CSB/SS signal. )“chip select bar” (CSB) or “slave select” (SS)).i.e. (/CS – The signal is active low)
  • When the signal for a particular device is lowered, that device is selected and considered active.
  •  
  • Timing
  •  
  •   When configuring your microcontroller, there are usually two settings that you need to take into account: clock polarity and clock phase.
  •  
  • 1.       CPHA = 0, CPOL = 0 – first bit starts as soon as SS is lowered, data sampled at rising edge of clock, data changes on falling edge.
  • 2.       CPHA = 0, CPOL = 1 –first bit starts as soon as SS is lowered, data sampled at falling edge, data changes on rising edge.
  • 3.       CPHA = 1, CPOL = 0 – first bit starts on first clock edge, data sampled at rising edge, data changes on falling edge.(No wfmS)
  • 4.       CPHA = 1, CPOL = 1 – first bit starts on first clock edge, data sampled at falling edge, data changes on rising edge. .(No wfmS)
  •  
  •  
  •  I can see that the first bit is already active even before the first clock edge. Also the data changes on the falling edge and is sampled at the rising edge.
  • SPI is full duplex
  •                          
  •   Half duplex                                         Full Duplex                                                                                                                                                 
  • SPEED 10MBPS
     
  •  
eMMC/SD Protocol
 
  • SD/MMC (Secure Digital/Multimedia Card) cards are small, high-speed storage media for portable items such as digital cameras, mobile phones, net book computers and similar devices.
  • The communications protocol is similar to SPI protocol.
  • SD was developed out of the MMC format. 
  •  Signals used are CLK, Data and CMD.
  •   Modes – SDR-DDR-BOOT
  •  
  • Concepts:
  •  CLK: Each cycle of this signal directs a one bit transfer on the command and either a one bit (1x) (SDR)or a two bits transfer (2x) (DDR)on all the data lines.
  •  
  • One Bit transfer on CMD line fig1
  •  
  • One Bit transfer on DATA line (SDR)   fig2     (only rising edge)
  • Two Bit transfer on DATA line (DDR) fig3   (rising edge and falling edge)
  •  
  •  CMD: This signal is a bidirectional command channel used for card initialization and transfer of commands.
  • Commands are sent from the Multimedia Card bus master to the card and responses are sent from the card to the host.
  • DAT0-DAT7(Data Lines): These are bidirectional data channels. Only the card or the host is driving these signals at a time.
  •  
  • Packet Format

  •  
  • Total of 48 bits
  •  
  • A command always starts with a start bit (always ‘0’), followed by the bit indicating the direction of transmission (host = ‘1’).(card =0)
  • The next 6 bits indicate the index of the command, this value being interpreted as a binary coded number (between 0 and 63).
  • Some commands need an argument (e.g. an address), which is coded by 32 bits.
  •  All commands are protected by a CRC (7 bits).
  •  1 end bit.
  •  Based on command index we can get to know the state of host or receiver.
  •   Based on transmission bit we will get to know who is communicating.
  •  Data starts sampled at the rising edge of the clock

CAN Protocol 


  • The CAN communication protocol is a CSMA/CD protocol. Carrier Sense Multiple Access with Collision Detection.
  • What this means is that every node on the network must monitor the bus for a period of no activity before trying to send a message on the bus (Carrier Sense).
  • Also, once this period of no activity occurs, every node on the bus has an equal opportunity to transmit a message (Multiple Access).
  •  If two nodes on the network start transmitting at the same time, the nodes will detect the ‘collision’ and take the appropriate action.
  •  
  •  First, logic states need to be defined as dominant (0) or recessive(1).
  • Second, the transmitting node must monitor the state of the bus to see if the logic state it is trying to send actually appears on the bus.
  • A dominant bit state will always win arbitration over a recessive bit state, therefore the lower the value in the Message Identifier (the field used in the message arbitration process), the higher the priority of the message.
  • CAN protocol is a message-based protocol, not an address based protocol.
  • This means that messages are not transmitted from one node to another node based on addresses.
  • Embedded in the CAN message itself is the priority and the contents of the data being transmitted.
  • All nodes in the system receive every message transmitted on the bus (and will acknowledge if the message was properly received).
  • It is up to each node in the system to decide whether the message received should be immediately discarded or kept to be processed.
  •  
  • For example, an automotive airbag sensor can be connected via CAN to a safety system router node only. This router node takes in other safety system information and routes it to all other nodes on the safety system network. Then all the other nodes on the safety system network can receive the latest airbag sensor information from the router at the same time, acknowledge if the message was received properly, and decide whether to utilize this information or discard it.
  •  
  •  Another useful feature built into the CAN protocol is the ability for a node to request information from other nodes. This is called a Remote Transmit Request (RTR).
  •  
  •  For example, a safety system in a car gets frequent updates from critical sensors like the airbags, but it may not receive frequent updates from other sensors like the oil pressure sensor or the low battery sensor to make sure they are functioning properly. Periodically, the safety system can request data from these other sensors and perform a thorough safety system check. The system designer can utilize this feature to minimize network traffic while still maintaining the integrity of the network.
  •  
  • One additional benefit of this message-based protocol is that additional nodes can be added to the system without the necessity to reprogram all other nodes to recognize this addition. This new node will start receiving messages from the network and, based on the message ID, decide whether to process or discard the received information.
  •  
  • FRAME DESCRIPTION
  •  
  • CAN protocol define four different types of messages (or Frames).
  • 1.     DATA FRAME.
  • 2.     Remote Frame, Frame, which is basically a Data Frame with the RTR bit set to signify it is a Remote Transmit Request.
  • 3.      Error Frame – handling errors - Error Frames are generated by nodes that detect any one of the many protocol errors defined by CAN.
  • 4.      Overload Frame –handling errors are generated by nodes that require more time to process messages already received
  •  
  •  
  • DATA FRAME(44 +8N)
  •  
  • 1.     Arbitration Field 12bits (11 identifier bits and 1 RTR bit)
  • 2.     Control Fields 6 bits
  • 3.      Data Fields no of data bytes described in the data length code of control field.
  • 4.      CRC Fields 15 bits + 1 crc delimeter
  • 5.       2-bit Acknowledge Field and an End of Frame(7bits)).


FlexRay Protocol (Image Courtesy - NI )
  •  For automobiles to continue to improve safety, increase performance, reduce environmental impact, and enhance comfort, the speed, quantity and reliability of data communicated between a car's electronic controls units (ECU) must increase.
  •  
  • FlexRay uses unshielded twisted pair cabling to connect nodes together
  •  
  • FlexRay Topology
  • Protocol
  • The FlexRay protocol is a unique time-triggered protocol.
  • FlexRay manages multiple nodes with a Time Division Multiple Access or TDMA scheme.( two nodes were to write at the same time).
  •  Every FlexRay node is synchronized to the same clock, and each nodes waits for its turn to write on the bus.
  •  
  • Communication Cycle
  • Static Segment 
    Reserved slots for deterministic data that arrives at a fixed period. 
  • The segment is broken up into slots, each slot containing a reserved frame of data.  When each slot occurs in time, the reserved ECU has the opportunity to transmit its data into that slot. Once that time passes, the ECU must wait until the next cycle to transmit its data in that slot.
  • If an ECU goes offline or decides not to transmit data, its slot remains open and is not used by any other ECU, as shown in Figure 4. 
  • Dynamic Segment
    The dynamic segment behaves in a fashion similar to CAN and is used for a wider variety of event-based data that does not require determinism. 
  • Symbol Window
    Typically used for network maintenance and signaling for starting the network. 
  • Network Idle Time
    A known "quiet" time used to maintain synchronization between node clocks
    . The ECUs make use of this idle time to make adjustments for any drift that may have occurred during the previous cycle.
  •  
  • FRAME FORMAT
  • The Header is 5 bytes (40 bits) long and includes the following fields: 
  • Status Bits - 5 bits
  • Frame ID - 11 bits
  • Payload Length - 7 bits
  • Header CRC - 11 bits
  • Cycle Count - 6 bits
  • The Frame ID defines the slot in which the frame should be transmitted and is used for prioritizing event-triggered frames.
  •  The Payload Length contains the number of words which are transferred in the frame.
  • The Header CRC is used to detect errors during the transfer.

Friday, January 6, 2012

Application Support Engineer, Mathworks Interview

Interview pattern is good. But the way they conduct it onsite was really bad
1) Phone screening by the TS Manager(Tech Support Manager)(20 mins or so ).Its basically a background check.(Was informed about the technical interview will conduct after 2 days)
2) After 2 days a Telephonic Technical Interview by a Application support Engineer from UK(1 hour)
It consists of Programming skills, Maths, C, MATLAB, Control Theory(One can choose embedded,DSP or control theory)

Maths- Eigen values, eigenvectors, linear diff eqn, partial differential eqn, matrix multiplication, matrix addition,explain differencetion and integration via graphs and their difference, definite n indefine integral, is -inf to +inf indefinite integral, probability prob, singular matrix, identity matrix, dot product etc
MATLAB - difference b/w a*b and a.*b,dot product, cross product in matlab, diffrnec b/w function and scripts, mex functions, workspace, current directory, cell array, transpose, determinant, datatypes in matlab etc
Programming skills - what is x86, x64 n wat do they stand for, string reversal, string palindrome, multicore processor, call by value and reference, pointer, recursive funtions, local and global variables, #define and const difference, mallac, null pointer and its significance, ++a and a++ difference, what is typedef, y we use it, C compilation procedure etc
Control Theory - 2nd order differential eqn, damping ratio, natural req, relationship b/w them, y do we need zeta, stabilty, RH -criterion, LTI systems, ROC, state space representations etc

Well, this one went on good, as I managed to answer more than 95% of them correctly. Even if u dont know try to make an attempt.
After few days of this interview the TS manager called me again for an Onsite interview.

This onsite interview is where things get harder. Even though ppl around acts like friendly but in reality they wern't.

I was told I will be facing a puzzle technical round followed by a application support team meet, video conferencing technical interview,
an casual interview with TS manager nad a few behaviourial round of interviews.

1st Puzzle, Programming Technical interview by a guy 4m developement team:
The guy seemed to be a moron to me. I was asked 3 puzzles and the guy who asked the same didnt put up the questions in the proper way. Finally I forced him to write to down the paper. I explain them here

1 Question: 9 balls out of which one is bigger. how do u find the bigger one. All seems alike. He didnt say that I have a weighing machine. I gave him answer using digital weighing machine. Later
he asked to do with analog weighing. I said it can be done in 3 timesie 4,4,1. He said to optimise it. I couldnt. He only new the answers and ter was no value for yout efforts. The answer was 3,3,3 which can be
resolved in only 2 weighings

2 Question: Bag with 16 red, 15 white, 33 green balls. Number of ways of picking the same. Pick One ball at a time.
I started working with combinations after few minutes he said the answer is simple, I just asked you how many times
I need to put my hand inside bag to get same color. I said him tat he told "Number of ways" to do it. The moron refused it.

Then I said 16+15+1 = times to get green ball
He said he can do tat in 4 ways. I did the above with the ball picking in the succession. I refused it saying he didnt said succession.
Answer: 1 - white, 2-green, 3-red, 4- can be anything. You got 2 colors and they call this as a puzzle. ha ha


3 Question: It was on C. Reverse a string Program.
I wrote and explained him( I used 2 arrays). He said to reduce the memory size WITHOUT USING ANOTHER VARIABLE. I was confused and was trying different.
Later he said he gave a funny clue n said "Do it by swapping".
I told him that one can swap only if he has 2 variables. Then use it he said. I told him tat he had asked me to do it without using another variable. He refused once again
saying that he had only asked me reduce the memory size. I wrote the program with a dummy char variable instead of an array and explained the same.
Still wondering how can Mathworks take such ppl into developement team.


2interview with the Application support engineer:
This is a formal background and get together kind of interview. 2 Application support engineers and me. This is where you get a chance to ask them who they work, whom tey work,
work environment etc. It was a good one indeed. They offered snacks and behaved like friends.


3interview(Pure technical)video conferencing with the US guy

Again the same Math, Matlab, Control Theory, Programming skills

Matlab - why, who, which command, subfunctions
Control Theory - asks you solve the problems. Lots of problems. Eigen values,vectors. Lapnov Stability, obeserbility,system controllable or not kind of prob
I answered and solved the problems but I didnt noe Lapnav stability and couldnt solve its problem



4th interview with TS manager:
I was totally Pissed off by the time and he gave only 4 mins for me to refresh. As I entered the interview room, he started asking me behavioural question

Y mathworks?? what challenge do you find in this job??I said customer prob is my challenge. "What else one can expect a challenge for an Application Support engineer".
Ur strenghts( I kept silent for 4 mins. I was totally out of mind I needed some break and waterto drink, he offered it).
Strengths??, weakness??, greatest disappointment??, what does ur current manager say about u??, wat do u think about ur manager??,
Again what challenges in this job?? ur greatest disappointment??, Ur acheivement??, Ur rewards for the same??, I told him tat ter plans of
doing my Phd 4m IISc while answering these questions. After how many yrs u wanted to do it??I said 3yrs 4m nw. Y only IISc?? Why Phd??U want to call urself a Dr. is it???
That's it. I was out. He said he couldnt able to process me further until technical results wer out.

I never sounded negative in my thoughts but I took a break during this interview which may sound odd to him. I wonder that was an behaviral or stress interview.
I was disappointed not because I was not selected, but because of the way the interview was conducted.

The next day my application in there website turned to "MATCH NOT IDEAL THIS TIME"

-----JUST A WASTE OF TIME-----