MSP430debugger

Launchpad is an easy to use development tool for msp430 family. The launchpad development board is MSP-EXP430G2. This board socket supports upto 20pins. It has a mini USB cable for connecting to PC. it has 2 programmable LEDs, 1 power LED and 2 programmable buttons. it has a 2 KB Flash memory for machine codes starting from 0xf800 and a 128B RAM for datas starting from 0x200h.
MSP430 is a microcontroller for low power management(msp430Watch out this video). The msp430 was connected to my usb port as it has the usb support. For confirming that the device was connected just type ‘dmesg’ in the terminal. There should be some debugging interface inmicrocontroller and msp430 provides wih 2 such interfaces. One is JTAG(Joint Test Architecture Group) which is inbuilt in almost all the modern microcntrollers and a 2 wire debugging interface known as Spy-Bi-Wire.

MSPDebug is a free debugger for use with MSP430 MCUs.It can be used as a proxy for gdb or as an independent debugger with support for programming, disassembly and reverse engineering. As there is no kernel support for msp430, libusb(‘libusb-dev’-package name) was installed . This will help in accessing the usb in userspace itself. Sometimes you need to install an additional package called ‘libreadline’. now download and make the mspdebug by typing the commands
make
make install(for which you need to be a super user).

Now for testing mspdebug just type mspdebug int he terminal. You should be a superuser to perform the following:

so some kind of drivers need to be specified.

Being a usb connection we can use rf2500 as the device specification. Now when this command ‘mspdebug rf2500’ the initialization message can be seen as follows:

On reading this initialization message it can be seen that device : MSP430F2013 is specified. If you are not really using this series you may get confused here as I was while using a MSP430G2231. But not to worry as the MSP430 series have almost the same memory layout. After initialization a set of available commands can also be seen for debugging. It should be noted that no code is running at this point of time. Before doing this one should be aware of he memory layout of the device.

MSP430 has RAM (starting from 0x200h) of 128 B, flash memory of 2KB. 16 registers (R0-R15) of which R0-R3 are Special Function Registers.
———————R0 represent PC
———————R1 represent SP
———————R2 represent SR/CG
———————R3 represent CG
RAM stores the data and flash memory stores the machine code. The commands usually used in mspdebug are as follows:

dis – for disassembling
erase – flash memory ges erased. The erase state of flash memory is 1.
regs – gives the register values.
help – for displaying the available commands.

MSPdebug is a low level tool for debugging. It doesn’t have any any awareness about higher level languages like C.

Some simple synchronisation patterns

Synchronization refers to relationship among different events. For example: an event1 should occur before event2. In computer programming we often come across synchronization constraints. There are different synchronization models or patterns. In the simplest model, execution takes place one after the other in sequence. Here synchronization is trivial. But the usage of multiple processors or multiple threads make the synchronization difficult.
Semaphores is a mechanism used to solve these synchronization problems. Semaphore has 3 fundumental operations:

———sem_init() to initialize the semaphore variable.

———sem_post(), which atomically increment the semaphore variable and wakes up a thread which went to sleep.

——–sem_wait(), which atomically decrements the semaphore variable and if semaphore variable is zero thread will sleep.

There are different synchronization patterns. Some of them are the following:
1. Rendezvous :

That is 2 threads at a pt of execution can continue only when both of the threads have arrived.

This solves the problem.

2. Mutex: Mutex is another pattern which locks the thread at the time it deals with a shared variable.

3. Multiplex : This pattern is like the flow of water through a pipe. The pipe has a specific capacity which it can hold at a time. Likewise a semaphore is initialized with the number of threads that can run in parallel at a time.

Similarly there are many synchronization patterns like queue, barriers.

Multithreading

Parallel processing is the ability to carry out multiple operations or task at the same time(our brain is a good example for parallel processing). A simple example from daily life is the manupilation of a Queue in front of a ticket counter. A single person can handle all the 100 persons in this ‘Q’ one by one. or else for eg: 5 persons can give tickets to these 100 persons. Both these way the ‘Q’ can be managed but which one is better? Obviously the 2nd one. Thus its clear that parallel processing makes the task to be done faster than the normal serial processing ie throughput can be increased.

Multithreading is a model for parallel programming. The different parts of a program that can be run in parallel are called threads. An OS has the ability to run different threads simultaneously. Its not a simple task as it is said. The programmer must carefully design the program in such a way that all the threads can run at the same time without interfering with each other.

Pthreads(POSIX threads), are POSIX(“Portable Operating System Interface [for Unix]) standard for threads. This is implemented using the thread library and a header pthread.h. Now look at the following example.

The function which forms the thread should accept an argument of the type void * and return a value of the type void *. pthread_create is a function to create the threads whose 1st parameter is an address of the variable of type pthread_t, 2nd parameter is taken as 0, 3rd parameter is the function name and 4th parameter is the argument to be passed to the function(default value zero). When this program is run it can be seen that it prints an output pattern as following:
thread 1:1
thread 1:2
thread 1:3
….. //for sometime the thread1 was executed and later switched to thread 2
thread 2:400
thread 2:401
….. // again the thread1 is executed
thread 1:1400
………
The threads are clones which share the same global address space of the parent function. From the practical point of view the problem arises from the fact that the sequence of execution and the time of execution is unknown to the programmer. When a thread is called there is no guarantee that the thread breaks after completing the function. It may break at any point in between the function. This problem becomes more complicated when the program deals with global variables.

Memoization & its implementation in python

Memoization is a technique used for optimizing the computer programs. The idea behind this is to avoid the repetitive functions call for inputs that were previously processed. The term was coined by Donald Michie. Here the results for each value is remembered and the next time when the same input is given is given to the function the remembered results are returned rather than recalculating it. Memoization lowers a function’s time cost.

In python it is implemented using the Dictionaries(Associative arrays) as follows:

Here the fibinocci series is given by the function fib(n). Now its clear from the function that for each term upto nth term for the same input the function should be called several times. for example
fib(n)—–> fib(n-1)+fib(n-2)

fib(n-1)—> fib(n-2)+fib(n-3)

Here fib(n-2) won’t be called a 2nd time but the value is taken directly from the dictionary mem.
IncPy(Incremental Python) interpreter is an enhanced python interpreter that does automatic memoization for python.

GDB

Gnu debugger is a powerful debugging tool. It helps to find the bugs by examining the state of the program by viewing the code, values of variables and many more…..
1. use -g option while compiling
     cc -g filename
2. now for debugging type in the prompt
   gdb ./a.out
file along with gdb should be an executable one. This will enter to the gdb prompt.

3. now in gdb prompt type “load ./a.out” to load the program. “run ” is a command to run the program. we can break at some specific line number. This is done by setting certain breakpoints,
   break linenumber
On running the program, the program automatically stops at this break point and we can examine the state of the program. Several breakpoints can be set for a single program. A command named ‘watch’ to watch a variable and break whenever the variable changes its value.
‘continue’ is used to move back to your program from the last point where it stopped for checking the program status.
Type ctrl-D to exit gdb.

4. Now there are some commands useful for examining the state of the program.
   print variable
This will print you the value of the variable at the point where the program stopped the last time. ‘list’ is another useful command. Simply typing list will show the 10 lines of the code. ‘backtrace’ is another command for looking at the stack of the program. backtrace will list out all the called functions that have not yet returned.

let us sow the debugging of a simple program:
   &nbsp gdb ./a.out
Reading symbols from /home/remya/oct19/a.out…done.
(gdb) list
1 #include
2 main()
3 {
4 int i, j, k, r;
5
6 scanf(“%d%d”, &i, &j);
7
8 k = i + j + 10;
9
10 r = i + j + 30;
(gdb) break 3
Breakpoint 1 at 0x804844d: file opt2.c, line 3.
(gdb) run
Starting program: /home/remya/oct19/a.out

Breakpoint 1, main () at opt2.c:6    //the pgm broke at the breakpoint set earlier
6 scanf(“%d%d”, &i, &j);

Kernel

When a computer is first powered on, it does not have an operating system in ROM or RAM. Modern OS and application program codes  are stored in Non-volatile data storage devices. The computer must initially execute a small program stored in ROM to access the nonvolatile devices from which the operating system programs and data are loaded into RAM.  The small program that starts this sequence of loading into RAM, is known as a boot loader. The small program is most often not itself an operating system, but only a second-stage boot loader, such as GRUB or BOOTMGR which loads the operating system.

Kernel is the heart of an OS. It connects the application level and the actual data processing level. The core functionalities of kernel are:

–  Memory Management

–  Scheduling.

There are two types of kernels: Monolithic kernel and Micro kernel.

If there is only a single executable file for implementing the core functions of the kernel along with other functions of OS like networking, file systems, device drivers etc, then it forms a Monolithic kernel. Linux is an example for monolithic kernel. Whereas a micro kernel provide the minimum functionalities(core functionalities) to build an OS.
The kernel programs are invoked by user program through system calls .
System calls provide the interface between a process and the operating system. There are many system calls. some of them are the following:

1. fork() – It creates a new process from an existing process. The new process is called the child process, and the existing process is called the parent. Each process will have many attributes associated with it, like:

#each process will have a unique numeric identifier associated with it, referred to as its process identification number, or PID

# each process also has a reference to the PID of its parent, i.e. the parent process id, or PPID

# each process will have priorities associated with it, i.e. the priority of order of execution

2. exit() – This function will terminate the current process.

3. wait() – This system call suspends execution of the calling process until one of its children terminates.

Similarly many more system call exist.