summaryrefslogtreecommitdiff
path: root/esp8266/modules
diff options
context:
space:
mode:
Diffstat (limited to 'esp8266/modules')
-rw-r--r--esp8266/modules/_boot.py4
-rw-r--r--esp8266/modules/apa102.py17
-rw-r--r--esp8266/modules/dht.py32
-rw-r--r--esp8266/modules/flashbdev.py2
-rw-r--r--esp8266/modules/inisetup.py52
-rw-r--r--esp8266/modules/neopixel.py32
-rw-r--r--esp8266/modules/ntptime.py36
-rw-r--r--esp8266/modules/port_diag.py33
8 files changed, 205 insertions, 3 deletions
diff --git a/esp8266/modules/_boot.py b/esp8266/modules/_boot.py
index c200b3d3f..81eb20dd6 100644
--- a/esp8266/modules/_boot.py
+++ b/esp8266/modules/_boot.py
@@ -5,9 +5,9 @@ from flashbdev import bdev
try:
if bdev:
- vfs = uos.VfsFat(bdev, "")
+ uos.mount(bdev, '/')
except OSError:
import inisetup
- vfs = inisetup.setup()
+ inisetup.setup()
gc.collect()
diff --git a/esp8266/modules/apa102.py b/esp8266/modules/apa102.py
new file mode 100644
index 000000000..41b7c0485
--- /dev/null
+++ b/esp8266/modules/apa102.py
@@ -0,0 +1,17 @@
+# APA102 driver for MicroPython on ESP8266
+# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch
+
+from esp import apa102_write
+from neopixel import NeoPixel
+
+
+class APA102(NeoPixel):
+ ORDER = (0, 1, 2, 3)
+
+ def __init__(self, clock_pin, data_pin, n, bpp=4):
+ super().__init__(data_pin, n, bpp)
+ self.clock_pin = clock_pin
+ self.clock_pin.init(clock_pin.OUT)
+
+ def write(self):
+ apa102_write(self.clock_pin, self.pin, self.buf)
diff --git a/esp8266/modules/dht.py b/esp8266/modules/dht.py
new file mode 100644
index 000000000..9a69e7e07
--- /dev/null
+++ b/esp8266/modules/dht.py
@@ -0,0 +1,32 @@
+# DHT11/DHT22 driver for MicroPython on ESP8266
+# MIT license; Copyright (c) 2016 Damien P. George
+
+import esp
+
+class DHTBase:
+ def __init__(self, pin):
+ self.pin = pin
+ self.buf = bytearray(5)
+
+ def measure(self):
+ buf = self.buf
+ esp.dht_readinto(self.pin, buf)
+ if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
+ raise Exception("checksum error")
+
+class DHT11(DHTBase):
+ def humidity(self):
+ return self.buf[0]
+
+ def temperature(self):
+ return self.buf[2]
+
+class DHT22(DHTBase):
+ def humidity(self):
+ return (self.buf[0] << 8 | self.buf[1]) * 0.1
+
+ def temperature(self):
+ t = ((self.buf[2] & 0x7f) << 8 | self.buf[3]) * 0.1
+ if self.buf[2] & 0x80:
+ t = -t
+ return t
diff --git a/esp8266/modules/flashbdev.py b/esp8266/modules/flashbdev.py
index 8f8df0b64..40ba655c6 100644
--- a/esp8266/modules/flashbdev.py
+++ b/esp8266/modules/flashbdev.py
@@ -3,7 +3,7 @@ import esp
class FlashBdev:
SEC_SIZE = 4096
- RESERVED_SECS = 0
+ RESERVED_SECS = 1
START_SEC = esp.flash_user_start() // SEC_SIZE + RESERVED_SECS
NUM_BLK = 0x6b - RESERVED_SECS
diff --git a/esp8266/modules/inisetup.py b/esp8266/modules/inisetup.py
new file mode 100644
index 000000000..af78dfad5
--- /dev/null
+++ b/esp8266/modules/inisetup.py
@@ -0,0 +1,52 @@
+import uos
+import network
+from flashbdev import bdev
+
+def wifi():
+ import ubinascii
+ ap_if = network.WLAN(network.AP_IF)
+ essid = b"MicroPython-%s" % ubinascii.hexlify(ap_if.config("mac")[-3:])
+ ap_if.config(essid=essid, authmode=network.AUTH_WPA_WPA2_PSK, password=b"micropythoN")
+
+def check_bootsec():
+ buf = bytearray(bdev.SEC_SIZE)
+ bdev.readblocks(0, buf)
+ empty = True
+ for b in buf:
+ if b != 0xff:
+ empty = False
+ break
+ if empty:
+ return True
+ fs_corrupted()
+
+def fs_corrupted():
+ import time
+ while 1:
+ print("""\
+The FAT filesystem starting at sector %d with size %d sectors appears to
+be corrupted. If you had important data there, you may want to make a flash
+snapshot to try to recover it. Otherwise, perform factory reprogramming
+of MicroPython firmware (completely erase flash, followed by firmware
+programming).
+""" % (bdev.START_SEC, bdev.blocks))
+ time.sleep(3)
+
+def setup():
+ check_bootsec()
+ print("Performing initial setup")
+ wifi()
+ uos.VfsFat.mkfs(bdev)
+ vfs = uos.VfsFat(bdev)
+ uos.mount(vfs, '/')
+ with open("boot.py", "w") as f:
+ f.write("""\
+# This file is executed on every boot (including wake-boot from deepsleep)
+#import esp
+#esp.osdebug(None)
+import gc
+#import webrepl
+#webrepl.start()
+gc.collect()
+""")
+ return vfs
diff --git a/esp8266/modules/neopixel.py b/esp8266/modules/neopixel.py
new file mode 100644
index 000000000..b13424d7d
--- /dev/null
+++ b/esp8266/modules/neopixel.py
@@ -0,0 +1,32 @@
+# NeoPixel driver for MicroPython on ESP8266
+# MIT license; Copyright (c) 2016 Damien P. George
+
+from esp import neopixel_write
+
+
+class NeoPixel:
+ ORDER = (1, 0, 2, 3)
+
+ def __init__(self, pin, n, bpp=3):
+ self.pin = pin
+ self.n = n
+ self.bpp = bpp
+ self.buf = bytearray(n * bpp)
+ self.pin.init(pin.OUT)
+
+ def __setitem__(self, index, val):
+ offset = index * self.bpp
+ for i in range(self.bpp):
+ self.buf[offset + self.ORDER[i]] = val[i]
+
+ def __getitem__(self, index):
+ offset = index * self.bpp
+ return tuple(self.buf[offset + self.ORDER[i]]
+ for i in range(self.bpp))
+
+ def fill(self, color):
+ for i in range(self.n):
+ self[i] = color
+
+ def write(self):
+ neopixel_write(self.pin, self.buf, True)
diff --git a/esp8266/modules/ntptime.py b/esp8266/modules/ntptime.py
new file mode 100644
index 000000000..a97e08e60
--- /dev/null
+++ b/esp8266/modules/ntptime.py
@@ -0,0 +1,36 @@
+try:
+ import usocket as socket
+except:
+ import socket
+try:
+ import ustruct as struct
+except:
+ import struct
+
+# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
+NTP_DELTA = 3155673600
+
+host = "pool.ntp.org"
+
+def time():
+ NTP_QUERY = bytearray(48)
+ NTP_QUERY[0] = 0x1b
+ addr = socket.getaddrinfo(host, 123)[0][-1]
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ s.settimeout(1)
+ res = s.sendto(NTP_QUERY, addr)
+ msg = s.recv(48)
+ s.close()
+ val = struct.unpack("!I", msg[40:44])[0]
+ return val - NTP_DELTA
+
+# There's currently no timezone support in MicroPython, so
+# utime.localtime() will return UTC time (as if it was .gmtime())
+def settime():
+ t = time()
+ import machine
+ import utime
+ tm = utime.localtime(t)
+ tm = tm[0:3] + (0,) + tm[3:6] + (0,)
+ machine.RTC().datetime(tm)
+ print(utime.localtime())
diff --git a/esp8266/modules/port_diag.py b/esp8266/modules/port_diag.py
new file mode 100644
index 000000000..ef8800355
--- /dev/null
+++ b/esp8266/modules/port_diag.py
@@ -0,0 +1,33 @@
+import esp
+import uctypes
+import network
+import lwip
+
+
+def main():
+
+ ROM = uctypes.bytearray_at(0x40200000, 16)
+ fid = esp.flash_id()
+
+ print("FlashROM:")
+ print("Flash ID: %x (Vendor: %x Device: %x)" % (fid, fid & 0xff, fid & 0xff00 | fid >> 16))
+
+ print("Flash bootloader data:")
+ SZ_MAP = {0: "512KB", 1: "256KB", 2: "1MB", 3: "2MB", 4: "4MB"}
+ FREQ_MAP = {0: "40MHZ", 1: "26MHZ", 2: "20MHz", 0xf: "80MHz"}
+ print("Byte @2: %02x" % ROM[2])
+ print("Byte @3: %02x (Flash size: %s Flash freq: %s)" % (ROM[3], SZ_MAP.get(ROM[3] >> 4, "?"), FREQ_MAP.get(ROM[3] & 0xf)))
+ print("Firmware checksum:")
+ print(esp.check_fw())
+
+ print("\nNetworking:")
+ print("STA ifconfig:", network.WLAN(network.STA_IF).ifconfig())
+ print("AP ifconfig:", network.WLAN(network.AP_IF).ifconfig())
+ print("Free WiFi driver buffers of type:")
+ for i, comm in enumerate(("1,2 TX", "4 Mngmt TX(len: 0x41-0x100)", "5 Mngmt TX (len: 0-0x40)", "7", "8 RX")):
+ print("%d: %d (%s)" % (i, esp.esf_free_bufs(i), comm))
+ print("lwIP PCBs:")
+ lwip.print_pcbs()
+
+
+main()