Oliver Fabel пре 1 година
родитељ
комит
bec09b2492

+ 1 - 0
CHANGELOG.md

@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
   * Log levels according to the Flipper Zero API: trace, debug, info, warn, error.
   * Only the root logger is supported, so no `getLogger` function.
   * Logs directly to the log output, so no output in the REPL.
+* UART support for the `flipperzero` module.
 
 ### Fixed
 

+ 16 - 0
examples/uart.py

@@ -0,0 +1,16 @@
+import time
+import flipperzero as f0
+
+def read_from_uart():
+  with f0.uart_open(f0.UART_MODE_USART, 115200) as uart:
+    for _ in range(1000):
+      raw_data = uart.read()
+
+      if len(raw_data) > 0:
+        data = raw_data.decode()
+
+        print(data)
+
+      time.sleep_ms(10)
+
+read_from_uart()

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit b8dd933830ff4128111e375f6d9f09cde02708a9
+Subproject commit a9aec44c88a78f16160beabb81474c741287a486

+ 2 - 0
lib/micropython-port/mp_flipper_file_helper.c

@@ -4,6 +4,8 @@
 #include <furi.h>
 #include <storage/storage.h>
 
+#include <py/mperrno.h>
+
 #include "mp_flipper_context.h"
 
 mp_flipper_import_stat_t mp_flipper_try_resolve_filesystem_path(FuriString* path) {

+ 2 - 0
lib/micropython-port/mp_flipper_file_reader.c

@@ -3,6 +3,8 @@
 #include <furi.h>
 #include <storage/storage.h>
 
+#include <py/mperrno.h>
+
 #include <mp_flipper_runtime.h>
 #include <mp_flipper_file_reader.h>
 

+ 81 - 0
lib/micropython-port/mp_flipper_modflipperzero_uart.c

@@ -0,0 +1,81 @@
+#include <furi.h>
+#include <furi_hal.h>
+
+#include <mp_flipper_modflipperzero.h>
+
+typedef struct {
+    FuriHalSerialHandle* handle;
+    FuriStreamBuffer* rx_buffer;
+} mp_flipper_uart_ctx_t;
+
+static void on_uart_rx(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
+    FuriStreamBuffer* buffer = context;
+
+    if(event & FuriHalSerialRxEventData) {
+        uint8_t data = furi_hal_serial_async_rx(handle);
+        furi_stream_buffer_send(buffer, &data, 1, 0);
+    }
+}
+
+inline void* mp_flipper_uart_open(uint8_t raw_mode, uint32_t baud_rate) {
+    FuriHalSerialId mode = raw_mode == MP_FLIPPER_UART_MODE_USART ? FuriHalSerialIdUsart :
+                                                                    FuriHalSerialIdLpuart;
+
+    if(furi_hal_serial_control_is_busy(mode)) {
+        return NULL;
+    }
+
+    mp_flipper_uart_ctx_t* ctx = malloc(sizeof(mp_flipper_uart_ctx_t));
+
+    ctx->handle = furi_hal_serial_control_acquire(mode);
+    ctx->rx_buffer = furi_stream_buffer_alloc(512, 1);
+
+    furi_hal_serial_init(ctx->handle, baud_rate);
+
+    furi_hal_serial_async_rx_start(ctx->handle, on_uart_rx, ctx->rx_buffer, false);
+
+    return ctx;
+}
+
+inline bool mp_flipper_uart_close(void* handle) {
+    mp_flipper_uart_ctx_t* ctx = handle;
+
+    furi_hal_serial_deinit(ctx->handle);
+    furi_hal_serial_control_release(ctx->handle);
+
+    furi_stream_buffer_free(ctx->rx_buffer);
+
+    free(ctx);
+
+    return true;
+}
+
+inline bool mp_flipper_uart_sync(void* handle) {
+    mp_flipper_uart_ctx_t* ctx = handle;
+
+    furi_hal_serial_tx_wait_complete(ctx->handle);
+}
+
+inline size_t mp_flipper_uart_read(void* handle, void* buffer, size_t size, int* errcode) {
+    mp_flipper_uart_ctx_t* ctx = handle;
+
+    size_t read = 0;
+    size_t total = 0;
+    size_t left = size;
+
+    do {
+        read = furi_stream_buffer_receive(ctx->rx_buffer, &buffer[read], left, 0);
+        total += read;
+        left -= read;
+    } while(read > 0);
+
+    return total;
+}
+
+inline size_t mp_flipper_uart_write(void* handle, const void* buffer, size_t size, int* errcode) {
+    mp_flipper_uart_ctx_t* ctx = handle;
+
+    furi_hal_serial_tx(ctx->handle, buffer, size);
+
+    return size;
+}

+ 2 - 0
lib/micropython-port/mp_flipper_runtime.c

@@ -1,6 +1,8 @@
 #include <furi.h>
 #include <storage/storage.h>
 
+#include <py/mperrno.h>
+
 #include <mp_flipper_runtime.h>
 #include <mp_flipper_modflipperzero.h>