BQ27510 电量计的校准 的 C语言实现
根据TI官方MSP430平台移植修改过来的,在Omap37xx(wince)平台测试,理论上和平台无关,伸手党,赶紧复制粘贴代码吧。如果这篇文章帮助了你,给了好评也无妨。
- /*
- ================================================================================
- * Texas Instruments OMAP(TM) Platform Software
- * (c) Copyright Texas Instruments, Incorporated. All Rights Reserved.
- *
- * Use of this software is controlled by the terms and conditions found
- * in the license agreement under which this software has been supplied.
- *
- ================================================================================
- */
- //
- // File: bqtool.c
- //
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <i2cproxy.h>
- #include "bq27500.h"
- #define CMD_MAX_DATA_SIZE 512
- #define MAX_LINE_LEN ((CMD_MAX_DATA_SIZE + 4) * 3)
- #define CMD_RETRY_DELAY 100 /* in ms */
- #define RETRY_LIMIT 3
- typedef enum {
- CMD_INVALID = 0,
- CMD_R, /* Read */
- CMD_W, /* Write */
- CMD_C, /* Compare */
- CMD_X, /* Delay */
- } cmd_type_t;
- /*
- * DO NOT change the order of fields - particularly reg
- * should be immediately followed by data
- */
- typedef struct {
- cmd_type_t cmd_type;
- unsigned char addr;
- unsigned char reg;
- union {
- unsigned char bytes[CMD_MAX_DATA_SIZE + 1];
- UINT delay;
- } data;
- unsigned char data_len;
- UINT32 line_num;
- CHAR *line;
- }cmd_t;
- static UINT32 line_num;
- /* Parse S into tokens separated by characters in DELIM.
- If S is NULL, the saved pointer in SAVE_PTR is used as
- the next starting point. For example:
- char s[] = "-abc-=-def";
- char *sp;
- x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
- x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
- x = strtok_r(NULL, "=", &sp); // x = NULL
- // s = "abc/0-def/0"
- */
- static char *strtok_r(char *s, const char *delim, char **save_ptr) {
- char *token;
- if (s == NULL) s = *save_ptr;
- /* Scan leading delimiters. */
- s += strspn(s, delim);
- if (*s == '\0')
- return NULL;
- /* Find the end of the token. */
- token = s;
- s = strpbrk(token, delim);
- if (s == NULL)
- /* This token finishes the string. */
- *save_ptr = strchr(token, '\0');
- else {
- /* Terminate the token and make *SAVE_PTR point past it. */
- *s = '\0';
- *save_ptr = s + 1;
- }
- return token;
- }
- static char *strtok(char *s, const char *delim)
- {
- static char *last;
- return strtok_r(s, delim, &last);
- }
- //写I2C,根据自己平台修改相关实现
- static BOOL i2c_write(HANDLE hI2C, cmd_t *cmd)
- {
- int ret;
- DWORD udwTemp;
- if(hI2C)
- {
- udwTemp=cmd->addr >>1;
- DeviceIoControl(hI2C, IOCTL_I2C_SET_SLAVE_ADDRESS, &udwTemp, sizeof(udwTemp), NULL, 0, NULL, NULL);
- SetFilePointer(hI2C,LOWORD(cmd->reg),NULL,FILE_BEGIN);
- if(!WriteFile(hI2C, cmd->data.bytes, cmd->data_len, &udwTemp, NULL))
- {
- RETAILMSG(1,(TEXT("ERROR: i2c_write: I2c WriteFile failed.\r\n")));
- }
- }
- return TRUE;
- }
- //读I2C,根据自己平台修改相关实现
- static BOOL i2c_Read(HANDLE hI2C, cmd_t *cmd)
- {
- int ret;
- DWORD udwTemp;
- if(hI2C)
- {
- udwTemp=cmd->addr >>1;
- DeviceIoControl(hI2C, IOCTL_I2C_SET_SLAVE_ADDRESS, &udwTemp, sizeof(udwTemp), NULL, 0, NULL, NULL);
- SetFilePointer(hI2C,LOWORD(cmd->reg),NULL,FILE_BEGIN);
- if(!ReadFile(hI2C, cmd->data.bytes, cmd->data_len,&udwTemp, NULL))
- {
- RETAILMSG(1,(TEXT("ERROR: i2c_Read: I2c ReadFile failed.\r\n")));
- return FALSE;
- }else
- {
- cmd->data_len=udwTemp;
- }
- }
- return TRUE;
- }
- static BOOL do_exec_cmd(HANDLE i2c_file, cmd_t *cmd)
- {
- unsigned char tmp_buf[CMD_MAX_DATA_SIZE];
- int j=0;
- switch (cmd->cmd_type) {
- case CMD_R:
- return i2c_Read(i2c_file, cmd);
- case CMD_W:
- return i2c_write(i2c_file, cmd);
- case CMD_C:
- memcpy(tmp_buf, cmd->data.bytes, cmd->data_len);
- if (!i2c_Read(i2c_file, cmd))
- return FALSE;
- if (memcmp(tmp_buf, cmd->data.bytes, cmd->data_len)) {
- RETAILMSG(1, (TEXT("Command C failed at line %d\r\n"),cmd->line_num));
- return FALSE;
- }
- return TRUE;
- case CMD_X:
- Sleep(cmd->data.delay);
- return TRUE;
- default:
- RETAILMSG(1, (TEXT("Unsupported command at line %d\r\n"),cmd->line_num));
- return FALSE;
- }
- }
- static BOOL execute_cmd(HANDLE i2c_file, cmd_t *cmd)
- {
- int i = 1,j=0;
- BOOL ret;
- RETAILMSG(0,(TEXT("cmd:cmd_type=%d;addr=%02x,reg=%02x,cmd->data_len=%d,"),cmd->cmd_type,cmd->addr,cmd->reg,cmd->data_len));
- #if 0 //only for debug TODO:delete
- RETAILMSG(1,(TEXT("line=%d:"),cmd->line_num));
- for(j=0;j<cmd->data_len;j++)
- {
- RETAILMSG(1,(TEXT("%02x "),cmd->data.bytes[j]));
- }
- RETAILMSG(1,(TEXT("\r\n")));
- #endif
- ret = do_exec_cmd(i2c_file, cmd);
- //if execute failed,retry three times
- while (!ret && i < RETRY_LIMIT) {
- Sleep(CMD_RETRY_DELAY);
- ret = do_exec_cmd(i2c_file, cmd);
- i++;
- }
- if (!ret) {
- RETAILMSG(1, (TEXT("Command execution failed at line %d addr=0x%02x reg=0x%02x\r\n"),cmd->line_num, cmd->addr, cmd->reg));
- }
- return ret;
- }
- static BOOL get_delay(UINT *delay)
- {
- char *tok;
- UINT temp;
- tok = strtok(NULL, " ");
- if (!tok)
- return FALSE; /*end of line or file */
- if (1 != sscanf(tok, "%u", &temp)) {
- RETAILMSG(1, (TEXT("Syntax error while parsing delay at line %d\r\n"),line_num));
- return FALSE; /* syntax error */
- }
- *delay = (UINT)temp;
- return TRUE;
- }
- /*
- * Returns:
- * 0: success
- * 1: EOF
- * -1: Parse error
- */
- static int get_byte(unsigned char *byte)
- {
- char *tok;
- unsigned char temp;
- tok = strtok(NULL, " ");
- if (!tok)
- return 1; /*end of line or file */
- if ((strlen(tok) != 2) || (sscanf(tok, "%2x", &temp) != 1)) {
- RETAILMSG(1, (TEXT("Syntax error at line %d\r\n"), line_num));
- return -1; /* syntax error */
- }
- *byte = (unsigned char)temp;
- return 0; /* success */
- }
- static BOOL get_addr_n_reg(cmd_t *cmd)
- {
- if (get_byte(&cmd->addr))
- return FALSE;
- if (get_byte(&cmd->reg))
- return FALSE;
- return TRUE;
- }
- static BOOL get_data_bytes(cmd_t *cmd)
- {
- int ret, i = 0;
- cmd->data_len = 0;
- do {
- ret = get_byte(&cmd->data.bytes[i++]);
- } while ((ret == 0) && (i <= CMD_MAX_DATA_SIZE));
- if (ret == 0) {
- RETAILMSG(1, (TEXT("More than allowed number of data bytes at line %d, data_len %d, i %d\r\n"), cmd->line_num,cmd->data_len, i));
- return FALSE;
- }
- cmd->data_len = i - 1;
- return TRUE;
- }
- static BOOL get_line(FILE *bqfs_file, char **buffer)
- {
- int c;
- int i = 0;
- BOOL ret = TRUE;
- char *buf;
- buf = malloc(MAX_LINE_LEN);
- line_num++;
- while (1) {
- c = fgetc(bqfs_file);
- if (feof(bqfs_file)) {
- break;
- } else if (ferror(bqfs_file)) {
- RETAILMSG(1, (TEXT("File read error\r\n")));
- ret = FALSE;
- break;
- }
- if (((c == '\r') || (c == '\n') || (c == '\t')
- || (c == ' ')) && (i == 0)) {
- /*
- * Skip leading white space, if any, at the beginning
- * of the line because this interferes with strtok
- */
- RETAILMSG(1, (TEXT("Leading whitespace at line %d\r\n"),line_num));
- if (c == '\n')
- line_num++;
- continue; /* blank line, let's continue */
- } else if (c == '\n') {
- /* We've reached end of line */
- break;
- }
- buf[i++] = c;
- if (i == MAX_LINE_LEN) {
- /*
- * Re-allocate in case the line is longer than
- * expected
- */
- buf = realloc(buf, MAX_LINE_LEN * 2);
- RETAILMSG(1, (TEXT("Line %d longer than expected,reallocating..\r\n"), line_num));
- } else if (i == MAX_LINE_LEN * 2) {
- /*
- * The line is already twice the expected maximum length
- * - maybe the bqfs/dffs needs to be fixed
- */
- RETAILMSG(1, (TEXT("Line %d too long, abort parsing..\r\n"),line_num));
- ret = FALSE;
- break;
- }
- }
- *buffer = buf;
- buf[i] = '\0';
- if (i < 1)
- ret = FALSE;
- return ret;
- }
- static BOOL get_cmd(FILE *bqfs_file, cmd_t *cmd)
- {
- char *res;
- char *tok;
- char *buf = NULL;
- BOOL ret;
- while ((ret = get_line(bqfs_file, &buf))) {
- if (buf[0] == ';') {
- /*
- * Comment line - ignore it and get the
- * next line
- */
- RETAILMSG(0, (TEXT("Comment line,line_num=%d\r\n"),line_num));
- free(buf);
- } else {
- break;
- }
- }
- if (!ret)
- goto error;
- cmd->line_num = line_num;
- tok = strtok(buf, ":");
- if (!tok || (strlen(tok) != 1)) {
- RETAILMSG(1, (TEXT("Error parsing command at line %d\r\n"),line_num));
- goto error;
- }
- switch (tok[0]) {
- case 'R':
- case 'r':
- cmd->cmd_type = CMD_R;
- if (!get_addr_n_reg(cmd))
- goto error;
- break;
- case 'W':
- case 'w':
- cmd->cmd_type = CMD_W;
- if (!get_addr_n_reg(cmd))
- goto error;
- if (!get_data_bytes(cmd))
- goto error;
- break;
- case 'C':
- case 'c':
- cmd->cmd_type = CMD_C;
- if (!get_addr_n_reg(cmd))
- goto error;
- if (!get_data_bytes(cmd))
- goto error;
- break;
- case 'X':
- case 'x':
- cmd->cmd_type = CMD_X;
- cmd->data_len = 1; //only one data
- if (!get_delay(&cmd->data.delay))
- goto error;
- break;
- default:
- RETAILMSG(1, (TEXT("No command or unexpected command at line %d.\r\n"),line_num));
- goto error;
- }
- if(buf)
- {
- free(buf);
- }
- return TRUE;
- error:
- RETAILMSG(1, (TEXT("get_line error,line_num=%d\r\n"),line_num));
- cmd->cmd_type = CMD_INVALID;
- free(buf);
- return FALSE;
- }
- //Param:char *fname
- // File to flash BQ27510 generate by TI's engineer.
- // for example:bq27510G3.bqfs(with G2 update G3 firmware)
- int bqfs_flash(char *fname)
- {
- FILE *bqfs_file = NULL;
- cmd_t *cmd = NULL;
- int ret = 0;
- DWORD udwTemp;
- HANDLE hI2C=NULL;
- RETAILMSG(0,(TEXT("bqfs_flush beging...\r\n")));
- bqfs_file = fopen(fname, "r");
- if (!bqfs_file) {
- RETAILMSG(1,(TEXT("bqfs_flush fopen failed.\r\n")));
- ret = -1;
- goto end;
- }
- hI2C=CreateFile(BATT_I2C_PORT, GENERIC_READ | GENERIC_WRITE, 0,NULL, OPEN_EXISTING, 0, NULL);
- if(!hI2C)
- {
- RETAILMSG(1,(TEXT("bqfs_flash: I2c CreateFile failed.\r\n")));
- ret = -1;
- goto end;
- }
- //I2C相关配置寻址,根据自己平台修改相关实现
- //set slave address
- udwTemp=I2CSLAVEADDR;
- DeviceIoControl(hI2C, IOCTL_I2C_SET_SLAVE_ADDRESS, &udwTemp, sizeof(udwTemp), NULL, 0, NULL, NULL);
- //set i2c work mode
- udwTemp=I2C_SUBADDRESS_MODE_8;
- DeviceIoControl(hI2C, IOCTL_I2C_SET_SUBADDRESS_MODE, &udwTemp, sizeof(udwTemp), NULL, 0, NULL, NULL);
- //set i2c transfer speed
- udwTemp=SLOWSPEED_MODE;
- DeviceIoControl(hI2C, IOCTL_I2C_SET_BAUD_INDEX, &udwTemp, sizeof(udwTemp), NULL, 0, NULL, NULL);
- cmd = malloc(sizeof(cmd_t));
- if(!cmd)
- {
- RETAILMSG(1, (TEXT("bqfs_flash malloc failed.\r\n")));
- ret=-1;
- goto end;
- }
- while (get_cmd(bqfs_file, cmd) && execute_cmd(hI2C, cmd));
- if (feof(bqfs_file)) {
- RETAILMSG(1, (TEXT("programmed successfully!\r\n")));
- ret = 0;
- } else {
- RETAILMSG(1, (TEXT("programming failed!!\r\n")));
- ret = -1;
- }
- end:
- if (cmd)
- free(cmd);
- if(hI2C)
- {
- CloseHandle(hI2C);
- hI2C=NULL;
- }
- if (bqfs_file)
- fclose(bqfs_file);
- return ret;
- }
BQ27510 电量计的校准 的 C语言实现的更多相关文章
- C/C++学习----使用C语言代替cmd命令、cmd命令大全
[开发环境] 物理机版本:Win 7 旗舰版(64位) IDE版本:Visual Studio 2013简体中文旗舰版(cn_visual_studio_ultimate_2013_with_upda ...
- 【转】android电池(四):电池 电量计(MAX17040)驱动分析篇
关键词:android 电池 电量计 MAX17040 任务初始化宏 power_supply 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台: ...
- C语言学习及应用笔记之七:C语言中的回调函数及使用方式
我们在使用C语言实现相对复杂的软件开发时,经常会碰到使用回调函数的问题.但是回调函数的理解和使用却不是一件简单的事,在本篇我们根据我们个人的理解和应用经验对回调函数做简要的分析. 1.什么是回调函数 ...
- android电池(四):电池 电量计(MAX17040)驱动分析篇【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/8969369 电池电量计,库仑计,用max17040这颗电量IC去计量电池电量,这种方法 ...
- Go语言经典库使用分析——高性能可扩展 HTTP 路由 httprouter(转载)
转载自 飞雪无情的博客 Go语言(golang)的一个很大的优势,就是很容易的开发出网络后台服务,而且性能快,效率高.在开发后端HTTP网络应用服务的时候,我们需要处理很多HTTP的请求访问,比如常见 ...
- C语言 · 高精度加法
问题描述 输入两个整数a和b,输出这两个整数的和.a和b都不超过100位. 算法描述 由于a和b都比较大,所以不能直接使用语言中的标准数据类型来存储.对于这种问题,一般使用数组来处理. 定义一个数组A ...
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
- C语言 · Anagrams问题
问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...
随机推荐
- rails创建项目,部署,测试流程(rails5.0+ruby2.3.1)
rails new test_app --skip-test-unit 不生成默认的test,稍后用rspeccd test_app 修改Gemfile(大部分为自动生成) source 'https ...
- windows下pip升级到8.1.2
升级pip只要切换到easy_install-3.5目录下: easy_install-3.5 pip==8.1.2
- emulator-arm.exe 已停止工作、 emulator-x86 已停止工作
问题描述: emulator-arm.exe 已停止工作. emulator-x86 已停止工作.AVD模拟器启动一直黑屏.AVD模拟器启动一直显示andorid界面 解决方法: 1. sdk的安 ...
- 使用php模拟post的几种方法
<?phpfunction file_get_contents_post($url,$post){$options=array('http'=>array('method'=>'PO ...
- RobotFrameWork接口报文测试-----(二)demo的升级版
在上一篇,简单的demo实现了讲xml的数据发送服务器端并取得recvi_buf,然后进行了简单的解析的操作.现在就要解决之前提过的2个问题: 1. 步骤这么多,难道每写一个脚本都要重复一次么? 2. ...
- css在IE和Firefox下的兼容性
1.div的垂直居中问题 vertical-align:middle,将行距增加到和整个div高度一样,加line-height:200px;然后插入文字就垂直居中了.缺点是要控制内容不要换行. 2. ...
- knockout之各种数据绑定方法:text、attr、visible、html、css、style绑定
http://knockoutjs.com/documentation/attr-binding.html(Knockout官网文档) 1.text绑定 目的:text 绑定到DOM元素上,使得该元素 ...
- Swift 注释
注释就是对代码的解释和说明.目的是为了让别人和自己很容易看懂.为了让别人一看就知道这段代码是做什么用的. 正确的程序注释一般包括序言性注释和功能性注释.序言性注释的主要内容包括模块的接口.数据的描述和 ...
- DOM4J解析xml案例
- 好用的 diskpart 命令,操作磁盘分区,并创建 EFI 引导分区
进入 win8 pe, 或在安装 win8 选择安装磁盘分区时按 shift+F10,都是可能用到 diskpart 去操作的时候. 进入cmd后, diskpart 进入 list disk---- ...