bool autoScan(Samba& samba, PortFactory& portFactory, string& port) { for (port = portFactory.begin(); port != portFactory.end(); port = portFactory.next()) { if (config.debug) printf("Trying to connect on %s\n", port.c_str()); if (samba.connect(portFactory.create(port))) return true; } return false; }
int main(int argc, char* argv[]) { int args; char* pos; CmdOpts cmd(argc, argv, sizeof(opts) / sizeof(opts[0]), opts); if ((pos = strrchr(argv[0], '/')) || (pos = strrchr(argv[0], '\\'))) argv[0] = pos + 1; if (argc <= 1) { fprintf(stderr, "%s: you must specify at least one option\n", argv[0]); return help(argv[0]); } args = cmd.parse(); if (args < 0) return help(argv[0]); if (config.read && (config.write || config.verify)) { fprintf(stderr, "%s: read option is exclusive of write or verify\n", argv[0]); return help(argv[0]); } if (config.read || config.write || config.verify) { if (args == argc) { fprintf(stderr, "%s: missing file\n", argv[0]); return help(argv[0]); } argc--; } if (args != argc) { fprintf(stderr, "%s: extra arguments found\n", argv[0]); return help(argv[0]); } if (config.help) { printf("Usage: %s [OPTION...] [FILE]\n", argv[0]); printf("Basic Open Source SAM-BA Application (BOSSA) Version " VERSION "\n" "Flash programmer for Atmel SAM devices.\n" "Copyright (c) 2011-2012 ShumaTech (http://www.shumatech.com)\n" "\n" "Examples:\n" " bossac -e -w -v -b image.bin # Erase flash, write flash with image.bin,\n" " # verify the write, and set boot from flash\n" " bossac -r0x10000 image.bin # Read 64KB from flash and store in image.bin\n" ); printf("\nOptions:\n"); cmd.usage(stdout); printf("\nReport bugs to <*****@*****.**>\n"); return 1; } try { Samba samba; PortFactory portFactory; FlashFactory flashFactory; if (config.debug) samba.setDebug(true); bool isUsb = false; if (config.forceUsb) { if (config.forceUsbArg.compare("true")==0) isUsb = true; else if (config.forceUsbArg.compare("false")==0) isUsb = false; else { fprintf(stderr, "Invalid USB value: %s\n", config.forceUsbArg.c_str()); return 1; } } if (config.port) { bool res; if (config.forceUsb) res = samba.connect(portFactory.create(config.portArg, isUsb)); else res = samba.connect(portFactory.create(config.portArg)); if (!res) { fprintf(stderr, "No device found on %s\n", config.portArg.c_str()); return 1; } } else { string port; if (!autoScan(samba, portFactory, port)) { fprintf(stderr, "Auto scan for device failed\n"); fprintf(stderr, "Try specifying a serial port with the '-p' option\n"); return 1; } printf("Device found on %s\n", port.c_str()); } uint32_t chipId = samba.chipId(); Flash::Ptr flash = flashFactory.create(samba, chipId); if (flash.get() == NULL) { fprintf(stderr, "Flash for chip ID %08x is not supported\n", chipId); return 1; } Flasher flasher(flash); if (config.unlock) flasher.lock(config.unlockArg, false); if (config.erase) flasher.erase(); if (config.write) flasher.write(argv[args]); if (config.verify) if (!flasher.verify(argv[args])) return 2; if (config.read) flasher.read(argv[args], config.readArg); if (config.boot) { printf("Set boot flash %s\n", config.bootArg ? "true" : "false"); flash->setBootFlash(config.bootArg); } if (config.bod) { printf("Set brownout detect %s\n", config.bodArg ? "true" : "false"); flash->setBod(config.bodArg); } if (config.bor) { printf("Set brownout reset %s\n", config.borArg ? "true" : "false"); flash->setBor(config.borArg); } if (config.security) { printf("Set security\n"); flash->setSecurity(); } if (config.lock) flasher.lock(config.lockArg, true); if (config.info) flasher.info(samba); if (config.reset) samba.reset(); } catch (exception& e) { fprintf(stderr, "\n%s\n", e.what()); return 1; } catch(...) { fprintf(stderr, "\nUnhandled exception\n"); return 1; } return 0; }
int main(int argc, char* argv[]) { int args; char* pos; CmdOpts cmd(argc, argv, sizeof(opts) / sizeof(opts[0]), opts); if ((pos = strrchr(argv[0], '/')) || (pos = strrchr(argv[0], '\\'))) argv[0] = pos + 1; if (argc <= 1) { fprintf(stderr, "%s: you must specify at least one option\n", argv[0]); return help(argv[0]); } args = cmd.parse(); if (args < 0) return help(argv[0]); if (config.read && (config.write || config.verify)) { fprintf(stderr, "%s: read option is exclusive of write or verify\n", argv[0]); return help(argv[0]); } if (config.read || config.write || config.verify) { if (args == argc) { fprintf(stderr, "%s: missing file\n", argv[0]); return help(argv[0]); } argc--; } if (args != argc) { fprintf(stderr, "%s: extra arguments found\n", argv[0]); return help(argv[0]); } if (config.help) { printf("Usage: %s [OPTION...] [FILE]\n", argv[0]); printf("Basic Open Source SAM-BA Application (BOSSA) Version " VERSION "\n" "Flash programmer for Atmel SAM devices.\n" "Copyright (c) 2011-2018 ShumaTech (http://www.shumatech.com)\n" "\n" "Examples:\n" " bossac -e -w -v -b image.bin # Erase flash, write flash with image.bin,\n" " # verify the write, and set boot from flash\n" " bossac -r0x10000 image.bin # Read 64KB from flash and store in image.bin\n" ); printf("\nOptions:\n"); cmd.usage(stdout); printf("\nReport bugs to <*****@*****.**>\n"); return 1; } try { Samba samba; PortFactory portFactory; if (config.debug) samba.setDebug(true); if (!config.port) config.portArg = portFactory.def(); if (config.arduinoErase) { SerialPort::Ptr port; port = portFactory.create(config.portArg, config.usbPortArg != 0); if(!port->open(1200)) { fprintf(stderr, "Failed to open port at 1200bps\n"); return 1; } port->setRTS(true); port->setDTR(false); port->close(); } if (config.portArg.empty()) { fprintf(stderr, "No serial ports available\n"); return 1; } bool res; if (config.usbPort) res = samba.connect(portFactory.create(config.portArg, config.usbPortArg != 0)); else res = samba.connect(portFactory.create(config.portArg)); if (!res) { fprintf(stderr, "No device found on %s\n", config.portArg.c_str()); return 1; } Device device(samba); device.create(); Device::FlashPtr& flash = device.getFlash(); BossaObserver observer; Flasher flasher(samba, device, observer); if (config.info) { FlasherInfo info; flasher.info(info); info.print(); } if (config.unlock) flasher.lock(config.unlockArg, false); if (config.erase) { timer_start(); flasher.erase(config.offsetArg); printf("\nDone in %5.3f seconds\n", timer_stop()); } if (config.write) { timer_start(); flasher.write(argv[args], config.offsetArg); printf("\nDone in %5.3f seconds\n", timer_stop()); } if (config.verify) { uint32_t pageErrors; uint32_t totalErrors; timer_start(); if (!flasher.verify(argv[args], pageErrors, totalErrors, config.offsetArg)) { printf("\nVerify failed\nPage errors: %d\nByte errors: %d\n", pageErrors, totalErrors); return 2; } printf("\nVerify successful\nDone in %5.3f seconds\n", timer_stop()); } if (config.read) { timer_start(); flasher.read(argv[args], config.readArg, config.offsetArg); printf("\nDone in %5.3f seconds\n", timer_stop()); } if (config.boot) { printf("Set boot flash %s\n", config.bootArg ? "true" : "false"); flash->setBootFlash(config.bootArg); } if (config.bod) { printf("Set brownout detect %s\n", config.bodArg ? "true" : "false"); flash->setBod(config.bodArg); } if (config.bor) { printf("Set brownout reset %s\n", config.borArg ? "true" : "false"); flash->setBor(config.borArg); } if (config.security) { printf("Set security\n"); flash->setSecurity(); } if (config.lock) flasher.lock(config.lockArg, true); flash->writeOptions(); if (config.reset) device.reset(); } catch (exception& e) { fprintf(stderr, "\n%s\n", e.what()); return 1; } catch(...) { fprintf(stderr, "\nUnhandled exception\n"); return 1; } return 0; }