0
  • 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++;
    }
}
code_debug
  • 25
  • 4
  • What does the parameter bytes do? What is `data` ? Your function cannot fill buffer as written. if you want the function `func` to send multiple bytes, and return multiple bytes, pass `buffer` as an argument, along with the number of bytes to rsend and the number of bytes to eceive, Otherwise, receive one byte at a time, and store in buffer as you go. – Michaël Roy Feb 16 '21 at 08:12
  • @MichaëlRoy , my bad I didn't notice carefully. So when func(address, bytes), let's say for example (0x2,1). So the address of the register is 0x2, number of bytes is 1. Which mean only 1 byte of data is written (the data could be any from 0 to 9). And can I not call the same buffer from i2c_read to int push function? Because p1 is a pointer to buffer right? – code_debug Feb 16 '21 at 08:25

1 Answers1

0

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]); 
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • but how can I push the buffer to the stack then? – code_debug Feb 16 '21 at 15:13
  • You pass a pointer to the bufffer. Simply passing it as an argument does that for you, except for 64 bit C on x86, which has a different calling convention. – Michaël Roy Feb 16 '21 at 16:54
  • You mean something like this `char the_buffer[256]; char *pointer_to_buffer = the buffer;` Or `void push(unsigned int *result_buffer) { }` // I have edited and showed the push function that I tried above? Or could you please show a sample code of how I could write the push function for more clarity? – code_debug Feb 16 '21 at 20:03
  • I have shown how to call a function, C will push the address of the buffer for you. Thia is C, not assembler. There is no concept of stack in C, nor is there any way to access directly the stack through the language. You pass variables through functions, and the compiler translates that to machine code that will push the variables on the stack. – Michaël Roy Feb 17 '21 at 00:01