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

📄 test.cpp

📁 coldfire5206芯片平台的自捡程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        for (i = 0; i < 7; i ++)
           time[i] = sTime[i];

        // 24HOUR system
        if ( DS1337_24HOUR_SYSTEM != (time[2]&DS1337_24HOUR_SYSTEM) )
        {
           time[2] &= ~DS1337_24HOUR_SYSTEM;
           localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
        }
        else
        // 12HOUR system
        {
           // Convert to 24HOUR system
           if (DS1337_HOUR_PM == (time[2]&DS1337_HOUR_PM))
           {
              time[2] &= 0x1f;
              localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf ) + 12;
           }
           else
              localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
        }
        delta = 1;
        
        break;

     default:
        result = 0;
        break;
   }

   // convert BCD data to HEX data
   if (0 != result)
   {
      localTime.second = ( time[0] >> 4 ) * 10 + ( time[0] & 0xf );
      localTime.minute = ( time[1] >> 4 ) * 10 + ( time[1] & 0xf );
      localTime.date    = ( time[3 + delta] >> 4 ) * 10 + ( time[3 + delta] & 0xf );
      localTime.month  = ( time[4 + delta] >> 4 ) * 10 + ( time[4 + delta] & 0xf );
      localTime.year   = ( time[5 + delta] >> 4 ) * 10 + ( time[5 + delta] & 0xf ) + 2000;

      // Check whether time is valid or not.
      if (    ( 0 <= localTime.second && 59 >= localTime.second )
           && ( 0 <= localTime.minute && 59 >= localTime.minute )
           && ( 0 <= localTime.hour   && 23 >= localTime.hour   )
           && ( 1 <= localTime.date   && 31 >= localTime.date    )
           && ( 1 <= localTime.month  && 12 >= localTime.month  )
           && ( 2000 <= localTime.year && 2099 >= localTime.year ) )
      {
         *theTime = localTime;
      }
      else
         result = 0;
   }

   return result;
}
/*
CHAR  RTCReadTime(Stime *theTime)
{
   UNCHAR time[7];
   CHAR   result;
   SHORT  delta = 0, i;
   Stime  localTime;

   // transfer data
   switch (sMBusInfo.rtcChip)
   {
     case  RTC_X1203:
        result = X1203ReadSeqByte(0x30, 7, sTime);

        // Copy data to a local memory.
        for (i = 0; i < 7; i ++)
           time[i] = sTime[i];

        // 24HOUR system
        if (X1203_24HOUR_SYSTEM == (time[2]&X1203_24HOUR_SYSTEM))
        {
           time[2] &= ~X1203_24HOUR_SYSTEM;
           localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
        }
        else
        // 12HOUR system
        {
           // Convert to 24HOUR system
           if (X1203_HOUR_PM == (time[2]&X1203_HOUR_PM))
           {
              time[2] &= 0x1f;
              localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf ) + 12;
           }
           else
              localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
        }

        break;

     case  RTC_RS5C372A:
        result = RS5C372AReadSeqByte(0, 7, sTime);

        // Copy data to a local memory.
        for (i = 0; i < 7; i ++)
           time[i] = sTime[i];

        localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
        delta = 1;
        break;

     default:
        result = 0;
        break;
   }

   // convert BCD data to HEX data
   if (0 != result)
   {
      localTime.second = ( time[0] >> 4 ) * 10 + ( time[0] & 0xf );
      localTime.minute = ( time[1] >> 4 ) * 10 + ( time[1] & 0xf );
      localTime.date   = ( time[3 + delta] >> 4 ) * 10 + ( time[3 + delta] & 0xf );
      localTime.month  = ( time[4 + delta] >> 4 ) * 10 + ( time[4 + delta] & 0xf );
      localTime.year   = ( time[5 + delta] >> 4 ) * 10 + ( time[5 + delta] & 0xf ) + 2000;

      // Check whether time is valid or not.
      if (    ( 0 <= localTime.second && 59 >= localTime.second )
           && ( 0 <= localTime.minute && 59 >= localTime.minute )
           && ( 0 <= localTime.hour   && 23 >= localTime.hour   )
           && ( 1 <= localTime.date   && 31 >= localTime.date   )
           && ( 1 <= localTime.month  && 12 >= localTime.month  )
           && ( 2000 <= localTime.year && 2099 >= localTime.year ) )
      {
         *theTime = localTime;
      }
      else
         result = 0;
   }

   return result;
}
*/
/**
 *  Real-time clock: set time.
 *
 *  @param  theTime    time to set.
 *
 *  @return  1   if the operation succeed.
 *           0   if the operation failure because packetQue is full.
 *
 *  time items(all value are HEX data)
 *         ITEM         VALUE RANGE
 *        second          0 - 59
 *        minute          0 - 59
 *        hour            0 - 23
 *        day             1 - 31
 *        month           1 - 12
 *        year            2000 - 2099
 *
 *  @note   The function will not check the value range, if value is invalid,
 *          data will also be transfered to RTC, and RTC will not receive it.
 */
CHAR  RTCSetTime(Stime *theTime)
{
   CHAR    result;
   UCHAR  time[7];
   INT16   delta = 0, year, i;

   year = theTime->year - 2000;

   if (   (RTC_RS5C372A == sMBusInfo.rtcChip)
        ||(RTC_DS1337 == sMBusInfo.rtcChip)  )
   {
      delta = 1;

      // day of week. is it valid that always set it to zero?
      time[3] = 0;
   }

   // transfer time data to BCD code
   time[0] = (theTime->second / 10) * 0x10 + (theTime->second % 10);
   time[1] = (theTime->minute / 10) * 0x10 + (theTime->minute % 10);
   time[2] = (theTime->hour   / 10) * 0x10 + (theTime->hour   % 10);

   time[3 + delta] = (theTime->date  / 10) * 0x10 + (theTime->date    % 10);
   time[4 + delta] = (theTime->month  / 10) * 0x10 + (theTime->month  % 10);
   time[5 + delta] = (year / 10) * 0x10 + (year % 10);

   // transfer data
   switch (sMBusInfo.rtcChip)
   {
     case  RTC_X1203:
        time[2] |= X1203_24HOUR_SYSTEM;
        result = X1203WritePage(0x30, 6, time);
        break;

     case  RTC_DS1337:
        time[2] &= ~DS1337_24HOUR_SYSTEM;
        result = DS1337WritePage(DS1337_SET_ADDR, 7, time);
        break;
        
     case  RTC_RS5C372A:
        result = RS5C372AWriteSeqByte(0, 7, time);
        break;

     default:
        result = 0;
        break;
   }

   for (i = 0; i < 7; i ++)
      sTime[i] = time[i];

   return result;
}
/*
CHAR  RTCSetTime(Stime *theTime)
{
   CHAR    result;
   UNCHAR  time[7];
   SHORT   delta = 0, year, i;

   year = theTime->year - 2000;

   if (RTC_RS5C372A == sMBusInfo.rtcChip)
   {
      delta = 1;

      // day of week. is it valid that always set it to zero?
      time[3] = 0;
   }

   // transfer time data to BCD code
   time[0] = (theTime->second / 10) * 0x10 + (theTime->second % 10);
   time[1] = (theTime->minute / 10) * 0x10 + (theTime->minute % 10);
   time[2] = (theTime->hour   / 10) * 0x10 + (theTime->hour   % 10);

   time[3 + delta] = (theTime->date   / 10) * 0x10 + (theTime->date   % 10);
   time[4 + delta] = (theTime->month  / 10) * 0x10 + (theTime->month  % 10);
   time[5 + delta] = (year / 10) * 0x10 + (year % 10);

   // transfer data
   switch (sMBusInfo.rtcChip)
   {
     case  RTC_X1203:
        time[2] |= X1203_24HOUR_SYSTEM;
        result = X1203WritePage(0x30, 6, time);
        break;

     case  RTC_RS5C372A:
        result = RS5C372AWriteSeqByte(0, 7, time);
        break;

     default:
        result = 0;
        break;
   }

   for (i = 0; i < 7; i ++)
      sTime[i] = time[i];

   return result;
}
*/
/**
 *  Real-Time clock initialize: check clock chip type, set time system,
 *  set periodic interrupt cycle.
 *
 *  @note     very important: This function work only in query mode.
 *
 *  @return   0   no RTC chip detected.
 *            1   initialization succeed and clock is OK
 *            2   initialization succeed but clock has been stopped, clock need set
 */
CHAR  RTCInitialize(VOID)
{
   CHAR      result;
   Stime  aTime;

   // Initialize: no chip detected.
   sMBusInfo.rtcChip = RTC_NO;
   result = 0;

   // Check whether the Real-time clock chip is X1203
   result = X1203Initialize( );
   if ( 0 != result)
      sMBusInfo.rtcChip = RTC_X1203;
   else
   {
      // Check whether the Real-time clock chip is RS5C372A
      result = RS5C372AInitialize( );
      if ( 0 != result)
         sMBusInfo.rtcChip = RTC_RS5C372A;
      else
      {
         // Check whether the Real-time clock chip is DS1337
         result = DS1337Initialize( );
         if ( 0 != result)
            sMBusInfo.rtcChip = RTC_DS1337;
      }         
   }

   // Read for one time to initialize data
   if (0 != result)
      RTCReadTime(&aTime);

   return result;
}
/*
CHAR  RTCInitialize(void)
{
   CHAR  result;
   Stime aTime;

   // Initialize: no chip detected.
   sMBusInfo.rtcChip = RTC_NO;
   result = 0;

   // Check whether the Real-time clock chip is X1203
   result = X1203Initialize( );
   if ( 0 != result)
      sMBusInfo.rtcChip = RTC_X1203;
   else
   {
      // Check whether the Real-time clock chip is RS5C372A
      result = RS5C372AInitialize( );
      if ( 0 != result)
         sMBusInfo.rtcChip = RTC_RS5C372A;
   }

   // Read for one time to initialize data
   if (0 != result)
      RTCReadTime(&aTime);

   return result;
}
*/

/**
 *  DS1337: write several bytes(less than 8 bytes).
 *
 *  @param  baseAddr   the first address
 *  @param  byteNum    the number of bytes to be written
 *  @param  data       point to what to write
 *
 *  @return  1   if the operation succeed.
 *           0   if the operation failure because packetQue is full.
 */
static CHAR  DS1337WritePage(UCHAR baseAddr, UCHAR byteNum, UCHAR* data)
{
   MBusPackType  thePack;

   CHAR i;

   if ( 1 > byteNum || 8 < byteNum)
      return 0;

   thePack.txLen1 = DS1337_HEAD_INFO + byteNum;
   thePack.txLen2 = 0;
   thePack.rxLen  = 0;

   thePack.txData[0] = DS1337_SLAVE_ADDR | OP_WRITE;
   thePack.txData[1] = baseAddr;
	
   thePack.repeatTimes = DS1337_WRITE_REPEAT;

   for (i = 0; i < byteNum; i ++)
      thePack.txData[DS1337_HEAD_INFO+i] = *data ++;
/*
   if (OPMODE_INTERRUPT == sMBusInfo.mode)
   {
      if (1 == MBusPackQuePut(&gDS1337WritePackQue, &thePack))
      {
         EnterInterrupt( );
         return 1;
      }
      return 0;
   }
   else
*/
      return MBusTransferByQuery(&thePack);
}


/**
 *  DS1337: read several bytes.
 *
 *  @param  baseAddr   the first byte address
 *  @param  byteNum    the number of bytes to be read
 *  @param  dataAddr   where to store result
 *
 *  @return  1   if the operation succeed.
 *           0   if the operation failure because packetQue is full.
 */
static CHAR  DS1337ReadSeqByte(UCHAR baseAddr, UCHAR byteNum, UCHAR *dataAddr)
{
   MBusPackType  thePack;

   thePack.txLen1 = DS1337_HEAD_INFO;
   thePack.txLen2 = 1;
   thePack.rxLen  = byteNum;
   thePack.rxData = dataAddr;

   thePack.repeatTimes = DS1337_READ_REPEAT;

   thePack.txData[0] = DS1337_SLAVE_ADDR | OP_WRITE;
   thePack.txData[1] = baseAddr;
   thePack.txData[2] = DS1337_SLAVE_ADDR | OP_READ;
/*
   if (OPMODE_INTERRUPT == sMBusInfo.mode)
   {
      if (1 == MBusPackQuePut(&gDS1337ReadPackQue, &thePack))
      {
         EnterInterrupt( );
         return 1;
      }
      return 0;
   }
   else
*/
      return MBusTransferByQuery(&thePack);
}
/**
 *  DS1337 initialize: check clock, set time system, set periodic interrupt
 *  cycle.
 *
 *  @return   0    DS1337 not exist.
 *            1    operation succeed and clock is OK
 *            2    operation succeed but clock has been stopped, clock need set
 *
 *  @note    very important: this function work only in query mode.
 */
static CHAR  DS1337Initialize( void )
{
   UCHAR status;
   
   // Default time: 2000.1.1, 00:00:00
   UCHAR  initTime[7] = {0, 0, 0, 0, 1, 1, 0x00};

   UCHAR  clockNeedSet = 0;
   UCHAR  data, ctrl_byte;

   // clear error counter
   sMBusInfo.transferFail = 0;

   // generate stop signal
   status  = *(UCHAR*)MBCR;
   status &= 0xDF;
   *(UCHAR*)MBCR = status;

	//write Control register ( EOSC )
   ctrl_byte = 0;
   DS1337WritePage( DS1337_CONTROL_REG, 1, &ctrl_byte );

   // read SR register
   if (0 == DS1337ReadSeqByte( DS1337_SR_REG, 1, &data))
      return 0;

   // write SR reg ( OSF )
   ctrl_byte = 0;
   DS1337WritePage( DS1337_SR_REG, 1, &ctrl_byte );

   // DS1337 not acknowledge, it may not exist.
   if (1 <= sMBusInfo.transferFail)
      return 0;

   // Determine whether clock need re-set or not
   if (DS1337_OSF == (DS1337_OSF&data))
      clockNeedSet = 1;

   if (clockNeedSet)
   {
      DS1337WritePage( DS1337_SET_ADDR, 7, initTime );
      return 2;
   }
   else
      return 1;
}

SHORT TestMBusClock()
{
	char buf[20], result, i;
	unsigned int	clock_cnt;

    //Timer2 initialization
	Timer2Cnt = 0;
	asm("   MOVE.L  #$90,D0");         //; Pick up configuration value for ICR10
	asm("   MOVE.B  D0,($60000000+$1d)");  //; Setup Timer 2 interrupt for lev 3, pri 0
	asm("   MOVE.L  #$83D6,D0");         //; Pick up compare value
	asm("   MOVE.W  D0,($60000000+$124)");   //; Load into TRR2, val = (54MHz / 16) / 100
	asm("   MOVE.L  #$631d,D0");          //; Pick up configuration value for TMR2
	asm("   MOVE.W  D0,($60000000+$120)");   // Setup Timer 2

	for ( i = 0; i < 10; i ++ )
	{
		if ( ( result = RTCInitialize() ) == 1 )
			break;
		Delay( 10 );
	}

	if ( result == 1 )
	{
		Delay( 10 );
	   Tsys.year = 2000;
	   Tsys.month = 1;
	   Tsys.date = 1;
	   Tsys.hour = 0;
	   Tsys.minute = 0;
	   Tsys.second = 1;
	   RTCSetTime( &Tsys );

	   asm("   MOVE.W  ($60000000+$36),D0");    // Get current interrupt lockout status
	   asm("   ANDI.L  #$fbff,D0 ");         // Set Timer 2 bit
	   asm("   MOVE.W  D0,($60000000+$36)");    // Enable Timer 2 interrupt
	   asm(

⌨️ 快捷键说明

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