Code: Select all
class flash():
def __init__(self,port,cs):
self._port = port
self._cs= cs
self._cs.high()
self._block_cnt=31
self._block_size=4096
self._baseoffsett=0x0
self.command(clsr, 0)
self.command(wren, 0)
def readblocks(self,block_num,buf):
cmd=bytearray(1)
cmd[0] = fourread
cmd.extend(self.block_to_bytes(self._baseoffsett+block_num *self._block_size))
#print('read cmd {}'.format(list(cmd)))
self._cs.low()
time.sleep_us(1)
self._port.send(cmd)
self._port.readinto(buf)
self._cs.high()
def writeblocks(self,block_num,buf):
pagesize = 256
cmd=bytearray(5)
cmd[0]=fourp4e
cmdwrite=bytearray(5)
cmdwrite[0] = fourpp
#for each block erase block and write again
nblocks, err = divmod(len(buf), self._block_size)
mv = memoryview(buf)
offset=0
curblock=0
while(curblock < nblocks):
#erase this block
cmd[1:4]=self.block_to_bytes(self._baseoffsett+(block_num*self._block_size)+(curblock*self._block_size))[0:3]
self.command(wren, 0)
self.command(cmd, 0)
#print('cmd_clear {}'.format(list(cmd)))
#wait for write complete
self.command(rdsr1, 1)
while ((self._buffer[0]&0x01) ==0x01):
#print('clear in progress')
self.command(rdsr1, 1)
#write this block
for x in range(self._block_size/pagesize):
#print('x {} start point {}'.format(x,curblock*self._block_size+(x*pagesize)))
cmdwrite[1:4]=self.block_to_bytes(self._baseoffsett+(block_num*self._block_size)+(curblock*self._block_size)+(x*pagesize))[0:3]
#print('cmdwrite = {}'.format(list(cmdwrite)))
self.command(wren, 0)
time.sleep_us(1)
self._cs.low()
time.sleep_us(1)
self._port.send(cmdwrite)
startpoint=(curblock*self._block_size)+(x*pagesize)
tempbuff = buf[startpoint:startpoint+pagesize]
print(tempbuff)
self._port.send(buf[startpoint:startpoint+pagesize])
self._cs.high()
time.sleep_us(1)
self.command(rdsr1, 1)
while ((self._buffer[0]&0x01) ==0x01):
print('Write in progress')
self.command(rdsr1, 1)
#print('Done')
curblock+=1
def ioctl(self,op,arg):
if op==4:
return self._block_size
if op==5:
return self._block_cnt
def count(self):
return self._block_cnt
>>> uos.VfsFat.mkfs(_mtr.flash_ic)
>>> vfs=uos.VfsFat(_mtr.flash_ic)
>>> uos.mount(vfs,'/test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV
I have verified the block read and write are working I am sure i am missing something small and simple But cannot seem to see it.