Friday, November 21, 2008

Codeblocks free C++ IDE.

Codeblocks is a free C++ IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable. Built around a plugin framework, Code::Blocks can be extended with plugins. It even includes a plugin wizard so you can create your own plugins! (Free SDK downloaded separately)

Feature

Highlights:
  • Open Source! GPL2, no hidden costs.
  • Cross-platform. Runs on Linux or Windows (uses wxWidgets).
  • Made in GNU C++. No interpreted languages or proprietary libs needed.
  • Comes in two presentations: Standalone, and MinGW bundle
  • Devpack support (optional)
  • Extensible through plugins (SDK available in the downloads section)

Compiler-related features:
  • Multiple compiler support: - GCC (MingW / Linux GCC) - MSVC++ - Digital Mars - Borland C++ 5.5 - Open Watcom
  • Compiles directly or with makefiles
  • Predefined project templates
  • Custom template support
  • Uses XML format for project files.
  • Multi-target projects
  • Workspaces support
  • Imports MSVC projects and workspaces
  • Imports Dev-C++ projects
  • Integrates with GDB for debugging

Interface Features:
  • Syntax highlighting, customizable and extensible
  • Code folding for C++ and XML files.
  • Tabbed interface
  • Code completion plugin
  • Class Browser
  • Smart indent
  • One-key swap between .h and .c/.cpp files
  • Open files list for quick switching between files (optional)
  • External customizable "Tools"
  • To-do list management with different users

Codeblocks setup for windows you can download codeblocks from codeblocks.org or codeblocks nightly build (recommended) or Codeblocks V1.0RC2 (not recommended) or download from below link.

Codeblocks : nightly build
CB_20070610_rev4084_win32.7z
mingwm10.7z
wxmsw28u_gcc_cb_wx284.7z

wxWidgets GUI toolkit
wxMSW-2.8.4-Setup.exe

MinGW GCC compiler
binutils-2.15.91-20040904-1.tar.gz
gcc-core-3.4.2-20040916-1.tar.gz
gcc-g++-3.4.2-20040916-1.tar.gz
gdb-5.2.1-1.exe
mingw32-make-3.80.0-3.exe
mingw-runtime-3.9.tar.gz
mingw-utils-0.3.tar.gz
w32api-3.9.tar.gz

Extract files
Codeblocks:
Extract CB_20070610_rev4084_win32.7z, mingwm10.7z, wxmsw28u_gcc_cb_wx284.7z to codeblocks's folder for example C:\codeblocks\
wxWidgets:
Install wxWidgets by using wxWidgets's installer (I'v installed to C:\wxWidgets-2.8.4)
MinGW GCC compiler:
Extract all files to MinGW's folder for example C:\MinGW\ After successful installation of all files, make sure you have the following directories included in your PATH environment variable: * \bin; * \mingw32\bin; (You can add them by right-clicking the My Computer icon on your desktop and selecting Properties. Then select the Advanced tab and choose Environment Variables.)



Building wxWidgets

Now it is time to compile wxWidgets, open the command promt and change to the wxWidgets directory:

cd \build\msw

NOTE: The following instructions do not work with MSYS, or other forms of Unix on Windows.

Now the first thing to do is to clean up the source:

mingw32-make -f makefile.gcc MONOLITHIC=1 SHARED=1 UNICODE=1 BUILD=release clean

NOTE: Make sure you use exactly the same options for this step and for the build step below. The clean target uses these variables and only cleans the specified version of the generated object and library files (so it will actually not clean the intended files if these variables are not identical).

NOTE: If you still have problems with compiling after cleaning, you could try to manually remove the gcc_msw directory under the build\msw directory. This directory is the place where the object files are stored.

When everything is clean you can start actually compiling wxWidgets:

mingw32-make -f makefile.gcc MONOLITHIC=1 SHARED=1 UNICODE=0 BUILD=release

NOTE: These options are the officially supported Code::Blocks options. If you want another version of wxWidgets (debug or non-unicode) you still have to make sure you build a MONOLITHIC version (one big DLL) for linking Code::Blocks against.

Now you can get a cup of coffee or do something else because this step takes some time. Make sure there was no compilation error before you continue.

Build for debug.

mingw32-make -f makefile.gcc MONOLITHIC=1 SHARED=1 UNICODE=0 BUILD=debug

When build is completed, copy wxmsw28_gcc_custom.dll, wxmsw28d_gcc_custom.dll from C:\wxWidgets-2.8.4\lib\gcc_dll to C:\WINDOWS\system32

Run codeblocks by double click codeblocks.exe (C:\Codeblocks\codeblocks.exe)



Create new project, File->New->Project then select wxWidgets project and click GO



Click Next to continue.



Select wxWidgets 2.8.x then click Next.



Fill Project title for example HelloWorld.



Fill Author detail.



Prefer GUI builder use select wxSmith, Application type use Dialog based.



Enter valid wxWidgets's folder.



Select GNU GCC COmpiler and checked Create debug, Create release.



Checked as below figure.



New project wizzard is completed.



Configuration new project by the following step.
  • Setup Global variables by select Setting->Global variables as below figure.



  • Setup Build option by select Project->Build option, select Debug in left pane, click on Linker settings tab and click on Add button to add wxWidgets library for debugging, example C:\wxWidgets-2.8.4\lib\gcc_dll\libwxmsw28d.a



  • Click on Search directorys tab, select Compiler tab and click on Add to add header's folder for compilation, for example C:\wxWidgets-2.8.4\lib\gcc_dll\mswd



  • Click on Search directorys select Linker tab and click on Add button to add linker's file, for example C:\wxWidgets-2.8.4\lib\gcc_dll\



  • Build option for Release diference from Debug are
    - change libwxmsw28d.a to libwxmsw28.a
    - change C:\wxWidgets-2.8.4\lib\gcc_dll\mswd to C:\wxWidgets-2.8.4\lib\gcc_dll\msw


After setup Build option is completed you can start build your project by select Build->Build or press Ctrl+F9, if error has occured please check configuration again.



Run your application by select Build->Run or press Ctrl+F10, you'll see below dialog.



Congratulation you've finished codeblocks setup.
*** Note ***
if you've install WINAVR please remove WINAVR paths from system' path before compile wxWidgets. If you've any question post your question on forum.


AVRnet CPanel built from codeblocks.

AVRnet : IP, ICMP, TCP, UDP checksum calculation

I'll explain about IP, ICMP, TCP checksum calculation on AVRnet's source code and use software_checksum function for reference to this article.

Code:
WORD software_checksum(BYTE *rxtx_buffer, WORD len, DWORD sum)
{
// build the sum of 16bit words
while(len>1)
{
sum += 0xFFFF & (*rxtx_buffer<<8|*(rxtx_buffer+1));
rxtx_buffer+=2;
len-=2;
}
// if there is a byte left then add it (padded with zero)
if (len)
{
sum += (0xFF & *rxtx_buffer)<<8;
}
// now calculate the sum over the bytes in the sum
// until the result is only 16bit long
while (sum>>16)
{
sum = (sum & 0xFFFF)+(sum >> 16);
}
// build 1's complement:
return( (WORD) sum ^ 0xFFFF);
}
Function arguments
  • BYTE *rxtx_buffer is a pointer, it's point to receive/transmit buffer at begin of data for checksum calculation, example &rxtx_buffer[ IP_P ]
  • WORD len is a length (in bytes) of data for checksum calculation, example sizeof(IP_HEADER)
  • DWORD sum is an initial checksum value, this argument for TCP's checksum only for IP and ICMP it's should be zero
Return value
  • This function will be return a 16-bits checksum value
How does function work?
Function will be calculate sum of 16-bits data until a len variable less than 2-bytes.

Code:
// build the sum of 16bit words
while(len>1)
{
sum += 0xFFFF & (*rxtx_buffer<<8|*(rxtx_buffer+1));
rxtx_buffer+=2;
len-=2;
}


If a len variable is an odd number, add zero to low byte.

Code:
// if there is a byte left then add it (padded with zero)
if (len)
{
sum += (0xFF & *rxtx_buffer)<<8;
}


If length of the result more than 16-bits, try to sum the result in 16-bits only.

Code:
// now calculate the sum over the bytes in the sum
// until the result is only 16bit long
while (sum>>16)
{
sum = (sum & 0xFFFF)+(sum >> 16);
}


IP checksum is the 16-bits one's complement checksum of the IP header and IP options, a checksum filed must be cleared before calculate new checksum.

Code:
ตัวอย่างการคำนวณค่า checksum ของ IP Header.



45 00
+ 00 34 = 4534
+ 48 18 = 8d4c
+ 40 00 = cd4c
+ 40 06 = 10d52
+ 00 00 = 10d52
+ 0a 01 = 11753
+ 01 4c = 1189f
+ 0a 01 = 122a0
+ 01 01 = 123a1

23a1 + 0001 = 23a2

1's complement of 23a2 = dc5d




ICMP checksum is the 16-bit one's complement of the sum of the ICMP message starting with the Type field. The checksum field should be cleared to zero before generating the checksum.



TCP checksum is the 16-bits checksum on the tcp header (including, of course, any tcp options that are present), data, and a 96 bit pseudo header made up of:



Protocol and tcp length will be pass to function by DWORD sum argument. The checksum field is the 16 bit one's complement of the sum of all 16 bit words in the header. For purposes of computing the checksum, the value of the checksum field is zero." (At least, according to rfc 791).



UDP checksum is the 16-bits one's complement of sum of a pseudo header of information from the IP header, the UDP header, and the data, padded as needed with zero bytes at the end to make a multiple of two bytes. If the checksum is cleared to zero, then checksuming is disabled. If the computed checksum is zero, then this field must be set to 0xFFFF.
When transported by IPv4, the pseudo header contains the following fields:







Contact : MSN : www.Stats.in.th

AVRnet Ethernet developmetn board V1.1

AVRnet V1.1 is out, added UDP bootloader support for firmware upgrade purpose. You can upgrade new firmware by using AVRnet CPanel V1.1.



AVRnetLoader require 4KBytes or 2KWords program memory



you can activate bootloader by change reset vector from 0x0000 to bootloader section (0x3800) then press reset buttom or click Upgrade buttom on AVRnet CPanel software, after mcu reset bootloader is executed, you will see LED2 blink 5-seconds and waiting for activate bootloader command from AVRnet CPanel. If no activate command, bootloader let check flash memory (0x0000-0x0040) it's already programed or not, if yes jump to application section (0x0000), if no activate bootloader by itself.
*** Do not forget to set boot flash section size=$2048-words and change reset vector to bootloader (0x3800)

Download *** Note **** Before you can build AVRnetLoader you must set .text section to 0x3800.



AVRnet Ethernet development board V1.0
I've been finished AVRnet V1.0, all hardware is base on AVRnet V0.9
What's new in AVRnet V1.0
  • UDP (User Datagram Protocol) has been added to V1.0
  • New UDP interface software I called AVRnet CPanel, it's written using CodeBlocks C++ IDE with wxWidgets GUI tool kits and MinGW GCC compiler.


More detail about UDP and checksum calculation see on AVRnet : Checksum calculation and Codeblocks installation

Download Optional download
Codeblocks : nightly build
http://forums.codeblocks.org/index.php?board=20.0

wxWidgets GUI tool kit
wxMSW-2.8.4-Setup.exe

MinGW GCC compiler
binutils-2.15.91-20040904-1.tar.gz
gcc-core-3.4.2-20040916-1.tar.gz
gcc-g++-3.4.2-20040916-1.tar.gz
gdb-5.2.1-1.exe
mingw32-make-3.80.0-3.exe
mingw-runtime-3.9.tar.gz
mingw-utils-0.3.tar.gz
w32api-3.9.tar.gz

AVRnet Ethernet development board V0.9
AVRnet is an Ethernet development board, you can develop your internet control system by using AVRnet or build small web-server. AVRnet used ATMEGA32 for main processing, ENC28J60 for Ethernet controller, LCD and push switch for user interface.
Web-client has been implemented on firmware V0.9, it's use for send temperature to web-server via http protocol, you can apply this option for web data logger.
IP, TCP, ICMP, ARP, HTTP protocols are implemented on firmware V0.9, UDP protocol is not implement on firmware V0.9 . and I'm going to prepare documents of all source code.


AVRnet's schematic.


web-client option is disabled by default on firmware V0.9, if you want to enable this option you should be install web-server and server side script, I recommended Apache web-server and PHP script.
AVRnet's source code is distribute under GPL and required WINAVR V.20070122 for AVR compiler, you can download WINAVR from http://winavr.sourceforge.net/


AVRnet complete board.



AVRnet's home page.



Web-logger sample page.


Requirement
AVR studio V4.13.528
WINAVR V20070525
Apache web-server [optional]
PHP script [optional]


Download
AVRnet Schematic
Source code V0.9.20070529
PHP script for web-logger

AVR JTAG Debugger/Programmer

AVR JTAG : Credit to IsoJtagIsp for schematic and bootloader. I've been modified a schematic from IsoJtagIsp by remove USB interface from schematic because I'd like to use AVR JTAG with USB2RS232 and serial port.


AVR JTAG schematic.


Download bootloader onto ATMEGA16, If you don't have a AVR programmer I recommended a simple programmer from Ponyprog, because Ponyprog need a few component for build a AVR programmer. If you already have a AVR programmer you can use it to download bootloader. If you're using Ponyprog the fuses should look like this.




Configuration bit.


Once the bootloader is programmed, the next step is upgrade JTAGICE firmware onto ATMEGA16 by using AVR Studio.
  • Set jumper to JTAG mode and powered to the board, then the Power LED should come on and Act LED should be blink.
  • Open AVR studio select Tools menu and select AVR Prog, AVR Prog connected with AVR JTAG look like this.



AVR JTAG firmware upgrade.


  • Click on Browse button and choose AVR JTAGICE firmware for upgrade like C:\MCU\Atmel\AVR Tools\JTAGICE\Upgrade.ebn
  • Click Program button and wait until upgrading complete.
  • Reset board by press reset button and wait until Act LED stop blink.
  • Return to AVR studio, select Tools menu->Program AVR->Connect, if firmware upgrade complete you can see JTAG ICE dialog look like this.





Upgrade OK


  • Testing AVR JTAG with ATMEGA32, Pin connection between AVR JTAG and MEGA32 such as

  • TCK_SCK -> TCK
  • TDO_MISO -> TDO
  • TMS_RST -> TMS
  • TDI_MOSI -> TDI


This is a simple source code for testing JTAG, If you don't have compiler you can download WinAVR from winavr.sourceforge.net it's free compiler. After WinAVR installed, create new project in AVR studio by select Project menu->New project, then you can see Create new project dialog, select AVR GCC in Project type, fill project name you want to create into Project name box, fill main into initial file box, final click finish button and put this code into main.c.

Code:
/***********************************************************************
*
* File : main.c
* Desc : Blink LED on PB0
* Compiler : AVR GCC
* Author : Jirawat Kongkaen
* Website : .
*
***********************************************************************/

#include

void delay(unsigned int delay);

int main(void)
{
DDRB = 0xFF; // Port B as out put

for(;;)
{
PORTB |= _BV(PB0);
//delay(50000);
PORTB &= ~(_BV(PB0));
//delay(50000);
}

return 0;
}

void delay(unsigned int delay)
{
while(delay) delay--;
}


Before start debugging you have to compile main.c first by select Build->Build or press F7 on keyboard, now let start debugging by select Debug menu->Start Debugging of click Start Debugging button, look like this.


Start debugging.


Connection between AVR JTAG and ATMEGA32 is connected (see below figure), Now let's start debugging by click Step into button or press F11, LED at PB0 will come On/Off.



Testing had finished and you can use AVR JTAG for debug/program AVR microcontroller. I will explain about how to AVR JTAG working, if you set JP1 to JTAG mode and power on the board, AVR JTAG wait for firmware upgrade until Act LED come off, You can upgrade firmware at this sequence. After Act LED com off AVR JTAG ready to use. If you set JP1 to ISP mode you should be able to program:


Bread board with AVR JTAG and ATMEGA32


Completed board.

Wireshark network protocol analyzer

This article suggest how to use Wireshark network protocol analyzer. It's easy way for someone who have had beginning developed system with network such as ethernet protocol, wireshark can help you to debug/analyze system. You can download wireshark form http://www.wireshark.org/ or wireshark-setup-0.99.5.exe
After installation completed, open wireshare by click on Start->All programs->Wireshark-> Wireshark, you'll see Wireshark main windows below figure.


Wireshark main windows.


You should be set the Capture options by select Capture->Options or press Ctrl+K to open Capture options dialog before.

Options">
Capture->Options


Select interface card in interface filed and click on Start button to start capture.


Setup Capture options.


Packet capture is now started, it's show you Capture status dialog.


Capture status dialog.


Now open command line by click on Start->Run and type "cmd" on Run dialog. In a command line window, type ping [ip address] -n 1 for example 10.1.1.1 -n 1 and press Enter. Return to capture status dialog you can see captured packets as below figure.


Captured packets.


Click on stop button to stop capture, you can see packet detail separate to 3-panes are
"Packet List" pane is summary of each packets.
"Packet Details" pane is detail of each packets.
"Packet Bytes" pane is raw information (hexadecimal) of each packet.
For detail of each pane please see in Wireshark help file.



First packet.


I will explain about each captured packets, first packet are contain Ethernet II and ARP (Address Resolution Protocol) protocol. Ethernet II protocol are contain source/destination MAC address and protocol type (ARP = 0806H), look at destination MAC address you can see 0xff,0xff,0xff,0xff,0xff,0xff if MAC address is 0xff,0xff,0xff,0xff,0xff,0xff this is broadcast packet. More detail about ARP protocol please visit http://www.networksorcery.com/enp/protocol/arp.htm


Second packet.


Second packet is echo reply from AVRnet Ethernet dev board, look at ARP opcode (opcode=0x0002 is echo reply).


Third packet.


Third packet is echo request from computer to AVRnet. It's ICMP (Internet Control Message Protocol) protocol, more detail about ICMP protocol please visit http://www.networksorcery.com/enp/protocol/icmp.htm


Fourth packet.


Fourth packet is echo reply from AVRnet. It's ICMP echo reply, look at ICMP type (type=0x00 is echo reply).

If you need more detail about Wireshark you can get help from help file or http://wiki.wireshark.org/

Download : wireshark-setup-0.99.7.exe