Skip to content

Commit aa6455b

Browse files
committed
Move OTG controls to the power service
1 parent 7291e6b commit aa6455b

File tree

21 files changed

+153
-35
lines changed

21 files changed

+153
-35
lines changed

applications/debug/accessor/accessor_app.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <furi.h>
33
#include <furi_hal.h>
44
#include <stdarg.h>
5+
#include <power/power_service/power.h>
56

67
void AccessorApp::run(void) {
78
AccessorEvent event;
@@ -35,16 +36,18 @@ AccessorApp::AccessorApp()
3536
: text_store{0} {
3637
notification = static_cast<NotificationApp*>(furi_record_open(RECORD_NOTIFICATION));
3738
expansion = static_cast<Expansion*>(furi_record_open(RECORD_EXPANSION));
39+
power = static_cast<Power*>(furi_record_open(RECORD_POWER));
3840
onewire_host = onewire_host_alloc(&gpio_ibutton);
3941
expansion_disable(expansion);
40-
furi_hal_power_enable_otg();
42+
power_switch_otg(power, true);
4143
}
4244

4345
AccessorApp::~AccessorApp() {
44-
furi_hal_power_disable_otg();
46+
power_switch_otg(power, false);
4547
expansion_enable(expansion);
4648
furi_record_close(RECORD_EXPANSION);
4749
furi_record_close(RECORD_NOTIFICATION);
50+
furi_record_close(RECORD_POWER);
4851
onewire_host_free(onewire_host);
4952
}
5053

applications/debug/accessor/accessor_app.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ class AccessorApp {
5353

5454
NotificationApp* notification;
5555
Expansion* expansion;
56+
Power* power;
5657
};

applications/examples/example_thermo/example_thermo.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <one_wire/maxim_crc.h>
2323
#include <one_wire/one_wire_host.h>
2424

25-
#include <furi_hal_power.h>
25+
#include <power/power_service/power.h>
2626

2727
#define UPDATE_PERIOD_MS 1000UL
2828
#define TEXT_STORE_SIZE 64U
@@ -76,6 +76,7 @@ typedef struct {
7676
FuriThread* reader_thread;
7777
FuriMessageQueue* event_queue;
7878
OneWireHost* onewire;
79+
Power* power;
7980
float temp_celsius;
8081
bool has_device;
8182
} ExampleThermoContext;
@@ -273,7 +274,7 @@ static void example_thermo_input_callback(InputEvent* event, void* ctx) {
273274
/* Starts the reader thread and handles the input */
274275
static void example_thermo_run(ExampleThermoContext* context) {
275276
/* Enable power on external pins */
276-
furi_hal_power_enable_otg();
277+
power_switch_otg(context->power, true);
277278

278279
/* Configure the hardware in host mode */
279280
onewire_host_start(context->onewire);
@@ -309,7 +310,7 @@ static void example_thermo_run(ExampleThermoContext* context) {
309310
onewire_host_stop(context->onewire);
310311

311312
/* Disable power on external pins */
312-
furi_hal_power_disable_otg();
313+
power_switch_otg(context->power, false);
313314
}
314315

315316
/******************** Initialisation & startup *****************************/
@@ -334,6 +335,8 @@ static ExampleThermoContext* example_thermo_context_alloc(void) {
334335

335336
context->onewire = onewire_host_alloc(&THERMO_GPIO_PIN);
336337

338+
context->power = furi_record_open(RECORD_POWER);
339+
337340
return context;
338341
}
339342

@@ -348,6 +351,7 @@ static void example_thermo_context_free(ExampleThermoContext* context) {
348351
view_port_free(context->view_port);
349352

350353
furi_record_close(RECORD_GUI);
354+
furi_record_close(RECORD_POWER);
351355
}
352356

353357
/* The application's entry point. Execution starts from here. */

applications/main/gpio/gpio_app.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ GpioApp* gpio_app_alloc(void) {
3030
app->gui = furi_record_open(RECORD_GUI);
3131
app->gpio_items = gpio_items_alloc();
3232

33+
app->power = furi_record_open(RECORD_POWER);
34+
3335
app->view_dispatcher = view_dispatcher_alloc();
3436
app->scene_manager = scene_manager_alloc(&gpio_scene_handlers, app);
3537
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
@@ -100,6 +102,7 @@ void gpio_app_free(GpioApp* app) {
100102
// Close records
101103
furi_record_close(RECORD_GUI);
102104
furi_record_close(RECORD_NOTIFICATION);
105+
furi_record_close(RECORD_POWER);
103106

104107
expansion_enable(app->expansion);
105108
furi_record_close(RECORD_EXPANSION);

applications/main/gpio/gpio_app_i.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "scenes/gpio_scene.h"
66
#include "gpio_custom_event.h"
77
#include "usb_uart_bridge.h"
8+
#include <power/power_service/power.h>
89

910
#include <gui/gui.h>
1011
#include <gui/view_dispatcher.h>
@@ -27,6 +28,7 @@ struct GpioApp {
2728
SceneManager* scene_manager;
2829
Widget* widget;
2930
DialogEx* dialog;
31+
Power* power;
3032

3133
VariableItemList* var_item_list;
3234
VariableItem* var_item_flow;

applications/main/gpio/scenes/gpio_scene_start.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void gpio_scene_start_on_enter(void* context) {
6060
GpioOtgSettingsNum,
6161
gpio_scene_start_var_list_change_callback,
6262
app);
63-
if(furi_hal_power_is_otg_enabled()) {
63+
if(power_is_otg_requested(app->power)) {
6464
variable_item_set_current_value_index(item, GpioOtgOn);
6565
variable_item_set_current_value_text(item, gpio_otg_text[GpioOtgOn]);
6666
} else {
@@ -80,9 +80,9 @@ bool gpio_scene_start_on_event(void* context, SceneManagerEvent event) {
8080

8181
if(event.type == SceneManagerEventTypeCustom) {
8282
if(event.event == GpioStartEventOtgOn) {
83-
furi_hal_power_enable_otg();
83+
power_switch_otg(app->power, true);
8484
} else if(event.event == GpioStartEventOtgOff) {
85-
furi_hal_power_disable_otg();
85+
power_switch_otg(app->power, false);
8686
} else if(event.event == GpioStartEventManualControl) {
8787
scene_manager_set_scene_state(app->scene_manager, GpioSceneStart, GpioItemTest);
8888
scene_manager_next_scene(app->scene_manager, GpioSceneTest);

applications/main/infrared/infrared_app.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "infrared_app_i.h"
22

3-
#include <furi_hal_power.h>
3+
#include <power/power_service/power.h>
44

55
#include <string.h>
66
#include <toolbox/path.h>
@@ -501,12 +501,16 @@ void infrared_set_tx_pin(InfraredApp* infrared, FuriHalInfraredTxPin tx_pin) {
501501
}
502502

503503
void infrared_enable_otg(InfraredApp* infrared, bool enable) {
504+
Power* power = furi_record_open(RECORD_POWER);
505+
504506
if(enable) {
505-
furi_hal_power_enable_otg();
507+
power_switch_otg(power, true);
506508
} else {
507-
furi_hal_power_disable_otg();
509+
power_switch_otg(power, false);
508510
}
509511
infrared->app_state.is_otg_enabled = enable;
512+
513+
furi_record_close(RECORD_POWER);
510514
}
511515

512516
static void infrared_load_settings(InfraredApp* infrared) {

applications/main/onewire/onewire_cli.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <furi.h>
22
#include <furi_hal.h>
33

4+
#include <power/power_service/power.h>
5+
46
#include <cli/cli.h>
57
#include <toolbox/args.h>
68

@@ -26,13 +28,14 @@ static void onewire_cli_print_usage(void) {
2628
static void onewire_cli_search(Cli* cli) {
2729
UNUSED(cli);
2830
OneWireHost* onewire = onewire_host_alloc(&gpio_ibutton);
31+
Power* power = furi_record_open(RECORD_POWER);
2932
uint8_t address[8];
3033
bool done = false;
3134

3235
printf("Search started\r\n");
3336

3437
onewire_host_start(onewire);
35-
furi_hal_power_enable_otg();
38+
power_switch_otg(power, true);
3639

3740
while(!done) {
3841
if(onewire_host_search(onewire, address, OneWireHostSearchModeNormal) != 1) {
@@ -49,8 +52,10 @@ static void onewire_cli_search(Cli* cli) {
4952
furi_delay_ms(100);
5053
}
5154

52-
furi_hal_power_disable_otg();
55+
power_switch_otg(power, false);
56+
5357
onewire_host_free(onewire);
58+
furi_record_close(RECORD_POWER);
5459
}
5560

5661
void onewire_cli(Cli* cli, FuriString* args, void* context) {

applications/main/subghz/helpers/subghz_txrx.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
#include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
55
#include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
66

7+
#include <power/power_service/power.h>
8+
79
#define TAG "SubGhz"
810

911
static void subghz_txrx_radio_device_power_on(SubGhzTxRx* instance) {
1012
UNUSED(instance);
13+
Power* power = furi_record_open(RECORD_POWER);
14+
1115
uint8_t attempts = 5;
1216
while(--attempts > 0) {
13-
if(furi_hal_power_enable_otg()) break;
17+
power_switch_otg(power, true);
18+
if(furi_hal_power_is_otg_enabled()) break;
1419
}
1520
if(attempts == 0) {
1621
if(furi_hal_power_get_usb_voltage() < 4.5f) {
@@ -20,11 +25,15 @@ static void subghz_txrx_radio_device_power_on(SubGhzTxRx* instance) {
2025
furi_hal_power_check_otg_fault() ? 1 : 0);
2126
}
2227
}
28+
29+
furi_record_close(RECORD_POWER);
2330
}
2431

2532
static void subghz_txrx_radio_device_power_off(SubGhzTxRx* instance) {
2633
UNUSED(instance);
27-
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
34+
Power* power = furi_record_open(RECORD_POWER);
35+
if(power_is_otg_requested(power)) power_switch_otg(power, false);
36+
furi_record_close(RECORD_POWER);
2837
}
2938

3039
SubGhzTxRx* subghz_txrx_alloc(void) {

applications/main/subghz/subghz_cli.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
#define TAG "SubGhzCli"
2929

3030
static void subghz_cli_radio_device_power_on(void) {
31+
Power* power = furi_record_open(RECORD_POWER);
32+
3133
uint8_t attempts = 5;
3234
while(--attempts > 0) {
33-
if(furi_hal_power_enable_otg()) break;
35+
power_switch_otg(power, true);
36+
if(furi_hal_power_is_otg_enabled()) break;
3437
}
3538
if(attempts == 0) {
3639
if(furi_hal_power_get_usb_voltage() < 4.5f) {
@@ -40,10 +43,14 @@ static void subghz_cli_radio_device_power_on(void) {
4043
furi_hal_power_check_otg_fault() ? 1 : 0);
4144
}
4245
}
46+
47+
furi_record_close(RECORD_POWER);
4348
}
4449

4550
static void subghz_cli_radio_device_power_off(void) {
46-
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
51+
Power* power = furi_record_open(RECORD_POWER);
52+
if(power_is_otg_requested(power)) power_switch_otg(power, false);
53+
furi_record_close(RECORD_POWER);
4754
}
4855

4956
static SubGhzEnvironment* subghz_cli_environment_init(void) {

0 commit comments

Comments
 (0)