a test code to implement and test LP1768 power control mode

Dependencies:   mbed

This code implemented some LP1768 power mode : Sleep(), DeepSleep(), PowerDown(), DeepPowerDown(), BOGD_PowerDown(). It also has a test code to test these power modes and wakeup using watch dog. The wakeup part is based on Erik's code but add implementation for LP1768. As LP1768 has debug enabled in default, it cannot be waked up in DeepSleep mode. Therefore this code use WDC reset to wake up the chips from deep sleep. The test code also allow test the power under two clock frequency (96 MHz and 48MHz). Inspired by Paul and Michael Wang, I also tested the power reduction by power off PHY. The analysis could be found in http://mbed.org/users/steniu01/notebook/lp1768-power-mode-implementation-and-measurement-/#

Committer:
steniu01
Date:
Thu Jul 24 08:53:44 2014 +0000
Revision:
1:571258908570
Parent:
0:5c4169623549
Child:
2:15d9501bf5b3
demo version ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
steniu01 0:5c4169623549 1 #include "mbed.h"
steniu01 0:5c4169623549 2 #include "RTC.h"
steniu01 0:5c4169623549 3 #include "PowerControl.h"
steniu01 0:5c4169623549 4 #include "EthernetPowerControl.h"
steniu01 0:5c4169623549 5 #include "WakeUp.h"
steniu01 0:5c4169623549 6 #include "steven_powercontrol.h"
steniu01 1:571258908570 7 #include "Pulses.h"
steniu01 1:571258908570 8
steniu01 1:571258908570 9
steniu01 0:5c4169623549 10 DigitalOut myled1(LED1);
steniu01 0:5c4169623549 11 DigitalOut myled2(LED2) ;
steniu01 0:5c4169623549 12 DigitalOut myled3(LED3) ;
steniu01 0:5c4169623549 13 Serial pc (USBTX, USBRX);
steniu01 0:5c4169623549 14 Timer t;
steniu01 1:571258908570 15 //CPU_Usage cpu(t, 1);
steniu01 0:5c4169623549 16
steniu01 1:571258908570 17 AnalogOut multimeter(p18);
steniu01 0:5c4169623549 18
steniu01 1:571258908570 19 // Example for energy meter with SO interface.
steniu01 1:571258908570 20 // SO output of energy meter is connected (over opto-coppler) to pin 8
steniu01 1:571258908570 21 #define SO_PULSES 2000 // SO interface of energy meter with 2000 pulses per kWh
steniu01 1:571258908570 22 Pulses pulses(p8, Pulses::FALL);
steniu01 0:5c4169623549 23
steniu01 0:5c4169623549 24 void steven_get_string(){
steniu01 0:5c4169623549 25 char input[50];
steniu01 0:5c4169623549 26 int num=0;
steniu01 0:5c4169623549 27 while (1){
steniu01 0:5c4169623549 28 char ret=pc.getc();
steniu01 0:5c4169623549 29 pc.putc(ret);
steniu01 0:5c4169623549 30 if (num >=50){
steniu01 0:5c4169623549 31 pc.printf("Overflow \n");
steniu01 1:571258908570 32 break;
steniu01 0:5c4169623549 33 }
steniu01 0:5c4169623549 34 if (ret!=(char)NULL)
steniu01 0:5c4169623549 35 input[num++]=ret;
steniu01 0:5c4169623549 36 if(ret==(char)13)
steniu01 0:5c4169623549 37 break;
steniu01 0:5c4169623549 38 }
steniu01 0:5c4169623549 39 pc.printf ("\n");
steniu01 0:5c4169623549 40 pc.printf ("you have input %s \n",input);
steniu01 0:5c4169623549 41 }
steniu01 0:5c4169623549 42
steniu01 1:571258908570 43 void power_measurement(void){
steniu01 1:571258908570 44 pulses.setFactor ( 3600.0f/SO_PULSES ); // Scale to kW; kWh
steniu01 1:571258908570 45 float averagePower = 0; // Average energy since last call of get()
steniu01 1:571258908570 46 float minPower = 0; // Min. energy since last call of get()
steniu01 1:571258908570 47 float maxPower = 0; // Max. energy since last call of get()
steniu01 1:571258908570 48 float sumEnergy = 0; // Sum of energy over all since start of mbed
steniu01 1:571258908570 49
steniu01 1:571258908570 50 while(1) {
steniu01 1:571258908570 51
steniu01 1:571258908570 52 pulses.get ( &averagePower, &minPower, &maxPower, &sumEnergy );
steniu01 1:571258908570 53
steniu01 1:571258908570 54 pc.printf ( "Power: %.3f (%.3f...%.3f) [kW] Energy: %.3f [kWh] SO-pulses=%d\r\n",
steniu01 1:571258908570 55 averagePower,
steniu01 1:571258908570 56 minPower,
steniu01 1:571258908570 57 maxPower,
steniu01 1:571258908570 58 sumEnergy / 3600.0f,
steniu01 1:571258908570 59 pulses.getCounter() );
steniu01 0:5c4169623549 60
steniu01 1:571258908570 61 // wait(6); // Example 6 seconds; typical 1 minute (60 sec) sample time
steniu01 1:571258908570 62
steniu01 1:571258908570 63 // Additional feature: Output of actual power as analog voltage (1V==1kW; max3.3kW). Connect a multimeter to pin 18 and GND
steniu01 1:571258908570 64 for ( int i=0; i<20; i++ ) {
steniu01 1:571258908570 65 multimeter = pulses.getAct() / 3.3f; // Scale to 1V==1kW
steniu01 1:571258908570 66 wait(0.3); // Example 6 seconds; typical 1 minute (60 sec) sample time
steniu01 1:571258908570 67 }
steniu01 1:571258908570 68 }
steniu01 0:5c4169623549 69 }
steniu01 1:571258908570 70 void profiling_PHY_PowerDown (void)
steniu01 0:5c4169623549 71 {
steniu01 1:571258908570 72 //PHY_PowerUp();
steniu01 1:571258908570 73 if (!Peripheral_GetStatus(LPC1768_PCONP_PCENET))
steniu01 1:571258908570 74 EMAC_Init(); //init EMAC if it is not already init'd
steniu01 1:571258908570 75
steniu01 1:571258908570 76 unsigned int regv;
steniu01 1:571258908570 77 wait (10);
steniu01 1:571258908570 78 myled1=1;
steniu01 1:571258908570 79 myled2=0;
steniu01 1:571258908570 80 myled2=0;
steniu01 1:571258908570 81 regv = read_PHY(PHY_REG_BMCR);
steniu01 1:571258908570 82 write_PHY(PHY_REG_BMCR, regv | (1 << PHY_REG_BMCR_POWERDOWN));
steniu01 1:571258908570 83 regv = read_PHY(PHY_REG_BMCR);
steniu01 1:571258908570 84 wait (10);
steniu01 1:571258908570 85 myled1=0;
steniu01 1:571258908570 86 myled2=1;
steniu01 1:571258908570 87 myled3=0;
steniu01 1:571258908570 88 //shouldn't need the EMAC now.
steniu01 1:571258908570 89 LPC_SC->PCONP &=~LPC1768_PCONP_PCENET;
steniu01 1:571258908570 90 wait (10);
steniu01 1:571258908570 91 myled1=0;
steniu01 1:571258908570 92 myled2=0;
steniu01 1:571258908570 93 myled3=1;
steniu01 1:571258908570 94 //and turn off the PHY OSC
steniu01 1:571258908570 95 LPC_GPIO1->FIODIR |= 0x8000000;
steniu01 1:571258908570 96 LPC_GPIO1->FIOCLR = 0x8000000;
steniu01 1:571258908570 97 while(1);
steniu01 1:571258908570 98
steniu01 0:5c4169623549 99 }
steniu01 1:571258908570 100
steniu01 0:5c4169623549 101 int main()
steniu01 0:5c4169623549 102 {
steniu01 1:571258908570 103 //profiling_PHY_PowerDown();
steniu01 1:571258908570 104 //PHY_PowerUp();// power up PHY because it was powerred up as default. We might power it down when test "Turn off PHY".
steniu01 0:5c4169623549 105 myled1=1;
steniu01 0:5c4169623549 106 wait(0.5);
steniu01 0:5c4169623549 107 myled1=0;
steniu01 0:5c4169623549 108 char char_get=0;
steniu01 0:5c4169623549 109 int num_get=0;
steniu01 1:571258908570 110 char* powermode[7]={ "[1] no low power", "[2] sleep mode", "[3] deep sleep mode", "[4] power down mode", "[5] Brown-out Reduced Power mode", "[6] Deep Power Down", "[7] Turn off PHY"};
steniu01 0:5c4169623549 111 while (num_get>7 || num_get<1){
steniu01 0:5c4169623549 112 printf("Which power mode you want to choose: \r\n");
steniu01 0:5c4169623549 113 for (int a=0;a<7;a++)
steniu01 0:5c4169623549 114 pc.printf("%s \r\n", powermode[a]);
steniu01 0:5c4169623549 115 char_get=pc.getc(); // get char from serial
steniu01 0:5c4169623549 116 num_get=char_get-48;// convert char to int, minus 48 since ascii num start from 48
steniu01 0:5c4169623549 117 if (num_get>7 || num_get<1)
steniu01 0:5c4169623549 118 pc.printf("You have choosen an invalid num, please choose a correct one\r\n");
steniu01 0:5c4169623549 119 }
steniu01 0:5c4169623549 120 pc.printf ("I am going to test %s\r\n",powermode[num_get-1]);
steniu01 0:5c4169623549 121 myled2=1;
steniu01 0:5c4169623549 122 wait(0.5);
steniu01 0:5c4169623549 123 myled2=0;
steniu01 1:571258908570 124 //WakeUp::set(10);
steniu01 1:571258908570 125 WakeUp::set(70);
steniu01 0:5c4169623549 126 switch (num_get){
steniu01 0:5c4169623549 127 case 1: break;
steniu01 0:5c4169623549 128 case 2: steven_Sleep();break;
steniu01 0:5c4169623549 129 case 3: steven_DeepSleep(); break;
steniu01 0:5c4169623549 130 case 4: steven_PowerDown();break;
steniu01 0:5c4169623549 131 case 5: steven_BOGD_PowerDown();break;
steniu01 1:571258908570 132 case 6: steven_DeepPowerDown(); break;
steniu01 1:571258908570 133 case 7: profiling_PHY_PowerDown(); break;
steniu01 0:5c4169623549 134 default: pc.printf("Not valid input\n"); break;
steniu01 0:5c4169623549 135 }
steniu01 0:5c4169623549 136 while (1);
steniu01 0:5c4169623549 137 }