00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: truetime_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.1 2000/02/14 18:53:30 lucky 00011 * Initial revision 00012 * 00013 * 00014 */ 00015 00016 /******************************************************************* 00017 * truetime.c * 00018 * * 00019 * Function to get time from the True-Time PC-SG board, * 00020 * using the device driver written by Tod Morcott, Mar 1996. * 00021 * Runs on OS/2 version 3. * 00022 *******************************************************************/ 00023 00024 #define INCL_DOSFILEMGR 00025 #define INCL_DOSPROCESS 00026 #define INCL_DOSDEVICES 00027 #define INCL_DOSPROFILE 00028 #include <os2.h> 00029 #include <bsedos.h> 00030 #include <stdio.h> 00031 #include <stdlib.h> 00032 #include <string.h> 00033 #include <process.h> 00034 #include <stddef.h> 00035 #include <math.h> 00036 #include <truetime.h> 00037 00038 #define TB_CATEGORY 0x80 00039 #define TB_FREEZE 0x41 00040 #define TB_RELEASE 0x42 00041 #define TB_GET_TIME 0X43 00042 #define READ_READY 0x80 00043 00044 static int MaxPassCnt; 00045 static int MaxPass = 0; 00046 static char buffer[32]; 00047 static char *pszFileName; 00048 static ULONG ulActionTaken, ulFileAttribute, ulOpenFlag, ulOpenMode, ulFileSize; 00049 static HFILE hFileHandle; 00050 static ULONG ulCategory; 00051 static ULONG ulFunction; 00052 static VOID *pParmList; 00053 static ULONG ulParmLengthMax; 00054 static ULONG ulParmLengthInOut; 00055 static ULONG ulDataLengthMax; 00056 static ULONG ulDataLengthInOut; 00057 static APIRET rc; 00058 00059 00060 00061 /*************************************************************** 00062 * OpenTrueTime() * 00063 * * 00064 * Open the True-Time PC-SG clock card * 00065 * * 00066 * Argument: * 00067 * MaxPassCount = Number of times the data ready bit is * 00068 * polled before an error is reported. MaxPassCount must * 00069 * be larger for faster processors, and for TrueTime * 00070 * PC-SG boards with different characteristics. A value * 00071 * of 100 was large enough, but 20 was not large enough * 00072 * (7/11/96) * 00073 ***************************************************************/ 00074 00075 int OpenTrueTime( int MaxPassCount ) 00076 { 00077 MaxPassCnt = MaxPassCount; 00078 00079 ulCategory = TB_CATEGORY; 00080 ulFileAttribute = FILE_NORMAL; 00081 ulOpenFlag = OPEN_ACTION_FAIL_IF_NEW | 00082 OPEN_ACTION_OPEN_IF_EXISTS; 00083 ulOpenMode = OPEN_FLAGS_FAIL_ON_ERROR | 00084 OPEN_FLAGS_NO_CACHE | 00085 OPEN_SHARE_DENYNONE | 00086 OPEN_ACCESS_READWRITE; 00087 pszFileName = "TimeBas$"; 00088 00089 rc = DosOpen( pszFileName, 00090 &hFileHandle, 00091 &ulActionTaken, 00092 ulFileSize, 00093 ulFileAttribute, 00094 ulOpenFlag, 00095 ulOpenMode, 00096 NULL ); 00097 if ( rc != 0 ) 00098 { 00099 printf( "Could not open %s\n", pszFileName ); 00100 return -1; 00101 } 00102 return 0; 00103 } 00104 00105 00106 int CloseTrueTime( void ) 00107 { 00108 DosClose( hFileHandle ); 00109 return MaxPass; 00110 } 00111 00112 00113 BOOL FreezeTime( void ) 00114 { 00115 ulFunction = TB_FREEZE; 00116 pParmList = NULL; 00117 ulParmLengthMax = 0; 00118 ulParmLengthInOut = 0; 00119 ulDataLengthMax = sizeof(int); 00120 ulDataLengthInOut = sizeof(int); 00121 00122 rc = DosDevIOCtl( hFileHandle, 00123 ulCategory, 00124 ulFunction, 00125 pParmList, 00126 ulParmLengthMax, 00127 &ulParmLengthInOut, 00128 buffer, 00129 ulDataLengthMax, 00130 &ulDataLengthInOut ); 00131 if ( rc != 0 ) 00132 { 00133 printf( "%s DosDevIOCtl = %d\n", __FUNCTION__, rc ); 00134 DosClose( hFileHandle ); 00135 return FALSE; 00136 } 00137 return TRUE; 00138 } 00139 00140 00141 00142 BOOL ReleaseTime( void ) 00143 { 00144 ulFunction = TB_RELEASE; 00145 00146 rc = DosDevIOCtl( hFileHandle, 00147 ulCategory, 00148 ulFunction, 00149 pParmList, 00150 ulParmLengthMax, 00151 &ulParmLengthInOut, 00152 buffer, 00153 ulDataLengthMax, 00154 &ulDataLengthInOut ); 00155 if ( rc != 0 ) 00156 { 00157 printf( "%s DosDevIOCtl = %d\n", __FUNCTION__, rc ); 00158 DosClose( hFileHandle ); 00159 return FALSE; 00160 } 00161 return TRUE; 00162 } 00163 00164 00165 00166 int GetTime( TrueTimeStruct *pTrueTime ) 00167 { 00168 int PassCount = 0; 00169 UCHAR data_ready; 00170 00171 ulFunction = TB_GET_TIME; 00172 pParmList = NULL; 00173 ulParmLengthMax = 0; 00174 ulParmLengthInOut = 0; 00175 ulDataLengthMax = sizeof( TrueTimeStruct ); 00176 ulDataLengthInOut = sizeof( TrueTimeStruct ); 00177 00178 pTrueTime->UnitMillisecs = (UCHAR)0xff; 00179 pTrueTime->TensMillisecs = (UCHAR)0xff; 00180 pTrueTime->HundredsMillisecs = (UCHAR)0xff; 00181 pTrueTime->UnitSeconds = (UCHAR)0xff; 00182 pTrueTime->TensSeconds = (UCHAR)0xff; 00183 pTrueTime->UnitMinutes = (UCHAR)0xff; 00184 pTrueTime->TensMinutes = (UCHAR)0xff; 00185 pTrueTime->UnitHours = (UCHAR)0xff; 00186 pTrueTime->TensHours = (UCHAR)0xff; 00187 pTrueTime->UnitDays = (UCHAR)0xff; 00188 pTrueTime->TensDays = (UCHAR)0xff; 00189 pTrueTime->HundredsDays = (UCHAR)0xff; 00190 pTrueTime->Status = (UCHAR)0xff; 00191 00192 /* Make several attempts to get the time from the True-Time clock 00193 **************************************************************/ 00194 do 00195 { 00196 rc = DosDevIOCtl( hFileHandle, 00197 ulCategory, 00198 ulFunction, 00199 pParmList, 00200 ulParmLengthMax, 00201 &ulParmLengthInOut, 00202 pTrueTime, 00203 ulDataLengthMax, 00204 &ulDataLengthInOut ); 00205 if ( rc != 0 ) 00206 { 00207 printf( "%s DosDevIOCtl = %d\n", __FUNCTION__, rc ); 00208 return -1; 00209 } 00210 00211 if ( ++PassCount == MaxPassCnt ) /* Error. Too many passes. */ 00212 return -2; 00213 00214 data_ready = pTrueTime->DataReady & READ_READY; 00215 00216 } while ( !data_ready ); 00217 00218 MaxPass = (PassCount > MaxPass) ? PassCount : MaxPass; 00219 return 0; 00220 } 00221 00222 00223 int GetTrueTime( TrueTimeStruct *pTrueTime ) 00224 { 00225 int rc; 00226 00227 FreezeTime(); 00228 rc = GetTime( pTrueTime ); 00229 ReleaseTime(); 00230 return rc; 00231 }