SPI bus reading in background

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
scardig
Posts: 20
Joined: Wed Jan 10, 2018 8:32 am

SPI bus reading in background

Post by scardig » Wed Jan 29, 2020 8:39 am

Hello,

we have a Pyboard 1.1 plus an IC connected to it via SPI bus. We need to read some data from the IC at 10 Hz and store it so that my main loop (that run at 2Hz) is able to read such data, perform some calcs, free memory then send the results via serial to another module. We thought to read SPI data within an ISR like this:

Code: Select all

	global myvar # any hint on a variable type suitable ?
	def lux(r):
		global myvar
		# read SPI data and store it in myvar
	mytimer = pyb.Timer(9, freq = 10)
	mytimer.callback(lux)

while True: # main loop
	# read myvar
	# send read data to UART...
	# pyb.delay(...)
Are we on the right way ?

User avatar
jimmo
Posts: 1769
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: SPI bus reading in background

Post by jimmo » Mon Feb 03, 2020 2:31 am

Hi,
scardig wrote:
Wed Jan 29, 2020 8:39 am
We need to read some data from the IC at 10 Hz and store it so that my main loop (that run at 2Hz)
Would it be possible just to run the main loop at 10Hz and only do the rest of the work every fifth iteration?

In general though what you're describing is possible with interrupts. You may want to use micropython.schedule from the ISR to turn it into a "soft" interrupt.

The other option is asyncio (uasyncio). You'd have two tasks running, one looping every 100ms for reading the SPI data, the other every 500ms for your main loop.

Post Reply