C language implementation of BQ27510 gauge calibration, bq27510 Gauge

Source: Internet
Author: User
Tags parse error strtok

C language implementation of BQ27510 gauge calibration, bq27510 Gauge

Tested On the Omap37xx (wince) Platform Based on TI's official MSP430 platform, which is theoretically irrelevant to the platform and can be used.


/* ===================================================== ========================================================== = * Texas Instruments OMAP (TM) platform Software * (c) Copyright Texas Instruments, inmarshated. 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 criteria # define MAX_LINE_LEN (pai_max_data_size + 4) * 3) # define criteria/* in MS */# define RETRY_LIMIT3typedef enum {pai_invalid = 0, CMD_R,/* Read */CMD_W,/* Write */CMD_C,/* Compare */CMD_X,/* Delay */} pai_t Ype_t;/** do not change the order of fields-fielularly reg * shocould be immediately followed by data */typedef struct {pai_type_t pai_type; unsigned char addr; unsigned char reg; union {unsigned char bytes [pai_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, th E 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) ;}// write I2C. Implement static BOOL i2c_write (HANDLE hI2C, cmd_t * cmd) based on your own platform modifications) {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 ;}// read I2C, and implement static BOOL i2c_Read (HANDLE hI2C, cmd_t * cmd) based on your own platform modifications) {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 when _r: return i2c_Read (i2c_file, cmd); case when _w: return i2c_write (i2c_file, cmd); case C MD_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 when _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: deleteRETAILMSG (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"); # endifret = do_exec_cmd (i2c_file, cmd ); // if execute failed, retry three timeswhile (! Ret & I <RETRY_LIMIT) {Sleep (pai_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 ++; contin Ue;/* 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': cmd-> else _type = else _r; if (! Get_addr_n_reg (cmd) goto error; break; 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': cmd-> cmd_type = CMD_C; if (! Get_addr_n_reg (cmd) goto error; if (! Get_data_bytes (cmd) goto error; break; case 'X': cmd-> export _type = export _x; cmd-> data_len = 1; // only one dataif (! 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-> pai_type = pai_invalid; free (buf); return FALSE ;} // Param: char * fname // File to flash BQ27510 generate by TI's engineer. // for example: bq27510G3. bqfs (wit H G2 update G3 firmware) int bqfs_flash (char * fname) {FILE * bqfs_file = NULL; empty _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;} // address of I2C configuration, modify the implementation according to your own platform // set slave addressudwTemp = I2CSLAVEADDR; deviceIoControl (hI2C, temperature, & udwTemp, sizeof (udwTemp), NULL, 0, NULL); // set i2c work modeudwTemp = I2C_SUBADDRESS_MODE_8; DeviceIoControl (hI2C, temperature, temperature, & udwTemp, sizeof (udwTemp), NULL, 0, NULL, NULL ); // Set i2c transfer speedudwTemp = SLOWSPEED_MODE; DeviceIoControl (hI2C, IOCTL_I2C_SET_BAUD_INDEX, & udwTemp, sizeof (udwTemp), NULL, 0, NULL, NULL ); cmd = malloc (sizeof (Bytes _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 ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.