view_direct_sampling.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. #include <cc1101_regs.h>
  5. static void direct_sampling_timer_start(ProtoViewApp *app);
  6. static void direct_sampling_timer_stop(ProtoViewApp *app);
  7. #define CAPTURED_BITMAP_BITS (128*64)
  8. #define CAPTURED_BITMAP_BYTES (CAPTURED_BITMAP_BITS/8)
  9. #define DEFAULT_USEC_PER_PIXEL 50
  10. #define USEC_PER_PIXEL_SMALL_CHANGE 5
  11. #define USEC_PER_PIXEL_LARGE_CHANGE 25
  12. #define USEC_PER_PIXEL_MIN 5
  13. #define USEC_PER_PIXEL_MAX 300
  14. typedef struct {
  15. uint8_t *captured; // Bitmap with the last captured screen.
  16. uint32_t captured_idx; // Current index to write into the bitmap
  17. uint32_t usec_per_pixel; // Number of useconds a pixel should represent
  18. bool show_usage_info;
  19. } DirectSamplingViewPrivData;
  20. /* Read directly from the G0 CC1101 pin, and draw a black or white
  21. * dot depending on the level. */
  22. void render_view_direct_sampling(Canvas *const canvas, ProtoViewApp *app) {
  23. DirectSamplingViewPrivData *privdata = app->view_privdata;
  24. if (!app->direct_sampling_enabled && privdata->show_usage_info) {
  25. canvas_set_font(canvas, FontSecondary);
  26. canvas_draw_str(canvas,2,9, "Direct sampling displays the");
  27. canvas_draw_str(canvas,2,18,"the captured signal in real");
  28. canvas_draw_str(canvas,2,27,"time, like in a CRT TV set.");
  29. canvas_draw_str(canvas,2,36,"Use UP/DOWN to change the");
  30. canvas_draw_str(canvas,2,45,"resolution (usec/pixel).");
  31. canvas_set_font(canvas, FontPrimary);
  32. canvas_draw_str(canvas,5,60,"To start/stop, press OK");
  33. return;
  34. }
  35. privdata->show_usage_info = false;
  36. /* Draw on screen. */
  37. int idx = 0;
  38. for (int y = 0; y < 64; y++) {
  39. for (int x = 0; x < 128; x++) {
  40. bool level = bitmap_get(privdata->captured,
  41. CAPTURED_BITMAP_BYTES,idx++);
  42. if (level) canvas_draw_dot(canvas,x,y);
  43. }
  44. }
  45. char buf[32];
  46. snprintf(buf,sizeof(buf),"%lu usec/px", privdata->usec_per_pixel);
  47. canvas_set_font(canvas, FontSecondary);
  48. canvas_draw_str_with_border(canvas,1,60,buf,ColorWhite,ColorBlack);
  49. }
  50. /* Handle input */
  51. void process_input_direct_sampling(ProtoViewApp *app, InputEvent input) {
  52. DirectSamplingViewPrivData *privdata = app->view_privdata;
  53. if (input.type == InputTypePress && input.key == InputKeyOk) {
  54. app->direct_sampling_enabled = !app->direct_sampling_enabled;
  55. }
  56. if ((input.key == InputKeyUp || input.key == InputKeyDown) &&
  57. (input.type == InputTypePress || input.type == InputTypeRepeat))
  58. {
  59. uint32_t change = input.type == InputTypePress ?
  60. USEC_PER_PIXEL_SMALL_CHANGE :
  61. USEC_PER_PIXEL_LARGE_CHANGE;
  62. if (input.key == InputKeyUp) change = -change;
  63. privdata->usec_per_pixel += change;
  64. if (privdata->usec_per_pixel < USEC_PER_PIXEL_MIN)
  65. privdata->usec_per_pixel = USEC_PER_PIXEL_MIN;
  66. else if (privdata->usec_per_pixel > USEC_PER_PIXEL_MAX)
  67. privdata->usec_per_pixel = USEC_PER_PIXEL_MAX;
  68. /* Update the timer frequency. */
  69. direct_sampling_timer_stop(app);
  70. direct_sampling_timer_start(app);
  71. }
  72. }
  73. /* Enter view. Stop the subghz thread to prevent access as we read
  74. * the CC1101 data directly. */
  75. void view_enter_direct_sampling(ProtoViewApp *app) {
  76. /* Set view defaults. */
  77. DirectSamplingViewPrivData *privdata = app->view_privdata;
  78. privdata->usec_per_pixel = DEFAULT_USEC_PER_PIXEL;
  79. privdata->captured = malloc(CAPTURED_BITMAP_BYTES);
  80. privdata->show_usage_info = true;
  81. if (app->txrx->txrx_state == TxRxStateRx &&
  82. !app->txrx->debug_timer_sampling)
  83. {
  84. subghz_devices_stop_async_rx(app->radio_device);
  85. /* To read data asynchronously directly from the view, we need
  86. * to put the CC1101 back into reception mode (the previous call
  87. * to stop the async RX will put it into idle) and configure the
  88. * G0 pin for reading. */
  89. subghz_devices_set_rx(app->radio_device);
  90. furi_hal_gpio_init(
  91. subghz_devices_get_data_gpio(app->radio_device),
  92. GpioModeInput,
  93. GpioPullNo,
  94. GpioSpeedLow);
  95. } else {
  96. raw_sampling_worker_stop(app);
  97. }
  98. // Start the timer to capture raw data
  99. direct_sampling_timer_start(app);
  100. }
  101. /* Exit view. Restore the subghz thread. */
  102. void view_exit_direct_sampling(ProtoViewApp *app) {
  103. DirectSamplingViewPrivData *privdata = app->view_privdata;
  104. if (privdata->captured) free(privdata->captured);
  105. app->direct_sampling_enabled = false;
  106. direct_sampling_timer_stop(app);
  107. /* Restart normal data feeding. */
  108. if (app->txrx->txrx_state == TxRxStateRx &&
  109. !app->txrx->debug_timer_sampling)
  110. {
  111. subghz_devices_start_async_rx(app->radio_device, protoview_rx_callback, NULL);
  112. } else {
  113. furi_hal_gpio_init(
  114. subghz_devices_get_data_gpio(app->radio_device),
  115. GpioModeInput,
  116. GpioPullNo,
  117. GpioSpeedLow);
  118. raw_sampling_worker_start(app);
  119. }
  120. }
  121. /* =========================== Timer implementation ========================= */
  122. static void ds_timer_isr(void *ctx) {
  123. ProtoViewApp *app = ctx;
  124. DirectSamplingViewPrivData *privdata = app->view_privdata;
  125. if (app->direct_sampling_enabled) {
  126. bool level = furi_hal_gpio_read(subghz_devices_get_data_gpio(app->radio_device));
  127. bitmap_set(privdata->captured,CAPTURED_BITMAP_BYTES,
  128. privdata->captured_idx,level);
  129. privdata->captured_idx = (privdata->captured_idx+1) %
  130. CAPTURED_BITMAP_BITS;
  131. }
  132. LL_TIM_ClearFlag_UPDATE(TIM2);
  133. }
  134. static void direct_sampling_timer_start(ProtoViewApp *app) {
  135. DirectSamplingViewPrivData *privdata = app->view_privdata;
  136. furi_hal_bus_enable(FuriHalBusTIM2);
  137. LL_TIM_InitTypeDef tim_init = {
  138. .Prescaler = 63, /* CPU frequency is ~64Mhz. */
  139. .CounterMode = LL_TIM_COUNTERMODE_UP,
  140. .Autoreload = privdata->usec_per_pixel
  141. };
  142. LL_TIM_Init(TIM2, &tim_init);
  143. LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  144. LL_TIM_DisableCounter(TIM2);
  145. LL_TIM_SetCounter(TIM2, 0);
  146. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, ds_timer_isr, app);
  147. LL_TIM_EnableIT_UPDATE(TIM2);
  148. LL_TIM_EnableCounter(TIM2);
  149. }
  150. static void direct_sampling_timer_stop(ProtoViewApp *app) {
  151. UNUSED(app);
  152. FURI_CRITICAL_ENTER();
  153. LL_TIM_DisableCounter(TIM2);
  154. LL_TIM_DisableIT_UPDATE(TIM2);
  155. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
  156. furi_hal_bus_disable(FuriHalBusTIM2);
  157. FURI_CRITICAL_EXIT();
  158. }