Problem with SPI write

ClosedLoop
Posts: 14
Joined: Mon May 16, 2022 8:13 am
Location: Portugal

Problem with SPI write

Postby ClosedLoop » Fri Jul 15, 2022 1:11 pm

Hello, i am trying to do a simple function to write in SPI. I am trying to send 0x02 then 0x00 followed by 0x00 and 0x10. When i first run the program i can see that everything is being written as it should be , in my logic analyzer. But when i reboot i get completely different values from what i was supposed to be getting.

My write function is the following:

Code: Select all

void max_write8(uint8_t address, uint8_t data, uint8_t sensor_ch){

	spi_transaction_t trans_desc;

	uint8_t tx_data[2];

	trans_desc.addr=0;
	trans_desc.cmd=0;
	trans_desc.flags = 0;
	trans_desc.length = 8;
	trans_desc.rxlength = 0;
	trans_desc.tx_buffer = tx_data;

	//memset( &trans_desc, 0, sizeof( spi_transaction_t ) );
	tx_data[0] = (address | 0x80);
	ESP_ERROR_CHECK(spi_device_transmit(handle, &trans_desc));


	tx_data[0] = data;

	ESP_ERROR_CHECK(spi_device_transmit(handle, &trans_desc));

}
I use the following function to initialize SPI and call two writes,

Code: Select all

void init(){

	bus_config.sclk_io_num = 18; // CLK
	bus_config.mosi_io_num = 23; // MOSI
	bus_config.miso_io_num = 19; // MISO
	bus_config.quadwp_io_num = -1; // Not used
	bus_config.quadhd_io_num = -1; // Not used

	ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 0));


	spi_device_interface_config_t dev_config;
	dev_config.address_bits = 0;
	dev_config.command_bits = 0;
	dev_config.dummy_bits = 0;
	dev_config.mode = 0;
	dev_config.duty_cycle_pos = 0;
	dev_config.cs_ena_posttrans = 0;
	dev_config.cs_ena_pretrans = 0;
	dev_config.clock_speed_hz = 1000000;
	dev_config.spics_io_num = 5;
	dev_config.flags = 0;
	dev_config.queue_size = 1;
	dev_config.pre_cb = NULL;
	dev_config.post_cb = NULL;

	ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &handle));

	max_write8(0x02, 0x00,0);

	max_write8(0x00, 0x10,0);
}
I don't understand why that happens. Shouldn't it be always the same values whether the ESP gets rebooted or not?

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

Re: Problem with SPI write

Postby ESP_Sprite » Fri Jul 15, 2022 1:43 pm

Not sure if that's the problem, but always make sure to clear stack-allocated structures:

Code: Select all

spi_transaction_t trans_desc={0};

ClosedLoop
Posts: 14
Joined: Mon May 16, 2022 8:13 am
Location: Portugal

Re: Problem with SPI write

Postby ClosedLoop » Fri Jul 15, 2022 2:02 pm

i tried it but it didn't work. I have the init inside a task could that be the problem? I tried putting everything on main but the issue still remained.

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

Re: Problem with SPI write

Postby ESP_Sprite » Sat Jul 16, 2022 2:18 am

You have more stack-allocated structures you don't clear (e.g. dev_config), have you fixed all of them? On main or in a task doesn't matter; the main function actually also runs in a task that is started by esp-idf and as such is not very different from tasks you start manually.

ClosedLoop
Posts: 14
Joined: Mon May 16, 2022 8:13 am
Location: Portugal

Re: Problem with SPI write

Postby ClosedLoop » Mon Jul 18, 2022 7:55 am

It's fixed now. Thank you :)

Who is online

Users browsing this forum: stvlaa and 110 guests