Chapter 3 – The summation and consideration
First and foremost, I learned that USB is a bitch.
Compared to a RS232 serial port and a decent terminal program, USB does not suffer the hobbyist gladly. There are no basic, free tools designed to read and write to USB like there are myriad COM-based apps, so it makes it effectively impossible for the firmware developer to test anything out without also having custom desktop software at their disposal. Since many hobbyists who wish to make a USB peripheral are also comfortable with desktop programming this might not seem so bad, but I believe I speak for the majority when I say I do not wish to learn how to use VB, or even worse C (shudder) just to make a USB device that other, far more competent desktop programmers can write for.
This project would have been flatly impossible without the assistance of my co-worker Casey, who wrote a simple app for me that looked for my VID/PID and placed a single byte of my choosing on the first endpoint at the click of a button. Without this piece of software, I could never have known whether or not I ever got it right, and I probably would have wussed out and integrated USB<>serial bridges in all my “USB peripherals”. I realize it still would have been a USB peripheral, but that is just butt-ugly cheating I cannot tolerate. ;) While waiting for inspiration to strike I took a couple days off USB and decided to concentrate on serial and LCD comms, which was a painful reminder of how bad it is on the USB dev side. I found an AWESOME app called Realterm that made fighting through my code mistakes and misconfigurations a breeze to test and evaluate. If there was something like this for USB, the hobbyist dev world would flip out with joy. The world needs an app where you can tell it to look for a specific VID and PID and a specific endpoint and push/pull up to 64 bytes on the bus, maybe with some macro capability to make sending repetitive sequences easier. I will suggest it to another guy I work with who is a PC programmer, as I feel such an app should have the far larger appeal of Windows, and I don’t want to burden Casey with any more Mac development when I personally want the app on PC. It is simply through the luck of my work situation that I had access to a Mac programmer of Casey’s caliber, and a Mac laptop I could run the app on. I use Windows, I need Windows. So does most of the rest of the world.
One of the most important things I learned was that the USBBuffer set up by EasyHID is in fact a 64 byte array, 8 locations of 8 bytes each. Since I was not specifying which byte of the array I wanted to examine for my conditional comparison to light my LEDs, nothing happened. I think someone suggested I try USBBuffer[1] in my code, and when that made my device enumerate and I realized where I had screwed up I went back and changed it to USBBuffer[0] because I was working with only a single byte on the USB bus and I assumed that only the first byte of the array held any data.
The reason the 64-byte array had only one byte filled was because of my testing app. I knew that you could send as much as 64 bytes at once, but since neither myself or the guy who wrote the app were absolutely sure of what we were doing it was made quick-and-dirty with a very limited functionality. It was a good decision to limit both the firmware and the software, and helped insure we weren’t dealing with more variables that we needed to be burdened with.
I must admit to being entirely mystified by the fact that the board would not enumerate until I started properly pulling bytes off the USB bus. As I said, I expected a “device not recognized” error, which would have encouraged me a lot more. Oh well.
I swear, a conspiracy nut could have a field day with the way technology has left the hobbyist just about impotent. From surface mount components to USB hardware/software/firmware interactions to gigahertz wireless digital data links to package lead pitches so small you almost can’t see the gaps… it’s a far cry from the old days when a person could go to a Radio Shack and get all the parts necessary to build a “HiFi stereo system” and Heathkit nuts would build their own 27” televisions, and this was in the FIFTIES!!!! But I digress…
Once it all started working, and I tried to extend the functionality of the LED blinking, I discovered the funny syntax error where I had to drop my desired result to the next line of a If..Then..Else statement. It was not made clear by the manual, but there were examples of it being used that way so I eventually got the clue.
I would say the ONLY reason this works TODAY is due to the contributions of the members of the Proton web forums. I will arrogantly posit that I likely would have figured it out EVENTUALLY, probably after days or weeks of playing around with various commands and concepts, and I would have eventually stumbled into some clue about arrays… but it would have hurt like hell. The forum community is amazing, and absolutely everything I had hoped for when I did my PICBASIC purchasing research and came to the conclusion that Proton’s combination of a great compiler and killer web forums was the way to go!
Back to the PICBASIC USB tutorial TOC