Beispiel #1
0
// Application Start
void main(void)
{
    // Prepare Application; MCC generated code
    SYSTEM_Initialize();
    // Custom Initializations
    
    
    
    /*   
     *   watchdog timer 
     *   configured for 131s timeout
     * 
     */
    //WDTCONbits.SWDTEN = 1; // enable watchdog
    //ClrWdt();
    
    
    
    oled_init();         // Dispaly
    USBDeviceInit();	// usb_device.c.  Initializes USB module SFRs and firmware
                        // variables to known states.
    
    
    
    
    USBDeviceAttach();
    // Interrupts Enabled
    PEIE = 1;
    GIE = 1;
    
    // Test Hardware
    moteApp_delayms(300);    
    modemResync();
    moteApp_delayms(300);
//    moteApp_clearCommand();
//    moteApp_delayms(100);   
    
    powerOnStatus = DATAEE_ReadByte(0x00);
#if 0
    if ( powerOnStatus != 0x55)
    {
        test_powerup();
        DATAEE_WriteByte(0x00, 0x55);
    }
#endif

    // Default to USB
    operationType = USB;   // Mode State
    USBapp_handlerState(USB_STARTUP);
    
    
    
    // Initial Display Print
    oled_clear();
    oled_putString("Scriptr IoT-X",0,0);
    oled_putString(" Dev: S1 ",0,1);
    oled_putString("   build 006   ",0,3);
    moteApp_delayms(700);
   
    // Application Loop
    while(1)
    {
        // Handle USB or Solo Mode
        switch (operationType)
        {
            default:        // Invalid
                while(1);   // Hold For Error;
            break;
            case USB:
                USBapp_Handler();   // It is held in here.
                // USB escaped; Cable is unplugged; Change OPERATION mode.
                operationType = MOTE;
                MOTEapp_handlerState(MOTE_STARTUP);   // Initial state for startup
            break;
            case MOTE:
                if (moteHandler() == MOTE_SWAP)
                {
                    // Return to USB Operation
                    operationType = USB;   // Mode State
                    USBapp_handlerState(USB_STARTUP);
                }
            break;
        }
    }
}
Beispiel #2
0
static uint8_t moteApp_GoToSleep(void)
{
    uint8_t status = 0;
    while (SWITCH_2_PORT == 0);     // Hold for Button Release
    modemSleep();
    oled_displayOff(true); 
    ADCON0bits.ADON = 0;       // ADC Off
    INTCONbits.GIE = 0;
    GIE = 0;
    PEIE = 0;
    // Prepare USB Detect
    USB_DET_ANSEL = 1;
    // Prepare IOC to wake us up
    USB_DET_EN = 1;      // Enables CMP1 interrupt.
    SWITCH_1_IOC = 1;
    SWITCH_2_IOC = 1;
    MODEM_WAKE_IOC = 1;
    IOC_ENABLE = 1;
    // Dummy Reads to Establish IOC level
    status = SWITCH_1_PORT;
    status = SWITCH_2_PORT;
    status = MODEM_WAKE_PORT;
    if (periodicState)
    {
        //WDTCONbits.SWDTEN = 1;
    }
    IOC_FLAG = 0;
    USB_DET_FLAG = 0;
    NOP();
    SLEEP();
    NOP();
    // Reconfigure Critical peripherals
    //WDTCONbits.SWDTEN = 0;
    EUSART_FlushBuffer();
    // Disable IOC
    IOC_ENABLE = 0;
    SWITCH_1_IOC = 0;
    SWITCH_2_IOC = 0;
    MODEM_WAKE_IOC = 0;
    PIE2bits.C1IE = 0;
    GIE = 1;

    PIN_MANAGER_Initialize();
    EUSART_Initialize();
    // Process Wake Up Event
    if (IOC_FLAG)
    {   // PushButton or Module IOC woke us
        IOC_FLAG = 0;
        status = 1;
    }
// TODO:
//    else if (Comparator)    // USB EVENT
//    {
//        status = 2;
//    }
    else
    {   // Time woke us. Reload; so wake from sleep is short
        secTicker = 10;
        status = 0;
    }
    USB_DET_ANSEL = 0;
    oled_displayOff(false);
    oled_init();         // Dispaly

    modemResync();
    return status;
}