我正在 ATSAMD21 上研究 USB CDC。我正在使用的代码是 D21 上 USB CD Echo 的 ATMEL START 示例。我在 atmel 工作室工作。
要求:
在我的应用程序中,主机向设备发送数据,我需要读取该数据并从不同的功能向主机发回不同的数据。
在回显示例中,数据接收和传输使用回调。我不知道如何在另一个函数中使用 write 调用。在这里,我附上下面的代码
读取并回显数据:
在此函数中,它用于读取数据并将其回显给主机。
static bool usb_device_cb_state_c(usb_cdc_control_signal_t state)
{
if (state.rs232.DTR ) {
/* Callbacks must be registered after endpoint allocation */
cdcdf_acm_register_callback(CDCDF_ACM_CB_READ, (FUNC_PTR)usb_device_cb_bulk_out);
cdcdf_acm_register_callback(CDCDF_ACM_CB_WRITE, (FUNC_PTR)usb_device_cb_bulk_in);
/* Start Rx */
cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer));
}
/* No error. */
return false;
////////////////////////////////////
从主机读取数据:
static bool usb_device_cb_bulk_out(const uint8_t ep, const enum
usb_xfer_code rc, const uint32_t count)
{
cdcdf_acm_write((uint8_t *)usbd_cdc_buffer, count);
return false;
写回数据:
static bool usb_device_cb_bulk_in(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)
{
/* Echo data. */
cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer));
/* No error. */
return false;
我需要在外部的另一个函数中使用这个读取调用。我直接在另一个函数中使用读取调用,它无法发送数据。 我怎样才能在另一个函数中使用这个调用。我们将不胜感激。
这里的回调会指向读写函数。
最佳答案
这很有趣!
在 main
循环中,使用堆栈分配的 char
缓冲区:
const char hello[] = "Hi There!\r\n";
cdcdf_acm_write((void*)hello, 11);
对我有用,但奇怪的是使用指向字符串常量的指针:
const char* hello = "Hi There!\r\n";
cdcdf_acm_write((void*)hello, 11);
不输出任何东西!同样,静态 const
缓冲区
const char hello[] = "Hi There!\r\n";
int main(void)
{
// init ...
while (1)
{
delay_ms(200);
cdcdf_acm_write((void*)hello, 11);
}
}
也不起作用,但是一个非常量静态缓冲区:
char hello[] = "Hi There!\r\n";
int main(void)
{
// init ...
while (1)
{
delay_ms(200);
cdcdf_acm_write((void*)hello, 11);
}
}
确实如此!
我的假设是 cdcdf_acm_write()
在 USB 堆栈的某处有问题,无法直接从闪存读取数据,这似乎是一个非常意外的陷阱,尤其是输出字符串常量可能是实现通信功能时要做的第一件事!
关于c - 在不同函数的回调中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54473686/