Wednesday 11 December 2013

Sed utility

Sed quick learn for general editing and use.
This is just a quick reference form myself and others who don't use it regularly.

Sed is one more editing utility inspired from ed command. Script ed is what it basically is.
ed edits a line at a time while sed edits all line in general it is a global.
sed [-e] 'instruction' file
 were -e is option is important when you are suppling multiple instruction.
 
example let a file contain a list of address and cities name in short form
then following:
sed 's/MA/Massachusetts/' list
where list is file containing address list.
In this as you can see that the instruction is   are similar to that you give in ed.
 but you see that you don't have to provide address(not the addresses in list it mean position or line number in the file) to lines as in ed. It simply traverses to all lines one by one. also it will print out the output.

two ways to specify multiple instruction
1st: Using a ';'

like
 sed 's/MA/Massachusatts/ ; s/PA/Pennsylvania/ ' list

2nd: Using -e

sed -e 's/MA/Massachusatts/' -e 's/PA/Pennsylvania/ ' list

or 
3rd: using multiline entery capability

sed '
s/MA/Massachusetts/
s/PA/Pennsylvania/' list

Ok thats it to entering commands

Now how to use script file as an input to sed?
script file can be like this
suppose  sedscr is a script file then
cat sedscr
s/MA/Massachusetts/
s/PA/Pennsylvania/

 and you can use it as

sed -f sedscr list

as sed directly print it to default output or terminal one can rather redirect it and store the modified out put in a file.
as
 sed -f sedscr list > newlist

you can suppress the printing on screen of all the line by using '-n' option
as
sed -n -e 's/MA/Massachusetts/' list
this will not give or display any out put
if you want to view just the modified lines use 'p' as in ed command
as
sed -n -e 's/MA/Massachusetts/p' list

Thats it for sed
look for further learning on :http://www.grymoire.com/Unix/Sed.html

Ed, the command line editor quick lookup

Ed Command line editor.

Ed is a unix command line editing utility which is interesting as it leads to Grep awk and sed commands to be understood better.
Ed is easier to understand and use.This not a tutorial it for quick lookup for me and those who have not used if from long time and need a little revision. :D enjoy.

consider you are want to edit a file with thousands of words.
 open up the terminal

at prompt type

>ed filename.txt 

nothing will be shown except number of characters in the file.
This command works on just a line at a time.
if you type 'p' and press enter

You will see last line to be printed. Because the ed was pointing at last line. 'p' stands for print.

Entering any number will point to that line number.

 Entering 'd' will delete the line ed is pointing.

rather than moving to a line by line number and then editing .you can specify a regular expression as an address to any particular line containing that regular expression.

suppose u want to delete a line having a word 'regular'  in that line.
then

/regular/d
will delete the line having regular. but this will only delete the line it will encounter the first time containing regular expression

suppose if content of file are,

asdfg
wer
asdfg
rew
asdfg
ewret

and we have processing with ed
the following commands

>3
asdfg
 >/asdfg/d
w
 will save the file as

asdfg
wer
asdfg
rew
ewret

deletion will occur to at next asdfg

g/regular/d
will delete all the lines containing 'regular' word

thats about deletion.

substituting text requires 's'

[address]s/pattern/replacement/flag

where a word pattern will be replaced with replacement. flag can be 'g' for global that is if a g is placed then all the patterns in line will be replaced by replacement.
s/regular/complex/g
above command changes all occurrences on the current line.

address should be specified for this command to act upon more than specific line
/regular/s/regular/complex/g

the above line affects the first line that matches the regular(or address specified).
g/regular/s/regular/complex/g


The family of Unix command Grep is derived from global command Ed as: G/re/p.

 One can write script of edit place them in a separate file and directing them as input to the line editor. For instance, if a series of commands were put in a file named ed-script, following command executes the script:
ed test< ed-script

further you can look at:http://unixhelp.ed.ac.uk/CGI/man-cgi?ed

CC2540 UART 0 alt1 setup

CC2540 UART 0 alt1 setup Instead of USB for CC2540 USB Dongle
this is setup for testing if the Uart works or not I have not connected arduino i have taken out chip and just used the board to make a UART bridge to test it with Btools.

I am working about 2 Months on a project that is under a professor. Project is a from NIF(National Innovation Foundation).
It is about same as a recently famous Tile Device (lost found or object locator) I call it object locator.
TI is based on similar technology developed based on Bluetooth that is BLE (Bluetooth Low Energy) or BLE 4.0
TI(texas Instruments ) have developed an SoC based on BLE . I am using CC2540 for similar application to Tile.
First the thing I wanted was to setup Dongle to UART so  Icould connect it to UART of Arduino as to make a independent Device from computer. In the Btool I saw the Communication (Rx/Tx Dump) so I had to first do bridging to a FTDI and setup dongle to uart connected to bridge and then test it to BTOOL.
 I wanted to use the same project HosttestRelease but wanted to use dongle as a network processor to Arduino to easily prototype the device to show it to the NIF.

So I had a PC setup for the Embedded Workbench after I used the software in my laptop(to TI people can the ble stack be used with other open source IDE's??) . In the project HostTestRelease,
In HAL>Target>CC2540USB>Config>hal_board_config.h
Line No 336 will be a #defined HAL_UART_ISR 0
set it to 1
just in next line #defined HAL_UART_USB 1
set it to 0

Now I wanted to not have a UART flow control or CTS and RTS cause I wanted to use it with arduino I don't want to waste pins of arduino's

for switching CTS and RTS pin off
go to NPI>npi.h file in that at line 77
there is a #defined NPI_UART_FC TRUE
setting all this will get your UART 0 to work on Dongle's I/O port's First and second pins P0_2(Rx) & P0_3(Tx)

There is one more setting to set the priority to UART 0 but I am not remembering where , I was something that I had to set to 0x00 to use UART 0 and that thing was not defined in the file it was in ISR's Method and was shown as error while compiling if in case u get it please post it in comments and I hope this post will help others cause I was also having trouble for 2 days because of no proper guidance like that in AVR atmel case ... Thank you all.


Sunday 7 July 2013

Time : the trouble with clocks running


Taking Clocks

whats time in your clock?...is it correct ? well if you have set it from some clock is your source correct?
There is world problem of clocks running different time with in a room.Difference may vary from mins to half hour.People some time do it to be early at there destination or wake up early .I do that too. So how do you get your watch a correct time.
You may think of matching you time to that of clock at railway station or at airplane .But what if your clock is losing time. you will lose it too unknowing. In my collage Profs don't let you sit in lectures if you are late as the clock timings vary in ever-bodies clock .
One day in class of Embedded Hardware Design Prof ask how would you keep all clock in all rooms of collage in sync? Simple solution was to appoint a person with a clock and asking him to reset all clock to the clock in his hand.
This can be a solution that can help in small places and if labor is cheap .Well as it was an embedded System class Prof's target was an answer in embedded System terms.One of boy suggested that all clocks update time from a single server ,but that could raise the price of clock that has ability to connect to Internet.Nobody had a perfect answer.
Many days Past and I was reading about cognitive radios and the cooperative model of communication and was into few of this concepts .Last night, suddenly I saw clocks talking and  in my ears when I was switching of my ears with fingers pushed in. I heard tick tick tick... although later I found out that it was my heart beat I heard of..stupid me. But it did spark me an Idea.
In past there use to be  a clock signal radio station  on a particular frequency when you tune to it you can listen to the sound of clock.
well anyways , What I though was an experiment ,
consider clock A is present and another clock B comes (assuming clocks can talk to each other) to A, asking for time so that it can reset itself .But how should B trust that A is at correct time?
You can go on thinking over this topics - cognitive radios,game theory, swarm intelligence and distributed computing to find some solutions.



Orignally written about 5 months ago. 

Time and Programing issues with it

Time and Issues with it.
I have worked with RTC (and arduino for a real life project )and some programming problems related to time.
I want to keep it noted so next time dont fall in this trouble.
This problems are mostly while comparing two time instances.

> Time I generally denote as hh:mm:ss ,also consider it 24 hours scale.
24th hour is 00:00:00.

> An hour starts as 01:00:00 and end at 01:59:59 thus an Interval is measured this way. Adding time such as adding 2:15 hours to time 1:47 as

new min=(15+47)%60 where % is module and floor of ((15+45)/60) is added to hour as 2+1+floor((15+45)/60).

> subtracting time such as 3:12 minus 2:31 is like
  subtract minutes as
if (12 <31),
 hour -1(i.e 3 - 1)
 and  than min + 60, (i.e 12+60)
and then subtracting minutes as 72-31=41 min and then subtracting hours as 2 - 2 = 0.
that gives 00:41 answer for the subtraction
I will still put others, I know more issues but I don't remembered most of those I dugout my previous code and put those too here soon.

 I was motivated by  Question on time intervals on Amazon Interview street challenge https://amazon.interviewstreet.com/challenges/dashboard/#problem/4fd651016cd04
 

Sunday 27 January 2013

Complete multithreaded Matrix multiply program in C

I am not the author of this code but I have integrated it to while doing assignment I just forgot those links but anyway I said it, its not my code.
Also if you think doing this is good or bad please comment that will be a good feedback so may be I can improve things in future.
//starting of code
#include<stdio.h>   
#include<pthread.h>
#define ARRAY_SIZE 3
       
    typedef int matrix_t[ARRAY_SIZE][ARRAY_SIZE];   
    matrix_t MAm,MBm,MCm;
    /* main: allocates matrix, assigns values, computes the results */   
    typedef struct {   
    int       id;   
    int       size;   
    int       row;   
    int       column;   
    matrix_t  *MA;   
    matrix_t  *MB;   
    matrix_t  *MC;   
    } matrix_work_order_t;   

void mult(int size,             /* size of the matrix */   
              int row,              /* row of result to compute */   
              int column,           /* column of result to compute */   
              matrix_t MA,          /* input matrix */   
              matrix_t MB,          /* input matrix */   
              matrix_t MC) {       /* result matrix */   
       
          int position;   
       
          MC[row][column] = 0;   
          for(position = 0; position < size; position++) {   
                MC[row][column] = MC[row][column] +   
                  ( MA[row][position]  *  MB[position][column] ) ;   
          }   
    }
    /*   
    * Routine to start off a peer thread   
    */   
       
    void peer_mult(matrix_work_order_t *work_orderp)   
    {   
       
    mult(work_orderp->size,   
         work_orderp->row,   
         work_orderp->column,   
         *(work_orderp->MA),   
         *(work_orderp->MB),   
         *(work_orderp->MC));   
       
    free(work_orderp);   
    }   
   
    extern int   
    main(void) {   
        int size = ARRAY_SIZE, row, column;   
          /* Fill in matrix values, currently values are hardwired */
  for (row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
      MAm[row][column] = 1;
    }
  }
  for (row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
      MBm[row][column] = row + column + 1;
    }
  }
  printf("MATRIX MAIN THREAD: The A array is is;\n");
  for(row = 0; row < size; row ++) {
    for (column = 0; column < size; column++) {
      printf("%5d ",MAm[row][column]);
    }
    printf("\n");
  }
  printf("MATRIX MAIN THREAD: The B array is is;\n");
  for(row = 0; row < size; row ++) {
    for (column = 0; column < size; column++) {
      printf("%5d ",MBm[row][column]);
    }
    printf("\n");
    }
       
        matrix_work_order_t *work_orderp;   
        pthread_t peer[size*size];       
        /* Process Matrix, by row, column */   
        int id=0 ;
        for(row = 0; row < size; row++)     {   
          for (column = 0; column < size; column++) {   
               id = column + row*3;   
               work_orderp =(matrix_work_order_t *)malloc(sizeof(matrix_work_order_t));   
               work_orderp->id = id;   
               work_orderp->size = size;   
               work_orderp->row = row;   
               work_orderp->column = column;   
               work_orderp->MA = &MAm;   
               work_orderp->MB = &MBm;   
               work_orderp->MC = &MCm;   
       
               pthread_create(&(peer[id]), NULL, (void *)peer_mult,   
                               (void *)work_orderp);   

          }   
        }   
        int i=0;   
               /* Wait for peers to exit */   
        for (i = 0; i < (size * size); i++) {   
           pthread_join(peer[i], NULL);   
        }       
printf("MATRIX: The resulting matrix C is:\n");   
          for(row = 0; row < size; row ++) {   
            for (column = 0; column < size; column++) {   
            printf("%5d ",MCm[row][column]);   
            }   
          printf("\n");   
          }
        return 0;   
    }

Tuesday 8 January 2013

x86 and linux process kernel Stack

I even had this as doubt until I figured it out and as of I tried to google the solution on net found non so this could help others to find the solution if they have similar doubts.
x86 have few registers to store data so efficient ways are implemented to consume less of available registers, one is to find the current process running .Many RISC arch processors store the address of current process running in a register but for x86 Linux does maps struct thread_info which has task_struct which points to process being running .
I read in book Linux Kernel Development in topic "Storing the process descriptor" page 26 in new edition about this but I did not got it.
I understood that when process block is made in RAM it is allocated to any physical address in RAM and so there is no perfect knowledge of the starting point or so. Thus we can't even say any thing about the stack's address. So I did not got how could masking just 13 bits of address of stack point of kernel process stack gives the address of current_thread_info.
This was wrong as right now I have no knowledge of how paging in done in physical address space. 
But then I tried to find out that how that happens and I had some idea that if it is at a predefined location than be can have such conditions.
I found the good explanation is Understanding linux Kernel in topic 3.2.2.1 "Process descriptor handling" page 4 of chapter 3.
Its simple, 2 pages are assigned to Kernel stack and current_thread_info struct which are aligned to multiple of 2 power 13. And this is the point I was not knowing. Rest all you can understand by diagram fro m the book.

Every process( ) has memory reserved for kernel function when those processes call the system calls .This kernel stack are used.