CMSIS-Driver_PIC32CM-PL  
Peripheral Library (PLIB) Documentation
 
Loading...
Searching...
No Matches
RTC Interface

Driver API for RTC (plib_rtc_16count.h) More...

Data Structures

struct  RTC_16COUNT_OBJECT
 RTC 16-bit counter object structure. More...
 

Typedefs

typedef uint32_t RTC_16COUNT_INT_MASK
 RTC 16-bit counter interrupt mask type.
 
typedef void(* RTC_16COUNT_CALLBACK) (RTC_16COUNT_INT_MASK int_cause)
 RTC callback function type for 16-bit counter mode.
 

Functions

void RTC_16COUNT_Initialize (void)
 Initializes the RTC in 16-bit counter mode. This sets up the interrupt handler for RTC 16-bit counter mode.
 
void RTC_16COUNT_SoftwareReset (void)
 Performs a software reset of the RTC module.
 
void RTC_16COUNT_SetMode (void)
 Configures the RTC to operate in 16-bit counter mode.
 
void RTC_16COUNT_Enable (void)
 Enables the RTC 16-bit counter.
 
void RTC_16COUNT_Disable (void)
 Disables the RTC 16-bit counter.
 
void RTC_16COUNT_SetPrescaler (uint16_t prescaler)
 Configures the RTC prescaler.
 
void RTC_16COUNT_CorrectFrequency (int8_t correction)
 Applies frequency correction to the RTC.
 
void RTC_16COUNT_EnableCountSync (void)
 Enables synchronization of the RTC counter.
 
void RTC_16COUNT_DisableCountSync (void)
 Disables synchronization of the RTC counter.
 
void RTC_16COUNT_SetCounter (uint16_t count)
 Sets the current value of the RTC counter.
 
void RTC_16COUNT_SetCompare0 (uint16_t compare_value)
 Sets the compare value for Compare 0.
 
void RTC_16COUNT_SetCompare1 (uint16_t compare_value)
 Sets the compare value for Compare 1.
 
void RTC_16COUNT_SetPeriod (uint16_t period_value)
 Sets the period value for the RTC counter.
 
uint32_t RTC_16COUNT_GetCounter (void)
 Gets the current value of the RTC counter.
 
uint16_t RTC_16COUNT_GetPeriod (void)
 Gets the value of the RTC period register.
 
void RTC_16COUNT_EnableInterrupt (RTC_16COUNT_INT_MASK interrupt_mask)
 Enables a specific RTC interrupt mask.
 
void RTC_16COUNT_DisableInterrupt (RTC_16COUNT_INT_MASK interrupt_mask)
 Disables a specific RTC interrupt mask.
 
void RTC_16COUNT_SetCallbackHandler (RTC_16COUNT_CALLBACK callback)
 Registers a callback function for RTC interrupts.
 
void RTC_SetInterruptHandler (void(*callback)(void))
 Registers the RTC interrupt callback function. The callback function can be called from main.c for direct callback registration.
 

Variables

RTC_16COUNT_CALLBACK RTC_16COUNT_OBJECT::timer16bit_callback
 Callback function pointer for RTC 16-bit counter interrupts.
 
RTC_16COUNT_INT_MASK RTC_16COUNT_OBJECT::timer16int_cause
 Interrupt cause mask for the last RTC interrupt.
 

Description

Driver API for RTC (plib_rtc_16count.h)

Usage Example

The following example demonstrates typical usage of the RTC peripheral:

/*******************************************************************************
* @file configure_rtc_16count.c
*
* @brief Example usage for RTC 16-bit Counter Mode configuration.
*
* @details
* This Example usage demonstrates how to configure and use the RTC peripheral in
* 16-bit counter mode (Mode 1). The counter counts from 0 up to the period
* value and generates an overflow interrupt when it wraps around.
*
* How it works:
* 1. Initialize and reset the RTC peripheral.
* 2. Set Mode to 16-bit counter and configure the prescaler to divide the
* RTC clock (0xA = divide by 1024).
* 3. Set the Period register (0x20) - the counter wraps at this value.
* 4. Set Compare0 (0x10) - triggers a compare match interrupt at the midpoint.
* 5. Set the initial counter value (0x9).
* 6. Enable overflow and compare match interrupts, register a callback.
* 7. Enable RTC and configure NVIC for interrupt handling.
*
* Available interrupts:
* - RTC_MODE1_INTENSET_OVF_Msk : Counter overflow (counter reached period)
* - RTC_MODE1_INTENSET_CMP0_Msk : Compare 0 match
* - RTC_MODE1_INTENSET_CMP1_Msk : Compare 1 match
*
* Users can modify the prescaler, period, compare, and counter values as
* needed. Add application-specific logic inside the callback or main loop
* to handle the interrupt events.
*******************************************************************************/
#include <pic32c.h>
#include <stdint.h>
#include <stdbool.h>
static volatile bool overflow_event = false;
static volatile bool mid_point_event = false;
/* RTC 16 bit counter callback function */
void RTC_EventCallback(RTC_16COUNT_INT_MASK int_cause){
if ((int_cause & RTC_MODE1_INTENSET_CMP0_Msk) == RTC_MODE1_INTENSET_CMP0_Msk)
{
/* User operation (e.g., toggle GPIO, schedule task) */
mid_point_event = true;
}
if ((int_cause & RTC_MODE1_INTENSET_OVF_Msk) == RTC_MODE1_INTENSET_OVF_Msk)
{
/* User operation (e.g., toggle GPIO, schedule task) */
overflow_event = true;
}
}
int32_t configure_rtc_16count(void)
{
/* Configuration constants */
const uint16_t prescaler = 0xAU;
const uint16_t period = 0x20U;
const uint16_t compare = 0x10U;
const uint16_t start_counter = 0x9U;
const uint32_t priority = 3U;
RTC_16COUNT_SetCounter(start_counter);
RTC_16COUNT_EnableInterrupt(RTC_MODE1_INTENSET_OVF_Msk);
RTC_16COUNT_EnableInterrupt(RTC_MODE1_INTENSET_CMP0_Msk);
RTC_16COUNT_SetCallbackHandler(&RTC_EventCallback);
__DMB();
__enable_irq();
NVIC_SetPriority(RTC_IRQn, priority);
NVIC_EnableIRQ(RTC_IRQn);
return 0;
}
void RTC_16COUNT_Initialize(void)
Initializes the RTC in 16-bit counter mode. This sets up the interrupt handler for RTC 16-bit counter...
uint32_t RTC_16COUNT_INT_MASK
RTC 16-bit counter interrupt mask type.
Definition plib_rtc_16count.h:55
void RTC_16COUNT_SetCompare0(uint16_t compare_value)
Sets the compare value for Compare 0.
void RTC_16COUNT_EnableInterrupt(RTC_16COUNT_INT_MASK interrupt_mask)
Enables a specific RTC interrupt mask.
void RTC_16COUNT_SetCounter(uint16_t count)
Sets the current value of the RTC counter.
void RTC_16COUNT_SetCallbackHandler(RTC_16COUNT_CALLBACK callback)
Registers a callback function for RTC interrupts.
void RTC_16COUNT_Enable(void)
Enables the RTC 16-bit counter.
void RTC_16COUNT_SoftwareReset(void)
Performs a software reset of the RTC module.
void RTC_16COUNT_SetPeriod(uint16_t period_value)
Sets the period value for the RTC counter.
void RTC_16COUNT_SetPrescaler(uint16_t prescaler)
Configures the RTC prescaler.
void RTC_16COUNT_SetMode(void)
Configures the RTC to operate in 16-bit counter mode.

Typedef Documentation

◆ RTC_16COUNT_CALLBACK

typedef void(* RTC_16COUNT_CALLBACK) (RTC_16COUNT_INT_MASK int_cause)

RTC callback function type for 16-bit counter mode.

◆ RTC_16COUNT_INT_MASK

typedef uint32_t RTC_16COUNT_INT_MASK

RTC 16-bit counter interrupt mask type.

Function Documentation

◆ RTC_16COUNT_CorrectFrequency()

void RTC_16COUNT_CorrectFrequency ( int8_t correction)

Applies frequency correction to the RTC.

Parameters
correctionSigned correction value.

◆ RTC_16COUNT_Disable()

void RTC_16COUNT_Disable ( void )

Disables the RTC 16-bit counter.

Returns
void

◆ RTC_16COUNT_DisableCountSync()

void RTC_16COUNT_DisableCountSync ( void )

Disables synchronization of the RTC counter.

Returns
void

◆ RTC_16COUNT_DisableInterrupt()

void RTC_16COUNT_DisableInterrupt ( RTC_16COUNT_INT_MASK interrupt_mask)

Disables a specific RTC interrupt mask.

Parameters
interrupt_maskInterrupt mask to disable.

◆ RTC_16COUNT_Enable()

void RTC_16COUNT_Enable ( void )

Enables the RTC 16-bit counter.

Returns
void

◆ RTC_16COUNT_EnableCountSync()

void RTC_16COUNT_EnableCountSync ( void )

Enables synchronization of the RTC counter.

Returns
void

◆ RTC_16COUNT_EnableInterrupt()

void RTC_16COUNT_EnableInterrupt ( RTC_16COUNT_INT_MASK interrupt_mask)

Enables a specific RTC interrupt mask.

Parameters
interrupt_maskInterrupt mask to enable.

◆ RTC_16COUNT_GetCounter()

uint32_t RTC_16COUNT_GetCounter ( void )

Gets the current value of the RTC counter.

Returns
Current counter value.

◆ RTC_16COUNT_GetPeriod()

uint16_t RTC_16COUNT_GetPeriod ( void )

Gets the value of the RTC period register.

Returns
Period register value.

◆ RTC_16COUNT_Initialize()

void RTC_16COUNT_Initialize ( void )

Initializes the RTC in 16-bit counter mode. This sets up the interrupt handler for RTC 16-bit counter mode.

Returns
void

◆ RTC_16COUNT_SetCallbackHandler()

void RTC_16COUNT_SetCallbackHandler ( RTC_16COUNT_CALLBACK callback)

Registers a callback function for RTC interrupts.

Parameters
callbackPointer to the callback function.

◆ RTC_16COUNT_SetCompare0()

void RTC_16COUNT_SetCompare0 ( uint16_t compare_value)

Sets the compare value for Compare 0.

Parameters
compare_valueCompare 0 match value.
Returns
void

◆ RTC_16COUNT_SetCompare1()

void RTC_16COUNT_SetCompare1 ( uint16_t compare_value)

Sets the compare value for Compare 1.

Parameters
compare_valueCompare 1 match value.
Returns
void

◆ RTC_16COUNT_SetCounter()

void RTC_16COUNT_SetCounter ( uint16_t count)

Sets the current value of the RTC counter.

Parameters
countCounter value to set.
Returns
void

◆ RTC_16COUNT_SetMode()

void RTC_16COUNT_SetMode ( void )

Configures the RTC to operate in 16-bit counter mode.

Returns
void

◆ RTC_16COUNT_SetPeriod()

void RTC_16COUNT_SetPeriod ( uint16_t period_value)

Sets the period value for the RTC counter.

Parameters
period_valuePeriod register value.
Returns
void

◆ RTC_16COUNT_SetPrescaler()

void RTC_16COUNT_SetPrescaler ( uint16_t prescaler)

Configures the RTC prescaler.

This function sets the prescaler field in the RTC CTRLA register. The input parameter is NOT the actual division factor (e.g., 1024), but the encoded value corresponding to the PRESCALER bit field

For example:

  • To achieve a division by 1024, pass the encoded value corresponding to DIV1024 (e.g., 0xB as per datasheet definition).
Note
The RTC must be disabled before calling this function.
Parameters
prescalerEncoded PRESCALER field value (not the division factor).

◆ RTC_16COUNT_SoftwareReset()

void RTC_16COUNT_SoftwareReset ( void )

Performs a software reset of the RTC module.

Returns
void

◆ RTC_SetInterruptHandler()

void RTC_SetInterruptHandler ( void(*)(void) callback)

Registers the RTC interrupt callback function. The callback function can be called from main.c for direct callback registration.

Parameters
[in]callbackPointer to user-defined RTC interrupt handler function. Pass NULL to unregister any existing callback.

Variable Documentation

◆ timer16bit_callback

RTC_16COUNT_CALLBACK RTC_16COUNT_OBJECT::timer16bit_callback

Callback function pointer for RTC 16-bit counter interrupts.

◆ timer16int_cause

RTC_16COUNT_INT_MASK RTC_16COUNT_OBJECT::timer16int_cause

Interrupt cause mask for the last RTC interrupt.