demo_iic.c
Go to the documentation of this file.
00001 /****************************************************************************
00002 * Copyright (c) Advantech Co., Ltd. All Rights Reserved
00003 *
00004 * File Name:
00005 *       demo_iic.c
00006 *
00007 * Programmers:
00008 *       Neo Lo
00009 *
00010 ****************************************************************************/
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 
00014 #include "REL_Linux_SUSI.H"
00015 
00016 // Return 0 if platform infomation is correctly obtained.
00017 // Otherwise, return 1.
00018 int show_platform_info(void)
00019 {
00020         int result;
00021         DWORD major, minor, year, month, date;
00022         const int BUF_LENGTH = 128;
00023         TCHAR buf[BUF_LENGTH];  // Buffer length includes the null character.
00024 
00025         SusiDllGetVersion(&major, &minor);
00026     year    = minor/10000;
00027     month   = minor%10000/100;
00028     date    = minor%100;
00029         printf("Version: %li (20%02li/%02li/%02li)\n", major, year, month, date);
00030 
00031         // Get platform name.
00032         result = SusiGetPlatformName(buf, BUF_LENGTH);
00033         if (result < 0) {
00034                 printf("SusiGetPlatformName() failed\n");
00035                 return 1;
00036         }
00037         else if (result > 0) {
00038                 printf("SusiGetPlatformName(): buffer is too short\n");
00039                 return 1;
00040         }
00041         else
00042                 printf("Platform name: %s\n", buf);
00043 
00044         // Get BIOS version.
00045         result = SusiGetBIOSVersion(buf, BUF_LENGTH);
00046         if (result < 0) {
00047                 return 1;
00048         }
00049         else if (result > 0) {
00050                 printf("SusiGetBIOSVersion(): buffer is too short\n");
00051                 return 1;
00052         }
00053         else
00054                 printf("BIOS version: %s\n", buf);
00055 
00056         return 0;
00057 }
00058 
00059 void show_menu(void)
00060 {
00061         printf("\n");
00062         printf("0) Terminate this program\n");
00063         printf("1) Read for i2c\n");
00064         printf("2) Write to i2c\n");
00065         printf("3) Write Read Combine\n");
00066         printf("Enter your choice: ");
00067 }
00068 
00069 // Return 0 on success and 1 on failure.
00070 int read_byte(void)
00071 {
00072         int result, data, i, Len = -1;
00073         BYTE address, *storage;
00074 
00075         printf("Address of the slave device: 0x");
00076         if (scanf("%x", &data) <= 0)
00077                 return 1;
00078         address = (BYTE) data;
00079 
00080         printf("Len: 0x");
00081         if (scanf("%x", &data) <= 0)
00082                 return 1;
00083         Len = data;
00084 
00085     if ( ( Len > 0 ) && ((storage = (BYTE *)malloc(Len)) != 0) )
00086     {
00087         result = SusiIICRead(SUSI_IIC_TYPE_PRIMARY, address, storage, Len);
00088         //result = SusiIICRead(address, offset, &storage);
00089         if (result == FALSE) {
00090                 printf("SusiIICRead() failed\n");
00091                 return 1;
00092         }
00093         else
00094         {
00095             printf("Data read:");
00096             for ( i = 0; i < Len; i++)
00097             {
00098                 
00099                     printf("0x%02x, ", storage[i]);
00100             }
00101             printf("\n");
00102             printf("Read successful.\n");
00103         }
00104 
00105         free(storage);
00106         return 0;
00107     }
00108     return 1;
00109 }
00110 
00111 // Return 0 on success and 1 on failure.
00112 int write_byte(void)
00113 {
00114         int result, data, i, Len = -1;
00115         BYTE address, *storage;
00116 
00117         printf("Address of the slave device: 0x");
00118         if (scanf("%x", &data) <= 0)
00119                 return 1;
00120         address = (BYTE) data;
00121 
00122         printf("Len: 0x");
00123         if (scanf("%x", &data) <= 0)
00124                 return 1;
00125         Len =  data;
00126 
00127     if ( (Len > 0) && ((storage = (BYTE *)malloc(Len)) != 0) )
00128     {
00129         for ( i = 0; i < Len; i++)
00130         {
00131                 printf("data to write[0x%x]: 0x", i);
00132                 if (scanf("%x", &data) <= 0)
00133                                 return 1;
00134                 storage[i] = (BYTE) data;
00135         }
00136 
00137         result = SusiIICWrite(SUSI_IIC_TYPE_PRIMARY, address, storage, Len);
00138         //result = SusiIICWrite(address, offset, storage);
00139         if (result == FALSE) {
00140                 printf("SusiIICWrite() failed\n");
00141                 return 1;
00142         }
00143         printf("Write successful.\n");
00144         return 0;
00145     }
00146     return 1;
00147 }
00148 
00149 int write_read_combine(void)
00150 {
00151         int result, data, i, writeLen = -1, readLen = -1;
00152         BYTE address, *wstorage, *rstorage;
00153 
00154         printf("Address of the slave device: 0x");
00155         if (scanf("%x", &data) <= 0)
00156                 return 1;
00157         address = (BYTE) data;
00158 
00159         printf("Write Len: 0x");
00160         if (scanf("%x", &data) <= 0)
00161                 return 1;
00162         writeLen = data;
00163     
00164         printf("Read Len: 0x");
00165         if (scanf("%x", &data) <= 0)
00166                 return 1;
00167         readLen =  data;
00168 
00169     if ( (writeLen > 0) && ((wstorage = (BYTE *)malloc(writeLen)) != 0) &&
00170           (readLen > 0) && ((rstorage = (BYTE *)malloc(readLen)) != 0))
00171     {
00172         for ( i = 0; i < writeLen; i++)
00173         {
00174                 printf("data to write[0x%x]: 0x", i);
00175                 if (scanf("%x", &data) <= 0)
00176                         return 1;
00177                 wstorage[i] = (BYTE) data;
00178         }
00179 
00180         result = SusiIICWriteReadCombine(SUSI_IIC_TYPE_PRIMARY, address, wstorage, writeLen, rstorage, readLen);
00181         if (result == FALSE) {
00182                 printf("SusiIICWriteReadCombine() failed\n");
00183                 return 1;
00184         }
00185         else
00186         {
00187             printf("Data read:");
00188             for ( i = 0; i < readLen; i++)
00189             {
00190                 
00191                     printf("0x%02x, ", rstorage[i]);
00192             }
00193             printf("\n");
00194             printf("Read successful.\n");
00195         }
00196 
00197         free(wstorage);
00198         free(rstorage);
00199         
00200         return 0;
00201         
00202     }
00203 
00204     return 1;
00205 }
00206 
00207 // Return 0 on success and 1 on failure.
00208 int scan_i2c(void)
00209 {
00210         int result, data;
00211         BYTE address, offset, storage;
00212 
00213         printf("Address of the slave device: 0x");
00214         if (scanf("%x", &data) <= 0)
00215                 return 1;
00216         address = (BYTE) data;
00217 
00218         printf("Offset: 0x");
00219         if (scanf("%x", &data) <= 0)
00220                 return 1;
00221         offset = (BYTE) data;
00222 
00223         printf("One byte data to write: 0x");
00224         if (scanf("%x", &data) <= 0)
00225                 return 1;
00226         storage = (BYTE) data;
00227 
00228     result = SusiIICWrite(SUSI_IIC_TYPE_PRIMARY, address+offset, &storage, sizeof(BYTE));
00229         //result = SusiIICWrite(address, offset, storage);
00230         if (result == FALSE) {
00231                 printf("SusiIICWrite() failed\n");
00232                 return 1;
00233         }
00234         return 0;
00235 }
00236 
00237 int main(void)
00238 {
00239         int result, done, op;
00240 
00241         result = SusiInit();
00242         if (result == FALSE) {
00243                 printf("SusiInit() failed\n");
00244                 return 1;
00245         }
00246 
00247         result = SusiIICAvailable();
00248         if (result == 0) {
00249                 printf("SusiIICAvailable() failed\n");
00250                 SusiDllUnInit();
00251                 return 1;
00252         }
00253 
00254         result = show_platform_info();
00255 
00256         done = 0;
00257         while (! done) {
00258                 show_menu();
00259                 if (scanf("%i", &op) <= 0)
00260                         op = -1;
00261 
00262                 switch (op) {
00263                 case 0:
00264                         done = 1;
00265                         continue;
00266                 case 1:
00267                         result = read_byte();
00268                         break;
00269                 case 2:
00270                         result = write_byte();
00271                         break;
00272                 case 3:
00273             result = write_read_combine();
00274                         break;
00275                 default:
00276                         printf("\nUnknown choice!\n\n");
00277                         continue;
00278                 }
00279                 if (result != 0) {
00280                         SusiDllUnInit();
00281                         return 1;
00282                 }
00283         }
00284 
00285         result = SusiDllUnInit();
00286         if (result == FALSE) {
00287                 printf("SusiDllUnInit() failed\n");
00288                 return 1;
00289         }
00290 
00291         return 0;
00292 }
00293 


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