Skip to content

Commit

Permalink
Merge pull request #26 from haisenteck/patch-1
Browse files Browse the repository at this point in the history
UART, power, and miscellaneous fixes
  • Loading branch information
ezod authored Feb 26, 2024
2 parents 9c47f1d + 76d832f commit d14c2a7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ the hardware setup.
* [u-blox NEO-6M]
* [u-blox NEO-7M]
* [Uputronics u-blox MAX-M8C Pico]
* Fastrax UP500

If you have verified this application working with a module not listed here,
please submit a PR adding it to the list.
Expand Down
34 changes: 27 additions & 7 deletions gps.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "constants.h"

#include <furi.h>
#include <furi_hal_power.h>
#include <gui/gui.h>
#include <string.h>

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

switch(gps_uart->speed_units) {
case KPH:
snprintf(buffer, 64, "%.2f km", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
snprintf(buffer, 64, "%.2f kph", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
break;
case MPH:
snprintf(buffer, 64, "%.2f mi", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
snprintf(buffer, 64, "%.2f mph", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
break;
case KNOTS:
default:
Expand Down Expand Up @@ -94,6 +95,20 @@ int32_t gps_app(void* p) {

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

bool otg_was_enabled = furi_hal_power_is_otg_enabled();
uint8_t attempts = 5;
while(--attempts > 0) {
if(furi_hal_power_enable_otg()) break;
}
if(attempts == 0) {
if(furi_hal_power_get_usb_voltage() < 4.5f) {
FURI_LOG_E(
"GPS",
"Error power otg enable. BQ2589 check otg fault = %d",
furi_hal_power_check_otg_fault() ? 1 : 0);
}
}

GpsUart* gps_uart = gps_uart_enable();

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

gps_uart_init_thread(gps_uart);
gps_uart->changing_baudrate = true;
furi_mutex_release(gps_uart->mutex);
view_port_update(view_port);
break;
case InputKeyRight:
gps_uart->speed_units++;
Expand All @@ -175,11 +192,10 @@ int32_t gps_app(void* p) {
}
}
}

view_port_update(view_port);
furi_mutex_release(gps_uart->mutex);

if(gps_uart->changing_baudrate) {
if(!gps_uart->changing_baudrate) {
furi_mutex_release(gps_uart->mutex);
view_port_update(view_port);
} else {
furi_delay_ms(1000);
gps_uart->changing_baudrate = false;
}
Expand All @@ -194,5 +210,9 @@ int32_t gps_app(void* p) {
furi_mutex_free(gps_uart->mutex);
gps_uart_disable(gps_uart);

if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
furi_hal_power_disable_otg();
}

return 0;
}
29 changes: 15 additions & 14 deletions gps_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ typedef enum {

#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)

static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
static void gps_uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent ev, void* context) {
GpsUart* gps_uart = (GpsUart*)context;

if(ev == UartIrqEventRXNE) {

if(ev == FuriHalSerialRxEventData) {
uint8_t data = furi_hal_serial_async_rx(handle);
furi_stream_buffer_send(gps_uart->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtRxDone);
}
}

static void gps_uart_serial_init(GpsUart* gps_uart) {
furi_hal_console_disable();
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gps_uart_on_irq_cb, gps_uart);
furi_hal_uart_set_br(FuriHalUartIdUSART1, gps_uart->baudrate);
furi_assert(!gps_uart->serial_handle);

gps_uart->serial_handle = furi_hal_serial_control_acquire(UART_CH);
furi_assert(gps_uart->serial_handle);
furi_hal_serial_init(gps_uart->serial_handle, gps_uart->baudrate);
furi_hal_serial_async_rx_start(gps_uart->serial_handle, gps_uart_on_irq_cb, gps_uart, false);
}

static void gps_uart_serial_deinit(GpsUart* gps_uart) {
UNUSED(gps_uart);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
furi_hal_console_enable();
furi_assert(gps_uart->serial_handle);
furi_hal_serial_deinit(gps_uart->serial_handle);
furi_hal_serial_control_release(gps_uart->serial_handle);
gps_uart->serial_handle = NULL;
}

static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line) {
Expand Down Expand Up @@ -103,11 +108,7 @@ static int32_t gps_uart_worker(void* context) {
do {
// receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
// the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
len = furi_stream_buffer_receive(
gps_uart->rx_stream,
gps_uart->rx_buf + rx_offset,
RX_BUF_SIZE - 1 - rx_offset,
0);
len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset, 0);
if(len > 0) {
// increase rx_offset by the number of bytes received, and null-terminate rx_buf
rx_offset += len;
Expand Down
3 changes: 3 additions & 0 deletions gps_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <notification/notification_messages.h>

#define RX_BUF_SIZE 1024
#define UART_CH (FuriHalSerialIdUsart)

static const int gps_baudrates[6] = {4800, 9600, 19200, 38400, 57600, 115200};
static int current_gps_baudrate = 1;
Expand Down Expand Up @@ -36,6 +37,8 @@ typedef struct {
bool changing_baudrate;
bool backlight_on;
SpeedUnit speed_units;

FuriHalSerialHandle* serial_handle;

GpsStatus status;
} GpsUart;
Expand Down

0 comments on commit d14c2a7

Please sign in to comment.