?? oap-power.c
字號:
/* * $Id: oap-power.c,v 1.4 2005/02/19 14:39:09 dwalters Exp $ * * Copyright (C) 2003 Dafydd Walters <dwalters@dragontechnology.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <linux/i2c.h>#include <linux/i2c-dev.h>#include "../../config_api/config_api.h"#include "../include26/i2c-dev-26.h"#define VERSION "1.0"#define CONFIG_FILENAME "/etc/oap.conf"#define READ_BATT_VOLTAGE_COMMAND 1#define READ_EXT_VOLTAGE_COMMAND 2#define READ_CHARGER_VOLTAGE_COMMAND 3#define READ_STATE_COMMAND 4#define READ_ERROR_CODE_COMMAND 5#define READ_SWITCHES_COMMAND 6#define READ_GOOD_EXT_THRESHOLD_COMMAND 7#define WRITE_GOOD_EXT_THRESHOLD_COMMAND 100static int device_address;static double vb_cal_factor;static double ve_cal_factor;static double vc_cal_factor;static char filename[100];static int file;static void read_config();static int check_params(int argc, char* argv[]);static void report_state(int file);static void calibrate(int file, char* name, char* config_setting, int smbus_command);static void report_error_code(int file);static void set_threshold(int file, double volts);static void show_usage(void);int main(int argc, char* argv[]){ if (!check_params(argc, argv)) { show_usage(); exit(EXIT_FAILURE); } /* Load configuration settings */ read_config(); /* Open device */ if ((file = open(filename, O_RDWR)) < 0) { fprintf(stderr, "Unable to open device %s\n", filename); exit(EXIT_FAILURE); } /* Select slave device address */ if (ioctl(file, I2C_SLAVE, device_address) < 0) { fprintf(stderr, "Unable to set slave device address to %d\n", device_address); close(file); exit(EXIT_FAILURE); } if (strcmp(argv[1], "r") == 0) { /* Report state of power management module */ report_state(file); } else if (strcmp(argv[1], "cb") == 0) { /* Calibrate battery voltage */ calibrate(file, "battery", "VbCalibrationFactor", READ_BATT_VOLTAGE_COMMAND); } else if (strcmp(argv[1], "ce") == 0) { /* Calibrate external voltage */ calibrate(file, "external", "VeCalibrationFactor", READ_EXT_VOLTAGE_COMMAND); } else if (strcmp(argv[1], "cc") == 0) { /* Calibrate charge voltage */ calibrate(file, "charge", "VcCalibrationFactor", READ_CHARGER_VOLTAGE_COMMAND); } else if (strcmp(argv[1], "t") == 0) { /* Set external supply threshold voltage */ set_threshold(file, atof(argv[2])); } if (strcmp(argv[1], "e") == 0) { /* Report error code and reset error state */ report_error_code(file); } close(file); exit(EXIT_SUCCESS);}void show_usage(void){ printf("\nOpen Automaton Project Power Management Module utility - " "(oap-power) version %s\nCopyright (C) 2003 Dafydd Walters\n\n" "oap-power comes with ABSOLUTYELY NO WARRANTY; for details " "see the\nGNU General Public License at http://www.gnu.org" "/licenses/gpl.html\n\n", VERSION); printf("Usage: oap-power <command>\n" "Commands:\n" " r Report voltages and state of PMM\n" " cb Calibrate battery voltage\n" " ce Calibrate external power supply voltage\n" " cc Calibrate charge voltage\n" " t <volts> Set good external voltage threshold (11.7 " "recommended)\n" " e Report error code and reset error condition\n" "\n");}void report_error_code(int file){ __s32 result; /* Read error code */ result = oap_i2c_smbus_read_byte_data(file, READ_ERROR_CODE_COMMAND); if (result < 0) { fprintf(stderr, "Error reading error code\n"); close(file); exit(EXIT_FAILURE); } printf("Error code: %d\n", result);}void set_threshold(int file, double volts){ __s32 result; __u8 threshold; threshold = volts / ve_cal_factor; /* Set external power good threshold */ result = oap_i2c_smbus_write_byte_data(file, WRITE_GOOD_EXT_THRESHOLD_COMMAND, threshold); if (result < 0) { fprintf(stderr, "Error setting external power good threshold\n"); close(file); exit(EXIT_FAILURE); }}void calibrate(int file, char* name, char* config_setting, int smbus_command){ config_t conf; __s32 result; double voltage; double cal_factor; char s_cal_factor[20]; printf("Apply known %s voltage. What is voltage? ", name); scanf("%lf", &voltage); /* Read level */ result = oap_i2c_smbus_read_byte_data(file, smbus_command); if (result < 0) { fprintf(stderr, "Error reading %s voltage\n", name); close(file); exit(EXIT_FAILURE); } cal_factor = voltage / result; sprintf(s_cal_factor, "%lf", cal_factor); /* Open configuration file */ if (!config_open(&conf, CONFIG_FILENAME, C_WRITE)) { fprintf(stderr, "Unable to open configuration file %s for writing\n", CONFIG_FILENAME); close(file); exit(EXIT_FAILURE); } /* Write configuration setting */ config_erase(&conf, "Power Management Module", config_setting); if (!config_write(&conf, "Power Management Module", config_setting, s_cal_factor)) { fprintf(stderr, "Unable to write %s to [Power Management Module] section of " "configuration file\n", config_setting); config_close(&conf); close(file); exit(EXIT_FAILURE); } /* Close configuration file */ config_close(&conf);}void report_state(int file){ __s32 result; /* Read battery voltage */ result = oap_i2c_smbus_read_byte_data(file, READ_BATT_VOLTAGE_COMMAND); if (result < 0) { fprintf(stderr, "Error reading battery voltage\n"); close(file); exit(EXIT_FAILURE); } printf("Raw battery level: %d (ADC bit counts)\n", result); printf("Calibrated battery level: %.1lf volts\n", result * vb_cal_factor); /* Read external supply voltage */ result = oap_i2c_smbus_read_byte_data(file, READ_EXT_VOLTAGE_COMMAND); if (result < 0) { fprintf(stderr, "Error reading external supply voltage\n"); close(file); exit(EXIT_FAILURE); } printf("External power supply level: %d (ADC bit counts)\n", result); printf("Calibrated external power supply level: %.1lf volts\n", result * ve_cal_factor); /* Read charger voltage */ result = oap_i2c_smbus_read_byte_data(file, READ_CHARGER_VOLTAGE_COMMAND); if (result < 0) { fprintf(stderr, "Error reading charger voltage\n"); close(file); exit(EXIT_FAILURE); } printf("Charger output level: %d (ADC bit counts)\n", result); printf("Calibrated charger output level: %.1lf volts\n", result * vc_cal_factor); /* Read good external supply threshold voltage */ result = oap_i2c_smbus_read_byte_data(file, READ_GOOD_EXT_THRESHOLD_COMMAND); if (result < 0) { fprintf(stderr, "Error reading good external supply threshold " "voltage\n"); close(file); exit(EXIT_FAILURE); } printf("Good external power supply threhsold level: %d (ADC bit counts)\n", result); printf("Calibrated good external power supply threshold level: %.1lf " "volts\n", result * ve_cal_factor); /* Read state */ result = oap_i2c_smbus_read_byte_data(file, READ_STATE_COMMAND); if (result < 0) { fprintf(stderr, "Error reading state\n"); close(file); exit(EXIT_FAILURE); } printf("State: %d\n", result); /* Read switches */ result = oap_i2c_smbus_read_byte_data(file, READ_SWITCHES_COMMAND); if (result < 0) { fprintf(stderr, "Error reading switches\n"); close(file); exit(EXIT_FAILURE); } printf("Microswitch 1 closed: %c\n", (result & 0x01) ? 'N' : 'Y'); printf("Microswitch 2 closed: %c\n", (result & 0x40) ? 'N' : 'Y'); printf("Microswitch 3 closed: %c\n", (result & 0x80) ? 'N' : 'Y');}int check_params(int argc, char* argv[]){ if ((argc == 2) && (strcmp(argv[1], "r") == 0)) return 1; if ((argc == 2) && (strcmp(argv[1], "cb") == 0)) return 1; if ((argc == 2) && (strcmp(argv[1], "ce") == 0)) return 1; if ((argc == 2) && (strcmp(argv[1], "cc") == 0)) return 1; if ((argc == 3) && (strcmp(argv[1], "t") == 0)) return 1; if ((argc == 2) && (strcmp(argv[1], "e") == 0)) return 1; return 0;}void read_config(){ config_t conf; char s_vb_cal_factor[20]; char s_ve_cal_factor[20]; char s_vc_cal_factor[20]; char s_device_address[4]; /* Open configuration file */ if (!config_open(&conf, CONFIG_FILENAME, C_READ)) { fprintf(stderr, "Unable to open configuration file %s for reading\n", CONFIG_FILENAME); exit(EXIT_FAILURE); } /* Read configuration settings */ if (!config_read(&conf, "I2C", "DeviceName", filename, 100)) { fprintf(stderr, "Unable to read DeviceName from [I2C] section of configuration file\n"); config_close(&conf); exit(EXIT_FAILURE); } if (!config_read(&conf, "Power Management Module", "I2CAddress", s_device_address, 4)) { fprintf(stderr, "Unable to read I2CAddress from [Power Management Module] section of\n" "configuration file.\n"); config_close(&conf); exit(EXIT_FAILURE); } device_address = atoi(s_device_address); if (!config_read(&conf, "Power Management Module", "VbCalibrationFactor", s_vb_cal_factor, 20)) { fprintf(stderr, "Unable to read VbCalibrationFactor from [Power Management Module] " "section of\nconfiguration file.\n"); config_close(&conf); exit(EXIT_FAILURE); } vb_cal_factor = atof(s_vb_cal_factor); if (!config_read(&conf, "Power Management Module", "VeCalibrationFactor", s_ve_cal_factor, 20)) { fprintf(stderr, "Unable to read VeCalibrationFactor from [Power Management Module] " "section of\nconfiguration file.\n"); config_close(&conf); exit(EXIT_FAILURE); } ve_cal_factor = atof(s_ve_cal_factor); if (!config_read(&conf, "Power Management Module", "VcCalibrationFactor", s_vc_cal_factor, 20)) { fprintf(stderr, "Unable to read VcCalibrationFactor from [Power Management Module] " "section of\nconfiguration file.\n"); config_close(&conf); exit(EXIT_FAILURE); } vc_cal_factor = atof(s_vc_cal_factor); /* Close configuration file */ config_close(&conf);}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -