Просмотр исходного кода

Merge pull request #26 from haisenteck/patch-1

UART, power, and miscellaneous fixes
Aaron Mavrinac 1 год назад
Родитель
Сommit
d14c2a7dc5
4 измененных файлов с 46 добавлено и 21 удалено
  1. 1 0
      README.md
  2. 27 7
      gps.c
  3. 15 14
      gps_uart.c
  4. 3 0
      gps_uart.h

+ 1 - 0
README.md

@@ -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.

+ 27 - 7
gps.c

@@ -2,6 +2,7 @@
 #include "constants.h"
 
 #include <furi.h>
+#include <furi_hal_power.h>
 #include <gui/gui.h>
 #include <string.h>
 
@@ -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:
@@ -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);
@@ -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++;
@@ -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;
         }
@@ -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;
 }

+ 15 - 14
gps_uart.c

@@ -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) {
@@ -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;

+ 3 - 0
gps_uart.h

@@ -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;
@@ -36,6 +37,8 @@ typedef struct {
     bool changing_baudrate;
     bool backlight_on;
     SpeedUnit speed_units;
+	
+    FuriHalSerialHandle* serial_handle;
 
     GpsStatus status;
 } GpsUart;