This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Issue in SPI trensfer inside timer handler function

I need to read a SPI device every fixed milliseconds so I thought to create a TIMER and read SPI slave inside its handler. But there are some problems cause I get all zero values and in some cases I get the "SPI timeout transfer" error. I think that is a problem related to the ineraction between the SPI and TIMER interrupts.

The TIMER definition:

K_TIMER_DEFINE( fetchlDataTimer, Timer_Handler, NULL );

...

k_timer_start( &fetchDataTimer, K_SECONDS( 1 ), K_SECONDS( 1 ) );

The SPI read function inside the TIMER handler:

void ICM20948_Read_Reg( const struct spi_dt_spec *spec, uint8_t reg, uint8_t *data, uint8_t dataLen )
{
    reg |= 0x80;

	const struct spi_buf spiBufTx = {
		.buf = &reg,
		.len = 1
	};

	struct spi_buf_set tx = {
		.buffers = &spiBufTx,
		.count = 1
	};

	struct spi_buf spiBufRx[] = {
		{
			.buf = NULL,
			.len = 1
		},
		{
			.buf = data,
			.len = dataLen
		}
	};

	struct spi_buf_set rx = {
		.buffers = spiBufRx,
		.count = 2
	};

	spi_transceive_dt( spec, &tx, &rx );
}

1) What's the problem?

2) When I create a timer with the K_TIMER_DEFINE macro am I going to use a TIMERx peripheral? If so, how can I choose one in particular?

3) OUT TOPIC: is the above SPI read routine a good choice in terms of time performance? If not, have you some suggestions?

Related