Bailey O.H.Embedded systems.Desktop integration.2005
.pdf
Chapter 9 / The PIC Prototype |
381 |
|
|
that, let’s build a simple USB device with HIDmaker. After installation HIDmaker will reside in a program group under Trace. To start HIDmaker, select Start | Programs | Trace | HIDmaker.
1.Since this is our first USB device, choose NORMAL Device and press Next. A project info screen will appear.
Chapter 9
Figure 9-13
2.In this window we enter a path to our project and a project name. This is followed by a description string and manufacturer string. The check boxes at the far right indicate these will be in the PIC firmware. The next two fields are vendor and product IDs. For development and test purposes you can use the default contents, but you must get your own IDs from usb.org before going to market. The next fields are the release number, which can also be inserted into the firmware, a device serial number, and a USB class name. Input your information into these fields and press the Next button.
3.The next window is where a lot of the work that HIDmaker performs is done. This is a very important window because it defines our descriptors and variables. For our example we
Chapter 9 / The PIC Prototype |
383 |
|
|
Figure 9-15
actually get much more complicated, but let’s walk before we |
9 |
Chapter |
|
6. For our first sample we will define two data items. We can |
|
run. Click the Data Item button, then click inside the |
|
recessed open area. You should see a button appear. Repeat |
|
this process once more for our second data item; your screen |
|
should look like Figure 9-15. To set the parameters for each |
|
item, double-click on that item and a Data Item Properties |
|
dialog will appear. |
|
7. Select Input from the Data Type drop-down dialog box and |
|
Usage ID 1 from the Usage Info box. Check the Data radio |
|
button since this is not a constant and the Variable radio but- |
|
ton since this is not a keyboard array of characters. Select |
|
the Absolute, No Wrap, Linear, No Preferred State, No |
|
Null State, and Bit Field radio buttons. The Report Size in |
|
bits should be 8, or the size of a byte. The Report Count will |
|
be 12 and will contain the string “Hello World” and a null |
|
terminator. Go to the Name field and name this HelloData. |
|
When the host initiates an input interrupt to the PIC, the |
|
message sent will be “Hello World.” Your screen should look |
|
similar to the following: |
|
386 |
Chapter 9 / The PIC Prototype |
|
|
The NetBurner SB72 Board
We covered the NetBurner TCP/IP interface in Chapter 8. In this chapter we will use the same hardware but use the UDP protocol instead. UDP stands for User Datagram Protocol. It differs from TCP/IP in that a session is connectionless. Instead of having a dedicated connection like TCP, UDP allows packets to be sent and received from multiple IP addresses without a dedicated session with any one address.
UDP does not promise that the data sent will ever be received. Since our implementation is on a LAN and is not currently required to be Internet accessible, UDP should provide a high degree of reliability. If we were using this across the Internet we probably would not select UDP, but this protocol has some advantages for working in a LAN environment. One example of this benefit is the ability to have temperature updates broadcast across the network to multiple workstations. If our thermostat were in use in a cold storage warehouse, this would allow anyone to “dial-in” to the thermostat and get real-time temperature and alarm data. Controlling the thermostat could become a nightmare unless we filter who or which terminals can access the control functions. This could be as simple as checking the origination IP address to see if it is an authorized control station. We could also add a password so that the user could be authenticated and allow access by only certain individuals from specific workstations. In short, there are several ways to provide controls prohibiting unauthorized use.
We could use the TCP/IP methods from Chapter 8 and they would work just fine. The TTL serial interface is identical in both situations. The following listing illustates one way of implementing UDP.
388 |
|
Chapter 9 / The PIC Prototype |
|
|
|
|
|
|
if (upkt.Validate()) |
// If we receive valid data, process |
|
|
{ |
|
|
|
WORD Data_len = upkt.GetDataSize(); |
// Data length |
|
|
printf("Got %d UDP Bytes From :", (int) Data_len); |
||
|
ShowIP(upkt.GetSourceAddress()); |
// Data Source IP Address |
|
|
printf("\n"); |
|
|
|
ShowData(upkt.GetDataBuffer(), Data_len); |
// Display actual data |
|
|
printf("\n"); |
|
|
|
} |
|
// End of Valid Packet Data Handler |
} |
|
|
// End of do forever loop |
} |
|
|
// End of UDPRead |
const char *AppName = "UDP Example"; |
|
void UserMain(void *pd) |
|
{ |
|
int UDP_portnum; |
// PortNumber Variable |
IPADDR UDP_addr; |
// UDP Address Variable |
char buffer[80]; |
// Buffer |
InitializeStack(); |
// Initialize the Program Stack |
EnableAutoUpdate(); |
// Enable Auto Update feature |
EnableTaskMonitor(); |
// Enable Task Monitor |
printf("UDP Test \n"); |
// Program Purpose |
printf("Input the port number?\n"); |
// Get Port Number from User |
scanf("%d", &UDP_portnum); |
// Scan and Store |
printf("\nEnter the IP Address to send to?"); |
// Get Target IP address |
buffer[0] = 0; |
// Buffer element = 0 |
while (buffer[0] == 0) |
// No keyboard data, wait |
{ |
|
gets(buffer); |
// else, get Keyboard data |
} |
|
UDP_addr = AsciiToIp(buffer); |
// Convert to dot notation |
// Print Port Chosen |
|
printf("%d UDP Port in use ", UDP_portnum); |
|
ShowIP(UDP_addr); |
// Print IP Address |
printf("\n"); |
|
