PowerManagementClass Class

PowerManagementClass Class

Description
A class used for managing the low-power operation of Ameba.

Syntax
class PowerManagementClass

Members

Public Constructors 
A public constructor should not be used as this class is intended to be a singleton class. Access member functions using the object instance named power management.
Public Methods 
PowerManagementClass::sleepAllow OS to automatically save power while idle
PowerManagementClass::activeDisallow OS to automatically save power while idle
PowerManagementClass::setPllReservedReserved PLL while sleeping
PowerManagementClass::deepsleepEnter deepsleep immediately
PowerManagementClass::safeLockCheck if the system is allowed to enter power save state
PowerManagementClass::setSafeLockPinSet pin used to control power save state
PowerManagementClass::softResetReboot system

PowerManagementClass:: sleep

Description
Allow OS to automatically save power while idle.

Syntax
void sleep(void);

Parameters
The function requires no input parameter.

Returns
The function returns nothing.

Example Code
Example: SleepBasic
This example demonstrates the use of sleep API for power management. Ameba will automatically suspend when there is no on-going task.

#include <PowerManagement.h>   
  
int counter = 0;  
  
void setup() {  
  
  /*  If you need any peripheral while sleep, remove the below line. 
   *  But it makes Ameba save less power (around 5.5 mA). */  
  PowerManagement.setPllReserved(false);  
  
  /* Make Ameba automatically suspend and resume while there is no on-going task. */  
  PowerManagement.sleep();  
}  
  
void loop() {  
  Serial.print("Enter sleep for 5s. Count: ");  
  Serial.println(counter++);  
  delay(5000);  
}  

Notes and Warnings
OS will invoke the system suspend if it anticipates an idle duration of more than 2 seconds.

PowerManagementClass::active

Description
Disallow OS to automatically save power while idle.

Syntax
void active(void);

Parameters
The function requires no input parameter.

Returns
The function returns nothing.

Example Code
NA

Notes and Warnings
NA

PowerManagementClass::setPllReserved

Description
Reserved PLL while sleeping.

Syntax
void setPllReserved(bool reserve);

Parameters
reserve: true to reserve PLL

Returns
The function returns nothing.

Example Code
Example: SleepBasic
Details of the code can be found in the previous section of PowerManagementClass:: sleep.

Notes and Warnings
Reserving PLL would keep the FIFO of peripherals enabled (e.g. UART) during sleep but would require more power. Not reserving PLL might lead to missing data due to disabled FIFO.

PowerManagementClass::deepsleep

Description
Enter deepsleep immediately.

Syntax
void deepsleep(uint32_t duration_ms);

Parameters
duration_ms: duration to enter deepsleep, expressed in milliseconds

Returns
The function returns nothing.

Example Code
Example: DeepSleepBasic
This example demonstrates the use of deepsleep API of power management.

/* 
 * This sketch makes a delay for the 30s before entering deepsleep. 
 * if Ameba is under deepsleep, it's not able to upload images. 
 * Without this delay, you will find it's hard to upload the image  
 * because Ameba is always under deepsleep. 
 * 
 * If you accidentally put deepsleep at the beginning, 
 * you can connect D18(For RTL8710, it's D2) to GND. It's a software lock of power save 
 * which prevents Ameba enter any power save state. 
 * 
 * When Ameba resume from deepsleep, it starts from the beginning  
 * instead of original program position. It's just like it is been  
 * through a reset. 
 */  
  
#include <PowerManagement.h>   
  
void setup() {  
  
#if defined(BOARD_RTL8710)  
  /* 
   *  Choose your safe lock pin. It's pin 2 by default 
   */  
  PowerManagement.setSafeLockPin(2);  
#endif  
  
  /*  Make a safe delay before entering deepsleep 
   *  if Ameba is under deepsleep, it's not able to upload images. 
   */  
  delay(30000);  
  
  Serial.println("Enter deepsleep for 5s");  
  PowerManagement.deepsleep(5000);  
  
  /*  After Ameba resume from deepsleep,  
   *  it starts from the beginning just like it's been through a reset. 
   */  
}  
  
void loop() {  
  Serial.println("Unable to deepsleep");  
  delay(1000);  
}  

Notes and Warnings
Deepsleep is the power state that uses the least power. When the system wakes from deepsleep, the code behaves like after a reset.

PowerManagementClass::safeLock

Description
Check if the system is allowed to enter any power-saving state.

Syntax
bool safeLock(void);

Parameters
The function requires no input parameter.

Returns
True if the system is not allowed to enter power save state.

Example Code
Example: DeepSleepWithDHTUdp
This example demonstrates the use of deepsleep API of power management with DHTxx and WiFi.

/* 
 * This sketch make wifi connection, read DHTxx results, send out 
 * results and make a deepsleep or delay for the 60s. 
 * If you accidentally put deepsleep at the beginning, 
 * you can connect D18(For RTL8710, it's D2) to GND. It's a software lock of power save which prevents Ameba enter any power save state. 
 */  
#include <WiFi.h>   
#include <WiFiUdp.h>   
#include <PowerManagement.h>   
#include "DHT.h"  
  
#define DHTPIN 2     // what digital pin we're connected to  
  
// Uncomment whatever type you're using!  
#define DHTTYPE DHT11   // DHT 11  
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321  
//#define DHTTYPE DHT21   // DHT 21 (AM2301)  
  
DHT dht(DHTPIN, DHTTYPE);  
  
// Update these with values suitable for your network.  
char ssid[] = "yourNetwork";     // your network SSID (name)  
char pass[] = "secretPassword";  // your network password  
int status  = WL_IDLE_STATUS;    // the Wifi radio's status  
  
char udpSrvIp[] = "192.168.1.1"; // the received UDP server IP  
int udpSrvPort = 2390;           // the received UDP server port  
  
char databuf[64];  
  
WiFiUDP Udp;  
  
void setup()  
{  
  Serial.begin(38400);  
  
  while (status != WL_CONNECTED) {  
    Serial.print("Attempting to connect to SSID: ");  
    Serial.println(ssid);  
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:  
    status = WiFi.begin(ssid, pass);  
  
    if (status == WL_CONNECTED) {  
      break;  
    }  
    // wait 10 seconds for retry:  
    delay(10000);  
  }  
  
  dht.begin();  
}  
  
void loop()  
{  
  float h = dht.readHumidity();  
  float t = dht.readTemperature();  
  
  while (isnan(h) || isnan(t)) {  
    Serial.println("DHTxx retry");  
    delay(2000);  
    h = dht.readHumidity();  
    t = dht.readTemperature();  
  }  
  
  sprintf(databuf, "h: %f %%\tt: %f *C", h, t);  
  
  Udp.beginPacket(udpSrvIp, udpSrvPort);  
  Udp.write(databuf);  
  Udp.endPacket();  
  
  if (!PowerManagement.safeLock()) {  
    delay(100);  
    WiFi.disconnect();  
    Serial.println("deepsleep 60s");  
    PowerManagement.deepsleep(60000);  
  
    Serial.println("You won't see this log");  
    delay(1000);  
  } else {  
    /* As D18 is connected to GND, a software safe lock prevents Ameba enter power save */  
    Serial.println("delay 60s");  
    delay(60000);  
  }  
}  

Notes and Warnings
A pin can be set as a safety lock pin. If the safety lock pin is High, Ameba is prevented from entering any power save state.

PowerManagementClass::setSafeLockPin

Description
Set pin used to control power save state.

Syntax
bool setSafeLockPin(int ulPin);

Parameters
ulPin: Pin to use as safe lock pin.

Returns
True: the pin is valid
False: the pin is invalid

Example Code
NA

Notes and Warnings
This function is used to change the default safe lock pin and needs to be called on every reset.

PowerManagementClass::softReset

Description
Reboot system.

Syntax
void softReset(void);

Parameters
The function requires no input parameter.

Returns
The function returns nothing.

Example Code
NA

Notes and Warnings
NA

Copyrights ©瑞晟微电子(苏州)有限公司 2021. All rights reserved.
Please confirm that QQ communication software is installed