demo_smbus.c
Go to the documentation of this file.
00001 /****************************************************************************
00002 * Copyright (c) Advantech Co., Ltd. All Rights Reserved
00003 *
00004 * File Name:
00005 *       demo_smbus.c
00006 *
00007 * Programmers:
00008 *       Neo Lo
00009 *
00010 ****************************************************************************/
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 
00015 #include "REL_Linux_SUSI.H"
00016 
00017 // Return 0 if platform infomation is correctly obtained.
00018 // Otherwise, return 1.
00019 int show_platform_info(void)
00020 {
00021         int result;
00022         DWORD major, minor, year, month, date;
00023         const int BUF_LENGTH = 128;
00024         TCHAR buf[BUF_LENGTH];  // Buffer length includes the null character.
00025 
00026         SusiDllGetVersion(&major, &minor);
00027     year    = minor/10000;
00028     month   = minor%10000/100;
00029     date    = minor%100;
00030         printf("Version: %li (20%02li/%02li/%02li)\n", major, year, month, date);
00031 
00032         // Get platform name.
00033         result = SusiGetPlatformName(buf, BUF_LENGTH);
00034         if (result < 0) {
00035                 printf("SusiGetPlatformName() failed\n");
00036                 return 1;
00037         }
00038         else if (result > 0) {
00039                 printf("SusiGetPlatformName(): buffer is too short\n");
00040                 return 1;
00041         }
00042         else
00043                 printf("Platform name: %s\n", buf);
00044 
00045         // Get BIOS version.
00046         result = SusiGetBIOSVersion(buf, BUF_LENGTH);
00047         if (result < 0) {
00048                 return 1;
00049         }
00050         else if (result > 0) {
00051                 printf("SusiGetBIOSVersion(): buffer is too short\n");
00052                 return 1;
00053         }
00054         else
00055                 printf("BIOS version: %s\n", buf);
00056 
00057         return 0;
00058 }
00059 
00060 void show_menu(void)
00061 {
00062         printf("\n");
00063         printf("0) Terminate this program\n");
00064         printf("1) Read a single byte\n");
00065         printf("2) Read a single word\n");
00066         printf("3) Read multiple bytes\n");
00067         printf("4) Write a single byte\n");
00068         printf("5) Write a single word\n");
00069         printf("6) Write multiple bytes\n");
00070         printf("7) Scan device\n");
00071         printf("Enter your choice: ");
00072 }
00073 
00074 int read_byte(void)
00075 {
00076         int result, data;
00077         BYTE address, offset, storage;
00078 
00079         printf("Address of the slave device: 0x");
00080         if (scanf("%x", &data) <= 0)
00081                 return 1;
00082         address = (BYTE) data;
00083 
00084         printf("Offset: 0x");
00085         if (scanf("%x", &data) <= 0)
00086                 return 1;
00087         offset = (BYTE) data;
00088 
00089         result = SusiSMBusReadByte(address, offset, &storage);
00090         if (result == FALSE) {
00091                 printf("SusiSMBusReadByte() failed\n");
00092                 return 1;
00093         }
00094         else
00095                 printf("Data read: 0x%02x\n", (int) storage);
00096         return 0;
00097 }
00098 
00099 int read_word(void)
00100 {
00101         int result, data;
00102         BYTE address, offset;
00103         WORD storage;
00104 
00105         printf("Address of the slave device: 0x");
00106         if (scanf("%x", &data) <= 0)
00107                 return 1;
00108         address = (BYTE) data;
00109 
00110         printf("Offset: 0x");
00111         if (scanf("%x", &data) <= 0)
00112                 return 1;
00113         offset = (BYTE) data;
00114 
00115         result = SusiSMBusReadWord(address, offset, &storage);
00116         if (result == FALSE) {
00117                 printf("SusiSMBusReadWord() failed\n");
00118                 return 1;
00119         }
00120         else
00121                 printf("Data read: 0x%04x\n", (int) storage);
00122         return 0;
00123 }
00124 
00125 int read_bytes(void)
00126 {
00127         int result, data, count, index;
00128         BYTE address, offset, *storage;
00129 
00130         printf("Address of the slave device: 0x");
00131         if (scanf("%x", &data) <= 0)
00132                 return 1;
00133         address = (BYTE) data;
00134 
00135         printf("Offset: 0x");
00136         if (scanf("%x", &data) <= 0)
00137                 return 1;
00138         offset = (BYTE) data;
00139 
00140         printf("Count: ");
00141         if (scanf("%i", &count) <= 0)
00142                 return 1;
00143         storage = (BYTE*) malloc(count);
00144         if (! storage) {
00145                 printf("Memory allocation failed\n");
00146                 return 1;
00147         }
00148 
00149         result = SusiSMBusReadByteMulti(address, offset, storage, count);
00150         if (result == FALSE) {
00151                 printf("SusiSMBusReadByteMulti() failed\n");
00152                 return 1;
00153         }
00154         else {
00155                 printf("Data read:");
00156                 for (index = 0; index < count; index++) {
00157                         printf(" %02x", storage[index]);
00158                 }
00159                 printf("\n");
00160         }
00161         free(storage);
00162         return 0;
00163 }
00164 
00165 int write_byte(void)
00166 {
00167         int result, data;
00168         BYTE address, offset, storage;
00169 
00170         printf("Address of the slave device: 0x");
00171         if (scanf("%x", &data) <= 0)
00172                 return 1;
00173         address = (BYTE) data;
00174 
00175         printf("Offset: 0x");
00176         if (scanf("%x", &data) <= 0)
00177                 return 1;
00178         offset = (BYTE) data;
00179 
00180         printf("Data to write: 0x");
00181         if (scanf("%x", &data) <= 0)
00182                 return 1;
00183         storage = (BYTE) data;
00184 
00185         result = SusiSMBusWriteByte(address, offset, storage);
00186         if (result == FALSE) {
00187                 printf("SusiSMBusWriteByte() failed\n");
00188                 return 1;
00189         }
00190         return 0;
00191 }
00192 
00193 int write_word(void)
00194 {
00195         int result, data;
00196         BYTE address, offset;
00197         WORD storage;
00198 
00199         printf("Address of the slave device: 0x");
00200         if (scanf("%x", &data) <= 0)
00201                 return 1;
00202         address = (BYTE) data;
00203 
00204         printf("Offset: 0x");
00205         if (scanf("%x", &data) <= 0)
00206                 return 1;
00207         offset = (BYTE) data;
00208 
00209         printf("Data to write: 0x");
00210         if (scanf("%x", &data) <= 0)
00211                 return 1;
00212         storage = (WORD) data;
00213 
00214         result = SusiSMBusWriteWord(address, offset, storage);
00215         if (result == FALSE) {
00216                 printf("SusiSMBusWriteWord() failed\n");
00217                 return 1;
00218         }
00219         return 0;
00220 }
00221 
00222 int write_bytes(void)
00223 {
00224         int result, data, count, index;
00225         BYTE address, offset, *storage, bt_count;
00226 
00227         printf("Address of the slave device: 0x");
00228         if (scanf("%x", &data) <= 0)
00229                 return 1;
00230         address = (BYTE) data;
00231 
00232         printf("Offset: 0x");
00233         if (scanf("%x", &data) <= 0)
00234                 return 1;
00235         offset = (BYTE) data;
00236 
00237         printf("Count: ");
00238         if (scanf("%i", &count) <= 0)
00239                 return 1;
00240         bt_count = (BYTE) count;
00241         storage = (BYTE*) malloc(bt_count);
00242         if (! storage) {
00243                 printf("Memory allocation failed\n");
00244                 return 1;
00245         }
00246 
00247         for (index = 0; index < bt_count; index++) {
00248                 printf("Data %i: 0x", index);
00249                 if (scanf("%x", &data) <= 0)
00250                         return 1;
00251                 storage[index] = (BYTE) data;
00252         }
00253 
00254         result = SusiSMBusWriteByteMulti(address, offset, storage, bt_count);
00255         if (result == FALSE) {
00256                 printf("SusiSMBusWriteByteMulti() failed\n");
00257                 return 1;
00258         }
00259         free(storage);
00260         return 0;
00261 }
00262 
00263 int scan_device(void)
00264 {
00265         int i, j, result;
00266         const int MAX_PORT = 0x80;
00267 
00268         printf("Existing smbus device address :\n");
00269         for (i=0; i < MAX_PORT; i+=16)
00270         {
00271                 for (j = 0; j  < 16; j++)
00272                 {
00273                         result = SusiSMBusScanDevice(i+j);
00274 
00275                         if (result < 0)
00276                         {
00277                                 printf("SusiSMBusScanDevice() result < 0");
00278                                 return 1;
00279                         }
00280 
00281                         if (result == 1)
00282                                 printf("0x%02x ", (i+j) << 1);
00283                 }
00284         }
00285         printf("\n");
00286                 
00287         return 0;
00288 }
00289 
00290 int main(void)
00291 {
00292         int result, done, op;
00293 
00294         result = SusiDllInit();
00295         if (result == FALSE) {
00296                 printf("SusiDllInit() failed\n");
00297                 return 1;
00298         }
00299 
00300         result = SusiSMBusAvailable();
00301         if (result == 0) {
00302                 printf("SusiSMBusAvailable() failed\n");
00303                 SusiDllUnInit();
00304                 return 1;
00305         }
00306 
00307         result = show_platform_info();
00308         
00309         done = 0;
00310         while (! done) {
00311                 show_menu();
00312                 if (scanf("%i", &op) <= 0)
00313                         op = -1;
00314 
00315                 switch (op) {
00316                 case 0:
00317                         done = 1;
00318                         continue;
00319                 case 1:
00320                         result = read_byte();
00321                         break;
00322                 case 2:
00323                         result = read_word();
00324                         break;
00325                 case 3:
00326                         result = read_bytes();
00327                         break;
00328                 case 4:
00329                         result = write_byte();
00330                         break;
00331                 case 5:
00332                         result = write_word();
00333                         break;
00334                 case 6:
00335                         result = write_bytes();
00336                         break;
00337                 case 7:
00338                         result = scan_device();
00339                         break;
00340                 default:
00341                         printf("\nUnknown choice!\n\n");
00342                         continue;
00343                 }
00344                 if (result != 0) {
00345                         printf("Library returns with error.\n");
00346                         SusiDllUnInit();
00347                         return 1;
00348                 }
00349         }
00350 
00351         result = SusiDllUnInit();
00352         if (result == FALSE) {
00353                 printf("SusiDllUnInit() failed\n");
00354                 return 1;
00355         }
00356 
00357         return 0;
00358 }
00359 


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