Преглед изворни кода

Lf rfid (#196)

* add lf rfid workaround
* add pwmn
* carrier generator
* proper exit for lf rfid

Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
coreglitch пре 5 година
родитељ
комит
e699c4a31e

+ 9 - 0
applications/applications.h

@@ -28,6 +28,7 @@ void backlight_control(void* p);
 void irda(void* p);
 void irda(void* p);
 void app_loader(void* p);
 void app_loader(void* p);
 void cc1101_workaround(void* p);
 void cc1101_workaround(void* p);
+void lf_rfid_workaround(void* p);
 void nfc_task(void* p);
 void nfc_task(void* p);
 
 
 const FlipperStartupApp FLIPPER_STARTUP[] = {
 const FlipperStartupApp FLIPPER_STARTUP[] = {
@@ -57,6 +58,10 @@ const FlipperStartupApp FLIPPER_STARTUP[] = {
     {.app = cc1101_workaround, .name = "cc1101 workaround", .libs = {1, FURI_LIB{"gui_task"}}},
     {.app = cc1101_workaround, .name = "cc1101 workaround", .libs = {1, FURI_LIB{"gui_task"}}},
 #endif
 #endif
 
 
+#ifdef APP_LF_RFID
+    {.app = lf_rfid_workaround, .name = "lf rfid workaround", .libs = {1, FURI_LIB{"gui_task"}}},
+#endif
+
 #ifdef APP_IRDA
 #ifdef APP_IRDA
     {.app = irda, .name = "irda", .libs = {1, FURI_LIB{"gui_task"}}},
     {.app = irda, .name = "irda", .libs = {1, FURI_LIB{"gui_task"}}},
 #endif
 #endif
@@ -108,6 +113,10 @@ const FlipperStartupApp FLIPPER_APPS[] = {
     {.app = cc1101_workaround, .name = "cc1101 workaround", .libs = {1, FURI_LIB{"gui_task"}}},
     {.app = cc1101_workaround, .name = "cc1101 workaround", .libs = {1, FURI_LIB{"gui_task"}}},
 #endif
 #endif
 
 
+#ifdef BUILD_LF_RFID
+    {.app = lf_rfid_workaround, .name = "lf rfid workaround", .libs = {1, FURI_LIB{"gui_task"}}},
+#endif
+
 #ifdef BUILD_IRDA
 #ifdef BUILD_IRDA
     {.app = irda, .name = "irda", .libs = {1, FURI_LIB{"gui_task"}}},
     {.app = irda, .name = "irda", .libs = {1, FURI_LIB{"gui_task"}}},
 #endif
 #endif

+ 15 - 0
applications/applications.mk

@@ -15,6 +15,7 @@ BUILD_EXAMPLE_BLINK = 1
 BUILD_EXAMPLE_UART_WRITE = 1
 BUILD_EXAMPLE_UART_WRITE = 1
 BUILD_EXAMPLE_INPUT_DUMP = 1
 BUILD_EXAMPLE_INPUT_DUMP = 1
 BUILD_CC1101 = 1
 BUILD_CC1101 = 1
+BUILD_LF_RFID = 1
 BUILD_SPEAKER_DEMO = 1
 BUILD_SPEAKER_DEMO = 1
 endif
 endif
 
 
@@ -146,6 +147,20 @@ APP_INPUT = 1
 APP_GUI = 1
 APP_GUI = 1
 endif
 endif
 
 
+APP_LF_RFID ?= 0
+ifeq ($(APP_LF_RFID), 1)
+CFLAGS		+= -DAPP_LF_RFID
+BUILD_LF_RFID = 1
+endif
+BUILD_LF_RFID ?= 0
+ifeq ($(BUILD_LF_RFID), 1)
+CFLAGS		+= -DBUILD_LF_RFID
+C_SOURCES	+= $(wildcard $(APP_DIR)/lf-rfid/*.c)
+CPP_SOURCES	+= $(wildcard $(APP_DIR)/lf-rfid/*.cpp)
+APP_INPUT = 1
+APP_GUI = 1
+endif
+
 APP_IRDA ?= 0
 APP_IRDA ?= 0
 ifeq ($(APP_IRDA), 1)
 ifeq ($(APP_IRDA), 1)
 CFLAGS		+= -DAPP_IRDA
 CFLAGS		+= -DAPP_IRDA

+ 119 - 0
applications/lf-rfid/lf-rfid.c

@@ -0,0 +1,119 @@
+#include "flipper_v2.h"
+
+typedef enum {
+    EventTypeTick,
+    EventTypeKey,
+} EventType;
+
+typedef struct {
+    union {
+        InputEvent input;
+    } value;
+    EventType type;
+} AppEvent;
+
+typedef struct {
+    uint32_t freq_khz;
+    bool on;
+} State;
+
+static void render_callback(CanvasApi* canvas, void* ctx) {
+    State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
+
+    canvas->clear(canvas);
+
+    canvas->set_color(canvas, ColorBlack);
+    canvas->set_font(canvas, FontPrimary);
+    canvas->draw_str(canvas, 2, 12, "LF RFID");
+
+    canvas->draw_str(canvas, 2, 24, state->on ? "ON" : "OFF");
+    char buf[12];
+    sprintf(buf, "%d kHz", state->freq_khz);
+    canvas->draw_str(canvas, 2, 36, buf);
+
+    release_mutex((ValueMutex*)ctx, state);
+}
+
+static void input_callback(InputEvent* input_event, void* ctx) {
+    osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
+
+    AppEvent event;
+    event.type = EventTypeKey;
+    event.value.input = *input_event;
+    osMessageQueuePut(event_queue, &event, 0, 0);
+}
+
+extern TIM_HandleTypeDef htim15;
+
+void lf_rfid_workaround(void* p) {
+    osMessageQueueId_t event_queue = osMessageQueueNew(1, sizeof(AppEvent), NULL);
+
+    State _state;
+    _state.freq_khz = 125;
+    _state.on = false;
+
+    ValueMutex state_mutex;
+    if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
+        printf("cannot create mutex\n");
+        furiac_exit(NULL);
+    }
+
+    Widget* widget = widget_alloc();
+
+    widget_draw_callback_set(widget, render_callback, &state_mutex);
+    widget_input_callback_set(widget, input_callback, event_queue);
+
+    // Open GUI and register widget
+    GuiApi* gui = (GuiApi*)furi_open("gui");
+    if(gui == NULL) {
+        printf("gui is not available\n");
+        furiac_exit(NULL);
+    }
+    gui->add_widget(gui, widget, WidgetLayerFullscreen);
+
+    AppEvent event;
+    while(1) {
+        osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 10000);
+        State* state = (State*)acquire_mutex_block(&state_mutex);
+
+        if(event_status == osOK) {
+            if(event.type == EventTypeKey) {
+                // press events
+                if(event.value.input.state && event.value.input.input == InputBack) {
+                    hal_pwmn_stop(&htim15, TIM_CHANNEL_1); // TODO: move to furiac_onexit
+                    // TODO remove all widgets create by app
+                    widget_enabled_set(widget, false);
+                    furiac_exit(NULL);
+                }
+
+                if(event.value.input.state && event.value.input.input == InputUp) {
+                    state->freq_khz += 10;
+                }
+
+                if(event.value.input.state && event.value.input.input == InputDown) {
+                    state->freq_khz -= 10;
+                }
+
+                if(event.value.input.state && event.value.input.input == InputLeft) {
+                }
+
+                if(event.value.input.state && event.value.input.input == InputRight) {
+                }
+
+                if(event.value.input.state && event.value.input.input == InputOk) {
+                    state->on = !state->on;
+                }
+            }
+        } else {
+            // event timeout
+        }
+
+        hal_pwmn_set(
+            state->on ? 0.5 : 0.0, (float)(state->freq_khz * 1000), &htim15, TIM_CHANNEL_1);
+
+        // common code, for example, force update UI
+        widget_update(widget);
+
+        release_mutex(&state_mutex, state);
+    }
+}

+ 2 - 2
firmware/targets/f2/Src/tim.c

@@ -157,9 +157,9 @@ void MX_TIM15_Init(void)
   TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
   TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
 
 
   htim15.Instance = TIM15;
   htim15.Instance = TIM15;
-  htim15.Init.Prescaler = 0;
+  htim15.Init.Prescaler = 16 - 1;
   htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
   htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
-  htim15.Init.Period = 65535;
+  htim15.Init.Period = 32-1;
   htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   htim15.Init.RepetitionCounter = 0;
   htim15.Init.RepetitionCounter = 0;
   htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
   htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

+ 21 - 0
firmware/targets/f2/api-hal/api-hal-pwm.c

@@ -17,6 +17,27 @@ void hal_pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t chann
     HAL_TIM_PWM_Start(tim, channel);
     HAL_TIM_PWM_Start(tim, channel);
 }
 }
 
 
+void hal_pwmn_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel) {
+    tim->Init.CounterMode = TIM_COUNTERMODE_UP;
+    tim->Init.Period = (uint32_t)((SystemCoreClock / (tim->Init.Prescaler + 1)) / freq - 1);
+    tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
+    tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
+    HAL_TIM_PWM_Init(tim);
+
+    TIM_OC_InitTypeDef sConfigOC;
+
+    sConfigOC.OCMode = TIM_OCMODE_PWM1;
+    sConfigOC.Pulse = (uint16_t)(tim->Init.Period * value);
+    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
+    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
+    HAL_TIM_PWM_ConfigChannel(tim, &sConfigOC, channel);
+    HAL_TIMEx_PWMN_Start(tim, channel);
+}
+
 void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
 void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
     HAL_TIM_PWM_Stop(tim, channel);
     HAL_TIM_PWM_Stop(tim, channel);
+}
+
+void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
+    HAL_TIMEx_PWMN_Stop(tim, channel);
 }
 }

+ 3 - 1
firmware/targets/f2/api-hal/api-hal-pwm.h

@@ -3,4 +3,6 @@
 #include "stdbool.h"
 #include "stdbool.h"
 
 
 void hal_pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel);
 void hal_pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel);
-void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel);
+void hal_pwmn_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel);
+void hal_pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel);
+void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel);