Ejemplo n.º 1
0
Archivo: PDU.c Proyecto: OfnyaHoo/EV15
void ReadCurrent(char gather) {
    if (gather) {
        //read the first three
        SetPin595(1, 4, ON);
        SetPin595(1, 3, OFF);
        SetPin595(1, 2, OFF);
        SetPin595(2, 4, ON);
        SetPin595(2, 3, OFF);
        SetPin595(2, 2, OFF);
        SetPin595(3, 4, ON);
        SetPin595(3, 3, OFF);
        SetPin595(3, 2, OFF);
        Update();
        CurrentADC[0] = ADC_GetConversion(U5Multisense);
        CurrentADC[2] = ADC_GetConversion(U8Multisense);
        CurrentADC[4] = ADC_GetConversion(U10Multisense);
    } else {
        //read the second three
        SetPin595(1, 4, ON);
        SetPin595(1, 3, OFF);
        SetPin595(1, 2, ON);
        SetPin595(2, 4, ON);
        SetPin595(2, 3, OFF);
        SetPin595(2, 2, ON);
        SetPin595(3, 4, ON);
        SetPin595(3, 3, OFF);
        SetPin595(3, 2, ON);
        Update();
        CurrentADC[1] = ADC_GetConversion(U5Multisense);
        CurrentADC[3] = ADC_GetConversion(U8Multisense);
        CurrentADC[5] = ADC_GetConversion(U10Multisense);
    }
}
Ejemplo n.º 2
0
uint16_t readADC() {
    return (uint16_t)ADC_GetConversion(FFT_CHANNEL);
}
Ejemplo n.º 3
0
static MOTE_RUNNING_T moteRunningProcess(bool changeStates, bool selectButton, bool messageReady, bool sleepEvent)
{
    uint8_t readByte = 0;

    if ((sleepEvent) && (periodicState))
    {
        if (sleepTicker >= PeriodicInSleep) // No Manual Transmission for a time; Do one Auto
        {   
            // Measure Sensors
            uint16_t Traw = 0;
            moteApp_delayms(10);
            for(uint8_t i = 32; i>0; i--)
            {
                light = (light + ADC_GetConversion(Light_channel)) / 2;
            }
            moteApp_delayms(10);
            for(uint8_t i = 32; i>0; i--)
            {
                Traw = (Traw + ADC_GetConversion(Temp_channel)) / 2;
            }
            temperature = ADC_TempConversion(Traw);
            // Do Normal Operation
            sleepTicker = 0;
            oled_clear();
            oled_putString("Issue Perodic TX",0,0);
            oled_putString("On Port #: ",0,1);
            oled_putUint8(randomPortNum,10,1);
            oled_putString("L:       T: ",0,2);
            oled_putUint16(light,3,2);
            oled_putUint8(temperature,12,2);
            oled_putString("(NAV)    (SEL) ",0,3);          
            // Prepare Buffers and data
            moteApp_clearBuffers();
            randomPortNum = TMR2_ReadTimer();
            // Make Sure Port is in allowed Range
            if (!randomPortNum)
                randomPortNum = 5;
            else if (randomPortNum > 223)
                randomPortNum = randomPortNum - 50;
            // Prepare DataBuffer for Tx
            moteApp_add8bToDataBuffer(randomPortNum, 0);
            dataBuffer[3] = 0x20;
            moteApp_add16bToDataBuffer(light, 4);
            moteApp_add8bToDataBuffer(temperature, 9);
            NOP();
            sleepTxOccurances = 0;
            oled_putString("C",15,1);
            sendDataCommand("radio tx ", dataBuffer, 12);
            secTicker = SleepTimeOut - (SleepTimeOut / 3);
            runningState = runningUplinkTransmit;
        }
        else
        {
            sleepTicker++;
            runningState = runningSleep;
            selectButton = true;
        }
    }
    
    moteApp_GoToSleep();

    // Menu Operation
    switch(runningState)
    {
        default:        // Invalid
            while(1);   // Hold for Error
        break;
    }
    return runningState; // no return value needed but we declare one for this function.
}
Ejemplo n.º 4
0
static MOTE_T moteJoiningProcess(bool messageReady, bool switchEvent)
{
    static int ch = 0;
    bool joiningComplete = false;
    static int mode = 0;
    static unsigned char pkt_cnt = 0;
    static unsigned char float_val = 255;
    static bool pending_confirm = false;
    char buf[50];
    static int skip_count = 0;
    

    /*if (switchEvent)
    {
        activeState = moteComFailure;
        while(!SWITCH_2_PORT);
    }*/

    switch (activeState)
    {
        default:        // Invalid
            while(1);   // Hold for Error
        break;
        case moteStartup:
            
            // HACK to block for Espruino
            //EUSART_TX_TRIS = INPUT;
            //while (1) {};
            
            //sendCommand("sys reset");
            
            LED_RED_LAT = 0;
            LED_GREEN_LAT = 0;
            
            
            // flush buffer
            while (MOTEapp_getModemResponse());
            
                    
            sendCommand("sys get ver");
            oled_clear();
            oled_putString("get ver",0,0);
            moteApp_delayms(100);
            activeState = moteReset;
            //activeState = moteStartup;
        break;
        case moteReset:
            if ( messageReady )
            {
                if(strncmp(&modemResonseBuffer[2], "2903", 4) == 0)   // Check 1st [2] Bytes for RN
                {
                    sendCommand("mac set devaddr DEAD00A1");
                    oled_clear();
                    oled_putString("set devaddr",0,0);
                    moteApp_delayms(80);
                    activeState = moteSetDevaddr;
                }
                else
                {
                    oled_clear();
                    oled_putString(modemResonseBuffer,0,3);
                    moteApp_delayms(1000);
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetDevaddr:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    sendCommand("mac set appskey 2B7E151628AED2A6ABF7158809CF4F3C");
                    oled_clear();
                    oled_putString("set appskey",0,0);
                    moteApp_delayms(80);
                    activeState = moteSetAppskey;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetAppskey:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    
                    sendCommand("mac set nwkskey 2B7E151628AED2A6ABF7158809CF4F3C");
                    oled_clear();
                    oled_putString("set nwkskey",0,0);
                    moteApp_delayms(80);
                    activeState = moteSetNwkskey;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetNwkskey:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                   
                    sendCommand("mac set adr off");
                    oled_clear();
                    oled_putString("set adr",0,0);
                    moteApp_delayms(80);
                    activeState = moteSetAdr;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetAdr:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    
                    sendCommand("mac set sync 34");
                    oled_clear();
                    oled_putString("set sync",0,0);
                    moteApp_delayms(80);
                    activeState = moteSetSync;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetSync:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    
                    sendCommand("mac set rx2 8 923300000");
                    oled_clear();
                    oled_putString("set rx2",0,0);
                    moteApp_delayms(80);
                    ch = 0; // start with channel 0
                    activeState = moteSetRx2;
                    //activeState = moteSetChStatus;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetRx2:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    // only enable subband #2
                    sprintf(cmd,"mac set ch status %d %s",
                            ch, 
                            (ch>=8 && ch<=15)?"on":"off");
                    sendCommand(cmd);
                    oled_clear();
                    oled_putString("set ch",0,0);
                    oled_putString(&cmd[18],0,1);                    
                    moteApp_delayms(80);
                    ch++;
                    if (ch<72) {
                        activeState = moteSetRx2;
                    } else {
                        activeState = moteSetChStatus;
                    }
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteSetChStatus:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    moteApp_delayms(200);
                    sendCommand("mac join abp");
                    oled_clear();
                    oled_putString("join abp",0,0);
                    moteApp_delayms(80);
                    activeState = moteJoinAbp;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteJoinAbp:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    // waiting now for "accepted"                    
                    oled_clear();
                    oled_putString("Wait accepted...",0,0);
                    moteApp_delayms(80);
                    activeState = moteWaitAccepted;
                }
                else // Retry Command
                {
                    activeState = moteStartup;
                }
            }
        break;
        case moteWaitAccepted:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "accepted") == 0)
                {     
                    //while (MOTEapp_getModemResponse());
                    
                    // send initial packet
                    
                    sendCommand("mac tx uncnf 1 00000000");
                    oled_clear();
                    oled_putString("ini tx",0,0);
                    moteApp_delayms(150);
                    activeState = moteIniTx;
                    LED_GREEN_LAT = 1;
                }
                else // Retry Command
                {
                    oled_clear();
                    oled_putString("moteWaitAccepted",0,0);
                    activeState = moteStartup;
                    moteApp_delayms(500);
                    LED_RED_LAT = 1;
                }
            }
        break;
        case moteIniTx:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    oled_clear();
                    oled_putString("wait mac_tx_ok",0,0);
                    moteApp_delayms(3000);
                    activeState = moteWaitIniTxOk;
                    LED_RED_LAT = 1;
                }

                else // Retry Command
                {
                    oled_clear();
                    oled_putString("moteIniTx",0,0);
                    activeState = moteStartup;
                    moteApp_delayms(500);
                    LED_GREEN_LAT = 0;
                }
            }
        break;
        case moteWaitIniTxOk:
            //if ( messageReady )
            {
                //if(strcmp(modemResonseBuffer, "mac_tx_ok") == 0)
                {
                    LED_RED_LAT = 1;
                    LED_GREEN_LAT = 0;
                    // next we rest for a while                                        
                    oled_clear();                 
                    oled_putString("Mode: ",0,0);                
                    oled_putString(autoMode?"AUTO  ":"MANUAL",6,0);
                    oled_putString("S1 = auto on/off",0,1);
                    oled_putString("S2 = manual send",0,2);
                    
                    moteApp_delayms(80);
                    ch = 0;
                    activeState = moteRest;
                }
                /*else if (strcmp(modemResonseBuffer, "") == 0) {
                    // glitch where we get an extra return
                    moteApp_delayms(1);
                    LED_GREEN_LAT = 0;
                    moteApp_delayms(1);
                    LED_GREEN_LAT = 1;
                    moteApp_delayms(1);                    
                    activeState = moteWaitIniTxOk;
                    
                }
                else // Retry Command
                {
                    oled_clear();
                    oled_putString("WaitIniTxOk",0,0);
                    oled_putString(modemResonseBuffer,0,2);
                    sprintf(cmd,"buflen=%d",strlen(modemResonseBuffer));
                    oled_putString(cmd,0,3);
                    activeState = moteStartup;
                    moteApp_delayms(1000);
                }*/
            }
        break;
        case moteRest:
            if (autoMode || manualTrigger) {
                ch++;
            }
            moteApp_delayms(100);
            /* check buttons */
            S1 = SWITCH_1_PORT; // 1 == unpressed, 0 = pressed
            S2 = SWITCH_2_PORT;
            if (S1 == 0 && prevS1 == 1) {
                /* rising edge of switch 1 */
                /* alternate mode */
                autoMode = !autoMode;
                oled_putString("Mode: ",0,0);                
                oled_putString(autoMode?"AUTO  ":"MANUAL",6,0);
            }
            if (S2 == 0 && prevS2 == 1) {
                /* rising edge of switch 2 */
                /* schedule a measurement */
                manualTrigger = true;
            }
            prevS1 = S1;
            prevS2 = S2;
            if (ch>20) {
                manualTrigger=false;
                ch = 0;
                // make a measurement
                uint16_t Traw = 0;
                moteApp_delayms(10);
                for(uint8_t i = 32; i>0; i--)
                {
                    light = (light + ADC_GetConversion(Light_channel)) / 2;
                }
                moteApp_delayms(10);
                for(uint8_t i = 32; i>0; i--)
                {
                    Traw = (Traw + ADC_GetConversion(Temp_channel)) / 2;
                }
                temperature = ADC_TempConversion(Traw);
                m10SecTicker = 0;
                
                pkts++;
                
                // display measurement
                oled_clear();
                oled_putString("Mode: ",0,0);                
                oled_putString(autoMode?"AUTO  ":"MANUAL",6,0);
                oled_putString("Light:",0,1);
                oled_putString("Temp :",0,2);
                oled_putString("C",12,2);
                oled_putUint16(light,6,1);
                oled_putUint16(temperature,6,2);                
                oled_putString("Pkts|:",0,3);
                oled_putUint16(pkts,6,3);
                
                
                sprintf(cmd,"mac tx uncnf 1 %04X%04X",
                        pkts,dr);
                sendCommand(cmd);
                
                activeState = moteTxUncnf;
            }
        break;
        case moteTxUncnf:
            //if ( messageReady )
            {
                //if(strcmp(modemResonseBuffer, "ok") == 0)
                {                    
                    oled_putString("Pkts/:",0,3);
                    oled_putUint16(pkts,6,3);
                    moteApp_delayms(80);
                    activeState = moteTxWait;
                }
                /*else 
                {
                    
                    oled_clear();
                    oled_putString("TxUncnf",0,0);
                    activeState = moteStartup;
                    moteApp_delayms(500);
                }*/                
            }
        break;
        case moteTxWait:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "mac_tx_ok") == 0)
                {
                    dr = (dr + 1) % 4; // 0-3
                    sprintf(cmd,"mac set dr %d",dr);                                        
                    sendCommand(cmd);
                    oled_putString("Pkts-:",0,3);
                    oled_putUint16(pkts,6,3);
                    activeState = moteNextDr;
                }
            }
        break;
        
        case moteNextDr:
            if ( messageReady )
            {
                if(strcmp(modemResonseBuffer, "ok") == 0)
                {
                    //oled_putString("Pkts-:",0,3);
                    //oled_putUint16(pkts,6,3);
                    activeState = moteRest;
                }
            }
        break;
        
        case moteDone:
            joiningComplete = true;
            moteApp_delayms(80);
        break;
        case moteComFailure:
            DATAEE_WriteByte(0x00, 0x00);
            while((!SWITCH_1_PORT) && (!SWITCH_2_PORT));
            moteApp_delayms(40);
            RESET();
            // Do Nothing; Wait for USB; Hold for Display
        break;
    }
    return joiningComplete;
}
Ejemplo n.º 5
0
MOTE_MODE_T moteHandler(void)
{
    bool gotoSleep = false;
    bool gotoNextState = false;
    bool s2Button = false;
    bool readMessage = false;
    buttonStates_t buttonPress = Released;


    
    
    // Check USB connection state
    if (USB_DET_PORT)
        moteState = MOTE_SWAP;

    // Get Timer Ticks
    if ( appTimer.mSecTick )
    {
        // Clear Flags for next task
        TMRapp_ClearBitFlags();
        // Execute Task
        TMRapp_Handler();
        MOTEapp_TimerBasedTask(appTimer);
    }

    // Handle button press events for menu navigation
    buttonPress = service_pushbutton();
    if (buttonPress == ShortPress1)
    {
        secTicker = 0;
        gotoNextState = true;   // Button Event has occured
    }
    else if (buttonPress == ShortPress2)
    {
        secTicker = 0;
        s2Button = true;   // Button Event has occured
    }

    // Gather response from LoRa Module if available
    if ( MOTEapp_getModemResponse() )
        readMessage = true;
    else
        readMessage = false;

    // Handle Timed Task
    if ( (runningState == runningUplink) || (runningState == runningSensorMenu) )
    {
        // Get Sensor Data every 10mS
        if (m10SecTicker >= LightCapTime)
        {
            uint16_t Traw = 0;
            moteApp_delayms(10);
            for(uint8_t i = 32; i>0; i--)
            {
                light = (light + ADC_GetConversion(Light_channel)) / 2;
            }
            moteApp_delayms(10);
            for(uint8_t i = 32; i>0; i--)
            {
                Traw = (Traw + ADC_GetConversion(Temp_channel)) / 2;
            }
            temperature = ADC_TempConversion(Traw);
            m10SecTicker = 0;
        }
    }
    // Sleep Event if idle
    if(secTicker)
    {
        if (secTicker >= SleepTimeOut)
        {
            gotoSleep = true;
            secTicker = 0;
        }
    }

    switch(moteState)
    {
        default:        // Invalid
            while(1);   // Hold For Error;
        break;
        case MOTE_STARTUP:
            oled_clear();
            oled_putString("MOTE Mode",1,0);
            oled_putString("Starting",2,1);
            oled_putString("ReRun PwrTst<S2>",0,3);
            activeState = moteStartup;
            moteState = MOTE_JOINING;
        break;
        case MOTE_JOINING:
            if ( moteJoiningProcess(readMessage, s2Button) )
            {
                oled_clear();
                oled_putString("Mobile Mote -App",0,0);
                oled_putString("Navigate  Select", 0,2);
                oled_putString(" (S1)      (S2) ", 0,3);
                runningState = runningEnter;
                moteState = MOTE_RUNNING;
            }
        break;
        case MOTE_RUNNING:
            moteRunningProcess(gotoNextState, s2Button, readMessage, gotoSleep);
        break;
        case MOTE_SWAP:
            // Do Nothing
        break;
    }
    return moteState;
}
Ejemplo n.º 6
0
/****************************************************************************
 *
 *                        Main application
 *
****************************************************************************/
void main(void)
{
//    adc_result_t vbatt; // raw ADC of battery voltage
//    adc_result_t vbus;  // raw ADC of charger input voltage
    uint8_t soc_leds;   // result of raw ADC to 5 SOC LED conversion
    uint8_t c;  // dbg0, dbg1,

    // initialize the device
    SYSTEM_Initializer();
    CE_N_SetLow();                  // enable the input charger
    USBA_EN_SetHigh();              // enable usb porta
    M1_A_SetLow();                  // don't care since using pin_ignore/I2C only mode
    M2_A_SetHigh();                  // don't care since using pin_ignore/I2C only mode
    EM_EN_A_SetHigh();               // don't care since using pin_ignore/I2C only mode
    USBB_EN_SetHigh();              // enable usb portb
    M1_B_SetLow();                  // don't care since using pin_ignore/I2C only mode
    M2_B_SetHigh();                  // don't care since using pin_ignore/I2C only mode
    EM_EN_B_SetHigh();               // don't care since using pin_ignore/I2C only mode
    otg_mode_flag = 0;


    // initialise variables
    BTN0_dbstate = 0;                 // initial pushbutton state = released
    BTN0_change = 0;                  // clear pushbutton change flag (no change)
    BTN1_dbstate = 0;                 // initial pushbutton state = released
    BTN1_change = 0;                  // clear pushbutton change flag (no change)

    ADC_read_flag = 0;                //
    //ADC_channel = 0;

    print_start_msg();

     __delay_ms(10); // DEBUG
    charger_init();
    __delay_ms(10);  //DS: Upon power-up, the UCS1002 will not respond to any SMBus communications for 5.5 ms
    usb_port_init(USBA_ADDR);   // setup the USB smart output chips
     __delay_ms(10);    // DEBUG
    usb_port_init(USBB_ADDR);   // setup the USB smart output chips

    //enable interrupts - TODO should this wait unitl after i2c init routines?
    INTCONbits.IOCIF = 0;
    IOCBF1 = 0;
    IOCBF2 = 0;
    IOCBF3 = 0;
    IOCBF4 = 0;
    INTCONbits.IOCIE = 1;
    TMR0_StartTimer();
    TMR1_StartTimer();
    TMR2_StartTimer();
    INTERRUPT_PeripheralInterruptEnable();
    INTERRUPT_GlobalInterruptEnable();               // enable global interrupts

    /**
     *                          CORE APPLICATION
     */

    while (1)
    {
        // Add your application code

        //***DEBUG***//
        if (EUSART_IsDataReady() == 1)	// check for input
        {
                c = EUSART_GetByte();   // reading RCREG clear RCIF
                select_status_report(c);
        }

        if (A_DET_A_N_GetValue() == 0)
            FLASHLIGHT_SetHigh();
        //***DEBUG***//

        // check for self-attached cable
        if (SELF_DETECT_GetValue() == 1)
        {
            USBA_EN_SetLow();
            putstring0("Self Detect, USBA disabled"); // DEBUG
        }
        else USBA_EN_SetHigh();

        //grab battery level, input voltage, update SOC byte
        if (ADC_read_flag == 1)
        {
            ADC_read_flag = 0;
            vbatt = (ADC_GetConversion(channel_AN1) << 1);  // input voltage divided by 2 - multiply it back
            __delay_ms(1);                      // provide switching time
//            vbus = (ADC_GetConversion(channel_AN2) << 1);  // TODO figure out what to do with this
        }
        soc_leds = calc_soc(vbatt);

        // check for debounced button press
//        if (BTN0_change && !BTN0_dbstate)       // if button state changed and pressed (low)
        if (DBG_SPARE_change && DBG_SPARE_dbstate)       // if button state changed and pressed (high)
        {
            FLASHLIGHT_Toggle();               //   turn flashlight off and on
//            BTN0_change = 0;                  //   clear button change flag
            DBG_SPARE_change = 0;                  //   clear button change flag
        }
        
//        if (BTN1_change && !BTN1_dbstate)       // if button state changed and pressed (low)
//        {
//            soc_cntr_start_flag = 1;
            soc_update(soc_leds);           //   display SOC animation
//            BTN1_change = 0;                  //   clear button change flag
//        }
//        if (soc_cntr_done_flag == 1)        // let the SOC display stand for 5s
//        {
//            dbg1 += 1;
//            soc_cntr_start_flag = 0;
//            soc_cntr_done_flag = 0;
//            soc_update(dbg1);
////            soc_update(CLR_SOC);
//        }


        // TODO update more charger regs, react
        slave_check_fault(CHRGR_ADDR);
        if (otg_mode_flag == 1)
            i2c_slave_command(CHRGR_ADDR, 0x01, 0x6B); //REG01 reset watchdog, enable OTG only
        else
            i2c_slave_command(CHRGR_ADDR, 0x01, 0x5B); //REG01 reset watchdog, enable charger only

        // TODO update more usba regs, react
        slave_check_fault(USBA_ADDR);
        usb_porta_regs.REG00 = i2c_slave_read(USBA_ADDR, 0x00);   //update the current reading

        // TODO update more usbb regs, react
        slave_check_fault(USBB_ADDR);
        usb_portb_regs.REG00 = i2c_slave_read(USBB_ADDR, 0x00);   //update the current reading


        //debug
//        for (dbg0 = 0; dbg0 < 255; dbg0++);
//        {
//            __delay_ms(100);
//        }
//        soc_update(dbg1);
//        dbg1 += 1;
//        if (dbg1 == 0x1F)
//            dbg1 = 0;
//        if (debug_rprt_flag == 1)
//        {
//            debug_rprt_flag = 0;
//            FLASHLIGHT_Toggle();
//
//        }
 
    }
}