Bluetooth on Gumstix computers

Bluetooth on Gumstix computers Dhawal Harsora Abstract Gumstix computer is a popular and new developing technology in an embedded system. Bluetooth i...
Author: Dwain Porter
5 downloads 2 Views 2MB Size
Bluetooth on Gumstix computers Dhawal Harsora

Abstract Gumstix computer is a popular and new developing technology in an embedded system. Bluetooth in the Gumstix enhances the scope for embedded applications. The combination of two adds simplicity and easiness in an application development. However, beginners who are not familiar with the Gumstix and Bluetooth may not get significant resources to get started. In this guide, we propose simple procedures which may help the user to get started with the Gumstix and Bluetooth.

1

Introduction: The following report is about Gumstix computers and their applications. It will give a brief introduction to Gumstix, Bluetooth communication and how to write programs for Gumstix. The tutorial is divided into three sections: section 1 gives details about Gumstix and its components; section 2 describes about build root, how to flash file system to Gumstix, and a sample Hello.c program; and finally, the last section contains an introduction to Bluetooth, its protocols and programming.

Note: Commands entered in Gumstix command prompt are shown in Brown color. Commands entered in the host machine are shown in Blue color.

2

Table of Contents Section 1 1.1 What is Gumstix?…………………………….………………..………………3 1.2 Gumstix Components 1.2.1 Basix……………………………………………...…………………4 1.2.2 Robostix…………………………….………….….………………...5 1.2.3 Tweener……...………………………………………………………6 Section 2

2.1 Build root…………………………………….……………………………….8 2.2 Power on Gumstix……………………………………………………………10 2.2 Flashing a file system to Gumstix…………….……………….……………..10 2.3 Hello.c………………………………………….………………………….…11 Section 3 3.1 Introduction to Bluetooth……………………….……………………………13 3.2 Bluetooth network topology…………………….…………………………...14 3.3 The Bluetooth protocol stack architecture….………………………………..15

3.4 RFCOMM……………….…………………………………………………..17 3.5 L2CAP……...………………………………………………………………..20 Summery Reference

3

Section 1: 1.1) What is Gumstix? Gumstix is small miniature computer with a Linux 2.6.0 operating system. Generally, the word “Gumstix” refers to mother board (Basix, Connex or Verdex). The Gumstix board is based on the Intel Processor ARM 128 [18] with 4/16 MB flash memory and 64 MB RAM. It contains a similar kind of hardware that can be found in a cell phone, PDA, iPod, or GPS. One can develop an mp3 player, PDA, robotics application, scrolling display, and many other embedded applications using Gumstix hardware. The Bluetooth feature in Gumstix helps in wireless communication. Gumstix comes with many other daughter cards which are helpful for adding more features. One can attach daughter cards like etherix, robostix, tweener, etc. to the Gumstix motherboard. This report describes about a basix-bt 400 MHz motherboard with robostix expansion board, tweener, and Bluetooth antenna.

Figure 1: Assembly of Basix bt 400xm pack 4

1.2) Gumstix components: [3] The following describes about the Gumstix components; basix, robostix and tweener. 1.2.1) Basix 400xm-bt:

Figure 2: Basix mother-board Basix is an Intel PXA255 driven motherboard with 16 MB flash memory, an option of extended memory card (MMC adapter) and a Bluetooth connector. It has one 60 pin connector for connecting an expansion board. Technical specifications of the basix 400xm are the following: CPU Speed Dimension SDRAM Flash memory 60-pin connection MMC/SD slot Bluetooth

400 MHz 80x20mm 64 MB 16 MB yes yes yes

5

1.2.2) Robostix:

Figure 3: Robostix Robostix is a stand alone microcontroller (Atmel AVR processor) which can be attached to the Gumstix through the 60-pin connector. Gumstix is not required for robostix to work since it can be connected to any Windows or Linux computer using ISP cable. Microcontroller in robostix helps in developing I/O applications.

6

1.2.3) Tweener:

Figure 4: Tweener Tweener is used to get console access via RS-232 serial connection. It has two 60 pin connectors. It connects in between a basix/connex motherboard and an expansion board. Tweener needs some modification in order to work with robostix: “The tweener was never really intended to be used with the robostix, since the robostix has a console port on it (albeit a logic level serial port). The tweener was designed to work with the original audiostix.” [9, 14] The problem is that there is a 5v to 3.3v converter on the robostix which is trying to control the Rx line to the Gumstix, and the tweener also has an RS-232 to 3.3v converter which is trying to control the Rx line on the Gumstix. What happens is that neither one works. Cutting the trace as shown in the following picture disconnects the robostix voltage converter, and thus there is only the tweener trying to drive the Rx line so everything works fine.

7

Figure 5: Back side of Tweener.

8

Section 2: 2.1) Build root: [5] Build root is a collection of patches and make files that are useful for building a file system and a cross-compiler tool chain for the target machine, i.e. Gumstix. Build root automates the building of a root file system with all needed tools. The embedded system uses processors that are usually different from the host machine. For example, a host machine has an x86 processor and Gumstix uses an ARM processor. In this kind of situation the host compiler tool chain is not helpful for compiling the Gumstix program. Code generated by the host compiler works for that particular processor, i.e. the host processor. The compilation tool chain consists of compilers, linker, assembler, and C libraries. Cross compilers are required in the host machine for generating binary files for Gumstix. Remember binary files generated by the cross compiler will not work in the host machine. Buildroot downloads all required code and data, the cross compiler tool chain for compiling code for ARM processor, compiles all Gumstix software and combines the result into a final file-system image that can be flashed to Gumstix. One should install build-essential, libncurses-dev, bison, flex, lexinfo, zlib1g-dev and gettext in Ubuntu before starting to the build root procedure. The command to install required packages in Ubuntu: apt-get install build-essential libncurses-dev bison flex texinfo zlib1g-dev gettext For checking out current version of build root, enter the following command: svn co http://svn.gumstix.com/gumstix-buildroot/trunk gumstix-buildroot For checking out specific revision (say of example 1421) of build root, enter the following command: svn co –r1421 http://svn.gumstix.com/gumstix-buildroot/trunk gumstix-buildroot

9

Check the revision number of the Gumstix by using the following command on Gumstix command prompt: cat /etc/gumstix-release | grep RELEASE The commands to build root (revision later than 865): ~>cd gumstix-buildroot ~>make defconfig ~>make For revision earlier than 865 use: ~>cd gumstix-buildroot ~>make Note: This is lengthy and fully automated procedure. It took more than 60 minutes in my machine. If every thing goes fine then you will find rootfs.arm_nofpu.jffs2 (root file system) and cross compiler tool chain. The following diagram shows the approach for wring simple Hello World program for the Gumstix.

1

Flash a file system

Build root (Creates a file system image and cross compiler tools for the Gumstix in the host machine.)

(Optional)

HelloWorld.c (Write a HelloWorld.c in the host machine for the Gumstix.)

Run

(Transfer the executable to the Gumstix and run the program.)

Compile (Compile the program by using cross compiler.)

Figure 6: Steps for wring a program for the Gumstix 2.2) Power on Gumstix: Run kermit (a file transfer program) on the host machine by type-in “kermit” on the command prompt. Setup the parameters of the Kermit program as shown in figure 6. Connect the tweeener to the serial port 0 of the host machine and power on the Gumstix. After type in a connect command, the Gumstix will start booting. The booting sequence will appear on the host machine console. After booting it will show the following message: Welcome to the Gumstix Linux Distribution! gumstix login: User name is 'root' and password is 'gumstix'. (user name and password are case sensitive.)

1

2.3) Flashing a file system [6]: One can flash the Gumstix file system [6] with a new file system (rootfs.arm_nofpu.jffs2). Note that there is no need to flash a file system unless it is required. The new uBoot boot loader can also be flashed [3]. If the new file system fails, it can be re-flashed again but messing up with uBoot flashing is dangerous, and there is no way to fix it excepts sending the Gumstix back to the company for re-flashing the boot image. The factory file system is also available on: http://docwiki.gumstix.org/Factory_flash_images The following shows commands used in Kermit program for flashing a file system to Gumstix. ~k>kermit ~k> set line /dev/ttyS0 ~k> set speed 115200 ~k> set carrier-watch off ~k> handshake none ~k> flow-control none ~k> reliable ~k> fast ~k> set prefixing all ~k> set file type bin ~k> set rec pack 4096 ~k> set send pack 4096 ~k> connect Figure 7: Commands for setting parameters for the Kermit. Now power on or reboot the Gumstix and press any key immediately to access uboot. It will show GUM prompt. GUM> loadb a2000000 Now go back to Kermit prompt by pressing Ctrl-backslash and then c. ~k> send rootfs.arm_nofpu.jffs2

1

~k> connect GUM> protect on 1:0-1 GUM> erase all GUM> cp.b a2000000 40000 ${filesize} GUM> boot 2.4) HelloWorld.c Example: main( ) { printf(“Hello World \n”); } Save the above program in the host machine. Compile the above program by using the cross compiler tool. Enter the following command: ~>./build_arm_nofpu/staging_dir/bin/arm-linux-gcc HelloWorld.c –o HelloWorld Remember the host machine will not be able to run executables generated by the cross compiler. Now it is time to send that executable to Gumstix.

Sending a file to the Gumstix: Run “rs” command on Gumstix command prompt by entering the following command on Gumstix prompt: ~>rs It will set Gumstix in receiving mode. Now use zmodem to transfer a binary file to the Gumstix. The following shows the step by step information for sending HelloWorld binary file to the Gumstix using minicom. ~>minicom –o Press CTRL A and then press S. It will show the option of zmode. Select zmode and upload binary file.

1

Section 3: 3.1) Introduction to Bluetooth: Electronics devices can communicate with each other by several ways. For example, by using Ethernet cable, wire, infrared signals, WiFi, Bluetooth, etc. the Bluetooth provides a way to communicate or exchange data wirelessly between Personal Area Network (PAN) devices like laptop, mobile, PDA, digital camera, head set etc. The Bluetooth technology was originally envisioned by Ericsson in 1994 for communicating between mobile devices in short range [8]. Its range of connectivity is up to 30 feet or 10 meters and it operates on radio frequency technology 2.4GHz Industrialscientific-medical (ISM). The major features of the Bluetooth are following: [8]

1



It is wireless and automatic. There is no need of cable or any connector for establishing a connection and it finds other nearby Bluetooth devices automatic.



It is inexpensive.



The Bluetooth has low power consumption.



The Bluetooth handles both data and voice transmission which is helpful for making mobile hands-free headset for voice, synchronizing the address book on PDA, laptops, cell phone etc.



The Bluetooth signals are omni-directional and can pass through any interfering medium, like walls.

But, why use Bluetooth instead of IEEE wireless card? Both the wireless-card like IEEE 802.11 and the Bluetooth operate in the same frequency range, i.e. 2.4 GHz, but both are designed for different purposes. The 802.11 card is design for high speed connectivity, i.e. 11MBps or more and a range of connectivity is from 100 feet to 300 feet. Power consumption of the wireless card is much higher [8]. Small mobile devices like PDAs or cell phones can not afford this much power consumption. The Bluetooth is designed to communicate between small devices with low speed, low range and low power consumption (the connectivity speed of the Bluetooth is 1.1Mbps and connection range is up to 10 meters). “The IEEE has designated its version of the Bluetooth specification 802.15. This version will complement 802.11b technology, and boost Bluetooth's transmission speed to 20 Mbit/sec, which would, for example, make it feasible to download photos from a digital camera wirelessly. [8]” 3.2) Bluetooth network topology [7]:

1

Scatternet

S1

S4 M1

S3 S2

Piconet A

M2

S2

S1

Piconet B

Figure 7: Bluetooth scatternet and piconets [8] Figure 7 shows the Bluetooth network topology; piconet and scatternet. The first level Bluetooth network is called piconet. All the devices in a piconet share same channel. A maximum of 255 devices can be linked together in a piconet, but only 8 devices can stay active at a same time. Out of eight devices, there is one master device and other seven are slave. All the active devices in a piconet can be identified by 3-bit active device address. The device that initiates a connection is considered as the master in a piconet and rest are called slaves. In figure 7 there are two piconets; piconet A and piconet B. M1 and M2 are mater devices in the piconet A and B respectively and the rest are slaves. S3 is a slave in piconet A and B. Slave device can have more then one master device, it can be a master of other piconet. Piconet is for establishing point-to-point or point-to-multipoint communication. Group of piconets can linked together and form a scatternet. 3.3) The Bluetooth protocol-stack [4, 8, 16]:

1

Voi ce

Applications

TCS

SDP

RFCOMM

Data L2CAP HCI LMP

TCS: Telephony control protocol Specification SDP: Service Discovery Protocol RFCOMM: Serial port emulation L2CAP: Logical link control and adaptation protocol HCI: Host controller interface LMP: Link manager protocol

Base Band

Radio

Figure 8: The Bluetooth protocol stack [8] The Bluetooth protocol stack has been developed and maintained by the Bluetooth special interest group (SIG) [10]. Figure 8 shows the high level components view of the Bluetooth protocol stack architecture. Radio module: The radio module is a physical layer in the protocol stack which is responsible for modulating and demodulating data into RF signal for transmission. Base band layer: The base band layer is responsible for controlling and sending data packets (data or voice) over radio link. For the transmission of voice over channel it maintains synchronies connection-oriented link, which are peer to peer connection links. SCO links reserve the channel bandwidth with periodically transmission of voice data so that user can not notice any time gap while talking. Base band layer also provides asynchronous connection-less link for transmission of data. Major difference between SCO links and ACL is SCO never retransmit the packets while ACL does retransmission when there is a delay or error in delivered packet. ACL ensures data integrity. LMP: The Link Manager Protocol handles connections establishment between devices which includes authentication, encryption etc. HCI: HCI is a dividing line between upper layer protocols and lower layer protocol. L2CAP: [8, 10, 12, 13] L2CAP supports channel multiplexing, quality of service, error retransmission, fragmentation and reassemble of data packets, and flow control. 1

There are various transport protocols available for Bluetooth communication. RFCOMM L2CAP BNEP The following briefly describes only the first two protocols, i.e. RFCOMM, and L2CAP. A 48-bit unique number is associated with any Bluetooth device, called Bluetooth address. A Bluetooth address is identical to a MAC address of Ethernet. It is helpful for establishing a connection between two devices. Bluetooth programming can be seen as an Internet Programming except Internet Programming uses IP addresses at the transport and network layer and machine addresses at the data link layer while Bluetooth programming uses Bluetooth addresses at all layers in the protocol. There are many libraries available for Bluetooth communication. For Bluetooth programming in C, install blueZ library, most Linux distribution comes with this package. One can also download it from http://www.bluez.org/. It is licensed under the GNU General Public License (GPL) and is free. Bluetooth programming is similar to Internet Programming. In Internet Programming, a client and server talk with each other through socket. Server creates a socket, binds that socket to some port number and waits for a connection from a client. When client requests for the connection; sever will accept the connection and then client and server can communicate with each other. For more information about socket programming (Internet Programming), refer Beej’s Guid to Network Programming. [11]

3.4) RFCOMM: [10] RFCOMM is similar to TCP in Internet Programming. It provides reliable service [19]. When the user is using the Bluetooth instead of RS-232 serial port for connecting to the Gumstix, the Gumstix uses RFCOMM protocol. There are only 30 ports available in

1

RFCOMM and port 1 is used for establishing Gumstix console. RFCOMM provides persistence connection like TCP. Following is an example of client/server communication with RFCOMM. //server.c #include #include #include #include

//from bluez library

#include

//from bluez library

int main() { //RFCOMM structure struct sockaddr_rc myAddress, remoteAddress; int listenfd, client; char buff[100]; listenfd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); if(cc < 0){ perror(“RFCOMM Socket”); exit(1); } myAddress.rc_family = AF_BLUETOOTH; myAddress.rc_bdaddr = *BDADDR_ANY; myAddress.rc_channel = (uint8_t) 1;

//defining binding port 1

bind(listenfd, (struct sockaddr *)&myAddress, sizeof(myAddress)); // listen to socket listen(listenfd, 1); //accept the requested connection int fsize = sizeof(remoteAddress) client = accept(listenfd, (struct sockaddr *)&remoteAddress, & fsize); memset(buf, 0, sizeof(buf)); /* ba2str takes 6-byte remoteAddress.rc_bdaddr and copy it into buf */

1

cc = ba2str( & remoteAddress.rc_bdaddr, buf ); if(ccgcc server.c –lbluetooth –o server To run: ~>./server //client.c #include #include #include #include #include int main() { struct sockaddr_rc addr; int listenfd, status; //XX:XX:XX:XX:XX:XX IS BLUETOOTH ADDRESS

2

char dest[18] = "XX:XX:XX:XX:XX:XX"; int cc = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); if(cc < 0){ perror(“Socket failed”); exit(1); } // set the connection parameters (who to connect to) addr.rc_family = AF_BLUETOOTH; addr.rc_channel = (uint8_t) 1; str2ba( dest, &addr.rc_bdaddr ); // connect to server status = connect(listenfd, (struct sockaddr *)&addr, sizeof(addr)); // send a message if( status == 0 ) { status = write(listenfd, "hello world!", 12); } if( status < 0 ){ perror("write failed"); exit(1); } return 0; } To compile client.c: ~>gcc client.c –lbluetooth –o client To run: ~>./client

Transferring data over a network requires consistent byte ordering. Bluetooth follows little-endian byte order where the least significant bytes are transferred first [12]. The BlueZ library provides four different byte ordering function to convert host byte order to Bluetooth byte order.

2

unsigned short int htobs( unsigned short int num );

// Convert a 16-bit integer from host to bluetooth byte order. unsigned short int btohs( unsigned short int num ); //

Convert a 16-bit integer from bluetooth byte order to host byte order.

unsigned int htobl( unsigned int num );

// Convert a 32-bit integer from host to bluetooth byte order. unsigned int btohl( unsigned int num ); //

Convert a 32-bit integer from bluetooth byte order to host byte order.

3.5) L2CAP: [10] Logical link control and adaptation protocol (L2CAP) provide both connectionoriented and connection-less service. By default it uses connection-oriented service and can reliably transfer datagrams of a default maximum length of 672 bytes. L2CAP has 1024 reserved ports and around 15,000 unreserved ports. Maximum datagram size can go up to 65KB. Major difference between L2CAP and RFCOMM is the following. RFCOMM has total 30 ports. (1-30, no reserved port) L2CAP has 1024 reserved ports (odd numbered 1-4095) and 14334 unreserved ports (odd numbered 4097-32765). L2CAP uses any of the following policies for re-transmitting data: [12] •

No retransmission



Retransmit not acknowledged or error packets.



Wait for a specified amount of time (0-1279 milliseconds) for the acknowledgement. If packet is not being acknowledged in specified time range then it will not re-transmit that packet.

//l2capServer.c #include #include #include #include #include

//from bluez library

#include

//from bluez library

2

int main(int argc, char **argv) { struct sockaddr_l2 myAddress, remoteAddress; char buf[1024]; memset(buf, 0, sizeof(buf)); int listenfd, client, bytes_read; int opt = sizeof(remoteAddress); // initialize socket listenfd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); if(listenfd 0 ) { printf("received [%s]\n", buf); }

2

// close connection close(client); close(listenfd); }

Compile: ~>gcc l2capServer.c –lbluetooth –o server Run: ~>./server

//l2capClient.c #include #include #include #include #include int main(int argc, char **argv) { struct sockaddr_l2 addr = { 0 }; int listenfd, status; char *message = "hello!"; char dest[18] = "XX:XX:XX:XX:XX:XX"; // XX:XX:XX:XX:XX:XX = BLUETOOTH ADDRESS // Initialize a socket listenfd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); // set the connection parameters (who to connect to, i.e. server) addr.l2_family = AF_BLUETOOTH; addr.l2_psm = htobs(0x1001); str2ba( dest, &addr.l2_bdaddr ); // connect to server status = connect(listenfd, (struct sockaddr *)&addr, sizeof(addr)); // send a message

2

if( status == 0 ) { status = write(s, "hello!", 6); } if( status < 0 ) perror("Message in not sent "); close(listenfd); }

~>gcc l2capClient.c –lbluetooth –o client ~>./client Try the above program for Gumstix. Compile the server program using the cross compiler and send it to Gumstix. Compile the client program using the host machine compiler. Now, run server and then client.

Summary: There are several interesting projects that can be developed by using Gumstix, like Bluetooth enabled LED sign board, webserver, Voice over IP phone etc. For controlling a message of the sign board only requires a computer and a serial port. Gumstix is not required but the option of Bluetooth helps sending a message to the LED scroll-board using PDA, cell phone or any other Bluetooth devices and it gives portability

2

to the user. One can also use Gumstix-bluetooth feature for controlling miniature aircraft or submarine kind of application.

References: [valid on July 29, 2007] [1] http://www3.ietf.org/proceedings/00jul/SLIDES/ipobt-tech/ [2] http://www.euglug.org/Sept-05-Embedded-linux-euglug.pdf [3] www.gumstix.com, http://docwiki.gumstix.org/ [4] Bluetooth: the new era of Connectivity:

2

http://ieeexplore.ieee.org/iel5/6668/22085/01028360.pdf?arnumber=1028360 [5] http://buildroot.uclibc.org/ [6] http://docwiki.gumstix.org/Replacing_the_filesystem_image [7] http://www.topxml.com/conference/wrox/wireless_2000/stecktext.pdf [8] http://developers.sun.com/mobility/midp/articles/bluetooth1/ [9] Personal communication, Dave Hylands [10] http://www.bluetooth.com/bluetooth/ [11] Online Beej’s Guid to Network Programming, http://www.beej.us/guide/bgnet/output/html/multipage/index.html [12] An Introduction to Bluetooth programming in GNU/Linux, http://people.csail.mit.edu/albert/bluez-intro/ [13] http://dec.bournemouth.ac.uk/staff/tboucouvalas/prep2005bluetooth.pdf [14] Tweener modification, http://docwiki.gumstix.org/Tweener_modifications [15] http://bluetooth.com/Bluetooth/Learn/Works/Data_Transport_Architecture.htm [16] http://msdn2.microsoft.com/en-us/library/Aa931801.aspx [17] http://www.bluez.org/ [18] http://www.arm.com/ [19] Computer Networks, Andrew Tanenbaum

2

2