Пример #1
0
void ICACHE_FLASH_ATTR user_init()
{
	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	os_delay_us(100);

	#ifdef PLATFORM_DEBUG
	ets_uart_printf("ESP8266 platform starting...\r\n");
	#endif
    
	struct softap_config apConfig;
	struct ip_info ipinfo;
	char ssid[33];
	char password[33];
	char macaddress[17];
	char info[150];

	if(wifi_get_opmode() != SOFTAP_MODE)
	{
		#ifdef PLATFORM_DEBUG
		ets_uart_printf("ESP8266 not in SOFTAP mode, restarting in SOFTAP mode...\r\n");
		#endif
		wifi_set_opmode(SOFTAP_MODE);
		//after esp_iot_sdk_v0.9.2, need not to restart
		//system_restart();
	}

	IP4_ADDR(&ipinfo.ip, 10, 10, 10, 1);
	IP4_ADDR(&ipinfo.gw, 10, 10, 10, 1);
	IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &ipinfo);

	wifi_get_macaddr(SOFTAP_IF, macaddr);
	wifi_softap_get_config(&apConfig);
	os_memset(apConfig.ssid, 0, sizeof(apConfig.ssid));
	os_sprintf(ssid, "%s", WIFI_APSSID);
	os_memcpy(apConfig.ssid, ssid, os_strlen(ssid));
	if (wifi_get_opmode() == SOFTAP_MODE) 
	{
		#ifdef WIFI_APWPA
		os_memset(apConfig.password, 0, sizeof(apConfig.password));
		os_sprintf(password, "%s", WIFI_APPASSWORD);
		os_memcpy(apConfig.password, password, os_strlen(password));
		apConfig.authmode = AUTH_WPA_WPA2_PSK;
		#else
		apConfig.authmode = AUTH_OPEN;
		#endif
		apConfig.channel = 7;
		apConfig.max_connection = 255;
		apConfig.ssid_hidden = 0;
		wifi_softap_set_config(&apConfig);
	}

	#ifdef PLATFORM_DEBUG
	if (wifi_get_opmode() == SOFTAP_MODE) 
	{
		wifi_softap_get_config(&apConfig);
		os_sprintf(macaddress, MACSTR, MAC2STR(macaddr));
		os_sprintf(info,"OPMODE: %u, SSID: %s, PASSWORD: %s, CHANNEL: %d, AUTHMODE: %d, MACADDRESS: %s\r\n",
					wifi_get_opmode(),
					apConfig.ssid,
					apConfig.password,
					apConfig.channel,
					apConfig.authmode,
					macaddress);
		ets_uart_printf(info);
	}
	#endif

	BtnInit();

	#ifdef PLATFORM_DEBUG
	ets_uart_printf("ESP8266 platform started!\r\n");
	#endif
}
Пример #2
0
int main (void)
{
//	_DOZE = 0;
   	_RCDIV = 0;
   	//pin mapping
    PinMapping();
    //Initialize the outputs
	OutputInit();
	// Set up the timer
	TimerInit();
	_system_flags.uart_hi_speed = 1;
	if (_system_flags.uart_hi_speed) {
		// divisor=8 for 460800bps
		initUART(8);
	} else {
    	initUART(34);
    }
	//Initialize the system
	SYSTEMInit();	
	ADCinit();	
	//Initialize the button processes	
	BtnInit();
	//Initialize the system flags
	FLAGInit();
	// variable to hold number of times we've passed the 50 msec counter without completing a UART receive
	char receivedCtr=0;

	while (1)
	{
		// check continuity to PIC
		if(uartDataAvailable_receive )
		{
			// set a flag if we're inside a receive packet
			// then, in 50msec timer, if we've not received anything after ~150-200msec, 
			// clear it and tell uart_getPacket to reset state
			_system_flags.PacketisBeingReceived=1;
	        uart_getPacket(0);
            uartDataAvailable_receive = 0;
		}
		if(_system_flags.TwelveBitflag)
		{
			if(_system_flags.EndofFrameflag)
			{
				// if stopped, and all data already pushed out, turn off HTPA
				if(_system_flags.Stopflag)
				{
					while (TX_Sent_Counter!=TX_ToSend_Counter);
					TurnOffHTPA();
					IEC0bits.U1TXIE = 1; // re-enable UART TX ISR
					_system_flags.Standbyflag=1;
					_system_flags.RefreshStatusflag = 1;
					_system_flags.Stopflag = 0;
				}
				_system_flags.EndofFrameflag = 0;
			}
		}
		if (ms_flag)// 1ms loop
		{	
			BtnProcessEvents();  	// Button debounce processing
			StateMachine();	
			if((_button_press.SW1DBB)&&(!_system_flags.SW1released))
			{
				SleepCounter = SLEEPTIME;
				LaserOutlat = ~LaserOutlat;
				LaserCounter=10;
				ButtonTimer = 3;
				_system_flags.SW1released = 1;
			}
			if( _system_flags.PacketisReadyforParse)
			{
				_system_flags.PacketisBeingReceived=0;
				receivedCtr=0;
				_system_flags.PacketisReadyforParse = 0;
				ParsingRXMsg();	
			}    
			if (TimeBase50ms())   //increment and check for 50ms software timer
			{
				if (_system_flags.PacketisBeingReceived)
				{
					// increment counter
					receivedCtr++;
					if (receivedCtr>2)
					{
						receivedCtr=0;
						// reset uart_getPacket
						_system_flags.PacketisBeingReceived=0;
						uart_getPacket(1);
					}
				}
			}//end of 50ms interval
    		if (TimeBase100ms())   //increment and check for 100ms software timer
    		{
    			// if we're in standby, and the change UART flag has been set, change it
    			if (state==STATE_STANDBY)
    			{
	    			if (_system_flags.ChangeUARTSpeed)
	          		{
		          		if (_system_flags.PacketisBeingReceived)
				    	{
				    		uart_getPacket(1);
				    	}
						_system_flags.ChangeUARTSpeed=0;
						// simply toggle UART speed if we get this command
						if (_system_flags.uart_hi_speed==1) {
            				_system_flags.uart_hi_speed=0;
            				initUART(34);
        				} else {
            				_system_flags.uart_hi_speed=1;
            				initUART(8);
        				}
        				FLAGInit();
        				SYSTEMInit();
        			}
       			}
			}//end of 100ms
			if (TimeBase250ms())   //increment and check for 250ms software timer   
			{
				LED1BlinkingCounter++;
				if(LED1BlinkingCounter == 1)
				{
					LED2lat = 1;//green off
					LED1lat = 1; // red is off
				}
				// blink at double speed if in high speed mode
				if(LED1BlinkingCounter > (10-(5*_system_flags.uart_hi_speed))) {
					if(state ==STATE_STREAM )
					{
						LED2lat = 0; // green flashing during streaming
						LED1lat = 1; // red is off
					}
					if(state ==STATE_STANDBY )
					{
						LED1lat = 0; // red flashing during standby
						LED2lat = 1; // green is off	
					}
					LED1BlinkingCounter = 0;
				}
				if((!_button_press.SW1DBB)&&(_system_flags.SW1released))
				{
					_system_flags.SW1released = 0;
					SleepCounter = SLEEPTIME;
				}
				if((ButtonTimer==0)&&(_system_flags.SW1released)&&(!_system_flags.InSleepWake))
				{
					if((_button_press.SW1DBB))
					{
						if(state==STATE_STREAM)
						{
							TurnOffHTPA();
						}	
						// nuclear option, reset firmware and start over with clean plate. 
						asm ("reset");
					}
				}
			}
			if (TimeBase1s())   //increment and check for 1000ms software timer
			{
				if (LaserCounter!=0) LaserCounter--;
				if (LaserCounter==0)
				{
					LaserOutlat = 0;
    			}    
				if(ButtonTimer>0) 
				{
					ButtonTimer=ButtonTimer-1;
				}
				_system_flags.InSleepWake=0;
				if(state == STATE_STANDBY)
				{
					if(SleepCounter>0) 
					{
						SleepCounter=SleepCounter-1;
					}
					if((SleepCounter==0))
					{
						// sleep flag will call state=SLEEP above
						_system_flags.Sleepflag = 1;
						_system_flags.RefreshStatusflag=1;
					}
				}//stand by mode
			}//End of TimeBase1s
			if (TimeBase1h())
			{				
			}//end of 1 hour base	
			ms_flag = 0;
		}// End of msflag 1 ms
	}// End of while (1)
} //End of main()
Пример #3
0
int main(void)
{

    // Start from displaying of PIC24 banners
	_display_state = DISP_HELLO;

    // Setup PortA IOs as digital
    AD1PCFG = 0xffff;

	//IO Mapping for PIC24FJ64GA004 
	#ifdef __PIC24FJ64GA004__ //Defined by MPLAB when using 24FJ64GA004 device
		ioMap();
		lockIO();
	#endif

    // Setup SPI to communicate to EEPROM
    SPIMPolInit();

    // Setup EEPROM IOs
    EEPROMInit();

    // Setup the UART
    UART2Init();

	// Setup the timer
	TimerInit();
    
	// Setup the LCD
	mLCDInit();

	// Setup debounce processing
	BtnInit(); 

    // Setup the ADC
    ADCInit();

	// Setup the banner processing
	BannerStart();

	// Setup the RTCC
    RTCCInit();

	while (1) {
		LCDProcessEvents();
        ADCProcessEvents();

		if (TimerIsOverflowEvent()){

			// Button debounce processing
			BtnProcessEvents();
			// State dependent processing
			switch (_display_state) {
                // Show Microchip banners
				case DISP_HELLO: BannerProcessEvents(); break;
                // Show clock
				case DISP_CLOCK: TBannerProcessEvents(); break;
                // Show voltage and temperature
				case DISP_VOLTAGE: VBannerProcessEvents(); break;

                default: _display_state = DISP_HELLO;
			}// End of switch (_display_state)...

            // If S6 is pressed show the next example
			if (BtnIsPressed(4)) {

                // Change state and clear display 
                if(!TBannerIsSetup()){
       				_display_state++;
                    if(_display_state > DISP_MAX)
                         _display_state = 0;

                    // Initialize state
                    switch (_display_state) {
                        // Microchip banners
                 	    case DISP_HELLO: BannerInit(); break;
                        // Clock
    				    case DISP_CLOCK: TBannerInit(); break;
                        // Voltage and temperature
	        			case DISP_VOLTAGE: VBannerInit(); break;
                        default:
                             _display_state = 0;
        		    }// End of switch (_display_state)...
                    mLCDClear();
                }else
                    TBannerNext();

                // wait for button released
                while (BtnIsPressed(4)){
					BtnProcessEvents();
				}
			}// End of 	if (BtnIsPressed(4)){...

            if(_display_state == DISP_CLOCK){

        		if (BtnIsPressed(1)){
                        TBannerSetup();
                    // wait for button released
                    while (BtnIsPressed(1))	BtnProcessEvents();
                }// End of if (BtnIsPressed(1 ...

                if(TBannerIsSetup()){
	           		if (BtnIsPressed(2)) {
                        TBannerChangeField(1);
                        // wait for button released
                        while (BtnIsPressed(2))	BtnProcessEvents();
    		    	}// End of if (BtnIsPressed(2)){...
    
	    		    if (BtnIsPressed(3)) {
                        // wait for button released
                        TBannerChangeField(0);
                        while (BtnIsPressed(3))	BtnProcessEvents();
        			}// End of if (BtnIsPressed(3)){...
                }// End of if(TBannerIsSetup( ...

            }// End of if(_display_state == DISP_SET_CLOCK ...



            if(_display_state == DISP_VOLTAGE){

        		if (BtnIsPressed(2)){
                    ADCSetFromMemory();
                    // wait for button released
                    while (BtnIsPressed(2)){
						BtnProcessEvents();
					}
                }// End of if (BtnIsPressed(2 ...

           		if (BtnIsPressed(3)){
                    ADCStoreTemperature();
                    // wait for button released
                    while (BtnIsPressed(3)){
						BtnProcessEvents();
					}
  		    	}// End of if (BtnIsPressed(3)){...

            }// End of if(_display_state ...




    	}// End of if (TimerIsOverflowEvent()...
    }// End of while(1)...
}// End of main()...