USB IR Toy: Sampling mode
From DP
USB IR Toy firmware v1.04+ has an infrared sampling mode that was added for a USB IR Toy WinLIRC plugin. This mode times the duration of infrared pulses and sends the measurements to the computer. It's currently receive-only, but we'll add a compatible transmit feature in a future update.
Contents |
Enter IR sample mode
The IR Toy appears as a serial port. Open it at any speed, 8/N/1.
First send 0x00 (raw byte value 0) to reset from any other mode. It might be good to send it 5 times to be sure it's out of SUMP mode too.
Next, send 's' or 'S' to enter IR sample mode. The IR Toy will respond with the protocol version (currently S01).
Now the bytes start flowing on any IR activity.
Timing information format
IRsample mode will spit out timing measurements for any IR activity it detects. 0xffff is sent after there hasn't been any activity for 1.7seconds.
The protocol is very simple. It might need to be updated in the future (see potential issues).
| pulse1-high8 | pulse1-low8 | blank1-high8 | blank1-low8 | ... | blankn-high8 | blankn-low8 |
The first IR pulse starts a timer that measures length of the pulse. The length is returned as a 16bit count, high byte first. Multiply the raw 16bit timer value by 21.3333us to determine the actual length in microseconds.
The IR Toy continues to spit out measurements of each pulse and the blank space between. Sampling stops when there is no IR change for a full (0xffff) period of 1.7seconds. This will hopefully end during a blank period, but could be a pulse period if an IR jammer were in use. After a full period of no change the IR Toy 'sleeps' and won't send any more data until the beginning of the next IR pulse.
Calculate the duration in microseconds (us)
To get the actual width of each pulse or space:
- Combine the high and low byte
- Multiply by 21.3333us
Here's an example:
{00}{2B}{00}{28}{00}{2A}{00}{27}{00}{2B}{00}{28}{00}{2A}
{00}{27}{00}{2B}{00}{27}{00}{2A}{00}{28}{00}{54}{00}{51}
{00}{2B}{00}{28}{00}{54}{00}{51}{00}{54}{00}{51}{00}{2A}{FF}{FF}
- 002b (first pulse duration) = 43 = 43 * 21.3333us = 917.3319us
- 0028 (first blank duration) = 40 = 40 * 21.3333us = 853.332us
- 002a (second pulse duration) = 42 = 42 * 21.3333us = 895.9986us
- ... 0xff 0xff = final blank space of 1.7seconds, end of data sequence
The measurements are all pretty close to the 889us bit period of an RC5 remote. We could probably improve accuracy by increasing the resolution of the timer measuring the pulse period to 10us or less. We won't worry about this unless there are problems with the current scheme, but it could easily be made adjustable.
Error codes
If the IR Toy can't keep up with the IR data, or the USB connection gets congested, it returns 0xff six times. After the error code it resets and waits for the next infrared pulse before sampling again.
Potential issues
Which byte is which? Tracking the byte stream.
There's no easy way to sync to the byte stream unless you track it from the start or wait for the first 0xff 0xff terminator. This probably isn't a huge deal in practice, just reset (0x00, 'S') and track the data from the beginning.
Alternatively, we could update the protocol to use a bit of the high byte to flag pulse and blank periods. The issue of determining which bytes in the stream are the high and low 8bits remains.
How to detect end of data? The long IR Toy timeout.
The IR Toy will send the 0xff 0xff terminator flag after no pin change is detected for one timer period, but that is nearly two seconds. A program will be really unresponsive if it depends on the terminator sequence to start processing the data.
Another issues is that the IR Toy doesn't care if the timeout happens during a pulse or blank period, and it has no ability to let us know a pulse is longer than 1.7seconds. A continuous IR pulse, like an IR jammer, would really mess up the bytestream. We made the IR Toy somewhat robust against this situation by syncing only to the start of an IR pulse.
Commands
By default the IR sample mode just sits and spits out infrared signal timing data according to the protocol described above. There are also commands that can be used to transmit data, or configure various aspects of the IR Toy.
0x00 Reset (returns to remote decoder mode) 0x01 RESERVED for SUMP RUN 0x02 RESERVED for SUMP ID 0x03 Transmit (FW v07+) 0x04 Frequency report (reserved for future hardware) 0x05 Setup sample timer (FW v07+) 0x06 Setup frequency modulation timer (FW v07+) 0x10 LED mute on (FW v07+) 0x11 LED mute off (FW v07+) 0x12 LED on (FW v07+) 0x13 LED off (FW v07+) 0x20 Use little endian byte format (FW v07+) 0x21 Use big endian byte format (FW v07+)
Commands available from raw sampling mode.
Reset (0x00)
Returns to IRman compatible decoder mode.
All IR Sample mode settings are cleared on exit. LED settings are cleared on exit and do not apply outside IR sample mode.
SUMP RUN and ID (0x01, 0x02)
Reserved if IR sample ever becomes the default mode and needs to support these SUMP commands.
Transmit (0x03)
| 0x03 | pulse1-high8 | pulse1-low8 | blank1-high8 | blank1-low8 | ... | blankn-high8 (0xff) | blankn-low8 (0xff) |
- Begins with 0x03, then send the same format as the timing data described above.
- Transmit ends on no data, or 0xffff for the IR off (blank) pulse (same as receive protocol).
- After transmit ends, send 0x03 again prior to the next transmission.
- LED is on during transmit
- The transmitting LED activates the IR Toy receiver, but receive is not active while transmitting. There is a minimum 426uS delay after transmitting ends before receive resumes. This delay this gives the receiver time to recover, and prevents self-inflicted data packets.
03 00 2C 00 27 00 2A 00 27 00 2B 00 27 00 2A 00 27 00 2C 00 27 00 2A 00 27 00 54 00 51 00 2C 00 27 00 54 00 51 00 54 00 51 00 2A FF FF
This example is PLAY on an RC5 remote control. 0x03 tells the IR toy to expect a packet to transmit. The data follows, ending with 0xff 0xff.
Note that the data packet, including 0xff 0xff at the end, was just copied verbatim from the receive mode.
Frequency report (0x04)
This command is for future hardware only.
This command gives information on the raw, modulated infrared signal. This can be used to find the frequency of the remote control signal.
The count is only accurate if the remote control is held within a centimeter of the detector.
| t1-high8 | t1-low8 | t2-high8 | t2-low8 | t3-high8 | t3-low8 | count-high8 | count-low8 |
Send 0x04 to get the raw frequency data for the previous infrared signal.
- t1 H/L - start timer at second rising edge of signal
- t2 H/L - timer at third rising edge
- t3 H/L - timer at fourth rising edge
- count H/L - total count of infrared modulated signal light pulses since last 0xffff
The measurements are taken from the beginning of the IR signal.
The measurement is not reacquired until the beginning of the next signal after the 0xffff terminator.
{00}{F4}{02}{43}{03}{91}{01}{B3}
- 0x00f4 - start timer
- 0x0243 - timer 1
- 0x0391 - timer 2
- 0x01b3 - total count of infrared modulated light pulses
This is an example of a reply to the 0x04 command.
- PIC timer Period 1 count = 0x0243-0x00f4=0x14F (334)
- PIC timer Period 2 count = 0x0391-0x0243=0x14E (333)
- Total infrared pulse count = 0x01b3 = 435
The timer counts are taken on the second, third, and fourth rising edge of the raw infrared signal. Subtract 2 from 3, and 3 from 4 to determine the period of the modulated IR light.
The total count is the total number of pulses. The maximum is 0xffff, and the counter will roll over to 0 after a long enough IR pulse.
(1/12000000)* PIC timer count = (1/12000000)*334 = 0.0000278333s
The PIC has a 12MHz internal clock signal, so the actual period is 0.0000278333s. The measurement is within +0.20% of the actual 0.0000277778s 36KHz period.
Setup sample timer (0x05)
| 0x05 | prescaler value |
The sample timer is 21.3333us by default. It can be shorter by modifying the prescaler for the timer.
| Setting | Prescale value | Sample timer |
|---|---|---|
| 111 (0x07) | 1:256 | 21.333uS |
| 110 (0x06) | 1:128 | |
| 101 (0x05) | 1:64 | |
| 100 (0x04) | 1:32 | |
| 011 (0x03) | 1:16 | |
| 010 (0x02) | 1:8 | |
| 001 (0x01) | 1:4 | |
| 000 (0x00) | 1:2 |
Setup transmit modulation (0x06)
| 0x06 | PR2 value | don't care byte |
Configures the modulation frequency of the IR transmitter.
IR sample mode starts with a default 36kHz infrared transmit modulation frequency. The rate can be changed with the setup modulation command, followed by a byte that sets the pulse-width modulator match value (PR2), and a don't care byte.
- First, calculate the timer PR2 value that gives the closest frequency. The best online tool we've found is this PWM Calculator.
- Setup the PR2 calculator with the USB IR Toy constants (the clock (fosc) is 48Mhz, duty cycle is always 50%, the prescaler is fixed at 4x). Enter your desired frequency in Hz (eg 38400), and click calculate.
- Choose the closest frequency from the table with a 4x prescaler. The values won't match exactly, but IR receivers usually work over a range of modulations, from roughly 30kHz to 40kHz.
- The USB IR Toy currently uses a fixed 4x prescaler, so choose a value with a 4x timer2 prescaler. The prescaler could be made adjustable in a future firmware if more flexibility is needed.
Send the IR Toy PR2. For 38.4kHz the PR2 value is 0b01001101, or 77 (0x4D). The complete command to send to the IR Toy is 0x06 0x4d 0x00:
- 0x06 is the setup TX command
- 0x4d is the PR2 value from the table
- 0x00 is a don't care byte (doesn't matter)
New settings take effect immediately. See also the PR2 example for IRIO mode.
LED mute on/off (0x10/0x11)
Enables/disables the LED in IR sample mode. The LED will not blink during receive or transmit. LED settings are cleared on exit and do not apply outside IR sample mode.
LED on/off (0x12/0x13)
Turn the IR Toy LED on/off. Respects the LED mute setting. LED settings are cleared on exit and do not apply outside IR sample mode.
Little endian byte format (0x20)
| high 8 bits | low 8 bits |
Default byte format. 16bit RX/TX values are handled high 8 bits first byte, low 8 bits second byte.
Big endian byte format (0x21)
| low 8 bits | high 8 bits |
Reverse the RX/TX byte order. 16bit RX/TX values are handled low 8 bits first byte, high 8 bits second byte.
Taking it further
- transmit underflow error notice
- version
- self test
- make sampling mode the default mode
See the USB IR Toy WinLirc how to.
