This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi Team,
We would like to ask your help regarding the customer's concern below.
I am struggling with an interrupt vector. I obtained a sample code for I2C transmit from master to slave. This code uses an interrupt service routine to send a data array to a transmit buffer. For this to work the data array has to be declared as a global.
When you have multiple devices on the i2c bus you will likely end up with multiple different arrays which need to be piped through the data array to the transmit buffer.
How do I create an undefined/variable array which I can use to pass other different size arrays to? This undefined array is part of the tx interrupt vector of the i2c which passes it data to the tx buffer.
I am looking for a simple structure. If I define the array size to a specific size the transmit buffer will sent all the elements. I am of course only interested in sending the elements with the required data in them.
Thank you for your support!
Regards,
Danilo
Hello,
I think the customer is asking if they can dynamically size an array. While it may be possible, I wouldn't recommend it. In our I2C code examples, you'll see there are several arrays of different sizes. This way, depending on the command, the appropriate array size can be used without having to size it dynamically.
//****************************************************************************** // Example Commands ************************************************************ //****************************************************************************** #define SLAVE_ADDR 0x48 /* CMD_TYPE_X_SLAVE are example commands the master sends to the slave. * The slave will send example SlaveTypeX buffers in response. * * CMD_TYPE_X_MASTER are example commands the master sends to the slave. * The slave will initialize itself to receive MasterTypeX example buffers. * */ #define CMD_TYPE_0_SLAVE 0 #define CMD_TYPE_1_SLAVE 1 #define CMD_TYPE_2_SLAVE 2 #define CMD_TYPE_0_MASTER 3 #define CMD_TYPE_1_MASTER 4 #define CMD_TYPE_2_MASTER 5 #define TYPE_0_LENGTH 1 #define TYPE_1_LENGTH 2 #define TYPE_2_LENGTH 6 #define MAX_BUFFER_SIZE 20 /* MasterTypeX are example buffers initialized in the master, they will be * sent by the master to the slave. * SlaveTypeX are example buffers initialized in the slave, they will be * sent by the slave to the master. * */ uint8_t MasterType2 [TYPE_2_LENGTH] = {'F', '4', '1', '9', '2', 'B'}; uint8_t MasterType1 [TYPE_1_LENGTH] = { 8, 9}; uint8_t MasterType0 [TYPE_0_LENGTH] = { 11}; uint8_t SlaveType2 [TYPE_2_LENGTH] = {0}; uint8_t SlaveType1 [TYPE_1_LENGTH] = {0}; uint8_t SlaveType0 [TYPE_0_LENGTH] = {0};
The way I dealt with this is to simply pass a pointer to the data string and its length. The I2C transmit routine then dealt with the details.
Dynamically sizing the arrays is of courses possible using malloc() and free() but is not recommended for embedded systems. Unless you are very very sure that you will never run out of heap space and you don't have any leaks.
**Attention** This is a public forum