2022-01-05 16:10:18 +00:00
|
|
|
#include "furi_hal_version.h"
|
|
|
|
#include "furi_hal_usb_i.h"
|
|
|
|
#include "furi_hal_usb.h"
|
2021-10-06 09:24:09 +00:00
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "FuriHalUsb"
|
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
#define USB_RECONNECT_DELAY 500
|
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
static FuriHalUsbInterface* usb_if_cur;
|
|
|
|
static FuriHalUsbInterface* usb_if_next;
|
2021-10-06 09:24:09 +00:00
|
|
|
|
|
|
|
static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
|
|
|
|
|
|
|
|
static uint32_t ubuf[0x20];
|
|
|
|
usbd_device udev;
|
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
static FuriHalUsbStateCallback callback;
|
|
|
|
static void* cb_ctx;
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length);
|
2022-01-21 16:55:44 +00:00
|
|
|
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
|
2022-01-05 16:10:18 +00: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 09:24:09 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
struct UsbCfg {
|
2021-10-06 09:24:09 +00:00
|
|
|
osTimerId_t reconnect_tmr;
|
|
|
|
bool enabled;
|
2021-10-21 18:12:20 +00:00
|
|
|
bool connected;
|
2022-01-21 16:55:44 +00:00
|
|
|
bool mode_changing;
|
2021-10-06 09:24:09 +00:00
|
|
|
} usb_config;
|
|
|
|
|
|
|
|
static void furi_hal_usb_tmr_cb(void* context);
|
|
|
|
|
|
|
|
/* Low-level init */
|
|
|
|
void furi_hal_usb_init(void) {
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
LL_PWR_EnableVddUSB();
|
2022-01-05 16:10:18 +00:00
|
|
|
|
2021-10-06 09:24:09 +00: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));
|
|
|
|
usbd_enable(&udev, true);
|
|
|
|
|
|
|
|
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 16:55:44 +00:00
|
|
|
// Reset callback will be enabled after first mode change to avoid getting false reset events
|
2021-10-06 09:24:09 +00:00
|
|
|
|
|
|
|
usb_config.enabled = false;
|
|
|
|
usb_config.reconnect_tmr = NULL;
|
|
|
|
HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
|
|
|
|
NVIC_EnableIRQ(USB_LP_IRQn);
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
void furi_hal_usb_set_config(FuriHalUsbInterface* new_if) {
|
|
|
|
if((new_if != usb_if_cur) && (usb_config.enabled)) { // Interface mode change - first stage
|
|
|
|
usb_config.mode_changing = true;
|
|
|
|
usb_if_next = new_if;
|
|
|
|
if(usb_config.reconnect_tmr == NULL)
|
|
|
|
usb_config.reconnect_tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
|
|
|
|
furi_hal_usb_disable();
|
|
|
|
usb_config.mode_changing = true;
|
|
|
|
osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
|
|
|
|
} else if(
|
|
|
|
(usb_config.mode_changing) &&
|
|
|
|
(usb_if_next != new_if)) { // Last interface mode change wasn't completed
|
|
|
|
osTimerStop(usb_config.reconnect_tmr);
|
|
|
|
usb_if_next = new_if;
|
|
|
|
osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
|
|
|
|
} else { // Interface mode change - second stage
|
|
|
|
if(usb_if_cur != NULL) usb_if_cur->deinit(&udev);
|
|
|
|
if(new_if != NULL) {
|
|
|
|
new_if->init(&udev, new_if);
|
|
|
|
usbd_reg_event(&udev, usbd_evt_reset, reset_evt);
|
|
|
|
FURI_LOG_I(TAG, "USB Mode change done");
|
|
|
|
usb_config.enabled = true;
|
|
|
|
usb_if_cur = new_if;
|
|
|
|
usb_config.mode_changing = false;
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
void furi_hal_usb_reinit() {
|
|
|
|
// Temporary disable callback to avoid getting false reset events
|
|
|
|
usbd_reg_event(&udev, usbd_evt_reset, NULL);
|
|
|
|
FURI_LOG_I(TAG, "USB Reinit");
|
|
|
|
furi_hal_usb_disable();
|
|
|
|
usbd_enable(&udev, false);
|
|
|
|
usbd_enable(&udev, true);
|
|
|
|
if(usb_config.reconnect_tmr == NULL)
|
|
|
|
usb_config.reconnect_tmr = osTimerNew(furi_hal_usb_tmr_cb, osTimerOnce, NULL, NULL);
|
|
|
|
usb_config.mode_changing = true;
|
|
|
|
usb_if_next = usb_if_cur;
|
|
|
|
osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
FuriHalUsbInterface* furi_hal_usb_get_config() {
|
2021-11-21 15:17:43 +00:00
|
|
|
return usb_if_cur;
|
2021-10-13 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
void furi_hal_usb_disable() {
|
2022-01-05 16:10:18 +00:00
|
|
|
if(usb_config.enabled) {
|
2021-10-06 09:24:09 +00:00
|
|
|
susp_evt(&udev, 0, 0);
|
|
|
|
usbd_connect(&udev, false);
|
|
|
|
usb_config.enabled = false;
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "USB Disable");
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_usb_enable() {
|
2022-01-05 16:10:18 +00:00
|
|
|
if((!usb_config.enabled) && (usb_if_cur != NULL)) {
|
2021-10-06 09:24:09 +00:00
|
|
|
usbd_connect(&udev, true);
|
|
|
|
usb_config.enabled = true;
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "USB Enable");
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void furi_hal_usb_tmr_cb(void* context) {
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_hal_usb_set_config(usb_if_next);
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get device / configuration descriptors */
|
2022-01-05 16:10:18 +00:00
|
|
|
static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length) {
|
2021-10-06 09:24:09 +00:00
|
|
|
const uint8_t dtype = req->wValue >> 8;
|
|
|
|
const uint8_t dnumber = req->wValue & 0xFF;
|
|
|
|
const void* desc;
|
|
|
|
uint16_t len = 0;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(usb_if_cur == NULL) return usbd_fail;
|
2021-10-06 09:24:09 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
switch(dtype) {
|
2021-10-06 09:24:09 +00:00
|
|
|
case USB_DTYPE_DEVICE:
|
2022-01-21 16:55:44 +00:00
|
|
|
if(callback != NULL) {
|
|
|
|
callback(FuriHalUsbStateEventDescriptorRequest, cb_ctx);
|
|
|
|
}
|
2021-11-21 15:17:43 +00:00
|
|
|
desc = usb_if_cur->dev_descr;
|
2021-10-06 09:24:09 +00:00
|
|
|
break;
|
|
|
|
case USB_DTYPE_CONFIGURATION:
|
2021-11-21 15:17:43 +00:00
|
|
|
desc = usb_if_cur->cfg_descr;
|
|
|
|
len = ((struct usb_string_descriptor*)(usb_if_cur->cfg_descr))->wString[0];
|
2021-10-06 09:24:09 +00:00
|
|
|
break;
|
|
|
|
case USB_DTYPE_STRING:
|
2022-01-05 16:10:18 +00:00
|
|
|
if(dnumber == UsbDevLang) {
|
2021-10-06 09:24:09 +00:00
|
|
|
desc = &dev_lang_desc;
|
2022-01-21 16:55:44 +00:00
|
|
|
} else if((dnumber == UsbDevManuf) && (usb_if_cur->str_manuf_descr != NULL)) {
|
2021-11-21 15:17:43 +00:00
|
|
|
desc = usb_if_cur->str_manuf_descr;
|
2022-01-21 16:55:44 +00:00
|
|
|
} else if((dnumber == UsbDevProduct) && (usb_if_cur->str_prod_descr != NULL)) {
|
2021-11-21 15:17:43 +00:00
|
|
|
desc = usb_if_cur->str_prod_descr;
|
2022-01-21 16:55:44 +00:00
|
|
|
} else if((dnumber == UsbDevSerial) && (usb_if_cur->str_serial_descr != NULL)) {
|
2021-11-21 15:17:43 +00:00
|
|
|
desc = usb_if_cur->str_serial_descr;
|
2022-01-05 16:10:18 +00:00
|
|
|
} else
|
2021-10-06 09:24:09 +00:00
|
|
|
return usbd_fail;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return usbd_fail;
|
|
|
|
}
|
2022-01-05 16:10:18 +00:00
|
|
|
if(desc == NULL) return usbd_fail;
|
2021-10-06 09:24:09 +00:00
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
if(len == 0) {
|
2021-10-06 09:24:09 +00:00
|
|
|
len = ((struct usb_header_descriptor*)desc)->bLength;
|
|
|
|
}
|
|
|
|
*address = (void*)desc;
|
|
|
|
*length = len;
|
|
|
|
return usbd_ack;
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
void furi_hal_usb_set_state_callback(FuriHalUsbStateCallback cb, void* ctx) {
|
|
|
|
callback = cb;
|
|
|
|
cb_ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|
|
|
if(callback != NULL) {
|
|
|
|
callback(FuriHalUsbStateEventReset, cb_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|
|
|
if((usb_if_cur != NULL) && (usb_config.connected == true)) {
|
2021-10-21 18:12:20 +00:00
|
|
|
usb_config.connected = false;
|
2021-11-21 15:17:43 +00:00
|
|
|
usb_if_cur->suspend(&udev);
|
2021-10-21 18:12:20 +00:00
|
|
|
}
|
2022-01-21 16:55:44 +00:00
|
|
|
if(callback != NULL) {
|
|
|
|
callback(FuriHalUsbStateEventSuspend, cb_ctx);
|
|
|
|
}
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
|
|
|
|
if((usb_if_cur != NULL) && (usb_config.connected == false)) {
|
2021-10-21 18:12:20 +00:00
|
|
|
usb_config.connected = true;
|
2021-11-21 15:17:43 +00:00
|
|
|
usb_if_cur->wakeup(&udev);
|
2022-01-05 16:10:18 +00:00
|
|
|
}
|
2022-01-21 16:55:44 +00:00
|
|
|
if(callback != NULL) {
|
|
|
|
callback(FuriHalUsbStateEventWakeup, cb_ctx);
|
|
|
|
}
|
2021-10-06 09:24:09 +00:00
|
|
|
}
|