May 22, 2013

Perl & gnuplot for generating graphs

I am going to present here one small script which I use to generate graphs. With small help of perl (to prepare data from text data containing hex values send from sensing board) and gnuplot.

I'll paste here a subroutine to generate a plot file for gnuplot. The subroutine has only one parameter, the file handle to generate a plot file. There's a multiplot with 3 graphs, 2 columns or only one in some cases. Depends on $outfile_name[3]. If there're no data available, only one column is genereated. This could be easily customize to your needs.

I have tweaked it to generate 2 graphs, one is multiplot 5,1 , another one is 3,1. All you have to do before you call this subroutine, prepare your data.



Here's an example of usage, create plt file,close it and call gnuplot.

generate_plot( *PLT );
close PLT;
`$gnuplot $file_name[6]`; #file_name is mine plt file (graph.plt)

May 16, 2013

OpenOCD with TWRK60 - Kinetis K60 board (tutorial for Windows)

This post will be about openOCD and OSBDM on TWRK60. I'll cover here only in windows system, linux might come later.

To start, download precompiled binaries (everything should be enabled, check a text file inside. What I am interested at the moment are two flags there --enable-osbdm). Or if you prefer to compile it with own flags, please visit official openOCD website.
Download windows precompiled openOCD binaries [freddiechopin.info/en]

Read libusb-1.0 drivers text file, there's link to ZADIG. libusb must be installed if you want to use J-Link or OSBDM. Having said that, I was not able to make it work with libusb filtered version. I received error during initialization: Can't open OSBDM. Therefore I simply replaced libusb (default) driver by WinUSB. This is the problem I have to solve. I simply don't want to replace driver between using CodeWarrior or OpenOCD.
Zadig [sourceforge.net/projects/libwdi]


Create own .cfg file (in my case openocd.cfg) which contains the following text:
source [find ../scripts/interface/osbdm.cfg]
source [find ../scripts/board/twr-k60n512.cfg]
$_TARGETNAME configure -event gdb-attach {
   echo "Halting target"
   halt
}

Open the command line and type:
openocd-0.7.0.exe -f openocd.cfg

The output from the console:
Open On-Chip Debugger 0.7.0 (2013-05-05-10:41)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.sourceforge.net/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'jtag'
srst_only separate srst_gates_jtag srst_open_drain connect_deassert_srst
cortex_m3 reset_config sysresetreq
Info : add flash_bank kinetis pflash.0
Info : add flash_bank kinetis pflash.1
Info : OSBDM has opened
Info : This adapter doesn't support configurable speed
Info : JTAG tap: k60.cpu tap/device found: 0x4ba00477 (mfg: 0x23b, part: 0xba00,
 ver: 0x4)
START...
Info : k60.cpu: hardware has 6 breakpoints, 4 watchpoints
END...

Keep the command line open which keeps the connection alive, open terminal (like Putty), create a new instance (telnet) with an address: localhost on port  4444. This should be the output on the terminal:
Open On-Chip Debugger


To flash our own image, halt the CPU.
>reset halt
JTAG tap: k60.cpu tap/device found: 0x4ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x4)
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00009640 msp: 0x2000fbfc

Ready to flash the S19 image - LED demo (as an example).
> flash write_image erase c:/TWRK60_DEMO.afx.S19
auto erase enabled
Probing flash info for bank 0
Probing flash info for bank 1
no flash bank found for address 1fff0000
wrote 0 bytes from file c:/TWRK60_DEMO.afx.S19 in 0.064003s (0.000 KiB/s)

I am currently having problems to reset the CPU. What is not working currently, it's run command. But if I invoke reset init and resume, it works.

There are many commands which are described in OpenOCD User’s Guide. I'll continue with J-Link and GDB.

Update June 8th:
I posted how to communicate with the tower board and jlink in the eclipse and I published trivial gpio gcc example for TWRK60N512. See my other posts.

References:
Official openOCD site http://openocd.sourceforge.net/

May 11, 2013

Coroutines in C (bookmarks)

I downloaded Contiki for educational purposes, and first thing which got me trapped are protothreads. I have never read anything about coroutines (yes, blame on me not to read Donald Knuth's books - I'll get to those once I empty my bookshelf). 

Coroutines in C [chiark.greenend.org.uk/~sgtatham]
Excellent article to the point, with examples and explanations.

Coroutines in less than 20 lines of standard C [fanf.livejournal.com]
Tony Finch's coroutines which uses setjmp and longjmp for saving the state and jumping to new coroutine.

Protothreads [dunkels.com/adam/pt]
Main page for protothreads, if any questions, answer is there probably. Check examples there, or directly contiki implementation.

Website to buy programming books (or any other)

When I started seriously reading books, not ebooks or just pdfs, I mostly went to amazon.com for checking book prices. I found out it is not always cheapest way to purchase a desired book there. I'll share here list what webpages I check when I look for a book (new or used)

Amazon [amazon.co.uk]
I prefer british version .co.uk because delivery is faster to Europe. I do not want to wait one month. A lot of sellers have their own webpages, but prices are sometimes different, as an example, I bought book on Amazon through BookDepository, which was cheaper with shipping, compare to the price on their own website.

Alibris [alibris.co.uk]
Again british version, .com version exists as well. They are more expensive considering 11 euros for shipping to Europe. I used it only once so far, book got lost, a replacement is on its way. I hope this one arrives to my location.

AbeBooks [abebooks]
I consider this to be USA based, most of sellers I checked, were from US. Many are from India with their international editions. Possible to find good bargain. Shipping varies with sellers, I always compare, sometimes more expensive book is cheaper combined with a shipping.

If you have any other tips, please share !

May 10, 2013

How to report bugs efectively (bookmark)

How to report bugs effectively [www.chiark.greenend.org.uk/~sgtatham]
This essay is a classic ! I did not bookmark it last time I had read it, it's worth to have it always by hand.

I should probably inspire myself and write something similar for our community. A user shares frequently details in a manner: "It does not work, please help me."

No platform, compiler or the line of code or at least function where it crashed or surprisingly returns an unexpected value.




May 5, 2013

The Practice of Programming (book review)

I don't need to introduce authors Brian W. Kernighan or Rob Pike. The authors were one of the reasons why I bought this book. It was published in 1999. Although it's a bit dated, but still relevant to this day. The book consists of 8 chapters. Each chapter is divided into suchapters and many have small paragraphs with different focuses. I'll keep it short not to spoil the book.

1. chapter - Style
key words: names , style consistency, macros, magic numbers, comments.
Be consistent in your code, use indent to show structure, use descriptive names for globals, don't leave comment for obvious code.

2. chapter - Algorithms and Data Structures
key words: searching - linear , binary searching, sorting - Quicksort, O-notation, Trees, Hash tables, Lists
This chapter serves as an introduction to a searching, a sorting and data structures.

3. chapter - Design and Implementation
key words: markov chain algorithm
Implementation of markov chain algorithm in C, C++, Java, Awk and Perl. They measure performance and show differences in the implementations.

4. chapter - Interfaces
key words: comma separated values
An implementation of CVS library in C and C++. What is discussed in this chapter : How to create an interface, error handling, resource management, implementation details.

5. chapter - Debugging
key words: bug, debugger, tracing
How to solve a bug, don't make the same mistake twice, stack trace and many more tips.

6. chapter - Testing
key words: white box testing, black box testing, scaffolds
Test bondaries, pre/post conditions, error returns, test scaffold, test coverage.

7. chapter - Performance
key words: efficiency, profiling
They used spam filter as an example to show how to improve performance. Few versions of spam filter is presented with their performance issues. There are advices how to tune the code.

8. chapter - Portability
key words: standard, undefined behavior, C libraries
Typical problems are presented like signess of char, byte order, shifts, bitfields and many more.

9. chapter - Notation
key words: regular expressions
How data are formatted (an example of creating a packet for sending). Regular expressions with grep, awk, tcl and perl examples.

Each chapters contains Supplementary reading which provides more sources. There are exercises in the end of many sub chapters. 

One addition which is from the last year from Rob Pike: http://www.informit.com/articles/article.aspx?p=1941206 . 
Thinking debugging is much more powerful than line-by-line debugging. I tend most of times to think first before I debug but I need more experience to do it quick and right the first time.

What I enjoyed, was real examples from their experience or others.

References: