Explorar o código

add basic adc support

Oliver Fabel hai 1 ano
pai
achega
6886fec6d0

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit 9fd53bc141006058c2b441602c0b7b254bbf9200
+Subproject commit 8772835d303ad98f749cc4f643b7394b26986ecb

+ 2 - 0
lib/micropython-port/mp_flipper_context.h

@@ -12,4 +12,6 @@ typedef struct {
     const char* dialog_message_button_left;
     const char* dialog_message_button_center;
     const char* dialog_message_button_right;
+    FuriHalAdcHandle* adc_handle;
+    bool* gpio_pins_used;
 } mp_flipper_context_t;

+ 43 - 0
lib/micropython-port/mp_flipper_modflipperzero_adc.c

@@ -0,0 +1,43 @@
+#include <furi_hal.h>
+
+#include <mp_flipper_modflipperzero.h>
+#include <mp_flipper_runtime.h>
+
+#include "mp_flipper_context.h"
+
+inline static FuriHalAdcChannel decode_pin_to_adc_channel(uint8_t pin) {
+    switch(pin) {
+    case MP_FLIPPER_GPIO_PIN_PC0:
+        return FuriHalAdcChannel1;
+    case MP_FLIPPER_GPIO_PIN_PC1:
+        return FuriHalAdcChannel2;
+    case MP_FLIPPER_GPIO_PIN_PC3:
+        return FuriHalAdcChannel4;
+    case MP_FLIPPER_GPIO_PIN_PA4:
+        return FuriHalAdcChannel9;
+    case MP_FLIPPER_GPIO_PIN_PA6:
+        return FuriHalAdcChannel11;
+    case MP_FLIPPER_GPIO_PIN_PA7:
+        return FuriHalAdcChannel12;
+    default:
+        return FuriHalAdcChannelNone;
+    }
+}
+
+inline uint16_t mp_flipper_adc_read_pin(uint8_t raw_pin) {
+    mp_flipper_context_t* ctx = mp_flipper_context;
+
+    furi_check(ctx->adc_handle, "missing ADC handle");
+
+    FuriHalAdcChannel channel = decode_pin_to_adc_channel(raw_pin);
+
+    return furi_hal_adc_read(ctx->adc_handle, channel);
+}
+
+inline float mp_flipper_adc_convert_to_voltage(uint16_t value) {
+    mp_flipper_context_t* ctx = mp_flipper_context;
+
+    furi_check(ctx->adc_handle, "missing ADC handle");
+
+    return furi_hal_adc_convert_to_voltage(ctx->adc_handle, value);
+}

+ 31 - 0
lib/micropython-port/mp_flipper_modflipperzero_gpio.c

@@ -1,6 +1,9 @@
 #include <furi_hal.h>
 
 #include <mp_flipper_modflipperzero.h>
+#include <mp_flipper_runtime.h>
+
+#include "mp_flipper_context.h"
 
 static const GpioPin* decode_pin(uint8_t pin) {
     switch(pin) {
@@ -82,6 +85,10 @@ inline void mp_flipper_gpio_init_pin(
     uint8_t raw_mode,
     uint8_t raw_pull,
     uint8_t raw_speed) {
+    mp_flipper_context_t* ctx = mp_flipper_context;
+
+    furi_check(raw_pin < MP_FLIPPER_GPIO_PINS);
+
     const GpioPin* pin = decode_pin(raw_pin);
     const GpioMode mode = decode_mode(raw_mode);
     const GpioPull pull = decode_pull(raw_pull);
@@ -96,6 +103,30 @@ inline void mp_flipper_gpio_init_pin(
         furi_hal_gpio_disable_int_callback(pin);
         furi_hal_gpio_remove_int_callback(pin);
     }
+
+    if(raw_mode == MP_FLIPPER_GPIO_MODE_ANALOG && ctx->adc_handle == NULL) {
+        ctx->adc_handle = furi_hal_adc_acquire();
+
+        furi_hal_adc_configure(ctx->adc_handle);
+    }
+
+    ctx->gpio_pins_used[raw_pin] = true;
+}
+
+inline void mp_flipper_gpio_deinit_pin(uint8_t raw_pin) {
+    mp_flipper_context_t* ctx = mp_flipper_context;
+
+    furi_check(raw_pin < MP_FLIPPER_GPIO_PINS);
+
+    if(ctx->gpio_pins_used[raw_pin]) {
+        const GpioPin* pin = decode_pin(raw_pin);
+
+        furi_hal_gpio_disable_int_callback(pin);
+        furi_hal_gpio_remove_int_callback(pin);
+        furi_hal_gpio_init_simple(pin, GpioModeAnalog);
+
+        ctx->gpio_pins_used[raw_pin] = false;
+    }
 }
 
 inline void mp_flipper_gpio_set_pin(uint8_t raw_pin, bool state) {

+ 21 - 1
lib/micropython-port/mp_flipper_runtime.c

@@ -6,7 +6,7 @@
 
 #include "mp_flipper_context.h"
 
-static void on_input_callback(InputEvent* event, void* ctx) {
+static void on_input_callback(const InputEvent* event, void* ctx) {
     uint16_t button = 1 << event->key;
     uint16_t type = 1 << (InputKeyMAX + event->type);
 
@@ -84,6 +84,14 @@ void* mp_flipper_context_alloc() {
     ctx->dialog_message_button_center = NULL;
     ctx->dialog_message_button_right = NULL;
 
+    ctx->adc_handle = NULL;
+
+    ctx->gpio_pins_used = malloc(MP_FLIPPER_GPIO_PINS * sizeof(bool));
+
+    for(uint8_t pin = 0; pin < MP_FLIPPER_GPIO_PINS; pin++) {
+        ctx->gpio_pins_used[pin] = false;
+    }
+
     return ctx;
 }
 
@@ -103,5 +111,17 @@ void mp_flipper_context_free(void* context) {
     furi_record_close(RECORD_GUI);
     furi_record_close(RECORD_INPUT_EVENTS);
 
+    // disable ADC handle
+    if(ctx->adc_handle) {
+        furi_hal_adc_release(ctx->adc_handle);
+    }
+
+    // de-initialize all GPIO pins
+    for(uint8_t pin = 0; pin < MP_FLIPPER_GPIO_PINS; pin++) {
+        mp_flipper_gpio_deinit_pin(pin);
+    }
+
+    free(ctx->gpio_pins_used);
+
     free(ctx);
 }