⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtc_functions.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************************/
/* rtc_functions.c :            contains low level function for the rtc             */
/*                                                                                  */
/*                                                                                  */
/*   Author: Laurent Sollier                                                        */
/*                                                                                  */
/*   version: 1.0                                                                   */
/*                                                                                  */
/*   Date: 03/20/01                                                                 */
/*   (C) Copyright 2001 by Texas Instruments Incorporated, All Rights Reserved      */   
/************************************************************************************/

#ifndef _WINDOWS
   #include "chipset.cfg"
#endif

#include <string.h> /* needed for memset */
#include "nucleus.h"

#include "rtc_messages_i.h"
#include "rvm_use_id_list.h"
#include "rtc_api.h"
#include "rtc_i.h"
#include "rtc_config.h"

#include "mem.h"
#include "iq.h"
#include "ulpd.h"


/* ----- Macro ----- */
/*-------------------------------------------------------------*/
/* RTC_STOP                                                    */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Stop the RTC (STOP_RTC is cleared)          */
/*-------------------------------------------------------------*/
#define RTC_STOP  (* (volatile UINT8 *) RTC_CTRL_REG &= ~RTC_START_RTC)

/*-------------------------------------------------------------*/
/* RTC_START                                                   */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Start the RTC                               */
/*-------------------------------------------------------------*/
#define RTC_START (* (volatile UINT8 *) RTC_CTRL_REG |= RTC_START_RTC) 

/*-------------------------------------------------------------*/
/* RTC_SET_32_CNT                                              */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Set the SET_32_COUNTER bit                  */
/*-------------------------------------------------------------*/
#define RTC_SET_32_CNT (* (volatile UINT8 *) RTC_CTRL_REG |= RTC_SET_32_COUNTER)

/*-------------------------------------------------------------*/
/* RTC_RESET_32_CNT                                            */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Reset the SET_32_COUNTER bit                */
/*-------------------------------------------------------------*/
#define RTC_RESET_32_CNT  (* (volatile UINT8 *) RTC_CTRL_REG &= ~RTC_SET_32_COUNTER)

/*-------------------------------------------------------------*/
/* RTC_SET_TEST_MODE                                           */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Set the TEST_MODE bit                       */
/*-------------------------------------------------------------*/
#define RTC_SET_TEST_MODE (* (volatile UINT8 *) RTC_CTRL_REG |= RTC_TEST_MODE)

/*-------------------------------------------------------------*/
/* RTC_ENABLE_IT_ALARM                                         */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Enable the IT_ALARM                         */
/*-------------------------------------------------------------*/
#define RTC_ENABLE_IT_ALARM (* (volatile UINT8 *) RTC_INTERRUPTS_REG |= RTC_IT_ALARM)

/*-------------------------------------------------------------*/
/* RTC_DISABLE_IT_ALARM                                        */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Disable the IT_ALARM                        */
/*-------------------------------------------------------------*/
#define RTC_DISABLE_IT_ALARM (* (volatile UINT8 *) RTC_INTERRUPTS_REG &= ~RTC_IT_ALARM)

/*-------------------------------------------------------------*/
/* RTC_ENABLE_IT_TIMER                                         */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Enable the IT_TIMER                         */
/*-------------------------------------------------------------*/
#define RTC_ENABLE_IT_TIMER (* (volatile UINT8 *) RTC_INTERRUPTS_REG |= RTC_IT_TIMER)

/*-------------------------------------------------------------*/
/* RTC_DISABLE_IT_TIMER                                        */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Disable the IT_TIMER                        */
/*-------------------------------------------------------------*/
#define RTC_DISABLE_IT_TIMER (* (volatile UINT8 *) RTC_INTERRUPTS_REG &= ~RTC_IT_TIMER)

/*-------------------------------------------------------------*/
/* RTC_SET_EVENT_TIMER                                         */
/*-------------------------------------------------------------*/
/* Parameters :   event to enable or disable                   */
/* Return :   none                                             */
/* Functionality : Set the event for the IT_TIMER              */
/*         Enable or disable IT                                */
/*-------------------------------------------------------------*/
#define RTC_SET_EVENT_TIMER(x) (* (volatile UINT8 *) RTC_INTERRUPTS_REG = (* (volatile UINT8 *) RTC_INTERRUPTS_REG & 0xfffc) | (x))

/*-------------------------------------------------------------*/
/* RTC_TEST_BUSY                                               */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Return the RTC state busy signal            */
/*-------------------------------------------------------------*/
#define RTC_TEST_BUSY ((* (volatile UINT8 *) RTC_STATUS_REG) & RTC_BUSY) 

/*-------------------------------------------------------------*/
/* RTC_RUN_STATE                                               */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Return the RTC run state                    */
/*-------------------------------------------------------------*/
#define RTC_RUN_STATE ((* (volatile UINT8 *) RTC_STATUS_REG) & RTC_RUN) 

/*-------------------------------------------------------------*/
/* RTC_FREE_AL_ITLINE                                          */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : free it AL line                             */
/*-------------------------------------------------------------*/

#define RTC_FREE_AL_ITLINE ((* (volatile UINT8 *) RTC_STATUS_REG) |= RTC_ALARM) 

/*-------------------------------------------------------------*/
/* RTC_FREE_POWER_UP                                           */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : clear reset event                           */
/*-------------------------------------------------------------*/

#define RTC_FREE_POWER_UP ((* (volatile UINT8 *) RTC_STATUS_REG) |= RTC_POWER_UP) 


/*-------------------------------------------------------------*/
/* RTC_GET_EVENT_TIMER                                         */
/*-------------------------------------------------------------*/
/* Parameters :   none                                         */
/* Return :   none                                             */
/* Functionality : Get the event status for the IT_TIMER       */
/*-------------------------------------------------------------*/
#define RTC_GET_EVENT_TIMER ((* (volatile UINT8 *) RTC_STATUS_REG & 0x3c))




/* ----- Global variables ----- */
static T_RTC_DATE_TIME rtc_date_time_alarm = {0};

static T_RV_RETURN rtc_return_path = {RVF_INVALID_ADDR_ID, NULL};
static T_RTC_ALARM* rtc_msg_alarm_event = NULL;

extern T_RTC_ENV_CTRL_BLK* rtc_env_ctrl_blk;

/* Number of 32 Khz clock */
static UINT32 rtc_nb_32khz = 0;

/* Number of high frequency clock */
static UINT32 rtc_nb_hf = 0;

static NU_HISR hisr;
char hisrStack[512];

// UINT16 toto = 0; tmp

void HisrEntryFunc(void)
{
   if (rtc_msg_alarm_event)
      rvf_send_msg(rtc_env_ctrl_blk->addr_id, rtc_msg_alarm_event);
   //char comp[15];
/*   sprintf(comp,"%d",toto);
   LCDG_WriteString( 4, 0, "              ");
   LCDG_WriteString( 3, 0, (char*) comp); //tmp */
}


/*---------------------------------------------------------------*/
/* conv_bin   DCB => B                                           */
/*---------------------------------------------------------------*/
/* Parameters : value to be convert                              */
/* Return :   none                                               */
/* Functionality : BCD et binary conversion function             */
/*---------------------------------------------------------------*/

static UINT8 conv_bin(UINT8 value)
{
   return (10*(value>>4) + ( value & 0x0f));
}

/*---------------------------------------------------------------*/
/* conv_bcd    B => DCB                                          */
/*---------------------------------------------------------------*/
/* Parameters : value to be convert                              */
/* Return :   none                                               */
/* Functionality : Binary to BCD conversion function             */
/*---------------------------------------------------------------*/

static UINT8 conv_bcd(UINT8 value)
{
   return ((value%10) | ( (value/10) <<4));
}



/*---------------------------------------------------------------*/
/* format_date_available                                         */
/*---------------------------------------------------------------*/
/* Parameters : T_RTC_DATE_TIME structure                        */
/* Return :     TRUE if format is available                      */
/*              FALSE else                                       */
/* Functionality : test if date and time format is available     */
/*---------------------------------------------------------------*/

BOOL format_date_available(T_RTC_DATE_TIME date_time)
{
   UINT8 m;

   if (date_time.second < 0 || date_time.second > 59)
      return FALSE;
   if (date_time.minute < 0 || date_time.minute > 59)
      return FALSE;
   if (date_time.mode_12_hour == FALSE)
   {
      if (date_time.hour < 0 || date_time.hour > 23)
         return FALSE;
   }
   else
      if (date_time.hour < 1 || date_time.hour > 12)
         return FALSE;

   if (date_time.month < 1 || date_time.month > 12)
      return FALSE;
   if (date_time.year < 0 || date_time.year > 99)
      return FALSE;
   if (date_time.wday < 0 || date_time.wday > 6)
      return FALSE;
   m = date_time.month;
   if (m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12)
   {
      if (date_time.day < 1 || date_time.day > 31)
         return FALSE;
   }
   else
   {
      if (m == 4||m == 6||m == 9||m == 11)
      {
         if (date_time.day < 1 || date_time.day > 30)
            return FALSE;
      }
      else
      {
         if (date_time.year%4)
         {
            if (date_time.day < 1 || date_time.day > 28)
               return FALSE;
         }
         else
         {
            if (date_time.day < 1 || date_time.day > 29)
               return FALSE;
         }
      }
   }
   return TRUE;
}



/*******************************************************************************
 *
 *                               RTC_Initialize
 *
 ******************************************************************************/



T_RVF_RET RTC_Initialize(void)
{
   T_RVF_MB_STATUS   mb_status;

   /* Reserve memory for alarm event */
   mb_status = rvf_get_buf (rtc_env_ctrl_blk->prim_id, sizeof (T_RTC_ALARM ), (void **) &rtc_msg_alarm_event);   

   if ((mb_status == RVF_GREEN) || (mb_status == RVF_YELLOW)) /* Memory allocation success */
   {      
      rtc_msg_alarm_event->os_hdr.msg_id = RTC_ALARM_EVT;
   }
   else
   {
      rtc_msg_alarm_event = NULL;
      return RVF_MEMORY_ERR;
   }

   rtc_env_ctrl_blk->msg_alarm_event = rtc_msg_alarm_event;


   /* Start RTC module */
   if (RTC_RUN_STATE == 0)
      RTC_START;

   /* Enable auto compensation */
    //*(volatile UINT8*) RTC_CTRL_REG |= RTC_AUTO_COMP;
   /* Disable auto compensation */
    *(volatile UINT8*) RTC_CTRL_REG &= ~RTC_AUTO_COMP;

    /* For CHIPSET = 7, 9, 10 or 11, set analog baseband type */
#if (((CHIPSET == 7) || (CHIPSET == 8) || (CHIPSET == 10) || (CHIPSET == 11)) && (ANALOG == 1))
   *(volatile UINT8*) RTC_CTRL_REG |= RTC_nDELTA_OMEGA;
#endif

   /* Activate periodic interrupt every minute*/
   /* Disable all IT before accessing register */
   rvf_disable(25);

   /* Enable IRQ9 and IRQ10 */
   IQ_Unmask(IQ_RTC_TIMER);
   IQ_Unmask(IQ_RTC_ALARM);

   while (RTC_TEST_BUSY);
   RTC_SET_EVENT_TIMER(RTC_EVERY_MIN); /* Set timer every minute */
   RTC_ENABLE_IT_TIMER;
   RTC_DISABLE_IT_ALARM;

   rvf_enable();

   /* The stack is entirely filled with the pattern 0xFE. */
   memset (hisrStack, 0xFE, sizeof(hisrStack));

   /* Temporary modification: create HISR to diplay compensation value */
   NU_Create_HISR(&hisr, "RTC_HISR", HisrEntryFunc, 2, hisrStack, sizeof(hisrStack)); // lowest prty
   /* end temporary modification */

   return RVF_OK;
}
/*******************************************************************************
 *
 *                               RTC_RtcReset
 *
 ******************************************************************************/

BOOL RTC_RtcReset(void)
{

   /* Read POWER UP bit to inform MMI of a possible RTC reset */
   if ( ((* (volatile UINT8*) RTC_STATUS_REG) & RTC_POWER_UP) )
   {
      RTC_FREE_POWER_UP;
      return TRUE;
   }
   else
      return FALSE;

}


/*******************************************************************************
 *
 *                               RTC_GetDateTime
 *
 ******************************************************************************/

T_RVF_RET RTC_GetDateTime(T_RTC_DATE_TIME* date_time)
{
   UINT8 sec;
   UINT8 min;
   UINT8 hour;
   UINT8 day;
   UINT8 month;
   UINT8 year;
   UINT8 wday;
   UINT8 hr_reg;

   /* Disable all IT before reading register */
   rvf_disable(25);

   if (RTC_TEST_BUSY)
   {   
      rvf_enable();
      return RVF_NOT_READY;
   }
   else
   {
      day  = * (volatile UINT8 *) RTC_DAYS_REG;
      month  = * (volatile UINT8 *) RTC_MONTHS_REG;
      year = * (volatile UINT8 *) RTC_YEARS_REG;
      wday = * (volatile UINT8 *) RTC_WEEK_REG;   
      sec  = * (volatile UINT8 *) RTC_SECONDS_REG;
      min  = * (volatile UINT8 *) RTC_MINUTES_REG;
      hr_reg = * (volatile UINT8 *) RTC_HOURS_REG;
      rvf_enable();
   }

   hour = (0x7f & hr_reg);

   date_time->second = conv_bin(sec);
   date_time->minute = conv_bin(min);
   date_time->hour = conv_bin(hour);

   if ( (* (volatile UINT8 *)RTC_CTRL_REG & RTC_MODE_12_24 ) == 0)
   {
      /* 24 hour mode */
      date_time->mode_12_hour = FALSE;
      date_time->PM_flag = FALSE;
   }
   else
   {
      /* 12 hour mode */
      date_time->mode_12_hour = TRUE;
      if ((hr_reg & 0x80) == 0)
         date_time->PM_flag = FALSE;
      else
         date_time->PM_flag = TRUE;
   }

   date_time->day = conv_bin(day);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -