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 save(self): """Save config (all sections) into file. Returns current size of config """ esp.flash_erase(CONFIG_BLOCK) meta = bytearray(3) sector = bytearray() for k, v in self.__dict__.items(): # skip class variables if k.startswith('_'): continue gc.collect() # metadata, bytes: # 0: name len # 1: type # 2: chksum (sum of name + type) # Param name len meta[0] = len(k) if isinstance(v, int): # type int meta[1] = 1 data = v.to_bytes(4, 'big') elif isinstance(v, str): # type str vlen = len(v) if vlen > 255: raise ConfigError('Too big: {}'.format(k)) meta[1] = 2 data = bytearray(vlen + 1) data[0] = vlen data[1:] = v.encode() elif isinstance(v, bool): # type bool meta[1] = 3 data = bytearray(1) data[0] = int(v) elif v is None: # None data = None meta[1] = 4 else: raise ConfigError("Unsupported type {}".format(type(v))) meta[2] = sum(meta[:2]) sector += meta sector += k.encode() # add data, if non zero / none if data is not None and len(data) > 0: sector += data sec_len = len(sector) if sec_len > 4000: raise ConfigError('Too large') # Add padding - esp.flash_write() requires block to be modulo 4 if sec_len % 4 != 0: sector += b'\xff' * (4 - sec_len % 4) esp.flash_write(CONFIG_BLOCK * BLOCK_SIZE, sector) gc.collect() return len(sector)
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 set_adc_vcc(): sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) data[107] = 255 esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) machine.reset()
def write_partition_chunk(self, buffer, id): chunkspersec = (self.SEC_SIZE // self.CHUNK_SIZE) if id % chunkspersec == 0: esp.flash_erase(self.next_boot_part_base_sec + id // chunkspersec) esp.flash_write( self.partitions[self.next_boot_partition][2] + self.CHUNK_SIZE * id, buffer) return len(buffer)
def set_adc_mode(mode): sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) if data[107] == mode: return # flash is already correct; nothing to do else: data[107] = mode # re-write flash esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) print("ADC mode changed in flash; restart to use it!") return
def setup(): print("Performing initial setup") filesystem = detect_filesystem() print('Detected filesystem: %r' % filesystem) if filesystem != FS_LITTLEFS: print('Erase flash start sector 0x%x' % flashbdev.bdev.START_SEC) esp.flash_erase(flashbdev.bdev.START_SEC) print('convert to littlefs2') uos.VfsLfs2.mkfs(flashbdev.bdev) print('mount filesystem') uos.mount(flashbdev.bdev, '/') with open("boot.py", "w") as f: f.write("""\ print('boot.py') # noqa isort:skip import gc import sys import esp import micropython import utime for no in range(2, 0, -1): print('%i boot.py wait...' % no) utime.sleep(1) esp.osdebug(None) # turn off vendor O/S debugging messages esp.sleep_type(esp.SLEEP_NONE) # Don't go into sleep mode micropython.alloc_emergency_exception_buf(128) gc.enable() # https://forum.micropython.org/viewtopic.php?f=2&t=7345&p=42390#p42390 gc.threshold(8192) # default is: # sys.path=['', '/lib', '/'] # But we would like to be possible to overwrite frozen modues with .mpy on flash drive ;) sys.path.insert(0, '.') print('sys.path=%r' % sys.path) print('boot.py END') """)
def convert_filesystem2littlefs(force=False): filesystem_hex_dump(line_count=5, chunk_size=16) filesystem = detect_filesystem() print('Detected filesystem: %r' % filesystem) if force is True or filesystem != FS_LITTLEFS: print('Erase sector 0x%x' % flashbdev.bdev.START_SEC) esp.flash_erase(flashbdev.bdev.START_SEC) print('\nconvert to littlefs2...\n') # only on ESP8266 and ESP32 uos.VfsLfs2.mkfs(flashbdev.bdev) filesystem_hex_dump(line_count=5, chunk_size=16) print('Detected filesystem:', detect_filesystem()) else: print('No need to format the flash filesystem, ok.')
def set_adc_mode(mode): sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) if data[107] == mode: return # flash is already correct; nothing to do else: print( "*** Going to adjust the ADC type in the flashed memory" ); print( "*** You will need to hit the 'RST' button, near the USB port" ); print( "*** when it's done" ); data[107] = mode # re-write flash esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) print("*** ADC mode changed in flash; restart to use it!") return
def set_adc_mode(adc=0): ADC_MODE_ADC = 0 ADC_MODE_VCC = 255 mode = {0: ADC_MODE_ADC, 1: ADC_MODE_VCC} sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) if data[107] == mode[adc]: print("ADC mode is already set in flash!") return else: data[107] = mode[adc] # re-write flash esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) print("ADC mode has been changed in flash; restart to use it!") return
def set_bl_flash_size(real_size): if real_size == 256 * 1024: code = 1 elif real_size == 512 * 1024: code = 0 elif real_size == 1024 * 1024: code = 2 elif real_size == 2048 * 1024: code = 3 elif real_size == 4096 * 1024: code = 4 else: code = 2 buf = bytearray(4096) esp.flash_read(0, buf) buf[3] = (buf[3] & 0xf) | (code << 4) esp.flash_erase(0) esp.flash_write(0, buf)
def adc_mode(mode=None): import flashbdev sector_size = flashbdev.bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) current_mode = data[107] if mode is None: # Read adc mode return current_mode if current_mode == mode: print("ADC mode already {}.".format(mode)) else: data[107] = mode # re-write flash esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) print("ADC mode is now {}. Reset to apply.".format(mode)) return mode
def set_bl_flash_size(real_size): if real_size == 256*1024: code = 1 elif real_size == 512*1024: code = 0 elif real_size == 1024*1024: code = 2 elif real_size == 2048*1024: code = 3 elif real_size == 4096*1024: code = 4 else: code = 2 buf = bytearray(4096) esp.flash_read(0, buf) buf[3] = (buf[3] & 0xf) | (code << 4) esp.flash_erase(0) esp.flash_write(0, buf)
def set_adc_mode(mode): sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) try: data = bytearray( esp.flash_read(init_sector * sector_size, sector_size)) if data[107] == mode: return True # flash is already correct; nothing to do else: data[107] = mode # re-write flash esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) return False except MemoryError: log = logger.get_logger() log.log_msg( logger.ERROR, "Could not allocate memory for setting adc mode, assuming it's {}" .format(_num_to_mode[mode])) return True
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 writeblocks(self, n, buf): esp.flash_erase(n + self.start) esp.flash_write((n + self.start) * SEC_SIZE, buf)
import esp import machine from flashbdev import bdev sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) data[107] = 255 esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) machine.reset() # from machine import ADC # ADC(1).read()
# 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