+\r
+\r
+//***************************************************************************\r
+// Interface to the main program\r
+//***************************************************************************\r
+\r
+// The function to receive a command from the client via USB\r
+bool cmd_receive(UsbCommand* cmd) {\r
+\r
+ // Check if there is a usb packet available\r
+ if (!usb_poll())\r
+ return false;\r
+\r
+ // Try to retrieve the available command frame\r
+ size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand));\r
+\r
+ // Check if the transfer was complete\r
+ if (rxlen != sizeof(UsbCommand))\r
+ return false;\r
+\r
+ // Received command successfully\r
+ return true;\r
+}\r
+\r
+\r
+// The function to send a response to the client via USB\r
+bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {\r
+ UsbCommand txcmd;\r
+\r
+ for (size_t i = 0; i < sizeof(UsbCommand); i++) {\r
+ ((uint8_t*)&txcmd)[i] = 0x00;\r
+ }\r
+\r
+ // Compose the outgoing command frame\r
+ txcmd.cmd = cmd;\r
+ txcmd.arg[0] = arg0;\r
+ txcmd.arg[1] = arg1;\r
+ txcmd.arg[2] = arg2;\r
+\r
+ // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+ if (data && len) {\r
+ len = MIN(len, USB_CMD_DATA_SIZE);\r
+ for (size_t i = 0; i < len; i++) {\r
+ txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
+ }\r
+ }\r
+\r
+ // Send frame and make sure all bytes are transmitted\r
+ if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;\r
+\r
+ return true;\r
+}\r