SPI: received data shifted by 1bit right

KanyeKanye
Posts: 54
Joined: Mon Dec 05, 2016 12:34 am

SPI: received data shifted by 1bit right

Postby KanyeKanye » Tue Jan 15, 2019 11:13 am

I am trying to test SPI device by sending configuration and than reading configuration register from TI ADC.
Code looks like below.

Code: Select all

while (1) {
	spi_device_handle_t spi;
	spi_bus_config_t buscfg = {
		.miso_io_num=PIN_NUM_SPI_MISO,
		.mosi_io_num=PIN_NUM_SPI_MOSI,
		.sclk_io_num=PIN_NUM_SPI_CLK,
		.quadwp_io_num=-1, .quadhd_io_num=-1,
	};
	spi_device_interface_config_t devcfg = {
		.spics_io_num=PIN_NUM_SPI_ADC_CS,
		.clock_speed_hz=1*1000*1000, // 1MHz
		.mode=1, // SPI MODE 1
		.command_bits=8, // 8-bit command length
		.address_bits=0, // no address
		.queue_size=2,
	};
	// should I use DMA? Does it work?
	ESP_ERROR_CHECK(spi_bus_initialize(VSPI_HOST, &buscfg, 0/*dma_chan - 0 no DMA*/));
	ESP_ERROR_CHECK(spi_bus_add_device(VSPI_HOST, &devcfg, &spi));

	// how to declare rx_data buffer for DMA usage?
	uint8_t rx_data[10];
	memset(rx_data, 0x00, sizeof(rx_data));

	ESP_LOGE(TAG, "write RESET");
	spi_transaction_t transaction;
	memset(&transaction, 0, sizeof(transaction));
	transaction.cmd = RESET;
	assert(spi_device_polling_transmit(spi, &transaction) == ESP_OK);
	vTaskDelay(1 / portTICK_RATE_MS);

	ESP_LOGE(TAG, "writeDefaultConfig");
	uint8_t data[4] = {DEFAULT_CONFIG_REG0, DEFAULT_CONFIG_REG1, DEFAULT_CONFIG_REG2, DEFAULT_CONFIG_REG3};
	memset(&transaction, 0, sizeof(transaction));
	transaction.cmd = WREG | 0b0000;
	transaction.length = 4*8;
	transaction.tx_buffer = data;
	assert(spi_device_polling_transmit(spi, &transaction) == ESP_OK);

	ESP_LOGE(TAG, "read RREG");
	memset(&transaction, 0, sizeof(transaction));
	transaction.cmd = RREG | 0b0000;
	transaction.rx_buffer = rx_data;
	transaction.rxlength = 4*8;
	// should I set .length equal .rxlength? Does .length count .cmd bytes?
	transaction.length = 4*8; 
	assert(spi_device_polling_transmit(spi, &transaction) == ESP_OK);
	ESP_LOG_BUFFER_HEX("Received", rx_data, 10 );

	memset(rx_data, 0x00, sizeof(rx_data));

	ESP_LOGE(TAG, "read single register");
	uint8_t address = 1;
	memset(&transaction, 0, sizeof(transaction));
	transaction.cmd = RREG | (address<<2);
	transaction.rx_buffer = rx_data;
	transaction.rxlength = 8;
	transaction.length = 8;
	assert(spi_device_polling_transmit(spi, &transaction) == ESP_OK);
	ESP_LOG_BUFFER_HEX("Received", rx_data, 10 );

	ESP_LOGE(TAG, "End");
	spi_bus_remove_device(spi);
	vTaskDelay(2500 / portTICK_RATE_MS);
}
No matter if I use DMA or not, no matter which SPI mode (ADC uses mode 1), no matter of speed: received data is shifted 1bit right
eg. I send 0x03, received is 0x81 and 0x80 in next byte
What to do? I am using current idf.

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: SPI: received data shifted by 1bit right

Postby loboris » Tue Jan 15, 2019 8:53 pm

Maybe you are using the wrong SPI mode. Try with mode 0.

Who is online

Users browsing this forum: No registered users and 243 guests