openBMC(todo)

简介: https://github.com/facebook/openbmc 1、GPIOint gpio_open(gpio_st *g, int gpio){ char buf[128]; int rc; snprintf(buf, sizeof(buf), "/sys/cla...

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);
}

 

目录
相关文章
|
Java 编译器 程序员
Checked Exception 和 Unchecked Exception 有什么区别?
Checked Exception 和 Unchecked Exception 有什么区别?
|
7月前
|
Java iOS开发
@try{}@catch (NSException *exception) {}在object c中的应用及问题
@try{}@catch (NSException *exception) {}在object c中的应用及问题
45 0
|
6月前
|
Python 容器
使用flet创建todo应用
使用flet创建todo应用
|
7月前
|
搜索推荐 Java
TODO有什么妙用
`TODO` 是Java开发中用于标记未完成功能或待修复问题的注解,能帮助追踪和管理开发任务。在代码中添加 `// TODO` 标记,如 `// TODO do something`,之后可通过搜索快速定位。IDEA还支持自定义`TODO`类型和颜色,以及全局查看和过滤器功能。阿里巴巴开发手册建议使用 `TODO` 表示待实现功能,`FIXME` 标记错误代码。推荐创建个性化代码模板以提高效率。
|
Java
throws 与 throw
/* * 有些时候,我们是可以对异常进行处理的,但是又有些时候,我们根本就没有权限去处理某个异常。 * 或者说,我处理不了,我就不处理了。 * 为了解决出错问题,Java针对这种情况,就提供了另一种处理方案:抛出。
1090 0
|
Java Android开发
Todo List
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/voidreturn/article/details/78702613 fragment里面如何处理back按键事件。
1028 0
|
存储 搜索推荐 NoSQL