static BoardConfig *GetDefaultConfiguration(void) { static BoardConfig *defaultConfig = NULL; if (!defaultConfig) { defaultConfig = NewBoardConfig(NULL, DEF_BOARD); SetConfigField(defaultConfig, "clkfreq", "80000000"); SetConfigField(defaultConfig, "clkmode", "XTAL1+PLL16X"); SetConfigField(defaultConfig, "baudrate", "115200"); SetConfigField(defaultConfig, "rxpin", "31"); SetConfigField(defaultConfig, "txpin", "30"); } return defaultConfig; }
bool LoadConfig(const char* file_name, config* cfg) { char* str = NULL; vector<OSL_LEXEM> lxm; bool ok = false; int lxm_count = 0; #ifdef _DEBUG_OSL WriteToLog(DEFAULT_OSL_LOG_NAME, "Loading config file: " + string(file_name)); WriteToLog(DEFAULT_OSL_LOG_NAME, "Input..."); #endif ok = osl_Input(file_name, &str); if(ok) { #ifdef _DEBUG_OSL WriteToLog(DEFAULT_OSL_LOG_NAME, "Input OK"); #endif } else { WriteToLog(DEFAULT_OSL_LOG_NAME, "Input FAILED"); return false; } #ifdef _DEBUG_OSL WriteToLog(DEFAULT_OSL_LOG_NAME, "Lexemize..."); #endif lxm_count = osl_Lexemize(str, &lxm); lxm_count > 0 ? ok = true : ok = false; if(ok) { #ifdef _DEBUG_OSL char buffer[64]; sprintf(buffer, "Lexemize OK (%u lexems token)", lxm_count); WriteToLog(DEFAULT_OSL_LOG_NAME, buffer); #endif delete[] str; } else { WriteToLog(DEFAULT_OSL_LOG_NAME, "Lexemize FAILED"); return false; } #ifdef _DEBUG_OSL osl_dDumpLexemStream(&lxm); #endif #ifdef _DEBUG_OSL WriteToLog(DEFAULT_OSL_LOG_NAME, "Applying config..."); #endif vector<OSL_LEXEM> sqm; for(vector<OSL_LEXEM>::iterator it = lxm.begin(); it != lxm.end(); it++) { if(!osl_IsSys((*it).token)) sqm.push_back((*it)); else OSL_CURRENT_LINE++; if(osl_IsSemicolon((*it).token)) { if(sqm.size() != 4) { WriteToLog(DEFAULT_OSL_LOG_NAME, "LoadConfig() error: invalid line"); } else { #ifdef _DEBUG_OSL osl_dDumpLexemStream(&sqm); #endif SetConfigField(cfg, sqm[0].token, sqm[2].token); sqm.clear(); } } } #ifdef _DEBUG_OSL WriteToLog(DEFAULT_OSL_LOG_NAME, "Config successfully loaded"); #endif return true; }
int main(int argc, char *argv[]) { char actualport[PATH_MAX]; char *infile = NULL, *p, *p2; int terminalMode = FALSE; BoardConfig *config, *configSettings; char *port, *board, *subtype, *value; System sys; int baud = 115200; int portFlags = 0; int flags = 0; int i; int terminalBaud = 0; int check_for_exit = 0; /* flag to terminal_mode to check for a certain sequence to indicate program exit */ int showPorts = FALSE; int showAll = TRUE; int pstMode = FALSE; /* make sure that the serial port gets closed on exit */ atexit(serial_done); /* just display a usage message if no arguments are supplied */ if (argc <= 1) Usage(); /* get the environment settings */ port = getenv("PROPELLER_LOAD_PORT"); board = getenv("PROPELLER_LOAD_BOARD"); /* setup a configuration to collect command line -D settings */ configSettings = NewBoardConfig(NULL, ""); /* get the arguments */ for(i = 1; i < argc; ++i) { /* handle switches */ if(argv[i][0] == '-') { switch(argv[i][1]) { case 'b': // select a target board if (argv[i][2]) board = &argv[i][2]; else if (++i < argc) board = argv[i]; else Usage(); break; case 'p': if(argv[i][2]) port = &argv[i][2]; else if(++i < argc) port = argv[i]; else Usage(); #if defined(CYGWIN) || defined(WIN32) || defined(LINUX) if (isdigit((int)port[0])) { #if defined(CYGWIN) || defined(WIN32) static char buf[10]; sprintf(buf, "COM%d", atoi(port)); port = buf; #endif #if defined(LINUX) static char buf[64]; sprintf(buf, "/dev/ttyUSB%d", atoi(port)); port = buf; #endif } #endif #if defined(MACOSX) if (port[0] != '/') { static char buf[64]; sprintf(buf, "/dev/cu.usbserial-%s", port); port = buf; } #endif break; case 'P': showPorts = TRUE; showAll = FALSE; break; case 'Q': showPorts = TRUE; showAll = TRUE; break; case 'e': flags |= LFLAG_WRITE_EEPROM; break; case 'l': flags |= LFLAG_WRITE_SDLOADER; break; case 'z': flags |= LFLAG_WRITE_SDCACHELOADER; break; case 'r': flags |= LFLAG_RUN; break; case 's': flags |= LFLAG_WRITE_BINARY; break; case 'x': flags |= LFLAG_WRITE_PEX; break; case 'T': pstMode = TRUE; // fall through case 't': terminalMode = TRUE; if (argv[i][2]) terminalBaud = atoi(&argv[i][2]); break; case 'q': check_for_exit = 1; break; case 'g': flags |= LFLAG_DEBUG; break; case 'D': if(argv[i][2]) p = &argv[i][2]; else if(++i < argc) p = argv[i]; else Usage(); if ((p2 = strchr(p, '=')) == NULL) Usage(); *p2++ = '\0'; SetConfigField(configSettings, p, p2); break; case 'I': if(argv[i][2]) p = &argv[i][2]; else if(++i < argc) p = argv[i]; else Usage(); xbAddPath(p); break; case 'v': portFlags = IFLAG_VERBOSE; break; case 'S': psetdelay(argv[i][2] ? atoi(&argv[i][2]) : 5); break; case 'f': flags |= LFLAG_WRITE_SDFILE; break; case '?': /* fall through */ default: Usage(); break; } } /* handle the input filename */ else { if (infile) Usage(); infile = argv[i]; } } /* make sure an input file was specified if needed */ if (infile && !flags) { printf("error: must specify -e, -r, or -s when an image file is specified\n"); return 1; } /* 1) look in the directory specified by the -I command line option (added above) 2) look in the directory where the elf file resides 3) look in the directory pointed to by the environment variable PROPELLER_ELF_LOAD 4) look in the directory where the loader executable resides if possible 5) look in /usr/local/propeller/propeller-load */ /* finish the include path */ if (infile) xbAddFilePath(infile); xbAddEnvironmentPath("PROPELLER_LOAD_PATH"); xbAddProgramPath(argv); #if defined(LINUX) || defined(MACOSX) || defined(CYGWIN) xbAddPath("/opt/parallax/propeller-load"); #endif sys.ops = &myOps; /* parse the board option */ if (board) { /* split the board type from the subtype */ if ((p = strchr(board, ':')) != NULL) { *p++ = '\0'; subtype = p; } /* no subtype */ else subtype = DEF_SUBTYPE; } else { board = DEF_BOARD; subtype = DEF_SUBTYPE; } /* setup for the selected board */ if (!(config = ParseConfigurationFile(&sys, board))) { printf("error: can't find board configuration '%s'\n", board); return 1; } /* select the subtype */ if (subtype) { if (!(config = GetConfigSubtype(config, subtype))) { printf("error: can't find board configuration subtype '%s'\n", subtype); return 1; } } /* override with any command line settings */ config = MergeConfigs(config, configSettings); /* use the baud rate from the configuration */ GetNumericConfigField(config, "baudrate", &baud); /* Use reset method from configuration */ if ((value = GetConfigField(config, "reset")) != NULL) { if (use_reset_method (value)) { printf("error: no reset type '%s'\n", value); return 1; } } else { use_reset_method ("dtr"); } /* check for being asked to show ports */ if (showPorts) { if (showAll) ShowPorts(PORT_PREFIX); else ShowConnectedPorts(PORT_PREFIX, baud, portFlags); return 0; } /* initialize the serial port */ if ((flags & NEED_PORT) != 0 || terminalMode) { int sts = InitPort(PORT_PREFIX, port, baud, portFlags, actualport); switch (sts) { case PLOAD_STATUS_OK: // port initialized successfully break; case PLOAD_STATUS_OPEN_FAILED: printf("error: opening serial port '%s'\n", port); perror("Error is "); return 1; case PLOAD_STATUS_NO_PROPELLER: if (port) printf("error: no propeller chip on port '%s'\n", port); else printf("error: can't find a port with a propeller chip\n"); return 1; } } /* load the image file */ if (infile) { if (flags & LFLAG_WRITE_SDFILE) WriteFileToSDCard(config, infile, NULL); else { if (!LoadImage(&sys, config, infile, flags)) { printf("error: load failed\n"); return 1; } } } /* check for loading the sd loader */ else if (flags & LFLAG_WRITE_SDLOADER) { if (!LoadSDLoader(&sys, config, "sd_loader.elf", flags)) { printf("error: load failed\n"); return 1; } } /* check for loading the sd cache loader */ else if (flags & LFLAG_WRITE_SDCACHELOADER) { if (!LoadSDCacheLoader(&sys, config, "sd_cache_loader.elf", flags)) { printf("error: load failed\n"); return 1; } } /* enter terminal mode if requested */ if (terminalMode) { printf("[ Entering terminal mode. Type ESC or Control-C to exit. ]\n"); fflush(stdout); if (terminalBaud && terminalBaud != baud) { serial_done(); serial_init(actualport, terminalBaud); } terminal_mode(check_for_exit, pstMode); } return 0; }
static BoardConfig *GetDefaultConfiguration(void) { static BoardConfig *defaultConfig = NULL; if (!defaultConfig) { defaultConfig = NewBoardConfig(NULL, DEF_BOARD); SetConfigField(defaultConfig, "clkfreq", "80000000"); SetConfigField(defaultConfig, "clkmode", "XTAL1+PLL16X"); SetConfigField(defaultConfig, "baudrate", "115200"); SetConfigField(defaultConfig, "loader-baud-rate", "115200"); SetConfigField(defaultConfig, "fast-loader-baud-rate", "921600"); SetConfigField(defaultConfig, "program-baud-rate", "115200"); SetConfigField(defaultConfig, "rxpin", "31"); SetConfigField(defaultConfig, "txpin", "30"); SetConfigField(defaultConfig, "sdspi-do", "22"); SetConfigField(defaultConfig, "sdspi-clk", "23"); SetConfigField(defaultConfig, "sdspi-di", "24"); SetConfigField(defaultConfig, "sdspi-cs", "25"); } return defaultConfig; }
/* ParseConfigurationFile - parse a configuration file */ BoardConfig *ParseConfigurationFile(const char *name) { char path[PATH_MAX]; BoardConfig *baseConfig, *config; const char *src; char *tag, *dst; LineBuf buf; FILE *fp; int ch; /* make a local copy of the name in lowercase */ src = name; dst = path; while ((*dst++ = tolower(*src++)) != '\0') ; /* check for a request for the default configuration */ if (strcmp(path, DEF_BOARD) == 0) return GetDefaultConfiguration(); /* make the configuration file name */ strcat(path, ".cfg"); /* open the configuration file */ if (!(fp = xbOpenFileInPath(path, "r"))) return NULL; /* create a new board configuration */ baseConfig = config = NewBoardConfig(GetDefaultConfiguration(), name); /* initialize the line number */ buf.lineNumber = 0; /* process each line in the configuration file */ while (fgets(buf.lineBuf, sizeof(buf.lineBuf), fp)) { char *p; int len; /* check for a comment at the end of the line */ if ((p = strchr(buf.lineBuf, '#')) != NULL) *p = '\0'; /* trim any trailing newline and spaces */ for (len = strlen(buf.lineBuf); len > 0; --len) if (!isspace(buf.lineBuf[len-1])) break; buf.lineBuf[len] = '\0'; /* initialize token parser */ buf.linePtr = buf.lineBuf; ++buf.lineNumber; /* look for the first token on the line */ switch (SkipSpaces(&buf)) { case '\0': /* blank line */ case '#': /* comment */ // ignore blank lines and comments break; case '[': /* configuration tag */ /* get the configuration name */ ++buf.linePtr; if (!(tag = NextToken(&buf, "]", &ch))) ParseError(&buf, "missing configuration tag"); if (ch != ']') { if (SkipSpaces(&buf) != ']') ParseError(&buf, "missing close bracket after configuration tag"); ++buf.linePtr; } if (SkipSpaces(&buf) != '\0') ParseError(&buf, "missing end of line"); /* add a new board configuration */ config = NewBoardConfig(baseConfig, tag); break; default: /* tag:value pair */ /* get the tag */ if (!(tag = NextToken(&buf, ":", &ch))) ParseError(&buf, "missing tag"); /* check for the colon separator */ if (ch != ':') { if (SkipSpaces(&buf) != ':') ParseError(&buf, "missing colon"); ++buf.linePtr; } /* skip leading spaces before the value */ SkipSpaces(&buf); /* set the configuration value */ SetConfigField(config, tag, buf.linePtr); break; } } /* close the board configuration file */ fclose(fp); /* return the board configuration */ return baseConfig; }