can a usb port be used as a counter? - Page 1
Forum section: electronics / projects

author: sarepairman2
author_rank: Frequent Contributor
timestamp: August 17, 2015, 03:27:03 am
content: Is it possible to use a USB ports signal lines to count pulses from an "analog" device that does not really make any kind of packets or anything like that (so long voltage levels are right)? or does there need to be handshaking/etc going on?

author: exmadscientist
author_rank: Frequent Contributor
timestamp: August 17, 2015, 03:35:27 am
content: Quote from: sarepairman2 on August 17, 2015, 03:27:03 amNo.Quote from: sarepairman2 on August 17, 2015, 03:27:03 amYes.USB is not a simple protocol. It is extremely unfriendly to improper or buggy implementations. It is also quite challenging to getright, and because it is a shared bus, ifyourdevice is flaky, you can take downotherdevices on the same root port.

author: rs20
author_rank: Super Contributor
timestamp: August 17, 2015, 03:36:32 am
content: No.Not with normal device drivers at least; IsupposeI can't rule out certain USB chips having a debug mode built in that you could abuse, or maybe you can hack the device-present detection mechanism somehow. But it's absolutely not normal to do things this way, and how you'd negotiate past the USB hubs built onto your motherboard is beyond me.

author: jeroen79
author_rank: Frequent Contributor
timestamp: August 17, 2015, 10:34:56 am
content: What do you want to use this for?At what frequency will these pulses come in?You could use a USB to serial adapter, a USB I/O board or perhaps a gamecontroller board if it is only for low frequencies and timing isn't critical.Otherwise you should program a suitable microcontroller to do the counting and report the result over USB.

author: compet17
author_rank: Regular Contributor
timestamp: August 17, 2015, 06:59:25 pm
content: If the frequency is low, take an old USB keyboard and rewire one of the keys. Then count the key presses....I once modified an USB mouse for a "who clicks most in one minute" competition with a relais on the mouse button. If I remember right I came up to 150Hz and broke all records

author: Jeroen3
author_rank: Super Contributor
timestamp: August 17, 2015, 08:02:40 pm
content: Well, you could theoretically abuse the cable detect mechanism.

author: electr_peter
author_rank: Unknown
timestamp: August 17, 2015, 08:14:05 pm
content: Answer to original question - no, no way. Accomplishing something like this requires either massive SW and HW modifications to USB bus (which would not be USB any longer) or dedicated MCU serving as an USB device and doing intended function.If only I could get a € for everytime I hear such question... It is very common to ask such thing amongst beginners. Some people think that if you have a PC, you should be able to anything on USB lines via means of software...However, all you get from USB is 5V rail at limited current and D+/D- communication lines. Communication protocol is specific and cannot be changed or directly accessed from PC side. D+/D- is only for USB protocol and nothing else (well, except USB abuse with chargers that is).

author: sarepairman2
author_rank: Frequent Contributor
timestamp: August 19, 2015, 12:37:28 am
content: Quote from: Jeroen3 on August 17, 2015, 08:02:40 pmi would like to know more

author: rs20
author_rank: Super Contributor
timestamp: August 19, 2015, 03:28:17 am
content: Quote from: sarepairman2 on August 19, 2015, 12:37:28 amHere is how USB speed and device detection works:http://www.beyondlogic.org/usbnutshell/usb2.shtmlTheoretically, you could poll the state of the host chip and "infer" something about the voltages on the D-/D+ lines. But there's no guarantee on speed (could only check 10 times a second and the user experience would be perfectly fine) or whether the hardware actually exposes any of this knowledge to the PC (it may just keep dead silent until it successfully establishes a proper USB protocol link). Hence why this is being described as a "theoretical" approach. trying to exploit this for a real application is undoubtedly a terrible idea. I'd be surprised if it were actually possible on a typical PC.

author: Jeroen3
author_rank: Super Contributor
timestamp: August 19, 2015, 06:27:09 pm
content: I'd assume that such a thing is most viable for Linux. You'd be limited to probalbe less than 1 plugin events per 10 seconds or so. Due to the many layers involved.For windows, don't.It's more reliable to get an mbed and create a simple tool to send some data over usb hid or usb uart.

author: ChristofferB
author_rank: Frequent Contributor
timestamp: August 20, 2015, 01:28:28 am
content: If it's not for the sake of seeing if it can be done (if youneedit, for something) - You could probably get a long way with either one of those little USB->serial or USB->parallel adapters. One of the handshaking inputs on either device could presumably do the trick, while keeping it "dumb" -no µcontroller.If you do get it to work on pure USB-please do tell how!-Christoffer

author: sarepairman2
author_rank: Frequent Contributor
timestamp: August 20, 2015, 02:10:39 am
content: Well I wanted like 100 KHz max counter on a regular USB port without the need for a microcontroller on the hardware side.

author: kc8apf
author_rank: Regular Contributor
timestamp: August 20, 2015, 02:50:57 am
content: Quote from: sarepairman2 on August 20, 2015, 02:10:39 amA soundcard line in is much better suited for this. If your signals voltage level is too high, add a voltage divider.

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 20, 2015, 03:30:58 am
content: Quote from: sarepairman2 on August 20, 2015, 02:10:39 amYou cannot talk to USB without a microcontroller.Quote from: kc8apf on August 20, 2015, 02:50:57 amIt seems unlikely that your average sound card would respond decently up to 100 KHz.OTOH, it is cheap and relatively simple to use one of the little boards that interfaces between USB and several raw I/O ports.For example, this SparkFun USB to FIFO Breakout - FT245RL $17.95https://www.sparkfun.com/products/7841

author: kc8apf
author_rank: Regular Contributor
timestamp: August 20, 2015, 04:22:22 am
content: Quote from: Richard Crowley on August 20, 2015, 03:30:58 amI'm not finding any info on interrupt on change capabilities for an FTDI. Polling over USB is going to be CPU intensive and slow. Hitting 1kHZ would surprise me.

author: sarepairman2
author_rank: Frequent Contributor
timestamp: August 20, 2015, 05:14:15 am
content: its more size related then $ related

author: c4757p
author_rank: Super Contributor
timestamp: August 20, 2015, 05:15:47 am
content: MCP2210and other related parts have an interrupt counter that you can read out over USB.

author: matseng
author_rank: Frequent Contributor
timestamp: August 20, 2015, 05:57:01 am
content: Quote from: c4757p on August 20, 2015, 05:15:47 amThat's a cool function, I didn't know that it had that. I'm filing that fact away in the might-be-handy-someday drawer in my brain :-) Thanks.

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 21, 2015, 11:43:10 pm
content: Quote from: sarepairman2 on August 20, 2015, 05:14:15 amYou can't hardly get much smaller than this...Espruino Pico PRODUCT ID: 2621 $24.95http://www.adafruit.com/product/2621

author: rs20
author_rank: Super Contributor
timestamp: August 22, 2015, 12:18:56 am
content: Quote from: Richard Crowley on August 21, 2015, 11:43:10 pmWhat? That'sMASSIVE!Yeah, to be fair that thing can't count pulses. But what itdoesdo is far more complex!