/* This file is licensed under the X11 license: Copyright (C) 2013 Andreas Ehliar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANDREAS EHLIAR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the name of Andreas Ehliar shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Andreas Ehliar. */ #include #include #include #include #include #include #include #include #include #include // General purpose error message // A real system would probably have a better error handling method... static void panic(char *message) { fprintf(stderr,"Fatal error: %s\n", message); exit(1); } #define MAX_LOGENTRIES 200000 static unsigned int logindex; static struct timespec timestamps[MAX_LOGENTRIES]; static void logtimestamp(void) { clock_gettime(CLOCK_MONOTONIC, ×tamps[logindex++]); if(logindex > MAX_LOGENTRIES){ logindex = 0; } } static void dumptimestamps(int unused) { FILE *fp = fopen("timestamps0.txt","w"); int i; for(i=0; i < logindex; i++){ if(timestamps[i].tv_sec > 0){ fprintf(fp,"%d.%09d\n", (int) timestamps[i].tv_sec, (int) timestamps[i].tv_nsec); } } fclose(fp); exit(0); } // Initialize a GPIO pin in Linux using the sysfs interface FILE *init_gpio(int gpioport) { // Export the pin to the GPIO directory FILE *fp = fopen("/sys/class/gpio/export","w"); fprintf(fp,"%d",gpioport); fclose(fp); // Set the pin as an output char filename[256]; sprintf(filename,"/sys/class/gpio/gpio%d/direction",gpioport); fp = fopen(filename,"w"); if(!fp){ panic("Could not open gpio file"); } fprintf(fp,"out"); fclose(fp); // Open the value file and return a pointer to it. sprintf(filename,"/sys/class/gpio/gpio%d/value",gpioport); fp = fopen(filename,"w"); if(!fp){ panic("Could not open gpio file"); } return fp; } // Given a FP in the stepper struct, set the I/O pin // to the specified value. Uses the sysfs GPIO interface. void setiopin(FILE *fp, int val) { fprintf(fp,"%d\n",val); fflush(fp); } // Demo program for running a stepper connected to the Raspberry PI // platform. int main(int argc, char **argv) { int delay = 1000; // Note: delay in us here FILE *pin0 = init_gpio(14); FILE *pin1 = init_gpio(15); FILE *pin2 = init_gpio(17); FILE *pin3 = init_gpio(18); signal(SIGINT, dumptimestamps); while(1){ usleep(delay); logtimestamp(); setiopin(pin0,1); usleep(delay); logtimestamp(); setiopin(pin3,0); usleep(delay); logtimestamp(); setiopin(pin1,1); usleep(delay); logtimestamp(); setiopin(pin0,0); usleep(delay); logtimestamp(); setiopin(pin2,1); usleep(delay); logtimestamp(); setiopin(pin1,0); usleep(delay); logtimestamp(); setiopin(pin3,1); usleep(delay); logtimestamp(); setiopin(pin2,0); } }