Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    SPI throughput, any tricks to speed it up?

    Scheduled Pinned Locked Moved LoPy
    lopy spi speed
    3 Posts 2 Posters 886 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      andrewj
      last edited by

      Enjoying experimenting with LoPy and LoRa.
      I currently monitor temperatures on a bee hive with a LoPy4 using Lora to get the data out of the farm paddock. This works well. However, the next step is to experiment with detection and analysis of sound with this system.
      For the first iteration I was going to use a microphone connected to a Microchip MCP3204 ADC and then read the data using the SPI on the LoPy. The sound would be recorded in short (2 second) chunks so I can just buffer samples before final processing.

      Using a baudrate of 1000000 the ADC has a claimed sustained throughput of 25,000 samples/second but the SPI interface on the LoPy4 can only just average 8000 samples/second.

      Outline of the test code:

      cs = Pin('P12',mode=Pin.OUT,pull=Pin.PULL_UP) #chip select
      #initialize SPI using default pins.
      spi=SPI(0,mode=SPI.MASTER,baudrate=1000000,polarity=0,phase=0)
      #create read buffer. will write three bytes and read three back
      numTest = 1000 #do multiple conversions for timing tests.
      rbuf = bytearray(3)
      wbuf = bytes([0x04,0x40,0x00]) #setup values for MCP3204
      bigBuf = bytearray()
      for idx in range(numTest):
          cs.value(False)
          spi.write_readinto(wbuf,rbuf)
          cs.value(True)
          bigBuf.extend(rbuf) #poor coding?
      

      Are there any tricks to making this faster? It would obviously be better to have the sampling controlled by a hardware timer rather than depending on a software loop.
      Thanks from a relative beginner.

      sebS 1 Reply Last reply Reply Quote 0
      • sebS Offline
        seb @andrewj
        last edited by

        @andrewj

        It might be worth looking into implementing a micropython module in C to achieve what you want. This will allow you to get better execution speeds compared to running through micropython.

        Please see here for some guidance on how to make micropython modules: http://micropython-dev-docs.readthedocs.io/en/latest/adding-module.html

        A 1 Reply Last reply Reply Quote 1
        • A Offline
          andrewj @seb
          last edited by

          @seb Thanks for taking the time to get back to me and for the link.
          I was worried that might be the solution. Will have to get started on learning.

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom