RTC

 #define RTC_INSTANCE   0
  nrf_drv_rtc_t  rtc = NRF_DRV_RTC_INSTANCE(RTC_INSTANCE);

  void Rtc_Handler(nrf_drv_rtc_int_type_t int_type)
  {
    if(int_type == NRF_DRV_RTC_INT_TICK)
    {
      nrf_gpio_pin_toggle(LED_1);
      
    }
  }
  //configure low frequency clock
  static void Low_freq_clk(void)
  {
    ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request(NULL);
  }

  //RTC configuration
  void Rtc_Config(void)
  {
  ret_code_t error_code;
  nrf_drv_rtc_config_t rtc_config = NRF_DRV_RTC_DEFAULT_CONFIG;
  rtc_config.prescaler = 4095;
  error_code = nrf_drv_rtc_init(&rtc,&rtc_config,Rtc_Handler);
  APP_ERROR_CHECK(error_code);
  nrfx_rtc_tick_enable(&rtc,true);
  nrf_drv_rtc_enable(&rtc);
  
  }

i am trying to toggle led with the help of rtc but  event = NRF_RTC_EVENT_TICK; was triggered initially afterwards event = NRF_RTC_EVENT_OVERFLOW; was triggered and why that overflow event was triggered?

Parents
  •   /**
      * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
      *
      * All rights reserved.
      *
      * Redistribution and use in source and binary forms, with or without modification,
      * are permitted provided that the following conditions are met:
      *
      * 1. Redistributions of source code must retain the above copyright notice, this
      *    list of conditions and the following disclaimer.
      *
      * 2. Redistributions in binary form, except as embedded into a Nordic
      *    Semiconductor ASA integrated circuit in a product or a software update for
      *    such product, must reproduce the above copyright notice, this list of
      *    conditions and the following disclaimer in the documentation and/or other
      *    materials provided with the distribution.
      *
      * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
      *    contributors may be used to endorse or promote products derived from this
      *    software without specific prior written permission.
      *
      * 4. This software, with or without modification, must only be used with a
      *    Nordic Semiconductor ASA integrated circuit.
      *
      * 5. Any software provided in binary form under this license must not be reverse
      *    engineered, decompiled, modified and/or disassembled.
      *
      * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
      * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
      * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
      * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
      * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
      * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      *
      */
      /** @file
      *
      * @defgroup blinky_example_main main.c
      * @{
      * @ingroup blinky_example
      * @brief Blinky Example Application main file.
      *
      * This file contains the source code for a sample application to blink LEDs.
      *
      */
    
      #include <stdbool.h>
      #include <stdint.h>
      #include "nrf_delay.h"
      #include "boards.h"
      #include "DAC.h"
    
      #include "stdio.h"
      #include "nrf_drv_spi.h"
      #include "nrf_gpio.h"
      #include <string.h>
      //#include "nrf_twi.h"
      //#include "nrfx_twi.h"
      #include "nrf_drv_twi.h"
      #include "nrf_log.h"
      #include "nrf_log_ctrl.h"
      #include "nrf_log_default_backends.h"
    
      #include "nrf_drv_uart.h"
      #include"nrf_uart.h"
      #include "nrf_temp.h"
      #include "nrf_drv_ppi.h"
      #include "nrf_drv_saadc.h"
      #include "nrf_drv_timer.h"
      #include "nrf_drv_rtc.h"
      #include "nrf_drv_clock.h"
      #include "nrf.h"
      #include "app_error.h"
      #include "nrf_drv_gpiote.h"
     
    
      //#define TWI_ADDRESSES      127
      #define EEPROM_ADDR          0xA0
      #define SAMPLES_IN_BUFFER    5
     
    
      //define the SPI instance
      #define SPI_INSTANCE   0 /**< SPI instance index. */
      nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
      bool spi_xfer_done;
    
      //define the I2C instance
      #define TWI_INSTANCE  1
      nrf_drv_twi_t twi =  NRF_DRV_TWI_INSTANCE(TWI_INSTANCE);
      static volatile bool  m_Rx_done = false;
    
      //define the uart instance
      #define UART_INSTANCE  0
      nrf_drv_uart_t uart = NRF_DRV_UART_INSTANCE(UART_INSTANCE);
      bool  uart_xfer_done = false;
    
      //define the timer instance
      #define TIMER_INSTANCE  0
      nrf_drv_timer_t timer = NRF_DRV_TIMER_INSTANCE(TIMER_INSTANCE);
    
     // static nrf_saadc_value_t       buffer[2][SAMPLES_IN_BUFFER];
      nrf_ppi_channel_t              ppi_channel;
    
     // uint8_t sample_data = 12;
    
      //define rtc instance
      #define RTC_INSTANCE    2
      nrf_drv_rtc_t  rtc = NRF_DRV_RTC_INSTANCE(RTC_INSTANCE);
    
      void Rtc_Handler(nrf_drv_rtc_int_type_t int_type)
      {
        if(int_type == NRF_DRV_RTC_INT_TICK)
        {
          nrf_gpio_pin_toggle(LED_1);
          
        }
      }
      //configure low frequency clock
      static void Low_freq_clk(void)
      {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_clock_lfclk_request(NULL);
      }
    
      //RTC configuration
      void Rtc_Config(void)
      {
        ret_code_t error_code;
        nrf_drv_rtc_config_t rtc_config = NRF_DRV_RTC_DEFAULT_CONFIG;
        rtc_config.prescaler = 4095;
        error_code = nrf_drv_rtc_init(&rtc,&rtc_config,Rtc_Handler);
        APP_ERROR_CHECK(error_code);
        nrfx_rtc_tick_enable(&rtc,true);
        nrf_drv_rtc_enable(&rtc);
      
      }
    
      void timer_event_handler(nrf_timer_event_t *p_event ,void*context)
      {
         uint8_t sample_data = 1;
         nrf_drv_uart_tx(&uart,&sample_data,sizeof(sample_data));
         nrf_delay_ms(1000);
    
      }
    
      //timer with ppi
      void timer_led_Init(void)
      {
        ret_code_t error_code;
        error_code = nrf_drv_ppi_init();
        APP_ERROR_CHECK(error_code);
        error_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(error_code);
    
        nrf_drv_gpiote_out_config_t gpio_config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false);
        error_code = nrf_drv_gpiote_out_init(LED_1,&gpio_config);
        APP_ERROR_CHECK(error_code);
        nrf_drv_timer_config_t  timer_config = NRF_DRV_TIMER_DEFAULT_CONFIG;
        timer_config.bit_width = NRF_TIMER_BIT_WIDTH_32;
        error_code = nrf_drv_timer_init(&timer,&timer_config,timer_event_handler);
    
        uint32_t ticks = nrf_drv_timer_ms_to_ticks(&timer,2000);
        APP_ERROR_CHECK(error_code);
        nrf_drv_timer_extended_compare(&timer,NRF_TIMER_CC_CHANNEL0,ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,false);
      
    
        uint32_t timer_event_address = nrf_drv_timer_event_address_get( &timer,NRF_TIMER_EVENT_COMPARE0);
        uint32_t task_address = nrf_drv_gpiote_out_task_addr_get(LED_1);
        error_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
        APP_ERROR_CHECK(error_code);
        error_code = nrf_drv_ppi_channel_assign( ppi_channel,timer_event_address,task_address);
        APP_ERROR_CHECK(error_code);
        error_code = nrf_drv_ppi_channel_enable(ppi_channel);
        APP_ERROR_CHECK(error_code);
        nrf_drv_gpiote_out_task_enable(LED_1);
       
      }
    
      void Sampling_evnt_Init(void)
      {
        ret_code_t error_code;
        error_code = nrf_drv_ppi_init();
        APP_ERROR_CHECK(error_code);
        nrf_drv_timer_config_t  timer_config = NRF_DRV_TIMER_DEFAULT_CONFIG;
        timer_config.bit_width = NRF_TIMER_BIT_WIDTH_32;
        error_code = nrf_drv_timer_init(&timer,&timer_config,timer_event_handler);
    
        uint32_t ticks = nrf_drv_timer_ms_to_ticks(&timer,400);
        APP_ERROR_CHECK(error_code);
        nrf_drv_timer_extended_compare(&timer,NRF_TIMER_CC_CHANNEL0,ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,false);
        nrf_drv_timer_enable(&timer);
    
        uint32_t timer_event_address = nrf_timer_compare_event_get(NRF_TIMER_CC_CHANNEL0);
        uint32_t saadc_task_address = nrf_drv_saadc_sample_task_get( );
    
        error_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
        APP_ERROR_CHECK(error_code);
    
        error_code = nrf_drv_ppi_channel_assign( ppi_channel,timer_event_address,saadc_task_address);
        APP_ERROR_CHECK(error_code);
      }
      void Saadc_event(nrf_drv_saadc_evt_t const * p_event)
      {
        /* ret_code_t error_code;
         uint8_t voltage ;
         if(p_event->type == NRF_DRV_SAADC_EVT_DONE)
         {
           error_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer,SAMPLES_IN_BUFFER);
           APP_ERROR_CHECK(error_code);
           for(uint8_t i = 0; i < SAMPLES_IN_BUFFER ; i++)
           {
             voltage = p_event->data.done.p_buffer[i]*3.6/1024;
             error_code = nrf_drv_uart_tx(&uart,&voltage,SAMPLES_IN_BUFFER);
           }
         }*/
    
      }
    
      void Sampling_Event_Enable(void)
      {
        ret_code_t error_code;
        error_code = nrf_drv_ppi_channel_enable(ppi_channel);
        APP_ERROR_CHECK(error_code);
      }
    
      void Saadc_Init(void)
      {
        ret_code_t error_code;
        nrf_saadc_channel_config_t  channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
        error_code = nrf_drv_saadc_init(NULL,Saadc_event);
        APP_ERROR_CHECK( error_code);
        error_code = nrf_drv_saadc_channel_init(0,&channel_config);
        APP_ERROR_CHECK(error_code);
        /*
        error_code = nrf_drv_saadc_buffer_convert(buffer[0],SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(error_code);
        error_code = nrf_drv_saadc_buffer_convert(buffer[1],SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(error_code);*/
      }
    
      void spi_event_handler(nrf_drv_spi_evt_t const * p_event,void *p_context)
      {
        spi_xfer_done = true;
      //  NRF_LOG_INFO("Transfer completed.");
      /*  if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }*/
      }
    
      void Spi0_init(void)
      {
        nrf_drv_spi_config_t spi_config ;
        spi_config.ss_pin   = SPI_SS_PIN;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        spi_config.frequency = NRF_SPI_FREQ_4M;
        spi_config.mode = NRF_SPI_MODE_0;
        spi_config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
      }
    
      //I2c event handler
      void twi_event( nrf_drv_twi_evt_t const * p_event_twi ,void *p_context_twi)
      {
        uint32_t err_code;
        uint8_t Tx_buffer[3] = {0};
        uint8_t Rx_buffer[3] = {0};
        bool detected_device = false;
        switch(p_event_twi->type)
        {
        case NRF_DRV_TWI_EVT_DONE :
    
        if (p_event_twi->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
        {
          // err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 2,0);
          // err_code = nrf_drv_twi_rx(&twi, (EEPROM_ADDR >> 1), Rx_buffer, 1);
          m_Rx_done = true;
        }
        break;
        default:
        break;
    
    
        }
    
      }
    
      void uart_handler(nrf_drv_uart_event_t *p_event)
      {
        if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
        {
            uart_xfer_done =true;
        }
        if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
        {
            uart_xfer_done =true;
        }
     
      }
      void I2C_1_init(void)
      {
     
        uint32_t err_code;
    
        nrf_drv_twi_config_t const config = {
        .scl                = ARDUINO_SCL_PIN,
        .sda                = ARDUINO_SDA_PIN,
        .frequency          = NRF_TWI_FREQ_400K,
        .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
        .clear_bus_init     = false,
        };
    
        err_code = nrf_drv_twi_init(&twi,&config,twi_event,NULL);
        APP_ERROR_CHECK(err_code);
        nrf_drv_twi_enable(&twi);
      }
    
      void UART_init(void)
      {
        uint32_t err_code;
        nrf_drv_uart_config_t const config = {
        .pselrxd = SER_CON_RX_PIN,
        .pseltxd = SER_CON_TX_PIN,
        .pselrts = SER_CON_RTS_PIN,
        .pselcts = SER_CON_CTS_PIN,
        .baudrate = NRF_UART_BAUDRATE_115200,
        .hwfc = NRF_UART_HWFC_DISABLED,
        .parity = NRF_UART_PARITY_INCLUDED,
        .interrupt_priority = 6,
        };
        err_code = nrf_drv_uart_init(&uart,&config,uart_handler);
    
      }
    
    void Mem_Read(void)
    {
        uint32_t err_code;
        uint8_t Tx_buffer[3] = {0};
        uint8_t Rx_buffer[3] = {0};
        bool detected_device = false;
        // nrf_delay_ms(1000);
        err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 2,0);
        nrf_delay_ms(1000);
        err_code = nrf_drv_twi_rx(&twi, (EEPROM_ADDR >> 1), Rx_buffer, 1);
        if (err_code == NRF_SUCCESS)
        {
            detected_device = true;
            NRF_LOG_INFO("TWI device detected at address 0x%x.",EEPROM_ADDR );
        }
        NRF_LOG_FLUSH();
    
        if (!detected_device)
        {
          NRF_LOG_INFO("No device was found.");
          NRF_LOG_FLUSH();
        }
    
    }
    
    void Mem_Write(void)
    {
        uint32_t err_code;
        uint8_t Tx_buffer[3] = {0};
        uint8_t Rx_buffer[3] = {0};
        bool detected_device = false;
        Tx_buffer[0] = 0x00;
        Tx_buffer[1] = 0x00;
        Tx_buffer[2] = 0x12;
        err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 3,0);
        if (err_code == NRF_SUCCESS)
        {
        detected_device = true;
        NRF_LOG_INFO("TWI device detected at address 0x%x.", EEPROM_ADDR);
        }
        NRF_LOG_FLUSH();
    
        if (!detected_device)
        {
        NRF_LOG_INFO("No device was found.");
        NRF_LOG_FLUSH();
        }
        nrf_delay_ms(1000);
    }
    
    void init_temp_sensor(void)
    {
      NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
      NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
      NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
      NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
      NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
      NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
      NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
      NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
      NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
      NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
      NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
      NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
      NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
      NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
      NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
      NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
      NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
    
    }
    
    float read_temp_sensor(void)
    {
      int result;
      NRF_TEMP->TASKS_START = 1;
      NRF_TEMP->EVENTS_DATARDY = 0;
      // Read it back to ensure it is written (needed on cortex m4)
      volatile uint32_t dummy = NRF_TEMP->EVENTS_DATARDY;
      (void) dummy;
      while (0 == NRF_TEMP->EVENTS_DATARDY);
      result = NRF_TEMP->TEMP;
      NRF_TEMP->TASKS_STOP = 1;
      return (float)result / 4;
    }
    
      /**
      * @brief Function for application main entry.
      */
      int main(void)
      {
      // float voltage;
       /* uint32_t err_code;
        uint8_t Tx_buffer[3] = {0};
        uint8_t Rx_buffer[3] = {0};
        bool detected_device = false;
        float temp;
        /* Configure board. */
        bsp_board_init(BSP_INIT_LEDS);
       // Spi0_init();
       // I2C_1_init();
       // UART_init();
      //  Mem_Write();
      //  Mem_Read();
      //  temp = read_temp_sensor();
    
       /*timer_led_Init();
        nrf_drv_timer_enable(&timer);*/
    
       /* Saadc_Init();
        nrf_saadc_value_t adc_value;*/
    
          Low_freq_clk();
          Rtc_Config();
        
       
     /*   
        Tx_buffer[0] = 0x00;
        Tx_buffer[1] = 0x00;
        Tx_buffer[2] = 0x12;
        err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 3,0);
        if (err_code == NRF_SUCCESS)
        {
            detected_device = true;
            NRF_LOG_INFO("TWI device detected at address 0x%x.", EEPROM_ADDR);
        }
        NRF_LOG_FLUSH();
    
        if (!detected_device)
        {
            NRF_LOG_INFO("No device was found.");
            NRF_LOG_FLUSH();
        }
          nrf_delay_ms(1000);
    
          err_code = nrf_drv_twi_tx(&twi, (EEPROM_ADDR >> 1), Tx_buffer, 2,0);
          err_code = nrf_drv_twi_rx(&twi, (EEPROM_ADDR >> 1), Rx_buffer, 1);
        if (err_code == NRF_SUCCESS)
          {
              detected_device = true;
              NRF_LOG_INFO("TWI device detected at address 0x%x.",EEPROM_ADDR );
          }
          NRF_LOG_FLUSH();
    
        if (!detected_device)
        {
            NRF_LOG_INFO("No device was found.");
            NRF_LOG_FLUSH();
        }*/
    
       // err_code = nrf_drv_uart_tx(&uart,&sample_data,sizeof(sample_data));
      
    
        /* Toggle LEDs. */
        while (true)
        {
          /* nrfx_saadc_sample_convert(0,&adc_value);
           nrf_delay_ms(500);
           voltage = adc_value*3.6/1024;*/
         //  nrf_drv_uart_tx(&uart,&voltage,sizeof(voltage));
      
          /*  for (int i = 0; i < LEDS_NUMBER; i++)
            {
                bsp_board_led_invert(i);
                nrf_delay_ms(500);
            }*/
          
         /*   for(uint16_t i = 500; i <= 2500; i += 500)
            {
                 Dac_Input(channel_A,i);
                 nrf_delay_ms(2000);
            }*/
          //  err_code = nrf_drv_uart_rx(&uart,&sample_data,1);
          
        }
      }
    
      /**
      *@}
      **/
    

    when i try like this first time rtc event triggered but next time onwards overflow event was triggered can i know which configuration i am suppose to change?

  • i have enabled spi uart twi did it affect the RTC and watch dog timer?

  • I am not sure, RTC is pretty simple to use and we also have a very clear example at examples\peripheral\rtc\main.c that matches your configuration. Tick event should work fine, it is well tested. Disable everything else other than RTC and verify that it is working and build your application logic on top of the working RTC.

Reply Children
No Data
Related