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

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

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

310

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

Figure 8-30

In Figure 8-30 we have allocated the BS2 pins 0-3 to the 4-Wire TTL serial interface. Our pin assignments are: P0 = TX, P1 = RX, P2 = CTS, and P3 = RTS. We will also connect the VSS from the BS2 to Gnd on the SB72. In Chapter 7 we covered how the RS-232 interface works. The following listing will echo any inbound data to the debug terminal screen.

Listing 8-4

//{$STAMP BS2p}

//{$PBASIC 2.5}

//Test Program for RS-232 TTL I/O using RTS Handshaking

//for Embedded Systems Desktop Integration Chapter 8

//Copyright 2004 - Oliver H. Bailey

//

// Pins are assigned in the following order:

// Pin 0 -

Transmit Data

(TX or TD) =>

// Pin 1 -

Receive Data

(RX or RD) <=

// Pin 2

-

Clear To Send

(CTS)

=>

// Pin 3

- Request To Send (RTS)

<=

//

 

 

 

 

TX CON 0

 

 

 

 

RX CON 1

CTS CON 2

 

Chapter 8 / The BASIC Stamp 2p Prototype

311

 

 

 

 

 

 

 

 

RTS CON 3

 

 

 

InData VAR Byte

 

 

 

OutData VAR Byte

 

 

 

LOW RX

// Set RD Low

 

LOW RTS

// Set RTS Low

 

Main:

 

 

 

SERIN RX\CTS, 110, [InData]

// 1=RD, 2=CTS, 110=9600, N, 8, 1

 

IF InData = 13 THEN

// If InData = CRLF

 

DEBUG CR, LF

// Send to Terminal

 

ELSE

// ELSE

 

DEBUG InData

// Send the Data

 

ENDIF

// End of IF Statement

 

GOTO Main:

// Start Again

 

Before we can test this program we need to change the baud rate on SB72 port 0 to 9600 baud. The BS2 cannot operate at speeds over 19200 so this change is required. To make this change we need to close serial port 0 on the SB72 and reopen it with a new baud rate. Listing 8-5 shows the changes needed to connect to the BS2.

Listing 8-5

#include "predef.h" #include <stdio.h> #include <ctype.h> #include <startnet.h> #include <autoupdate.h> #include <serial.h> #include <iosys.h> #include <utils.h>

#include <taskmon.h> #include <gdbstub.h>

#define

DATAPORT 0

//

Port

0

for

the

BS2

#define

DEBUGPORT 1

//

Port

1

for

the

Debugger

Chapter 8

312

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

 

 

extern "C" {

 

void UserMain(void * pd);

 

}

 

 

const char * AppName="serialtest";

 

void UserMain(void * pd)

 

{

 

 

InitializeStack();

// Initialize TCP/IP

OSChangePrio(MAIN_PRIO);

// Change to priority 50

EnableAutoUpdate();

// Allow program updates

SerialClose(DATAPORT);

// Close current Data Port

SerialClose(DEBUGPORT);

// Close current Debug Port

InitGDBStubNoBreak(DEBUGPORT, 57600);

// Initialize Debugger

int fddata = OpenSerial(DATAPORT, 9600, 1, 8, eParityNone); writestring(fddata, "Application started\r\n"); EnableTaskMonitor();

while (1)

{

OSTimeDly(20);

writestring(fddata, "Hello from Embedded Systems!\r\n");

}

}

Notice how we close both data and debug serial ports and then reopen them. You’ll also notice that we are using the writestring C function instead of printf or iprintf. This is because we have not redirected where the stdin and stdout go. Instead, writestring uses the file descriptor from the OpenSerial function call. We could check fddata to see it is valid, but the purpose is to demonstrate the process. If you have mtty attached you will see the message “Hello from Embedded Systems!” displayed every 20 clock ticks. The same data is being sent to the BS2 via the TTL level RS-232.

Now that we’ve tested receiving data from the BS2 it’s time to test sending data from the BS2. Since we don’t need the debugger right now, we can disconnect the power from the SB72 I/O card and disconnect the card from the main SB72 board. Our first task is to connect the power and ground wires to the SB72 from our BS2 board. The center pin of the J8 connector on the SB72 is ground. Either of the outside pins on J8 are +5. Make

Chapter 8 / The BASIC Stamp 2p Prototype

313

 

 

sure the power to the BS2 is off before connecting these pins. Remember that port 0 on the SB72 is our data port and port 1 is our debug port.

Now it’s time to add support for TCP/IP to our program. We are already initializing the IP stack since we are using AutoUpdate to transfer programs to the SB72 board. Now we will add support for testing our BS2 data I/O to a host computer. To do this we will use the telnet program to connect to the SB72, send data to the BS2, and display data from the BS2 in the telnet terminal window. Now things get a little tricky because we need to write software for the SB72 and the BS2. Since we’ve already got our BS2 receiving data, we need to only add the send data routine. This time, however, instead of our mtty program we will substitute the telnet program, which in effect is an Ethernet terminal program. Since we will be developing our host software later, using telnet will allow us to not only test but also provide us with a working code foundation. Let’s start with the software for the SB72 in Listing 8-6.

Listing 8-6

#include "predef.h" #include <stdio.h> #include <ctype.h> #include <startnet.h> #include <autoupdate.h> #include <serial.h> #include <iosys.h> #include <utils.h> #include <ip.h> #include <tcp.h> #include <taskmon.h>

#define TIMEOUT (60)

// Timeout between packets

#define OVERRIDE_DLY (20)

 

#define MSG_WAITING "Waiting to Connect\r\n"

// IP waiting

#define MSG_CONNECT "Connection Opened\r\n"

// IP open

#define MSG_CLOSED

"Connection Closed\r\n"

// IP closed

#define MSG_TO

"Connection Timeout\r\n"

// IP timeout

Chapter 8

314

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

#define MSG_TO_CLOSED "Connection Timeout, Connection will be closed\r\n"

#define DATAPORT 0

// Data com port

#define DEBUGPORT 1

// Data debug port

#define BAUDRATE 9600

// Serial baud rate

#define STOPBITS 1

// Stop bits

#define DATABITS 8

// Data bits

#define TCPIP_PORT 23

// Telnet port

extern "C" {

 

void UserMain(void * pd);

 

}

 

const char * AppName="TestTCP";

// Set Application Name

void UserMain(void * pd)

 

{

 

InitializeStack();

// Initialize TCP/IP Stack

OSChangePrio(MAIN_PRIO);

// Move priority up to 50

EnableAutoUpdate();

// Allow Ethernet based updates

EnableTaskMonitor();

// Enable Task Monitor

SerialClose(DATAPORT);

// Close and reopen data port with

 

// new parameters

int fdserial = OpenSerial(DATAPORT, BAUDRATE, STOPBITS, DATABITS, eParityNone);

writestring(fdserial, "Application Starting\r\n");

// Display start message

int fdlisten = listen(INADDR_ANY, TCPIP_PORT, 5);

// listen for any remote request

if (fdlisten > 0 )

// Connection request = fdlisten > 0

{

 

 

IPADDR host_addr;

// address of connecting client

WORD port;

// port request came in on

writestring(fdserial, MSG_WAITING);

// display wait message

while (1)

// Do forever

{

 

 

int fdnet = accept(fdlisten, &host_addr, &port, 0);

// accept connection request

writestring(fdserial, MSG_CONNECT);

// Display connect message

writestring(fdnet, MSG_CONNECT);

// Display message to telnet

int ip_timeout = 0;

// Set timeout to zero

while (fdnet > 0)

// While active socket connection

{

 

 

fd_set read_file;

// create read file descriptor

fd_set error_file;

// create error file descriptor

 

Chapter 8 / The BASIC Stamp 2p Prototype

315

 

 

 

 

FD_ZERO(&read_file);

// zero read_file descriptor contents

 

FD_SET(fdnet, &read_file);

// set fdnet with read_file descriptor info

 

FD_SET(fdserial, &read_file);

// set fdserial also with read_file descriptor info

if (ip_timeout >= OVERRIDE_DLY)// If timeout > delay

 

{

 

 

 

FD_SET(fdlisten, &read_file);// set fdlisten with read_file descriptor info

 

}

 

 

 

FD_ZERO(&error_file);

// Zero error file descriptor

 

FD_SET(fdnet, &error_file);

// set fdnet with error_file info

 

// Wait for descriptor events - IO Event

if (select(FD_SETSIZE, &read_file, (fd_set *) 0, &error_file, TIMEOUT))

{

// Check for event descriptors

 

if (FD_ISSET(fdnet, &read_file))

// fdnet read_file

{

 

char buf[40];

// allocate buffer

int n = read(fdnet, buf, 40);

// read up to 40 characters

write(fdserial, buf, n);

// echo to serial port

ip_timeout = 0;

// reset timeout value to zero

}

 

if (FD_ISSET(fdserial, &read_file))

// fdserial read_file - Input from BS2

{

 

char buf[40];

// allocate 40-character buffer

int n = read(fdserial, buf, 40);

// read up to 40 characters

write(fdnet, buf, n);

// write to telnet device

}

 

if (FD_ISSET(fdnet, &error_file))

// fdnet error_file

{

 

writestring(fdserial, MSG_CLOSED);

// send close message to serial port - Error to BS2

close(fdnet);

// close IP port

fdnet = 0;

// Set file descriptor to zero

writestring(fdserial, MSG_WAITING);

// Send waiting message to BS2

}

 

if (FD_ISSET(fdlisten, &read_file))

// No data timeout

{

 

writestring(fdserial, MSG_TO);

// Send message to BS2

writestring(fdnet, MSG_TO);

// Send message to telnet

Chapter 8

316

 

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

 

 

 

 

 

 

 

 

 

close(fdnet);

// close telnet port after message

 

 

fdnet = 0;

// Zero file descriptor for socket

 

}

 

 

 

}

 

 

 

else

// Else we have no pending events so

 

{

 

 

 

ip_timeout++;

// increment ip_timout counter

 

if(ip_timeout > TIMEOUT)

// timeout period exceeded

 

{

 

 

 

 

writestring(fdserial, MSG_TO);

// Send Stamp timeout message

 

 

writestring(fdnet, MSG_TO); // send connected socket device timeout message

 

 

close(fdnet);

// close socket

 

 

writestring(fdserial, MSG_TO_CLOSED);// send close message to BS2

 

 

fdnet = 0;

// Set file descriptor for socket to zero

 

}

 

 

 

}

 

// End of else statement

 

}

 

// End of open socket handler

 

}

 

// End of While (1) loop

}

 

 

// End of Connection Request Loop

}

 

 

// End of main program

Every connected device has an associated file descriptor once opened. In Listing 8-6 the BS2 is the fdserial device and the telnet program is the fdnet device. The fdlisten descriptor is the socket waiting for a connection request, whereas the fdnet descriptor is the opened socket device. The call to select is looking for an event change so further processing can take place. If an fdnet device is opened and receiving data, a timeout counter is kept. If no data is received before the counter expires, the opened socket is closed.

For the sake of clarity I used text messages for status, but since we are communicating between machines, we can replace those messages with single-byte numeric values in the finished prototype. If you compile and download this program, you need to connect the mtty program to port 0, at 9600 baud, 8 data bits, 1 stop bit, and no parity. On the Windows OS, open a command box and execute the telnet program as shown in Figure 8-31.

Chapter 8 / The BASIC Stamp 2p Prototype

317

 

 

Chapter 8

Figure 8-31

From a Windows XP command prompt type telnet and press Enter. You will get the telnet> prompt. Type open and press Enter and you will see a <to> prompt displayed on the next line. Enter the IP address of the SB72 board, which in this case is 192.168.0.252. If you typed the correct address, you will get a connect message. The escape character for telnet is Ctrl+] (right

Figure 8-32

318

Chapter 8 / The BASIC Stamp 2p Prototype

 

 

bracket). Pressing these control keys will allow you to enter telnet command sequences that are not echoed to the SB72. Remember, the telnet program is a simple Ethernet-based terminal program. It will display characters sent from the Stamp and the Stamp will echo characters received from telnet. I kept the mtty program attached to monitor data traffic. Figure 8-32 shows the data that was seen by the BASIC Stamp.

When the telnet program opened a socket to the SB72 a “Connection Opened” message was sent to the BS2. I typed “This is a test” two times and the BS2 responded with “This is the BS2p Stamp” message. Nothing further was sent from the telnet program, so after a couple of minutes the SB72 closed the connection. The first message was a “Connection Timeout” message, which was followed by a close message. Once closed, the SB72 goes back into listen mode. The final code listing for the BS2 is in Listing 8-7.

Listing 8-7

//{$STAMP BS2p}

//{$PBASIC 2.5}

//Test Program for RS-232 TTL I/O using RTS/CTS Handshaking

//for Embedded Systems Desktop Integration Chapter 8

//Copyright 2004 - Oliver H. Bailey

//

// Pins are assigned in the following order:

// Pin 0 -

Transmit Data

(TX or TD)

=>

// Pin 1

-

Receive Data

(RX or RD)

<=

// Pin 2

-

Clear To Send

(CTS)

=>

// Pin 3

- Request To Send (RTS)

<=

//

 

 

 

 

 

TX

CON 0

 

 

 

RX

CON 1

 

 

 

CTS CON 2

RTS CON 3

InData VAR Byte

OutData VAR Byte

 

Chapter 8 / The BASIC Stamp 2p Prototype

319

 

 

 

LOW 14

// Set RD Low

 

LOW 15

// Set RTS Low

 

Main:

 

 

SERIN RX\CTS, 110, [InData]

// 1=RD,2=CTS,110=9600,N,8,1

 

IF InData = 13 THEN

// If InData = CRLF

 

DEBUG CR,LF

// Send to Terminal

 

GOSUB SendMsg

// Send Response Message

 

ELSE

// ELSE

 

DEBUG InData

// Send the Data

 

ENDIF

// End of IF Statement

 

GOTO Main:

// Start Again

 

SendMsg:

SEROUT TX/RTS, 110, ["This is the BS2p Stamp"]

Return

You now have a pretty fair idea of the capabilities of the SB72 and the amount of work and code needed to provide Ethernet communications to the BS2. Now it’s time to move on to our USB interface. Remember the code for the complete listings are available at www.wordware.com/files/embsys or at www.time-lines.com.

The USB Interface

The USB interface is a very complex topic. Rather than cover the entire topic here, we will instead cover only the necessary items to get our prototype working. If you want to become a USB wizard, go to www.usb.org and download the documentation. The USB 2.0 specification file is over 200 pages in PDF format.

Having said that, we will take the ten-cent tour on how USB works. Electrically USB is a pretty neat interface. It has only four wires, a 5-volt supply line, a positive data line, a negative data line, and a ground wire. Data is transmitted and received by differential signaling, which means that data bytes are sent by inverting signals on the data lines. The host is also known as the root hub and initiates all data transactions. Actual communications occur within frames and a very formal protocol. A client

Chapter 8

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