/*---------------------------------------------------------------------
        Copyright (C) 1997, 1998 Nintendo.
        
        File            rtcinit.c
        Coded    by     Shigeo Kimura.  Oct 14, 1997.
        Modified by     Koji Mitsunari. Jan 22, 2001.
        Comments        Real Time Clock Library
   
        $Id: rtcinit.c,v 1.1 2004/02/12 20:16:24 tong Exp $
   ---------------------------------------------------------------------*/
/**************************************************************************
 *
 *  $Revision: 1.1 $
 *  $Date: 2004/02/12 20:16:24 $
 *  $Source: 
 *
 **************************************************************************/
#include <PR/os.h>

#include "rtc.h"
#include "rtcint.h"

OSTimer		__osRtcTimer;
OSMesgQueue	__osRtcTimerQ;
OSMesg		__osRtcTimerMsg;
s32		__osRTCInitialize = 0;

s32
osRTCInit(OSMesgQueue *mq)
{
  s32	ret;
  u8	status;
  u8	temp[8];

  ret = __osRTCGetStatus(mq, &status);
  if (ret == 0) {
    if (__osRTCInitialize == 0) {
      osCreateMesgQueue(&__osRtcTimerQ, &__osRtcTimerMsg, 1);
      __osRTCInitialize = 1;
    }
    if (status & RTC_STATUS_BUSY){
      ret = __osRTCRawRead(mq, RTC_ADDR_FLAG, temp);
      if (ret == 0 || ret == CONT_ERR_RTC_BUSY) {
	temp[1] &= ~6;
	ret = __osRTCRawWrite(mq, RTC_ADDR_FLAG, temp);
      }
    }
    if (status & RTC_STATUS_BDOWN) {
      if (status & RTC_STATUS_DDOWN) {
	ret = CONT_ERR_RTC_BDDOWN;
      } else {
	ret = CONT_ERR_RTC_BDOWN;
      }
    } else if (status & RTC_STATUS_DDOWN) {
      ret = CONT_ERR_RTC_DDOWN;
    }
  }
  return(ret);
}

