2023-02-07 19:33:05 +03:00
|
|
|
#include <furi_hal_version.h>
|
|
|
|
#include <furi_hal_usb_i.h>
|
|
|
|
#include <furi_hal_usb.h>
|
2022-02-10 21:17:41 +03:00
|
|
|
#include <furi_hal_power.h>
|
2023-05-29 19:05:57 +03:00
|
|
|
|
2022-03-30 18:23:40 +03:00
|
|
|
#include <stm32wbxx_ll_pwr.h>
|
2023-05-29 19:05:57 +03:00
|
|
|
#include <stm32wbxx_ll_rcc.h>
|
2021-10-06 12:24:09 +03:00
|
|
|
#include <furi.h>
|
2022-11-29 15:50:55 +03:00
|
|
|
#include <toolbox/api_lock.h>
|
2021-10-06 12:24:09 +03:00
|
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "FuriHalUsb"
|
|
|
|
|
2021-10-06 12:24:09 +03:00
|
|
|
#define USB_RECONNECT_DELAY 500
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
typedef enum {
|
|
|
|
UsbApiEventTypeSetConfig,
|
|
|
|
UsbApiEventTypeGetConfig,
|
|
|
|
UsbApiEventTypeLock,
|
|
|
|
UsbApiEventTypeUnlock,
|
|
|
|
UsbApiEventTypeIsLocked,
|
|
|
|
UsbApiEventTypeEnable,
|
|
|
|
UsbApiEventTypeDisable,
|
|
|
|
UsbApiEventTypeReinit,
|
|
|
|
UsbApiEventTypeSetStateCallback,
|
|
|
|
} UsbApiEventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FuriHalUsbStateCallback callback;
|
|
|
|
void* context;
|
|
|
|
} UsbApiEventDataStateCallback;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FuriHalUsbInterface* interface;
|
|
|
|
void* context;
|
|
|
|
} UsbApiEventDataInterface;
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
UsbApiEventDataStateCallback state_callback;
|
|
|
|
UsbApiEventDataInterface interface;
|
|
|
|
} UsbApiEventData;
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
bool bool_value;
|
|
|
|
void* void_value;
|
|
|
|
} UsbApiEventReturnData;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FuriApiLock lock;
|
|
|
|
UsbApiEventType type;
|
|
|
|
UsbApiEventData data;
|
|
|
|
UsbApiEventReturnData* return_data;
|
|
|
|
} UsbApiEventMessage;
|
|
|
|
|
2022-02-16 20:52:34 +03:00
|
|
|
typedef struct {
|
|
|
|
FuriThread* thread;
|
2022-11-29 15:50:55 +03:00
|
|
|
FuriMessageQueue* queue;
|
2022-02-16 20:52:34 +03:00
|
|
|
bool enabled;
|
|
|
|
bool connected;
|
2022-03-24 18:45:03 +03:00
|
|
|
bool mode_lock;
|
2022-11-29 15:50:55 +03:00
|
|
|
bool request_pending;
|
|
|
|
FuriHalUsbInterface* interface;
|
|
|
|
void* interface_context;
|
2022-02-16 20:52:34 +03:00
|
|
|
FuriHalUsbStateCallback callback;
|
2022-11-29 15:50:55 +03:00
|
|
|
void* callback_context;
|
2022-02-16 20:52:34 +03:00
|
|
|
} UsbSrv;
|
|
|
|
|
|
|
|
typedef enum {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbEventReset = (1 << 0),
|
|
|
|
UsbEventRequest = (1 << 1),
|
|
|
|
UsbEventMessage = (1 << 2),
|
2022-02-16 20:52:34 +03:00
|
|
|
} UsbEvent;
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
#define USB_SRV_ALL_EVENTS (UsbEventReset | UsbEventRequest | UsbEventMessage)
|
2022-02-16 20:52:34 +03:00
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
PLACE_IN_SECTION("MB_MEM2") static UsbSrv usb = {0};
|
2023-03-28 10:34:49 +03:00
|
|
|
PLACE_IN_SECTION("MB_MEM2") static uint32_t ubuf[0x20];
|
|
|
|
PLACE_IN_SECTION("MB_MEM2") usbd_device udev;
|
2021-10-06 12:24:09 +03:00
|
|
|
|
|
|
|
static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
|
|
|
|
|
2022-02-16 20:52:34 +03:00
|
|
|
static int32_t furi_hal_usb_thread(void* context);
|
2022-01-05 19:10:18 +03:00
|
|
|
static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length);
|
2022-01-21 19:55:44 +03:00
|
|
|
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
|
2022-01-05 19:10:18 +03:00
|
|
|
static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep);
|
|
|
|
static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep);
|
2021-10-06 12:24:09 +03:00
|
|
|
|
|
|
|
/* Low-level init */
|
|
|
|
void furi_hal_usb_init(void) {
|
2023-05-29 19:05:57 +03:00
|
|
|
LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLLSAI1);
|
|
|
|
|
2021-10-06 12:24:09 +03:00
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
LL_PWR_EnableVddUSB();
|
2022-01-05 19:10:18 +03:00
|
|
|
|
2021-10-06 12:24:09 +03:00
|
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
|
|
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
|
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_10;
|
|
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
usbd_init(&udev, &usbd_hw, USB_EP0_SIZE, ubuf, sizeof(ubuf));
|
2023-05-29 19:05:57 +03:00
|
|
|
|
|
|
|
FURI_CRITICAL_ENTER();
|
2021-10-06 12:24:09 +03:00
|
|
|
usbd_enable(&udev, true);
|
2023-05-29 19:05:57 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2021-10-06 12:24:09 +03:00
|
|
|
|
|
|
|
usbd_reg_descr(&udev, usb_descriptor_get);
|
|
|
|
usbd_reg_event(&udev, usbd_evt_susp, susp_evt);
|
|
|
|
usbd_reg_event(&udev, usbd_evt_wkup, wkup_evt);
|
2022-01-21 19:55:44 +03:00
|
|
|
// Reset callback will be enabled after first mode change to avoid getting false reset events
|
2021-10-06 12:24:09 +03:00
|
|
|
|
2022-02-16 20:52:34 +03:00
|
|
|
usb.enabled = false;
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.interface = NULL;
|
2022-03-30 18:23:40 +03:00
|
|
|
NVIC_SetPriority(USB_LP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
NVIC_SetPriority(USB_HP_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));
|
2021-10-06 12:24:09 +03:00
|
|
|
NVIC_EnableIRQ(USB_LP_IRQn);
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 16:29:51 +03:00
|
|
|
NVIC_EnableIRQ(USB_HP_IRQn);
|
2021-10-06 12:24:09 +03:00
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.queue = furi_message_queue_alloc(1, sizeof(UsbApiEventMessage));
|
2022-11-23 15:49:17 +03:00
|
|
|
usb.thread = furi_thread_alloc_ex("UsbDriver", 1024, furi_hal_usb_thread, NULL);
|
|
|
|
furi_thread_mark_as_service(usb.thread);
|
2022-02-16 20:52:34 +03:00
|
|
|
furi_thread_start(usb.thread);
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-10-06 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
static void furi_hal_usb_send_message(UsbApiEventMessage* message) {
|
|
|
|
furi_message_queue_put(usb.queue, message, FuriWaitForever);
|
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
|
|
|
api_lock_wait_unlock_and_free(message->lock);
|
|
|
|
}
|
2022-03-24 18:45:03 +03:00
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
bool furi_hal_usb_set_config(FuriHalUsbInterface* new_if, void* ctx) {
|
|
|
|
UsbApiEventReturnData return_data = {
|
|
|
|
.bool_value = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeSetConfig,
|
|
|
|
.data.interface =
|
|
|
|
{
|
|
|
|
.interface = new_if,
|
|
|
|
.context = ctx,
|
|
|
|
},
|
|
|
|
.return_data = &return_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
|
|
|
return return_data.bool_value;
|
2022-01-21 19:55:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FuriHalUsbInterface* furi_hal_usb_get_config() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventReturnData return_data = {
|
|
|
|
.void_value = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeGetConfig,
|
|
|
|
.return_data = &return_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
|
|
|
return return_data.void_value;
|
2021-10-13 19:38:24 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 18:45:03 +03:00
|
|
|
void furi_hal_usb_lock() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeLock,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
2022-03-24 18:45:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_usb_unlock() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeUnlock,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
2022-03-24 18:45:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_usb_is_locked() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventReturnData return_data = {
|
|
|
|
.bool_value = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeIsLocked,
|
|
|
|
.return_data = &return_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
|
|
|
return return_data.bool_value;
|
2022-03-24 18:45:03 +03:00
|
|
|
}
|
|
|
|
|
2021-10-06 12:24:09 +03:00
|
|
|
void furi_hal_usb_disable() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeDisable,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
2021-10-06 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_usb_enable() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeEnable,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_usb_reinit() {
|
2022-11-29 15:50:55 +03:00
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeReinit,
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
|
|
|
|
UsbApiEventMessage msg = {
|
|
|
|
.lock = api_lock_alloc_locked(),
|
|
|
|
.type = UsbApiEventTypeSetStateCallback,
|
|
|
|
.data.state_callback =
|
|
|
|
{
|
|
|
|
.callback = cb,
|
|
|
|
.context = ctx,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
furi_hal_usb_send_message(&msg);
|
2021-10-06 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get device / configuration descriptors */
|
2022-01-05 19:10:18 +03:00
|
|
|
static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
|
2021-10-06 12:24:09 +03:00
|
|
|
const uint8_t dtype = req->wValue >> 8;
|
|
|
|
const uint8_t dnumber = req->wValue & 0xFF;
|
|
|
|
const void* desc;
|
|
|
|
uint16_t len = 0;
|
2022-11-29 15:50:55 +03:00
|
|
|
if(usb.interface == NULL) return usbd_fail;
|
2021-10-06 12:24:09 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
switch(dtype) {
|
2021-10-06 12:24:09 +03:00
|
|
|
case USB_DTYPE_DEVICE:
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventRequest);
|
2022-02-16 20:52:34 +03:00
|
|
|
if(usb.callback != NULL) {
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.callback(FuriHalUsbStateEventDescriptorRequest, usb.callback_context);
|
2022-01-21 19:55:44 +03:00
|
|
|
}
|
2022-11-29 15:50:55 +03:00
|
|
|
desc = usb.interface->dev_descr;
|
2021-10-06 12:24:09 +03:00
|
|
|
break;
|
|
|
|
case USB_DTYPE_CONFIGURATION:
|
2022-11-29 15:50:55 +03:00
|
|
|
desc = usb.interface->cfg_descr;
|
|
|
|
len = ((struct usb_string_descriptor*)(usb.interface->cfg_descr))->wString[0];
|
2021-10-06 12:24:09 +03:00
|
|
|
break;
|
|
|
|
case USB_DTYPE_STRING:
|
2022-01-05 19:10:18 +03:00
|
|
|
if(dnumber == UsbDevLang) {
|
2021-10-06 12:24:09 +03:00
|
|
|
desc = &dev_lang_desc;
|
2022-11-29 15:50:55 +03:00
|
|
|
} else if((dnumber == UsbDevManuf) && (usb.interface->str_manuf_descr != NULL)) {
|
|
|
|
desc = usb.interface->str_manuf_descr;
|
|
|
|
} else if((dnumber == UsbDevProduct) && (usb.interface->str_prod_descr != NULL)) {
|
|
|
|
desc = usb.interface->str_prod_descr;
|
|
|
|
} else if((dnumber == UsbDevSerial) && (usb.interface->str_serial_descr != NULL)) {
|
|
|
|
desc = usb.interface->str_serial_descr;
|
2022-01-05 19:10:18 +03:00
|
|
|
} else
|
2021-10-06 12:24:09 +03:00
|
|
|
return usbd_fail;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return usbd_fail;
|
|
|
|
}
|
2022-01-05 19:10:18 +03:00
|
|
|
if(desc == NULL) return usbd_fail;
|
2021-10-06 12:24:09 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(len == 0) {
|
2021-10-06 12:24:09 +03:00
|
|
|
len = ((struct usb_header_descriptor*)desc)->bLength;
|
|
|
|
}
|
|
|
|
*address = (void*)desc;
|
|
|
|
*length = len;
|
|
|
|
return usbd_ack;
|
|
|
|
}
|
|
|
|
|
2022-01-21 19:55:44 +03:00
|
|
|
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(dev);
|
|
|
|
UNUSED(event);
|
|
|
|
UNUSED(ep);
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventReset);
|
2022-02-16 20:52:34 +03:00
|
|
|
if(usb.callback != NULL) {
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.callback(FuriHalUsbStateEventReset, usb.callback_context);
|
2022-01-21 19:55:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(dev);
|
|
|
|
UNUSED(event);
|
|
|
|
UNUSED(ep);
|
2022-11-29 15:50:55 +03:00
|
|
|
if((usb.interface != NULL) && (usb.connected == true)) {
|
2022-02-16 20:52:34 +03:00
|
|
|
usb.connected = false;
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.interface->suspend(&udev);
|
2022-02-10 21:17:41 +03:00
|
|
|
|
|
|
|
furi_hal_power_insomnia_exit();
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
2022-02-16 20:52:34 +03:00
|
|
|
if(usb.callback != NULL) {
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.callback(FuriHalUsbStateEventSuspend, usb.callback_context);
|
2022-01-21 19:55:44 +03:00
|
|
|
}
|
2021-10-06 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(dev);
|
|
|
|
UNUSED(event);
|
|
|
|
UNUSED(ep);
|
2022-11-29 15:50:55 +03:00
|
|
|
if((usb.interface != NULL) && (usb.connected == false)) {
|
2022-02-16 20:52:34 +03:00
|
|
|
usb.connected = true;
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.interface->wakeup(&udev);
|
2022-02-10 21:17:41 +03:00
|
|
|
|
|
|
|
furi_hal_power_insomnia_enter();
|
2022-01-05 19:10:18 +03:00
|
|
|
}
|
2022-02-16 20:52:34 +03:00
|
|
|
if(usb.callback != NULL) {
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.callback(FuriHalUsbStateEventWakeup, usb.callback_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_process_mode_start(FuriHalUsbInterface* interface, void* context) {
|
|
|
|
if(usb.interface != NULL) {
|
|
|
|
usb.interface->deinit(&udev);
|
|
|
|
}
|
|
|
|
|
|
|
|
__disable_irq();
|
|
|
|
usb.interface = interface;
|
|
|
|
usb.interface_context = context;
|
|
|
|
__enable_irq();
|
|
|
|
|
|
|
|
if(interface != NULL) {
|
|
|
|
interface->init(&udev, interface, context);
|
|
|
|
usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
|
|
|
|
FURI_LOG_I(TAG, "USB Mode change done");
|
|
|
|
usb.enabled = true;
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
static void usb_process_mode_change(FuriHalUsbInterface* interface, void* context) {
|
2023-02-13 18:43:29 +03:00
|
|
|
if((interface != usb.interface) || (context != usb.interface_context)) {
|
2022-11-29 15:50:55 +03:00
|
|
|
if(usb.enabled) {
|
|
|
|
// Disable current interface
|
|
|
|
susp_evt(&udev, 0, 0);
|
|
|
|
usbd_connect(&udev, false);
|
|
|
|
usb.enabled = false;
|
|
|
|
furi_delay_ms(USB_RECONNECT_DELAY);
|
|
|
|
}
|
|
|
|
usb_process_mode_start(interface, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_process_mode_reinit() {
|
|
|
|
// Temporary disable callback to avoid getting false reset events
|
|
|
|
usbd_reg_event(&udev, usbd_evt_reset, NULL);
|
|
|
|
FURI_LOG_I(TAG, "USB Reinit");
|
|
|
|
susp_evt(&udev, 0, 0);
|
|
|
|
usbd_connect(&udev, false);
|
|
|
|
usb.enabled = false;
|
|
|
|
|
2023-05-29 19:05:57 +03:00
|
|
|
FURI_CRITICAL_ENTER();
|
2022-11-29 15:50:55 +03:00
|
|
|
usbd_enable(&udev, false);
|
|
|
|
usbd_enable(&udev, true);
|
2023-05-29 19:05:57 +03:00
|
|
|
FURI_CRITICAL_EXIT();
|
2022-11-29 15:50:55 +03:00
|
|
|
|
|
|
|
furi_delay_ms(USB_RECONNECT_DELAY);
|
|
|
|
usb_process_mode_start(usb.interface, usb.interface_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool usb_process_set_config(FuriHalUsbInterface* interface, void* context) {
|
|
|
|
if(usb.mode_lock) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
usb_process_mode_change(interface, context);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_process_enable(bool enable) {
|
|
|
|
if(enable) {
|
|
|
|
if((!usb.enabled) && (usb.interface != NULL)) {
|
|
|
|
usbd_connect(&udev, true);
|
|
|
|
usb.enabled = true;
|
|
|
|
FURI_LOG_I(TAG, "USB Enable");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(usb.enabled) {
|
|
|
|
susp_evt(&udev, 0, 0);
|
|
|
|
usbd_connect(&udev, false);
|
|
|
|
usb.enabled = false;
|
|
|
|
usb.request_pending = false;
|
|
|
|
FURI_LOG_I(TAG, "USB Disable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_process_message(UsbApiEventMessage* message) {
|
|
|
|
switch(message->type) {
|
|
|
|
case UsbApiEventTypeSetConfig:
|
|
|
|
message->return_data->bool_value = usb_process_set_config(
|
|
|
|
message->data.interface.interface, message->data.interface.context);
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeGetConfig:
|
|
|
|
message->return_data->void_value = usb.interface;
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeLock:
|
|
|
|
FURI_LOG_I(TAG, "Mode lock");
|
|
|
|
usb.mode_lock = true;
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeUnlock:
|
|
|
|
FURI_LOG_I(TAG, "Mode unlock");
|
|
|
|
usb.mode_lock = false;
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeIsLocked:
|
|
|
|
message->return_data->bool_value = usb.mode_lock;
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeDisable:
|
|
|
|
usb_process_enable(false);
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeEnable:
|
|
|
|
usb_process_enable(true);
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeReinit:
|
|
|
|
usb_process_mode_reinit();
|
|
|
|
break;
|
|
|
|
case UsbApiEventTypeSetStateCallback:
|
|
|
|
usb.callback = message->data.state_callback.callback;
|
|
|
|
usb.callback_context = message->data.state_callback.context;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
api_lock_unlock(message->lock);
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:52:34 +03:00
|
|
|
static int32_t furi_hal_usb_thread(void* context) {
|
2022-05-06 16:37:10 +03:00
|
|
|
UNUSED(context);
|
2022-02-16 20:52:34 +03:00
|
|
|
uint8_t usb_wait_time = 0;
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
if(furi_message_queue_get_count(usb.queue) > 0) {
|
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb.thread), UsbEventMessage);
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while(true) {
|
2022-07-20 13:56:33 +03:00
|
|
|
uint32_t flags = furi_thread_flags_wait(USB_SRV_ALL_EVENTS, FuriFlagWaitAny, 500);
|
2022-11-29 15:50:55 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
UsbApiEventMessage message;
|
|
|
|
if(furi_message_queue_get(usb.queue, &message, 0) == FuriStatusOk) {
|
|
|
|
usb_process_message(&message);
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
2022-11-29 15:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if((flags & FuriFlagError) == 0) {
|
|
|
|
if(flags & UsbEventReset) {
|
2022-03-25 19:07:37 +03:00
|
|
|
if(usb.enabled) {
|
2022-11-29 15:50:55 +03:00
|
|
|
usb.request_pending = true;
|
2022-03-25 19:07:37 +03:00
|
|
|
usb_wait_time = 0;
|
|
|
|
}
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
2022-11-29 15:50:55 +03:00
|
|
|
if(flags & UsbEventRequest) {
|
|
|
|
usb.request_pending = false;
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
2022-11-29 15:50:55 +03:00
|
|
|
} else if(usb.request_pending) {
|
2022-02-16 20:52:34 +03:00
|
|
|
usb_wait_time++;
|
|
|
|
if(usb_wait_time > 4) {
|
2022-11-29 15:50:55 +03:00
|
|
|
usb_process_mode_reinit();
|
|
|
|
usb.request_pending = false;
|
2022-02-16 20:52:34 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-21 19:55:44 +03:00
|
|
|
}
|
2022-02-16 20:52:34 +03:00
|
|
|
return 0;
|
2021-10-06 12:24:09 +03:00
|
|
|
}
|