Search code examples
cembeddedi2cmaster-slaveeeprom

How can I push buffer into the stack provided the buffer contains a function value stored to it as per the following code?


  • struct dev *address - It reads the addresses from a structure of registers.
  • unsigned int bytes- number of bytes of data being read from the register address.
  • For example - (0x2, 1). 0x2 - register address and 1 - number of bytes.
i2c_read(struct dev *address, unsigned int bytes)
{
 char buff[256];
    char *p1 = buff; // Contains a pointer to a buffer of 256 bytes

 p1 = func(address, bytes); //values to be stored to the "pointer of a buffer of 256 bytes".
}
  • The buffer contains values to be stored from func(address, bytes).
int push() //to push the buffer into  the stack.
{
//push the buffer to stack  here?.
}

I tried doing something like this -

void push(unsigned int *result_buffer)
{
    unsigned int *tos, *p1, arr_stack[MAX_SIZE];
    //MAX_SIZE is the size of the stack.
    tos = arr_stack; /* tos points to the top of stack */
    p1 = arr_stack; /* initialize p1 */
    printf("\n Simple Stack Example - Pointers");
    if (p1 == (tos + MAX_SIZE)) {
        printf("\nStatus : Stack Overflow.\n");
    } else {
        *p1 = *result_buffer;
        printf("\nPush Value : %d ", *(p1));
        p1++;
    }
}

Solution

  • Your error is in the definition of func(). You need to define your function. Let's say you have an i2c port. A function to comunicate with it should need the following to do its tasks:

    • An address for the i2c device.
    • The number of bytes to expect in the response.
    • An address to a buffer to store the response.

    It would also be wise to return a code indicating the status of the operation, did it succeed? Why did it fail?

    the signatre of the function should then be, assuming the error code is expressed as an integer:

    int i2c_read(struct dev *address, int result_length, unsigned char* result_buffer);
    

    At the call site, you must create a buffer that's large enough to store the result.

    As in:

    // Example: You've just sent read serial command... to read 4 bytes serial #...
    
    unsigned char buffer[4];
    
    if (i2c_read(address, 4, buffer) < 0)  // buffer decays to a pointer when
                                           // passed to the function.
    {
        printf("error! failed to read serial # !!!\n");
    }
    else
    {
        printf("serial # is: %02x%02x-%02x%02x\n", buffer[0], buffer[1], buffer[2], buffer[3]); 
    }
    

    Your question was not very clear....

    Usally I2c requires a command to be sent and a response to be received. In most i2c API I've seen, the signature of the send/receive function is:

    int i2c_sendCcommand(WORD addr, int nbytesCommand, const unsigned char* command, int nBytesResponse, unsigned char* response);
    
    // usage:
    
    unsigned char cmdBuffer[] = { 0x43, 0x01 };
    unsigned char respBuffer[4];
    
    if (i2c_sendCcommand(0x1234, 2, cmdBuffer, 4, respBuffer) < 0)
    {
        printf("error! failed to read serial # !!!\n");
    }
    else
    {
        printf("serial # is: %02x%02x-%02x%02x\n", respBuffer[0], 
                      respBuffer[1], respBuffer[2], respBuffer[3]); 
    }