如果缓冲区包含按照以下代码存储到它的函数值,我如何将缓冲区压入堆栈?
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 - 它从寄存器结构中读取地址。
- unsigned int bytes- 从寄存器地址读取的数据字节数。
- 例如 - (0x2, 1)。 0x2 - 寄存器地址和 1 - 字节数。
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".
}
- 缓冲区包含要从 func(address, bytes) 存储的值。
int push() //to push the buffer into the stack.
{
//push the buffer to stack here?.
}
我试过这样做 -
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++;
}
}
你的错误在 func() 的定义中。你需要定义你的功能。假设您有一个 i2c 端口。与其通信的函数应该需要以下内容来完成它的任务:
- i2c 设备的地址。
- 响应中预期的字节数。
- 用于存储响应的缓冲区地址。
最好return一个表示操作状态的代码,成功了吗?为什么失败了?
函数的签名应该是,假设错误代码表示为整数:
int i2c_read(struct dev *address, int result_length, unsigned char* result_buffer);
在调用点,您必须创建一个足够大的缓冲区来存储结果。
如:
// 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]);
}
你的问题不是很清楚....
通常I2c需要发送命令和接收响应。在我见过的大多数 i2c API 中,send/receive 函数的签名是:
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]);
}
- struct dev *address - 它从寄存器结构中读取地址。
- unsigned int bytes- 从寄存器地址读取的数据字节数。
- 例如 - (0x2, 1)。 0x2 - 寄存器地址和 1 - 字节数。
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".
}
- 缓冲区包含要从 func(address, bytes) 存储的值。
int push() //to push the buffer into the stack.
{
//push the buffer to stack here?.
}
我试过这样做 -
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++;
}
}
你的错误在 func() 的定义中。你需要定义你的功能。假设您有一个 i2c 端口。与其通信的函数应该需要以下内容来完成它的任务:
- i2c 设备的地址。
- 响应中预期的字节数。
- 用于存储响应的缓冲区地址。
最好return一个表示操作状态的代码,成功了吗?为什么失败了?
函数的签名应该是,假设错误代码表示为整数:
int i2c_read(struct dev *address, int result_length, unsigned char* result_buffer);
在调用点,您必须创建一个足够大的缓冲区来存储结果。
如:
// 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]);
}
你的问题不是很清楚....
通常I2c需要发送命令和接收响应。在我见过的大多数 i2c API 中,send/receive 函数的签名是:
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]);
}