SPI master RX

lchazall
Posts: 7
Joined: Tue Nov 22, 2016 7:26 pm

SPI master RX

Postby lchazall » Mon Mar 13, 2017 4:18 pm

Hi,

I am currently trying to use SPI master driver.
With the help of esp-idf examples I succeeded sending my command.
However, they do not provide examples for reading from buffer.
To do it myself I configured my transaction structure like this :

Code: Select all

static spi_transaction_t trans;
trans.flags		= 0;				// Bitwise
trans.command	= 0x01;			// 0x01 = Reading request
trans.address		= 0;
trans.length		= 0;
trans.rxlength		= bytesToReceive * 8;
trans.user		= NULL;
trans.rx_buffer		= data;
trans.tx_buffer 	= NULL;
Where bytesToReceive is the data length (in bytes) I am expecting to get and data is my buffer.

I configured my bus driver like this :

Code: Select all

devcfg.command_bits		= 8;
devcfg.address_bits			= 0;
devcfg.dummy_bits			= 0;
devcfg.mode				= mode;
devcfg.duty_cycle_pos		= 0;
devcfg.cs_ena_pretrans		= 0;
devcfg.cs_ena_posttrans		= 2;
devcfg.clock_speed_hz 		= clockRate;
devcfg.spics_io_num		= SPI_SS;
devcfg.flags				= 0;
devcfg.flags				|= (SPI_DEVICE_TXBIT_LSBFIRST|SPI_DEVICE_RXBIT_LSBFIRST);
devcfg.queue_size			= 1;
devcfg.pre_cb				= NULL;
devcfg.post_cb			= NULL;
Then, when I start spi_device_transmit, I get :

Code: Select all

assertion "ret_trans==trans_desc" failed: file "...../esp-idf/components/driver/./spi_master.c", line 702, function: spi_device_transmit
abort() was called at PC 0x400f388b
Are my settings OK?
Best regards,
lchazall

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: SPI master RX

Postby ESP_Sprite » Tue Mar 14, 2017 4:41 am

Can you also post the code that does the actual transmission/reception? This happens if you call spi_device_queue_trans without spi_device_get_trans_result (thus leaving a transaction in the SPI drivers queue) and then do a spi_device_transmit (which then receives the response from the earlier queued transaction instead of the the one it queued itself and abort()s out.)

In general, if you set a receive buffer in the spi_transaction_t structure, the SPI device driver will do reception; after spi_device_transmit has finished your receive buffer should be filled with the result values.

lchazall
Posts: 7
Joined: Tue Nov 22, 2016 7:26 pm

Re: SPI master RX

Postby lchazall » Tue Mar 14, 2017 2:26 pm

Thank you for answering.

The bug disappeared, I do not know how and I do not use any version control manager.
I was using spi_device_transmit function which is supposed to be blocking my code till transaction is done. I cannot figure out what was the problem.

Now my problem is about receiving via SPI using DMA.
When I have to get data bigger than THRESH_DMA_TRANS, what I receive is totally wrong.

Here are my send and receive functions :

Code: Select all

esp_err_t SPI_send(char *data, size_t bytes)
{
	esp_err_t err = ESP_OK;

	static spi_transaction_t trans;

	char * dma_data = pvPortMallocCaps(bytes, MALLOC_CAP_DMA);
	memcpy(dma_data,data,bytes);

	trans.flags		= 0;				// Bitwise
	trans.command 	= 0x00;				// Writing request
	trans.address		= 0;
	trans.length		= bytes * 8;
	trans.rxlength		= 0;
	trans.user		= NULL;
	trans.rx_buffer		= NULL;
	trans.tx_buffer 	= dma_data;

	printf("Sending %u bytes = %u bits\n", bytes, trans.length);

	err = spi_device_transmit(spi, &trans);

	free(dma_data);

	if(err != ESP_OK)
		printf(" SPI TX error : %d\n", err);
	return err;
}

Code: Select all

esp_err_t SPI_receive(char *data, size_t bytes)
{
	esp_err_t err = ESP_OK;

	static spi_transaction_t trans;

	char * dma_data = pvPortMallocCaps(bytes, MALLOC_CAP_DMA);

	memset(data,0,bytes);
	memset(dma_data,0,bytes);

	trans.flags		= 0;				// Bitwise
	trans.command 	= 0x01;				// Reading request
	trans.address		= 0;
	trans.length		= 0;
	trans.rxlength		= bytes * 8;
	trans.user		= NULL;
	trans.rx_buffer		= dma_data;
	trans.tx_buffer 	= NULL;

	printf("Receiving %u bytes = %u bits\n", bytes, trans.rxlength);
	err = spi_device_transmit(spi, &trans);

	if(err != ESP_OK)
		printf(" SPI RX error : %d\n", err);
	else
		memcpy(data,dma_data, bytes);

	free(dma_data);

	return err;
}
I made a bitbanging driver to do the same job and the data I get is the data I want.

What am I doing wrong ?
Best regards,
lchazall

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: SPI master RX

Postby ESP_Sprite » Thu Mar 16, 2017 4:28 am

I can't tell from that code... what is 'completely wrong' in this case and what do you expect? Also, do you use the latest update of esp-idf? There have been some issues with DMA transfers before; they should be mostly fixed by now.

Who is online

Users browsing this forum: No registered users and 143 guests