Bailey O.H.Embedded systems.Desktop integration.2005
.pdf
x |
Contents |
|
|
Before the Installation. . . . . . . . . . . . . . . . . . . . . . |
291 |
After the Installation . . . . . . . . . . . . . . . . . . . . . . |
292 |
Testing the SB72 Setup . . . . . . . . . . . . . . . . . . . . . |
292 |
Debugging an SB72 Program . . . . . . . . . . . . . . . . . . |
301 |
Testing Ethernet Communications . . . . . . . . . . . . . . . . |
303 |
Connecting the SB72 to the BASIC Stamp . . . . . . . . . . . . |
309 |
The USB Interface . . . . . . . . . . . . . . . . . . . . . . . . . |
319 |
Implementing the USB Circuits . . . . . . . . . . . . . . . . |
320 |
The Parallax USB2SER Device . . . . . . . . . . . . . . . . . |
327 |
The Maxim/Dallas Hardware Interfaces . . . . . . . . . . . . . . . |
331 |
The 1-Wire Interface Hardware . . . . . . . . . . . . . . . . . . |
331 |
The 3-Wire Hardware Interface . . . . . . . . . . . . . . . . . . |
334 |
Adding RS-232 Communications . . . . . . . . . . . . . . . . . . . |
341 |
Finishing Touches for Our BS2p Prototype . . . . . . . . . . . . . |
343 |
Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . |
344 |
Chapter 9 The PIC Prototype . . . . . . . . . . . . . . . . . . . . . . 345
Similarities between the BASIC Stamp and the PIC . . . . . . . . 346 Differences between the BASIC Stamp and the PIC . . . . . . . . 347 Types of PIC Microcontrollers and Development Languages . . . . 348 The PIC Development Cycle . . . . . . . . . . . . . . . . . . . . . 349 Development Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 351 What Is HIDmaker? . . . . . . . . . . . . . . . . . . . . . . . . . . 353 The dsPIC30F2010 Processor . . . . . . . . . . . . . . . . . . . . 356 The Ethernet Interface . . . . . . . . . . . . . . . . . . . . . . . . 358 The dsPIC Code Development Environments . . . . . . . . . . . . 359
The MPLAB IDE . . . . . . . . . . . . . . . . . . . . . . . . . . 359 The RS-232 Interface . . . . . . . . . . . . . . . . . . . . . . . . . 360 The dsPIC Peripheral Libraries . . . . . . . . . . . . . . . . . . . . 361 The SPI Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 The I2C Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 The Dallas 1-Wire Interface . . . . . . . . . . . . . . . . . . . . . . 373 Implementing USB and Ethernet Communications . . . . . . . . . 374 The PIC 16C745 USB Chip . . . . . . . . . . . . . . . . . . . . . . 375 The Microchip USB Developer Kits . . . . . . . . . . . . . . . . . 377 The Microchip USB Engine . . . . . . . . . . . . . . . . . . . . . . 378 Defining a USB Device to the Host System . . . . . . . . . . . . . 380 Finishing the Communications Tasks. . . . . . . . . . . . . . . . . 385 The NetBurner SB72 Board. . . . . . . . . . . . . . . . . . . . . . 386
The TTL Serial Routine . . . . . . . . . . . . . . . . . . . . . . 390 The Finished Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Chapter 10 The PSoC Prototype . . . . . . . . . . . . . . . . . . . . . 397
What Is the PSoC? . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Comparing the PIC and the PSoC. . . . . . . . . . . . . . . . . . . 398
Contents |
xi |
|
|
The PSoC Design Process. . . . . . . . . . . . . . . . . . . . . . . 400 The PSoC Designer . . . . . . . . . . . . . . . . . . . . . . . . . . 400 PSoC Assembly Language. . . . . . . . . . . . . . . . . . . . . . . 402 The PSoC C Compiler . . . . . . . . . . . . . . . . . . . . . . . . . 402 Available Hardware Development Tools . . . . . . . . . . . . . . . 403 The EZ-USB Chip . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Development Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 407 Using the PSoC Designer . . . . . . . . . . . . . . . . . . . . . . . 408
The PSoC I2C Function Library . . . . . . . . . . . . . . . . . . 419 Adding User Modules to the PSoC Designer . . . . . . . . . . . 420 The PSoC 1-Wire Interface . . . . . . . . . . . . . . . . . . . . . . 420 The PSoC SPI User Module. . . . . . . . . . . . . . . . . . . . . . 421 The PSoC RS-232 Interface . . . . . . . . . . . . . . . . . . . . . . 423 The Cypress EZ-USB Chip . . . . . . . . . . . . . . . . . . . . . . 429 The EZ-USB UARTs . . . . . . . . . . . . . . . . . . . . . . . . 438 The Ethernet Interface . . . . . . . . . . . . . . . . . . . . . . . . 439 The NetBurner SB72 Board . . . . . . . . . . . . . . . . . . . . 439 PSoC Schematics . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Hardware Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Chapter 11 Cross-Platform Application Development . . . . . . . . . 447
Ethernet, TCP/IP, and UDP . . . . . . . . . . . . . . . . . . . . . . 448 RS-232 Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 USB Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 The User Application Layer . . . . . . . . . . . . . . . . . . . . . . 450 Commercial Compilers . . . . . . . . . . . . . . . . . . . . . . . 450 Delphi/Kylix . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 C++Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 C++BuilderX . . . . . . . . . . . . . . . . . . . . . . . . . . 451 GNU Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 Application Frameworks . . . . . . . . . . . . . . . . . . . . . . 451 Common Application Layers . . . . . . . . . . . . . . . . . . . . 452 The Device Interface Layer . . . . . . . . . . . . . . . . . . . . . . 452 The Ethernet Software Interface . . . . . . . . . . . . . . . . . . . 453 Welcome to the World of Sockets . . . . . . . . . . . . . . . . . 453 The Standard Function Library . . . . . . . . . . . . . . . . . . 454 Alternative Cross-Platform Choices. . . . . . . . . . . . . . . . 455 Visual vs. Code Solutions . . . . . . . . . . . . . . . . . . . . . . . 456 Socket Implementation . . . . . . . . . . . . . . . . . . . . . . . 458 Platform-Independent USB Development . . . . . . . . . . . . . . 459 Comparing Delphi/Kylix to C++BuilderX . . . . . . . . . . . . 460 The End User Application Layer . . . . . . . . . . . . . . . . . . . 461 Delphi/C++Builder/Kylix . . . . . . . . . . . . . . . . . . . . . 461 C++BuilderX. . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Mono and the .NET Framework . . . . . . . . . . . . . . . . . . 462
xii |
Contents |
|
|
wxWidgets and DialogBlocks . . . . . . . . . . . . . . . . . . . 462 It’s Time to Develop the User Application . . . . . . . . . . . . 462 The Delphi Development Effort . . . . . . . . . . . . . . . . 463 The wxWidgets Development Effort . . . . . . . . . . . . . . 465 Distributing the End Results . . . . . . . . . . . . . . . . . . 467
The Sky Is the Limit . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Foreword
Oliver Bailey has taken a unique approach in this book by using a real product design process, complete with imaginative requests from a marketing department and managers. Early in the book, I identified with many circumstances that regularly occur in our office when we’re considering a new product. It reminded me that there’s so much more to product design than a schematic, some microcontroller code, and a printed circuit board. It’s a reminder that designing the prototype is 90 percent of the fun and 10 percent of the work, and that you can swap these two numbers to bring a product to market. With his business-minded approach, Oliver challenges us to put thought into the software integration, operating systems, hardware design, and marketing department requests up front. The result is that the prototype will be more complete, and completing the project will involve less feature creep. This is what it will take, because embedded developers are always looking at desktop integration alternatives.
Many books dealing with microcontrollers are very firmware (microcontroller code) and hardware (circuit design) oriented. They provide code examples, a schematic, and a description of how the design should work. They rarely cover the hardware connection solutions (USB, serial, Ethernet), not to mention the variety of operating systems and drivers for the hardware. Knowing more about microcontrollers than operating systems, I spent all of my time reviewing the sections on designing cross-platform user applications and their programming environments. From reading microcontroller discussion forums it is clear that most users need support with the subject. Now they have it in one place. These considerations are far more important to embedded developers than they used to be.
xiii
xiv |
Foreword |
|
|
We have lots of confidence in Oliver’s ability to research and recommend the right technical solution for the project. For example, Oliver spent many days using the USB to serial chips with different operating systems. The result is that he chose a common implementation with FTDI and off-the-shelf drivers. In our office we had gone through the same process a few months earlier and spent at least 40 hours before we came to the same design conclusions. Use this book to help you choose and develop the interface, because you can have confidence the research was done correctly before any conclusions were drawn.
This book is also very timely. Oliver provides overviews of the most popular new USB solutions and several microcontrollers. And with the GNU tools maturing and developers looking at alternative operating systems more each year, I’d say this book was just in time.
Oliver’s triple-decade experience is now yours. Embedded Systems: Desktop Integration fills a void in the industry by presenting the communication solutions between microcontrollers and PCs, by developing cross-platform executables from a single source code base, and putting it all together in one place. Writing this book was clearly no easy task considering the scope, but it all came together in a way that we can understand. His research will save us a lot of time on our next project.
Finally, Oliver’s writing style is really easy to follow. You’ll be part of the project and appreciate the conversational approach he uses along the way. There’s no stuffy desktop-reference book approach anywhere in this book.
Ken Gracey
Parallax, Inc.
Preface
If you ever want to realize how much you don’t know about something, write a book about the subject. This has been a humbling experience but a fun adventure as well. For me it all started when I was doing machine tool design in LaPorte in the late ’70s. I needed a tube for a black and white TV so I went to the local Radio Shack store. While I was there I noticed this ugly gray monitor and keyboard just sitting in a corner playing Pong. I asked the manager what it was, and he explained it was a computer that was fully programmable. That Radio Shack became a regular hangout for me both before and after I bought that old TRS-80 (which I still have).
What happened next was nothing short of a miracle. LaPorte, Indiana, in those days was a small town of about 10,000 that sits on the Indiana-Michigan line. Rich in heritage but nestled in the corn fields away from the big city and big business. Not long after I bought the TRS-80, a blind ad ran for a programmer who had TRS-80 experience. I applied for the job and as it turned out the employer was none other than Howard W. Sams Publishing, a company moving into the software industry. Never would I have thought that a software opportunity would present itself in a small, sleepy town like LaPorte, Indiana. But it did and the rest is history. From 1979 to 1986 I developed operating systems, accounting software, programmer tools, and numerous other products. In 1986 I began developing embedded machine control systems and have either been on my own or owned companies since that time. Over the years I have written hundreds of articles, spoken at conferences, and even appeared on video for a variety of topics.
Over the years, the software industry and America has changed. In recent years I’ve decided to leave the software and hardware industry to move into something else. So I decided to
xv
xvi |
Preface |
|
|
share my experience and insight with the software and hardware community before moving on. This is the first in a series of books that cover topics such as embedded systems, communications, host application development, Java, Internet communications, and wireless embedded systems. I hope you enjoy reading this book as much as I have writing it.
Acknowledgments
While I may have written the content, there are a lot of people who supported this effort. Wes Beckwith and Beth Kohler at Wordware Publishing deserve a lot of thanks for their efforts. Karen Giles, Troy Kitch, and John Ray Thomas at Borland; Ken Gracey and Erik Wood at Parallax; Tom Kenney at NetBurner; Jeff Schmoyer at microEngineering Labs; Eric Sells, Brian Schneider, and Dan Butler at MicroChip; Corey Wilner and Louis Bolma at Cypress Semiconductor; Pavel at PicAnt.com; Dr. Robert Miller at Trace Systems; Sara and the support staff at HI-TECH Software; Susie Penner at Trolltech; my old friends at Custom Computer Services (CCS); the folks at MotherBoards Direct (thanks for your help, Dave); Jack Schoof at NetMedia; Alan Towne at Saelig; the fine people at Sysvestor; Matthew Daughtrey at Embedded Ethernet; Frank Miller at Pulsar; Jan Axelson; the folks at Linx Technologies; Keith Dingwall of FTDI; Adam Lintott of Matrix Orbital; Dave Smiczek of Dallas Semiconductor; and my reviewers, John Wilczynski and Keith Kehl.
xvii
Introduction
In this book, we design a thermostat that interfaces to a host system through RS-232, USB, and Ethernet. To make things fun, the device layer and user applications have to run on Windows, Linux, and FreeBSD. We build three prototype thermostats using the BASIC Stamp, PIC Microcontroller, and PSOC Microcontroller. We implement RS-232 using the old-fashioned line drivers, and then implement USB using three different USB components. We also implement Ethernet using two different components. In the end, you’ll have experience with three different microcontrollers, two different Ethernet controllers, and three different USB interfaces, three different compilers and user interface builders, and five different embedded compilers and languages.
The book is divided into two parts. Part I includes Chapters 1–6 and focuses on project design. Part II includes Chapters 7–11 and discusses the implementation of the project.
Intended Audiences
There is something for everyone in this book. If you’re new to electronics, then go through all of Chapters 1–8. If you have some experience, I would suggest reading Chapters 1–6, 7, and 9, as building the PIC-based prototype is more challenging than the BASIC Stamp for someone with prior electronics expereince. If you’re a master of electronics, then I suggest sinking your teeth into the PSOC prototype. The PSOC is a fun little device that has endless possibilities for configurations.
xviii
Introduction |
xix |
|
|
Writing Style
I write in a conversational style. In other words, I talk to you, not at you. Even though we aren’t face to face, you are actively involved in the conversation. I have tried very hard to present this in a real-world scene as much as possible. You will notice that occasionally I will even change direction or implementation methods or parts because that’s how it is done in the real world. If I take out important information simply because we don’t use it, then I haven’t given you, the reader, the full experience.
What Is Not Covered
I don’t cover programming languages as there are thousands of books devoted to those topics. This is not a book on structured program design methodology. It also doesn’t include a large amount of program listings in the text. Program listings and other information that may change during the life cycle of the book can be downloaded from www.wordware.com/files/embsys or www.time-lines.com.
