Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 5 > How do I compile for sleep on PSoC5

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



How do I compile for sleep on PSoC5
Moderator:
ANCY

Post Reply
Follow this topic



How do I compile for sleep on PSoC5

Helmut posted on 27 Jun 2012 5:40 AM PST
Top Contributor
48 Forum Posts

 I'm moving up from PSoC1 to PSoC5 for the first time.  I'm trying to make the PSoC5 sleep repeatedly in a simple loop.

Currently, I'm trying:

 

CyPmSaveClocks(); // Save clock configuration before entering sleep

CyPmSleep(PM_SLEEP_TIME_CTW_16MS, PM_SLEEP_SRC_NONE); // Go to sleep for 16ms.  Wakeup source implied by sleep time setting

CyPmRestoreClocks(); // Restore clock configuration after exiting sleep

 

but it won't compile because it can't find PM_SLEEP_TIME_CTW_16MS.  I look in cyPm.h and discover that none of the times are defined for PSoC5.

So how do I put the PSoC5 to sleep for some regular interval?

Please note I'll have a followup question later.  I intend to do some SPI communication each time a wake up.  Will that continue to clock if I go back to sleep before the transmission is finished?  Or do I need to poll completion of the transmission before I go to sleep?  (This assumes the sleep interval  is longer than the transmission time, and I haven't addressed the type or deptch of sleep yet.)

Thanks,




Re: How do I compile for sleep on PSoC5

Bob Marlowe posted on 27 Jun 2012 06:23 AM PST
Top Contributor
1768 Forum Posts

Have a loook at the "System Reference Guide" page 36 for CyPmSleep, there is clearly marked that for PSoC 5 the parameter you used should be set to "NONE".

You access the guide with

Help -> Documentation -> System Reference -> System Reference Guide

 

Bob



Re: How do I compile for sleep on PSoC5

Bob Marlowe posted on 27 Jun 2012 06:28 AM PST
Top Contributor
1768 Forum Posts

Oh, yes, I forgot:

You have to wait until all your communications have completed! To save more energy think about sending additionally all unused modules and pins into an energy-saving state.

Bob



Re: How do I compile for sleep on PSoC5

danaaknight posted on 27 Jun 2012 06:28 AM PST
Top Contributor
1773 Forum Posts

Logically in code I would suggest initiate sleep after timer has isr'ed and

SPI communication complete, unless application does not require SPI compeltion to sleep.

 

Start page of creator, example projects, is sleep timer project you can look at.

 

Regards, Dana.



Re: How do I compile for sleep on PSoC5

Helmut posted on 27 Jun 2012 06:49 AM PST
Top Contributor
48 Forum Posts

 Bob,

Thanks for your advice.  FYI, I was already looking at a downloaded copy of the system reference guide, that does NOT provide such advice.  It turns out that doc copy was for cy_boot Component v2.20.  Via the PSoC Creator V2.0 CP1 help menu, however, I get a different version for cy_boot Component V2.40.  That one indeed states on page 34 (yet another version from your page 36) "PSoC 5: Neither parameter to this function is used for PSoC 5.  The device will go into Sleep mode until it is woken by an interrupt from the Central Time Wheel (CTW).  The CTW must already be configured to generate an inerttupe.  It is configured using the SleepTimer component..."

I had SleepTimer in here before but commented it out per what must have been an PSoC3 (undoc) example.

TO ALL:

Now, with SleepTimer, my program hangs on CyPmSleep().  Here's the code.  Any additional help is greatly appreciated.

uint8 DEBUG_PIN_VALUE = 0;

void init()

{

// Initialize debug output pin to 0

DEBUG_Write(DEBUG_PIN_VALUE = 0); // Clear desired pin value and write to pin

// Initialize Sleep Timer

SleepTimer_1_Start(); // Turn on the sleep timer

SleepTimer_1_SetInterval(SleepTimer_1__CTW_16_MS); // Set CTW to 16ms

SleepTimer_1_EnableInt(); // Enable sleep timer interrupt

}


void main()

{

    init();


    CyGlobalIntEnable; // Enable global interrupts

    for(;;)

    {

// Sleep until next SleepTimer interrupt

CyPmSaveClocks(); // Save clock configuration before entering sleep

CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_NONE); // Go to sleep for CTW time, previously set to 16ms.  Note PSOC5 requires CTW set up previously and pass NONE to two arguments

CyPmRestoreClocks(); // Restore clock configuration after exiting sleep

// DEBUG - Twiddle a pin to show we have woken up from sleep

DEBUG_Write(DEBUG_PIN_VALUE = ~DEBUG_PIN_VALUE); // Toggle desired pin value and write to pin

    }

}



Re: How do I compile for sleep on PSoC5

Bob Marlowe posted on 27 Jun 2012 07:51 AM PST
Top Contributor
1768 Forum Posts

I hadn't time to test this, but the Cypress's example used an Isr to get woken up.


Look at at project here www.cypress.com/?id=4&rID=58953 


Hope that helps


Bob



Re: How do I compile for sleep on PSoC5

srim posted on 27 Jun 2012 09:29 AM PST
Cypress Employee
121 Forum Posts

 Hello Helmut,

 

You are not clearing the interrupt which can be done using CyPmReadStatus(CY_PM_CTW_INT) which can be done in the interrupt routine of the isr connected to sleeptimer.

You can try the below lines of code:

 

#include <device.h>

#include <cyPm.h>

 

 

CY_ISR(MY_ISR)

{

CyPmReadStatus(CY_PM_CTW_INT);

}

void main()

{    

CYGlobalIntEnable;  /* Uncomment this line to enable global interrupts. */    

   /* ISR to allow wake up */  

   isr_1_StartEx(MY_ISR);

    

   

    while(1)

    {

SleepTimer_1_Start();

Led_Write(1);

        CyPmSaveClocks();

#if(CY_PSOC5)

CyPmSleep(PM_SLEEP_TIME_NONE,PM_SLEEP_SRC_NONE);

#else

CyPmSleep(PM_SLEEP_TIME_NONE,PM_SLEEP_SRC_PICU);

#endif

 

CyPmRestoreClocks();

Led_Write(0);

SleepTimer_1_Stop();

LCD_Position(1u,0u);

    LCD_PrintString("Woke up");

       

    }

}

 

 

Thanks,

srim



Re: How do I compile for sleep on PSoC5

srim posted on 27 Jun 2012 09:38 AM PST
Cypress Employee
121 Forum Posts

 Alternatively you can call SleepTimer_GetStatus( ) API which in turn calls the CyPmReadStatus( ). I'm starting the SleepTimer component before putting device to sleep and stopping it soon after the wake-up. This is optional and can be done only when you want every sleep interval to behave similarly as the first interval (refer SRG for details on this). That said, you can always start sleeptimer outside the for-loop as you have done in your code above.

 

Thanks,

srim



Re: How do I compile for sleep on PSoC5

Helmut posted on 27 Jun 2012 12:19 PM PST
Top Contributor
48 Forum Posts

DETAIL FOR OTHERS further below.

Thanks, SRIM.  This got me past the hang, but the timing is still not correct.  Please help me some more, if you will please.

My code appears below.  It runs and my DEBUG pin toggles.  But it toggles every 140ms, not every 16ms as expected.  Due to the SleepTimer_1_SetInterval(SleepTimer_1__CTW_16_MS);, I would think it would toggle every 16ms.  Is the clock save and restore causing this?  Note that I have a 20MHz Crystal and so defined in Creator, with a 30MHz PLL output.  I don't know exactly where the CTW gets its clock from.  Researching, perhaps it's the ILO.  My ILO has the default configuration of 1kHz.

So, bottom line, why is my DEBUG pin toggling every 140ms rather than every 16ms???

Thanks.

 

 

#include <device.h>


uint8 DEBUG_PIN_VALUE = 0;


void init()

{

// Initialize debug output pin to 0

DEBUG_Write(DEBUG_PIN_VALUE = 0); // Clear desired pin value and write to pin

// Initialize Sleep Timer

SleepTimer_1_Start(); // Turn on the sleep timer

SleepTimer_1_SetInterval(SleepTimer_1__CTW_16_MS); // Set CTW to 16ms

SleepTimer_1_EnableInt(); // Enable sleep timer interrupt

ISR_SleepTimer_StartEx(ISR_SleepTimer_Interrupt);

}

 


void main()

{

    init();


    CyGlobalIntEnable; // Enable global interrupts

    for(;;)

    {

// Sleep until next SleepTimer interrupt

 

CyPmSaveClocks(); // Save clock configuration before entering sleep

CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_NONE); // Go to sleep for CTW time, previously set to 16ms.  Note PSOC5 requires CTW set up previously and pass NONE to two arguments

CyPmRestoreClocks(); // Restore clock configuration after exiting sleep

// DEBUG - Twiddle a pin to show we have woken up from sleep

DEBUG_Write(DEBUG_PIN_VALUE = ~DEBUG_PIN_VALUE); // Toggle desired pin value and write to pin

    }

 

}

 

 

DETAIL FOR OTHERS:

Note that you can't simply call SleepTimer_GetStatus( ) or CyPmReadStatus( ) from your main loop.  It must be called from the ISR.  And, I guess depending on how you use the Creator, you can't simply define CY_ISR as SRIM's example implies.  

Instead, here's what I had to do.  First, my SleepTimer was routed to an ISR component named "ISR_SleepTimer".  I had to go find the auto-generated ISR_SleepTimer.c and find the auto-generated CY_ISR() inside there.  It has provision to add lines of code.  Inside that region, I added the CyPmReadStatus(CY_PM_CTW_INT);.

In addition, it's important not to miss SRIM's "isr_1_StartEx(MY_ISR);".  For my naming, this becomes ISR_SleepTimer_StartEx(ISR_SleepTimer_Interrupt);, which I put right after my other SleepTimer init stuff.



Re: How do I compile for sleep on PSoC5

Helmut posted on 27 Jun 2012 01:12 PM PST
Top Contributor
48 Forum Posts

 ...ok, it appears that the 140ms is because other things in the loop, such as clock save and restore, can't run fast enough to stay within a 16ms timing loop.  Therefore, just as is done in the example project suggested by Bob Marlowe (http://www.cypress.com/?id=4&rID=58953), I put my sleep inside a loop to occur 64 times, where 64*16ms=1.024s, or approximately 1 second.

Well, I got 640ms instead of 1s.  I put in a second debug pin inside the loop.  It's occuring 64 times per other debug pin outside the loop -- as expected.  However, the second debug pin is toggling every 8.2ms.  It should be every 16ms.

So my NEW problem is this:  Why is CyPmSleep() returning approximately every 8.2ms when my SleepTimer_1 is configured to generate an interrupt every 16ms?

(Overall, it's curious that 8.2ms is very close to 16ms/2.  Also, the 640ms outside loop time exceeds 64*8.2=524.8ms, suggesting 115.2ms of overhead in the loop.  This suggestion is reasonably consistent with the prior 140ms loop with assumed overhead of 140-16=124ms.)

BUT I'M STILL NOT THERE YET.  Again, why 8.2ms per CyPmSleep()?

Thanks.



Re: How do I compile for sleep on PSoC5

Helmut posted on 27 Jun 2012 01:20 PM PST
Top Contributor
48 Forum Posts

 ...hmm, someone PLEASE CONFIRM for me.  The ILO is specified as 1kHz -50% +100%.  If I'm getting 8.2ms instead of 16ms, that's consistent with the ILO being +95% fast.  That's WAY near the outer bound, which I would not expect.

So, is this merely a very bad example of ILO inaccuracy, or am I off by a factor of two somewhere somehow?



Re: How do I compile for sleep on PSoC5

Bob Marlowe posted on 27 Jun 2012 02:00 PM PST
Top Contributor
1768 Forum Posts

Again, please refer to the System Reference Guide which shows, that for a PSoC5 the sleep-time is half of the CTW-timer set. So set at 16ms it will wait for 8ms which is according to your measured 8.2ms. And 8ms is the max. value for PSoC5.

Bob



Re: How do I compile for sleep on PSoC5

Helmut posted on 27 Jun 2012 02:12 PM PST
Top Contributor
48 Forum Posts

 Doh! Done. Thx.



Re: How do I compile for sleep on PSoC5

SpudPug posted on 27 Jun 2012 02:38 PM PST
Cypress Employee
1 Forum Post

The behavior you're observing is known in the PSoC 5 and you're not doing anything wrong. The CyPmSleep section of the System Reference guide explains that the amount of time the PSoC 5 is alseep will be half the time specified in the function parameter, minus up to 1 ms. This is the result of how the API handles the timer when preparing PSoC 5 for sleep, as well as where the 1 kHz ILO edge is at when sleep actually occurs. You can continue as you have been, with the wake and increment approach, without worry.

Just so you know, a new application note (AN77900 - http://www.cypress.com/?rID=64554) focused on using the low-power features of PSoC 3/5 has just been posted to the Cypress webiste. It may have some other information that can help you. We'd love to hear your feedback about it and any ideas for additional content.



Re: How do I compile for sleep on PSoC5

srim posted on 27 Jun 2012 09:41 PM PST
Cypress Employee
121 Forum Posts

 One more thing to note : IMO should be the source for Master clock in sleep mode. So your 20 MHz XTAL clock is switched to 12 MHz IMO(48Mhz if Fast IMO is enabled) before entering low power modes.



Re: How do I compile for sleep on PSoC5

Bob Marlowe posted on 28 Jun 2012 08:51 AM PST
Top Contributor
1768 Forum Posts

There's a brand-new app-note for low power. Look here https://secure.cypress.com/?rID=64726 

Bob



Re: How do I compile for sleep on PSoC5

Helmut posted on 28 Jun 2012 10:11 AM PST
Top Contributor
48 Forum Posts

 Please see http://www.cypress.com/?app=forum&id=2233&rID=64739






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20