Browse Source

Switching speeds. Huge performance improvement.

kmpm 8 years ago
parent
commit
03612c3064
4 changed files with 34 additions and 19 deletions
  1. 9 1
      README.md
  2. 1 3
      lib/luacode.py
  3. 7 3
      lib/main.py
  4. 17 12
      lib/uploader.py

+ 9 - 1
README.md

@@ -56,11 +56,19 @@ SOFTWARE.
 
 
 Usage (part of it)
 Usage (part of it)
 ------------------
 ------------------
-* --baud are set at a default of 9600
+* --start_baud set at a default of 9600 (the speed of the nodemcu at boot)
+* --baud are set at a default of 115200
 * --port is by default __/dev/ttyUSB0__,
 * --port is by default __/dev/ttyUSB0__,
   __/dev/tty.SLAB_USBtoUART__ if on Mac and __COM1__ on Windows
   __/dev/tty.SLAB_USBtoUART__ if on Mac and __COM1__ on Windows
 * the environment variable __SERIALPORT__ will override any default port
 * the environment variable __SERIALPORT__ will override any default port
 
 
+Since v0.2.1 the program works with 2 speeds. It connects at a default
+(--start_baud) of 9600 baud which is what the default firmware uses. Immediately after
+first established communication it changes to a higher (--baud) speed which defaults
+to 115200. This allows all communication to happen much faster without having to
+recompile the firmware or do any manual changes to the speed.
+When done and before it closes the port it changes the speed back to normal.
+
 ###Upload
 ###Upload
 Uploading a number of files.
 Uploading a number of files.
 Supports multiple files. If you want an alternate destination name, just
 Supports multiple files. If you want an alternate destination name, just

+ 1 - 3
lib/luacode.py

@@ -24,16 +24,14 @@ function recv_block(d)
     else
     else
       file.close()
       file.close()
       uart.on('data')
       uart.on('data')
-      uart.setup(0,{baud},8,0,1,1)
     end
     end
   else
   else
     uart.write(0, '\021' .. d)
     uart.write(0, '\021' .. d)
-    uart.setup(0,{baud},8,0,1,1)
     uart.on('data')
     uart.on('data')
   end
   end
 end
 end
 function recv_name(d) d = string.gsub(d, '\000', '') file.remove(d) file.open(d, 'w') uart.on('data', 130, recv_block, 0) uart.write(0, '\006') end
 function recv_name(d) d = string.gsub(d, '\000', '') file.remove(d) file.open(d, 'w') uart.on('data', 130, recv_block, 0) uart.write(0, '\006') end
-function recv() uart.setup(0,{baud},8,0,1,0) uart.on('data', '\000', recv_name, 0) uart.write(0, 'C') end
+function recv() uart.on('data', '\000', recv_name, 0) uart.write(0, 'C') end
 function shafile(f) file.open(f, "r") print(crypto.toHex(crypto.hash("sha1",file.read()))) file.close() end
 function shafile(f) file.open(f, "r") print(crypto.toHex(crypto.hash("sha1",file.read()))) file.close() end
 """
 """
 
 

+ 7 - 3
lib/main.py

@@ -120,6 +120,12 @@ def main_func():
         type=arg_auto_int,
         type=arg_auto_int,
         default=Uploader.BAUD)
         default=Uploader.BAUD)
 
 
+    parser.add_argument(
+        '--start_baud', '-B',
+        help='Initial Serial port baudrate',
+        type=arg_auto_int,
+        default=Uploader.START_BAUD)
+
     subparsers = parser.add_subparsers(
     subparsers = parser.add_subparsers(
         dest='operation',
         dest='operation',
         help='Run nodemcu-uploader {command} -h for additional help')
         help='Run nodemcu-uploader {command} -h for additional help')
@@ -209,9 +215,7 @@ def main_func():
 
 
     logging.basicConfig(level=default_level, format='%(message)s')
     logging.basicConfig(level=default_level, format='%(message)s')
 
 
-    uploader = Uploader(args.port, args.baud)
-
-
+    uploader = Uploader(args.port, args.baud, start_baud=args.start_baud)
 
 
     if args.operation == 'upload':
     if args.operation == 'upload':
         operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,
         operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,

+ 17 - 12
lib/uploader.py

@@ -26,17 +26,20 @@ class Uploader(object):
     """Uploader is the class for communicating with the nodemcu and
     """Uploader is the class for communicating with the nodemcu and
     that will allow various tasks like uploading files, formating the filesystem etc.
     that will allow various tasks like uploading files, formating the filesystem etc.
     """
     """
-    BAUD = 9600
+    BAUD = 115200
+    START_BAUD = 9600
     TIMEOUT = 5
     TIMEOUT = 5
     PORT = default_port()
     PORT = default_port()
 
 
-    def __init__(self, port=PORT, baud=BAUD):
-        log.info('opening port %s with %s baud', port, baud)
+    def __init__(self, port=PORT, baud=BAUD, start_baud=START_BAUD):
+        log.info('opening port %s with %s baud', port, start_baud)
         if port == 'loop://':
         if port == 'loop://':
-            self._port = serial.serial_for_url(port, baud, timeout=Uploader.TIMEOUT)
+            self._port = serial.serial_for_url(port, start_baud, timeout=Uploader.TIMEOUT)
         else:
         else:
-            self._port = serial.Serial(port, baud, timeout=Uploader.TIMEOUT)
+            self._port = serial.Serial(port, start_baud, timeout=Uploader.TIMEOUT)
 
 
+        self.start_baud = start_baud
+        self.baud = baud
         # Keeps things working, if following conections are made:
         # Keeps things working, if following conections are made:
         ## RTS = CH_PD (i.e reset)
         ## RTS = CH_PD (i.e reset)
         ## DTR = GPIO0
         ## DTR = GPIO0
@@ -56,12 +59,7 @@ class Uploader(object):
 
 
         sync()
         sync()
 
 
-        if baud != Uploader.BAUD:
-            log.info('Changing communication to %s baud', baud)
-            self.writeln(UART_SETUP.format(baud=baud))
-
-            # Wait for the string to be sent before switching baud
-            time.sleep(0.1)
+        if baud != start_baud:
             self.set_baudrate(baud)
             self.set_baudrate(baud)
 
 
             # Get in sync again
             # Get in sync again
@@ -70,6 +68,10 @@ class Uploader(object):
         self.line_number = 0
         self.line_number = 0
 
 
     def set_baudrate(self, baud):
     def set_baudrate(self, baud):
+        log.info('Changing communication to %s baud', baud)
+        self.writeln(UART_SETUP.format(baud=baud))
+        # Wait for the string to be sent before switching baud
+        time.sleep(0.1)
         try:
         try:
             self._port.setBaudrate(baud)
             self._port.setBaudrate(baud)
         except AttributeError:
         except AttributeError:
@@ -136,10 +138,12 @@ class Uploader(object):
         self._port.flush()
         self._port.flush()
         return self.expect(timeout=timeout)
         return self.expect(timeout=timeout)
 
 
+
     def close(self):
     def close(self):
         """restores the nodemcu to default baudrate and then closes the port"""
         """restores the nodemcu to default baudrate and then closes the port"""
         try:
         try:
-            self.writeln(UART_SETUP.format(baud=Uploader.BAUD))
+            if self.baud != self.start_baud:
+                self.set_baudrate(self.start_baud)
             self._port.flush()
             self._port.flush()
             self.clear_buffers()
             self.clear_buffers()
         except serial.serialutil.SerialException:
         except serial.serialutil.SerialException:
@@ -147,6 +151,7 @@ class Uploader(object):
         log.debug('closing port')
         log.debug('closing port')
         self._port.close()
         self._port.close()
 
 
+
     def prepare(self):
     def prepare(self):
         """
         """
         This uploads the protocol functions nessecary to do binary
         This uploads the protocol functions nessecary to do binary