Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2c recommended link size (IDFGH-13621) #14507

Open
3 tasks done
florentbr opened this issue Sep 4, 2024 · 0 comments
Open
3 tasks done

i2c recommended link size (IDFGH-13621) #14507

florentbr opened this issue Sep 4, 2024 · 0 comments
Labels
Status: Opened Issue is new Type: Bug bugs in IDF

Comments

@florentbr
Copy link

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

5.2.2

Espressif SoC revision.

ESP32 v3.1

Operating System used.

Linux

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

Development Kit.

ESP32-DevKitC V4

Power Supply used.

USB

What is the expected behavior?

The constant I2C_INTERNAL_STRUCT_SIZE in driver/i2c.h should match a link size which is sizeof(i2c_cmd_link_t).
It should be 20 and not 24.

#define I2C_INTERNAL_STRUCT_SIZE (24)

This is useful to estimate accurately the required size to allocate a static buffer or memory on the stack.
The macro ``I2C_LINK_RECOMMENDED_SIZE` rely on this constant. Since it's wrong, the estimated size is not even close to the used size.

What is the actual behavior?

Used size after [allocate, start, write, write, write, stop]:

command size 0 links:: 20
command size 1 links:: 40
command size 2 links:: 60
command size 3 links:: 80
command size 4 links:: 100
command size 5 links:: 120

Recommended size from i2c.h

I2C_INTERNAL_STRUCT_SIZE: 24
I2C_LINK_RECOMMENDED_SIZE(0): 48
I2C_LINK_RECOMMENDED_SIZE(1): 168
I2C_LINK_RECOMMENDED_SIZE(2): 288
I2C_LINK_RECOMMENDED_SIZE(3): 408
I2C_LINK_RECOMMENDED_SIZE(4): 528

Steps to reproduce.

#include <stdio.h>
#include <stdint.h>
#include "driver/i2c.h"


// https://github.com/espressif/esp-idf/blob/cfd8a30995089e2a297f8c08ed8064be46ee7866/components/driver/i2c/i2c.c#L152
typedef struct {
    void *head;     /*!< head of the command link */
    void *cur;      /*!< last node of the command link */
    void *free;     /*!< the first node to free of the command link */
    void *free_buffer;    /*!< pointer to the next free data in user's buffer */
    uint32_t free_size;      /*!< remaining size of the user's buffer */
} i2c_cmd_desc_t;

#define print_size(msg, cmd)  printf("%s: %i \n", msg,\
   (int)( 1024 - ((i2c_cmd_desc_t*)cmd)->free_size ));


void app_main() {

  uint8_t buffer[1024];

  i2c_cmd_handle_t cmd = i2c_cmd_link_create_static(buffer, sizeof(buffer));
  print_size("command size 0 links:", cmd);

  i2c_master_start(cmd);
  print_size("command size 1 links:", cmd);

  i2c_master_write_byte(cmd, 0x55, true);
  print_size("command size 2 links:", cmd);

  i2c_master_write(cmd, buffer, 20, true);
  print_size("command size 3 links:", cmd);

  i2c_master_write(cmd, buffer, 30, true);
  print_size("command size 4 links:", cmd);

  i2c_master_stop(cmd);
  print_size("command size 5 links:", cmd);

  printf("I2C_INTERNAL_STRUCT_SIZE: %i\n", (int)I2C_INTERNAL_STRUCT_SIZE);

  unsigned recommended_size[5];
  recommended_size[0] = I2C_LINK_RECOMMENDED_SIZE(0);
  recommended_size[1] = I2C_LINK_RECOMMENDED_SIZE(1);
  recommended_size[2] = I2C_LINK_RECOMMENDED_SIZE(2);
  recommended_size[3] = I2C_LINK_RECOMMENDED_SIZE(3);
  recommended_size[4] = I2C_LINK_RECOMMENDED_SIZE(4);

  for (unsigned i = 0; i < 5; ++i)
    printf("I2C_LINK_RECOMMENDED_SIZE(%u): %u\n", i, recommended_size[i]);
}

Debug Logs.

No response

More Information.

No response

@florentbr florentbr added the Type: Bug bugs in IDF label Sep 4, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label Sep 4, 2024
@github-actions github-actions bot changed the title i2c recommended link size i2c recommended link size (IDFGH-13621) Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new Type: Bug bugs in IDF
Projects
None yet
Development

No branches or pull requests

2 participants