forked from flipperdevices/flipperzero-catalog-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "main.h" | ||
#include "usb.h" | ||
#include <furi.h> | ||
#include <furi_hal.h> | ||
|
||
// Define MTP specific request constants | ||
#define MTP_REQ_GET_DEVICE_STATUS 0x67 | ||
#define MTP_REQ_SEND_DATA 0x68 | ||
#define MTP_REQ_GET_DATA 0x69 | ||
|
||
AppMTP* global_mtp; | ||
|
||
typedef enum { | ||
EventExit = 1 << 0, | ||
EventReset = 1 << 1, | ||
EventRxTx = 1 << 2, | ||
|
||
EventAll = EventExit | EventReset | EventRxTx, | ||
} MTPEvent; | ||
|
||
int32_t usb_mtp_worker(void* ctx) { | ||
AppMTP* mtp = ctx; | ||
usbd_device* dev = mtp->dev; | ||
while(true) { | ||
furi_thread_flags_wait(EventAll, FuriFlagWaitAny, FuriWaitForever); | ||
if(furi_thread_flags_get() & EventExit) break; | ||
|
||
if(furi_thread_flags_get() & EventReset) { | ||
furi_thread_flags_clear(EventReset); | ||
furi_hal_usb_set_config(mtp->old_usb, NULL); | ||
} | ||
|
||
if(furi_thread_flags_get() & EventRxTx) { | ||
furi_thread_flags_clear(EventRxTx); | ||
// Handle MTP RX/TX data here | ||
// Implement the logic for processing MTP requests, sending responses, etc. | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) { | ||
UNUSED(intf); | ||
AppMTP* mtp = ctx; | ||
global_mtp = mtp; | ||
|
||
usbd_reg_control(dev, usb_mtp_control); | ||
usbd_connect(dev, true); | ||
|
||
mtp->worker_thread = furi_thread_alloc(); | ||
furi_thread_set_name(mtp->worker_thread, "FlipperMTPUsb"); | ||
furi_thread_set_stack_size(mtp->worker_thread, 1024); | ||
furi_thread_set_context(mtp->worker_thread, ctx); | ||
furi_thread_set_callback(mtp->worker_thread, usb_mtp_worker); | ||
furi_thread_start(mtp->worker_thread); | ||
} | ||
|
||
usbd_respond usb_mtp_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) { | ||
UNUSED(callback); | ||
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) != | ||
(USB_REQ_INTERFACE | USB_REQ_CLASS)) { | ||
if(global_mtp != NULL) { | ||
global_mtp->usb_connected = false; | ||
} | ||
return usbd_fail; | ||
} | ||
switch(req->bRequest) {}; | ||
return usbd_fail; | ||
} | ||
|
||
void usb_deinit(usbd_device* dev) { | ||
usbd_reg_control(dev, NULL); | ||
|
||
AppMTP* mtp = global_mtp; | ||
if(!mtp || mtp->dev != dev) { | ||
FURI_LOG_E("MTP", "deinit mtp_cur leak"); | ||
return; | ||
} | ||
|
||
global_mtp = NULL; | ||
|
||
furi_assert(mtp->worker_thread); | ||
furi_thread_flags_set(furi_thread_get_id(mtp->worker_thread), EventExit); | ||
furi_thread_join(mtp->worker_thread); | ||
furi_thread_free(mtp->worker_thread); | ||
mtp->worker_thread = NULL; | ||
} | ||
|
||
void usb_wakeup(usbd_device* dev) { | ||
UNUSED(dev); | ||
} | ||
|
||
void usb_suspend(usbd_device* dev) { | ||
AppMTP* mtp = global_mtp; | ||
if(!mtp || mtp->dev != dev) return; | ||
furi_thread_flags_set(furi_thread_get_id(mtp->worker_thread), EventReset); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#pragma once | ||
#include <furi.h> | ||
#include <furi_hal.h> | ||
#include <furi_hal_usb.h> | ||
|
||
/* === START furi_hal_usb_i.h === */ | ||
// https://github.com/flipperdevices/flipperzero-firmware/blob/03196fa11007c0f1e002cbb0b82102d8492456b5/targets/f7/furi_hal/furi_hal_usb_i.h#L5 | ||
#define USB_EP0_SIZE 8 | ||
|
||
enum UsbDevDescStr { | ||
UsbDevLang = 0, | ||
UsbDevManuf = 1, | ||
UsbDevProduct = 2, | ||
UsbDevSerial = 3, | ||
}; | ||
/* === END furi_hal_usb_i.h === */ | ||
|
||
void usb_mtp_init(void); | ||
void usb_mtp_deinit(void); | ||
void usb_mtp_wakeup(void); | ||
void usb_mtp_suspend(void); | ||
|
||
#define USB_EP0_SIZE 64 | ||
#define USB_MTP_RX_EP 0x01 | ||
#define USB_MTP_TX_EP 0x81 | ||
#define USB_MTP_RX_EP_SIZE 64 | ||
#define USB_MTP_TX_EP_SIZE 64 | ||
|
||
static const struct usb_string_descriptor dev_manuf_desc = USB_STRING_DESC("Flipper Devices Inc."); | ||
static const struct usb_string_descriptor dev_prod_desc = USB_STRING_DESC("MTP Device"); | ||
|
||
struct MtpDescriptor { | ||
struct usb_config_descriptor config; | ||
struct usb_interface_descriptor intf; | ||
struct usb_endpoint_descriptor ep_rx; | ||
struct usb_endpoint_descriptor ep_tx; | ||
} __attribute__((packed)); | ||
|
||
static const struct usb_device_descriptor usb_mtp_dev_descr = { | ||
.bLength = sizeof(struct usb_device_descriptor), | ||
.bDescriptorType = USB_DTYPE_DEVICE, | ||
.bcdUSB = VERSION_BCD(2, 0, 0), | ||
.bDeviceClass = USB_CLASS_STILL_IMAGE, // MTP falls under Still Image class | ||
.bDeviceSubClass = 1, // Subclass for MTP | ||
.bDeviceProtocol = 1, // Protocol for MTP | ||
.bMaxPacketSize0 = USB_EP0_SIZE, | ||
.idVendor = 0x0483, // STMicroelectronics | ||
.idProduct = 0x5741, // Custom Product ID | ||
.bcdDevice = VERSION_BCD(1, 0, 0), | ||
.iManufacturer = UsbDevManuf, // UsbDevManuf | ||
.iProduct = UsbDevProduct, // UsbDevProduct | ||
.iSerialNumber = UsbDevSerial, // UsbDevSerial | ||
.bNumConfigurations = 1, | ||
}; | ||
|
||
static const struct MtpDescriptor usb_mtp_cfg_descr = { | ||
.config = | ||
{ | ||
.bLength = sizeof(struct usb_config_descriptor), | ||
.bDescriptorType = USB_DTYPE_CONFIGURATION, | ||
.wTotalLength = sizeof(struct MtpDescriptor), | ||
.bNumInterfaces = 1, | ||
.bConfigurationValue = 1, | ||
.iConfiguration = NO_DESCRIPTOR, | ||
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED, | ||
.bMaxPower = USB_CFG_POWER_MA(100), | ||
}, | ||
.intf = | ||
{ | ||
.bLength = sizeof(struct usb_interface_descriptor), | ||
.bDescriptorType = USB_DTYPE_INTERFACE, | ||
.bInterfaceNumber = 0, | ||
.bAlternateSetting = 0, | ||
.bNumEndpoints = 2, | ||
.bInterfaceClass = USB_CLASS_STILL_IMAGE, | ||
.bInterfaceSubClass = 1, // Subclass for MTP | ||
.bInterfaceProtocol = 1, // Protocol for MTP | ||
.iInterface = NO_DESCRIPTOR, | ||
}, | ||
.ep_rx = | ||
{ | ||
.bLength = sizeof(struct usb_endpoint_descriptor), | ||
.bDescriptorType = USB_DTYPE_ENDPOINT, | ||
.bEndpointAddress = USB_MTP_RX_EP, | ||
.bmAttributes = USB_EPTYPE_BULK, | ||
.wMaxPacketSize = USB_MTP_RX_EP_SIZE, | ||
.bInterval = 0, | ||
}, | ||
.ep_tx = | ||
{ | ||
.bLength = sizeof(struct usb_endpoint_descriptor), | ||
.bDescriptorType = USB_DTYPE_ENDPOINT, | ||
.bEndpointAddress = USB_MTP_TX_EP, | ||
.bmAttributes = USB_EPTYPE_BULK, | ||
.wMaxPacketSize = USB_MTP_TX_EP_SIZE, | ||
.bInterval = 0, | ||
}, | ||
}; | ||
|
||
FuriHalUsbInterface usb_mtp_interface = { | ||
.init = usb_mtp_init, | ||
.deinit = usb_mtp_deinit, | ||
.wakeup = usb_mtp_wakeup, | ||
.suspend = usb_mtp_suspend, | ||
|
||
.dev_descr = (struct usb_device_descriptor*)&usb_mtp_dev_descr, | ||
|
||
.str_manuf_descr = (void*)&dev_manuf_desc, | ||
.str_prod_descr = (void*)&dev_prod_desc, | ||
.str_serial_descr = NULL, | ||
|
||
.cfg_descr = (void*)&usb_mtp_cfg_descr, | ||
}; |