int pin_mapping[5] = { -1, -1, -1, -1, -1 };
int pin_reverse[5] = { 0, 0, 0, 0, 0 };
+#define MAXLEN 100
+#define MAXSETTINGS 100
+
static struct pins_backend *backend = NULL;
extern struct pins_backend serial_raw, serial, parallel;
static struct pins_backend *pins_backends[] = {
NULL
};
+static struct confsetting {
+ char name[MAXLEN];
+ char value[MAXLEN];
+} confsettings[MAXSETTINGS];
+
+static int num_confsettings = 0;
+
int get_pin_id(char *name)
{
int i;
return -1;
}
+int pins_read_config_file(char *name)
+{
+ char bit[10], pin[10];
+ int line = 0;
+ FILE *fd = fopen(name, "r");
+
+ if(!fd) {
+ fprintf(stderr, "Can't open %s!\n"
+ "Type 'man 5 at89prog' for information about the RC file format\n", name);
+ perror("fopen");
+ return 1;
+ }
+
+ while(!feof(fd))
+ {
+ line++;
+ if(fscanf(fd, "%100s %100s\n", bit, pin) < 2) {
+ fprintf(stderr, "Invalid line %d in %s, ignoring\n", line, name);
+ continue;
+ }
+
+ if(!strcasecmp(bit, "type")) pins_set_backend(pin);
+ else if(!strcasecmp(bit, "port")) pins_set_location(pin);
+ else {
+ if(num_confsettings > MAXSETTINGS) {
+ fprintf(stderr, "Maximum number of %d configuration settings reached\n", num_confsettings);
+ return -1;
+ }
+ strncpy(confsettings[num_confsettings].name, bit, MAXLEN);
+ strncpy(confsettings[num_confsettings].value, pin, MAXLEN);
+ num_confsettings++;
+ }
+ }
+
+ fclose(fd);
+ return 0;
+}
+
+int pins_set_backend(char *name)
+{
+ int i;
+ for(i = 0; pins_backends[i]; i++) {
+ if(!strcmp(pins_backends[i]->name, name)) {
+ backend = pins_backends[i];
+ return 0;
+ }
+ }
+
+ fprintf(stderr, "No such port type '%s'\n", name);
+ return -1;
+}
+
+int pins_set_location(char *loc)
+{
+ location = strdup(loc);
+ return 0;
+}
+
int SetPinVariable(char *name, char *value)
{
int i, j;
return -1;
}
+int pins_init()
+{
+ int i;
+ if(!backend) backend = pins_backends[0];
+ for(i = 0; i < num_confsettings; i++) {
+ SetPinVariable(confsettings[i].name, confsettings[i].value);
+ }
+
+ return backend->init(location);
+}
+
+
+
void SetMOSI() {
if(pin_mapping[PIN_MOSI] == -1) return;
if(pin_reverse[PIN_MOSI]) backend->clear(pin_mapping[PIN_MOSI]);
return backend->get(pin_mapping[PIN_MISO]);
}
-int LoadPinBackend(char *name)
-{
- int i;
- for(i = 0; pins_backends[i]; i++) {
- if(!strcmp(pins_backends[i]->name, name)) {
- backend = pins_backends[i];
- return 0;
- }
- }
- fprintf(stderr, "No such port type '%s'\n", name);
- return -1;
-}
-
-int BackendInit()
-{
- return backend->init(location);
-}
-
-int ClosePinBackend()
+int pins_fini()
{
if(backend->close) return backend->close();
return 0;
char progress = 0;
-char *backendname = "serial";
char *port = NULL;
void usage(poptContext pc)
void quit(int s)
{
deactivate();
- ClosePinBackend();
+ pins_fini();
fprintf(stderr, "Received signal, exiting...\n");
exit(0);
}
}
-int read_pin_configuration(char *name)
-{
- char bit[10], pin[10];
- int line = 0;
- FILE *fd = fopen(name, "r");
-
- if(!fd) {
- fprintf(stderr, "Can't open %s!\n"
- "Type 'man 5 at89prog' for information about the RC file format\n", name);
- perror("fopen");
- return 1;
- }
-
- while(!feof(fd))
- {
- line++;
- if(fscanf(fd, "%4s %6s\n", bit, pin) < 2) {
- fprintf(stderr, "Invalid line %d in %s, ignoring\n", line, name);
- continue;
- }
-
- switch(SetPinVariable(bit, pin)) {
- case -1:
- fprintf(stderr, "Invalid configuration variable '%s' at line %d in %s, ignoring\n", bit, line, name);
- continue;
- if(!fd) {
- fprintf(stderr, "Can't open %s!\n"
- "Type 'man 5 at89prog' for information about the RC file format\n", name);
- perror("fopen");
- return 1;
- }
-
- while(!feof(fd))
- {
- line++;
- if(fscanf(fd, "%4s %6s\n", bit, pin) < 2) {
- fprintf(stderr, "Invalid line %d in %s, ignoring\n", line, name);
- continue;
- }
-
- switch(SetPinVariable(bit, pin)) {
- case -1:
- fprintf(stderr, "Invalid configuration variable '%s' at line %d in %s, ignoring\n", bit, line, name);
- continue;
-
- case -2:
- fprintf(stderr, "Invalid value '%s' for configuration variable '%s' at line %d in %s, ignoring\n", pin, bit, line, name);
- continue;
-
- default:
- break;
- }
- }
-
- fclose(fd);
- return 0;
-}
-
int main(int argc, const char **argv)
{
int datamem = 0, codemem = 0, verbose = 0, do_verify = 0, ignore_chk = 0;
char *format = "auto";
char *rcfile = NULL;
char c, print_usage = 1;
+ char *backendname = NULL, *location = NULL;
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "data-memory", 'd', POPT_ARG_NONE, &datamem, 0, "Write specified file to data memory" },
}
if(rcfile) {
- if(read_pin_configuration(rcfile) != 0) return 1;
+ if(pins_read_config_file(rcfile) != 0) return 1;
}
- if(LoadPinBackend(backendname) != 0) return 1;
- if(port)SetPinVariable(NULL, port);
- if(BackendInit() != 0) return 1;
+ if(backendname && pins_set_backend(backendname) != 0) return 1;
+ if(location && pins_set_location(location) != 0) return 1;
+ if(pins_init() != 0) return 1;
signal(SIGINT, quit);
signal(SIGSEGV, quit);
return 1;
}
address = strtol(poptGetArg(pc), NULL, 16);
+
+ if(datamem) printf("%x\n", readdata(address));
+ else printf("%x\n", readcode(address));
+ } else if(!strcmp(poptPeekArg(pc), "version")) {
+ fprintf(stderr, "at89prog - a AT89S8252 programmer using SPI\n");
+ fprintf(stderr, " Version "VERSION"\n");
+ fprintf(stderr, " (C) 2003 Jelmer Vernooij <jelmer@samba.org>\n");
+ fprintf(stderr, " Published under the GNU GPL\n");
+ return 0;
+ } else {
+ fprintf(stderr, "Unknown command %s\n", poptGetArg(pc));
+ usage(pc);
+ }
+
+ deactivate();
+ poptFreeContext(pc);
+
+ return 0;
+}