unsigned int HCA700_get_battery() { unsigned int ret; ret = get_gpio_value(IO_PORT,DC5V_PLUGIN_INDICATOR_PIN); ret |= get_gpio_value(IO_PORT,CHARGER_FULL_INDICATOR_PIN)<<1; ret |= get_gpio_value(IO_PORT,NO_BATTERY_INDICATOR_PIN)<<2; }
unsigned int HCA700_get_key() { unsigned int ret; ret = get_gpio_value(IO_PORT,KEY_VOL_DOWN_PIN); ret |= get_gpio_value(IO_PORT,KEY_VOL_UP_PIN) << 1; ret |= get_gpio_value(IO_PORT,KEY_VOL_POWER_PIN) << 2; ret |= get_gpio_value(IO_PORT,KEY_VOL_MUTE_PIN) << 3; return ret; }
int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { int status = 0; if (strcmp(argv[1], "set") == 0) { u32 gpio_number = 0; u32 gpio_value = 0; if (argc != 4) { printf("error invalid usage\n"); return -1; } gpio_number = simple_strtoul(argv[2], NULL, 10); gpio_value = simple_strtoul(argv[3], NULL, 10); configure_pad_mode(gpio_number); configure_gpio_output(gpio_number); set_gpio_output(gpio_number, (gpio_value == 0) ? 0 : 1); }else if(strcmp(argv[1], "get") == 0){ u32 gpio_number = 0; if (argc != 3) { printf("error invalid usage\n"); return -1; } gpio_number = simple_strtoul(argv[2], NULL, 10); printf("Read GPIO Pin %d = 0x%x\n", gpio_number, get_gpio_value(gpio_number)); } return status; }
//reading the input value if it's 1 the button is not pushed if 0 the button is pushed int GPIO_Controller::getGPIO_Value(string& val) { stringstream ss; ss << this->gpio_number; string numb_string = ss.str(); string gpio_direction = "/sys/class/gpio/gpio" + numb_string + "/value"; ifstream get_gpio_value(gpio_direction.c_str()); if(get_gpio_value < 0) { cout << "Error while reading value file of GPIO number: " << this->gpio_number << endl; return -1; } else { get_gpio_value >> val; get_gpio_value.close(); } if(val == "0") { return 0; } else { return 1; } }
int main(int argc, char *argv[]) { extern char *optarg; char *cptr; int gpio_value = 0; int nchannel = 0; int c; int i; opterr = 0; while ((c = getopt(argc, argv, "g:io:ck")) != -1) { switch (c) { case 'g': gl_gpio_base = (int)strtoul(optarg, &cptr, 0); if (cptr == optarg) usage(argv[0]); break; case 'i': gpio_opt = IN; break; case 'o': gpio_opt = OUT; gpio_value = (int)strtoul(optarg, &cptr, 0); if (cptr == optarg) usage(argv[0]); break; case 'c': gpio_opt = CYLON; break; case 'k': gpio_opt = KIT; break; case '?': usage(argv[0]); default: usage(argv[0]); } } if (gl_gpio_base == 0) { usage(argv[0]); } nchannel = open_gpio_channel(gl_gpio_base); signal(SIGTERM, signal_handler); /* catch kill signal */ signal(SIGHUP, signal_handler); /* catch hang up signal */ signal(SIGQUIT, signal_handler); /* catch quit signal */ signal(SIGINT, signal_handler); /* catch a CTRL-c signal */ switch (gpio_opt) { case IN: set_gpio_direction(gl_gpio_base, nchannel, "in"); gpio_value=get_gpio_value(gl_gpio_base, nchannel); fprintf(stdout,"0x%08X\n", gpio_value); break; case OUT: set_gpio_direction(gl_gpio_base, nchannel, "out"); set_gpio_value(gl_gpio_base, nchannel, gpio_value); break; case CYLON: #define CYLON_DELAY_USECS (10000) set_gpio_direction(gl_gpio_base, nchannel, "out"); for (;;) { for(i=0; i < ARRAY_SIZE(cylon); i++) { gpio_value=(int)cylon[i]; set_gpio_value(gl_gpio_base, nchannel, gpio_value); } usleep(CYLON_DELAY_USECS); } case KIT: #define KIT_DELAY_USECS (10000) set_gpio_direction(gl_gpio_base, nchannel, "out"); for (;;) { for (i=0; i<ARRAY_SIZE(kit); i++) { gpio_value=(int)kit[i]; set_gpio_value(gl_gpio_base, nchannel, gpio_value); } usleep(KIT_DELAY_USECS); } default: break; } close_gpio_channel(gl_gpio_base); return 0; }