I2C : why can't I send twice the same cmd ?

jmcornil
Posts: 21
Joined: Mon Feb 10, 2020 1:42 pm

I2C : why can't I send twice the same cmd ?

Postby jmcornil » Fri Apr 30, 2021 9:09 am

Hello

I have an issue with the i2c_master_cmd_begin() function.

I want to read the 14 data registers of an mpu_6050 (Acc_x, Acc_y, Acc_z, Temp, G_x, G_y, G_z).
So I make the following command :

Code: Select all

   esp_err_t ret ;
     
   /// Buffer où mettre les 14 octets lus depuis le mpu
   uint8_t p_buff_raw_data[14];
   
   /// Préparation d'une commande lecture de 14 octets à partir de <ACCEL_XOUT_H_ADDR>
   /// Création d'un variable (handler) <cmd_read_14_AccX> grâce auquel on va construire */
   i2c_cmd_handle_t cmd_read_14_AccX = i2c_cmd_link_create();
   /// Signal <START>  (voir description I2C)
   i2c_master_start(cmd_read_14_AccX);
   /// Adresse du périphérique (écriture)
   i2c_master_write_byte(cmd_read_14_AccX, 0x68 << 1, ACK_CHECK_EN); 
   /// Adresse du premier registre
   i2c_master_write_byte(cmd_read_14_AccX, 0x3B, ACK_CHECK_EN);
   /// Nouveau signal <START> 
   i2c_master_start(cmd_read_14_AccX);
   /// Adresse du périphérique (lecture)
   i2c_master_write_byte(cmd_read_14_AccX, (0x68 << 1)|I2C_MASTER_READ, ACK_CHECK_EN);
   /// Lecture de 13 octets  avec un signal <ACK> en retour
   i2c_master_read(cmd_read_14_AccX, p_buff_raw_data, 13, ACK_VAL);
   /// Lecture du 14-èmè avec un signal <NACK> en retour
   i2c_master_read_byte(cmd_read_14_AccX, p_buff_raw_data+13, NACK_VAL);
   /// Signal <STOP>
   i2c_master_stop(cmd_read_14_AccX);vTaskDelay(100);
If then I run :

Code: Select all

   ret = i2c_master_cmd_begin(0, cmd_read_14_AccX , 1000 / portTICK_RATE_MS);
   printf("*** (1) * err retour = %i \r\n",ret);
it works fine and I get ret= ESP_OK

Since I want to read these registers again and again, I tought that I could use the same cmd_read_14_AccX without deleting and rebuildind it. But If i execute twice i2c_master_cmd_begin() with the same cmd_read_14_AccX :

Code: Select all

   ret = i2c_master_cmd_begin(0, cmd_read_14_AccX , 1000 / portTICK_RATE_MS);
   printf("*** (1) * err retour = %i \r\n",ret);
   vTaskDelay(100);
   ret = i2c_master_cmd_begin(0, cmd_read_14_AccX , 1000 / portTICK_RATE_MS);
   printf("*** (2) * err retour = %i \r\n",ret);
the second time I get ret=-1 and I don't understand why.

Moreover the I2C seems blocked and I have to unplug the device before getting another correct read.

Any hint ?

Thanks in advance.


I am working with : esp-idf v4.1-dev-2020-ga7bbc74a2-dirty

ESP_jakob
Posts: 49
Joined: Mon Jun 01, 2020 6:28 am

Re: I2C : why can't I send twice the same cmd ?

Postby ESP_jakob » Thu May 06, 2021 1:42 am

Hi jmcornil,

we had some issues with re-using the I2C commands. I personally don't know the details but for now, please create a new I2C command for every transaction.

Best,
Jakob

ESP_morris
Posts: 290
Joined: Wed Sep 05, 2018 6:23 am

Re: I2C : why can't I send twice the same cmd ?

Postby ESP_morris » Thu May 06, 2021 3:03 am

no, we can't reuse the i2c_cmd_handle_t after i2c_master_cmd_begin, which will change the internal state of i2c_cmd_handle_t.

jmcornil
Posts: 21
Joined: Mon Feb 10, 2020 1:42 pm

Re: I2C : why can't I send twice the same cmd ?

Postby jmcornil » Thu May 06, 2021 9:14 am

Thank you for your replies.

However I think it would be writtent in the documetation page https://docs.espressif.com/projects/esp ... TickType_t that there is a side effect on the cmd_handle argument and it is no more usuable for a next call.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 119 guests