15MHz not working with some hubs

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

15MHz not working with some hubs

Post by mojo » Wed Jun 03, 2009 12:53 am

Hi.

I have been looking to upgrade from 12MHz to a 15MHz crystal so I have some more processing time available. Unfortunately, I am having problems getting 15MHz to work reliably.

At 12MHz the device detects on all hubs as well as directly connected to the PC and works properly. At 15MHz, a few hubs are okay but most either produce an "unknown device" or simply do not register that anything is connected.

I have tried a couple of 15MHz crystals, just in case one was a dud, but they seem to be fine. The two test devices are identical, and one was working with a 12MHz crystal before I upgraded it to 15MHz. The code remained exactly the same, I just used AVR Studio to clean all files then "Rebuild All" and flash. I am using the latest version of V-USB.

Any ideas? Perhaps it's a bug in V-USB, but it seems unlikely that it would not have been spotted since it affects four out of five hubs I have tried (three externally powered, two bus powered, no other devices on them). I will try down-grading to a 12MHz crystal tomorrow just to verify the hardware again, but I'm pretty sure it isn't a hardware issue as it works normally directly connected or on that one working hub. I am on Windows XP x64 if it makes any difference.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Re: 15MHz not working with some hubs

Post by mojo » Wed Jun 03, 2009 8:54 pm

Okay, I think I have discovered the problem.

For some reason at 15MHz the start-up time for the AVR needs to be set to set to maximum (16CK/14CK+65ms). I had it set one lower than that (16CK/14CK+4.1ms) and it would not detect on all hubs, even powered ones where you would expect Vcc rise time to be fairly quick. I have a feeling it may be related to the cabling or the length of the cable, because it works fine at the lower setting on my prototype PCBs with a short USB cable, but not on my stripboard prototype with a longer cable. The only other difference is that the PCBs use 100ohm resistors instead of 68ohm for the USB data lines.

This setting also improves compatibility with 12MHz too. I used to have one hub which would never detect anything but an "unknown device", however it now works about 50% of the time.

More testing is required to determine exactly what combination of factors affects this.

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Re: 15MHz not working with some hubs

Post by christian » Fri Jun 05, 2009 5:44 pm

This may also be a voltage level issue. Please check the idle voltage on D-, it should be close to 3.3 V.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Re: 15MHz not working with some hubs

Post by mojo » Fri Jun 05, 2009 9:00 pm

I checked and I get 2.8V at idle. I am using 3.6V zeners to clamp it. It's a bit low but within spec...

I checked some older builds using an ATmega8 and found the same thing, however the mega8 builds work perfectly on all hubs. Some are using a modified version of Igor's code and some AVR-USB as it was then. The newer builds use zeners from two different sources so it seems unlikely they are at issue, but I will try and get some from a different source just in case. I'm sure some of them are the same as the ones used on the mega8 builds though.

I am going to try compiling with an older version of V-USB. V-USB and the AVR in use (newer builds use an ATtiny861, older ones use the mega8) are the only variables that have not been eliminated. I have a tiny461 I can try with an old version of the code (newer one does not fit in 4k). Otherwise, the USB circuitry, the USB cables, the matrix boards and PCBs, the computers/OSs, the hubs and everything else I can think of are the same.

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Re: 15MHz not working with some hubs

Post by christian » Sat Jun 06, 2009 11:32 am

If the version of V-USB makes a difference, please try to find the last version which worked and the first which did not.

Another comment makes me curious: You say that newer versions don''t fit in 4k: I've taken care that no new release exceeds the code size of the previous release. Are you sure you get bigger code with a new version?

Regarding voltage levels: From experience, everything below 3 V may cause troubles. If you can't get the clamping right with zeners, try to use LEDs in series to construct the voltage.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Re: 15MHz not working with some hubs

Post by mojo » Sat Jun 06, 2009 9:19 pm

Okay, I am working my way back down the versions, but it will take a bit of time to find the exact cut off point, if there even is one. It could still be down to some difference between the mega8 and tiny861.

As for the voltage issue, I changed the pull-up resistor from 2k2 to 1k and that pulled the idle voltage to 3.16V. There has been no change in compatibility though. I do still wonder if it is voltage related though, because I have an ATtiny2313 project running at 3.3V which works okay on the hubs which don't work with this new one. It's odd that the mega8 should work too, despite having the same zeners and the same pull-up resistor (2k2).

What do you mean LEDs in series? I'm not sure how you would connect them to the D+/- lines to get the right voltages.

It's a shame I can't run the AVR at 3.3V, but I really need 15MHz to decode a 2MHz bus and for optimal compatibility. All the other devices being connected run at 5V as well so I can't really change.

Oh, I tried another 12MHz crystal, didn't make any difference. Tried 100 and 68 ohm resistors on D+/-, no change.

The code size is down to my code, not V-USB. As I added more functionality to the device I started to run out of room. I'm currently at 5238 bytes with -Os (64% of 8k), but there is more code to add and I want a bootloader as well. It's a good job the AVR has a largely orthogonal instruction set. Without optimisation or with -O3 the flash memory is over 80% full.

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Re: 15MHz not working with some hubs

Post by christian » Sat Jun 06, 2009 9:28 pm

You can try the the very old version on the new chip first. If it does not fix anything, then it's probably not a firmware issue.

Regarding pull-ups, zeners and LEDs: 1.5k is probably better, even if you run on 5 V. You need the additional current for the zeners. But if even 1k does not make a difference, I doubt that it's a voltage level problem. The LEDs can make a substitute for the zeners. Two LEDs (in forward direction) can replace one zener if their forward voltage sums up to ~ 3.3 V.

Since you say that you decode a 2 MHz bus: You are aware that the USB interrupt MUST have highest priority? This means that all other interrupts MUST have SEI as one of their first instructions (best before pushing any registers). And you MUST NOT disable interrupts for longer than a couple of cycles.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Re: 15MHz not working with some hubs

Post by mojo » Sat Jun 06, 2009 11:58 pm

christian wrote:You can try the the very old version on the new chip first. If it does not fix anything, then it's probably not a firmware issue.


Yeah, will try that tomorrow and report back.

Since you say that you decode a 2 MHz bus: You are aware that the USB interrupt MUST have highest priority? This means that all other interrupts MUST have SEI as one of their first instructions (best before pushing any registers). And you MUST NOT disable interrupts for longer than a couple of cycles.


Yes, I am aware of that. Luckily the AVR is the bus master, and the other device only responds only when a command is first written. In theory, a USB interrupt could occur during a bus write/read cycle but all that would happen is the command would either fail or the reply not be read correctly, and the code can deal with either situation. In practice, with a HID gamepad/joystick device you get several ms or more after a poll request to do all your data gathering and I have never actually seen a USB interrupt occur during the time it takes to respond, even when reading 20+ bits from really slow TTL parallel-in serial-out buffers.

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Re: 15MHz not working with some hubs

Post by mojo » Sun Jun 07, 2009 8:32 pm

Okay, I got to the bottom of it!

The problem is to do with the way the port the USB data lines were on was being configured. Originally I had them set as inputs with pull-ups on the 861, and it only worked on some USB ports.

I noticed that the mega8 code had them as outputs pulled low initially. It worked on all hubs I tried, but when I set the 861 that way it would not work at all on anything.

Somewhere along the line my code was changed to use usbDeviceDisconnect() and usbDeviceConnect() for the USB reset in main() rather that in the hardware init section. The key is that you actually have to do the reset condition yourself by changing the relevant bits on the port, which my code was not doing. The fact that it worked on the mega8 is a pure accident down to the way I was using the ports on it.

The wiki states that V-USB will pull D- to ground for you, but it does not work. Looking at the source in usbdrv.h, it only toggles the DDR bit for D-, which of course will not actually work if the bit in the PORT register is 1.

I can't see where it states you must have the bit in the PORT register set to 0 before calling usbDeviceDisconnect(). usbdrv.h implies it if you read the comments. Did I miss it?

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Re: 15MHz not working with some hubs

Post by christian » Sun Jun 07, 2009 8:44 pm

This is indeed not sufficiently documented. Since DDR and PORT = 0 is the default after a CPU reset, there is no need to initialize them in usbInit() or usbDeviceDisconnect(). If you have changed these defaults, you must set them back to 0 before calling any USB functions.

I have updated the comments for usbInit() in usbdrv.h for the next release. Thanks for the hint!

mojo
Rank 1
Rank 1
Posts: 39
Joined: Tue Mar 04, 2008 3:57 pm

Re: 15MHz not working with some hubs

Post by mojo » Tue Jun 09, 2009 12:24 am

Thank you, V-USB an a very nice framework!

Post Reply