Hello everyone,
I'm currently working with a Teensy 4.0 and doing USB communication. This is generally working, but now I have a problem which I cannot answer myself. It is about the way the USB library handles buffers. I need to do some sendControlPacket(...) calls and this requires a buffer with the bytes sent. My question is how the library handles the buffer passed. It works with a local static buffer, but what does the library do inside? Does it copy the bytes provided? Does it have memory management or just assume that the address with the buffer just exists when the library gets around to sending the bytes out? From what I've seen the library sends data asynchronously.
The question came as I experienced a weird behaviour with it. Im doing this and it works:
When I wrap this exact code in a function with the declaration
it doesn't work anymore. But as soon as I put buf as a parameter like so:
it works again.
My knowledge about C/C++ is okay, but not great and my only guess is that the scope of the function disappears too fast, while the loop scope still exists while the data is read. But this also doesn't seem right. Maybe I'm totally on the wrong path and I just made a error, but any clues are welcome
Also how would I program a system where I could manage the buffers if I have to provide them from the outside, when I have no way of knowing when I'm good to delete the buffer.
Thanks in advance!
I'm currently working with a Teensy 4.0 and doing USB communication. This is generally working, but now I have a problem which I cannot answer myself. It is about the way the USB library handles buffers. I need to do some sendControlPacket(...) calls and this requires a buffer with the bytes sent. My question is how the library handles the buffer passed. It works with a local static buffer, but what does the library do inside? Does it copy the bytes provided? Does it have memory management or just assume that the address with the buffer just exists when the library gets around to sending the bytes out? From what I've seen the library sends data asynchronously.
The question came as I experienced a weird behaviour with it. Im doing this and it works:
Code:
uint8_t buf[] = {
0xB3, // 0xB3
3, // Length
2, // Host type
1, // Reserved
1 // Reserved
};
bool result = hid.sendControlPacket(0x21, 0x09, 0x0300, 2, 5, buf);
When I wrap this exact code in a function with the declaration
Code:
bool send_b3_packet(USBHIDParser hid_parser);
Code:
bool send_b3_packet(USBHIDParser hid_parser, uint8_t* buf);
My knowledge about C/C++ is okay, but not great and my only guess is that the scope of the function disappears too fast, while the loop scope still exists while the data is read. But this also doesn't seem right. Maybe I'm totally on the wrong path and I just made a error, but any clues are welcome
Also how would I program a system where I could manage the buffers if I have to provide them from the outside, when I have no way of knowing when I'm good to delete the buffer.
Thanks in advance!
Last edited: