Thursday, March 31, 2011

Unique device serial number

A unique device serial number for security and other purposes.
On power-up I would read the area of non-volatile memory that was intended to hold the serial number and other information.
If the information passed a validity check I would write the serial number into another special area of non-volatile memory.
If the validity check failed I would instead write a fictitious serial number.

Note: Our serial numbers were to be based on the production shift,production line, and the data and time of production.
I worked with the manufacturing group to create a serial number based on a non-existent production shift and line to guarantee my fictitious serial number would never be mistaken as genuine.

Mike Ficco - Cut & Paste Engineering

Friday, March 25, 2011

Sine wave generation in matlab/octave


frequency = 1000;
timeperiod = 1/frequency;
amplitude = 1;
dcoffset = 0;
t=0:0.00001:2*timeperiod;
out=dcoffset+amplitude*cos(2*pi*frequency*t);
plot(t,out)


Tested on Octave 3.2.4: March - 25, 2011



%Requirement: For plotting 2 periods of the signal
fs = 100000; %100KHz sampling frequency
f = 1000; %1KHz signal frequency
t = 0:1/fs:2*(1/f);
x = sin(2*pi*f*t);
plot(t,x)


%Requirement: For getting 10 sec samples

fs = 100000; %100Khz sampling frequency
f = 2; % 2Hz signal frquency
t = 0:1/fs:10-1/fs; %10sec sampleshttp://www.blogger.com/img/blank.gif
x = sin(2*pi*f*t);
plot(t,x)


%Requirement: For getting 100 samples
fs = 500; %500Hz sampling frequency
f = 100;
t = (0:100-1)/fs;
x = sin(2*pi*f*t);
plot(t,x)

Matlab Tutorial link

Monday, March 21, 2011

dBm and watts


  • dBm = (10Log10(milliWatts))


  • 1 mW = 0 dBm





  • Watts = 10((dBm - 30)/10)


  • milliWatts = 10(dBm/10)


  • Example:
    Gps Received Signal Power in watts = 1.41e-16W
    Gps Received Signal Power in milliwatts = 1.41e-13mW
    Gps Received Signal Power in dBm = -128.5dBm

    watts-dbm Calculator

    #C_CODE: Single Linked List Manipulations in C

    /* Details: Single linked list manipulation functions
    * Author : Vinod Sasidharan
    * Date : Monday, March 21, 2011, 11.41 am
    * Remarks: reverse, findNthNodeFromLast,deletedata
    */


    /* Include files */
    #include <stdio.h>
    #include <malloc.h>
     
    /* typedef for meaningful name */
    typedef struct singlelink node;
    
    /* The single linked list structure */
    struct singlelink
    {
        int data;
        node *link;
    };
    
    /* Function: append()
     * Appends the input data to the end of the linked list.
     * If it's the 1st node, then process it separately.
     */
    void append(node **q, int num)
    {
        node *temp = *q,*r;
        r = (node *)malloc(sizeof(node));
        r->data = num;
        r->link = NULL; //This would be the last node in list
    
        if(NULL == temp) // If it's going to be the 1st node...
            *q = r;
        else
        {
            //Traverse through the list to reach the last node
             while(NULL != temp->link) 
                temp = temp->link; 
    
            temp->link = r;
        }
    }
    
    /* Function: display()
     * Displays the linked list data on the standard output  
     */
    void display(node *temp)
    {
        printf("\nThe list: ");
        while(temp)
        {
            printf("%d ",temp->data);
            temp = temp->link;
        }
        printf("\n");
        getchar(); // Wait for user.
    }
    
    /* Function delatbeg()
     * Deletes the node at the beginning of the list
     */
    void delatbeg(node **q)
    {
        node *temp = *q;
        *q = temp->link;
        free(temp);
    }
    
    /* Function: addatbeg()
     * Adds a node at the beginning of the list with input data.
     */
    void addatbeg(node **q,int num)
    {
        node *r;
        r = (node *)malloc(sizeof(node));
        r->data = num;
        r->link = *q;
        *q = r;
    }
    
    /* Function: findNthNodeFrmLast()
     * Find the data in the Nth node(input) from last pos in
     * the list.
     */
    void findNthNodeFrmLast(node *temp,int pos)
    {
        node *Nth = temp;
        int i = 0;
    
        while(temp)
        {
            if(i++ >= pos) //The core logic
                Nth = Nth->link;
            temp = temp->link;
        }
        printf("Data in position %d node from last: %d\n",
                pos,Nth->data);
        getchar(); //Wait for user...
    }
    
    /* Function: reverse()
     * Reverse the linked list.
     */
    void reverse(node **q)
    {
        node *temp1 = *q;
        node *temp2 = NULL;
        node *temp3 = NULL;
    
        if(!temp1) return; //Sanity check
    
        while(temp1)
        {
            temp2 = temp1->link; // Save the next pointer
            temp1->link = temp3; // Save the previous
            temp3 = temp1; // Update the previous
            temp1 = temp2; // Update the next
        }
        *q = temp3; // Update the head node pointer.
    }
    
    /* Function: deletedata()
     * Deletes a particular data(input) from the list
     */
    void deletedata(node **q,int data)
    {
        node *temp = *q, *old = NULL;
        while(temp)
        {
            if(data == temp->data)
            {
                if(*q == temp)
                    *q = temp->link;
                else
                    old->link = temp->link;
    
                free(temp); // Free the node using the free().
                return;     // Expected exit.
            }
            else
            {
                old = temp;
                temp = temp->link;
            }
        }
    }
    
    /* Function: Main() 
     * Calls the single linked list manipulation functions 
     */
    int main()
    {
        /* Initialize the head node */
        node *head = NULL; 
        int i;
    
        /* Append 10 nos of data to the linked list */
        for(i=0; i<10; i++)
            append(&head,i);
    
        /* Display the linked list */ 
        display(head);
    
        /* Reverse the linked list & display it. */
        reverse(&head);
        printf("The reversed list: ");
        display(head);
    
        /* Find Nth node from last */
        findNthNodeFrmLast(head,3);
    
        /* delete a particular data */
        deletedata(&head,4);
        printf("Delete data 4: ");
        display(head);
    
        /*Delete the node at the beginning of the list */
        delatbeg(&head);
        delatbeg(&head);
        printf("Delete 1st two data: ");
        display(head);
    
        /* Add a data at the beginning of the list */
        addatbeg(&head,22);
        addatbeg(&head,25);
        printf("Add 22 & 25 at beginning: ");
        display(head);
    }

    Tested on: Visual C++ 2008 Express Edition, Windows XP

    Tuesday, March 15, 2011

    #C_CODE: palindrome code in c using pointers.



    /* Details: Palindrome code using pointer
    * Author : Vinod Sasidharan
    * Date : Wednesday, March 16, 2011, 10.14 am
    * Remarks: Multiple words possible in input string due
    * to use of fgets.
    */


    /* Include files */
    #include <stdio.h>
    #include <string.h>

    /* Function prototype declaration */
    unsigned char palindrome(char *s);

    /* Function name: Palindrome
    * Input : Pointer to a string
    * Output : 8bit unsigned - 0/1
    */
    unsigned char palindrome(char *s)
    {
    char *s2 = s + strlen(s) - 1;

    /* Sanity check */
    if(!*s) return 0;

    while(*(s2--) == *(s++) && *s);
    return((unsigned char)(!*s && *(++s2) == *(--s)));
    }

    /* Main function */
    int main()
    {
    char buff[35];

    fputs("Input string: ",stdout);
    fgets(buff,sizeof(buff),stdin);
    fflush(stdin);

    /* To avoid the \n in the input string */
    *(buff + strlen(buff) - 1) = '\0';

    printf("Is the string %s palindrome: %s\n",buff,
    palindrome(buff)?"YES":"NO");
    getchar();
    return 0;
    }

    Tested on: Visual C++ 2008 Express Edition, Windows XP

    Monday, March 14, 2011

    How is size of int determined?

    int data type's size is usually chosen as natural size of the ALU.
    Width of ALU = Size of operand.
    Popular ALU's are 16 bits or 32 bits.
    int is a non-portable data type.
    To develop portable programs, two suffixes are used viz. short and long.

    NOTE: Size of every type of pointer is the size of int

    Wednesday, March 9, 2011

    C Code:Estimate Mean,Variance & Standard Deviation




    #include <stdio.h>
    #include <math.h >

    /* Compute a running average and variance of a set of input values. */
    void AvgVar(double x, double *mean, double *var)
    {
        static int n = 0;
        static double sum = 0,sumSq = 0;
        sum += x;
        sumSq += (x*x);
        n++;
        *mean = sum/n;
        *var = (sumSq/n) - (*mean * *mean);
    }
    int main()
    {
        double arr[10] = {4,2,5,3,2,4,2,6,3,4,};
        double mean,var;
        int i;
        for(i=0; i<10; i++)
            AvgVar(arr[i],&mean,&var);
        printf("Mean = %f, Variance = %f\n",mean,var);
        printf("Standard Deviation = %f\n",sqrt(var));
        getchar();
        return 0;
    }

    Ref: eetimes.com - Jack Crenshaw

    Tuesday, March 8, 2011

    Priority inversion

    In priority based scheduling, a high priority task may be waiting for a resource held by a low priority task, which in turn is not running.
    The priority of this task effectively gets lowered to that of the task holding the resource.
    Until the low priority task gets the CPU, the high priority task cannot run.

    Disadvantages of having many tasks in an RTOS

    1. More likely to have data sharing between tasks: increased likelihood of shared-data
    problems.
    2. More need fo semaphores to protect shared data: increased overhead and possible semaphore bugs.
    3. More need for messages, queues, pipes: increased overhead and likelihood of bugs.
    4. More memory needed for stacks,message buffers.
    5. More CPU time spent switching tasks
    6. More calls to RTOS,increasing system overhead.

    Recommendation:
    “other things being equal, use as few tasks as you can get away with; add more tasks to your design only for clear reasons.”

    What makes an OS an RTOS?

    1. Multi threaded and preemptive (i.e. the kernel itself is preemptive)
    2. Thread priorities (No deadline driven systems exists)
    3. Predictable thread synchronization mechanisms
    4. Priority inheritance
    5. Known OS behavior

    Code Optimization Techniques

    1. Inline functions - Eliminates the runtime overhead associated with the function call. (For functins with few lines of code)
    2. Table lookups - A table of pointers to functions
    3. Hand-coded assembly - Compiler not aware of special instructions
    4. Register variables - Place variable in general-purpose register rather than on stack
    5. Fixed-point arithmetic - Avoid using a floating point library which contains software subroutines which will emulate floating-point instructions.
    6. Variable size -Use processor's native register width for variables whenever possible.
    7. Loop unrolling - Unrolling duplicates the body of the loop multiple times.

    Murphy's law

    "The first time you enable the compiler's optimization feature, your previously working program will suddenly fail"

    The ARM processor

    ARM has seven basic operating modes:
    1. USER: Unprivileged mode under which most tasks run
    2. FIQ: Entered when a high priority(fast) interrupt is raised
    3. IRQ: Entered when a low priority(normal) interrupt is raised
    4. SUPERVISOR: Entered on RESET and when a software interrupt instruction(SWI) is executed.(Protected mode for operating system support)
    5. ABORT: Used to handle memory access violations
    6. UNDEF: Used to handle undefined instructions
    7. SYSTEM: Privileged mode using same registers as user mode
    - Except USER mode all other modes are privileged
    ---------------------------------
    T = Thumb instruction set support
    D = Debug interface support(JTAG/ICE)
    M = Multiplier (clock related)
    I = Interrupt (Fast interrupt support)
    E = Enhanced DSP instructions
    J = Jazelle (Java byte code execution)
    S = Synthesizable core
    T2= Thumb2 instruction set
    Z = TrustZone instructions
    -----------------------------------
    R0 - R12 are general purpose registers
    R13 is Stack pointer(SP)
    R14 is subroutine link regiser(LR)
    R15 is program counter(PC)
    CPSR is current program status regiser

    NOTE: R0-R3 
        When a function is called the arguments are passed in these registers.
        The return values of a function are also placed in these registers.
        (R0 - for a     32bit value)
    -----------------------------------
    Exceptions have priorities:
    Reset (highest priority)
    Data Abort
    FIQ
    IRQ
    Prefetch Abort
    SWI (Lowest priority)
    ----------------------------------