Hi,
I'm trying to set the C5517 RTC module, but it doesn't seem to actually update the RTC registers, despite not throwing any errors. I've tried setting RGKR_LSW and _MSW to 0x95A4 and 0xF1E0 respectively, to no avail.. The program should zero out the RTC at soft reset, but this does not happen either. Reads work fine and time increments 1s / s as expected. Main program:
/*
* RTC Demo
*
* This demo configures the RTC date, time and reads the
* updated date and time for every 1 secs. Date and time
* value read from RTC library shall be displayed on
* serial monitor.
*/
#include "RTC_lib.h"
RTCDate rtcDateRead;
RTCTime rtcTimeRead;
int result;
void setup()
{
int *RCSR, *RGKR_LSW, *RGKR_MSW, *RTCUPDATE;
// RCSR = (int *) 0x1C27;
RGKR_LSW = (int *) 0x196C;
RGKR_MSW = (int *) 0x196D;
// RTCUPDATE = (int *) 0x1901;
// *RCSR = 1;
*RGKR_LSW = 0x95A4;
*RGKR_MSW = 0xF1E0;
RTCDate rtcDate;
RTCTime rtcTime;
RTC.init();
RTC.start();
Serial.begin(9600);
// Print the format of date which will be displayed on serial monitor
Serial.println("Date Format - DD:MM:YY");
Serial.println(__DATE__);
Serial.println("Time Format - HH:MM:SS");
Serial.println(__TIME__);
//Resets and configures RTC time registers, enables RTC interrupts
char timeStr[3] = {0};
memcpy(timeStr, __TIME__, 2);
rtcTime.hours = atoi(timeStr);
memcpy(timeStr, __TIME__+3, 2);
rtcTime.mins = atoi(timeStr);
memcpy(timeStr, __TIME__+6, 2);
rtcTime.secs = atoi(timeStr);
// Set the RTC time
result = RTC.setTime(&rtcTime);
// *RTCUPDATE = 0x8000;
if (0 == result)
{
delay(10);
memcpy(timeStr, __DATE__, 2);
rtcDate.day = atoi(timeStr);
memcpy(timeStr, __DATE__+4, 2); //MONTH IS CHAR
rtcDate.month = 1;//atoi(timeStr);
memcpy(timeStr, __DATE__+9, 2);
rtcDate.year = atoi(timeStr);
// Set the RTC date
result = RTC.setDate(&rtcDate);
if (0 == result)
{
RTC.start(); //RTC starts counting the time
delay(1000); // Give some delay for the time to get updated
RTC.getDate(&rtcDateRead); // Read the date
Serial.print ("\nRTC Started");
Serial.print ("\nCurrent Date - ");
Serial.print (rtcDateRead.day);
Serial.print (":");
Serial.print (rtcDateRead.month);
Serial.print (":");
Serial.println (rtcDateRead.year);
}
}
}
void loop()
{
if (0 == result)
{
RTC.getTime(&rtcTimeRead); //Read RTC time
Serial.print ("\nCurrent Time - ");
Serial.print (rtcTimeRead.hours);
Serial.print (":");
Serial.print (rtcTimeRead.mins);
Serial.print (":");
Serial.print (rtcTimeRead.secs);
delay(1000); // Give some delay for the time to get updated
}
}
RTC_lib.h:
/** @file RTC_lib.h
*
* @brief RTC library header file
*
*/
#ifndef _RTC_H_
#define _RTC_H_
#include "tistdtypes.h"
#include "csl_rtc.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief RTC date structure.
*
* Contains fields of year, month and day for RTC date.
*/
typedef struct RTCDate {
unsigned short year; /**< Year field of RTC date : 0 - 99 */
unsigned short month; /**< Month field of RTC date : 1 - 12 */
unsigned short day; /**< Day field of RTC date : 1 - 31 */
} RTCDate;
/**
* \brief RTC time structure.
*
* Contains field of hours, minutes, seconds and milliseconds for RTC time
*/
typedef struct RTCTime {
unsigned short hours; /**< Hours field of RTC time : 0 - 23 */
unsigned short mins; /**< Minutes field of RTC time : 0 - 59 */
unsigned short secs; /**< Seconds field of RTC time : 0 - 59 */
unsigned short msecs; /**< Milliseconds field of RTC time : 0 - 1023 */
} RTCTime;
/**
* \brief RTC Class
*
* Contains prototypes for functions in RTC library
*/
class RTCClass {
public:
void init(void);
void start(void);
void stop(void);
int setTime(RTCTime *pRtcTime);
int setDate(RTCDate *pRtcDate);
int getTime(RTCTime *pRtcTime);
int getDate(RTCDate *pRtcDate);
int setAlarm(RTCTime *pRtcTime, RTCDate *pRtcDate);
int getAlarm(RTCTime *pRtcTime, RTCDate *pRtcDate);
};
extern RTCClass RTC;
/**< RTC class instance extern which can used by application programs
* to access RTC DSP APIs
*/
#ifdef __cplusplus
}
#endif
#endif //_RTC_H_
RTC_lib.cpp:
/** @file RTC_lib.cpp
*
* @brief RTC implementation
*
*/
#include "RTC_lib.h"
#include "core.h"
#include <stdio.h>
#include <string.h>
/**
* Class identifier declaration
*/
RTCClass RTC;
/** ===========================================================================
* @n@b init()
*
* @b Description
* @n Function initializes RTC Module.
*
* @b Arguments
* @verbatim
@endverbatim
*
* <b> Return Value </b>
* @n None
*
* ===========================================================================
*/
void RTCClass::init(void)
{
CSL_RtcConfig rtcConfig;
RTC_reset();
rtcConfig.rtcyear = 0;
rtcConfig.rtcmonth = 0;
rtcConfig.rtcday = 0;
rtcConfig.rtchour = 0;
rtcConfig.rtcmin = 0;
rtcConfig.rtcsec = 0;
rtcConfig.rtcmSec = 0;
rtcConfig.rtcyeara = 0;
rtcConfig.rtcmontha = 0;
rtcConfig.rtcdaya = 0;
rtcConfig.rtchoura = 0;
rtcConfig.rtcmina = 0;
rtcConfig.rtcseca = 0;
rtcConfig.rtcmSeca = 0;
rtcConfig.rtcintcr = 0x803F;
RTC_config(&rtcConfig);
}
/** ===========================================================================
* @n@b start
*
* @b Description
* @n Starts RTC
*
* ===========================================================================
*/
void RTCClass::start(void)
{
RTC_start();
}
/** ===========================================================================
* @n@b stop
*
* @b Description
* @n Stops RTC
*
* ===========================================================================
*/
void RTCClass::stop(void)
{
RTC_stop();
}
/** ===========================================================================
* @n@b setTime
*
* @b Description
* @n Sets RTC time.
*
* @b Arguments
* @verbatim
* pRtcTime - Pointer to RTC time structure
@endverbatim
*
* <b> Return Value </b> CSL_Status
* @li CSL_SOK - Set time is successful
* @li CSL_ESYS_INVPARAMS - Time Parameters are invalid
*
* ===========================================================================
*/
int RTCClass::setTime(RTCTime *pRtcTime)
{
CSL_Status status;
CSL_RtcTime rtcTime;
status = CSL_ESYS_INVPARAMS;
if(pRtcTime != NULL)
{
rtcTime.mSecs = pRtcTime->msecs;
rtcTime.secs = pRtcTime->secs;
rtcTime.mins = pRtcTime->mins;
rtcTime.hours = pRtcTime->hours;
status = RTC_setTime(&rtcTime);
}
return status;
}
/** ===========================================================================
* @n@b setDate
*
* @b Description
* @n Sets RTC date
*
* @b Arguments
* @verbatim
* pRtcDate - Pointer to RTC date structure
@endverbatim
*
* <b> Return Value </b> CSL_Status
* @li CSL_SOK - Set date is successful
* @li CSL_ESYS_INVPARAMS - Date Parameters are invalid
*
* ===========================================================================
*/
int RTCClass::setDate(RTCDate *pRtcDate)
{
CSL_Status status;
CSL_RtcDate rtcDate;
status = CSL_ESYS_INVPARAMS;
if(pRtcDate != NULL)
{
rtcDate.day = pRtcDate->day;
rtcDate.month = pRtcDate->month;
rtcDate.year = pRtcDate->year;
status = RTC_setDate(&rtcDate);
}
return status;
}
/** ===========================================================================
* @n@b getTime
*
* @b Description
* @n Reads RTC time.
*
* @b Arguments
* @verbatim
* pRtcTime - Pointer to RTC time structure
@endverbatim
*
* <b> Return Value </b> CSL_Status
* @li CSL_SOK - Reading time is successful
* @li CSL_ESYS_INVPARAMS - Time Parameters are invalid
*
* ===========================================================================
*/
int RTCClass::getTime(RTCTime *pRtcTime)
{
CSL_Status status;
CSL_RtcTime rtcTime;
status = CSL_ESYS_INVPARAMS;
if(pRtcTime != NULL)
{
status = RTC_getTime(&rtcTime);
if(status == CSL_SOK)
{
pRtcTime->msecs = rtcTime.mSecs;
pRtcTime->secs = rtcTime.secs;
pRtcTime->mins = rtcTime.mins;
pRtcTime->hours = rtcTime.hours;
}
}
return status;
}
/** ===========================================================================
* @n@b getDate
*
* @b Description
* @n Reads RTC date
*
* @b Arguments
* @verbatim
* pRtcDate - Pointer to RTC date structure
@endverbatim
*
* <b> Return Value </b> CSL_Status
* @li CSL_SOK - Reading date is successful
* @li CSL_ESYS_INVPARAMS - Date Parameters are invalid
*
* ===========================================================================
*/
int RTCClass::getDate(RTCDate *pRtcDate)
{
CSL_Status status;
CSL_RtcDate rtcDate;
status = CSL_ESYS_INVPARAMS;
if(pRtcDate != NULL)
{
status = RTC_getDate(&rtcDate);
if(status == CSL_SOK)
{
pRtcDate->day = rtcDate.day;
pRtcDate->month = rtcDate.month;
pRtcDate->year = rtcDate.year;
}
}
return status;
}
/** ===========================================================================
* @n@b setAlarm
*
* @b Description
* @n Sets RTC alarm.
*
* @b Arguments
* @verbatim
* pRtcTime - Pointer to RTC time structure
* pRtcDate - Pointer to RTC date structure
@endverbatim
*
* <b> Return Value </b> CSL_Status
* @li CSL_SOK
* @li CSL_ESYS_INVPARAMS
*
* ===========================================================================
*/
int RTCClass::setAlarm(RTCTime *pRtcTime, RTCDate *pRtcDate)
{
CSL_Status status;
CSL_RtcAlarm rtcAlarm;
status = CSL_ESYS_INVPARAMS;
if((pRtcTime != NULL) && (pRtcDate != NULL))
{
rtcAlarm.day = pRtcDate->day;
rtcAlarm.month = pRtcDate->month;
rtcAlarm.year = pRtcDate->year;
rtcAlarm.mSecs = pRtcTime->msecs;
rtcAlarm.secs = pRtcTime->secs;
rtcAlarm.mins = pRtcTime->mins;
rtcAlarm.hours = pRtcTime->hours;
status = RTC_setAlarm(&rtcAlarm);
}
return status;
}
/** ===========================================================================
* @n@b getAlarm
*
* @b Description
* @n Reads RTC alarm time.
*
* @b Arguments
* @verbatim
* pRtcTime - Pointer to RTC time structure
* pRtcDate - Pointer to RTC date structure
@endverbatim
*
* <b> Return Value </b> CSL_Status
* @li CSL_SOK
* @li CSL_ESYS_INVPARAMS
*
* ===========================================================================
*/
int RTCClass::getAlarm(RTCTime *pRtcTime, RTCDate *pRtcDate)
{
CSL_Status status;
CSL_RtcAlarm rtcAlarm;
status = CSL_ESYS_INVPARAMS;
if((pRtcTime != NULL) && (pRtcDate != NULL))
{
status = RTC_getAlarm(&rtcAlarm);
pRtcDate->day = rtcAlarm.day;
pRtcDate->month = rtcAlarm.month;
pRtcDate->year = rtcAlarm.year;
pRtcTime->msecs = rtcAlarm.mSecs;
pRtcTime->secs = rtcAlarm.secs;
pRtcTime->mins = rtcAlarm.mins;
pRtcTime->hours = rtcAlarm.hours;
}
return status;
}