/** * @file main.c * * @brief Addressing multiple BeagleBone GPIO pins in C * @details Self-contined program that will turn ON/OFF a specified group of * of GPIO pins in the beaglebone board. This code was possible due to prior * efforts by Christian Mitchell, Markus Klink and Matt Richardson. To compile * and run this code, type something like "gcc main.c ; ./a.out ; rm -f a.out" * * @author Nuno Alves * @date 17/March/2012 **/ #include #include #include #include #include #include //If DEBUG is defined to TRUE (1), intermediate debug messages will be displayed //when code is executing. You may set it to FALSE (0) for no debug information. #define DEBUG 0 //The following #defines make it easier for the user to add differnt pins #define P00 0 #define P01 1 #define P02 2 #define P03 3 #define P04 4 #define P05 5 #define P06 6 #define P07 7 /** @brief Contains all elements which we need to pass into the beagleboard * to specify which GPIO pin we want to turn ON/OFF **/ struct gpioID{ char *PINNAME; //e.g: P07 char * GPIOID; //e.g: gpio0[30] int GPIONUMBER; //e.g: 30 char *GPIOMUX; //e.g: gpmc_wait0; }; /** * @brief Writes a value to a particular GPIO, logic-HIGH or logic-LOW. * @param GPIONUMBER The GPIO number (e.g. 38 for gpio1[6]) * @param value The logic value we want to write to the GPIO (0 or 1) **/ void write_GPIO_value(int GPIONUMBER, int value){ char export_filename[50]; FILE *f = NULL; sprintf(export_filename, "/sys/class/gpio/gpio%d/value",GPIONUMBER); f = fopen(export_filename,"w"); assert(f!=NULL); fprintf(f , "%d",value); pclose(f); } /** * @brief Tells the OS that we are done with using GPIOs * @param selected_GPIOs[] initialized array of gpioID. * @param selectedPins[] Array with the user specified pins * @param sizeof_selectedPins Number of pins that were specified by the user **/ void cleanup_GPIO(struct gpioID selected_GPIOs[],int selectedPins[], int nbr_selectedPins){ char export_filename[50]; FILE *f = NULL; int i; for (i=0; i= 0x01 ; data_to_write >>=1){ turn_ON_OFF_pins(enabled_gpio,data_to_write,nbr_selectedPins); vDelay(150); } //we should now tell the OS that we are done with the GPIOs cleanup_GPIO(enabled_gpio,selectedPins,nbr_selectedPins); return 0; }