June 25, 2013

MBED and other freedom boards

This is just a small update about my activity lately. I have been busy with mbed and porting new target into it. I decided to port at least one more freedom board. First choice was a board FRDM-KL05Z.

I started few days ago, the learning curve is quite fast in the beginning. I'll try to summarize my findings here in another post. The process is still ongoing, because I am not certain if I ported everything correctly, therefore I have to run all tests which are available in mbed to be certain that the target it's ready to be published.

Does any of you want any other freedom board to be part of mbed? Or do you have any problems with freedom boards? It's quite astonishing platform, but still seems to be fresh new, not much projects have been on the internet, or at least I have not searched thoroughly :-)

June 15, 2013

Kinetis L header files

I needed CMSIS header file for the Freedom board FRDM-KL05Z. I have spent at least half hour searching. Once I found it, I would like to share where you can find other headers. If you know any place where are all those cmsis headers available, leave a comment.
I started with KEIL webpage, there are published many header files for kinetis K or kinetis L. But no luck with KL05. Therefore I kept digging, until I found it in the sample code for KL25 (really? KL25 demo code contains other MCU header files).

Kinetis L Sample Code (KL25) [www.freescale.com]
This package is in the revision 6 at the moment. It contains the following CMSIS header files
  • MKL04
  • MKL05
  • MKL14
  • MKL15
  • MKL24
  • MKL25

CMSIS KL26 header file [www.keil.com]
There are more headers, just filter a search on google.

Once I find all possible CMSIS headers for Kinetis L, I'll publish them on github.

June 9, 2013

Kinetis K GCC startup

I have pushed "lightweight" gcc demo to my github repository. A generated CodeWarrior project for Kinetis K with GCC compiler contains a lot of "bloat" in my opinion therefore I decided to write trivial startup without including any libraries (only stdin included for C99 types). C++ support is not present, neither RAM target.

It contains a GPIO demo for the TWRK60N512 at the moment. The demo instantly toogles LED1 on the tower board and LED2 toogles only when the button SW1 is pressed.

The demo contains following files:
  • common/common.h - including purpose
  • cpu/headers/derivative.h - include cpu specific header
  • cpu/headers/MK60DZ10.h - freescale's header file
  • cpu/startup_k60.c - vectors plus reset handler
  • cpu/system_k60.c - system init function which inicializes CPU (clocks - 96MHz). This needs to be edited to reflect desired cpu clock freq
  • cpu/system_k60.h
  • k60n512_flash.ld - GNU linker for K60
  • main.h
  • main.c
  • Makefile
As could be seen, there are no drivers included. The demo itself only contains necessary files to initialize CPU and jump to the main function.

This example could be used with the tutorial which I wrote previously, openOCD with the kinetis K60. I used it as the test project.

Kinetis Kxx gcc startup on github : GPIO gcc Kinetis K (K60) [www.github.com/0xc0170/]

Update1: I added K20D72M board

June 2, 2013

GCC ARM Embedded + Eclipse on Kinetis with JLink

I have been writing my own start-up and a linker command file for K60 Tower board. Unfortunately, I am still having an unidentified problem with OSJTAG, I  had decided to use jlink with eclipse. I don't use yagarto tool chain, decided to go with GCC ARM embedded.
 There's been many resources on the internet, I'll post which helped me to set it correctly.

Getting Started with the ARM GCC Compiler on Windows PART 1 [thehackerworkshop.com]
Guide how to set eclipse development (GCC ARM, make, eclipse). This guide (Part 1 plus part 2) is more detailed than the original one published on emb4fun.

You can skip few steps in PART 2, right to the Step 5 if you are not using the ST discovery board. This is the most important one, once you set everything, add files, you should be able to compile and link your project files.

This is a highlight what you should look for:
  • C/C++ Build - External builder , build command - make (or set PATH correctly)
  • C/C++ Build - Discovery options - set arm-none-eabi-
  • In linker options inside Tool Settings - there's a flag where you type a path to a linker command file (an example : -T ./../Sources/PK60N512_flash.ld)
  • In compiler options inside Tool Settings - there's other flags option where you type at least these two flags -mcpu=cortex-m4 -mthumb (same should apply for assembler flags)

The last step before debugging, is to set gdb commands inside a created debug configuration (click 2x on the GDB hardware debugging to create a new one). This are taken from the example shared on yagarto webpage, targeted for K40. They are valid for jlink.

These are initialization commands:
target remote localhost:2331
monitor flash device = MK60DN512xxx10
monitor flash download = 1
monitor flash breakpoints = 1
monitor clrbp
monitor endian little
monitor speed 1000
monitor reset
monitor sleep 100
monitor speed auto
monitor writeu32 0xE000ED08 = 0x00000000

These are run commands:
monitor reg r13 = (0x00000000)
monitor reg pc = (0x00000004)
break main
continue

I did not want to repeat what you can find online, but if there's anything unclear, not able to debug your application using jlink on any Tower from Kinetis line, let me know and I can expand this into a small tutorial ;)

I am planning to publish a small gcc application for some tower boards, or perhaps all, depends on how much time I'll have these days ;) Because when you look into code what CodeWarrior generates with gcc, you marvel how much lines that code contains, most of it are unnecessary.

Update June 9th: a demo app for TWRK60N512 is published.

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 !