v1 Stable

Dependencies:   F401RE-USBHost USBHostXpad mbed

main.cpp

Committer:
Ownasaurus
Date:
2016-10-26
Revision:
0:eb2258e8c4b5
Child:
1:3c21da72660d

File content as of revision 0:eb2258e8c4b5:

#include "mbed.h"

extern "C" void my_wait_us_asm (int n);

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx
DigitalInOut data(PA_8);
DigitalIn button(PC_13);

// 0 is 3 microseconds low followed by 1 microsecond high
// 1 is 1 microsecond low followed by 3 microseconds high
unsigned int GetMiddleOfPulse()
{
    // wait for line to go high
    while(1)
    {
        if(data.read() == 1) break;
    }
    
    // wait for line to go low
    while(1)
    {
        if(data.read() == 0) break;
    }
    
    // now we have the falling edge
    // wait 2 microseconds to be in the middle of the pulse, and read. high --> 1.  low --> 0. 
    my_wait_us_asm(2);
    return (unsigned int) data.read();
}

// continuously read bits until at least 9 are read, confirm valid command, return without stop bit
unsigned int readCommand()
{
    unsigned int command = GetMiddleOfPulse(), bits_read = 1;
    
    while(1) // read at least 9 bits (2 bytes + stop bit)
    {
        //my_wait_us_asm(4);
        command = command << 1; // make room for the new bit
        //command += data.read(); // place the new bit into the command
        command += GetMiddleOfPulse();
        command &= 0x1FF; // remove all except the last 9 bits
        
        bits_read++;
        
        if(bits_read >= 9) // only consider when at least a whole command's length has been read
        {
            if(command == 0x3 || command == 0x1 || command == 0x1FF)
            {
                // 0x3 = 0x1 + stop bit --> get controller state
                // 0x1 = 0x0 + stop bit --> who are you?
                // 0x1FF = 0xFF + stop bit --> reset signal
                command = command >> 1; // get rid of the stop bit
                return command;
            }
        }
    }
}

void write_1()
{
    data = 0;
    my_wait_us_asm(1);
    data = 1;
    my_wait_us_asm(3);
    //data = 0;
    //pc.printf("1");
}

void write_0()
{
    data = 0;
    my_wait_us_asm(3);
    data = 1;
    my_wait_us_asm(1);
    //data = 0;
    //pc.printf("0");
}


void sendStop()
{
    data = 0;
    my_wait_us_asm(1);
    data = 1;
}

void sendByte(unsigned char b)
{
    //pc.printf("\nTrying to send 0x%x\n",b);
    for(int i = 0;i < 8;i++) // send all 8 bits, one at a time
    //for(int i = 7;i >= 0;i--) // send all 8 bits, in reverse order, one at a time
    {
        if((b >> i) & 1)
        {
            write_1();
        }
        else
        {
            write_0();
        }
    }
}

int main()
{
    __disable_irq();    // Disable Interrupts
    pc.printf("SystemCoreClock = %d Hz\n", SystemCoreClock);
    
    while(1)
    {
        // Set pin mode to input
        data.input();
        
        // Read command
        unsigned int cmd = readCommand();
        
        //pc.printf("Read command: %u\n", cmd);
        
        my_wait_us_asm(2); // wait a small amount of time before replying
        
        // Set pin mode to output
        data.output();
        
        switch(cmd)
        {
            case 0x00: // identity
            case 0xFF: // reset
                //pc.printf("Received identity ");
                // reply 0x05, 0x00, 0x02
                sendByte(0x05);
                sendByte(0x00);
                sendByte(0x02);
                sendStop();
                break;
            case 0x01: // poll for state
                if(!button.read()) // simulate Start pressed
                {
                    sendByte(0x08); // I think this is start, 4th bit
                    sendByte(0x00);
                    sendByte(0x00);
                    sendByte(0x00);
                    sendStop();
                    break;
                }
                else
                {
                    // respond with controller state
                    sendByte(0x00);
                    sendByte(0x00);
                    sendByte(0x00);
                    sendByte(0x00);
                    sendStop();
                    break;
                }
        }
    }
}