MKMxxZxxACxx5 Bare Metal Software Drivers  R4.1.6
Reference Manual
IRTC R/W Calendar

This example shows typical use of the Independent Real Time Clock (IRTC) module and specifically its calendaring, alarm and tamper detection circuitries. Initially RTC is initialized to 20.10.2012 23:59:58 and starts counting. An alarm occurs when RTC counters reach 21.10.2012 00:00:35. The alarm interrupt, serviced by the irtc_callback function, causes re-initialization of the RTC counters to initial values 20.10.2012 23:59:58 and RTC starts counting again. The periodic 1 second event is also serviced by the irtc_callback function. At this event, the content of RTC counters is written into rTime variable. When using TWR-KM34Z50M board, user can also evaluate hardware tamper detection capabilities by pushing SW3 button. On TWR-KM34Z50M board, the SW3 button is connected to the TAMPER1 pin. The tamper interrupt, also serviced by the irtc_callback function, performs reading the time stamp value and storing it into log variable. Make sure J10 pins 1-2 are closed and pins 7-8 are opened prior testing tamper detection capabilities on TWR-KM34Z75M board.

Source code:
/******************************************************************************
* (c) Copyright 2010-2015, Freescale Semiconductor Inc.
* ALL RIGHTS RESERVED.
******************************************************************************
* irtc_test.c
******************************************************************************/
#include <string.h>
#include "drivers.h"
/* Initialize time variable for 20.10.2012 Saturday 23:59:58 */
static tTIME wtime = {58,59,23,20,SATURDAY,OCTOBER,2012}, rtime;
static tALARM atime = {35, 0, 0,21,OCTOBER,2012};
static tTAMPER_LOG log;
static void irtc_callback (IRTC_CALLBACK_TYPE type, void *data);
void main (void)
{
IRTC_InstallCallback (PRI_LVL3,irtc_callback);
IRTC_SetClock (&wtime);
IRTC_SetDST (2,25,MARCH,3,28,OCTOBER);
while(1);
}
static void irtc_callback (IRTC_CALLBACK_TYPE type, void *data)
{
if(type == IRTC_1HZ_CALLBACK ) { memcpy (&rtime,data,sizeof(tTIME)); }
if(type == IRTC_ALM_CALLBACK ) { IRTC_SetClock(&wtime); }
if(type == IRTC_TAMPER1_CALLBACK) { memcpy (&log,data,sizeof(tTAMPER_LOG)); }
}
Toolchain support:
IAR EWARM 7.40.7CodeWarrior 10.6KEIL uVision 5.15CrossWorks 3.6ATOLLIC TrueStudio 5.3.0Kinetis Design Studio 3.0.0