[FL-1506, FL-2197] Power, USB, LED driver improvements (#966)
* Power, USB, LED driver improvements * u2f hid descriptor fix * variable_item_list: value alignment fix * InputTypeRepeat handling in menu/submenu/var_item_list * lp5562: fix bugs on 400khz i2c * Scripts: lint in parallel. * FuriHal: rename some USB structure to match naming convention. Drivers: update magic values in LP5562. Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
		@@ -28,6 +28,7 @@ void furi_hal_light_init() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void furi_hal_light_set(Light light, uint8_t value) {
 | 
			
		||||
    uint8_t prev = 0;
 | 
			
		||||
    furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
 | 
			
		||||
    switch(light) {
 | 
			
		||||
    case LightRed:
 | 
			
		||||
@@ -40,10 +41,12 @@ void furi_hal_light_set(Light light, uint8_t value) {
 | 
			
		||||
        lp5562_set_channel_value(&furi_hal_i2c_handle_power, LP5562ChannelBlue, value);
 | 
			
		||||
        break;
 | 
			
		||||
    case LightBacklight:
 | 
			
		||||
        lp5562_set_channel_value(&furi_hal_i2c_handle_power, LP5562ChannelWhite, value);
 | 
			
		||||
        prev = lp5562_get_channel_value(&furi_hal_i2c_handle_power, LP5562ChannelWhite);
 | 
			
		||||
        lp5562_execute_ramp(
 | 
			
		||||
            &furi_hal_i2c_handle_power, LP5562Engine1, LP5562ChannelWhite, prev, value, 100);
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    furi_hal_i2c_release(&furi_hal_i2c_handle_power);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -216,6 +216,13 @@ bool furi_hal_power_is_otg_enabled() {
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void furi_hal_power_check_otg_status() {
 | 
			
		||||
    furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
 | 
			
		||||
    if(bq25896_check_otg_fault(&furi_hal_i2c_handle_power))
 | 
			
		||||
        bq25896_disable_otg(&furi_hal_i2c_handle_power);
 | 
			
		||||
    furi_hal_i2c_release(&furi_hal_i2c_handle_power);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t furi_hal_power_get_battery_remaining_capacity() {
 | 
			
		||||
    furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
 | 
			
		||||
    uint32_t ret = bq27220_get_remaining_capacity(&furi_hal_i2c_handle_power);
 | 
			
		||||
@@ -297,6 +304,7 @@ void furi_hal_power_dump_state() {
 | 
			
		||||
           BQ27220_ERROR) {
 | 
			
		||||
        printf("Failed to get bq27220 status. Communication error.\r\n");
 | 
			
		||||
    } else {
 | 
			
		||||
        // Operation status register
 | 
			
		||||
        printf(
 | 
			
		||||
            "bq27220: CALMD: %d, SEC0: %d, SEC1: %d, EDV2: %d, VDQ: %d, INITCOMP: %d, SMTH: %d, BTPINT: %d, CFGUPDATE: %d\r\n",
 | 
			
		||||
            operation_status.CALMD,
 | 
			
		||||
 
 | 
			
		||||
@@ -9,15 +9,19 @@
 | 
			
		||||
 | 
			
		||||
#define USB_RECONNECT_DELAY 500
 | 
			
		||||
 | 
			
		||||
static UsbInterface* usb_if_cur;
 | 
			
		||||
static UsbInterface* usb_if_next;
 | 
			
		||||
static FuriHalUsbInterface* usb_if_cur;
 | 
			
		||||
static FuriHalUsbInterface* usb_if_next;
 | 
			
		||||
 | 
			
		||||
static const struct usb_string_descriptor dev_lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
 | 
			
		||||
 | 
			
		||||
static uint32_t ubuf[0x20];
 | 
			
		||||
usbd_device udev;
 | 
			
		||||
 | 
			
		||||
static FuriHalUsbStateCallback callback;
 | 
			
		||||
static void* cb_ctx;
 | 
			
		||||
 | 
			
		||||
static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_t* length);
 | 
			
		||||
static void reset_evt(usbd_device* dev, uint8_t event, uint8_t ep);
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
@@ -25,6 +29,7 @@ struct UsbCfg {
 | 
			
		||||
    osTimerId_t reconnect_tmr;
 | 
			
		||||
    bool enabled;
 | 
			
		||||
    bool connected;
 | 
			
		||||
    bool mode_changing;
 | 
			
		||||
} usb_config;
 | 
			
		||||
 | 
			
		||||
static void furi_hal_usb_tmr_cb(void* context);
 | 
			
		||||
@@ -48,6 +53,7 @@ void furi_hal_usb_init(void) {
 | 
			
		||||
    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);
 | 
			
		||||
    // Reset callback will be enabled after first mode change to avoid getting false reset events
 | 
			
		||||
 | 
			
		||||
    usb_config.enabled = false;
 | 
			
		||||
    usb_config.reconnect_tmr = NULL;
 | 
			
		||||
@@ -57,28 +63,49 @@ void furi_hal_usb_init(void) {
 | 
			
		||||
    FURI_LOG_I(TAG, "Init OK");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void furi_hal_usb_set_config(UsbInterface* new_if) {
 | 
			
		||||
    if(new_if != usb_if_cur) {
 | 
			
		||||
        if(usb_config.enabled) {
 | 
			
		||||
            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();
 | 
			
		||||
            osTimerStart(usb_config.reconnect_tmr, USB_RECONNECT_DELAY);
 | 
			
		||||
        } else {
 | 
			
		||||
            if(usb_if_cur != NULL) usb_if_cur->deinit(&udev);
 | 
			
		||||
            if(new_if != NULL) {
 | 
			
		||||
                new_if->init(&udev, new_if);
 | 
			
		||||
                FURI_LOG_I(TAG, "USB mode change");
 | 
			
		||||
                usb_config.enabled = true;
 | 
			
		||||
                usb_if_cur = new_if;
 | 
			
		||||
            }
 | 
			
		||||
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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
UsbInterface* furi_hal_usb_get_config() {
 | 
			
		||||
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() {
 | 
			
		||||
    return usb_if_cur;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -113,6 +140,9 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
 | 
			
		||||
 | 
			
		||||
    switch(dtype) {
 | 
			
		||||
    case USB_DTYPE_DEVICE:
 | 
			
		||||
        if(callback != NULL) {
 | 
			
		||||
            callback(FuriHalUsbStateEventDescriptorRequest, cb_ctx);
 | 
			
		||||
        }
 | 
			
		||||
        desc = usb_if_cur->dev_descr;
 | 
			
		||||
        break;
 | 
			
		||||
    case USB_DTYPE_CONFIGURATION:
 | 
			
		||||
@@ -122,11 +152,11 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
 | 
			
		||||
    case USB_DTYPE_STRING:
 | 
			
		||||
        if(dnumber == UsbDevLang) {
 | 
			
		||||
            desc = &dev_lang_desc;
 | 
			
		||||
        } else if(dnumber == UsbDevManuf) {
 | 
			
		||||
        } else if((dnumber == UsbDevManuf) && (usb_if_cur->str_manuf_descr != NULL)) {
 | 
			
		||||
            desc = usb_if_cur->str_manuf_descr;
 | 
			
		||||
        } else if(dnumber == UsbDevProduct) {
 | 
			
		||||
        } else if((dnumber == UsbDevProduct) && (usb_if_cur->str_prod_descr != NULL)) {
 | 
			
		||||
            desc = usb_if_cur->str_prod_descr;
 | 
			
		||||
        } else if(dnumber == UsbDevSerial) {
 | 
			
		||||
        } else if((dnumber == UsbDevSerial) && (usb_if_cur->str_serial_descr != NULL)) {
 | 
			
		||||
            desc = usb_if_cur->str_serial_descr;
 | 
			
		||||
        } else
 | 
			
		||||
            return usbd_fail;
 | 
			
		||||
@@ -144,11 +174,25 @@ static usbd_respond usb_descriptor_get(usbd_ctlreq* req, void** address, uint16_
 | 
			
		||||
    return usbd_ack;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void susp_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
 | 
			
		||||
    if((usb_if_cur != NULL) && (usb_config.connected == true)) {
 | 
			
		||||
        usb_config.connected = false;
 | 
			
		||||
        usb_if_cur->suspend(&udev);
 | 
			
		||||
    }
 | 
			
		||||
    if(callback != NULL) {
 | 
			
		||||
        callback(FuriHalUsbStateEventSuspend, cb_ctx);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
 | 
			
		||||
@@ -156,4 +200,7 @@ static void wkup_evt(usbd_device* dev, uint8_t event, uint8_t ep) {
 | 
			
		||||
        usb_config.connected = true;
 | 
			
		||||
        usb_if_cur->wakeup(&udev);
 | 
			
		||||
    }
 | 
			
		||||
    if(callback != NULL) {
 | 
			
		||||
        callback(FuriHalUsbStateEventWakeup, cb_ctx);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -385,7 +385,7 @@ static const struct CdcConfigDescriptorDual
 | 
			
		||||
static struct usb_cdc_line_coding cdc_config[IF_NUM_MAX] = {};
 | 
			
		||||
static uint8_t cdc_ctrl_line_state[IF_NUM_MAX];
 | 
			
		||||
 | 
			
		||||
static void cdc_init(usbd_device* dev, UsbInterface* intf);
 | 
			
		||||
static void cdc_init(usbd_device* dev, FuriHalUsbInterface* intf);
 | 
			
		||||
static void cdc_deinit(usbd_device* dev);
 | 
			
		||||
static void cdc_on_wakeup(usbd_device* dev);
 | 
			
		||||
static void cdc_on_suspend(usbd_device* dev);
 | 
			
		||||
@@ -393,12 +393,12 @@ static void cdc_on_suspend(usbd_device* dev);
 | 
			
		||||
static usbd_respond cdc_ep_config(usbd_device* dev, uint8_t cfg);
 | 
			
		||||
static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
 | 
			
		||||
static usbd_device* usb_dev;
 | 
			
		||||
static UsbInterface* cdc_if_cur = NULL;
 | 
			
		||||
static FuriHalUsbInterface* cdc_if_cur = NULL;
 | 
			
		||||
static bool connected = false;
 | 
			
		||||
static CdcCallbacks* callbacks[IF_NUM_MAX] = {NULL};
 | 
			
		||||
static void* cb_ctx[IF_NUM_MAX];
 | 
			
		||||
 | 
			
		||||
UsbInterface usb_cdc_single = {
 | 
			
		||||
FuriHalUsbInterface usb_cdc_single = {
 | 
			
		||||
    .init = cdc_init,
 | 
			
		||||
    .deinit = cdc_deinit,
 | 
			
		||||
    .wakeup = cdc_on_wakeup,
 | 
			
		||||
@@ -413,7 +413,7 @@ UsbInterface usb_cdc_single = {
 | 
			
		||||
    .cfg_descr = (void*)&cdc_cfg_desc_single,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
UsbInterface usb_cdc_dual = {
 | 
			
		||||
FuriHalUsbInterface usb_cdc_dual = {
 | 
			
		||||
    .init = cdc_init,
 | 
			
		||||
    .deinit = cdc_deinit,
 | 
			
		||||
    .wakeup = cdc_on_wakeup,
 | 
			
		||||
@@ -428,7 +428,7 @@ UsbInterface usb_cdc_dual = {
 | 
			
		||||
    .cfg_descr = (void*)&cdc_cfg_desc_dual,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void cdc_init(usbd_device* dev, UsbInterface* intf) {
 | 
			
		||||
static void cdc_init(usbd_device* dev, FuriHalUsbInterface* intf) {
 | 
			
		||||
    usb_dev = dev;
 | 
			
		||||
    cdc_if_cur = intf;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -236,7 +236,7 @@ static struct HidReport {
 | 
			
		||||
    struct HidReportConsumer consumer;
 | 
			
		||||
} __attribute__((packed)) hid_report;
 | 
			
		||||
 | 
			
		||||
static void hid_init(usbd_device* dev, UsbInterface* intf);
 | 
			
		||||
static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf);
 | 
			
		||||
static void hid_deinit(usbd_device* dev);
 | 
			
		||||
static void hid_on_wakeup(usbd_device* dev);
 | 
			
		||||
static void hid_on_suspend(usbd_device* dev);
 | 
			
		||||
@@ -348,7 +348,7 @@ bool furi_hal_hid_consumer_key_release(uint16_t button) {
 | 
			
		||||
    return hid_send_report(ReportIdConsumer);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
UsbInterface usb_hid = {
 | 
			
		||||
FuriHalUsbInterface usb_hid = {
 | 
			
		||||
    .init = hid_init,
 | 
			
		||||
    .deinit = hid_deinit,
 | 
			
		||||
    .wakeup = hid_on_wakeup,
 | 
			
		||||
@@ -363,7 +363,7 @@ UsbInterface usb_hid = {
 | 
			
		||||
    .cfg_descr = (void*)&hid_cfg_desc,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void hid_init(usbd_device* dev, UsbInterface* intf) {
 | 
			
		||||
static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf) {
 | 
			
		||||
    if(hid_semaphore == NULL) hid_semaphore = osSemaphoreNew(1, 1, NULL);
 | 
			
		||||
    usb_dev = dev;
 | 
			
		||||
    hid_report.keyboard.report_id = ReportIdKeyboard;
 | 
			
		||||
 
 | 
			
		||||
@@ -48,8 +48,7 @@ static const uint8_t hid_u2f_report_desc[] = {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
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("U2F Token test");
 | 
			
		||||
static const struct usb_string_descriptor dev_serial_desc = USB_STRING_DESC("TODO: serial");
 | 
			
		||||
static const struct usb_string_descriptor dev_prod_desc = USB_STRING_DESC("U2F Token");
 | 
			
		||||
 | 
			
		||||
/* Device descriptor */
 | 
			
		||||
static const struct usb_device_descriptor hid_u2f_device_desc = {
 | 
			
		||||
@@ -65,7 +64,7 @@ static const struct usb_device_descriptor hid_u2f_device_desc = {
 | 
			
		||||
    .bcdDevice = VERSION_BCD(1, 0, 0),
 | 
			
		||||
    .iManufacturer = UsbDevManuf,
 | 
			
		||||
    .iProduct = UsbDevProduct,
 | 
			
		||||
    .iSerialNumber = UsbDevSerial,
 | 
			
		||||
    .iSerialNumber = 0,
 | 
			
		||||
    .bNumConfigurations = 1,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -138,7 +137,7 @@ static const struct HidConfigDescriptor hid_u2f_cfg_desc = {
 | 
			
		||||
        },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void hid_u2f_init(usbd_device* dev, UsbInterface* intf);
 | 
			
		||||
static void hid_u2f_init(usbd_device* dev, FuriHalUsbInterface* intf);
 | 
			
		||||
static void hid_u2f_deinit(usbd_device* dev);
 | 
			
		||||
static void hid_u2f_on_wakeup(usbd_device* dev);
 | 
			
		||||
static void hid_u2f_on_suspend(usbd_device* dev);
 | 
			
		||||
@@ -171,7 +170,7 @@ void furi_hal_hid_u2f_set_callback(HidU2fCallback cb, void* ctx) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
UsbInterface usb_hid_u2f = {
 | 
			
		||||
FuriHalUsbInterface usb_hid_u2f = {
 | 
			
		||||
    .init = hid_u2f_init,
 | 
			
		||||
    .deinit = hid_u2f_deinit,
 | 
			
		||||
    .wakeup = hid_u2f_on_wakeup,
 | 
			
		||||
@@ -181,12 +180,12 @@ UsbInterface usb_hid_u2f = {
 | 
			
		||||
 | 
			
		||||
    .str_manuf_descr = (void*)&dev_manuf_desc,
 | 
			
		||||
    .str_prod_descr = (void*)&dev_prod_desc,
 | 
			
		||||
    .str_serial_descr = (void*)&dev_serial_desc,
 | 
			
		||||
    .str_serial_descr = NULL,
 | 
			
		||||
 | 
			
		||||
    .cfg_descr = (void*)&hid_u2f_cfg_desc,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void hid_u2f_init(usbd_device* dev, UsbInterface* intf) {
 | 
			
		||||
static void hid_u2f_init(usbd_device* dev, FuriHalUsbInterface* intf) {
 | 
			
		||||
    if(hid_u2f_semaphore == NULL) hid_u2f_semaphore = osSemaphoreNew(1, 1, NULL);
 | 
			
		||||
    usb_dev = dev;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user