Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Bailey O.H.Embedded systems.Desktop integration.2005

.pdf
Скачиваний:
73
Добавлен:
23.08.2013
Размер:
9.53 Mб
Скачать

300

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

that is connected to the MTTY program. Running the program template without modification should produce output in MTTY similar to that in Figure 8-26.

Figure 8-26

As you can see, there is information sent to the terminal we didn’t print so let’s examine each line. The first s printed after the reset command has been issued to the SB72. This gives us two seconds to interrupt the start of execution. If we don’t interrupt the program, the IP address and network mask will be displayed. The next line prints the MAC address, which is a unique address assigned to each and every network card or chip used. Every Ethernet interface must have a unique address! The last line prints our “Application started” message, so all of the preceding information is from the boot program prior to passing control to our application.

Chapter 8 / The BASIC Stamp 2p Prototype

301

 

 

Debugging an SB72 Program

Debugging a program on the SB72 takes a slightly different process. Let’s add one line to our program and then compile and debug it. Add the following line just above the OSTimeDly(20) line in the program:

printf("Hello from Embedded Systems!\r\n");

This one line will simply print the message “Hello from Embedded Systems!” every 20 clock ticks. After you have added the line of code, select Build | Make Clean again to remove the old code files. This time we will select Build | Make Debug Version to compile and link the debug version of the code. We have not downloaded the finished program this time because we load and execute through the Insight debugger, which is started by selecting Tools | GDB INSIGHT from the menu bar. The Insight debugger will open with an empty source window. Choose File | Open, navigate to the Nburn\embbk1\Chapter8\test1 folder, and choose the DBTemplate.elf file. When we compile for the debugger the letters DB are prepended to the filename. The elf extension is the excitable code that the debugger will associate with the source file. Once the selection is made, the source code is loaded into the source code window.

Our next step is to connect to the SB72 board. We do this by selecting Run | Connect to Target.

The following screen will be displayed.

Chapter 8

Figure 8-27

302

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

To connect properly, the target, baud rate, and port selections must be set correctly. The choices for Target are:

Cisco/Serial — Cisco Serial Protocol

Cisco/TCP — Cisco TCP Protocol

Remote/Serial — GDB Direct Connect Serial

Remote/TCP — GDB Direct Connect TCP

GDBServer/Serial — GDBServer Serial Connection

GDBServer/TCP — GDBServer TCP Connection

Since we are using the AutoUpdate utility, the Dev C++ IDE will automatically load the compiled program using TCP/IP to the SB72 board. Since we are using the SB72 TCP/IP protocol for our main program, we will choose Remote/Serial as our connection. This allows us to do real-time debugging of the Ethernet program while it is running.

Our Baud Rate selections are 9600, 19200, 38400, 57600, and 115200. While 38400 is the default selection, the NetBurner COM port defaults to 57600. The COM port list should show the available COM ports for your machine. Choose the port connected to the SB72 board. If your choices are correct, you will be connected to the SB72 and the breakpoint will rest at either the program’s main function or the breakpoint line you’ve chosen, depending on the status of the Set Breakpoint at Main check box. You can now step through the program and display variables, buffers, etc., without affecting the program operation.

Note:

I strongly suggest reading the documentation for both the GDB debugger and Insight Debugger IDE to both understand and utilize the full feature set of these tools.

The complete source for this project can be downloaded at www.wordware.com/files/embsys or at www.time-lines.com/ embedbk1/source.

Chapter 8 / The BASIC Stamp 2p Prototype

303

 

 

Testing Ethernet Communications

We are now ready to test our Ethernet interface. This will be a two-stage process. The first stage is to test the connection between the Ethernet board and the BASIC Stamp. Once we have that working, our next task will be testing the Ethernet to network connection.

To test the connection between the BASIC Stamp and SB72 TTL serial connection we will need a Windows 2000 or Windows XP machine with the following:

Serial port to connect the console program to the SB72

Serial port to connect the SDB Debugger to the SB72

Serial port to connect SB72 to TTL pins in BASIC Stamp

The SB72 development kit installed on the Windows machine

The BASIC Stamp Editor and Debugger installed on the Windows machine

An Ethernet card installed and working in the Windows machine

An Ethernet hub with at least two available ports

Two Ethernet cables to connect the SB72 and Windows machine to the hub

If you have already installed and tested the SB72 development software, then you are already familiar with installing the serial cables and starting an mtty terminal and debugger session. Running both of these at the same time normally requires two serial cables, or USB to serial cables. You can install the Ethernet cables to the hub now or wait until we complete our first test.

To understand how the communications between the SB72 and other microcontrollers work requires reading the NetBurner NNDK user manual, which is provided in a Windows help file format. The getting started section, titled “Networking in 1 Day,” helps the user implement a network interface in one working day. If you want to understand more about how Ethernet and Internet

Chapter 8

304

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

protocols work, I would strongly suggest spending some time working through this section of the manual. Another section of interest for our purpose is “The NetBurner Cookbook,” which is a very detailed set of instructions on installing and troubleshooting your development system. For software development purposes we will utilize the chapters on uC/OS and NetBurner System I/O libraries for our first test.

Establishing communications between the SB72 and BASIC Stamp is done through the use of the NetBurner System I/O routines. The debugger is attached to serial port 1, and the mtty terminal program is attached to serial port 0. As we walk through these short but informational listings, our objective is to reach three goals. First, we need to be able to send data between the BASIC Stamp and the SB72 boards. This is accomplished by writing a very simple program that takes input from mtty and sends it to the BASIC Stamp debug screen. We also need to send data from the BASIC Stamp to the SB72, so we will implement sending data via the BASIC Stamp debug command back to the SB72 and to the NetBurner debugger. This will validate that our baud rates, stop bits, parity, and handshake work the way we expect them to.

Once that is working we will wrap these functions in the uC/OS task manager so they run as tasks and will allow for Ethernet data to be sent and received at the same time. Finally, we will add the ability to send and receive data to the Ethernet interface while also handling serial port data. Figure 8-28 shows a diagram of the first task: sending data between the BASIC Stamp and SB72 boards.

Chapter 8 / The BASIC Stamp 2p Prototype

305

 

 

 

Main Program

 

 

Loop

 

 

 

Echo Data

 

 

to

 

YES

Port 1

 

Port 0 Data

 

 

available

 

Loop

 

 

Continuous

NO

 

 

 

Echo Data

 

 

to

 

YES

Port 0

 

Port 1 Data

 

 

available

 

 

NO

 

Figure 8-28

That’s a pretty simple task and doesn’t even require we use the operating system in the SB72. To get started let’s walk through developing a NetBurner application together. If you’ve installed the NetBurner NNDK into the default directory, it will be located in C:\Nburn and a NetBurner NNDK program group will be created. To develop a NetBurner application we will create a template using the NBAppWizard. When started, the NBAppWizard displays the following screen.

Chapter 8

Figure 8-29

// Escalate Program Priority to Highest (5) // Enable AutoUpdate facilities
// Send Start Message to Terminal

306

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

We’ve created a subdirectory named Ch8Test1\serTest1 to hold the project and source files. We’ve named the project Test1 and chosen the SB72-512 platform as our target. We have also checked the auto update and DHCP options. Auto update allows the program to be downloaded to the device automatically after compilation, and DHCP permits an IP address to be acquired automatically. Once we set the options we press the Create key; the project will be created and the window will disappear. Next, we load the project into the editor so we can finish writing the test program.

Start the Dev-C++ editor and load the main.cpp file located in the Nburn\examples\Ch8Test1\serTest1 folder. Below is the skeleton code generated from the application wizard. I entered the comments on the right side of the listing.

Listing 8-2

#include "predef.h"

// Version Information

#include <stdio.h>

// Standard I/O Definitions for C

#include <ctype.h>

// C Type Definitions

#include <startnet.h>

// Network Startup Include File List

#include <autoupdate.h>

// Autoupdate Definitions

#include <dhcpclient.h>

// DHCP Client Definitions

extern "C" {

// Declare UserMain function as external C function

void UserMain(void * pd);

 

}

 

const char * AppName="Test1";

// User Application Name

void UserMain(void * pd)

// UserMain is where the work is done!

{

 

InitializeStack();

// Initialize TCP/IP Stack

if (EthernetIP == 0) GetDHCPAddress();

//If No IP address has been then assigned,

//get one from DHCP Server

OSChangePrio(MAIN_PRIO);

EnableAutoUpdate();

iprintf("Application started\n");

 

Chapter 8 /

The BASIC Stamp 2p Prototype

307

 

 

 

 

 

 

 

 

 

while (1)

// Loop infinitely

 

{

 

 

 

 

OSTimeDly(20);

// Delay every 20

ms to service background tasks

 

}

// End of Loop

 

 

 

}

// End of Program

 

 

 

If you’re familiar with the C language you know that main() is where a program starts. In this case, however, main() initializes the uC/OS and our application is wrapped as a task in the function UserMain(* pd). *pd is a pointer to the process descriptor assigned by the task manager in main().

When our prototype is completed we will no longer use the SB72 I/O board because our connections will go directly to the SB72 processor. For now, however, we need to connect both boards together for debugging purposes. Since we have chosen TTL serial between the Stamp and SB72, we will need to take one of those ports from either mtty or the SDB debugger. To eliminate the need for a third serial port, we will implement the RS-232 send and receive routines for test purposes to the mtty program. Once we have those routines working we will disconnect mtty and connect serial port 0 via TTL to the BASIC Stamp. This will give us the chance to debug our serial I/O using a PC Terminal program and narrow any problems in the interface to the BASIC Stamp code. Using the same template program created earlier, we will add the six lines of code shown in bold in Listing 8-3.

Listing 8-3

#include "predef.h"

// Version Information

#include <stdio.h>

// Standard I/O Definitions for C

#include <ctype.h>

// C Type Definitions

#include <startnet.h>

// Network Startup Include File List

#include <autoupdate.h>

// Autoupdate Definitions

#include <dhcpclient.h>

// DHCP Client Definitions

#include <gdbstub.h>

// Debugger Stub

#include <serial.h>

// Used for serial I/O to Stamp

#include <taskmon.h>

// Used to monitor uC/OS tasks

Chapter 8

extern "C" {

// Declare UserMain function as external C function

308

 

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

 

 

 

 

 

void UserMain(void * pd);

 

}

 

 

 

const char * AppName="Test1";

// User Application Name

void UserMain(void * pd)

// UserMain is where the work is done!

{

 

 

 

InitializeStack();

// Initialize TCP/IP Stack

OSChangePrio(MAIN_PRIO);

// Escalate Program Priority to Highest

EnableAutoUpdate();

// Enable AutoUpdate facilities

InitGDBStubNoBreak(1, 57600);

// initialize Debugger to Port 1

iprintf("Application started\n");

// Send Start Message to Terminal

EnableTaskMonitor();

// Start Task Monitor

while (1)

// Loop infinitely

{

 

 

 

 

OSTimeDly(20);

// Delay every 20 ms to service background tasks

 

iprintf(“Hello from Embedded Systems!\r\n”, Secs);

}

 

 

// End of Loop

}

 

 

// End of Program

These additional six lines of code load and initialize the debugger stub in the program, load the task monitor interface, and print a message to the second serial port (connected to the mtty terminal window). If you read the documentation on how to build and debug a program, you may find you can’t get the debugger to work properly. Before we continue with running the debugger let’s go through the proper steps to compile, load, and debug the source code.

1.Compile a debug version of the code by selecting Build | Make Debug Version. This will place a binary file in the Nburn\bin folder with the prefix DB.

2.Run the AutoUpdate utility and download the file in step 1 to the SB72.

3.Run the Insight Debugger by selecting Tools | GDB INSIGHT.

Chapter 8 / The BASIC Stamp 2p Prototype

309

 

 

4.After Insight has loaded, select File | Open and load the debug file with the same filename in step 1 but with the elf extension. This file will be located in your project folder and not in the bin folder.

5.Unplug the power connector to the SB72 for 30 seconds, then plug it in again.

6.Select Run | Connect from the Insight menu bar.

You should see a message box pop up with a success message. If you don’t see the success popup box, follow the troubleshooting instructions found in the online help of NNDK. This is accessible from the Help button in DevCpp. Look for the topic “Using the GDB/Insight on the NetBurner Platform.” Read the topic and check the debug connection to see if data is being exchanged properly.

Connecting the SB72 to the BASIC Stamp

Now that we’ve tested the SB72 and its functions, it’s time to connect it to the BASIC Stamp. First, disconnect the power to the SB72, then disconnect the SB72 serial adaptor board by disconnecting the ribbon cable attaching it to connector J5 on the SB72. The last program we loaded to the SB72 is in flash memory so it will be retained even without power. According to Table 8-2 the following connections are for port 0: 1 = Gnd, 6 = RTS0, 7 = CTS0, 8 = TX0, 10 = RX0. For power and ground connections, we have 1 = Gnd and 9 = +5 volts (Vcc). We will use port 0 since port 1 is attached to the debugger. If we connect the BS2 to connector J5 we can leave the mtty program attached to port 0. All data to and from the Stamp will go through the RS-232 transceiver on the I/O board and be displayed in the mtty screen. To use mtty this way we need to turn off handshaking since the SERIN and SEROUT commands will provide this at the TTL level. Before we continue let’s update our schematic to reflect the four I/O lines needed for our TTL serial interface.

Chapter 8

Соседние файлы в предмете Электротехника