USB-UART: New GUI (#826)

* USB-UART: new gui
* Furi: use furi_console for logging instead of printf.
* CDC: calling open/close callbacks on interface change
* fix vcp_tx block on disconnect
* USB mode set by struct pointer
* FuriHal: proper event sequence on vcp reconnect
* disable debug prints
* HAL: add context to UART IRQ's
* Context usage in UART IRQ and CDC callbacks
* USB-UART: geting rid of baudrate limitations
* FuriHal: remove struct pollutant in usb api.

Co-authored-by: あく <alleteam@gmail.com>
Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
Nikolay Minaylov
2021-11-21 18:17:43 +03:00
committed by GitHub
parent a5052a0375
commit efded63bcb
37 changed files with 1444 additions and 961 deletions

View File

@@ -1,3 +1,4 @@
ADD_SCENE(gpio, start, Start)
ADD_SCENE(gpio, test, Test)
ADD_SCENE(gpio, usb_uart, UsbUart)
ADD_SCENE(gpio, usb_uart_cfg, UsbUartCfg)

View File

@@ -2,9 +2,9 @@
#include "furi-hal-power.h"
enum GpioItem {
GpioItemOtg,
GpioItemTest,
GpioItemUsbUart,
GpioItemTest,
GpioItemOtg,
};
enum GpioOtg {
@@ -22,11 +22,9 @@ static void gpio_scene_start_var_list_enter_callback(void* context, uint32_t ind
furi_assert(context);
GpioApp* app = context;
if(index == GpioItemTest) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_TEST);
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventManualConrol);
} else if(index == GpioItemUsbUart) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_USB_UART);
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventUsbUart);
}
}
@@ -36,11 +34,9 @@ static void gpio_scene_start_var_list_change_callback(VariableItem* item) {
variable_item_set_current_value_text(item, gpio_otg_text[index]);
if(index == GpioOtgOff) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_OTG_OFF);
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOff);
} else if(index == GpioOtgOn) {
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_START_CUSTOM_EVENT_OTG_ON);
view_dispatcher_send_custom_event(app->view_dispatcher, GpioStartEventOtgOn);
}
}
@@ -51,6 +47,11 @@ void gpio_scene_start_on_enter(void* context) {
VariableItem* item;
variable_item_list_set_enter_callback(
var_item_list, gpio_scene_start_var_list_enter_callback, app);
variable_item_list_add(var_item_list, "USB-UART bridge", 0, NULL, NULL);
variable_item_list_add(var_item_list, "GPIO manual control", 0, NULL, NULL);
item = variable_item_list_add(
var_item_list,
"5V on GPIO",
@@ -64,8 +65,6 @@ void gpio_scene_start_on_enter(void* context) {
variable_item_set_current_value_index(item, GpioOtgOff);
variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOff]);
}
variable_item_list_add(var_item_list, "GPIO tester", 0, NULL, NULL);
variable_item_list_add(var_item_list, "USB-UART bridge", 0, NULL, NULL);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioSceneStart));
@@ -78,15 +77,15 @@ bool gpio_scene_start_on_event(void* context, SceneManagerEvent event) {
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_OTG_ON) {
if(event.event == GpioStartEventOtgOn) {
furi_hal_power_enable_otg();
} else if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_OTG_OFF) {
} else if(event.event == GpioStartEventOtgOff) {
furi_hal_power_disable_otg();
} else if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_TEST) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, 1);
} else if(event.event == GpioStartEventManualConrol) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemTest);
scene_manager_next_scene(app->scene_manager, GpioSceneTest);
} else if(event.event == GPIO_SCENE_START_CUSTOM_EVENT_USB_UART) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, 2);
} else if(event.event == GpioStartEventUsbUart) {
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemUsbUart);
scene_manager_next_scene(app->scene_manager, GpioSceneUsbUart);
}
consumed = true;

View File

@@ -1,138 +1,65 @@
#include "../usb_uart_bridge.h"
#include "../gpio_app_i.h"
#include "furi-hal.h"
#include "../usb_uart_bridge.h"
typedef enum {
UsbUartLineIndexVcp,
UsbUartLineIndexUart,
UsbUartLineIndexBaudrate,
UsbUartLineIndexEnable,
UsbUartLineIndexDisable,
} LineIndex;
typedef struct {
UsbUartConfig cfg;
UsbUartState state;
} SceneUsbUartBridge;
static UsbUartConfig* cfg_set;
static SceneUsbUartBridge* scene_usb_uart;
static const char* vcp_ch[] = {"0 (CLI)", "1"};
static const char* uart_ch[] = {"USART1", "LPUART1"};
static const char* baudrate_mode[] = {"Host"};
static const uint32_t baudrate_list[] = {
2400,
9600,
19200,
38400,
57600,
115200,
230400,
460800,
921600,
};
bool gpio_scene_usb_uart_on_event(void* context, SceneManagerEvent event) {
//GpioApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GPIO_SCENE_USB_UART_CUSTOM_EVENT_ENABLE) {
usb_uart_enable(cfg_set);
} else if(event.event == GPIO_SCENE_USB_UART_CUSTOM_EVENT_DISABLE) {
usb_uart_disable();
}
consumed = true;
}
return consumed;
}
static void line_vcp_cb(VariableItem* item) {
//GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, vcp_ch[index]);
cfg_set->vcp_ch = index;
}
static void line_port_cb(VariableItem* item) {
//GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, uart_ch[index]);
if(index == 0)
cfg_set->uart_ch = FuriHalUartIdUSART1;
else if(index == 1)
cfg_set->uart_ch = FuriHalUartIdLPUART1;
}
static void line_baudrate_cb(VariableItem* item) {
//GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
char br_text[8];
if(index > 0) {
snprintf(br_text, 7, "%lu", baudrate_list[index - 1]);
variable_item_set_current_value_text(item, br_text);
cfg_set->baudrate = baudrate_list[index - 1];
} else {
variable_item_set_current_value_text(item, baudrate_mode[index]);
cfg_set->baudrate = 0;
}
}
static void gpio_scene_usb_uart_enter_callback(void* context, uint32_t index) {
void gpio_scene_usb_uart_callback(GpioCustomEvent event, void* context) {
furi_assert(context);
GpioApp* app = context;
if(index == UsbUartLineIndexEnable)
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_USB_UART_CUSTOM_EVENT_ENABLE);
else if(index == UsbUartLineIndexDisable)
view_dispatcher_send_custom_event(
app->view_dispatcher, GPIO_SCENE_USB_UART_CUSTOM_EVENT_DISABLE);
view_dispatcher_send_custom_event(app->view_dispatcher, event);
}
void gpio_scene_usb_uart_on_enter(void* context) {
GpioApp* app = context;
VariableItemList* var_item_list = app->var_item_list;
uint32_t prev_state = scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUart);
if(prev_state == 0) {
scene_usb_uart = furi_alloc(sizeof(SceneUsbUartBridge));
scene_usb_uart->cfg.vcp_ch = 0; // TODO: settings load
scene_usb_uart->cfg.uart_ch = 0;
scene_usb_uart->cfg.flow_pins = 0;
scene_usb_uart->cfg.baudrate_mode = 0;
scene_usb_uart->cfg.baudrate = 0;
app->usb_uart_bridge = usb_uart_enable(&scene_usb_uart->cfg);
}
cfg_set = furi_alloc(sizeof(UsbUartConfig));
VariableItem* item;
variable_item_list_set_enter_callback(var_item_list, gpio_scene_usb_uart_enter_callback, app);
item = variable_item_list_add(var_item_list, "VCP Channel", 2, line_vcp_cb, app);
variable_item_set_current_value_index(item, 0);
variable_item_set_current_value_text(item, vcp_ch[0]);
item = variable_item_list_add(var_item_list, "UART Port", 2, line_port_cb, app);
variable_item_set_current_value_index(item, 0);
variable_item_set_current_value_text(item, uart_ch[0]);
item = variable_item_list_add(
var_item_list,
"Baudrate",
sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
line_baudrate_cb,
app);
variable_item_set_current_value_index(item, 0);
variable_item_set_current_value_text(item, baudrate_mode[0]);
item = variable_item_list_add(var_item_list, "Enable", 0, NULL, NULL);
item = variable_item_list_add(var_item_list, "Disable", 0, NULL, NULL);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioSceneUsbUart));
usb_uart_get_config(app->usb_uart_bridge, &scene_usb_uart->cfg);
usb_uart_get_state(app->usb_uart_bridge, &scene_usb_uart->state);
gpio_usb_uart_set_callback(app->gpio_usb_uart, gpio_scene_usb_uart_callback, app);
scene_manager_set_scene_state(app->scene_manager, GpioAppViewUsbUart, 0);
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUart);
}
bool gpio_scene_usb_uart_on_event(void* context, SceneManagerEvent event) {
GpioApp* app = context;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(app->scene_manager, GpioAppViewUsbUart, 1);
scene_manager_next_scene(app->scene_manager, GpioAppViewUsbUartCfg);
return true;
} else if(event.type == SceneManagerEventTypeTick) {
uint32_t tx_cnt_last = scene_usb_uart->state.tx_cnt;
uint32_t rx_cnt_last = scene_usb_uart->state.rx_cnt;
usb_uart_get_state(app->usb_uart_bridge, &scene_usb_uart->state);
gpio_usb_uart_update_state(
app->gpio_usb_uart, &scene_usb_uart->cfg, &scene_usb_uart->state);
if(tx_cnt_last != scene_usb_uart->state.tx_cnt)
notification_message(app->notifications, &sequence_blink_blue_10);
if(rx_cnt_last != scene_usb_uart->state.rx_cnt)
notification_message(app->notifications, &sequence_blink_green_10);
}
return false;
}
void gpio_scene_usb_uart_on_exit(void* context) {
GpioApp* app = context;
usb_uart_disable();
scene_manager_set_scene_state(
app->scene_manager,
GpioSceneUsbUart,
variable_item_list_get_selected_item_index(app->var_item_list));
variable_item_list_clean(app->var_item_list);
free(cfg_set);
uint32_t prev_state = scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUart);
if(prev_state == 0) {
usb_uart_disable(app->usb_uart_bridge);
free(scene_usb_uart);
}
}

View File

@@ -0,0 +1,140 @@
#include "../usb_uart_bridge.h"
#include "../gpio_app_i.h"
#include "furi-hal.h"
typedef enum {
UsbUartLineIndexVcp,
UsbUartLineIndexBaudrate,
UsbUartLineIndexUart,
UsbUartLineIndexFlow,
} LineIndex;
static UsbUartConfig* cfg_set;
static const char* vcp_ch[] = {"0 (CLI)", "1"};
static const char* uart_ch[] = {"13,14", "15,16"};
static const char* flow_pins[] = {"None", "2,3", "6,7"};
static const char* baudrate_mode[] = {"Host"};
static const uint32_t baudrate_list[] = {
2400,
9600,
19200,
38400,
57600,
115200,
230400,
460800,
921600,
};
bool gpio_scene_usb_uart_cfg_on_event(void* context, SceneManagerEvent event) {
//GpioApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
}
return consumed;
}
static void line_vcp_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, vcp_ch[index]);
cfg_set->vcp_ch = index;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
static void line_port_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, uart_ch[index]);
if(index == 0)
cfg_set->uart_ch = FuriHalUartIdUSART1;
else if(index == 1)
cfg_set->uart_ch = FuriHalUartIdLPUART1;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
static void line_flow_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, flow_pins[index]);
cfg_set->flow_pins = index;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
static void line_baudrate_cb(VariableItem* item) {
GpioApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
char br_text[8];
if(index > 0) {
snprintf(br_text, 7, "%lu", baudrate_list[index - 1]);
variable_item_set_current_value_text(item, br_text);
cfg_set->baudrate = baudrate_list[index - 1];
} else {
variable_item_set_current_value_text(item, baudrate_mode[index]);
cfg_set->baudrate = 0;
}
cfg_set->baudrate_mode = index;
usb_uart_set_config(app->usb_uart_bridge, cfg_set);
}
void gpio_scene_usb_uart_cfg_on_enter(void* context) {
GpioApp* app = context;
VariableItemList* var_item_list = app->var_item_list;
cfg_set = furi_alloc(sizeof(UsbUartConfig));
usb_uart_get_config(app->usb_uart_bridge, cfg_set);
VariableItem* item;
char br_text[8];
item = variable_item_list_add(var_item_list, "USB Channel", 2, line_vcp_cb, app);
variable_item_set_current_value_index(item, cfg_set->vcp_ch);
variable_item_set_current_value_text(item, vcp_ch[cfg_set->vcp_ch]);
item = variable_item_list_add(
var_item_list,
"Baudrate",
sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
line_baudrate_cb,
app);
variable_item_set_current_value_index(item, cfg_set->baudrate_mode);
if(cfg_set->baudrate_mode > 0) {
snprintf(br_text, 7, "%lu", baudrate_list[cfg_set->baudrate_mode - 1]);
variable_item_set_current_value_text(item, br_text);
} else {
variable_item_set_current_value_text(item, baudrate_mode[cfg_set->baudrate_mode]);
}
item = variable_item_list_add(var_item_list, "UART Pins", 2, line_port_cb, app);
variable_item_set_current_value_index(item, cfg_set->uart_ch);
variable_item_set_current_value_text(item, uart_ch[cfg_set->uart_ch]);
item = variable_item_list_add(var_item_list, "RTS/DTR Pins", 3, line_flow_cb, app);
variable_item_set_current_value_index(item, cfg_set->flow_pins);
variable_item_set_current_value_text(item, flow_pins[cfg_set->flow_pins]);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUartCfg));
view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
}
void gpio_scene_usb_uart_cfg_on_exit(void* context) {
GpioApp* app = context;
scene_manager_set_scene_state(
app->scene_manager,
GpioAppViewUsbUartCfg,
variable_item_list_get_selected_item_index(app->var_item_list));
variable_item_list_clean(app->var_item_list);
free(cfg_set);
}