demo_gpio.c
Go to the documentation of this file.
00001 /****************************************************************************
00002 * Copyright (c) Advantech Co., Ltd. All Rights Reserved
00003 *
00004 * File Name:
00005 *       demo_gpio.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 
00033         // Get platform name.
00034         result = SusiGetPlatformName(buf, BUF_LENGTH);
00035         if (result < 0) {
00036                 printf("SusiGetPlatformName() failed\n");
00037                 return 1;
00038         }
00039         else if (result > 0) {
00040                 printf("SusiGetPlatformName(): buffer is too short\n");
00041                 return 1;
00042         }
00043         else
00044                 printf("Platform name: %s\n", buf);
00045 
00046         // Get BIOS version.
00047         result = SusiGetBIOSVersion(buf, BUF_LENGTH);
00048         if (result < 0) {
00049                 return 1;
00050         }
00051         else if (result > 0) {
00052                 printf("SusiGetBIOSVersion(): buffer is too short\n");
00053                 return 1;
00054         }
00055         else
00056                 printf("BIOS version: %s\n", buf);
00057 
00058         return 0;
00059 }
00060 
00061 void show_menu(void)
00062 {
00063         printf("\n");
00064         printf("0) Terminate this program\n");
00065         printf("1) Get GPIO direction mask\n");
00066         printf("2) Read a single GPIO pin\n");
00067         printf("3) Read multiple GPIO pins\n");
00068         printf("4) Write a single GPIO pin\n");
00069         printf("5) Write multiple GPIO pins\n");
00070         printf("6) Set a GPIO direction\n");
00071         printf("7) Set multi GPIO directions\n");
00072         printf("Enter your choice: ");
00073 }
00074 
00075 // Return 0 on success and 1 on failure.
00076 int get_dir_mask(void)
00077 {
00078         DWORD TargetPinMask, PinDirMask, InPinDirMask, OutPinDirMask;
00079 
00080         if (SusiIOQueryMask(ESIO_SMASK_PIN_FULL, &TargetPinMask) == FALSE) {
00081                 printf("SusiIOQueryMask() failed\n");
00082                 return 1;
00083         }
00084     
00085         if (SusiIOQueryMask(ESIO_DMASK_DIRECTION, &PinDirMask) == FALSE) {
00086                 printf("SusiIOQueryMask() failed\n");
00087                 return 1;
00088         }
00089     
00090         if (SusiIOQueryMask(ESIO_DMASK_IN, &InPinDirMask) == FALSE) {
00091                 printf("SusiIOQueryMask() failed\n");
00092                 return 1;
00093         }
00094     
00095         if (SusiIOQueryMask(ESIO_DMASK_OUT, &OutPinDirMask) == FALSE) {
00096                 printf("SusiIOQueryMask() failed\n");
00097                 return 1;
00098         }
00099     
00100         else
00101         {
00102         printf("Total pins mask             = 0x%08lX\n", TargetPinMask);
00103         printf("direction mask(0:out, 1:in) = 0x%08lX\n", PinDirMask);
00104                 printf("input pins mask             = 0x%08lX\n", InPinDirMask);
00105                 printf("output pins mask            = 0x%08lX\n", OutPinDirMask);
00106         }
00107         return 0;
00108 }
00109 
00110 // Return 0 on success and 1 on failure.
00111 int read_pin(void)
00112 {
00113         int result, data;
00114         BYTE pin;
00115         BOOL status;
00116 
00117         printf("Which pin: ");
00118         if (scanf("%i", &data) <= 0)
00119                 return 1;
00120         pin = (BYTE) data;
00121         result = SusiIOReadEx(pin, &status);
00122         if (result == FALSE) {
00123                 printf("SusiIOReadEx() failed\n");
00124                 return 1;
00125         }
00126         else
00127                 printf("Status: %s\n", status ? "ONE" : "ZERO");
00128         return 0;
00129 }
00130 
00131 // Return 0 on success and 1 on failure.
00132 int read_pins(void)
00133 {
00134         int result, data;
00135         DWORD pins, status;
00136 
00137         printf("Bit mask of the pins: 0x");
00138         if (scanf("%x", &data) <= 0)
00139                 return 1;
00140         pins = (DWORD) data;
00141         result = SusiIOReadMultiEx(pins, &status);
00142         if (result == FALSE) {
00143                 printf("SusiIOReadMultiEx() failed\n");
00144                 return 1;
00145         }
00146         else
00147                 printf("Status: 0x%lx\n", status);
00148         return 0;
00149 }
00150 
00151 // Return 0 on success and 1 on failure.
00152 int write_pin(void)
00153 {
00154         int result, data;
00155         BYTE pin;
00156         BOOL boolean;
00157 
00158         printf("Which pin: ");
00159         if (scanf("%i", &data) <= 0)
00160                 return 1;
00161         pin = (BYTE) data;
00162         printf("Value (0 or 1): ");
00163         if (scanf("%i", &data) <= 0)
00164                 return 1;
00165         boolean = (data) ? 1 : 0;
00166         result = SusiIOWriteEx(pin, boolean);
00167         if (result == FALSE) {
00168                 printf("SusiIOWriteEx() failed\n");
00169                 return 1;
00170         }
00171         return 0;
00172 }
00173 
00174 // Return 0 on success and 1 on failure.
00175 int write_pins(void)
00176 {
00177         int result, data;
00178         DWORD pins, status;
00179 
00180         printf("Bit mask of the pins: 0x");
00181         if (scanf("%x", &data) <= 0)
00182                 return 1;
00183         pins = (DWORD) data;
00184         printf("Bit pattern: 0x");
00185         if (scanf("%x", &data) <= 0)
00186                 return 1;
00187         status = (DWORD) data;
00188         result = SusiIOWriteMultiEx(pins, status);
00189         if (result == FALSE) {
00190                 printf("SusiIOWriteMulti() failed\n");
00191                 return 1;
00192         }
00193         return 0;
00194 }
00195 // Return       0       on success and 1 on     failure.
00196 int set_pin_direction(void)
00197 {
00198         BYTE PinNum, IO;
00199         int result, data;
00200          
00201         printf("Which pin: ");
00202         if (scanf("%x", &data) <= 0)
00203                 return 1;
00204         PinNum = (BYTE)data;
00205         printf("direction (input pin :1 output pin:0 ): \n");
00206         if (scanf("%x", &data) <= 0)
00207                 return 1;
00208         IO = (BYTE)     data ;
00209          
00210         result=SusiIOSetDirection(PinNum,IO, NULL);
00211         if (result == FALSE) 
00212         {
00213                 printf("set_pin_direction() failed\n");
00214                 return 1;
00215         }
00216         return 0;
00217 }
00218 
00219 // Return       0       on success and 1 on     failure.
00220 int set_pins_direction(void)
00221 {
00222         int result, data;
00223         DWORD PinNum, PinDirMask;
00224 
00225         printf("mask of the pins: 0x");
00226         if (scanf("%x", &data) <= 0)
00227                 return 1;
00228         PinNum = (DWORD) data;
00229         printf("PinDirMask(0: output, 1: input): 0x");
00230         if (scanf("%x", &data) <= 0)
00231                 return 1;
00232         PinDirMask = (DWORD) data;
00233         result = SusiIOSetDirectionMulti(PinNum, &PinDirMask);
00234         if (result == FALSE) 
00235         {
00236                 printf("SusiIOSetDirectionMulti() failed\n");
00237                 return 1;
00238         }
00239         return 0;
00240 }
00241 
00242 int main(void)
00243 {
00244         int result, done, op;
00245 
00246         result = SusiDllInit();
00247         if (result == FALSE) {
00248                 printf("SusiDllInit() failed\n");
00249                 return 1;
00250         }
00251 
00252         result = SusiIOAvailable();
00253         if (result == 0) {
00254                 printf("SusiIOAvailable() failed\n");
00255                 SusiDllUnInit();
00256                 return 1;
00257         }
00258 
00259         result = show_platform_info();
00260 
00261         done = 0;
00262         while (! done) {
00263                 show_menu();
00264                 if (scanf("%i", &op) <= 0)
00265                         op = -1;
00266 
00267                 switch (op) {
00268                 case 0:
00269                         done = 1;
00270                         continue;
00271                 case 1:
00272                         result = get_dir_mask();
00273                         break;
00274                 case 2:
00275                         result = read_pin();
00276                         break;
00277                 case 3:
00278                         result = read_pins();
00279                         break;
00280                 case 4:
00281                         result = write_pin();
00282                         break;
00283                 case 5:
00284                         result = write_pins();
00285                         break;
00286                 case 6:
00287                         result = set_pin_direction();//PCA9554 gpio
00288                         break;
00289                 case 7:
00290                         result = set_pins_direction();  
00291                         break;          
00292                 default:
00293                         printf("\nUnknown choice!\n\n");
00294                         continue;
00295                 }
00296                 if (done != 1 && result < 0) {
00297                         SusiDllUnInit();
00298                         return 1;
00299                 }
00300         }
00301 
00302         result = SusiDllUnInit();
00303         if (result == FALSE) {
00304                 printf("SusiDllUnInit() failed\n");
00305                 return 1;
00306         }
00307 
00308         return 0;
00309 }
00310 


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