def deepSleep(self):
        """
		Put the memory in deep sleep mode.
		
		
		:param none: 
		:returns: none
		"""
        self.CE_SELECT()  #enable device
        spi.writebytes([CMD_DEEPSLP])  #  select write to status register
        self.CE_DESELECT()  # disable the device
        return 0
    def wakeUp(self):
        """
		Wake up from deep sleep mode.
		
		
		:param none: 
		:returns: none
		"""
        self.CE_SELECT()  # enable device
        spi.writebytes([CMD_RESDPD])  # select write to status register
        self.CE_DESELECT()  # disable the device
        return 0
	def deepSleep(self):
		"""
		Put the memory in deep sleep mode.
		
		
		:param none: 
		:returns: none
		"""
		self.CE_SELECT()                           #enable device
		spi.writebytes([CMD_DEEPSLP])             #  select write to status register
		self.CE_DESELECT()                        # disable the device
		return 0
	def wakeUp(self):
		"""
		Wake up from deep sleep mode.
		
		
		:param none: 
		:returns: none
		"""
		self.CE_SELECT()		                    # enable device
		spi.writebytes([CMD_RESDPD])                # select write to status register
		self.CE_DESELECT()                        # disable the device
		return 0
	def jedec_ID_Read(self):
		"""
		Flash memory chip class.
		
		
		:param none: 
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([JEDEC])                           #Send JEDEC ID command (9Fh)    
		jedec = spi.readbytes(3)
		self.CE_DESELECT()
		jedid = int((jedec[0] << 16) | (jedec[1] << 8) | jedec[2])
		return jedid
    def readStatusRegister(self, statReg):
        """
		Read the contents of the status register.
		
		
		:param statReg:  Address of one of the status registers
		:returns: stat - Status register contents
		"""
        self.CE_SELECT()
        spi.writebytes([statReg])  # send RDSR command
        byte = spi.readbytes(1)  # receive byte
        self.CE_DESELECT()

        return byte
	def readStatusRegister(self,statReg):
		"""
		Read the contents of the status register.
		
		
		:param statReg:  Address of one of the status registers
		:returns stat:  Status register contents
		"""
		self.CE_SELECT()
		spi.writebytes([statReg])                      # send RDSR command
		byte = spi.readbytes(1)                      # receive byte
		self.CE_DESELECT()
		
		return byte
    def jedec_ID_Read(self):
        """
		Flash memory chip class.
		
		
		:param none: 
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([JEDEC])  #Send JEDEC ID command (9Fh)
        jedec = spi.readbytes(3)
        self.CE_DESELECT()
        jedid = int((jedec[0] << 16) | (jedec[1] << 8) | jedec[2])
        return jedid
	def chipErase(self):
		"""
		Flash memory chip class.
		
		
		:param none: 
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_WREN])                   #Send WREN command
		self.CE_DESELECT()
		
		self.CE_SELECT()
		spi.writebytes([ERASE_CHIP])                 #Send Sector Erase command
		self.CE_DESELECT()
    def setBlockProtection(self, prot):
        """
		Set the contents of a specific block as protected.
		
		
		:param prot: 	Specific block
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_EWSR])  #enable write to volatile register
        self.CE_DESELECT()

        self.CE_SELECT()
        spi.writebytes([CMD_WRSR, (prot & 0x0f) << 2])  #write status register
        self.CE_DESELECT()
	def sectorErase(self,address):
		"""
		Erase a sector from the flash memory chip.
		
		
		:param address: Sector address
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_WREN])                    #Send WREN command
		self.CE_DESELECT()
		
		self.CE_SELECT()
		spi.writebytes([ERASE_SECTOR,(address&0xFF0000)>>16,(address&0xFF00)>>8,address&0xFF,0])                #Send Sector Erase command
		self.CE_DESELECT()
	def blockErase(self,address):
		"""
		Erase a block from the flash memory chip.
		
		
		:param address:  Block initial address
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_WREN])                      #Send WREN command
		self.CE_DESELECT()
		data = [hex(address >> i & 0xff) for i in (16,8,0)]
		self.CE_SELECT()
		spi.writebytes([ERASE_BLOCK64,data])                 #Send Sector Erase command
		self.CE_DESELECT()
    def blockErase(self, address):
        """
		Erase a block from the flash memory chip.
		
		
		:param address:  Block initial address
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_WREN])  #Send WREN command
        self.CE_DESELECT()
        data = [hex(address >> i & 0xff) for i in (16, 8, 0)]
        self.CE_SELECT()
        spi.writebytes([ERASE_BLOCK64, data])  #Send Sector Erase command
        self.CE_DESELECT()
    def chipErase(self):
        """
		Flash memory chip class.
		
		
		:param none: 
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_WREN])  #Send WREN command
        self.CE_DESELECT()

        self.CE_SELECT()
        spi.writebytes([ERASE_CHIP])  #Send Sector Erase command
        self.CE_DESELECT()
	def readByte(self,address):
		"""
		Read one byte from the given memory address.
		
		
		:param address: Address of data in memory.
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_READ_DATA])
		spi.writebytes([(address&0xFF0000)>>16,(address&0xFF00)>>8,address&0xFF,0])	#Read command,Send 3 address bytes
		mybyte = spi.readbytes(1)
		self.CE_DESELECT()
		
		return mybyte	#Return one byte read
	def setBlockProtection(self,prot):
		"""
		Set the contents of a specific block as protected.
		
		
		:param prot: 	Specific block
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_EWSR])		#enable write to volatile register
		self.CE_DESELECT()

		self.CE_SELECT()
		spi.writebytes([CMD_WRSR,(prot & 0x0f)<<2])		#write status register
		self.CE_DESELECT()
    def readByte(self, address):
        """
		Read one byte from the given memory address.
		
		
		:param address: Address of data in memory.
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_READ_DATA])
        spi.writebytes([(address & 0xFF0000) >> 16, (address & 0xFF00) >> 8,
                        address & 0xFF, 0])  #Read command,Send 3 address bytes
        mybyte = spi.readbytes(1)
        self.CE_DESELECT()

        return mybyte  #Return one byte read
	def readArray(self,address, arrayLength):
		"""
		Flash memory chip class.
		
		
		:param address: Starting address of array
		:param arrayLength: Array length in bytes
		:returns: none
		"""
		pData = []
		self.CE_SELECT()
		spi.writebytes([CMD_READ_DATA,(address&0xFF0000)>>16,(address&0xFF00)>>8,address&0xFF,0])                       #Read command
		pData = spi.readbytes(arrayLength)
		self.CE_DESELECT()
		
		return pData
	def isWriteBusy(self):
		"""
		Check if a write is in process.
		
		
		:param none: 
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_RDSR1])                   #Send RDSR command
		temp = spi.read(1)
		self.CE_DESELECT()
		
		if (temp and 0x01):
			return 1
		else: 
			return 0
    def isWriteBusy(self):
        """
		Check if a write is in process.
		
		
		:param none: 
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_RDSR1])  #Send RDSR command
        temp = spi.read(1)
        self.CE_DESELECT()

        if (temp and 0x01):
            return 1
        else:
            return 0
    def sectorErase(self, address):
        """
		Erase a sector from the flash memory chip.
		
		
		:param address: Sector address
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_WREN])  #Send WREN command
        self.CE_DESELECT()

        self.CE_SELECT()
        spi.writebytes([
            ERASE_SECTOR, (address & 0xFF0000) >> 16, (address & 0xFF00) >> 8,
            address & 0xFF, 0
        ])  #Send Sector Erase command
        self.CE_DESELECT()
Exemple #22
0
	def send(self, data, dataOrCmd=True, length=4096):
		"""
		Writes a byte or array of bytes to the display. 
		dataOrCmd parameter controls if byte should be interpreted as display data (True)/ command data otherwise.  
		Length is an optional size of bytes to write in a single SPI transaction, with a default of 4096. 
		:param data: Single byte or an array of bytes.
		:param dataOrCmd: Flag for command or data mode
		:param length: size of array
		:returns none :
		"""
		# Set DC low for command, high for data.
		GPIO.output(self.dc, dataOrCmd)
		# Convert scalar argument to list so either can be passed as parameter.
		if isinstance(data, numbers.Number):
			data = [data & 0xFF]
		# Write data a chunk at a time.
		for start in range(0, len(data), length):
			end = min(start+length, len(data))
			spi.writebytes(data[start:end])
    def readArray(self, address, arrayLength):
        """
		Flash memory chip class.
		
		
		:param address: Starting address of array
		:param arrayLength: Array length in bytes
		:returns: none
		"""
        pData = []
        self.CE_SELECT()
        spi.writebytes([
            CMD_READ_DATA, (address & 0xFF0000) >> 16, (address & 0xFF00) >> 8,
            address & 0xFF, 0
        ])  #Read command
        pData = spi.readbytes(arrayLength)
        self.CE_DESELECT()

        return pData
    def writeArray(self, address, pData, arrayLength):
        """
		Write an array to the memory chip.
		
		
		:param address: Address of data in memory.
		:param pData: 
		:param arrayLength:
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_WREN])  #Send WREN command
        self.CE_DESELECT()

        self.CE_SELECT()
        spi.writebytes([
            PAGE_PROGRAM, (address & 0xFF0000) >> 16, (address & 0xFF00) >> 8,
            address & 0xFF, 0
        ])  #Send Byte

        for item in range(len(pData)):
            data = ord(
                pData[item]
            )  #this is a hack ,convert char data to hex and write bytes individually, slow
            spi.writebytes([data])

        self.CE_DESELECT()
    def readID(self):
        """
		Flash memory chip class.
		
		
		:param none: 
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_RDID])  #Send read ID command (90h or ABh)
        spi.writebytes([0x00])  #Send dummy byte
        spi.writebytes([0x00])  #Send dummy byte
        spi.writebytes([0x00])  #Send 0x00
        id = spi.readbytes(2)  #Send either manufacturer ID or device ID
        self.CE_DESELECT()
        return ((id[1] << 8) | id[0])
	def	readID(self):
		"""
		Flash memory chip class.
		
		
		:param none: 
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_RDID])                #Send read ID command (90h or ABh)
		spi.writebytes([0x00])                    #Send dummy byte
		spi.writebytes([0x00])                    #Send dummy byte
		spi.writebytes([0x00])                    #Send 0x00
		id = spi.readbytes(2)                #Send either manufacturer ID or device ID
		self.CE_DESELECT()
		return ((id[1] << 8) | id[0])
	def writeByte(self,data,address):
		"""
		Write one byte at a specific address in the memory chip.
		
		
		:param data: 
		:param address: 
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_WREN])                  #Send WREN command
		self.CE_DESELECT()
		
		self.CE_SELECT()
		spi.writebytes([PAGE_PROGRAM])
		spi.writebytes([(address&0xFF0000)>>16,(address&0xFF00)>>8,address&0xFF,0])
		spi.writebytes([data])  					#Program page, and send 3 address bytes and data
		self.CE_DESELECT()
    def writeByte(self, data, address):
        """
		Write one byte at a specific address in the memory chip.
		
		
		:param data: 
		:param address: 
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_WREN])  #Send WREN command
        self.CE_DESELECT()

        self.CE_SELECT()
        spi.writebytes([PAGE_PROGRAM])
        spi.writebytes([(address & 0xFF0000) >> 16, (address & 0xFF00) >> 8,
                        address & 0xFF, 0])
        spi.writebytes([data
                        ])  #Program page, and send 3 address bytes and data
        self.CE_DESELECT()
	def writeStatusRegister(self,statReg):
		"""
		Write a new value to the contents of the status register
		
		:param statReg:  Address of one of the status registers
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_WREN])                    #Send WREN command
		self.CE_DESELECT()
		
		self.CE_SELECT()
		spi.writebytes([CMD_WRSR])                    # select write to status register
		spi.writebytes([statReg])                     # data that will change the status of BPx or BPL (only bits 2,3,4,5,7 can be written)
		self.CE_DESELECT()
    def writeStatusRegister(self, statReg):
        """
		Write a new value to the contents of the status register
		
		:param statReg:  Address of one of the status registers
		:returns: none
		"""
        self.CE_SELECT()
        spi.writebytes([CMD_WREN])  #Send WREN command
        self.CE_DESELECT()

        self.CE_SELECT()
        spi.writebytes([CMD_WRSR])  # select write to status register
        spi.writebytes(
            [statReg]
        )  # data that will change the status of BPx or BPL (only bits 2,3,4,5,7 can be written)
        self.CE_DESELECT()
	def writeArray(self,address, pData, arrayLength):
		"""
		Write an array to the memory chip.
		
		
		:param address: Address of data in memory.
		:param pData: 
		:param arrayLength:
		:returns: none
		"""
		self.CE_SELECT()
		spi.writebytes([CMD_WREN])                   #Send WREN command
		self.CE_DESELECT()

		self.CE_SELECT()
		spi.writebytes([PAGE_PROGRAM,(address&0xFF0000)>>16,(address&0xFF00)>>8,address&0xFF,0])               #Send Byte 
		
		for item in range(len(pData)):
			data = ord(pData[item])			#this is a hack ,convert char data to hex and write bytes individually, slow
			spi.writebytes([data])
			
		self.CE_DESELECT()