SPI throughput, any tricks to speed it up?

  • 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.
    #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):
        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.

  • @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.

  • @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

Log in to reply

Pycom on Twitter