コード例 #1
0
ファイル: main.c プロジェクト: Athuli7/Microchip
/*************************************************************************
 * Function:        ManageDemoState
 *
 * Preconditions:   The DemoState global variable must be initialized to
 *                  DEMO_STATE_IDLE (0).  (This occurs on reset.)
 *
 * Input:           DemoState (global)
 *                  Actions selected based value of DemoState on function
 *                  entry.
 *
 *                  deviceAddress (global)
 *                  May use device address to access device, depending on
 *                  state.
 *
 *                  DataPacket (global)
 *                  May read data from packet buffer, depending on state.
 *
 * Output:          DemoState (global)
 *                  Updates demo state as appropriate.
 *
 *                  DataPacket (global)
 *                  May cause data in the packet buffer to be updated,
 *                  depending on state.
 *
 * Returns:         None
 *
 * Side Effects:    Depend on state transition
 *
 * Overview:        This routine maintains the state of the application,
 *                  updateing global data and taking actions as necessary
 *                  to maintain the custom demo operations.
 *************************************************************************/
void ManageDemoState ( void )
{
    BYTE RetVal;

    BlinkStatus();

    // Watch for device timeouts
//    if (MSTimerGetTime() > DEMO_TIMEOUT_LIMIT)
//    {
//        if (DemoState == DEMO_STATE_IDLE)
//        {
//            LCDWriteLine( 2, "Awaiting Device" );
//        }
//        else
//        {
//            UART2PrintString( "Device Time-out Error!\r\n" );
//            LCDWriteLine( 2, "Dev Time-out Err" );
//            DemoState = DEMO_STATE_ERROR;
//        }
//    }

    // Watch for device detaching
    if (USBHostGenericDeviceDetached(deviceAddress) && deviceAddress != 0)
    {
        UART2PrintString( "Generic demo device detached - polled\r\n" );
        DemoState = DEMO_INITIALIZE;
        deviceAddress   = 0;
    }

    switch (DemoState)
    {
    case DEMO_INITIALIZE:
        InitLcdMessage();
        DemoState = DEMO_STATE_IDLE;
        break;

    /** Idle State:  Loops here until attach **/
    case DEMO_STATE_IDLE:
        if (CheckForNewAttach())
        {
            DemoState = DEMO_STATE_GET_DEV_VERSION;
        }
        break;

    /** Sequence: Read Dev FW Version **/
    case DEMO_STATE_GET_DEV_VERSION:
        // Send the Read Version command
        DataPacket.CMD = READ_VERSION;
        DataPacket.len = 2;
        if (!USBHostGenericTxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS )
            {
                DemoState = DEMO_STATE_WAITING_VER_REQ;
            }
            else
            {
                UART2PrintString( "1 Device Write Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Write Error " );
            }
        }
        break;

    case DEMO_STATE_WAITING_VER_REQ:
        if (!USBHostGenericTxIsBusy(deviceAddress) )
            DemoState = DEMO_STATE_READ_DEV_VERSION;
        break;

    case DEMO_STATE_READ_DEV_VERSION:
        if (!USBHostGenericRxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 4)) == USB_SUCCESS )
            {
                DemoState = DEMO_STATE_WAITING_READ_VER;
            }
            else
            {
                UART2PrintString( "1 Device Read Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Read Error  " );
            }
        }
        break;

    case DEMO_STATE_WAITING_READ_VER:
        if (!USBHostGenericRxIsBusy(deviceAddress))
            DemoState = DEMO_STATE_VERIFY_DEV_FW_VER;
        break;

    case DEMO_STATE_VERIFY_DEV_FW_VER:
        if (ValidateAndDisplayDeviceFwVersion())
            DemoState = DEMO_STATE_GET_TEMPERATURE;
        else
            DemoState = DEMO_STATE_ERROR;
        break;

    /** Sequence: Read Temperature Sensor Data **/
    case DEMO_STATE_GET_TEMPERATURE:
        // Send the Read Temperature command
        DataPacket.CMD = RD_TEMP;
        DataPacket.len = 2;
        if (!USBHostGenericTxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS)
            {
                DemoState = DEMO_STATE_WAITING_GET_TEMP;
            }
            else
            {
                UART2PrintString( "2 Device Write Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Write Error " );
            }
        }
        break;

    case DEMO_STATE_WAITING_GET_TEMP:
        if (!USBHostGenericTxIsBusy(deviceAddress) )
        {
            DemoState = DEMO_STATE_READ_TEMPERATURE;
        }
        break;

    case DEMO_STATE_READ_TEMPERATURE:
        if (!USBHostGenericRxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 3)) == USB_SUCCESS)
            {
                DemoState = DEMO_STATE_WAITING_READ_TEMP;
            }
            else
            {
                UART2PrintString( "2 Device Read Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Read Error  " );
            }
        }
        break;

    case DEMO_STATE_WAITING_READ_TEMP:
        if (!USBHostGenericRxIsBusy(deviceAddress))
        {
            DemoState = DEMO_STATE_DISPLAY_TEMPERATURE;
        }
        break;

    case DEMO_STATE_DISPLAY_TEMPERATURE:
        DisplayTemperature();
        DemoState = DEMO_STATE_GET_POT;
        break;

    /** Sequence: Read POT Sensor Data **/
    case DEMO_STATE_GET_POT:
        // Send the Read POT command
        DataPacket.CMD = RD_POT;
        DataPacket.len = 2;
        if (!USBHostGenericTxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS)
            {
                DemoState = DEMO_STATE_WAITING_GET_POT;
            }
            else
            {
                UART2PrintString( "3 Device Write Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Write Error " );
            }
        }
        break;

    case DEMO_STATE_WAITING_GET_POT:
        if (!USBHostGenericTxIsBusy(deviceAddress) )
            DemoState = DEMO_STATE_READ_POT;
        break;

    case DEMO_STATE_READ_POT:
        if (!USBHostGenericRxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 3)) == USB_SUCCESS)
            {
                DemoState = DEMO_STATE_WAITING_READ_POT;
            }
            else
            {
                UART2PrintString( "3 Device Read Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Read Error  " );
            }
        }
        break;

    case DEMO_STATE_WAITING_READ_POT:
        if (!USBHostGenericRxIsBusy(deviceAddress))
            DemoState = DEMO_STATE_DISPLAY_POT;
        break;

    case DEMO_STATE_DISPLAY_POT:
        DisplayPot();
        DemoState = DEMO_STATE_SEND_SET_LED;
        break;

    /** Sequence:  Update LEDs **/
    case DEMO_STATE_SEND_SET_LED:
        // Send the set-LED command
        DataPacket.CMD = UPDATE_LED;
        DataPacket.len = 3;
        if (Switch3WasPressed())
        {
            DataPacket.led_num    = 3;  // LED 3 on original PIC18 FS USB board
            DataPacket.led_status = LEDState.bits.b3 ^ 1;
        }
        else if (Switch6WasPressed())
        {
            DataPacket.led_num    = 4;  // LED 4 on original PIC18 FS USB board
            DataPacket.led_status = LEDState.bits.b4 ^ 1;
        }
        else
        {
            DemoState = DEMO_STATE_GET_TEMPERATURE;
            break;
        }
        if (!USBHostGenericTxIsBusy(deviceAddress))
        {
            if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 3)) == USB_SUCCESS)
            {
                DemoState = DEMO_STATE_WAITING_SET_LED;
            }
            else
            {
                UART2PrintString( "4 Device Write Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Write Error " );
            }
        }
        break;

    case DEMO_STATE_WAITING_SET_LED:
        if (!USBHostGenericTxIsBusy(deviceAddress) )
            DemoState = DEMO_STATE_READ_SET_LED_RESP;
        break;

    case DEMO_STATE_READ_SET_LED_RESP:
        if (!USBHostGenericRxIsBusy(deviceAddress))
        {
            DataPacket.CMD = CMD_INVALID;
            if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 1)) == USB_SUCCESS)
            {
                DemoState = DEMO_STATE_WAITING_LED_RESP;
            }
            else
            {
                UART2PrintString( "4 Device Read Error 0x" );
                UART2PutHex(RetVal);
                UART2PrintString( "\r\n" );
                LCDWriteLine( 2, "Dev Read Error  " );
            }
        }
        break;

    case DEMO_STATE_WAITING_LED_RESP:
        if (!USBHostGenericRxIsBusy(deviceAddress))
            DemoState = DEMO_STATE_UPDATE_LED_STATE;
        break;

    case DEMO_STATE_UPDATE_LED_STATE:
        if (DataPacket.CMD == UPDATE_LED)   // CMD updated by read from device
        {
            if (DataPacket.led_num == 3)    // led_num left-over from set-LED command send
            {
                LEDState.bits.b3 ^= 1;
//                mLED_10_Toggle();
            }
            else if (DataPacket.led_num == 4)
            {
                LEDState.bits.b4 ^= 1;
//                mLED_9_Toggle();
            }
        }
        DemoState = DEMO_STATE_GET_TEMPERATURE;
        break;

    /** Error state:  Hold here until detached **/
    case DEMO_STATE_ERROR:                          // To Do: Flash LEDs
        break;

    default:
        DemoState = DEMO_INITIALIZE;
        break;
    }

    DelayMs(1); // 1ms delay

} // ManageDemoState
コード例 #2
0
uint8_t ChipKITUSBGenericHost::Read( uint8_t deviceAddress, void *buffer, DWORD length)
{
    return(USBHostGenericRead(deviceAddress, buffer, length));
}