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

How to use of RTC to wake up from sleep

Hello,

I am after an example for nRF5340 showing how to use RTC to wake up from sleep.

I have a couple of timers running few tasks at regular intervals in a forever loop then I send the chip to sleep. At this point everything stops including the timers.

I need an interrupt source to wake the chip up and the RTC seems like a good way but I am not sure how to set it up and configure it to use.

Note, I am using SES V5.34a as a development environment and Zephyr V1.5.0-rc1.

Thank you.

Kind regards

Mohamed

Parents
  • Hi Mohamed,

    The Zephyr RTOS handles this for you. In the idle thread it will essentially call WFE, to enter system on low power mode. If you want to wake up after some time to do some work, you can use k_sleep() or similar. You can refer to the basic thread example to see how this is done.

    You are free to use an RTC peripheral directly and do it that way, but there are few good reasons for not using the build in thread and work queues support in Zephyr as suggested above.

    Einar

  • Thank you Einar for the quick response.

    I am not using threads for now. I may be using threads at some point in the near future.

    You are free to use an RTC peripheral directly and do it that way,

    Could you please provide an example where the RTC is used to come out of sleep?

    but there are few good reasons for not using the build in thread and work queues support in Zephyr as suggested above.

    Are you saying there are few good reasons for NOT using the built-in thread and work queues?

    In other ways you are discouraging me from using threads. Is this what you meant to say?

    Kind regards

    Mohamed

  • Thank you Einar.

    I experimented with the ADC and stopped reading the ADC results to see what effect it has on current consumption. To my disbelief the current consumption increased. How can this be?

    Note, the ADC was still initialised and bound. For the ADC peripheral there must be a disable or power down procedure to go through.

    I have not played with the SPI yet.

    Kind regards

    Mohamed

  • Hi Mohamed,

    Are you using the nrfx_saadc API? If so you can call nrfx_saadc_uninit() after finishing the conversion. If current consumption is still high after uninitializing then you can use the following as well:

        *(volatile uint32_t *)0x40007FFC = 0;
        *(volatile uint32_t *)0x40007FFC = 1;

    The above snippet uses an undocumented register to reset the SAADC peripheral.

    Einar

  • Hi Einar,

    Thank you for the tips.

    Are you using the nrfx_saadc API?

    I am not sure. How do I know for sure if I am using nrfx_saadc API?

    I looked in the build.emProject file and I can see the following which suggests I am using nrfx_saadc API. An extract from build.emProject is included below. The file nrfx_saadc_uninit() is declared in the header file C:\Zypher\v1.5.0-rc1\modules\hal\nordic\nrfx\drivers\include\nrfx_saadc.h which is included in the project file below. However, I cannot build the project because it returns the error undefined reference to nrfx_saadc_uninit().

    </project>
    <project Name="app/libapp.a">
    <configuration
    Name="Common"
    build_output_file_name="app\libapp.a"
    project_dependencies="cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;cmake_object_order_depends_target_app;zephyr/driver_validation_h_target;zephyr/kobj_types_h_target;zephyr/syscall_list_h_target;zephyr/zephyr_generated_headers"
    project_directory=""
    project_type="Externally Built Library" />
    <folder Name="C_COMPILER__app_">
    <file file_name="../src/adc/pid4_adc.c">
    <configuration
    Name="Common"
    build_dependency_file_name="$(ProjectDir)/CMakeFiles\app.dir\src\adc\pid4_adc.c.obj.d"
    build_object_file_name="CMakeFiles/app.dir/src/adc/pid4_adc.c.obj"
    c_only_additional_options="-DBUILD_VERSION=v2.4.99-ncs1-rc1;-DKERNEL;-DNRF5340_XXAA_APPLICATION;-DUSE_PARTITION_MANAGER=0;-D_FORTIFY_SOURCE=2;-D__LINUX_ERRNO_EXTENSIONS__;-D__PROGRAM_START;-D__ZEPHYR__=1;
    -IC:/Zypher/v1.5.0-rc1/modules/hal/nordic/nrfx/drivers/include
    -IC:/Zypher/v1.5.0-rc1/zephyr/include;-Izephyr/include/generated;
    -IC:/Zypher/v1.5.0-rc1/zephyr/soc/arm/nordic_nrf/nrf53;
    -IC:/Zypher/v1.5.0-rc1/zephyr/lib/libc/newlib/include;
    -IC:/Zypher/v1.5.0-rc1/nrf/include;
    -IC:/Zypher/v1.5.0-rc1/modules/hal/cmsis/CMSIS/Core/Include;
    -IC:/Zypher/v1.5.0-rc1/modules/hal/nordic/nrfx;
    -IC:/Zypher/v1.5.0-rc1/modules/hal/nordic/nrfx/drivers/include;
    -IC:/Zypher/v1.5.0-rc1/modules/hal/nordic/nrfx/mdk;
    -IC:/Zypher/v1.5.0-rc1/zephyr/modules/hal_nordic/nrfx/.;
    -IC:/Zypher/v1.5.0-rc1/modules/debug/segger/rtt;
    -isystem;C:/Zypher/v1.5.0-rc1/nrfxlib/crypto/nrf_cc312_platform/include;
    -Og;-imacros;
    ...

    I have this config line in my prj.conf file 

    CONFIG_ADC=y

    Which header file should I be including?

    Kind regards

    Mohamed

  • Hi Mohamed,

    Learner said:
    I am not sure. How do I know for sure if I am using nrfx_saadc API?

    You can know by looking at the functions you call to use the SAADC. Do you for instance call functions that look like nrfx_saadc_() or do they look like adc_*()? I assume the latter as you get undefined reference to nrfx_saadc_uninit().

    If you want to use the nrfx API instead then you should include the nrfx_saadc.h file. This may be useful if you are not able to get the ADC current consumption down without disabling it, as the Zephyr ADC API does not have any API for that.

    Br,

    Einar

  • Hello Einar,

    Thank you.

    You are right, I am using functions adc_read() to read adc results. So, I am using Zephyr ADC NOT nrfx_saadc API. These are the header files I am currently including in my adc.c file.

    #include <zephyr.h>
    #include <kernel.h>
    #include <sys/printk.h>
    #include <drivers/adc.h>
    #include <hal/nrf_saadc.h> /*ADC definitions and includes*/
    #include <device.h>
    #include <drivers/gpio.h>

    Which header files should I be including for nrfx_saadc APIs and which one ones should I remove from the ones currently included above or can they co-exist?

    Also, do I have to change the prj.conf file?

    it currently contains this line CONFIG_ADC=y.

    Finally, do you an example you can share with me that uses nrfx_saadc APIs

    Kind regards

    Mohamed

Reply
  • Hello Einar,

    Thank you.

    You are right, I am using functions adc_read() to read adc results. So, I am using Zephyr ADC NOT nrfx_saadc API. These are the header files I am currently including in my adc.c file.

    #include <zephyr.h>
    #include <kernel.h>
    #include <sys/printk.h>
    #include <drivers/adc.h>
    #include <hal/nrf_saadc.h> /*ADC definitions and includes*/
    #include <device.h>
    #include <drivers/gpio.h>

    Which header files should I be including for nrfx_saadc APIs and which one ones should I remove from the ones currently included above or can they co-exist?

    Also, do I have to change the prj.conf file?

    it currently contains this line CONFIG_ADC=y.

    Finally, do you an example you can share with me that uses nrfx_saadc APIs

    Kind regards

    Mohamed

Children
Related