class FlashBdev: SEC_SIZE = 4096 RESERVED_SECS = 20 #needed for user native code extensions #NOTE: will destroy existing filesystems built with previous commits #NOTE: might not work with 1-Mbit flash size devices and OTA firmware START_SEC = esp.flash_user_start() // SEC_SIZE + RESERVED_SECS NUM_BLK = 0x6b - RESERVED_SECS def __init__(self, blocks=NUM_BLK): self.blocks = blocks def readblocks(self, n, buf): #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf) def writeblocks(self, n, buf): #print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) #assert len(buf) <= self.SEC_SIZE, len(buf) esp.flash_erase(n + self.START_SEC) esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf) def ioctl(self, op, arg): #print("ioctl(%d, %r)" % (op, arg)) if op == 4: # BP_IOCTL_SEC_COUNT return self.blocks if op == 5: # BP_IOCTL_SEC_SIZE return self.SEC_SIZE
class FlashBdev: SEC_SIZE = 4096 START_SEC = esp.flash_user_start() // SEC_SIZE NUM_BLK = 0x6b def __init__(self, blocks=NUM_BLK): self.blocks = blocks def readblocks(self, n, buf): #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf) def writeblocks(self, n, buf): #print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) #assert len(buf) <= self.SEC_SIZE, len(buf) esp.flash_erase(n + self.START_SEC) esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf) def ioctl(self, op, arg): #print("ioctl(%d, %r)" % (op, arg)) if op == 4: # BP_IOCTL_SEC_COUNT return self.blocks if op == 5: # BP_IOCTL_SEC_SIZE return self.SEC_SIZE
class FlashBdev: SEC_SIZE = 4096 RESERVED_SECS = 1 START_SEC = esp.flash_user_start() // SEC_SIZE + RESERVED_SECS NUM_BLK = 0x6b - RESERVED_SECS def __init__(self, blocks=NUM_BLK): self.blocks = blocks def readblocks(self, n, buf, off=0): #print("readblocks(%s, %x(%d), %d)" % (n, id(buf), len(buf), off)) esp.flash_read((n + self.START_SEC) * self.SEC_SIZE + off, buf) def writeblocks(self, n, buf, off=None): #print("writeblocks(%s, %x(%d), %d)" % (n, id(buf), len(buf), off)) #assert len(buf) <= self.SEC_SIZE, len(buf) if off is None: esp.flash_erase(n + self.START_SEC) off = 0 esp.flash_write((n + self.START_SEC) * self.SEC_SIZE + off, buf) def ioctl(self, op, arg): #print("ioctl(%d, %r)" % (op, arg)) if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT return self.blocks if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE return self.SEC_SIZE if op == 6: # MP_BLOCKDEV_IOCTL_BLOCK_ERASE esp.flash_erase(arg + self.START_SEC) return 0
def flashinfo(display=True): """ Get flash informations """ try: import esp result = b"Flash user=%s size=%s" % (sizeToBytes( esp.flash_user_start(), 1), sizeToBytes(esp.flash_size(), 1)) if display: print(tostrings(result)) else: return result except: return b"Flash unavailable"
class sensors: profile_length = const(0x6A - 0x55 + 1) profile = [[bytearray(profile_length) for x in range(2)] for y in range(2)] stored_profile = [[bytearray(profile_length) for x in range(2)] for y in range(2)] profile_cal_status = [[bytearray(4) for x in range(2)] for y in range(2)] addr_start = esp.flash_user_start() data_list = [] cal_status = [[bytearray(4) for x in range(2)] for y in range(2)] not_cal_count = [[0 for x in range(2)] for y in range(2)] data = [[bytearray(11) for x in range(2)] for y in range(2)] found = [] imus = [] def __init__(self, pins): self.i2c = [] print("Starting Sensors") self.i2c.append( machine.I2C(0, scl=machine.Pin(pins[0]), sda=machine.Pin(pins[1]), freq=400000)) self.i2c.append( machine.I2C(1, scl=machine.Pin(pins[2]), sda=machine.Pin(pins[3]), freq=400000)) self.read_config_file() def read_config_file(self): try: file = open(file="cal.txt", mode="rb") except OSError: file = open(file="cal.txt", mode="w") file.close() file = open(file="cal.txt", mode="rb") #file.seek(0) for i in range(2): for j in range(2): file.readinto(self.stored_profile[i][j], self.profile_length) print(self.stored_profile[i][j]) for i in range(2): for j in range(2): file.readinto(self.profile_cal_status[i][j], len(self.profile_cal_status[i][j])) print(self.profile_cal_status[i][j]) file.close() def write_config_file(self): file = open(file="cal.txt", mode="wb+") #self.file.seek(0) for i in range(2): for j in range(2): file.write(self.stored_profile[i][j]) print(self.stored_profile[i][j]) for i in range(2): for j in range(2): file.write(self.profile_cal_status[i][j]) print(self.profile_cal_status[i][j]) file.close() def is_found(self, bus_list): ret = [] ret.append(1 if 0x28 in bus_list else 0) ret.append(1 if 0x29 in bus_list else 0) return ret def scan(self): self.found = [] self.found.append(self.is_found(self.i2c[0].scan())) self.found.append(self.is_found(self.i2c[1].scan())) print('Devices found: ', end='') print(self.found) self.imus = [] for i in range(2): imu_bus = [] for j in range(2): if self.found[i][j]: imu_bus.append( BNO055(i2c=self.i2c[i], address=0x28 if j == 0 else 0x29, calibration=self.stored_profile[i][j])) else: imu_bus.append(None) self.imus.append(imu_bus) def reconnect(self, i, j): try: #if not self.imus[i][j] is None: # del self.imus[i][j] print('Reset [' + str(i) + '],[' + str(j) + ']', end='') self.imus[i][j].reset() except RuntimeError: pass except OSError: pass def read_data(self): self.data_list.clear() for j in range(2): for i in range(2): if self.found[i][j]: try: self.data[i][j][0] = 0 # Transaction Type self.data[i][j][1] = i # Bus Number self.data[i][j][2] = j # Device Number self.data[i][j][3:11] = self.imus[i][j].raw_quaternion( ) #self.data[i][j][11:17] = self.imus[i][j].raw_gravity() self.data_list.append(self.data[i][j]) except OSError: print('X [' + str(i) + '],[' + str(j) + ']', end='') self.reconnect(i, j) continue def read_grav(self): self.data_list.clear() for j in range(2): for i in range(2): if self.found[i][j]: try: self.data[i][j][0] = 1 # Transaction Type self.data[i][j][1] = i # Bus Number self.data[i][j][2] = j # Device Number self.data[i][j][3:9] = self.imus[i][j].raw_gravity() self.data_list.append(self.data[i][j]) except OSError: print('X [' + str(i) + '],[' + str(j) + ']', end='') self.reconnect(i, j) continue def read_id(self): self.data_list.clear() machine_id = machine.unique_id() self.data[0][0][0] = 9 # Transaction Type self.data[0][0][1:11] = machine_id self.data_list.append(self.data[0][0]) def read_cal(self): for j in range(2): for i in range(2): if self.found[i][j]: try: self.cal_status[i][j] = self.imus[i][j].cal_status( self.cal_status[i][j]) except OSError: print("Error reading calibration status") def check_cal(self): self.read_cal() changed = False for j in range(2): for i in range(2): if self.found[i][j]: cal = self.cal_status[i][j] print("Cal: sys: " + str(cal[0]) + " gyro: " + str(cal[1]) + " acc: " + str(cal[2]) + " mag: " + str(cal[3]), end="") higher = 0 lower = 0 for k in range(len(self.profile_cal_status[i][j])): if self.cal_status[i][j][k] > self.profile_cal_status[ i][j][k]: higher += 1 if self.cal_status[i][j][k] < self.profile_cal_status[ i][j][k]: lower += 1 #print(str(self.cal_status[i][j]) + " vs " + str(self.profile_cal_status[i][j])) print(" Higher: " + str(higher) + " Lower: " + str(lower)) if (higher > 0 and lower == 0): self.profile[i][j] = self.imus[i][j].read_profile( self.profile[i][j]) for k in range(len(self.cal_status[i][j])): self.profile_cal_status[i][j][k] = self.cal_status[ i][j][k] for k in range(len(self.profile[i][j])): self.stored_profile[i][j][k] = self.profile[i][j][ k] changed = True if changed: print("Saving File") self.write_config_file()
async def send_response(self, reader, writer): peername = writer.get_extra_info('peername') print('\nRequest from:', peername) await writer.awrite(b'HTTP/1.0 200 OK\r\n') await writer.awrite(b'Content-type: text/html; charset=utf-8\r\n\r\n') await writer.awrite(_HTML_PREFIX) await writer.awrite(b'Your IP: %s port:%s\n' % peername) await writer.awrite(b'\n') method, url, querystring = await self.parse_request(reader) offset = int(querystring.get('offset', 0)) print('Current offset:', repr(offset)) search_text = querystring.get('text', '') print('search text:', repr(search_text)) await writer.awrite(b'<pre>') # await writer.awrite(b'Method: %s\n' % method) # await writer.awrite(b'URL: %s\n' % url) # await writer.awrite(b'querystring: %s\n' % querystring) await writer.awrite(b'flash size: {size} Bytes (0x{size:x})\n'.format( size=esp.flash_size() )) await writer.awrite(b'flash user start: {user_start} Bytes (0x{user_start:x})\n'.format( user_start=esp.flash_user_start() )) if search_text: await writer.awrite(b'search for: %r\n' % search_text) offset = await search(writer, offset, text=search_text.encode('UTF-8')) await writer.awrite(b'</pre>') await writer.awrite(b'<table id="hex">') await hex_dump(writer, offset=offset) await writer.awrite(b'</table>') back_offset = offset - (LINE_COUNT * CHUNK_SIZE) next_offset = offset + (LINE_COUNT * CHUNK_SIZE) print('back', back_offset, 'next', next_offset) await writer.awrite( b'<a href="/?offset={back}">back</a> <a href="/?offset={next}">next</a>'.format( back=back_offset, next=next_offset, ) ) alloc = gc.mem_alloc() / 1024 free = gc.mem_free() / 1024 await writer.awrite( b'<p>RAM total: {total:.2f} KB, used: {alloc:.2f} KB, free: {free:.2f} KB</p>'.format( total=alloc + free, alloc=alloc, free=free ) ) await writer.awrite( b'<p>Render time: %i ms</p>' % utime.ticks_diff(utime.ticks_ms(), self.start_time) ) await writer.awrite(_HTML_FOOTER.format(text=search_text)) await writer.aclose()
def readblocks(self, n, buf): # print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf) def writeblocks(self, n, buf): # print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf))) # assert len(buf) <= self.SEC_SIZE, len(buf) esp.flash_erase(n + self.START_SEC) esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf) def ioctl(self, op, arg): # print("ioctl(%d, %r)" % (op, arg)) if op == 4: # BP_IOCTL_SEC_COUNT return self.blocks if op == 5: # BP_IOCTL_SEC_SIZE return self.SEC_SIZE size = esp.flash_size() vfs = esp.partition_find_first(esp.PARTITION_TYPE_DATA, esp.PARTITION_SUBTYPE_DATA_FAT, "vfs") if size < 1024 * 1024: # flash too small for a filesystem bdev = None elif vfs is not None: bdev = PartitionBdev(vfs, esp.SPI_FLASH_SEC_SIZE) else: # for now we use a fixed size for the filesystem bdev = FlashBdev(esp.flash_user_start(), size, esp.SPI_FLASH_SEC_SIZE)
if text in BUFFER: print('Found in 0x%x !' % offset, end=' ') print_throughput() return offset if utime.time() >= next_update: print('Search: 0x%x ...' % offset, end=' ') print_throughput() next_update = utime.time() + 1 if end_researched: print('Memory end researched, searched up to 0x%x' % (offset + CHUNK_SIZE)) print_throughput() return -1 offset += offset_step if __name__ == '__main__': print('esp.flash_user_start(): 0x%x' % (esp.flash_user_start())) print('esp.flash_size()......: 0x%x' % (esp.flash_size())) offset = 0x0 while True: offset = search(offset=offset, text='micropython') if offset == -1: # not found or end researched break hex_dump(offset=offset) offset += (CHUNK_SIZE * LINE_COUNT) - 1
async def index(request, response, args): """ Function define the web page to display all informations of the board """ try: allocated = gc.mem_alloc() freed = gc.mem_free() memAlloc = useful.sizeToBytes(allocated) memFree = useful.sizeToBytes(freed) memTotal = useful.sizeToBytes(allocated + freed) except: memAlloc = b"Unvailable" memFree = b"Unvailable" memTotal = b"Unvailable" try: flashUser = useful.sizeToBytes(esp.flash_user_start()) flashSize = useful.sizeToBytes(esp.flash_size()) except: flashUser = b"Unavailable" flashSize = b"Unavailable" try: frequency = b"%d" % (machine.freq() // 1000000) except: frequency = b"Unvailable" try: platform = useful.tobytes(sys.platform) except: platform = b"Unavailable" date = useful.dateToBytes() page = mainPage( content=[Br(),Container([\ Card([\ Form([\ Br(), Edit(text=b"Date", value=date, disabled=True), Edit(text=b"Platform", value=platform, disabled=True), Edit(text=b"Frequency", value=frequency, disabled=True), Edit(text=b"Memory free", value=memFree, disabled=True), Edit(text=b"Memory allocated", value=memAlloc, disabled=True), Edit(text=b"Memory total", value=memTotal, disabled=True), Edit(text=b"Flash user", value=flashUser, disabled=True), Edit(text=b"Flash size", value=flashSize, disabled=True), Tag(b''' <button onclick="confirmReboot()" class="btn btn-outline-primary ">Reboot</button> <script> function confirmReboot() { if (confirm("Confirm reboot")) { var xhttp = new XMLHttpRequest(); xhttp.open("GET","reboot",true); xhttp.send(); } } </script>''') ]) ]) ]) ], title=args["title"], active=args["index"], request=request, response=response) await response.sendPage(page)
print(titulo) print("-" * len(titulo)) print("Nombre del sistema:" + os.uname().sysname) print("Nombre del nodo:" + os.uname().nodename) print("Lanzamiento:" + os.uname().release) print("version:" + os.uname().version) print("Maquina:" + os.uname().machine) print("Frecuencia de relog del procesador:" + str(machine.freq() / 1000000) + " MHz") gc.collect() titulo = "\nDatos de memoria" print(titulo) print("-" * len(titulo)) print("Memoria RAM disponible:" + str(gc.mem_free() / 1024) + " Kbytes") print("Memoria flash:" + str(esp.flash_size() / 1024) + " Kbytes") print("Memoria flash del usuario:" + str(esp.flash_user_start() / 1024) + " Kbytes") titulo = "\nDatos de red" print(titulo) print("-" * len(titulo)) if sta_if.active(): print("Wifi activa:" + sta_if.ifconfig()[0]) print("Mac:" + str(sta_if.config('mac'))) if sta_if.isconnected(): print("Conectada") print("Essid:" + sta_if.config('essid')) else: print("Desconectada") else: print("Wifi inactiva") if ap_if.active():
def writeblocks(self, n, buf, off=None): # print("writeblocks(%s, %x(%d), %d)" % (n, id(buf), len(buf), off)) # assert len(buf) <= self.SEC_SIZE, len(buf) if off is None: esp.flash_erase(n + self.start_sec) off = 0 esp.flash_write((n + self.start_sec) * self.SEC_SIZE + off, buf) def ioctl(self, op, arg): # print("ioctl(%d, %r)" % (op, arg)) if op == 4: # MP_BLOCKDEV_IOCTL_BLOCK_COUNT return self.blocks if op == 5: # MP_BLOCKDEV_IOCTL_BLOCK_SIZE return self.SEC_SIZE if op == 6: # MP_BLOCKDEV_IOCTL_BLOCK_ERASE esp.flash_erase(arg + self.start_sec) return 0 size = esp.flash_size() if size < 1024 * 1024: bdev = None else: start_sec = esp.flash_user_start() // FlashBdev.SEC_SIZE if start_sec < 256: start_sec += 1 # Reserve space for native code # 20K at the flash end is reserved for SDK params storage bdev = FlashBdev(start_sec, (size - 20480) // FlashBdev.SEC_SIZE - start_sec)
# The esp module: import esp esp.osdebug(None) # turn off vendor O/S debugging messages esp.osdebug(0) # redirect vendor O/S debugging messages to UART(0) sector_no = 1 # Placeholders byte_offset = 0 buffer = [0] # low level methods to interact with flash storage esp.flash_size() esp.flash_user_start() esp.flash_erase(sector_no) esp.flash_write(byte_offset, buffer) esp.flash_read(byte_offset, buffer) # The esp32 module: import esp32 esp32.hall_sensor() # read the internal hall sensor esp32.raw_temperature( ) # read the internal temperature of the MCU, in Fahrenheit esp32.ULP() # access to the Ultra-Low-Power Co-processor # RMT import esp32 from machine import Pin