def init(self, baud=115200, stopbits=1, parity="none"): """ Open the serial port, set baud rate, parity, etc. """ if stopbits == 1: stopbits = 0 elif stopbits == 1.5: stopbits = 1 elif stopbits == 2: stopbits = 2 else: raise ValueError("Invalid stop-bit spec: %s" % str(stopbits)) if parity == "none": parity = 0 elif parity == "odd": parity = 1 elif parity == "even": parity = 2 elif parity == "mark": parity = 3 elif parity == "space": parity = 4 else: raise ValueError("Invalid parity spec: %s" % str(parity)) cmdbuf = packuint32(baud) cmdbuf.append(stopbits) cmdbuf.append(parity) cmdbuf.append(8) # Data bits self._usartTxCmd(self.USART_CMD_INIT, cmdbuf) self._usartTxCmd(self.USART_CMD_ENABLE)
def readMemory(self, addr, dlen, memname="flash"): """ Read memory such as FLASH or EEPROM. Can specify an arbitrary length of data. Args: addr (int): Address to read from. dlen (in): How much data to read. Kwargs: memname (str): Type of memory, such as "flash" or "eeprom". Defaults to 'flash', but this will often work for other memory types. Returns: list. Raises: IOError """ memread = 0 endptsize = 64 # start = 0 # end = endptsize self._avrDoWrite(self.ISP_CMD_LOAD_ADDRESS, data=packuint32(addr)) membuf = [] while memread < dlen: # Read into internal buffer ramreadln = dlen - memread # Check if maximum size for internal buffer if ramreadln > self.MAX_BUFFER_SIZE: ramreadln = self.MAX_BUFFER_SIZE self._avrDoWrite(self.ISP_CMD_READ_FLASH_ISP, data=[0x00, 0x01, 0x20]) epread = 0 # First we need to fill the page buffer in the USB Interface using smaller transactions while epread < ramreadln: epreadln = ramreadln - epread if epreadln > endptsize: epreadln = endptsize # Read data out progressively membuf.extend( self._avrDoRead(self.ISP_CMD_GET_RAMBUF | (epread << 8), dlen=epreadln)) # print epread epread += epreadln memread += ramreadln return membuf
def setParamTimeout(self, timeoutMS): """ Set timeout for executing PDI commands, sets timeout both on NAEUSB chip and host-side USB API. """ self._timeout = timeoutMS + 50 timeoutticks = int((float(timeoutMS) / 1000.0) * 2500.0) pload = [self.XPROG_PARAM_TIMEOUT] pload.extend(packuint32(timeoutticks)) self._xmegaDoWrite(self.XPROG_CMD_SET_PARAM, pload)
def readMemory(self, addr, dlen, memname="flash"): """ Read memory such as FLASH or EEPROM. Can specify an arbitrary length of data. Args: addr (int): Address to read from. dlen (in): How much data to read. Kwargs: memname (str): Type of memory, such as "flash" or "eeprom". Defaults to 'flash', but this will often work for other memory types. Returns: list. Raises: IOError """ memread = 0 endptsize = 64 # start = 0 # end = endptsize self._avrDoWrite(self.ISP_CMD_LOAD_ADDRESS, data=packuint32(addr)) membuf = [] while memread < dlen: # Read into internal buffer ramreadln = dlen - memread # Check if maximum size for internal buffer if ramreadln > self.MAX_BUFFER_SIZE: ramreadln = self.MAX_BUFFER_SIZE self._avrDoWrite(self.ISP_CMD_READ_FLASH_ISP, data=[0x00, 0x01, 0x20]) epread = 0 # First we need to fill the page buffer in the USB Interface using smaller transactions while epread < ramreadln: epreadln = ramreadln - epread if epreadln > endptsize: epreadln = endptsize # Read data out progressively membuf.extend(self._avrDoRead(self.ISP_CMD_GET_RAMBUF | (epread << 8), dlen=epreadln)) # print epread epread += epreadln memread += ramreadln return membuf
def writeMemory(self, addr, data, memname, erasePage=False, programPage=True): """ Write memory such as FLASH or EEPROM. Can specify an arbitrary length of data. Args: addr (int): Address to write at, should be page aligned if writing paged memory! data (list): Data to write. memname (str): Type of memory, such as "flash" or "eeprom". Kwargs: erasePage (bool): Should we perform a page erase before writing? Defaults to FALSE. programPage (bool): Should we perform a page write once memory copied? Defaults to TRUE. If writing internal RAM set this to FALSE, but for writing FLASH/ EEPROM leave as TRUE. Raises: IOError """ self.validate_mode() PAGEMODE_WRITE = (1 << 1) PAGEMODE_ERASE = (1 << 0) memspec = self._chip.memtypes[memname] memwritten = 0 endptsize = 64 start = 0 end = endptsize pagesize = memspec["pagesize"] if addr % pagesize: logging.warning( 'You appear to be writing to an address that is not page aligned, you will probably write the wrong data' ) while memwritten < len(data): epwritten = 0 # First we need to fill the page buffer in the USB Interface using smaller transactions while epwritten < pagesize: # Check for less than full endpoint written if end > len(data): end = len(data) # Get slice of data epdata = data[start:end] # print "%d %d %d" % (epwritten, len(epdata), memwritten) # Copy to USB interface buffer self._xmegaDoWrite(self.XPROG_SET_RAMBUF | (epwritten << 8), data=epdata, checkStatus=False) epwritten += len(epdata) # Check for final write indicating we are done if end == len(data): break start += endptsize end += endptsize # Copy internal buffer to final location (probably FLASH memory) if not ("type" in memspec.keys()): raise IOError( "Write on memory type that doesn't have 'type', probably read-only?" ) # Do write into memory type infoblock = [memspec["type"], 0] if programPage: infoblock[1] |= PAGEMODE_WRITE if erasePage: infoblock[1] |= PAGEMODE_ERASE infoblock.extend(packuint32(addr + memwritten)) infoblock.append(epwritten & 0xff) infoblock.append((epwritten >> 8) & 0xff) # print "%x" % (addr + memwritten) # print epwritten self._xmegaDoWrite(self.XPROG_CMD_WRITE_MEM, data=infoblock) memwritten += epwritten
def readMemory(self, addr, dlen, memname="flash"): """ Read memory such as FLASH or EEPROM. Can specify an arbitrary length of data. Args: addr (int): Address to read from. dlen (in): How much data to read. Kwargs: memname (str): Type of memory, such as "flash" or "eeprom". Defaults to 'flash', but this will often work for other memory types. Returns: list. Raises: IOError """ self.validate_mode() memspec = self._chip.memtypes[memname] memread = 0 endptsize = 64 # start = 0 # end = endptsize if "readsize" in memspec.keys(): readsize = memspec["readsize"] elif "pagesize" in memspec.keys(): readsize = memspec["pagesize"] else: readsize = dlen membuf = [] while memread < dlen: #Read into internal buffer ramreadln = dlen - memread # Check if maximum size for memory type if ramreadln > readsize: ramreadln = readsize # Check if maximum size for internal buffer if ramreadln > self.MAX_BUFFER_SIZE: ramreadln = self.MAX_BUFFER_SIZE infoblock = [0] # memspec["type"] infoblock.extend(packuint32(addr + memread)) infoblock.append(ramreadln & 0xff) infoblock.append((ramreadln >> 8) & 0xff) self._xmegaDoWrite(self.XPROG_CMD_READ_MEM, data=infoblock) epread = 0 # First we need to fill the page buffer in the USB Interface using smaller transactions while epread < ramreadln: epreadln = ramreadln - epread if epreadln > endptsize: epreadln = endptsize # Read data out progressively membuf.extend( self._xmegaDoRead(self.XPROG_GET_RAMBUF | (epread << 8), dlen=epreadln)) # print epread epread += epreadln memread += ramreadln return membuf
def writeMemory(self, addr, data, memname, erasePage=False, programPage=True): """ Write memory such as FLASH or EEPROM. Can specify an arbitrary length of data. Args: addr (int): Address to write at, should be page aligned if writing paged memory! data (list): Data to write. memname (str): Type of memory, such as "flash" or "eeprom". Kwargs: erasePage (bool): Should we perform a page erase before writing? Defaults to FALSE. programPage (bool): Should we perform a page write once memory copied? Defaults to TRUE. If writing internal RAM set this to FALSE, but for writing FLASH/ EEPROM leave as TRUE. Raises: IOError """ PAGEMODE_WRITE = (1 << 1) PAGEMODE_ERASE = (1 << 0) memspec = self._chip.memtypes[memname] memwritten = 0 endptsize = 64 start = 0 end = endptsize pagesize = memspec["pagesize"] if addr % pagesize: logging.warning('You appear to be writing to an address that is not page aligned, you will probably write the wrong data') while memwritten < len(data): epwritten = 0 # First we need to fill the page buffer in the USB Interface using smaller transactions while epwritten < pagesize: # Check for less than full endpoint written if end > len(data): end = len(data) # Get slice of data epdata = data[start:end] # print "%d %d %d" % (epwritten, len(epdata), memwritten) # Copy to USB interface buffer self._xmegaDoWrite(self.XPROG_SET_RAMBUF | (epwritten << 8), data=epdata, checkStatus=False) epwritten += len(epdata) # Check for final write indicating we are done if end == len(data): break start += endptsize end += endptsize # Copy internal buffer to final location (probably FLASH memory) if not ("type" in memspec.keys()): raise IOError("Write on memory type that doesn't have 'type', probably read-only?") # Do write into memory type infoblock = [memspec["type"], 0] if programPage: infoblock[1] |= PAGEMODE_WRITE if erasePage: infoblock[1] |= PAGEMODE_ERASE infoblock.extend(packuint32(addr + memwritten)) infoblock.append(epwritten & 0xff) infoblock.append((epwritten >> 8) & 0xff) # print "%x" % (addr + memwritten) # print epwritten self._xmegaDoWrite(self.XPROG_CMD_WRITE_MEM, data=infoblock) memwritten += epwritten
def readMemory(self, addr, dlen, memname="flash"): """ Read memory such as FLASH or EEPROM. Can specify an arbitrary length of data. Args: addr (int): Address to read from. dlen (in): How much data to read. Kwargs: memname (str): Type of memory, such as "flash" or "eeprom". Defaults to 'flash', but this will often work for other memory types. Returns: list. Raises: IOError """ memspec = self._chip.memtypes[memname] memread = 0 endptsize = 64 # start = 0 # end = endptsize if "readsize" in memspec.keys(): readsize = memspec["readsize"] elif "pagesize" in memspec.keys(): readsize = memspec["pagesize"] else: readsize = dlen membuf = [] while memread < dlen: #Read into internal buffer ramreadln = dlen - memread # Check if maximum size for memory type if ramreadln > readsize: ramreadln = readsize # Check if maximum size for internal buffer if ramreadln > self.MAX_BUFFER_SIZE: ramreadln = self.MAX_BUFFER_SIZE infoblock = [0] # memspec["type"] infoblock.extend(packuint32(addr + memread)) infoblock.append(ramreadln & 0xff) infoblock.append((ramreadln >> 8) & 0xff) self._xmegaDoWrite(self.XPROG_CMD_READ_MEM, data=infoblock) epread = 0 # First we need to fill the page buffer in the USB Interface using smaller transactions while epread < ramreadln: epreadln = ramreadln - epread if epreadln > endptsize: epreadln = endptsize # Read data out progressively membuf.extend(self._xmegaDoRead(self.XPROG_GET_RAMBUF | (epread << 8), dlen=epreadln)) # print epread epread += epreadln memread += ramreadln return membuf