uctypes and spi

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
codyhanks
Posts: 17
Joined: Thu Nov 30, 2017 7:30 pm

uctypes and spi

Post by codyhanks » Fri Jun 15, 2018 10:44 pm

I am having an issue with read write to mem locations.

Code: Select all

import uctypes
ba=uctypes.bytearray_at(0x10000000,65536)
flash_ic=flash_drv.flash(icport,flash_cs)
flash_ic.readblocks(0,ba)
Basically i have a SPI port with IC attached that is read write in blocks of 64Kb I am attempting to use the CCRAM on a STM32F429 processor in order to read-mod-write this as blocks of memory the issue is that when i try to read into the bytearray(ba) it is showing up with error

Traceback (most recent call last):
File "flash_drv.py", line 107, in readblocks
OSError: [Errno 110] ETIMEDOUT

if i use

Code: Select all

ba=bytearray(4096)
flash_ic=flash_drv.flash(icport,flash_cs)
flash_ic.readblocks(0,ba)
it works correctly...

a quick note the 65536 I have reduced to 4096 errors still remain, also the flash_drv follows the defined block device examples.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: uctypes and spi

Post by Damien » Sat Jun 16, 2018 12:07 am

What is line 107 where it fails with the exception?

Can you read/write the ba CCRAM object just using normal Python array subscripts?

codyhanks
Posts: 17
Joined: Thu Nov 30, 2017 7:30 pm

Re: uctypes and spi

Post by codyhanks » Sat Jun 16, 2018 10:06 am

Code: Select all

 
 def readblocks(self,block_num,buf):
        try:
            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)
 107**           self._port.readinto(buf)
            self._cs.high()
        except Exception as e:
            sys.print_exception(e)
 

its a portion of the driver built in post
viewtopic.php?f=2&t=4900

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: uctypes and spi

Post by chuckbook » Mon Jun 18, 2018 8:00 am

CCM RAM is not accessible via DMA.
DocID018909 Rev 10, 2.1, pp 59

codyhanks
Posts: 17
Joined: Thu Nov 30, 2017 7:30 pm

Re: uctypes and spi

Post by codyhanks » Wed Jun 20, 2018 7:54 pm

So after a bit of review on spi i was able to find a post (https://github.com/micropython/micropython/issues/685) specifies
Therefore, the SPI send/recv/send_recv methods will choose to use DMA if it detects that interrupts are enabled at that point, otherwise it uses the original polling transfers. I've tested with both cases (using nRF24L01 driver) and it seems to work correctly.
With this i was able to encapsulate the access in

Code: Select all

    def readblocks(self,block_num,buf):
        try:
            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)
            if(uctypes.addressof(buf)==0x10000000):
                state=machine.disable_irq()
            self._port.send(cmd)
            self._port.readinto(buf)
            if(uctypes.addressof(buf)==0x10000000):
                machine.enable_irq(state)
            self._cs.high()
        except Exception as e:
            sys.print_exception(e)


Now I have a new issue

Code: Select all

ba=uctypes.bytearray_at(0x10000000,65535)
flash_ic=flash_drv.flash(icport,flash_cs)
flash_ic.readblocks(0,ba)
works fine but

Code: Select all

ba=uctypes.bytearray_at(0x10000000,65536)
fails with

Traceback (most recent call last):
File "flash_drv.py", line 109, in readblocks
OSError: [Errno 5] EIO
PYB: enabling IRQs

codyhanks
Posts: 17
Joined: Thu Nov 30, 2017 7:30 pm

Re: uctypes and spi

Post by codyhanks » Mon Jun 25, 2018 6:32 pm

I was able to fix this issue by reducing the number of bytes written/read at one time I segmented the the read/writes over spi to 256 byte segments.

Post Reply