eric6/MicroPython/MicroPythonCommandsInterface.py

changeset 8115
cef34b4ee310
parent 8061
979562f350bf
child 8117
aaa5e0eacd4e
diff -r b7abbf3f82a3 -r cef34b4ee310 eric6/MicroPython/MicroPythonCommandsInterface.py
--- a/eric6/MicroPython/MicroPythonCommandsInterface.py	Thu Feb 18 17:44:53 2021 +0100
+++ b/eric6/MicroPython/MicroPythonCommandsInterface.py	Thu Feb 18 19:57:41 2021 +0100
@@ -754,49 +754,93 @@
         
         @exception OSError raised to indicate an issue with the device
         """
+        # rtc_time[0] - year    4 digit
+        # rtc_time[1] - month   1..12
+        # rtc_time[2] - day     1..31
+        # rtc_time[3] - weekday 1..7 1=Monday
+        # rtc_time[4] - hour    0..23
+        # rtc_time[5] - minute  0..59
+        # rtc_time[6] - second  0..59
+        # rtc_time[7] - yearday 1..366
+        # rtc_time[8] - isdst   0, 1, or -1
         now = time.localtime(time.time())
         commands = [
             "\n".join([
                 "def set_time(rtc_time):",
                 "    rtc = None",
-                "    try:",           # Pyboard (it doesn't have machine.RTC())
-                "        import pyb as __pyb_",
-                "        rtc = __pyb_.RTC()",
-                "        clock_time = rtc_time[:3] +"
-                " (rtc_time[6] + 1,) + rtc_time[3:6] + (0,)",
-                "        rtc.datetime(clock_time)",
-                "        del __pyb_",
+                # Pyboard (pyboard doesn't have machine.RTC()).
+                # The pyb.RTC.datetime function takes the arguments in the
+                # order:
+                # (year, month, day, weekday, hour, minute, second, subseconds)
+                # http://docs.micropython.org/en/latest/library/pyb.RTC.html
+                # #pyb.RTC.datetime
+                "    try:",
+                "        import pyb",
+                "        rtc = pyb.RTC()",
+                "        rtc.datetime(rtc_time)",
                 "    except Exception:",
                 "        try:",
-                "            import machine as __machine_",
-                "            rtc = __machine_.RTC()",
-                "            try:",     # ESP8266 may use rtc.datetime()
-                "                clock_time = rtc_time[:3] +"
-                " (rtc_time[6],) + rtc_time[3:6] + (0,)",
-                "                rtc.datetime(clock_time)",
-                "            except Exception:",  # ESP32 uses rtc.init()
-                "                import os",
-                "                if 'LoBo' in os.uname()[0]:",  # LoBo MPy
-                "                    clock_time = rtc_time + (0,)",
-                "                else:",
-                "                    clock_time = rtc_time[:3] +"
-                " (rtc_time[6],) + rtc_time[3:6] + (0,)",
-                "                rtc.init(clock_time)",
-                "            del __machine_",
+                # PyCom's machine.RTC takes its arguments in a slightly
+                # different order than the official machine.RTC.
+                # (year, month, day, hour, minute, second[, microsecond[,
+                # tzinfo]])
+                # https://docs.pycom.io/firmwareapi/pycom/machine/rtc/
+                # #rtc-init-datetime-none-source-rtc-internal-rc
+                "            import pycom",
+                "            rtc_time2 = (rtc_time[0], rtc_time[1],"
+                " rtc_time[2], rtc_time[4], rtc_time[5], rtc_time[6])",
+                "            import machine",
+                "            rtc = machine.RTC()",
+                "            rtc.init(rtc_time2)",
                 "        except:",
                 "            try:",
-                "                import rtc as __rtc_",
-                "                import time as __time_",
-                "                clock=__rtc_.RTC()",
-                "                clock.datetime = __time_.struct_time("
-                "rtc_time + (-1, -1))",
-                "                del __rtc_, __time_",
+                # The machine.RTC documentation was incorrect and doesn't agree
+                # with the code, so no link is presented here. The order of the
+                # arguments is the same as the pyboard except for LoBo MPy.
+                "                import machine",
+                "                rtc = machine.RTC()",
+                "                try:",      # ESP8266 may use rtc.datetime()
+                "                    rtc.datetime(rtc_time)",
+                "                except Exception:",  # ESP32 uses rtc.init()
+                "                    import os",
+                "                    if 'LoBo' in os.uname()[0]:",  # LoBo MPy
+                "                        clock_time = rtc_time[:3] +"
+                " rtc_time[4:7] + (rtc_time[3], 0)",
+                "                    else:",
+                "                        clock_time = rtc_time",
+                "                    rtc.init(clock_time)",
                 "            except:",
-                "                pass",
+                "                try:",
+                # Check for CircuitPython devices
+                "                    import rtc",
+                "                    import time",
+                "                    clock = rtc.RTC()",
+                "                    clock_time = rtc_time[:3] + rtc_time[4:7]"
+                " + (rtc_time[3], -1, -1)",
+                "                    clock.datetime = time.struct_time("
+                "clock_time)",
+                "                except:",
+                # Check for the Raspberry Pi Pico - machine.RTC doesn't exist
+                "                    try:",
+                "                        import os",
+                "                        if os.uname().sysname == 'rp2':",
+                "                            setup_0 = rtc_time[0] << 12 |"
+                " rtc_time[1] << 8 | rtc_time[2]",
+                "                            setup_1 = (rtc_time[3] % 7) << 24"
+                " | rtc_time[4] << 16 | rtc_time[5] << 8 | rtc_time[6]",
+                "                            machine.mem32[0x4005c004] ="
+                " setup_0",
+                "                            machine.mem32[0x4005c008] ="
+                " setup_1",
+                "                            machine.mem32[0x4005c00c] |="
+                " 0x10",
+                "                    except:",
+                "                        pass",
             ]),
-            "set_time({0})".format((now.tm_year, now.tm_mon, now.tm_mday,
-                                    now.tm_hour, now.tm_min, now.tm_sec,
-                                    now.tm_wday)),
+            "set_time({0})".format((
+                now.tm_year, now.tm_mon, now.tm_mday, now.tm_wday + 1, 
+                now.tm_hour, now.tm_min, now.tm_sec, 0
+            )),
             "del set_time",
         ]
         out, err = self.execute(commands)

eric ide

mercurial