openBMC(todo)

https://github.com/facebook/openbmc

 

1、GPIO

int gpio_open(gpio_st *g, int gpio)
{
  char buf[128];
  int rc;

  snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%u/value", gpio);
  rc = open(buf, O_RDWR);
  if (rc == -1) {
    rc = errno;
    LOG_ERR(rc, "Failed to open %s", buf);
    return -rc;
  }
  g->gs_fd = rc;
  g->gs_gpio = gpio;
  return 0;
}


gpio_value_en gpio_read(gpio_st *g)
{
  char buf[32] = {0};
  gpio_value_en v;
  lseek(g->gs_fd, 0, SEEK_SET);
  read(g->gs_fd, buf, sizeof(buf));
  v = atoi(buf) ? GPIO_VALUE_HIGH : GPIO_VALUE_LOW;
  LOG_VER("read gpio=%d value=%d %d", g->gs_gpio, atoi(buf), v);
  return v;
}

void gpio_write(gpio_st *g, gpio_value_en v)
{
  lseek(g->gs_fd, 0, SEEK_SET);
  write(g->gs_fd, (v == GPIO_VALUE_HIGH) ? "1" : "0", 1);
  LOG_VER("write gpio=%d value=%d", g->gs_gpio, v);
}

int gpio_change_direction(gpio_st *g, gpio_direction_en dir)
{
  char buf[128];
  char *val;
  int fd = -1;
  int rc = 0;

  snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%u/direction", g->gs_gpio);
  fd = open(buf, O_WRONLY);
  if (fd == -1) {
    rc = errno;
    LOG_ERR(rc, "Failed to open %s", buf);
    return -rc;
  }

  val = (dir == GPIO_DIRECTION_IN) ? "in" : "out";
  write(fd, val, strlen(val));

  LOG_VER("change gpio=%d direction=%s", g->gs_gpio, val);

  if (fd != -1) {
    close(fd);
  }
  return -rc;
}

 

 

 

2、fan

int write_fan_speed(const int fan, const int value) {
  /*
   * The hardware fan numbers for pwm control are different from
   * both the physical order in the box, and the mapping for reading
   * the RPMs per fan, above.
   */

  int real_fan = fan_to_pwm_map[fan];

  if (value == 0) {
#if defined(CONFIG_WEDGE100)
    return write_fan_value(real_fan, "fantray%d_pwm", 0);
#else
    return write_fan_value(real_fan, "pwm%d_en", 0);
#endif
  } else {
    int unit = value * PWM_UNIT_MAX / 100;
    int status;

#if defined(CONFIG_WEDGE100)
    // Note that PWM for Wedge100 is in 32nds of a cycle
    return write_fan_value(real_fan, "fantray%d_pwm", unit);
#else
    if (unit == PWM_UNIT_MAX)
      unit = 0;

    if ((status = write_fan_value(real_fan, "pwm%d_type", 0)) != 0 ||
      (status = write_fan_value(real_fan, "pwm%d_rising", 0)) != 0 ||
      (status = write_fan_value(real_fan, "pwm%d_falling", unit)) != 0 ||
      (status = write_fan_value(real_fan, "pwm%d_en", 1)) != 0) {
      return status;
    }
#endif
  }
}


int write_fan_value(const int fan, const char *device, const int value) {
  char full_name[LARGEST_DEVICE_NAME];
  char device_name[LARGEST_DEVICE_NAME];
  char output_value[LARGEST_DEVICE_NAME];

  snprintf(device_name, LARGEST_DEVICE_NAME, device, fan);
  snprintf(full_name, LARGEST_DEVICE_NAME, "%s/%s", PWM_DIR, device_name);
  snprintf(output_value, LARGEST_DEVICE_NAME, "%d", value);
  return write_device(full_name, output_value);
}

 

posted @ 2017-08-09 21:48  soul.stone  阅读(1233)  评论(0编辑  收藏  举报