Microchip® Advanced Software Framework

Quick Start Guide for RTC Tamper with DMA

In this use case, the RTC is set up in count mode.

The quick start configures the RTC in 32-bit mode and . The rest of the configuration is according to the default. A callback is implemented for when the RTC capture tamper stamp.

Setup

Prerequisites

The Generic Clock Generator for the RTC should be configured and enabled; if you are using the System Clock driver, this may be done via conf_clocks.h.

Code

Add to the main application source file, outside of any functions:

struct rtc_module rtc_instance;
DmacDescriptor example_descriptor SECTION_DMAC_DESCRIPTOR;

The following must be added to the user application: Function for setting up the module:

void configure_rtc(void)
{
struct rtc_count_config config_rtc_count;
rtc_count_get_config_defaults(&config_rtc_count);
config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
struct rtc_tamper_config config_rtc_tamper;
rtc_tamper_get_config_defaults(&config_rtc_tamper);
config_rtc_tamper.dma_tamper_enable = true;
config_rtc_tamper.in_cfg[0].level = RTC_TAMPER_LEVEL_RISING;
config_rtc_tamper.in_cfg[0].action = RTC_TAMPER_INPUT_ACTION_CAPTURE;
rtc_tamper_set_config(&rtc_instance, &config_rtc_tamper);
}

Callback function:

void rtc_tamper_callback(void)
{
/* Do something on RTC tamper capture here */
LED_On(LED_0_PIN);
}

Function for setting up the callback functionality of the driver:

Add to user application initialization (typically the start of main()):

/* Initialize system. Must configure conf_clocks.h first. */
/* Configure and enable RTC */
configure_rtc();
/* Configure and enable callback */
setup_transfer_descriptor(&example_descriptor);
dma_add_descriptor(&example_resource, &example_descriptor);
while (true) {
/* Infinite while loop */
}

Workflow

  1. Initialize system.
  2. Configure and enable module.
    configure_rtc();
    1. Create a RTC configuration structure to hold the desired RTC driver settings and fill it with the configuration values.
      struct rtc_count_config config_rtc_count;
      rtc_count_get_config_defaults(&config_rtc_count);
      config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
      rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    2. Initialize the RTC module.
      struct rtc_count_config config_rtc_count;
      rtc_count_get_config_defaults(&config_rtc_count);
      config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
      rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
    3. Create a RTC tamper configuration structure and fill it with the configuration values.
      struct rtc_tamper_config config_rtc_tamper;
      rtc_tamper_get_config_defaults(&config_rtc_tamper);
      config_rtc_tamper.dma_tamper_enable = true;
      config_rtc_tamper.in_cfg[0].level = RTC_TAMPER_LEVEL_RISING;
      config_rtc_tamper.in_cfg[0].action = RTC_TAMPER_INPUT_ACTION_CAPTURE;
      rtc_tamper_set_config(&rtc_instance, &config_rtc_tamper);
    4. Enable the RTC module, so that it may begin counting.
  3. Configure callback functionality.
    1. Register overflow callback.
    2. Enable overflow callback.
  4. Configure the DMA.
    1. Create a DMA resource configuration structure, which can be filled out to adjust the configuration of a single DMA transfer.
      struct dma_resource_config config;
    2. Initialize the DMA resource configuration struct with the module's. default values.
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    3. Set extra configurations for the DMA resource. ADC_DMAC_ID_RESRDY trigger causes a beat transfer in this example.
      config.peripheral_trigger = RTC_DMAC_ID_TIMESTAMP;
      config.trigger_action = DMA_TRIGGER_ACTION_BEAT;
    4. Allocate a DMA resource with the configurations.
      dma_allocate(resource, &config);
    5. Create a DMA transfer descriptor configuration structure, which can be filled out to adjust the configuration of a single DMA transfer.
      struct dma_descriptor_config descriptor_config;
    6. Initialize the DMA transfer descriptor configuration struct with the module's default values.
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    7. Set the specific parameters for a DMA transfer with transfer size, source address, and destination address.
      descriptor_config.beat_size = DMA_BEAT_SIZE_WORD;
      descriptor_config.dst_increment_enable = false;
      descriptor_config.src_increment_enable = false;
      descriptor_config.block_transfer_count = 1;
      descriptor_config.source_address = (uint32_t)(&rtc_instance.hw->MODE0.TIMESTAMP.reg);
      descriptor_config.destination_address = (uint32_t)(buffer_rtc_tamper);
      descriptor_config.next_descriptor_address = (uint32_t)descriptor;
    8. Create the DMA transfer descriptor.
      dma_descriptor_create(descriptor, &descriptor_config);
    9. Add DMA descriptor to DMA resource.
      dma_add_descriptor(&example_resource, &example_descriptor);

Implementation

Code

Add to user application main:

  1. Infinite while loop while waiting for callbacks.
    while (true) {
    /* Infinite while loop */
    }

Callback

When the RTC tamper captured, the callback function will be called.

  1. LED0 on for RTC tamper capture:
    /* Do something on RTC tamper capture here */
    LED_On(LED_0_PIN);