Beispiel #1
0
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
    EUSART_Initialize();
    EPWM1_Initialize();

    uint16_t dutyValue;
    uint16_t angle;
    uint8_t c;
    uint8_t buf[32] = {'\0'};
    uint8_t cnt = 0;
    
    // read angle from EEPROM
    angle = (uint16_t)DATAEE_ReadByte(0);
    // printf("read angle from EEPROM: %d\n", angle);
    dutyValue = calc_duty(angle);
    EPWM1_LoadDutyValue(dutyValue);
            
    while (1)
    {
        __delay_ms(500);
        LATCbits.LATC4 ^= 1;

        do {
            c = EUSART_Read();
            if (c == '\n') {
                buf[cnt] = '\0';
                cnt = 0;
                if (strcmp(buf, "w") == 0) {
                    DATAEE_WriteByte(0, (uint8_t)angle);
                    // printf("write the last angle onto EEPROM: %d\n", angle);
                } else {
                    angle = atoi(buf);
                    dutyValue = calc_duty(angle);
                    EPWM1_LoadDutyValue(dutyValue);
                }
            } else {
                buf[cnt++] = c;
            }
        } while (EUSART_DataReady);

        CLRWDT();
    } 
}
Beispiel #2
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;
}
Beispiel #3
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 #4
0
void main(void)
{
	receive0_cpt_ticks = 0xFF;
	receive1_cpt_ticks = 0xFF;
	receive2_cpt_ticks = 0xFF;
	fifo0wr=0;
	fifo0rd=0;
	fifo1wr=0;
	fifo1rd=0;
	fifo2wr=0;
	fifo2rd=0;
	send_channel = 0;
	cpt_led = 0;
	sending = 0;
	state = 0;
	timer_ini = DATAEE_ReadByte(0x00);
	if (timer_ini==0xff) {
		timer_ini=TIMER_INI_DEFAULT;
		DATAEE_WriteByte(0x00,timer_ini);
	}

	wait = DATAEE_ReadByte(0x01);
	if (wait==0xff) {
		wait=WAIT_DEFAULT;
		DATAEE_WriteByte(0x01,wait);
	}
	send_wait_next_byte = wait;

	Init();

	LED0 = 0;

	while(1)
	{
		/* flash de 600us toutes les 3.4 secondes */

		if (VALID==0)
		{
			LED0 = 0;
			asm("sleep");
			asm("nop");
		}

		if (cpt_led<LED_OFF)
		{
			LED0 = 1;
		}
		else
		{
			LED0 = 0;
		}
		if (RCIF==1)
		{
			received_data = RCREG;
			RCIF=0;
			switch(state)
			{
				case 1 :
					send(received_data);
					switch(received_data)
					{
						case 'v' : // display current timer value
							send(ValueToAsciiHex(timer_ini/16));
							send(ValueToAsciiHex(timer_ini%16));
							send(0x0d);
							send(0x0a);
							state=0;
						break;
						case 'w' : // write timer value to EEPROM
							DATAEE_WriteByte(0x00,timer_ini);
							sendline("Ok.");
							state=0;
						break;
						case 'r' : // read timer value from EEPROM
							timer_ini = DATAEE_ReadByte(0x00);
							send(ValueToAsciiHex(timer_ini/16));
							send(ValueToAsciiHex(timer_ini%16));
							send(0x0d);
							send(0x0a);
							state=0;
						break;
						case 'd' : // define timer value, need 2 extra bytes
							send('=');
							state=2;
						break;
						default :
							sendline("??? command aborted.");
							state=0;
						break;
					}
					break;

				case 2 :
					send(received_data);
					if (isxdigit(received_data))
					{
						par_hi = received_data;
						state = 3;
					}
					else
					{
						sendline("??? command aborted.");
						state = 0;
					}
					received_data=' ';
					break;
				
				case 3 :
					send(received_data);
					if (isxdigit(received_data))
					{
						timer_ini = TwoAsciiHexToValue(par_hi,received_data);
						sendline(" Ok.");
						state = 0;
					}
					else
					{
						sendline("??? command aborted.");
						state = 0;
					}
					received_data=' ';
					break;

				case 11 :
					send(received_data);
					switch(received_data)
					{
						case 'v' : // display current wait value
							send(ValueToAsciiHex(wait/16));
							send(ValueToAsciiHex(wait%16));
							send(0x0d);
							send(0x0a);
							state=0;
						break;
						case 'w' : // write wait value to EEPROM
							DATAEE_WriteByte(0x01,wait);
							sendline("Ok.");
							state=0;
						break;
						case 'r' : // read wait value from EEPROM
							wait = DATAEE_ReadByte(0x01);
							send(ValueToAsciiHex(wait/16));
							send(ValueToAsciiHex(wait%16));
							send(0x0d);
							send(0x0a);
							state=0;
						break;
						case 'd' : // define wait value, need 2 extra bytes
							send('=');
							state=12;
						break;
						default :
							sendline("??? command aborted.");
							state=0;
						break;
					}
					break;

				case 12 :
					send(received_data);
					if (isxdigit(received_data))
					{
						par_hi = received_data;
						state = 13;
					}
					else
					{
						sendline("??? command aborted.");
						state = 0;
					}
					received_data=' ';
					break;
				
				case 13 :
					send(received_data);
					if (isxdigit(received_data))
					{
						wait = TwoAsciiHexToValue(par_hi,received_data);
						sendline(" Ok.");
						state = 0;
					}
					else
					{
						sendline("??? command aborted.");
						state = 0;
					}
					received_data=' ';
					break;

				default :
					switch (received_data)
					{
						case 'V' :
						case 'v' :
							sendline("1.1");
						break;

						case 'P' :
						case 'p' :
							sendline("00168A");
						break;

						case 'S' :
						case 's' :
							sendline("0005");
						break;

						case 'B' :
						case 'b' :
							ADCON0 = 0x9D;	// power on
							somme = 0;
							for (i=0; i<100; i++)
							{
								ADCON0 = 0x9F;	// go
								do
								{
									etat = (ADCON0 & 0x02);
								}
								while (etat==0x02);
								wval= ADRESH;
								wval = wval << 8;
								wval = wval + ADRESL;
								somme += wval;
							}
							wval = somme / 39;
							if (wval<=325)
							{
								wval=0;
							}
							else
							if (wval>=416)
							{
								wval=100;
							}
							else
							{
								wval=wval -316;
							}
							send((wval/100)+0x30);
							send(((wval/10)%10)+0x30);
							send(((wval % 10)+0x30));
							sendline ("%");
							ADCON0 = 0x9C;	// power off
						break;

						case 'T' :
						case 't' :
							send(received_data);
							state = 1;
						break;

						case 'W' :
						case 'w' :
							send(received_data);
							state = 11;
						break;

						default :
							sendline ("");
							sendline ("------ RS232 Mux (c)Metraware 2007 ------");
							sendline ("List of supported commands :");
							sendline ("H    : This help");
							sendline ("V    : Software version");
							sendline ("P    : Part number");
							sendline ("S    : Serial number");
							sendline ("B    : Battery (0-100%)");
							sendline ("Tdhh : Define value for baud rate generator. From 00 to FF. Default 1D (decimal 29)");
							sendline ("Tv   : Current value of T");
							sendline ("Tw   : Save T value in EEPROM");
							sendline ("Tr   : Read T value from EEPROM");
							sendline ("Wdhh : Define value for timeout at end of line. From 00 to FF. Default C8 (decimal 200)");
							sendline ("Wv   : Current value of timeout");
							sendline ("Ww   : Save timeout value in EEPROM");
							sendline ("Wr   : Read timeout value from EEPROM");
							sendline ("?    : This help");
							sendline ("");
							sendline ("Configuration : 3 channels, FIFO_SIZE bytes fifo.");
							sendline ("-------------------------------------------");
							sendline ("");
						break;
					}
				break;
			}
		}

	}
}