Flyport WiFi Energy Saving Mode

From openPicus Wiki
Jump to: navigation, search

Contents

Introduction

The openPicus Framework permits using advanced energy saving methods. This is an advanced feature, and the FLYPORT's normal operations and functions will be different, and all the TCP/IP related functions do not work in these modes. As a general rule, when in an energy saving mode, none of the functions described in the chapter “Using the TCP/IP stack” should be used since are all TCP/IP dependant.

There are two ways to reduce power consumption:

  • Turning OFF WiFi transceiver only (Hibernate mode)
  • Turning OFF WiFi transceiver and put in SLEEP state the microcontroller (Sleep mode)


Both modes turn OFF the Wi-Fi transceiver to save power, but the Sleep mode saves more power since it stop the Microcontroller. Only the RTCC and external interrupts can wake up the Microcontroller once it is in sleep mode.


Special considerations:

  • All the TCPSockets and UDPSockets must be reinitialized after waking up from hibernate or sleep mode. This must be done each time the FLYPORT WiFi wakes up, and for every Socket used, or the FLYPORT will fail its tasks executions and will definitely be locked!
  • Please pay very special attention to socket handles as these are the most delicate software code for power saving modes.

Hibernate Mode

Activating Hibernate mode, the WiFi transceiver will be turned OFF, the OpenPicus Framework will kill the TCP task, and only the user task will be executed. This is not the maximum power saving mode, but PIC is still powered.

The functions described in “Controlling the FLYPORT hardware” chapter, and “RTCC peripheral module” can be used without any kind of warning as they are only PIC dependant.

To Activate Hibernate Mode → WFHibernate(); This function activates the Hibernate mode, so turn OFF the Wi-Fi transceiver

To Deactivate the Hibernate Mode → WFOn(); This function turns ON the Wi-Fi transceiver again

NOTE: this function does NOT reconnect FLYPORT to Wi-Fi network automatically. The User should handle the connection of the Wireless LAN after the transceiver is enabled.

Sleep Mode

When Sleep mode is activated, the WiFi transceiver is turned OFF, and also PIC microcontroller enters a low power mode to save energy. This mode however locks almost all the power of the microcontroller, so only external events can wake up FLYPORT from this state.

The external events that can wake up FLYPORT are 2: * RTCC alarm * External Interrupts (up to 3 different)


Both of the above events launch special Interrupt Service Routines (ISRs) that permit the microcontroller to wake up from a sleep condition. It is not important what the ISR related functions do, the important point is that they are activated from external events(the RTCC alarm can be seen as an external event since it uses a secondary oscillator that is independent from the microcontroller).

To Activate Sleep Mode → WFSleep(); This function activates the Sleep mode, so turn OFF the Wi-Fi transceiver and stops microcontroller working.

To Deactivate Sleep Mode Only RTCC and external interrupts can deactivate the sleep mode.

NOTE

Sleep mode deactivates both transceiver and Microcontroller. When Flyport go out from Sleep condition, only Microcontroller is turned on. Flyport Firmware will continue from the same position it was interrupted, just like a pause.

The next example shows how both the RTCC and External Interrupts can be used to wake up FLYPORT from Sleep at a defined time or with the change of a microcontroller pin state.

Flyport PRO

When the Sleep Mode is activated, the ENVREF pin (which enables the VREF diode) is not switched off automatically. If the lowest possible consumption is wanted, this pin must be switched off with the simple following code before WFSleep:

ADCRefEnable(FALSE);

Now, the VREF diode is powered off and no current flows through it.

Remember: after waking up, the ENVREF must be switched on manually if you want to use the internal ADC or similar.
Line code to do it

ADCRefEnable(TRUE);

Sleep Mode consumption [uA] (only FlyportPRO)
ENVREF enabled 1265
ENVREF disabled 180

Energy Saving Usage Example

Here is a simple power saving example, using Serial commands to switch from normal operation to hibernate or sleep, and then use the RTCC or External interrupt 2 to wake up from Sleep mode.

#include "taskFlyport.h"
#include "rtcc.h"
 
char input[20];
char rtccString[50];
 
extern BOOL alarmflag;
 
t_RTCC myrtcc;
t_RTCC myalarm;
t_RTCC nowrtcc;
 
 
void external_interrupt_function()
{ 
	// Reset interrupt 
	IOPut(o4,on);
	UARTWrite(1,"Wakeup!\r\n");
}
 
 
void FlyportTask()
{
	vTaskDelay(200);
	UARTWrite(1,"\r\nPower saving mode test...\r\n");
 
	// Set external interrupt
	IOInit(p5,inup);
	IOInit(p5, EXT_INT2);
	INTInit(2, external_interrupt_function, 1);
	INTEnable(2);
 
	// Set RTCC
	myrtcc.year = 11; //last two year number	
	myrtcc.month = 11;
	myrtcc.dweek = 3; //sunday is 0
	myrtcc.day = 2;
	myrtcc.hour = 16;
	myrtcc.min = 23;
	myrtcc.sec = 1;
	// Write settings on internal registers
	RTCCWrite(&myrtcc);
	// Create alarm configuration
	myalarm = myrtcc;
	myalarm.sec = myalarm.sec + 20;
	// Set Alarm configuration to internal registers
	RTCCSetAlarm(&myalarm, REPEAT_INFINITE, EVERY_TEN_SEC);
	// Active alarm
	RTCCRunAlarm(1); // 1 turn on, 0 turn off
 
	while(1)
	{
		// Check RTCC alarm flag
		if(alarmflag == 1)
		{
			alarmflag = 0;
			vTaskDelay(1);
			UARTWrite(1, "ALARM!!!\r\n");
                }
		// Check UART commands
		if (UARTBufferSize(1) > 1)
		{
			vTaskDelay(50);
			char uread[257];
			int toread = UARTBufferSize(1);
			UARTRead(1,uread, toread);	
			uread[toread+1]='\0';	
 
			// Command parsing
			if (strstr(uread,"off")!=NULL)
			{
				UARTWrite(1,"Wi-Fi OFF\r\n");
				WFHibernate();
			}
			else if (strstr(uread,"sleep")!=NULL)
			{
				UARTWrite(1,"Flyport is sleeping... \r\n");
 
                                #ifdef FLYPORT_PRO
        	                      ADCRefEnable(FALSE);
                                #endif
 
				WFSleep();
			}
			else if (strstr(uread,"on")!=NULL)
			{
				UARTWrite(1,"Wi-Fi transceiver activation...\r\n");
				WFOn();
			}
			else if (strstr(uread,"up")!=NULL)
			{
				UARTWrite(1,"Connecting...\r\n");
				WFConnect(WF_DEFAULT);
				UARTWrite(1,"Connection launched...\r\n");
			}
			else if (strstr(uread,"dn")!=NULL)
			{
				UARTWrite(1,"Disconnecting...\r\n");
				WFDisconnect();
			}
			else if (strstr(uread,"rtcc")!=NULL)
			{
				// Print RTCC (only if NOT IN SLEEP MODE)
				RTCCRead(&nowrtcc);	// get the current time
				UARTWrite(1, "RTCC state:\r\n");
				sprintf(rtccString, "%d/%d/%d\r\n", nowrtcc.hour, nowrtcc.min, nowrtcc.sec);
				UARTWrite(1, rtccString);
			}
			UARTFlush(1);
		}
	}
}

In this example all the code needed to use the power saving modes is shown. Using UART commands the user can control switching between normal operation, sleep mode, WiFi off, connection, disconnection and printing the dynamic value of the RTCC. To switch from one state to another some thought must be used, for example, if the user tries to connect to the WiFi network when the transceiver is turned OFF, FLYPORT will attempt unsuccessfully to open a connection, but no warnings messages will be shown on the UART.

Commands for switching between the different Power Saving Modes

The following command sequences can be used without problems:

  • To switch to transceiver off user should use: “off”
  • To switch to normal mode user should use: “on”

NOTE: with this command, if the FLYPORT was connected before, the user should then connect again to WiFi network using the command “up”.

  • To switch between normal state and transceiver off, the commands “on” and “off” are used, with attention to re-connecting the FLYPORT to the Wi-Fi profile after turning on the transceiver.
  • To switch to sleep mode user should use: “sleep”

NOTE: with this command, only interrupts can wake up FLYPORT. In this specific example the RTCC alarm or external interrupt 2 connected to pin 5 with pull up resistor may be used. To wake up FLYPORT from sleep the user can wait for an RTC ALARM or connect pin 5 to Ground (which will generate an external interrupt).

NOTE: after the wake up from sleep, the user must manually connect FLYPORT to WiFi network using the command “up”.

  • To check if the microcontroller is running or sleeping, the user can use the command: “rtcc”

NOTE: This command makes FLYPORT transmit the value of the RTCC over the UART, but works only when the microcontroller is active; FLYPORT will not respond when in sleep state!

  • Switching between normal and sleep states can be done with the command “sleep” and then waiting for an RTCC alarm or changing state of the interrupt pin, remembering to re-connect the FLYPORT to the Wi-Fi after wakeup.

Warning: Pay attention also to reinitialization of the sockets used in the application. All the TCP sockets used in the User task should be reinitialized to “INVALID_SOCKET” value before opening them again; the same is true for UDP Sockets, which should be reinitialized to “0”. This is necessary, and it is suggested to do it each time the user turns on the transceiver, and also before any network connection.

Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox