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.
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.
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.
// 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");
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.