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

📄 clk.c

📁 一个关于s1d13806的应用程序
💻 C
字号:
/*
**===========================================================================
** CLK.C
**
** Program clock generator.
**
**---------------------------------------------------------------------------
** Copyright (c) 1998, 2001 Epson Research and Development, Inc.
** All Rights Reserved.
**===========================================================================
*/
#if defined(INTEL_W32) || defined(INTEL_DOS)

#ifndef __GNUC__
#pragma warning(disable:4032)  // disable warning: formal parameter 1 has different type when promoted
#include <conio.h>
#endif
#endif

#if !defined(__GNUC__)

#include <conio.h>
#endif

#if defined(__GNUC__) && defined(INTEL_DOS)
#include <conio.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "hal.h"
#include "appcfg.h"
#include "clock.h"

/*-------------------------------------------------------------------------*/

char Revision[] = "CLK.C=$Revision: 3 $";

static const char szVersion[] = "1.01";

/*-------------------------------------------------------------------------*/

#define SKIP_WHITESPACE( a )  while(isspace((int) *a))a++

/*-------------------------------------------------------------------------*/

extern void DisplayCopyright(void);
extern void DisplayFrequencies(void);
extern void DisplayUsage(void);

/*-------------------------------------------------------------------------*/

//
// _RegLinearAddress is an internal variable in the HAL which represents
// the address of the first register (REG[0])
//
extern volatile DWORD _RegLinearAddress;

/*-------------------------------------------------------------------------*/

/*
** ArgLen()
**
** Returns:
** The length of the argument pointed to by szArg but
** does not include the final delimiter in the length.
*/
int ArgLen( char *szArg )
{
   int nLen = 0;

   while (!isspace( (int) *szArg ) && ('\0' != *szArg))
   {
      szArg++;
      nLen++;
   }
   return nLen;
}

/*-----------------------------------------------------------------------*/

/*
** AscToUpper()
**
** This function converts an ASCII string to upper case.
*/
void AscToUpper(char *szAscii)
{
   while (*szAscii != 0)
      {
      if ((*szAscii <= 'z') && (*szAscii >= 'a'))
         *szAscii -= 'a' - 'A';

      ++szAscii;
      }
}

/*-----------------------------------------------------------------------*/

/*
** htol() - Hexadecimal ASCII to Long integer.
*/
long htol(char *szAscii )
{
   long lTmp;
   char ch;

   lTmp = 0;
   SKIP_WHITESPACE( szAscii );

   /*
   ** Check that we have no more than 32 'bits' of argument.
   */
   if (ArgLen( szAscii ) > 32/4)
      return 0;

   while (!isspace( (int) *szAscii ) && ('\0' != *szAscii))
   {
      ch = (char)toupper( *szAscii );

      if (!isxdigit( (int) ch ))
         return 0;

      if ((ch >= 'A') && (ch <= 'F'))
         lTmp = lTmp * 16 + 10 + (ch - 'A');
      else
         lTmp = lTmp * 16 + (ch - '0');

      szAscii++;
   }
   return lTmp;
}

/*-------------------------------------------------------------------------*/

int main( int argc, char *argv[] )
{
   int ClkiFreqIndex = -1;
   int Clki2FreqIndex = -1;
   int check;
   DWORD dwFreq;
   BYTE *pRegisterBaseAddress = (void *) 0;


   static const char strUnknownCmd[] = "\nERROR: Unknown command line argument.\n";

   /*
   ** Parse the command line.
   */
   for (check = 1; check < argc; ++check)
   {
      AscToUpper(argv[check]);

      if (('/' == argv[check][0]) || ('-' == argv[check][0]))
      {
         switch (argv[check][1])
         {
            case '?':
               DisplayCopyright();
               DisplayUsage();
               return 0;
               break;

            default:
               DisplayUsage();
               printf( strUnknownCmd );
               return 1;
         }
      }
      else if (!strncmp(argv[check], "CLKI=?", 6) ||
               !strncmp(argv[check], "CLKI2=?", 7))
         {
         DisplayFrequencies();
         return 0;
         }
      else if (!strncmp(argv[check], "CLKI=", 5))
         {
         ClkiFreqIndex = atol(&argv[check][5]);

         if ((ClkiFreqIndex < 0) || (ClkiFreqIndex >= MAX_FREQ_INDEX))
            {
            DisplayUsage();
            printf("\n");
            printf("ERROR: Invalid frequency index for CLKI=n\n");
            printf("       For a list of frequencies, type CLKI=?\n");
            return 1;
            }
         }
      else if (!strncmp(argv[check], "CLKI2=", 6))
         {
         Clki2FreqIndex = atol(&argv[check][6]);

         if ((Clki2FreqIndex < 0) || (Clki2FreqIndex >= MAX_FREQ_INDEX))
            {
            DisplayUsage();
            printf("\n");
            printf("ERROR: Invalid frequency index for CLKI2=n\n");
            printf("       For a list of frequencies, type CLKI2=?\n");
            return 1;
            }
         }
      else
      {
         DisplayCopyright();
         DisplayUsage();
         printf(strUnknownCmd);
         exit(1);
      }
   }


   if ((ClkiFreqIndex == -1) &&
       (Clki2FreqIndex == -1))
      {
      DisplayCopyright();
      DisplayUsage();
      printf("\n");
      printf("ERROR: Must select one of the command line options\n");
      printf("       CLKI=n or CLKI2=n\n");
      exit(1);
      }


   /*
   ** Attempt to register with the HAL.
   */
   switch (seRegisterDevice(&HalInfo))
   {
      case ERR_OK:
         break;
      
#ifdef INTEL_W32
      case ERR_PCI_DRIVER_NOT_FOUND:
         printf("ERROR: PCI driver not found.\n");
         return 1;
      
      case ERR_PCI_BRIDGE_ADAPTER_NOT_FOUND:
         printf("ERROR: PCI bridge adapter not found.\n");
         return 1;
#endif

      case ERR_UNKNOWN_DEVICE:
         printf("ERROR: Could not detect S1D13806.\n");
         return 1;
      
      default:
         printf("ERROR: Could not map memory from evaluation board to host platform.\n");
         return 1;
   }

   //
   // Get register address
   //
   pRegisterBaseAddress = (BYTE *) _RegLinearAddress;


   if (ClkiFreqIndex != -1)
      {
      dwFreq = ClockBits[ClkiFreqIndex].dwFreq;

      if (SetClock(CLKI, ClkiFreqIndex, pRegisterBaseAddress) != OK)
         {
         printf("\nERROR: Could not program CLKI with %ld.%03ld MHz\n",
                dwFreq / 1000L,
                dwFreq - ((dwFreq / 1000L) * 1000L));
         }
      else
         {
         printf("\nProgrammed CLKI=%ld.%03ld MHz\n",
                dwFreq / 1000L,
                dwFreq - ((dwFreq / 1000L) * 1000L));
         }
      }


   if (Clki2FreqIndex != -1)
      {
      dwFreq = ClockBits[Clki2FreqIndex].dwFreq;

      if (SetClock(CLKI2, Clki2FreqIndex, pRegisterBaseAddress) != OK)
         {
         printf("\nERROR: Could not program CLKI2 with %ld.%03ld MHz\n",
                dwFreq / 1000L,
                dwFreq - ((dwFreq / 1000L) * 1000L));
         }
      else
         {
         printf("\nProgrammed CLKI2=%ld.%03ld MHz\n",
                dwFreq / 1000L,
                dwFreq - ((dwFreq / 1000L) * 1000L));
         }
      }

   return 0;
}

/*-------------------------------------------------------------------------*/

void DisplayCopyright(void)
{
   const char *szHalVer;
   const char *szHalStatus;
   const char *szHalStatusRev;

   seGetHalVersion( &szHalVer, &szHalStatus, &szHalStatusRev );

   printf("\n13806CLK.EXE - Program clock generator - Version %s  [HAL %s%s%s]\n", szVersion, szHalVer, szHalStatus, szHalStatusRev);
   printf("Copyright (c) 1999, 2001 Epson Research and Development, Inc.\n");
   printf("All Rights Reserved.\n\n");
}

/*-------------------------------------------------------------------------*/

void DisplayFrequencies(void)
{
   DWORD bits;
   char *szFreq;
   int i;
   int DisplayTitle = TRUE;

   for (i = 0; i < MAX_FREQ_INDEX; ++i)
      {
      if (DisplayTitle)
         {
         printf("\nIndex  Frequency"
                "\n-----------------");

         DisplayTitle = FALSE;
         }

      GetClockChipBits(i, CLK_VREG0, &bits, &szFreq);
      printf("\n %02u   %7s MHz", i, szFreq);

      if ((i > 0) && ((i % 15) == 0))
         {
         printf("\n\nPress any key to continue...\n");
         getch();
         DisplayTitle = TRUE;
         }
      }

   printf("\n");
}

/*-------------------------------------------------------------------------*/

void DisplayUsage(void)
{
   printf("Usage: 13806CLK [clki=n | clki=?] [clki2=n | clki2=?] [/?]\n");
   printf("   clki=n   select CLKI frequency based on frequency index n\n");
   printf("   clki=?   shows the available frequencies for index n\n");
   printf("   clki2=n  select CLKI2 frequency based on frequency index n\n");
   printf("   clki2=?  shows the available frequencies for index n\n");
   printf("   /? shows this usage message\n");
}

/*-------------------------------------------------------------------------*/

⌨️ 快捷键说明

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