Skip to content

Commit d14c2a7

Browse files
authored
Merge pull request #26 from haisenteck/patch-1
UART, power, and miscellaneous fixes
2 parents 9c47f1d + 76d832f commit d14c2a7

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ the hardware setup.
5959
* [u-blox NEO-6M]
6060
* [u-blox NEO-7M]
6161
* [Uputronics u-blox MAX-M8C Pico]
62+
* Fastrax UP500
6263

6364
If you have verified this application working with a module not listed here,
6465
please submit a PR adding it to the list.

gps.c

+27-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "constants.h"
33

44
#include <furi.h>
5+
#include <furi_hal_power.h>
56
#include <gui/gui.h>
67
#include <string.h>
78

@@ -41,10 +42,10 @@ static void render_callback(Canvas* const canvas, void* context) {
4142

4243
switch(gps_uart->speed_units) {
4344
case KPH:
44-
snprintf(buffer, 64, "%.2f km", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
45+
snprintf(buffer, 64, "%.2f kph", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
4546
break;
4647
case MPH:
47-
snprintf(buffer, 64, "%.2f mi", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
48+
snprintf(buffer, 64, "%.2f mph", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
4849
break;
4950
case KNOTS:
5051
default:
@@ -94,6 +95,20 @@ int32_t gps_app(void* p) {
9495

9596
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
9697

98+
bool otg_was_enabled = furi_hal_power_is_otg_enabled();
99+
uint8_t attempts = 5;
100+
while(--attempts > 0) {
101+
if(furi_hal_power_enable_otg()) break;
102+
}
103+
if(attempts == 0) {
104+
if(furi_hal_power_get_usb_voltage() < 4.5f) {
105+
FURI_LOG_E(
106+
"GPS",
107+
"Error power otg enable. BQ2589 check otg fault = %d",
108+
furi_hal_power_check_otg_fault() ? 1 : 0);
109+
}
110+
}
111+
97112
GpsUart* gps_uart = gps_uart_enable();
98113

99114
gps_uart->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
@@ -159,6 +174,8 @@ int32_t gps_app(void* p) {
159174

160175
gps_uart_init_thread(gps_uart);
161176
gps_uart->changing_baudrate = true;
177+
furi_mutex_release(gps_uart->mutex);
178+
view_port_update(view_port);
162179
break;
163180
case InputKeyRight:
164181
gps_uart->speed_units++;
@@ -175,11 +192,10 @@ int32_t gps_app(void* p) {
175192
}
176193
}
177194
}
178-
179-
view_port_update(view_port);
180-
furi_mutex_release(gps_uart->mutex);
181-
182-
if(gps_uart->changing_baudrate) {
195+
if(!gps_uart->changing_baudrate) {
196+
furi_mutex_release(gps_uart->mutex);
197+
view_port_update(view_port);
198+
} else {
183199
furi_delay_ms(1000);
184200
gps_uart->changing_baudrate = false;
185201
}
@@ -194,5 +210,9 @@ int32_t gps_app(void* p) {
194210
furi_mutex_free(gps_uart->mutex);
195211
gps_uart_disable(gps_uart);
196212

213+
if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
214+
furi_hal_power_disable_otg();
215+
}
216+
197217
return 0;
198218
}

gps_uart.c

+15-14
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@ typedef enum {
1010

1111
#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
1212

13-
static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
13+
static void gps_uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent ev, void* context) {
1414
GpsUart* gps_uart = (GpsUart*)context;
15-
16-
if(ev == UartIrqEventRXNE) {
15+
16+
if(ev == FuriHalSerialRxEventData) {
17+
uint8_t data = furi_hal_serial_async_rx(handle);
1718
furi_stream_buffer_send(gps_uart->rx_stream, &data, 1, 0);
1819
furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtRxDone);
1920
}
2021
}
2122

2223
static void gps_uart_serial_init(GpsUart* gps_uart) {
23-
furi_hal_console_disable();
24-
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gps_uart_on_irq_cb, gps_uart);
25-
furi_hal_uart_set_br(FuriHalUartIdUSART1, gps_uart->baudrate);
24+
furi_assert(!gps_uart->serial_handle);
25+
26+
gps_uart->serial_handle = furi_hal_serial_control_acquire(UART_CH);
27+
furi_assert(gps_uart->serial_handle);
28+
furi_hal_serial_init(gps_uart->serial_handle, gps_uart->baudrate);
29+
furi_hal_serial_async_rx_start(gps_uart->serial_handle, gps_uart_on_irq_cb, gps_uart, false);
2630
}
2731

2832
static void gps_uart_serial_deinit(GpsUart* gps_uart) {
29-
UNUSED(gps_uart);
30-
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
31-
furi_hal_console_enable();
33+
furi_assert(gps_uart->serial_handle);
34+
furi_hal_serial_deinit(gps_uart->serial_handle);
35+
furi_hal_serial_control_release(gps_uart->serial_handle);
36+
gps_uart->serial_handle = NULL;
3237
}
3338

3439
static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line) {
@@ -103,11 +108,7 @@ static int32_t gps_uart_worker(void* context) {
103108
do {
104109
// receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
105110
// the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
106-
len = furi_stream_buffer_receive(
107-
gps_uart->rx_stream,
108-
gps_uart->rx_buf + rx_offset,
109-
RX_BUF_SIZE - 1 - rx_offset,
110-
0);
111+
len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset, 0);
111112
if(len > 0) {
112113
// increase rx_offset by the number of bytes received, and null-terminate rx_buf
113114
rx_offset += len;

gps_uart.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <notification/notification_messages.h>
55

66
#define RX_BUF_SIZE 1024
7+
#define UART_CH (FuriHalSerialIdUsart)
78

89
static const int gps_baudrates[6] = {4800, 9600, 19200, 38400, 57600, 115200};
910
static int current_gps_baudrate = 1;
@@ -36,6 +37,8 @@ typedef struct {
3637
bool changing_baudrate;
3738
bool backlight_on;
3839
SpeedUnit speed_units;
40+
41+
FuriHalSerialHandle* serial_handle;
3942

4043
GpsStatus status;
4144
} GpsUart;

0 commit comments

Comments
 (0)