📄 cminifile.c
字号:
/* * Module: cmIniFile.c * * Description: This file contains all of the routines associated with * reading an .ini file upon startup */#include <stdio.h>#include <string.h>#include "cmAPI.h"#include "cmRegisterIO.h"static char initfilename[] = "cmAPI.ini"; /* default file name */extern g_debug;/* forward declarations */int parseCmd (FILE *fp, unsigned char *cmd_p, unsigned int *addr_p, unsigned int *data_p);#define CMD_WRITE 0#define CMD_READ 1/* * Function: cmIniFileRead * * Description: Read the ini initialization file * This function calls the Register I/O interface to set up * values. * * FORMAT: * r 05 Reads register 0x05 * w 05 32 Writes 0x32 to register 0x05 * * Each command is executed in the sequence provided. * * Input: * filename If null then just exit. Otherwise, read the * file. */int cmIniFileRead (char *filename){ FILE *fp; /* file pointer */ unsigned char command; /* 00 = write, 01 = read */ unsigned int addr; /* register address to read or write to */ unsigned int data; /* data to read or write */ int status = 0; /* - = error */ if (filename == NULL) { return status; } /* open the initialization file */ fp = fopen (filename, "r"); if (fp == NULL) { printf ("\nError opening file %s\n", filename); return -1; } /* read in command from file. Command :00 = write register, 01 = read register. Load the address and data variables. */ while (1) { /* parse command */ status = parseCmd (fp, &command, &addr, &data); if (status < 0) { break; } if (status == 1) { /* end of file */ if (g_debug > 0) printf ("\nDetected end of file \n"); break; } /* process command */ if (command == CMD_WRITE) { status = cmRegisterWrite (addr, data); if (status < 0) { printf ("\nError writing register\n"); break; } } else { status = cmRegisterRead (addr, &data); if (status < 0) { printf ("\nError reading register\n"); break; } } } fclose (fp); return status;}/* * parsecmd * * Assumes an ascii text file */int parseCmd (FILE *fp, unsigned char *cmd_p, unsigned int *addr_p, unsigned int *data_p){ int status = 0; int s = 0; unsigned char buf[4]; *addr_p = 0; *data_p = 0; /* read in one line. Note that reads only have two tokens, but we try to read 3. That is OK. We will just ignore the "data" variable value */ s = fscanf (fp, "%s", buf); if (s == EOF) { status = 1; /* end of file condition */ } else { if (strcmp (buf, "r") == 0) { /* read */ *cmd_p = CMD_READ; *data_p = 0; s = fscanf (fp, "%x", addr_p); /* read address */ if (s == EOF) { if (g_debug > 2) printf ("EOF detected\n"); *addr_p = 0; status = 1; } } else if (strcmp (buf, "w") == 0) { *cmd_p = CMD_WRITE; s = fscanf (fp, "%x", addr_p); /* read address */ if (s == EOF) { if (g_debug > 2) printf ("EOF detected\n"); *addr_p = 0; status = 1; } else { s = fscanf (fp, "%x", data_p); /* read data */ if (s == EOF) { if (g_debug > 2) printf ("EOF detected\n"); *addr_p = 0; *data_p = 0; status = 1; } } } else { printf ("Invalid command\n"); status = -1; } if (g_debug > 2) { printf ("\nCommand = %s, addr = %d, data = %d\n", buf, *addr_p, *data_p); } } return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -