|
| 1 | +import micropython |
1 | 2 |
|
2 |
| -def space_free(): # Display remaining free space |
3 |
| - from os import statvfs |
4 |
| - |
5 |
| - bits = statvfs('/flash') |
6 |
| - # print(str(bits)) |
7 |
| - blksize = bits[0] # 4096 |
8 |
| - blkfree = bits[3] # 12 |
9 |
| - freesize = blksize * blkfree # 49152 |
10 |
| - mbcalc = 1024 * 1024 # 1048576 |
11 |
| - mbfree = freesize / mbcalc # 0.046875 |
12 |
| - print("Free space:" + str(mbfree)) |
13 |
| - |
14 |
| - |
15 |
| -def m_freq(): # Get current machine Freq |
16 |
| - import machine |
17 |
| - |
18 |
| - gfr = str(machine.freq()) |
19 |
| - |
20 |
| - print("Mhz: " + gfr) |
21 |
| - |
22 |
| - return gfr |
23 |
| - |
24 |
| - |
25 |
| -def raw_temp(): # Get current CPU temp |
26 |
| - import esp32 |
27 |
| - |
28 |
| - raw = str(esp32.raw_temperature()) |
29 |
| - rtemp = ("CPU Temp: " + raw + "F") |
30 |
| - print(rtemp) |
31 |
| - rrr = rtemp |
32 |
| - return rrr |
33 |
| - |
34 |
| - |
35 |
| -def showVoltage(): # Show current(pun intended) used Voltage |
36 |
| - # noinspection PyUnresolvedReferences |
37 |
| - from machine import ADC, Pin |
38 |
| - |
39 |
| - adc = ADC(Pin(32)) |
40 |
| - |
41 |
| - vref = int(1100) |
42 |
| - |
43 |
| - v = adc.read() |
44 |
| - battery_voltage = (float(v) / 4095.0) * 2.0 * 3.3 * (vref / 1000.0) |
45 |
| - voltage = ("Voltage: {0:0.2f}v".format(battery_voltage)) |
46 |
| - print(voltage) |
47 | 3 |
|
48 |
| - ddd = voltage |
49 |
| - return ddd |
50 |
| - |
51 |
| - |
52 |
| -def getmac(): # Get and display chip MAC address |
| 4 | +def bug_boot(): |
53 | 5 | import network
|
54 | 6 | import ubinascii
|
55 |
| - mac = ubinascii.hexlify(network.WLAN(1).config('mac'), ':').decode() |
56 |
| - print(str(mac)) |
57 |
| - |
58 |
| - |
59 |
| -def pprint(): # Put it all together and PRINT |
| 7 | + import machine |
| 8 | + from os import statvfs |
| 9 | + import os |
60 | 10 | import gc
|
| 11 | + import time |
61 | 12 |
|
| 13 | + bits = statvfs('/flash') |
| 14 | + mbfree = bits[0] * bits[3] / (1024 * 1024) |
| 15 | + mac = ubinascii.hexlify(network.WLAN(1).config('mac'), ':').decode() |
| 16 | + dirr = str(os.listdir()) |
| 17 | + time.sleep_ms(100) |
| 18 | + |
| 19 | + print("") |
| 20 | + print(f"MAC: {mac}") |
| 21 | + print(f"Mhz: {machine.freq() / 1e6:.0f}") |
| 22 | + print(f"Flash: {mbfree:.2f} MB") |
62 | 23 | print("")
|
63 |
| - print("-------------------") |
64 |
| - getmac() |
65 |
| - space_free() |
66 |
| - m_freq() |
67 |
| - raw_temp() |
68 |
| - showVoltage() |
69 |
| - print("--------------------") |
| 24 | + print(f"Files: {dirr}") |
70 | 25 | print("")
|
71 | 26 |
|
| 27 | + del statvfs, network, ubinascii, machine, os |
72 | 28 | gc.collect()
|
73 | 29 |
|
74 | 30 |
|
75 |
| -''' |
76 |
| -N = 200_000 |
77 | 31 | def time_it(f, n):
|
| 32 | + import utime |
78 | 33 | t0 = utime.ticks_us()
|
79 | 34 | f(n)
|
80 | 35 | t1 = utime.ticks_us()
|
81 | 36 | dt = utime.ticks_diff(t1, t0)
|
82 | 37 | fmt = '{:5.3f} sec, {:6.3f} usec/read : {:8.2f} kreads/sec'
|
83 | 38 | print(fmt.format(dt * 1e-6, dt / n, n / dt * 1e3))
|
84 | 39 |
|
85 |
| -time_it(showVoltage, N) |
86 |
| -''' |
| 40 | + # time_it(showVoltage, N) |
| 41 | + |
| 42 | + |
| 43 | +def serial_mem(mp: bool): |
| 44 | + import micropython |
| 45 | + |
| 46 | + micropython.mem_info(mp) |
| 47 | + del micropython |
| 48 | + |
| 49 | + |
| 50 | +@micropython.native |
| 51 | +def crash_esp32(): |
| 52 | + import machine |
| 53 | + """ |
| 54 | + Crash the ESP32 by writing to an invalid memory address. |
| 55 | + This will trigger a panic and reset the device. |
| 56 | + """ |
| 57 | + # Define an invalid memory address (aligned to 4 bytes) |
| 58 | + address = 0x00000000 # Using address 0, which is typically unmapped |
| 59 | + |
| 60 | + try: |
| 61 | + # Attempt to read from the invalid address |
| 62 | + machine.mem32[address] |
| 63 | + except Exception as e: |
| 64 | + print(f"Crash attempt triggered an exception: {e}") |
| 65 | + print("ESP32 should reset now.") |
| 66 | + |
| 67 | + |
| 68 | +def inf_recurs(depth=0): |
| 69 | + import utime |
| 70 | + """Crash by stack overflow due to infinite recursion.""" |
| 71 | + print(f"Recursion depth: {depth}") |
| 72 | + utime.sleep_ms(1) # Small delay to allow for print |
| 73 | + return inf_recurs(depth + 1) |
| 74 | + |
| 75 | + |
| 76 | +def watchout(): |
| 77 | + """Crash by triggering the watchdog timer.""" |
| 78 | + # Disable the software WDT if it's running |
| 79 | + try: |
| 80 | + import machine |
| 81 | + machine.WDT(timeout=1000) # 1 second timeout |
| 82 | + except: |
| 83 | + pass # Hardware WDT might still be active |
| 84 | + |
| 85 | + print("Waiting for watchdog timer...") |
| 86 | + while True: |
| 87 | + pass # Busy loop to prevent feeding the watchdog |
0 commit comments