Page 1 of 1

SPI bus reading in background

Posted: Wed Jan 29, 2020 8:39 am
by scardig
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 ?

Re: SPI bus reading in background

Posted: Mon Feb 03, 2020 2:31 am
by jimmo
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.