demo_cpu.c
Go to the documentation of this file.
00001 /****************************************************************************
00002 * Copyright (c) Advantech Co., Ltd. All Rights Reserved
00003 *
00004 * File Name:
00005 *       demo_cpu.c
00006 *
00007 * Programmers:
00008 *       Neo Lo
00009 *
00010 ****************************************************************************/
00011 
00012 #include <stdio.h>
00013 #include <unistd.h>
00014 #include <stdlib.h>
00015 
00016 #include "REL_Linux_SUSI.H"
00017 
00018 
00019 // Return 0 if platform infomation is correctly obtained.
00020 // Otherwise, return 1.
00021 int show_platform_info(void)
00022 {
00023         int result;
00024         DWORD major, minor, year, month, date;
00025         const int BUF_LENGTH = 128;
00026         TCHAR buf[BUF_LENGTH];  // Buffer length includes the null character.
00027 
00028         SusiDllGetVersion(&major, &minor);
00029     year    = minor/10000;
00030     month   = minor%10000/100;
00031     date    = minor%100;
00032         printf("Version: %li (20%02li/%02li/%02li)\n", major, year, month, date);
00033 
00034         // Get platform name.
00035         result = SusiGetPlatformName(buf, BUF_LENGTH);
00036         if (result < 0) {
00037                 printf("SusiGetPlatformName() failed\n");
00038                 return 1;
00039         }
00040         else if (result > 0) {
00041                 printf("SusiGetPlatformName(): buffer is too short\n");
00042                 return 1;
00043         }
00044         else
00045                 printf("Platform name: %s\n", buf);
00046 
00047         // Get BIOS version.
00048         result = SusiGetBIOSVersion(buf, BUF_LENGTH);
00049         if (result < 0) {
00050                 return 1;
00051         }
00052         else if (result > 0) {
00053                 printf("SusiGetBIOSVersion(): buffer is too short\n");
00054                 return 1;
00055         }
00056         else
00057                 printf("BIOS version: %s\n", buf);
00058 
00059         return 0;
00060 }
00061 
00062 static int check_file(const char *file_name)
00063 {
00064     /* check file exist */
00065         if ((access(file_name, F_OK|R_OK)) != 0) 
00066     {
00067             return -1;
00068         }
00069 
00070     return 0;
00071 }
00072 
00073 static int show_all_cpu_speed(void)
00074 {
00075     char buff[64];
00076     FILE *fd;
00077     int i;
00078 
00079     printf("Current CPU Speed:\n");
00080 
00081     //MAX support 4 CPU
00082     for (i = 0; i < 4; i++)
00083     {
00084         sprintf(buff, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
00085         if (check_file(buff) != 0)
00086                 break;
00087         if ((fd = fopen(buff, "r")) == NULL)
00088             break;
00089 
00090         if (fgets(buff, 32, fd) != NULL)
00091             printf("  CPU%d Hz: %s", i, buff);
00092         
00093         fclose(fd);
00094     }
00095 
00096     if (i == 0)
00097         printf("Your system does not support CPU freq in sysfs.\nYou can use command [cat /proc/cpuinfo] to check it.\n");
00098 
00099         return 0;
00100 }
00101 
00102 int get_mode(void)
00103 {
00104     BYTE CpuMode;
00105 
00106     if ((SusiPlusSpeedRead(&CpuMode, NULL) != 0) ||
00107         (CpuMode > 2))
00108     {
00109         printf("SusiPlusSpeedRead() failed\n");
00110         return -1;
00111     }
00112 
00113     printf("CPU SpeedStep Mode is (%d)", CpuMode);
00114 
00115     switch(CpuMode)
00116     {
00117         case CORE_CPU_FULL_SPEED:
00118             printf("CORE_CPU_FULL_SPEED\n");
00119             break;
00120         case CORE_CPU_LOW_SPEED:
00121             printf("CORE_CPU_LOW_SPEED\n");
00122             break;
00123         case CORE_CPU_DYNAMIC:
00124             printf("CORE_CPU_DYNAMIC\n");
00125             break;
00126         default:
00127             break;
00128     }
00129 
00130     return 0;
00131 }
00132 
00133 int set_mode(void)
00134 {
00135     int CpuMode;
00136 
00137 retry:
00138     printf("\nSet CPU SpeedStep Mode:\n");
00139         printf("0) CORE_CPU_FULL_SPEED\n");
00140         printf("1) CORE_CPU_LOW_SPEED\n");
00141         printf("2) CORE_CPU_DYNAMIC\n");
00142     printf("Enter your choice: ");
00143 
00144         if (scanf("%i", &CpuMode) <= 0)
00145                 return 1;
00146         
00147     if (CpuMode > 2)
00148     {
00149         printf("CpuMode must be 0, 1, 2.\n");
00150         goto retry;
00151     }
00152 
00153     if (SusiPlusSpeedWrite(CpuMode, 0) != 0)
00154     {
00155         printf("SusiPlusSpeedWrite() failed\n");
00156         return -1;
00157     }
00158 
00159     return 0;
00160 }
00161 
00162 void show_menu(void)
00163 {
00164         printf("\n");
00165     show_all_cpu_speed();
00166         printf("0) Terminate this program\n");
00167         printf("1) Get CPU SpeedStep Mode\n");
00168         printf("2) Set CPU SpeedStep Mode\n");
00169         printf("Enter your choice: ");
00170 }
00171 
00172 int main(void)
00173 {
00174         int result;
00175         int done, op;
00176 
00177         result = SusiDllInit();
00178         if (result == FALSE) {
00179                 printf("SusiDllInit() failed\n");
00180                 return 1;
00181         }
00182 
00183         result = SusiCoreAvailable();
00184         if (result == 0) {
00185                 printf("SusiCoreAvailable() failed\n");
00186                 SusiDllUnInit();
00187                 return 1;
00188         }
00189 
00190         show_platform_info();
00191 
00192         result = SusiPlusSpeedSetActive();
00193         if (result != 0) {
00194                 printf("SusiPlusSpeedSetActive() failed\n");
00195         printf("CPU or OS does not support speedstep!!\n");
00196                 SusiDllUnInit();
00197                 return 1;
00198         }
00199 
00200         done = 0;
00201         while (! done) {
00202                 show_menu();
00203                 if (scanf("%i", &op) <= 0)
00204                         op = -1;
00205                 
00206                 switch (op) {
00207                 case 0:
00208                         done = 1;
00209                         continue;
00210                 case 1:
00211                         result = get_mode();
00212                         break;
00213                 case 2:
00214                         result = set_mode();
00215                         break;
00216                 default:
00217                         printf("\nUnknown choice!\n\n");
00218                         continue;
00219                 }
00220                 if (result != 0) {
00221                         SusiDllUnInit();
00222                         return 1;
00223                 }
00224         }
00225 
00226         result = SusiPlusSpeedSetInactive();
00227         if (result != 0) {
00228                 printf("SusiPlusSpeedSetInactive() failed\n");
00229                 SusiDllUnInit();
00230                 return 1;
00231         }
00232 
00233         result = SusiDllUnInit();
00234         if (result == FALSE) {
00235                 printf("SusiDllUnInit() failed\n");
00236                 return 1;
00237         }
00238 
00239         return 0;
00240 }
00241 


advantech
Author(s):
autogenerated on Fri Feb 7 2014 11:36:51