/ adafruit_epd / mcp_sram.py
mcp_sram.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2018 Dean Miller for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `adafruit_epd.mcp_sram` - Adafruit MCP SRAM - sram driver 24 ==================================================================================== 25 CircuitPython driver for Microchip SRAM chips 26 * Author(s): Dean Miller 27 """ 28 29 from micropython import const 30 from adafruit_bus_device import spi_device 31 32 __version__ = "0.0.0-auto.0" 33 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_EPD.git" 34 35 SRAM_SEQUENTIAL_MODE = const(1 << 6) 36 37 38 class Adafruit_MCP_SRAM_View: 39 """A interface class that turns an SRAM chip into something like a memoryview""" 40 41 def __init__(self, sram, offset): 42 self._sram = sram 43 self._offset = offset 44 self._buf = [0] 45 46 def __getitem__(self, i): 47 return self._sram.read(self._offset + i, 1)[0] 48 49 def __setitem__(self, i, val): 50 self._buf[0] = val 51 self._sram.write(self._offset + i, self._buf) 52 53 54 class Adafruit_MCP_SRAM: 55 """supporting class for communicating with 56 Microchip SRAM chips""" 57 58 SRAM_READ = 0x03 59 SRAM_WRITE = 0x02 60 SRAM_RDSR = 0x05 61 SRAM_WRSR = 0x01 62 63 def __init__(self, cs_pin, spi): 64 # Handle hardware SPI 65 self._spi = spi_device.SPIDevice(spi, cs_pin, baudrate=8000000) 66 self.spi_device = spi 67 self.cs_pin = cs_pin 68 self._buf = bytearray(3) 69 self._buf[0] = Adafruit_MCP_SRAM.SRAM_WRSR 70 self._buf[1] = 0x43 71 with self._spi as spidev: 72 spidev.write(self._buf, end=2) # pylint: disable=no-member 73 74 def get_view(self, offset): 75 """Create an object that can be used as a memoryview, with a given offset""" 76 return Adafruit_MCP_SRAM_View(self, offset) 77 78 def write(self, addr, buf, reg=SRAM_WRITE): 79 """write the passed buffer to the passed address""" 80 self._buf[0] = reg 81 self._buf[1] = (addr >> 8) & 0xFF 82 self._buf[2] = addr & 0xFF 83 84 with self._spi as spi: 85 spi.write(self._buf, end=3) # pylint: disable=no-member 86 spi.write(bytearray(buf)) # pylint: disable=no-member 87 88 def read(self, addr, length, reg=SRAM_READ): 89 """read passed number of bytes at the passed address""" 90 self._buf[0] = reg 91 self._buf[1] = (addr >> 8) & 0xFF 92 self._buf[2] = addr & 0xFF 93 94 buf = bytearray(length) 95 with self._spi as spi: 96 spi.write(self._buf, end=3) # pylint: disable=no-member 97 spi.readinto(buf) # pylint: disable=no-member 98 return buf 99 100 def read8(self, addr, reg=SRAM_READ): 101 """read a single byte at the passed address""" 102 return self.read(addr, 1, reg)[0] 103 104 def read16(self, addr, reg=SRAM_READ): 105 """read 2 bytes at the passed address""" 106 buf = self.read(addr, 2, reg) 107 return buf[0] << 8 | buf[1] 108 109 def write8(self, addr, value, reg=SRAM_WRITE): 110 """write a single byte at the passed address""" 111 self.write(addr, [value], reg) 112 113 def write16(self, addr, value, reg=SRAM_WRITE): 114 """write 2 bytes at the passed address""" 115 self.write(addr, [value >> 8, value], reg) 116 117 def erase(self, addr, length, value): 118 """erase the passed number of bytes starting at the passed address""" 119 self._buf[0] = Adafruit_MCP_SRAM.SRAM_WRITE 120 self._buf[1] = (addr >> 8) & 0xFF 121 self._buf[2] = addr & 0xFF 122 fill = bytearray([value]) 123 with self._spi as spi: 124 spi.write(self._buf, end=3) # pylint: disable=no-member 125 for _ in range(length): 126 spi.write(fill) # pylint: disable=no-member