Kjetil's Information Center: A Blog About My Projects

USB IR REMOCON Firmware Replacement

I bought this Bit Trade One USB Infrared Remote Control Kit at an electronics store in Japan, which lets you use a standard TV remote to control a PC. While the device works on Linux, since it presents itself as a standard USB HID device, it can only be configured in Windows. But a bigger problem is that the original firmware contained some kind of bug where part of the TV remote mappings would get corrupted after power cycling.

To get around these limitations and problems I have created a new firmware replacement for the onboard PIC18F14K50 microcontroller. For maximum flexibility I decided to make something that emulates the protocol used by the Dangerous Prototypes USB IR Toy. This means the device can be used together with LIRC on Linux, which offers a lot of advanced functionality. This also moves the configuration aspect to software on Linux instead of needing to re-flash the PIC microcontroller EEPROM. Note that this device also has an IR sender, but I only implemented support for the IR receive part.

You can get the firmware here or the MPLAB project source code here. I have used Microchip's own USB stack for this project, which is licensed under the Apache license. The parts I wrote myself I consider to be released unlicensed directly into the public domain. I'm including the main part of the code here for an easy reference:

#include "system.h"

#include <stdint.h>
#include <string.h>
#include <stddef.h>

#include "usb.h"

#include "app_irtoy_emu.h"
#include "usb_config.h"

static uint8_t readBuffer[CDC_DATA_OUT_EP_SIZE];
static uint8_t writeBuffer[CDC_DATA_IN_EP_SIZE];

static bool inSamplingMode = false;
static uint16_t pulseHighLen = 0;
static uint16_t pulseLowLen = 0;
static uint16_t pendingPulse = 0;
static bool endOfTransmission = true;
static bool waitForEOT = false;

void samplingModeSetup(void)
{
    /* Set variables to known state. */
    pulseHighLen = 0;
    pulseLowLen = 0;
    pendingPulse = 0;
    endOfTransmission = true;
    waitForEOT = false;

    T0CON = 0b11000000; /* Enable Timer0 as 8-Bit with no scaling. */

    RCONbits.IPEN = 1; /* Enable priority levels on interrupts. */
    INTCON2bits.TMR0IP = 1; /* Make Timer0 a high priority interrupt. */
    INTCONbits.TMR0IF = 0; /* Clear Timer0 interrupt flag. */
    INTCONbits.TMR0IE = 1; /* Enable Timer0 interrupts. */
    INTCONbits.GIEH = 1; /* Make sure high priority interrupts are enabled. */
    INTCONbits.GIEL = 1; /* Make sure low priority interrupts are enabled. */
}

void samplingModeService(void)
{
    uint8_t numBytesRead;

    numBytesRead = getsUSBUSART(readBuffer, 1);
    if (numBytesRead == 1 && readBuffer[0] == 0x00) /* Reset */
    {
        /* Disable Timer0 interrupts. */
        INTCONbits.TMR0IE = 0;
        inSamplingMode = false;
        return;
    }

    INTCONbits.TMR0IE = 0;
    if (pendingPulse > 5)
    {
        writeBuffer[0] = (uint8_t)(pendingPulse / 256);
        writeBuffer[1] = (uint8_t)(pendingPulse % 256);
        putUSBUSART(writeBuffer, 2);
        pendingPulse = 0;
        endOfTransmission = false;
        waitForEOT = true;
    }

    if ((waitForEOT == true) && (endOfTransmission == true))
    {
        writeBuffer[0] = 0xFF;
        writeBuffer[1] = 0xFF;
        putUSBUSART(writeBuffer, 2);
        waitForEOT = false;
    }
    INTCONbits.TMR0IE = 1;
}

void APP_IRToyEmulationInterrupt(void)
{
    if (INTCONbits.TMR0IF == 1)
    {
        /* Check if IR sensor (inverted input) is active. */
        if (PORTCbits.RC7 == 0)
        {
            if (pulseLowLen > 5)
            {
                pendingPulse = pulseLowLen;
            }
            pulseLowLen = 0;

            pulseHighLen += 2;
        }
        else
        {
            if (pulseHighLen > 5)
            {
                pendingPulse = pulseHighLen;
                endOfTransmission = false;
            }
            pulseHighLen = 0;

            if (endOfTransmission == false)
            {
                pulseLowLen += 2;
                if (pulseLowLen >= 0xFFFE)
                {
                    pulseLowLen = 0;
                    endOfTransmission = true;
                }
            }
        }
        INTCONbits.TMR0IF = 0;
    }
}

void APP_IRToyEmulationInitialize()
{   
    line_coding.bCharFormat = 0;
    line_coding.bDataBits = 8;
    line_coding.bParityType = 0;
    line_coding.dwDTERate = 115200;

    /* I/O settings as used in orignal Bit Trade One firmware. */
    TRISB  = 0x00;
    TRISC  = 0x80;
    LATC   = 0xFF;
    ANSEL  = 0x00;
    ANSELH = 0x00;

    /* But turn off IR sender diode output, no support implemented. */
    PORTCbits.RC5 = 0;
}

void APP_IRToyEmulationTasks()
{
    /* If the USB device isn't configured yet, we can't really do anything
     * else since we don't have a host to talk to.  So jump back to the
     * top of the while loop. */
    if (USBGetDeviceState() < CONFIGURED_STATE)
    {
        return;
    }

    /* If we are currently suspended, then we need to see if we need to
     * issue a remote wakeup.  In either case, we shouldn't process any
     * keyboard commands since we aren't currently communicating to the host
     * thus just continue back to the start of the while loop. */
    if (USBIsDeviceSuspended() == true)
    {
        return;
    }

    if (inSamplingMode == true)
    {
        samplingModeService();
    }
    else
    {
        if (USBUSARTIsTxTrfReady() == true)
        {
            uint8_t numBytesRead;
            numBytesRead = getsUSBUSART(readBuffer, 1);

            if (numBytesRead == 1)
            {
                switch (readBuffer[0])
                {
                case 'S': /* IRIO Sampling Mode */
                case 's':
                    writeBuffer[0] = 'S'; /* Answer OK */
                    writeBuffer[1] = '0';
                    writeBuffer[2] = '1';
                    putUSBUSART(writeBuffer, 3);
                    samplingModeSetup();
                    inSamplingMode = true;
                    break;

                case 'V': /* Acquire Version */
                case 'v':
                    writeBuffer[0] = 'V'; /* Answer OK */
                    writeBuffer[1] = '9'; /* Hardware Version */
                    writeBuffer[2] = '9'; /* Firmware Version High */
                    writeBuffer[3] = '9'; /* Firmware Version Low */
                    putUSBUSART(writeBuffer, 4);
                    break;

                default: /* All the rest is unsupported! */
                    writeBuffer[0] = '?';
                    putUSBUSART(writeBuffer, 1);
                    break;
                }
            }
        }
    }

#if defined(USB_POLLING)
    USBDeviceTasks();
#endif
    CDCTxService();
}
          


To use it in LIRC, setup lirc_options.conf similar to the IR Toy using:

driver = irtoy
device = /dev/ttyACM0
          


Topic: Scripts and Code, by Kjetil @ 23/01-2021, Article Link