scope_scene_run.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include <float.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <furi_hal_bus.h>
  5. #include <furi_hal_resources.h>
  6. #include <gui/gui.h>
  7. #include <gui/view_dispatcher.h>
  8. #include <gui/scene_manager.h>
  9. #include <gui/modules/submenu.h>
  10. #include <gui/modules/variable_item_list.h>
  11. #include <gui/modules/widget.h>
  12. #include <notification/notification_messages.h>
  13. #include "stm32wbxx_hal.h"
  14. #include "stm32wbxx_hal_tim.h"
  15. #include "stm32wbxx_nucleo.h"
  16. #include "stm32wbxx_hal_adc.h"
  17. #include "../scope_app_i.h"
  18. #define DIGITAL_SCALE_12BITS ((uint32_t)0xFFF)
  19. #define ADC_CONVERTED_DATA_BUFFER_SIZE ((uint32_t)128)
  20. #define VAR_CONVERTED_DATA_INIT_VALUE (DIGITAL_SCALE_12BITS + 1)
  21. #define VAR_CONVERTED_DATA_INIT_VALUE_16BITS (0xFFFF + 1U)
  22. #define __ADC_CALC_DATA_VOLTAGE(__VREFANALOG_VOLTAGE__, __ADC_DATA__) \
  23. ((__ADC_DATA__) * (__VREFANALOG_VOLTAGE__) / DIGITAL_SCALE_12BITS)
  24. #define VDDA_APPLI ((uint32_t)2500)
  25. // ramVector found from - https://community.nxp.com/t5/i-MX-Processors/Relocate-vector-table-to-ITCM/m-p/1302304
  26. // the aligned aspect is key!
  27. #define TABLE_SIZE 79
  28. uint32_t ramVector[TABLE_SIZE + 1] __attribute__((aligned(512)));
  29. const uint32_t AHBPrescTable[16UL] =
  30. {1UL, 3UL, 5UL, 1UL, 1UL, 6UL, 10UL, 32UL, 2UL, 4UL, 8UL, 16UL, 64UL, 128UL, 256UL, 512UL};
  31. const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
  32. const uint32_t MSIRangeTable[16UL] = {
  33. 100000UL,
  34. 200000UL,
  35. 400000UL,
  36. 800000UL,
  37. 1000000UL,
  38. 2000000UL,
  39. 4000000UL,
  40. 8000000UL,
  41. 16000000UL,
  42. 24000000UL,
  43. 32000000UL,
  44. 48000000UL,
  45. 0UL,
  46. 0UL,
  47. 0UL,
  48. 0UL}; /* 0UL values are incorrect cases */
  49. char* time; // Current time period text
  50. double freq; // Current samplerate
  51. uint8_t pause = 0; // Whether we want to pause output or not
  52. enum measureenum type; // Type of measurement we are performing
  53. int toggle = 0; // Used for toggling output GPIO, only used in testing
  54. void Error_Handler() {
  55. while(1) {
  56. }
  57. }
  58. static ADC_HandleTypeDef hadc1;
  59. static DMA_HandleTypeDef hdma_adc1;
  60. static TIM_HandleTypeDef htim2;
  61. __IO uint16_t
  62. aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE]; // Array that ADC data is copied to, via DMA
  63. __IO uint16_t aADCxConvertedData_Voltage_mVoltA
  64. [ADC_CONVERTED_DATA_BUFFER_SIZE]; // Data is converted to range from 0 to 2500
  65. __IO uint16_t aADCxConvertedData_Voltage_mVoltB
  66. [ADC_CONVERTED_DATA_BUFFER_SIZE]; // Data is converted to range from 0 to 2500
  67. __IO uint8_t ubDmaTransferStatus = 2; // DMA transfer status
  68. __IO uint16_t* mvoltWrite =
  69. &aADCxConvertedData_Voltage_mVoltA[0]; // Pointer to area we write converted voltage data to
  70. __IO uint16_t* mvoltDisplay =
  71. &aADCxConvertedData_Voltage_mVoltB[0]; // Pointer to area of memory we display
  72. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
  73. GPIO_InitTypeDef GPIO_InitStruct = {0};
  74. if(hadc->Instance == ADC1) {
  75. __HAL_RCC_ADC_CLK_ENABLE();
  76. __HAL_RCC_GPIOC_CLK_ENABLE();
  77. GPIO_InitStruct.Pin = GPIO_PIN_0;
  78. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  79. GPIO_InitStruct.Pull = GPIO_NOPULL;
  80. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  81. hdma_adc1.Instance = DMA1_Channel1;
  82. hdma_adc1.Init.Request = DMA_REQUEST_ADC1;
  83. hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
  84. hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
  85. hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
  86. hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  87. hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  88. hdma_adc1.Init.Mode = DMA_CIRCULAR;
  89. hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
  90. if(HAL_DMA_Init(&hdma_adc1) != HAL_OK) {
  91. Error_Handler();
  92. }
  93. __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1);
  94. HAL_NVIC_SetPriority(ADC1_IRQn, 15, 0);
  95. HAL_NVIC_EnableIRQ(ADC1_IRQn);
  96. }
  97. }
  98. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) {
  99. if(hadc->Instance == ADC1) {
  100. __HAL_RCC_ADC_CLK_DISABLE();
  101. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
  102. HAL_DMA_DeInit(hadc->DMA_Handle);
  103. HAL_NVIC_DisableIRQ(ADC1_IRQn);
  104. }
  105. }
  106. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) {
  107. if(htim_base->Instance == TIM2) {
  108. __HAL_RCC_TIM2_CLK_ENABLE();
  109. HAL_NVIC_SetPriority(TIM2_IRQn, 15, 0);
  110. HAL_NVIC_EnableIRQ(TIM2_IRQn);
  111. }
  112. }
  113. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) {
  114. if(htim_base->Instance == TIM2) {
  115. __HAL_RCC_TIM2_CLK_DISABLE();
  116. HAL_NVIC_DisableIRQ(TIM2_IRQn);
  117. }
  118. }
  119. void DMA1_Channel1_IRQHandler(void) {
  120. HAL_DMA_IRQHandler(&hdma_adc1);
  121. }
  122. void ADC1_IRQHandler(void) {
  123. HAL_ADC_IRQHandler(&hadc1);
  124. }
  125. void TIM2_IRQHandler(void) {
  126. HAL_TIM_IRQHandler(&htim2);
  127. }
  128. // Setup ADC1 to be triggered by timer2
  129. static void MX_ADC1_Init(void) {
  130. ADC_ChannelConfTypeDef sConfig = {0};
  131. hadc1.Instance = ADC1;
  132. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  133. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  134. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  135. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  136. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  137. hadc1.Init.LowPowerAutoWait = DISABLE;
  138. hadc1.Init.ContinuousConvMode = DISABLE;
  139. hadc1.Init.NbrOfConversion = 1;
  140. hadc1.Init.DiscontinuousConvMode = DISABLE;
  141. hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO;
  142. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  143. hadc1.Init.DMAContinuousRequests = ENABLE;
  144. hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  145. hadc1.Init.OversamplingMode = DISABLE;
  146. if(HAL_ADC_Init(&hadc1) != HAL_OK) {
  147. Error_Handler();
  148. }
  149. sConfig.Channel = ADC_CHANNEL_1;
  150. sConfig.Rank = ADC_REGULAR_RANK_1;
  151. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  152. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  153. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  154. sConfig.Offset = 0;
  155. if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
  156. Error_Handler();
  157. }
  158. }
  159. // Only used in testing, for toggling GPIO pin, to measure timer frequency
  160. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
  161. if(htim->Instance == TIM2) {
  162. toggle ^= 1;
  163. furi_hal_gpio_write(&gpio_ext_pa7, toggle);
  164. }
  165. }
  166. // Init timer2
  167. static void MX_TIM2_Init(uint32_t period) {
  168. if(!furi_hal_bus_is_enabled(FuriHalBusTIM2)) {
  169. furi_hal_bus_enable(FuriHalBusTIM2);
  170. }
  171. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  172. TIM_MasterConfigTypeDef sMasterConfig = {0};
  173. htim2.Instance = TIM2;
  174. htim2.Init.Prescaler = 1;
  175. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  176. htim2.Init.Period = period;
  177. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  178. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  179. if(HAL_TIM_Base_Init(&htim2) != HAL_OK) {
  180. Error_Handler();
  181. }
  182. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  183. if(HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) {
  184. Error_Handler();
  185. }
  186. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  187. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  188. if(HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) {
  189. Error_Handler();
  190. }
  191. }
  192. static void MX_DMA_Init(void) {
  193. __HAL_RCC_DMAMUX1_CLK_ENABLE();
  194. __HAL_RCC_DMA1_CLK_ENABLE();
  195. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 15, 0);
  196. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  197. }
  198. static void MX_GPIO_Init(void) {
  199. __HAL_RCC_GPIOC_CLK_ENABLE();
  200. }
  201. // Swap pointer addresses, used for double buffer
  202. void swap(__IO uint16_t** a, __IO uint16_t** b) {
  203. __IO uint16_t* tmp;
  204. tmp = *a;
  205. *a = *b;
  206. *b = tmp;
  207. }
  208. // Write end half of DMA buffer to converted output
  209. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
  210. UNUSED(hadc);
  211. uint32_t tmp_index = 0;
  212. for(tmp_index = (ADC_CONVERTED_DATA_BUFFER_SIZE / 2);
  213. tmp_index < ADC_CONVERTED_DATA_BUFFER_SIZE;
  214. tmp_index++) {
  215. mvoltWrite[tmp_index] = __ADC_CALC_DATA_VOLTAGE(VDDA_APPLI, aADCxConvertedData[tmp_index]);
  216. }
  217. ubDmaTransferStatus = 1;
  218. // Swap double buffer, so new data can be displayed, provided we're not paused
  219. if(!pause) swap(&mvoltWrite, &mvoltDisplay);
  220. }
  221. // Write first half of DMA buffer to converted output
  222. void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) {
  223. UNUSED(hadc);
  224. uint32_t tmp_index = 0;
  225. for(tmp_index = 0; tmp_index < (ADC_CONVERTED_DATA_BUFFER_SIZE / 2); tmp_index++) {
  226. mvoltWrite[tmp_index] = __ADC_CALC_DATA_VOLTAGE(VDDA_APPLI, aADCxConvertedData[tmp_index]);
  227. }
  228. ubDmaTransferStatus = 0;
  229. }
  230. void HAL_ADC_ErrorCallback(ADC_HandleTypeDef* hadc) {
  231. UNUSED(hadc);
  232. Error_Handler();
  233. }
  234. // Used to draw to display
  235. static void app_draw_callback(Canvas* canvas, void* ctx) {
  236. UNUSED(ctx);
  237. static int16_t index[ADC_CONVERTED_DATA_BUFFER_SIZE];
  238. static float data[ADC_CONVERTED_DATA_BUFFER_SIZE];
  239. static float crossings[ADC_CONVERTED_DATA_BUFFER_SIZE];
  240. static char buf1[50];
  241. float max = 0.0;
  242. float min = FLT_MAX;
  243. int count = 0;
  244. // Calculate voltage measurements
  245. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  246. if(mvoltDisplay[x] < min) min = mvoltDisplay[x];
  247. if(mvoltDisplay[x] > max) max = mvoltDisplay[x];
  248. }
  249. max /= 1000;
  250. min /= 1000;
  251. switch(type) {
  252. case m_time: {
  253. // Display current time period
  254. snprintf(buf1, 50, "Time: %s", time);
  255. canvas_draw_str(canvas, 10, 10, buf1);
  256. // Shift waveform across a virtual 0 line, so it crosses 0
  257. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  258. index[x] = -1;
  259. crossings[x] = -1.0;
  260. data[x] = ((float)mvoltDisplay[x] / 1000) - min;
  261. data[x] = ((2 / (max - min)) * data[x]) - 1;
  262. }
  263. // Find points at which waveform crosses virtual 0 line
  264. for(uint32_t x = 1; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  265. if(data[x] >= 0 && data[x - 1] < 0) {
  266. index[count++] = x - 1;
  267. }
  268. }
  269. count = 0;
  270. // Linear interpolation to find zero crossings
  271. // see https://gist.github.com/endolith/255291 for Python version
  272. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  273. if(index[x] == -1) break;
  274. crossings[count++] =
  275. (float)index[x] - data[index[x]] / (data[index[x] + 1] - data[index[x]]);
  276. }
  277. float avg = 0.0;
  278. float countv = 0.0;
  279. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  280. if(x + 1 >= ADC_CONVERTED_DATA_BUFFER_SIZE) break;
  281. if(crossings[x] == -1 || crossings[x + 1] == -1) break;
  282. avg += crossings[x + 1] - crossings[x];
  283. countv += 1;
  284. }
  285. avg /= countv;
  286. // Display frequency of waveform
  287. snprintf(buf1, 50, "Freq: %.1f Hz", (double)((float)freq / avg));
  288. canvas_draw_str(canvas, 10, 20, buf1);
  289. } break;
  290. case m_voltage: {
  291. // Display max, min, peak-to-peak voltages
  292. snprintf(buf1, 50, "Max: %.2fV", (double)max);
  293. canvas_draw_str(canvas, 10, 10, buf1);
  294. snprintf(buf1, 50, "Min: %.2fV", (double)min);
  295. canvas_draw_str(canvas, 10, 20, buf1);
  296. snprintf(buf1, 50, "Vpp: %.2fV", (double)(max - min));
  297. canvas_draw_str(canvas, 10, 30, buf1);
  298. } break;
  299. default:
  300. break;
  301. }
  302. // Draw lines between each data point
  303. for(uint32_t x = 1; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  304. uint32_t prev = 64 - (mvoltDisplay[x - 1] / (VDDA_APPLI / 64));
  305. uint32_t cur = 64 - (mvoltDisplay[x] / (VDDA_APPLI / 64));
  306. canvas_draw_line(canvas, x - 1, prev, x, cur);
  307. }
  308. // Draw graph lines
  309. canvas_draw_line(canvas, 0, 0, 0, 63);
  310. canvas_draw_line(canvas, 0, 63, 128, 63);
  311. }
  312. static void app_input_callback(InputEvent* input_event, void* ctx) {
  313. furi_assert(ctx);
  314. FuriMessageQueue* event_queue = ctx;
  315. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  316. }
  317. void scope_scene_run_widget_callback(GuiButtonType result, InputType type, void* context) {
  318. ScopeApp* app = context;
  319. if(type == InputTypeShort) {
  320. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  321. }
  322. }
  323. void scope_scene_run_on_enter(void* context) {
  324. ScopeApp* app = context;
  325. // Find string representation of time period we're using
  326. for(uint32_t i = 0; i < COUNT_OF(time_list); i++) {
  327. if(time_list[i].time == app->time) {
  328. time = time_list[i].str;
  329. break;
  330. }
  331. }
  332. // Currently un-paused
  333. pause = 0;
  334. // What type of measurement are we performing
  335. type = app->measurement;
  336. // Test purposes
  337. //furi_hal_gpio_write(&gpio_ext_pa7, false);
  338. //furi_hal_gpio_init( &gpio_ext_pa7, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  339. // Copy vector table, modify to use our own IRQ handlers
  340. __disable_irq();
  341. memcpy(ramVector, (uint32_t*)(FLASH_BASE | SCB->VTOR), sizeof(uint32_t) * TABLE_SIZE);
  342. SCB->VTOR = (uint32_t)ramVector;
  343. ramVector[27] = (uint32_t)DMA1_Channel1_IRQHandler;
  344. ramVector[34] = (uint32_t)ADC1_IRQHandler;
  345. ramVector[44] = (uint32_t)TIM2_IRQHandler;
  346. __enable_irq();
  347. // Found this recommended by https://www.freertos.org/RTOS-Cortex-M3-M4.html
  348. // although we're using after RTOS started
  349. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  350. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  351. uint32_t tmp_index_adc_converted_data = 0;
  352. MX_GPIO_Init();
  353. MX_DMA_Init();
  354. // Hack -- PCLK1 - seems to be twice what is reported? Not sure how?
  355. uint32_t period = (uint32_t)((double)(HAL_RCC_GetPCLK1Freq() * 2) * app->time);
  356. freq = 1 / app->time;
  357. MX_TIM2_Init(period);
  358. // Set VREFBUF to 2.5V, as vref isn't connected to 3.3V itself in the flipper zero
  359. VREFBUF->CSR |= VREFBUF_CSR_ENVR;
  360. VREFBUF->CSR &= ~VREFBUF_CSR_HIZ;
  361. VREFBUF->CSR |= VREFBUF_CSR_VRS;
  362. while(!(VREFBUF->CSR & VREFBUF_CSR_VRR)) {
  363. };
  364. MX_ADC1_Init();
  365. // Setup initial values from ADC
  366. for(tmp_index_adc_converted_data = 0;
  367. tmp_index_adc_converted_data < ADC_CONVERTED_DATA_BUFFER_SIZE;
  368. tmp_index_adc_converted_data++) {
  369. aADCxConvertedData[tmp_index_adc_converted_data] = VAR_CONVERTED_DATA_INIT_VALUE;
  370. aADCxConvertedData_Voltage_mVoltA[tmp_index_adc_converted_data] = 0;
  371. aADCxConvertedData_Voltage_mVoltB[tmp_index_adc_converted_data] = 0;
  372. }
  373. if(HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK) {
  374. Error_Handler();
  375. }
  376. // Use to generate interrupt to toggle GPIO for testing
  377. //if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK) {
  378. if(HAL_TIM_Base_Start(&htim2) != HAL_OK) {
  379. Error_Handler();
  380. }
  381. // Start DMA transfer
  382. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)aADCxConvertedData, ADC_CONVERTED_DATA_BUFFER_SIZE) !=
  383. HAL_OK) {
  384. Error_Handler();
  385. }
  386. ViewPort* view_port = view_port_alloc();
  387. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  388. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  389. // Register view port in GUI
  390. Gui* gui = furi_record_open(RECORD_GUI);
  391. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  392. InputEvent event;
  393. bool running = true;
  394. while(running) {
  395. if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
  396. if((event.type == InputTypePress) || (event.type == InputTypeRepeat)) {
  397. switch(event.key) {
  398. case InputKeyLeft:
  399. break;
  400. case InputKeyRight:
  401. break;
  402. case InputKeyUp:
  403. break;
  404. case InputKeyDown:
  405. break;
  406. case InputKeyOk:
  407. pause ^= 1;
  408. break;
  409. default:
  410. running = false;
  411. break;
  412. }
  413. }
  414. }
  415. view_port_update(view_port);
  416. }
  417. // Stop DMA and switch back to original vector table
  418. HAL_ADC_Stop_DMA(&hadc1);
  419. __disable_irq();
  420. SCB->VTOR = 0;
  421. __enable_irq();
  422. if(furi_hal_bus_is_enabled(FuriHalBusTIM2)) {
  423. furi_hal_bus_disable(FuriHalBusTIM2);
  424. }
  425. view_port_enabled_set(view_port, false);
  426. gui_remove_view_port(gui, view_port);
  427. view_port_free(view_port);
  428. // Switch back to original scene
  429. furi_record_close(RECORD_GUI);
  430. scene_manager_previous_scene(app->scene_manager);
  431. submenu_set_selected_item(app->submenu, 0);
  432. }
  433. bool scope_scene_run_on_event(void* context, SceneManagerEvent event) {
  434. ScopeApp* app = context;
  435. bool consumed = false;
  436. UNUSED(app);
  437. UNUSED(event);
  438. return consumed;
  439. }
  440. void scope_scene_run_on_exit(void* context) {
  441. ScopeApp* app = context;
  442. // Clear views
  443. widget_reset(app->widget);
  444. }