Jeffrey's Log

Archives | Subscribe


Older posts

Multitask scheduler for MSP430F5529 launchpad


One of the best ways to gain some good understanding about a concept is by doing experiments rather than reading theory about it. Multitasking in operating system was one such concept for me. In the beginning stages of learning C programming, I used to wonder how operating systems were implemented. At that time I knew that operating systems were written in C language but never knew how.

Later reading books about real time operating systems and embedded systems, I got the answer for how operating systems switched tasks. Its known as “context switching“. Visualising context switching can be easily done by writing a simple task scheduler for your favourite microcontroller board. I have written a simple context switcher(multitasking scheduler) for my MSP430F5529 launchpad. I will explain about it in this post.

What is multitasking, scheduling and context switching ?

Mutlitasking is a method where a CPU does multiple jobs at the same time. If you have programmed microcontrollers in C, you would have written a main() function inside which you put what you want the CPU to do. What ever you write inside it will be executed line by line by the microcontroller. Just imagine you have written a program to blink a LED and also to send data via UART inside a while(1) loop.

//pseudo code
main()
{
  while(1)
  {
    blink_led();
    UART_TXdata();
  }
}

Here the microcontroller will first blink the LED, then it will stop blinking the LED, then it will send data via UART, then stop sending the data, then again it would start blinking the LED… and this process goes on. But what if you want both of these to be executed in parallel i.e blink LED and in parallel send data ? Yes, this is possible. This is way of doing multiple tasks at the same time is called as multitasking. You can read more from here http://en.wikipedia.org/wiki/Computer_multitasking. The multitasking scheduler which I have written here is preemptive.

Actually a CPU is not executing multiple instructions at the same time. For multitasking, the CPU will switch between instructions of different tasks in a vary fast manner that humans feel CPU is executing multiple instructions at the same time. This way of switching between tasks is known as context switching.

Which task should be executed next(i.e to be context switched) is decided by the scheduler. Once the time has reached, the scheduler will trigger a context switch. Scheduler gets support from a timer to trigger the context switching i.e if you want to switch between tasks every 1ms then the timer is configured for 1ms. Every 1ms a interrupt will be triggered which will call the scheduler.

But how to do this ?

A CPU does manipulation on the data stored in its registers. When you write a C program, the C compiler will generate assembly instructions to manipulate data on these registers. In some cases when you write large programs, the available registers wont be enough. In that case, stack is used. Extra data are stored in stack and when required will be moved to registers for the CPU to use. If you want to know how a C code uses the CPU registers and stack, read these links

http://blog.vinu.co.in/2011/09/hello-world-asm-msp430g2231.html

http://harijohnkuriakose.blogspot.in/2010/11/translating-c-constructs-to-msp430.html

So data stored in CPU registers and data stored in stack are very important. If you have multiple task, you need to divide the RAM to those tasks. E.g. If you have 4kB of RAM, you may divide it to 1kB per task. Some RAM is also required(in our case the leftover 1kB) for scheduler, initialisation routines etc.

When we write a C program for our microcontroller, there is a main loop where we write the task for the CPU. Just imagine we have two functions (as I said when I explained about multi tasking) which needs to be executed in parallel. C code that we have written will be executed line by line one after the other. But if we have a scheduler which could do context switching, then we can achieve parallel execution.

As I said before, inside the main() function whatever we write will be considered as a single task. But while implementing scheduler, we will write our scheduler code inside the main() function (and also partially inside timer interrupt service routine which I will explain later). Tasks which have to be multi-tasked should be written as separate functions. In order to context switch at particular intervals, a timer should be configured. In the timer’s interrupt service routine we should write the code to switch to the next task. Before switching to the next task, we must backup the values kept in the CPU registers and stack so that next time when this old task again comes, we need to load back these values into the CPU registers and stack. The function address will be loaded to the program counter so that the CPU will execute that function.

You should read these two chapters from the FreeRTOS which has pictorial representation of context switching

Section 1 – RTOS fundamentals

Section 2 – RTOS Implementation Example

Lets implement our Multitask Scheduler !

MSP430F series uses 20-bit address space. Some other MSP430 series has a 16-bit address. So if you want to migrate it to such architectures, make sure you make necessary – The extra 4bits in addition to the 16bits are stored along with status register(SR) which needs to be corrected. My scheduler is currently designed to run on MSP430F5529 Launchpad. It can handle upto 3 tasks(but we can alter it as required). Tasks will be switched in round robin way. MSP430F5529 has totally 8 kB of RAM out of which each task will get 2kB and pending 2kB is used by the scheduler.

Before doing the scheduling, we need to do certain initialisations. First we need to switch off the watchdog timer. Then we will clock the CPU to its highest frequency so that it runs at its best speed. Then we have to initialise the RAM(I will explain this in the below paragraph) for each task. The watchdog timer will be used to call the scheduler periodically. It needs to be configured in timer mode(otherwise each watchdog interrupt would trigger a CPU reset). The watchdog timer will be automatically enabled when we load our first task for execution. After all these, we are ready to load our first task. For this we have to load the stack pointer(SP) with the first tasks initialised RAM. After this the watchdog timer will be enabled and also the CPU will start executing the first task. In between the interrupt will be triggered when the timer has elapsed. This will call the scheduler which has to first save the current CPU registers, then has decide which task has to be loaded next and then trigger a context switch.

RAM initialisation and context switching are the most trickiest part since you need to know the hardware well. I will first explain about RAM initialisation. As we know for the CPU to execute, there are few registers it requires (i.e R0 to R15). These register should be filled with proper values for attaining code execution.

Below are the CPU register of MSP430F5529 and its uses.

# CPU Register Name Use
1 R0 Program Counter (PC) Holds the address of the next instruction to be executed
2 R1 Pointer Stack (SP) Holds the address of the last value pushed to stack
3 R2 Status Register Can control the CPU and also holds CPU flags
4 R3 Constant Generator Registers (CG1 and CG2) Not our interest. Refer datasheet for more info
5 R4 – R15 General Purpose Registers Used by CPU for data manipulation

Out of these, R0, R1, R2 and R4 to R15 are the most important registers which have to be backed up. Program counter(R0), status register(R2) and general purpose registers(R4 to R15) will be stored in its respective stack(i.e RAM location). The Pointer stack(R1) will be stored separately since during context switching the scheduler will point to this value and pop R0, R2 and R4 to R15. During the RAM initialisation(i.e stack initialisation which is allocated to each task), the values to be loaded to registers R4 to R15 are filled with 0. Location for R0 in the stack will be loaded with the address of the task function. Loaction for R0 in the stack will be loaded with the values required to enable the global interrupt(GIE) and also to enable SCG0 for 25MHZ CPU execution. Reading the source code will give more clarity.

Once the task has started execution, the CPU will automatically increment the program counter(PC) register. So each time when we come back to a task after context switching, the execution is continued from the location where the task had reached. We wont start from the beggining of the task each time. During RAM initialisation, we didn’t store the pointer stack(SP) in the stack along with other registers. This is stored separately. When a task was executing, it would have pushed some values into stack. Stack pointer(SP) register will have that value. While context switching we will save this value so that next time when we come to this task, we will again load this value to stack pointer(SP) register so that the execution is resumed from the place where we stopped.

Enough talk! Show me the code!

I have checked in the code to my github repo. Its an completely open source scheduler. Feel free to use it and learn more. I have made the code self explanatory. Enjoy!

There are three tasks – first task for blinking the red LED at a particular rate, the second for blinking the green LED at a particular rate and last one for reading both the buttons. When you press the button, you could toggle the blinking of both the LED’s. Buttons are not properly detected due to software debouncing issue.

NOTE: If you want to program your MSP430F5529 launchpad using GNU/Linux, read my previous post – Running MSP430F5529 Launchpad using GNU/Linux

Further development:
1) Try to send data via serial port in a task
2) Find a way to compute the stack consumed by each task and send this value via serial port
3) The scheduler is premptive. Try implementing a cooperative scheduler.
4) In cooperative scheduler, also add a feature to find the free time left in a process.

If you want to debug your code, read this – Debugging with msp430-gdb using mspdebug as gdb proxy http://karuppuswamy.com/wordpress/2010/10/14/setup-build-and-debug-environment-for-msp430-mcus-using-mspgcc-and-mspdebug-in-ubuntu-linux/

Posted in: MSP430, operating systems, Projects, tutorials | Tagged under: , , | 10 Comments

Running MSP430F5529 Launchpad using GNU/Linux


I own an MSP430F5529 USB launchpad from Texas Instruments. Cheap in cost, this launchpad is a good resource for developing your USB applications. It has an open source onboard debugger eZ-FET lite.

For developing applications for MSP430, there is already a well maintained C toolchain(gcc-msp430) available. On an Ubuntu/Debian machine, you can install it using the below command
sudo apt-get install gcc-msp430 msp430-libc msp430mcu

For debugging purpose, I am using mspdebug. But I had some hard time in setting up this debugger on my GNU/Linux machine. Due to some driver issues, the default mspdebug package from Ubuntu repository didn’t work. To get it working, there are some extra packages provided by TI to be installed.

Another alternative was to get the pre-compiled mspdebug from Energia. Energia comes with all these issues solved. But if you want to try the hard way, refer this.

Below steps describe how to use mspdebug from Energia
1) Download Energia for GNU/Linux from http://energia.nu/download/
2) Extract the tgz file. I got a folder named energia-0101E0011. The number in the folder name might change for you depending on the energia version you are using.
3) Go into the folder energia-0101E0011/hardware/tools/msp430/bin/ using the cd command.
3) Copy libmsp430.so to /usr/lib/
4) Update the firmware of the debugger using sudo ./mspdebug tilib --allow-fw-update
5) Now you can run mspdebug using the following command sudo ./mspdebug tilib

If you were successful, you will get the mspdebug console
(mspdebug)

Now lets write a small code and see if we can flash it to the launchpad.

Save the below code as blink.c

#include <msp430.h>

main()
{
  unsigned int i = 0;
  P1DIR = 1;

  while(1)
  {
    P1OUT = 1;
    for (i=0; i < 65535; i++);
    P1OUT = 0;
    for (i=0; i < 65535; i++);
  }
}

Lets compile this software using the below command
msp430-gcc -mmcu=msp430f5529 -mdisable-watchdog blink.c

We need to flash the output of the above compilation to the launchpad. Launch mspdebug using sudo ./mspdebug tilib. Then run
(mspdebug) prog a.out
Note: Since mspdebug is inside the energia folder and this blink.c file is at another location, when using the prog command in mspdebug, you have to give the path to the a.out file i.e
(mspdebug) prog /path/to/the/file/a.out

Now the LED on the launchpad will start to blink!
MSP430F5529 blinking LED

Posted in: Electronics, MSP430 | Tagged under: , | 2 Comments

Designing a D flip-flop using Migen


If you haven’t installed Migen or don’t know about Migen, read my previous blog post Installing Migen – the Python based hardware description language

D flip-flops

D flip-flop

A D flip-flop spits the data out which is kept in the data input pin when the clock is applied(in positive edge or negative edge). The D flip-flop that we are going to design has the input pin D, output pins Q and Qi(inverted of Q). It also has a clock clock input and a reset pin which makes the flip-flop synchronous.

The D flip-flop shown over here is a positive edge triggered flip-flop. The truth table represents the mapping between the input pins to the output pins based on the reset pin and clock pin state. Since its a positive edge triggered flip-flop, the clock pin is shown as a transition from low to high.

Just for a reference, this is the datasheet of a D flip-flop IC.

A D flip-flop using Migen

Create a file named Dff.py and add this code to it.

from migen.fhdl.std import *

class Dflipflop(Module):
  def __init__(self, D, Q, Qi):
    self.sync += Q.eq(D)
    self.comb += Qi.eq(~Q)

Digital circuits can be divided into synchronous(i.e works based on the clock input) and asynchronous(independent of clock) circuits. Asynchronous circuits are also known as combinational logic. The Python syntax used for describing the D flip-flop can be more clearly understood by reading the Migen user guide. So I won’t be explaining here the syntax used. Only describing the synchronous and combinational(asynchronous) statements are in my scope.

self.sync += Q.eq(D)
Q.eq(D) equates/copies the content of D to Q which are the input and output of our flip-flop. This is then assigned as a synchronous using self.sync +=. As mentioned before, a D flip-flop copies the data from the input pin to the output pin in synchronous to the clock transition. Hence this circuit is a synchronous circuit.

self.comb += Qi.eq(~Q)
Qi.eq(~Q) is used to invert Q and copy to Qi. This is assigned as a combinational logic. This means Qi is independent of the clock. But out flip-flop is a synchronous. This doesn’t means that Qi will change asynchronously since the input of Qi is from a synchronous logic Q.

Testing the D flip-flop

Update your Dff.py with the below code.

from migen.fhdl.std import *
from migen.fhdl import verilog
from migen.sim.generic import Simulator, TopLevel
from random import randrange

class Dflipflop(Module):
  def __init__(self, D, Q, Qi):
    self.sync += Q.eq(D)
    self.comb += Qi.eq(~Q)

  def do_simulation(self,s):
    s.wr(D,randrange(2))

#Simulation and verilog conversion
D  = Signal()
Q  = Signal()
Qi = Signal()

#print(verilog.convert(Dflipflop(D, Q, Qi), ios={D,Q,Qi}))

sim = Simulator(Dflipflop(D,Q,Qi), TopLevel("Dff.vcd"))
sim.run(100)

Execute this python script using the below command. This will generate a Dff.vcd which contains the test result. The vcd file can be viewed using GTKWave tool.
python3 Dff.py
gtkwave Dff.vcd

GTKWave-Dff.png

As I mentioned before, read the official Migen user guide and tutorial to understand what the code means.

To test the D flip-flop, we need to check if the input (D) is copied to the output(Q and also Qi). For this the input D should be fed with values(0 or 1). We can use the module random in Python to generate some random values.

s.wr(D,randrange(2))
The function randrange(2) generates a random number – either 0 or 1. s.wr writes the random number to D. The write happens only after the positive edge of the clock cycle.

Verilog equivalent of the D flip-flop

As I mentioned in my previous blog post (Installing Migen – the Python based hardware description language), Migen converts the Python code to equivalent Verilog code. Since the FPGA tools which we currently have only understands only Verilog/VHDL, this is required. The generated Verilog code can be loaded to your FPGA vendor’s software and verified on real hardware. Mibuild(a tool inside Migen) supports few FPGA vendors for which you don’t have to manually paste your Verilog code to the FPGA vendor’s software. Mibuild will do it for you(by accessing vendor tools via command line) and even it can load the bit stream file to FPGA. Usage of Mibuild will be explained later.

Uncomment the line in your Dff.py file and run the python code again. This line will print the Verilog equivalent code of our D flip-flop.
print(verilog.convert(Dflipflop(D, Q, Qi), ios={D,Q,Qi}))

Below is the Verilog output.

/* Machine-generated using Migen */
module top(
        input D,
        output reg Q,
        output Qi,
        input sys_clk,
        input sys_rst
);


// synthesis translate_off
reg dummy_s;
initial dummy_s <= 1'd0;
// synthesis translate_on
assign Qi = (~Q);

always @(posedge sys_clk) begin
        if (sys_rst) begin
                Q <= 1'd0;
        end else begin
                Q <= D;
        end
end

endmodule

Another way of testing D flip-flops: A frequency divider

Note: Below shown is not the right way to do a frequency division. This is just an example to show Migen's testing capabilities.

Dff-FreqDivider

A D filp-flop can be used as a frequency divider (frequency / 2). Read this for a more clear explanation. Make a connection from Qi to D to divide the frequency(fed to the clock pin) by 2. Connecting a wire between pins is very easy in Migen - just use the same Signal() for both the pins.

Create a file named Dff_Divider.py and add the below code.

from migen.fhdl.std import *
from migen.fhdl import verilog
from migen.sim.generic import Simulator, TopLevel

class Dflipflop(Module):
  def __init__(self, D, Q, Qi):
    self.sync += Q.eq(D)
    self.comb += Qi.eq(~Q)

#Simulation and verilog conversion part
Connect = Signal()
Q = Signal()

sim = Simulator(Dflipflop(Connect,Q,Connect), TopLevel("Dff_Divider.vcd"))
sim.run(100)
Posted in: Electronics, FPGA, GNU/Linux | Tagged under: , , , | 5 Comments

Installing Migen – the Python based hardware description language


Migen is a Python tool which helps you to describe hardware(like Verilog and VHDL) in Python langauge. Like a C compiler which converts C language to assembly, Migen converts Python code to Verilog/VHDL statements. Migen helps in describing hardware in a more Object-oriented manner which would be hard to realise in Verilog/VHDL. A free and open source project (https://github.com/m-labs/migen), Migen is also used for designing the SoC (MiSoc) of an open source hardware project Milkymist One.

Migen toolset contains the Migen itself, the Mibuild and the simulator. Migen converts the Python code to Verilog/VHDL. Mibuild is for converting the Verilog/VHDL code to bitstream file and transfering to the FPGA. If your FPGA is not supported by Mibuild, you can add support for it or you can paste your Verilog/VHDL code to your FPGA tool. The simulator just integrates Migen with Icarus Verilog for simulation. Simulation requires a test bench which is also written in Python. I will explain here how to install Migen and the simulator. Installing Mibuild will be done later(once I get my FPGA board).

Though there exist a tutorial about installing and using Migen, I faced some problems while installing in my Ubuntu machine which was not as per the tutorial. After reading this post, I would recommend to read the official tutorial from the Migen page. This will provide some more clarity. Also make sure to read this post complete before proceeding on your machine.

1) Make sure you have installed Python3, setuptool and git. On an Ubuntu machine this can be done using the below command
sudo apt-get install git python3 python3-setuptools

2) Get the Migen tool to your machine
git clone https://github.com/m-labs/migen.git

3) Move into Migen folder and install
cd migen
python3 setup.py install

Note: There is an option to use Migen – without installation! Just you need the project in your machine. Show the path to Python so that next time when you import Migen modules, Python will search those paths. For this you don’t need the python3-setuptools mentioned in Step 1. Clone the project to the machine using Step 2 and then run export PYTHONPATH=‘pwd‘/migen

Now the simulator installation. Once you are inside the Migen folder(cd migen), move into the vpi folder using cd vpi. We need to run the make file inside the vpi folder. But before that there are some more things to be done.

1) Install Icarus Verilog which is the actual simulator. Also the simulator generates a wave dump of the simulation which can be viewed using GTKWave.
sudo apt-get install verilog gtkwave

2) Run the following commands inside the vpi folder to install the Migen simulator.
make
sudo make install

You might get an error while running sudo make install. In my machine the error was
install: accessing ‘/usr/lib/ivl’: No such file or directory

This might be beacuse the path were Icarus Verilog is installed might be at a different location. A peak into the Makefile and also to the error can give you some hints on what to search for. I need to find where the ivl folder is. For that I used the find command find /usr/ -name ivl\*. I have to pass the new found path (/usr/lib/i386-linux-gnu/ivl) to the make install command. Run the following to install

sudo make install INSTDIR=/usr/lib/i386-linux-gnu/ivl/

Thats it! You have successfully installed Migen and its simulator.

There is another tutorial about Migen. Read it – Designing a D flip-flop using Migen

Posted in: Electronics, FPGA | Tagged under: , , | 3 Comments

Recovering from wp-login.php attack


Recently I was not able to login to my wordpress account. If I visit my wp-login.php page, I was getting a 403 error. Initially I thought it was problem with my wordpress installation but it was not. The error was because of a brute force attack on the wordpress login pages. My server was blocking access to this page. Lot of people are facing this problem. So I thought of blogging. There are plugins to block this attack. But they can only be installed and activated once you are able to login.

Below is the solution for the wp-login.php issue which I found from internet. My initial target was to login. The quickest solution was to rename wp-login.php to a new file name and also replace with the new file name in the renamed wp-login.php file. In the following link, you will get the details how to do http://wordpress.org/support/topic/wp-loginphp-change-to-your-custom-url. This is not a recommended method but a quick solution to get a login.

Once you login, you can search for wordpress security plugins. Some restricts the number of login, some hides the login page. Search and install which one you found better. I am using Better WP Security. Once you enable your security plugin, I would suggest to revert back to file name wp-login.php and also replace inside the php file.

Happy blogging!

Posted in: Blog, Review | Tagged under: , , , | 2 Comments


Older posts