Thursday, March 22, 2012

Reg communication protocols in Embedded

Not only for human beings communication is important even for silicon chips the communication is very important
In embedded communication can be divided mainly into 2 parts
1. PC to embedded controller communication
2. Embedded controller to embedded controller communication
Even there is another communication method called ASIC communication which happens internally in the ASIC
So now we will see the first part
1. For outside world to communicate with embedded controller a communication is required for example PC sends a command to switch on a relay based on ambient temperature
For this to happen a communication is required, communication means a set of protocol has to be transmitted from PC to Chip , chip has to understand what PC communicates and then has to take decision
In order chip to understand it must recognise first communication started so START bit is initiated and chip also need to understand that communication is terminated generally humans do by telling BYE in this case STOP bit
and chip also should understand that communication is meant for this chip then chip has address has humans has name
and the actual message
so we can tell that communication can be START ADDRESS MESSAGE STOP
but chip also should verify whether received data is correct as transmitted to avoid miscommunication so PARITY is also required
START ADDRESS MESSAGE PARITY STOP
This can be a basic communication from PC to chip
but to transmit data phsically from one place to another place we need physical medium so Tx, Rx, Gnd are minimum needed
Based on number of communication lines and protocols used
1, RS232 communication
2. RS485 communication
3. I2C Communication
4. SPI Communication
6. LIN Communication
7. CAN communication are mostly popular so please read the above communications througly before attending interview best of all

GOOD COMMUNICATION SKILLS ALONG WITH GOOD KNOWLEDGE ON COMMUNICATION PROTOCOLS FETCH YOU A GOOD JOB

BEST OF LUCK

Monday, March 19, 2012

Videos on Embedded systems

For all the video stuff please visit the facebook page Saty Endra

I dont want to distract the attention of readers :-)))

But videos are not updated still yet

Facebook page

search for Saty Endra on Facebook to find updates and subscribe

First Thing to Know

Embedded System is directly related to the microcontroller. So you should have at least knowledge on one microcontroller (8/16/32 Bit). Presently most of the embedded professionals are prefer to write the code in C language. So you should have a good knowledge on C. Instead of C if you know the some other languages like: - C++, Embedded C, Assembly and ADA, it will put very good impression on Interviewer.

Books to Read>

1. 8051 controller by Mazidi

Practice all the exercises not leaving a single example

Some more questions on Interview

1. Write the syntax of function pointer. Then write a function how to use declare, define and use the function pointer.
2. Take 2 string and contactinate with out using the library functions
copy one string to another string.
3. Difference between structure and union. gave a structure decalration and union declaration and asked to say the difference. how to assign the values or initialize a structure members and union members. what is the size of the structure of the given structure and the given union.
4. What is static? what happens if a variable is declared with the storage class static and what happens if the function is declared as static and what happens if the a function pointer is declared as static.
5. How to know the size of the 'int' of a particular micro controller whose int size is not known(1bit or 32bit) without using the sizeof operator.
6. if extern declaration is made in a .h file do we need to include this .h in the .c file?
7. If a static variable is declared both globally and locally in a function. then when we are using that varible in the function which variable will be considered whether the local variable or global variable?
8. How a volatile variable is used give one usecase how to use?
9. What is an inline function? what is the differnce between inline and macro? which will be better?
10. AUTOSAR archetecture where our modules lye and what are upper layers to that?
11. Write a macro to in generic way to set and bit and reset a bit.
12. write a function to mask nth bit of the number(set and reset).
13. questions regarding typecasting
14. CAN - how to gain the bus. and how to know which node had gained the bus.
Describe the field of the CAN frame
15. how to add two numbers using bitwise operators
16. Where the global, static, local variables are allocated in the RAM. What heap ares contais?
17. fibonic series program logic

strcpy(p, q)

strcpy(char *src, char *dst)
{
while(*src++ =! '\0')
{
*dst++ = *src++;
}
}

strcpy(p, q)

strcpy(char *src, char *dst)
{
while(*dst++ =! '\0')
{
*src++ = *dst++;
}
}

main()
{
int f1, f2, f3;
f1 = 0;
f2 = 1;
while(i != 8)
{
f3 = f2 + f1;
f1 = f2;
f2 = f3;
}
}

Set(int n)
{
return((data) || (01 << n));
}

Reset(int n)
{
return((data) && ~(01 << n));
}

Qualifiers in C

JB Enterprises Type Qualifiers In C 2005.06.28
Johan Bezem
tqinc.doc – rev. 1.1 Page 1 of 4
Type qualifiers in C
Abstract
Type qualifiers are part of the C language since 1989 (const and volatile) respectively 1999
(restrict). They are used to qualify types, modifying the properties of variables in certain ways.
Since qualifiers are one of the lesser-understood features of the language, this article aims at
experienced C programmers, and explains the reasoning behind these qualifiers.
Introduction
Since 'Standard C' (C89), all variables are considered "unqualified" if none of the available qualifiers is
used in the definition of the variable. Additionally, since all three qualifiers are completely independent
from one another, for each unqualified simple type, we may have seven (23 – 1) forms of qualified
types.
Beware that qualifiers change the properties of their variables only for the scope and context in which
they are used. A variable declared 'const' is a constant only as far as the current scope and context is
concerned. If we widen the scope (and regard, for instance, the callers of the function) or the context
(for instance, other threads or tasks, interrupt service routines, or different autonomous systems), the
variable may very well be not constant at all. For 'volatile' and 'restrict', similar arguments exist.
Consider calling 'memcpy', prototyped
void *memcpy(void *dest, const void *src, size_t len);
with a source pointer pointing to a regular (non-read-only) part of your memory.
Const
The qualifier 'const' is most often used in modern programs, and probably best understood. The
addition of a 'const' qualifier indicates that the (relevant part of the) program may not modify the
variable. Such variables may even be placed in read-only storage (cf. section ""). It also allows certain
kinds of optimizations, based on the premise that the variable’s value cannot change. Please note,
however, that "const-ness" may be cast away explicitly.
Since 'const' variables cannot change their value during runtime (at least not within the scope and
context considered), they must be initialized at their point of definition.
Example:
const int i = 5;
An alternate form is also acceptable, since the order of type specifiers and qualifiers does not matter:
int const i = 5;
Order becomes important when composite types with pointers are used:
int * const cp = &i; /* const pointer to int */
const int * ptci; /* pointer to const int */
int const * ptci; /* pointer to const int */
The pointer cp is itself const, i.e. the pointer cannot be modified; the integer variable it points to can.
The pointer ptci can be modified, however, the variable it points to cannot.
Using typedef complicates the placement issue even more:
typedef int * ip_t;
const ip_t cp1 = &i; /* const pointer to int */
ip_t const cp2 = &i; /* const pointer to int!! */
Casting away 'const-ness' is possible, but considered dangerous. Modifying a const-qualified variable
in that way is not only dangerous, but may even lead to run-time errors, if the values are placed in
read-only storage:
const int * ptci;
int *pti, i;
const int ci;
ptci = pti = &i;
JB Enterprises Type Qualifiers In C 2005.06.28
Johan Bezem
tqinc.doc – rev. 1.1 Page 2 of 4
ptci = &ci;
*ptci = 5; /* Compiler error */
pti = &ci; /* Compiler error */
pti = ptci; /* Compiler error */
pti = (int *)&ci; /* OK, but dangerous */
*pti = 5; /* OK, dangerous and potential runtime error */
PC-Lint and similar tools, as well as some compilers, will warn you about such dangerous situations, if
you will let them.
Placement
Placement of variables in actual memory is hardly standardized, because of the many requirements of
specific compilers, processor architectures and requirements. But especially for const-qualified
variables, it is a very interesting topic, and needs some discussion.
First of all, placement is compiler-specific. This means, that a compiler may specify how a programmer
or system architect may direct the linker an loader as to where to place which variables or categories
of variables. This may be done using extra configuration files, or using #pragma's, or some other way.
Refer to your compiler manual, especially when writing code for embedded systems.
If you revert to the compiler defaults, the compiler/linker/loader1 may put const-qualified variables (not
such combinations like 'pointer-to-const', since here the variable is a 'pointer' and non-const!) into readonly
storage. If the compiler has no other indication, and can oversee the full scope of the variable (for
instance, a static const int const_int = 5; at the global level in some C source file), it may
even optimize in such a way, that the variable effectively disappears (replacing each occurrence with
an immediate value), though not all compilers provide this kind of optimization.
If the compiler retains the variable as such (i.e. the variable is still present in the object-file), qualified
with the property 'const', the linker combines all corresponding references throughout all modules into
one, complaining if the qualifications do not match, and the loader gets to decide, where the variable is
placed in memory (dynamic linkers are even more complex, and disregarded here). If a memory area
with read-only storage is available, const-qualified variables may end up there, at the discretion of the
loader.
For details, consult your compiler manuals.
Volatile
The qualifier 'volatile' is normally avoided, understood only marginally, and quite often forgotten. It
indicates to the compiler, that a variable may be modified outside the scope of the program. Such
situations may occur for example in multitasking/-threading systems, when writing drivers with interrupt
service routines, or in embedded systems, where the peripheral registers may also be modified by
hardware alone.
The following fragment is a classical example of an endless loop:
int ready = 0;
while (!ready);
An aggressively optimizing compiler may very well create a simple endless loop (Microsoft Visual
Studio 6.0, Release build with full optimization):
$L837:
; 5 : int ready = 0;
; 6 : while (!ready);
00000 eb fe jmp SHORT $L837
If we now add 'volatile', indicating that the variable may be changed out of context, the compiler is
not allowed to eliminate the variable entirely:
1 In most compiler tool chains, the loader is an integrated part of the linker. For several embedded systems, however, the linker
only produces relocatable code segments, to be effectively placed in memory by the loader.
JB Enterprises Type Qualifiers In C 2005.06.28
Johan Bezem
tqinc.doc – rev. 1.1 Page 3 of 4
volatile int ready = 0;
while (!ready);
becomes (using the option "favor small code"):
; 5 : volatile int ready = 0;
00004 33 c0 xor eax, eax
00006 89 45 fc mov DWORD PTR _ready$[ebp], eax
$L845:
; 6 : while (!ready);
00009 39 45 fc cmp DWORD PTR _ready$[ebp], eax
0000c 74 fb je SHORT $L845
As you can see, even with aggressive, full optimization, the code still checks the variable every time
through the loop.
Most compilers do not optimize this aggressively by default, but it is good to know that it is possible.
The ordering issues as discussed in the section for the qualifier 'const' also apply for 'volatile'; if in
doubt, refer back to page 1.
When do you need to use 'volatile'?
The basic principle is simple: Every time when a variable is used in more than one context, qualify it
with 'volatile':
· Whenever you use a common variable in more than one task or thread;
· Whenever you use a variable both in a task and one or more interrupt service routines;
· Whenever a variable corresponds to processor-internal registers configured as input (consider
the processor or external hardware to be an extra context).
Does it hurt to use 'volatile' unnecessarily?
Well, yes and no. The functionality of your code will still be correct. However, the timing and memory
footprint of your application will change: Your program will run slower, because of the extra read
operations, and your program will be larger, since the compiler is not allowed to optimize as
thoroughly, although that would have been possible.
Why don’t we declare all variables 'volatile'?
Well, we partially do: On DEBUG-builds, all optimization is usually disabled. This is not quite the same,
since the read operations needed extra are not necessarily inserted, but for most practical purposes,
no optimizations involving the (missing) 'volatile' qualification are executed. This can be considered
at least partially equivalent.
We don’t, however, deliver DEBUG-builds to the customer: They usually are too big and too slow
(among a few other properties), just like if we declare all variables to be 'volatile'.
But, whenever in doubt, it is better to use 'volatile' unnecessarily, than to forget it when really
necessary.
Restrict
A discussion of the qualifier 'restrict' is postponed until later, for multiple reasons:
· It is a new addition for C99 (the standard from 1999), hardly available in older compilers;
· The optimizations enabled by using the 'restrict' qualification have been present in most
commercial compilers, however, only on a global basis (compiler options); the extra gain to be
expected is not quite as large, therefore, the necessity of using this qualifier can be considered
minimal;
· My personal experience with this qualifier is still minimal.
JB Enterprises Type Qualifiers In C 2005.06.28
Johan Bezem
tqinc.doc – rev. 1.1 Page 4 of 4
One hint: If your compiler doesn't implement 'restrict' (yet), and doesn't even reserve the keyword,
make sure to define a high-level macro, in order to prevent programmers using the name for a variable
or similar:
#define restrict /* Reserved word */
Combining qualifiers
In the introduction the existence of seven different qualified types for each (simple) unqualified one has
been stated. Using three qualifiers, this means that qualifiers can be combined.
In C89, each qualifier may only be used once, in C99, multiple occurrences of each single qualifier are
explicitly allowed and silently ignored.
But now, take 'volatile' and 'const': What does it mean to have a variable qualified with both:
const volatile unsigned int * const ptcvi = 0xFFFFFFCAUL;
OK, the initialization value is hexadecimal, unsigned long, and taken from an imaginary embedded
processor. The pointer is const, so I cannot change the pointer. And the value it points to is "const
volatile unsigned int": I cannot change the value (within my current scope and context), and
the value may be changed out of context.
So, imagine a free running counter, counting upwards from 0 to 65535 (hexadecimal 0xFFFF or 16 bit),
and rolling over again to 0. If this counter is automatically started by the hardware, or started by the
(assembly-coded) startup-routine (outside the cope of the C program), is never stopped, and only used
for relative time measurements, we have exactly this situation: The counter is read-only, so I want the
compiler to supervise all programmers, that they do not try to write the counter register.
At the same time, the value is constantly changed, so if I want to use the value, the compiler better
make sure to re-read the value in every single case.
You can also imagine a battery backed-up clock chip, running autonomously, with values for the
current date and time memory-mapped into the processors virtual memory space.
OK, such situations will not occur every day, and for many programmers they will never occur. But it is
not unimaginable. And now go out and ask the most experienced C programmer you know, whether it
is possible, allowed and/or useful. You’ll be amazed about the answers you’ll get (or maybe not).
Literature:
C A Reference Manual – (5th edition); Samuel P. Harbison III, Guy L. Steele Jr; Prentice hall, 2002;
ISBN 0-13-089592X.
Comments, remarks and criticism are welcome.
Johan Bezem
j.bezem@computer.org
http://www.bezem.de

Learn this and get job

Interview Questions:
1. What are static variables?
2. What are volatile variables?
3. What do you mean by const keyword ?
4. What is interrupt latency?
5. How you can optimize it?
6. What is size of character, integer, integer pointer, character pointer?
7. What is NULL pointer and what is its use?
8. What is void pointer and what is its use?
9. What is ISR?
10.What is return type of ISR?
11.Can we use any function inside ISR?
12.Can we use printf inside ISR?
13.Can we put breakpoint inside ISR?
14.How to decide whether given processor is using little endian format or big endian format ?
15.What is Top half & bottom half of a kernel?
16.Difference between RISC and CISC processor.
17.What is RTOS?
18.What is the difference between hard real-time and soft real-time OS?
19.What type of scheduling is there in RTOS?
20.What is priority inversion?
21.What is priority inheritance?
22.How many types of IPC mechanism you know?
23.What is semaphore?
24.What is spin lock?
25.What is difference between binary semaphore and mutex?
26.What is virtual memory?
27.What is kernel paging?
28.Can structures be passed to the functions by value?
29.Why cannot arrays be passed by values to functions?
30.Advantages and disadvantages of using macro and inline functions?
31.What happens when recursion functions are declared inline?
32.#define cat(x,y) x##y concatenates x to y. But cat(cat(1,2),3) does not expand but gives
preprocessor warning. Why?
33.Can you have constant volatile variable? Yes, you can have a volatile pointer?
34.++*ip increments what? it increments what ip points to
35.Operations involving unsigned and signed — unsigned will be converted to signed
36.malloc(sizeof(0)) will return — valid pointer
37.main() {fork();fork();fork();printf("hello world"); } — will print 8 times.
38.Array of pts to functions — void (*fptr[10])()
39.Which way of writing infinite loops is more efficient than others? there are 3ways.
40.Who to know whether system uses big endian or little endian format and how to convert
among them?
41.What is forward reference w.r.t. pointers in c?
42.How is generic list manipulation function written which accepts elements of any kind?
43.What is the difference between embedded systems and the system in which RTOS is running?
44.How can you define a structure with bit field members?
45.How do you write a function which takes 2 arguments - a byte and a field in the byte and
returns the value of the field in that byte?
46.Which parameters decide the size of data type for a processor ?
47.What is job of preprocessor, compiler, assembler and linker ?
48.What is the difference between static linking and dynamic linking ?
49.How to implement a WD timer in software ?