如何将缓冲区内容复制到结构中定义的另一个缓冲区 [英] How to copy buffer contents to another buffer which is defined in structure
问题描述
我创建了一个缓冲区...
char buffer [8] =mindflow;
和我试图将这个缓冲区内容复制到另一个缓冲区buffer_p.count中,该缓冲区是在嵌套结构中定义的....
/ * DMA回调函数处理P到U转移的产生事件。 * /
无效
CyFxSlFifoPtoUDmaCallback(
CyU3PDmaChannel * chHandle,
CyU3PDmaCbType_t类型,
CyU3PDmaCBInput_t *输入
)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
if(type == CY_U3P_DMA_CB_PROD_EVENT)
{
/ *这是向CPU发出的生产事件通知。收到每个缓冲区后收到此通知
*。除非明确提交,否则不会发送缓冲区
* out。如果
*是总线重置/ usb断开连接或者有任何应用程序错误,则调用将失败。 * /
char buffer [9] =MIND FLOW;
status = CyU3PDmaChannelCommitBuffer(chHandle,input-> buffer_p.count ,0);
if(status!= CY_U3P_SUCCESS)
{
CyU3PDebugPrint(4,CyU3PDmaChannelCommitBuffer失败,错误代码=%d \ n,状态);
}
/ *递增计数器。 * /
glDMATxCount ++;
}
}
我尝试过:
I have created a buffer...
char buffer[8]="mindflow";
and i tried to copy this buffer contents into another buffer buffer_p.count which is defined in nested structure....
/* DMA callback function to handle the produce events for P to U transfers. */
void
CyFxSlFifoPtoUDmaCallback (
CyU3PDmaChannel *chHandle,
CyU3PDmaCbType_t type,
CyU3PDmaCBInput_t *input
)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
if (type == CY_U3P_DMA_CB_PROD_EVENT)
{
/* This is a produce event notification to the CPU. This notification is
* received upon reception of every buffer. The buffer will not be sent
* out unless it is explicitly committed. The call shall fail if there
* is a bus reset / usb disconnect or if there is any application error. */
char buffer[9]= "MIND FLOW";
status = CyU3PDmaChannelCommitBuffer (chHandle, input->buffer_p.count, 0);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "CyU3PDmaChannelCommitBuffer failed, Error code = %d\n", status);
}
/* Increment the counter. */
glDMATxCount++;
}
}
What I have tried:
/* DMA callback function to handle the produce events for P to U transfers. */
void
CyFxSlFifoPtoUDmaCallback (
CyU3PDmaChannel *chHandle,
CyU3PDmaCbType_t type,
CyU3PDmaCBInput_t *input
)
{
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
if (type == CY_U3P_DMA_CB_PROD_EVENT)
{
/* This is a produce event notification to the CPU. This notification is
* received upon reception of every buffer. The buffer will not be sent
* out unless it is explicitly committed. The call shall fail if there
* is a bus reset / usb disconnect or if there is any application error. */
char buffer[9]= "MIND FLOW";
status = CyU3PDmaChannelCommitBuffer (chHandle, input->buffer_p.count, 0);
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "CyU3PDmaChannelCommitBuffer failed, Error code = %d\n", status);
}
/* Increment the counter. */
glDMATxCount++;
}
}
推荐答案
您可以使用memcpy [ ^ ]用于此目的的功能。
请注意您首先要分配(可能)目标缓冲区的内存(然后你必须释放动态分配的内存)。
You might use the memcpy[^] function for the purpose.
Please note you have first to allocate (possibly dynamic) memory for the destination buffer (and then you have to release the dynamically allocated memory).
回答的声明输入 - > buffer_p [。必须知道计数]
应该从哪里复制多少字节。请注意,我已将.count
放入括号中,因为名称表示它是计数/大小值而不是缓冲区。
好的,我搜索了它并在 https://github.com/nickdademo/cypress-fx3-sdk-linux/blob/master/firmware/u3p_firmware/inc/cyu3dma.h [ ^ ]:
To answer that the declaration ofinput->buffer_p[.count]
must be known and how many bytes should be copied from where to where. Note that I have put the.count
into brackets because the name indicates that it is a count/size value and not a buffer.
Okay, I searched for it and found it at https://github.com/nickdademo/cypress-fx3-sdk-linux/blob/master/firmware/u3p_firmware/inc/cyu3dma.h[^]:
typedef struct CyU3PDmaBuffer_t
{
uint8_t *buffer; /**< Pointer to the data buffer. */
uint16_t count; /**< Byte count of valid data in buffer. */
uint16_t size; /**< Actual size of the buffer in bytes. Should be a multiple of 16. */
uint16_t status; /**< Buffer status. This is a four bit data field defined by
CY_U3P_DMA_BUFFER_STATUS_MASK. This holds information like
whether the buffer is occupied, whether the buffer holds the
end of packet and whether the buffer encountered a DMA error. */
} CyU3PDmaBuffer_t;
/** \brief DMA channel callback input.
**Description**\n
This data structure is used to provide event specific information when a DMA callback
is called. This structure is defined as a union to facilitate future updates to the
DMA manager.
**\see
*\see CyU3PDmaBuffer_t
*\see CyU3PDmaCallback_t
*/
typedef union CyU3PDmaCBInput_t
{
CyU3PDmaBuffer_t buffer_p; /**< Data about the DMA buffer that caused the callback to be called. */
} CyU3PDmaCBInput_t;
据说你可以用
According to that you can copy the data to your buffer with
memcpy(buffer, input->buffer_p.buffer, input->buffer_p.count);
前提是你的缓冲区足够大(size> = 输入 - > buffer_p.count
)。
将数据复制到 input-> buffer_p
,反方向复制并在检查后相应地设置其他结构成员( count
, status
)足够的大小。
provided that your buffer is large enough (size >= input->buffer_p.count
).
To copy data to the input->buffer_p
, copy in the reverse direction and set the other structure members accordingly (count
, status
) after checking for sufficient size.
uint16_t bufSize = sizeof(buffer); // or whatever the actual size is
if (bufSize <= input->buffer_p.size)
{
input->buffer_p.count = bufSize;
input->buffer_p.status = theStatus; //
memcpy(input->buffer_p.buffer, buffer, bufSize);
}
但你必须遵守SDK的规格
[/ EDIT]
我不确定这里发出回调的方向。但你应该知道什么时候做这种低级别的东西。
But you have to comply with the specifications of the SDK
[/EDIT]
I'm not sure for which direction the callback is issued here. But you should know when doing such low level stuff.
这篇关于如何将缓冲区内容复制到结构中定义的另一个缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!