2021-11-21 18:17:43 +03:00
|
|
|
#include "../usb_uart_bridge.h"
|
|
|
|
#include "../gpio_app_i.h"
|
2023-02-07 19:33:05 +03:00
|
|
|
#include <furi_hal.h>
|
2021-11-21 18:17:43 +03:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
UsbUartLineIndexVcp,
|
|
|
|
UsbUartLineIndexBaudrate,
|
|
|
|
UsbUartLineIndexUart,
|
|
|
|
UsbUartLineIndexFlow,
|
|
|
|
} LineIndex;
|
|
|
|
|
|
|
|
static const char* vcp_ch[] = {"0 (CLI)", "1"};
|
|
|
|
static const char* uart_ch[] = {"13,14", "15,16"};
|
2022-10-18 18:06:18 +03:00
|
|
|
static const char* flow_pins[] = {"None", "2,3", "6,7", "16,15"};
|
2021-11-21 18:17:43 +03:00
|
|
|
static const char* baudrate_mode[] = {"Host"};
|
|
|
|
static const uint32_t baudrate_list[] = {
|
2023-01-11 16:58:51 +03:00
|
|
|
1200,
|
2021-11-21 18:17:43 +03:00
|
|
|
2400,
|
2023-01-11 16:58:51 +03:00
|
|
|
4800,
|
2021-11-21 18:17:43 +03:00
|
|
|
9600,
|
|
|
|
19200,
|
2023-01-11 16:58:51 +03:00
|
|
|
28800,
|
2021-11-21 18:17:43 +03:00
|
|
|
38400,
|
|
|
|
57600,
|
|
|
|
115200,
|
|
|
|
230400,
|
|
|
|
460800,
|
|
|
|
921600,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool gpio_scene_usb_uart_cfg_on_event(void* context, SceneManagerEvent event) {
|
2022-11-29 15:50:55 +03:00
|
|
|
GpioApp* app = context;
|
|
|
|
furi_assert(app);
|
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
|
|
|
if(event.event == GpioUsbUartEventConfigSet) {
|
|
|
|
usb_uart_set_config(app->usb_uart_bridge, app->usb_uart_cfg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-05-06 16:37:10 +03:00
|
|
|
return false;
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
2022-10-18 18:06:18 +03:00
|
|
|
void line_ensure_flow_invariant(GpioApp* app) {
|
|
|
|
// GPIO pins PC0, PC1 (16,15) are unavailable for RTS/DTR when LPUART is
|
|
|
|
// selected. This function enforces that invariant by resetting flow_pins
|
|
|
|
// to None if it is configured to 16,15 when LPUART is selected.
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
uint8_t available_flow_pins = app->usb_uart_cfg->uart_ch == FuriHalUartIdLPUART1 ? 3 : 4;
|
2022-10-18 18:06:18 +03:00
|
|
|
VariableItem* item = app->var_item_flow;
|
|
|
|
variable_item_set_values_count(item, available_flow_pins);
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
if(app->usb_uart_cfg->flow_pins >= available_flow_pins) {
|
|
|
|
app->usb_uart_cfg->flow_pins = 0;
|
2022-10-18 18:06:18 +03:00
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
variable_item_set_current_value_index(item, app->usb_uart_cfg->flow_pins);
|
|
|
|
variable_item_set_current_value_text(item, flow_pins[app->usb_uart_cfg->flow_pins]);
|
2022-10-18 18:06:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
static void line_vcp_cb(VariableItem* item) {
|
|
|
|
GpioApp* app = variable_item_get_context(item);
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_assert(app);
|
2021-11-21 18:17:43 +03:00
|
|
|
uint8_t index = variable_item_get_current_value_index(item);
|
|
|
|
|
|
|
|
variable_item_set_current_value_text(item, vcp_ch[index]);
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->vcp_ch = index;
|
|
|
|
view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void line_port_cb(VariableItem* item) {
|
|
|
|
GpioApp* app = variable_item_get_context(item);
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_assert(app);
|
2021-11-21 18:17:43 +03:00
|
|
|
uint8_t index = variable_item_get_current_value_index(item);
|
|
|
|
|
|
|
|
variable_item_set_current_value_text(item, uart_ch[index]);
|
|
|
|
|
|
|
|
if(index == 0)
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->uart_ch = FuriHalUartIdUSART1;
|
2021-11-21 18:17:43 +03:00
|
|
|
else if(index == 1)
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->uart_ch = FuriHalUartIdLPUART1;
|
|
|
|
|
2022-10-18 18:06:18 +03:00
|
|
|
line_ensure_flow_invariant(app);
|
2022-11-29 15:50:55 +03:00
|
|
|
view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void line_flow_cb(VariableItem* item) {
|
|
|
|
GpioApp* app = variable_item_get_context(item);
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_assert(app);
|
2021-11-21 18:17:43 +03:00
|
|
|
uint8_t index = variable_item_get_current_value_index(item);
|
|
|
|
|
|
|
|
variable_item_set_current_value_text(item, flow_pins[index]);
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->flow_pins = index;
|
|
|
|
view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void line_baudrate_cb(VariableItem* item) {
|
|
|
|
GpioApp* app = variable_item_get_context(item);
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_assert(app);
|
2021-11-21 18:17:43 +03:00
|
|
|
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);
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->baudrate = baudrate_list[index - 1];
|
2021-11-21 18:17:43 +03:00
|
|
|
} else {
|
|
|
|
variable_item_set_current_value_text(item, baudrate_mode[index]);
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->baudrate = 0;
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg->baudrate_mode = index;
|
|
|
|
view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void gpio_scene_usb_uart_cfg_on_enter(void* context) {
|
|
|
|
GpioApp* app = context;
|
2022-11-29 15:50:55 +03:00
|
|
|
furi_assert(app);
|
2021-11-21 18:17:43 +03:00
|
|
|
VariableItemList* var_item_list = app->var_item_list;
|
|
|
|
|
2022-11-29 15:50:55 +03:00
|
|
|
app->usb_uart_cfg = malloc(sizeof(UsbUartConfig));
|
|
|
|
usb_uart_get_config(app->usb_uart_bridge, app->usb_uart_cfg);
|
2021-11-21 18:17:43 +03:00
|
|
|
|
|
|
|
VariableItem* item;
|
|
|
|
char br_text[8];
|
|
|
|
|
|
|
|
item = variable_item_list_add(var_item_list, "USB Channel", 2, line_vcp_cb, app);
|
2022-11-29 15:50:55 +03:00
|
|
|
variable_item_set_current_value_index(item, app->usb_uart_cfg->vcp_ch);
|
|
|
|
variable_item_set_current_value_text(item, vcp_ch[app->usb_uart_cfg->vcp_ch]);
|
2021-11-21 18:17:43 +03:00
|
|
|
|
|
|
|
item = variable_item_list_add(
|
|
|
|
var_item_list,
|
|
|
|
"Baudrate",
|
|
|
|
sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
|
|
|
|
line_baudrate_cb,
|
|
|
|
app);
|
2022-11-29 15:50:55 +03:00
|
|
|
variable_item_set_current_value_index(item, app->usb_uart_cfg->baudrate_mode);
|
|
|
|
if(app->usb_uart_cfg->baudrate_mode > 0) {
|
|
|
|
snprintf(br_text, 7, "%lu", baudrate_list[app->usb_uart_cfg->baudrate_mode - 1]);
|
2021-11-21 18:17:43 +03:00
|
|
|
variable_item_set_current_value_text(item, br_text);
|
|
|
|
} else {
|
2022-11-29 15:50:55 +03:00
|
|
|
variable_item_set_current_value_text(
|
|
|
|
item, baudrate_mode[app->usb_uart_cfg->baudrate_mode]);
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
item = variable_item_list_add(var_item_list, "UART Pins", 2, line_port_cb, app);
|
2022-11-29 15:50:55 +03:00
|
|
|
variable_item_set_current_value_index(item, app->usb_uart_cfg->uart_ch);
|
|
|
|
variable_item_set_current_value_text(item, uart_ch[app->usb_uart_cfg->uart_ch]);
|
2021-11-21 18:17:43 +03:00
|
|
|
|
2022-10-18 18:06:18 +03:00
|
|
|
item = variable_item_list_add(
|
|
|
|
var_item_list, "RTS/DTR Pins", COUNT_OF(flow_pins), line_flow_cb, app);
|
2022-11-29 15:50:55 +03:00
|
|
|
variable_item_set_current_value_index(item, app->usb_uart_cfg->flow_pins);
|
|
|
|
variable_item_set_current_value_text(item, flow_pins[app->usb_uart_cfg->flow_pins]);
|
2022-10-18 18:06:18 +03:00
|
|
|
app->var_item_flow = item;
|
|
|
|
line_ensure_flow_invariant(app);
|
2021-11-21 18:17:43 +03:00
|
|
|
|
|
|
|
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));
|
2022-01-21 20:32:03 +03:00
|
|
|
variable_item_list_reset(app->var_item_list);
|
2022-11-29 15:50:55 +03:00
|
|
|
free(app->usb_uart_cfg);
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|