This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

interrupt (X16IRQ) from XOSC16M instead "while (hal_clk_get_16m_source() != HAL_CLK_XOSC16M) {}"

Hello! I want to use the start time of the XOSC16M in NRF24LE1. I got ~4 ms. Can I put the processor to sleep at this time in standby mode? How to do it to wait for wakeup from (X16IRQ) from XOSC16M active?

  • Hi,

    I think you can take a look at this:

    /*
    * An nRF24LE1 REGISTER RETENTION TIMER ON example application 
    */
    
    #include <Nordic\reg24le1.h>
    #include "hal_clk.h"
    #include "hal_rtc.h"
    
    void mcu_init(void)
    {
    P0CON = 0x70; // Disable digital input buffer, pin used for 32kHz.
    P0CON = 0x71; // Disable digital input buffer, pin used for 32kHz.
    
    P0DIR = 0x03; // Two pins used for 32 kHz XO.
    P1DIR = 0x00;
    P2DIR = 0x00;
    P3DIR = 0x00;
    
    P0 = 0x00;
    P1 = 0x00;
    P2 = 0x00;
    P3 = 0x00;
    }
    
    void wakeup_tick() interrupt INTERRUPT_TICK
    {
    P02 = !P02;
    }
    
    void main(void)
    {
    mcu_init();
    
    hal_rtc_start(false);
    hal_clklf_set_source(HAL_CLKLF_XOSC32K); 
    hal_rtc_set_compare_mode(HAL_RTC_COMPARE_MODE_0);
    hal_rtc_set_compare_value(0x7FFF);
    hal_rtc_start(true);
    
    // Wait for the 32kHz to startup (change phase)
    while((CLKLFCTRL&0x80)==0x80);
    while((CLKLFCTRL&0x80)!=0x80);
    
    IEN1 = 0x20;
    EA = 1; 
    
    while(1)
    {
    // Register retention mode
    PWRDWN = 0x04;
    // Standby mode (wait for interrupt, during this period the external 16MHz will startup)
    PWRDWN = 0x07;
    // Clear PWRDWN
    PWRDWN = 0x00;
    // Continue to run code
    P03 = !P03; 
    };
    }

    The reason there is a "double" PWRDWN call in the while(1)-loop as shown below, is because the system will wakeup 1.5ms prior to the actual RTC wakeup tick. The system does so to startup the 16MHz crystal, to make sure the 16MHz crystal is running by the time the RTC wakeup tick occurs. During this 1.5ms period the CPU can go to standby to save current until the RTC wakeup tick occurs (another approach would be to use a flag in the while loop, and wait until the flag has been cleared in the RTC wakeup tick.

    // Register retention mode

    PWRDWN = 0x04;

    // Standby mode (wait for interrupt, during this period the external 16MHz will startup)

    PWRDWN = 0x07;

    Note, that the startup time must be below 1.5ms. So if you measure 4ms, then you must replace the 16MHz crystal with one that fulfill the stated crystal spec in the nRF24LE1 datasheet.

    Best regards,
    Kenneth

  • Thank! I realized that stand-up mode can not do. I made the capacitors of a crystal oscillator 12 pF.

Related