scope_scene_run.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #include <float.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <furi_hal_resources.h>
  5. #include <gui/gui.h>
  6. #include <gui/view_dispatcher.h>
  7. #include <gui/scene_manager.h>
  8. #include <gui/modules/submenu.h>
  9. #include <gui/modules/variable_item_list.h>
  10. #include <gui/modules/widget.h>
  11. #include <notification/notification_messages.h>
  12. #include "stm32wbxx_ll_adc.h"
  13. #include "stm32wbxx_ll_dma.h"
  14. #include "stm32wbxx_ll_crs.h"
  15. #include "stm32wbxx_ll_rcc.h"
  16. #include "stm32wbxx_ll_bus.h"
  17. #include "stm32wbxx_ll_system.h"
  18. #include "stm32wbxx_ll_exti.h"
  19. #include "stm32wbxx_ll_cortex.h"
  20. #include "stm32wbxx_ll_utils.h"
  21. #include "stm32wbxx_ll_pwr.h"
  22. #include "stm32wbxx_ll_tim.h"
  23. #include "stm32wbxx_ll_gpio.h"
  24. #include "../scope_app_i.h"
  25. #define DIGITAL_SCALE_12BITS ((uint32_t)0xFFF)
  26. #define ADC_CONVERTED_DATA_BUFFER_SIZE ((uint32_t)128)
  27. #define VAR_CONVERTED_DATA_INIT_VALUE (DIGITAL_SCALE_12BITS + 1)
  28. #define VAR_CONVERTED_DATA_INIT_VALUE_16BITS (0xFFFF + 1U)
  29. #define __ADC_CALC_DATA_VOLTAGE(__VREFANALOG_VOLTAGE__, __ADC_DATA__) \
  30. ((__ADC_DATA__) * (__VREFANALOG_VOLTAGE__) / DIGITAL_SCALE_12BITS)
  31. #define VDDA_APPLI ((uint32_t)2500)
  32. #define TIMER_FREQUENCY_RANGE_MIN (1UL)
  33. #define TIMER_PRESCALER_MAX_VALUE (0xFFFFFFFF - 1UL)
  34. #define ADC_DELAY_CALIB_ENABLE_CPU_CYCLES (LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES * 32)
  35. // ramVector found from - https://community.nxp.com/t5/i-MX-Processors/Relocate-vector-table-to-ITCM/m-p/1302304
  36. // the aligned aspect is key!
  37. #define TABLE_SIZE 79
  38. uint32_t ramVector[TABLE_SIZE + 1] __attribute__((aligned(512)));
  39. const uint32_t AHBPrescTable[16UL] =
  40. {1UL, 3UL, 5UL, 1UL, 1UL, 6UL, 10UL, 32UL, 2UL, 4UL, 8UL, 16UL, 64UL, 128UL, 256UL, 512UL};
  41. const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
  42. const uint32_t MSIRangeTable[16UL] = {
  43. 100000UL,
  44. 200000UL,
  45. 400000UL,
  46. 800000UL,
  47. 1000000UL,
  48. 2000000UL,
  49. 4000000UL,
  50. 8000000UL,
  51. 16000000UL,
  52. 24000000UL,
  53. 32000000UL,
  54. 48000000UL,
  55. 0UL,
  56. 0UL,
  57. 0UL,
  58. 0UL}; /* 0UL values are incorrect cases */
  59. char* time; // Current time period text
  60. double freq; // Current samplerate
  61. uint8_t pause = 0; // Whether we want to pause output or not
  62. enum measureenum type; // Type of measurement we are performing
  63. int toggle = 0; // Used for toggling output GPIO, only used in testing
  64. void Error_Handler() {
  65. while(1) {
  66. }
  67. }
  68. __IO uint16_t
  69. aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE]; // Array that ADC data is copied to, via DMA
  70. __IO uint16_t aADCxConvertedData_Voltage_mVoltA
  71. [ADC_CONVERTED_DATA_BUFFER_SIZE]; // Data is converted to range from 0 to 2500
  72. __IO uint16_t aADCxConvertedData_Voltage_mVoltB
  73. [ADC_CONVERTED_DATA_BUFFER_SIZE]; // Data is converted to range from 0 to 2500
  74. __IO uint8_t ubDmaTransferStatus = 2; // DMA transfer status
  75. __IO uint16_t* mvoltWrite =
  76. &aADCxConvertedData_Voltage_mVoltA[0]; // Pointer to area we write converted voltage data to
  77. __IO uint16_t* mvoltDisplay =
  78. &aADCxConvertedData_Voltage_mVoltB[0]; // Pointer to area of memory we display
  79. void AdcDmaTransferComplete_Callback();
  80. void AdcDmaTransferHalf_Callback();
  81. void AdcGrpRegularOverrunError_Callback(void) {
  82. LL_ADC_DisableIT_OVR(ADC1);
  83. }
  84. void AdcDmaTransferError_Callback() {
  85. }
  86. void DMA1_Channel1_IRQHandler(void) {
  87. if(LL_DMA_IsActiveFlag_TC1(DMA1) == 1) {
  88. LL_DMA_ClearFlag_TC1(DMA1);
  89. AdcDmaTransferComplete_Callback();
  90. }
  91. if(LL_DMA_IsActiveFlag_HT1(DMA1) == 1) {
  92. LL_DMA_ClearFlag_HT1(DMA1);
  93. AdcDmaTransferHalf_Callback();
  94. }
  95. if(LL_DMA_IsActiveFlag_TE1(DMA1) == 1) {
  96. LL_DMA_ClearFlag_TE1(DMA1);
  97. AdcDmaTransferError_Callback();
  98. }
  99. }
  100. void ADC1_IRQHandler(void) {
  101. if(LL_ADC_IsActiveFlag_OVR(ADC1) != 0) {
  102. LL_ADC_ClearFlag_OVR(ADC1);
  103. AdcGrpRegularOverrunError_Callback();
  104. }
  105. }
  106. void TIM2_IRQHandler(void) {
  107. }
  108. static void MX_ADC1_Init(void) {
  109. LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
  110. LL_ADC_InitTypeDef ADC_InitStruct = {0};
  111. LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
  112. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  113. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC);
  114. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
  115. GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  116. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  117. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  118. LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  119. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_ADC1);
  120. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
  121. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
  122. LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
  123. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
  124. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
  125. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
  126. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
  127. LL_DMAMUX_SetRequestID(DMAMUX1, LL_DMAMUX_CHANNEL_0, LL_DMAMUX_REQ_ADC1);
  128. LL_DMA_ConfigAddresses(
  129. DMA1,
  130. LL_DMA_CHANNEL_1,
  131. LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA),
  132. (uint32_t)&aADCxConvertedData,
  133. LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
  134. LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, ADC_CONVERTED_DATA_BUFFER_SIZE);
  135. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  136. LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_1);
  137. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
  138. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  139. NVIC_SetPriority(ADC1_IRQn, 0);
  140. NVIC_EnableIRQ(ADC1_IRQn);
  141. ADC_CommonInitStruct.CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
  142. LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
  143. ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
  144. ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
  145. ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE;
  146. LL_ADC_Init(ADC1, &ADC_InitStruct);
  147. ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_EXT_TIM2_TRGO;
  148. ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
  149. ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
  150. ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
  151. ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED;
  152. ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_OVERWRITTEN;
  153. LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
  154. LL_ADC_SetOverSamplingScope(ADC1, LL_ADC_OVS_DISABLE);
  155. LL_ADC_REG_SetTriggerEdge(ADC1, LL_ADC_REG_TRIG_EXT_FALLING);
  156. LL_ADC_DisableDeepPowerDown(ADC1);
  157. LL_ADC_EnableInternalRegulator(ADC1);
  158. uint32_t wait_loop_index;
  159. wait_loop_index =
  160. ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
  161. while(wait_loop_index != 0) {
  162. wait_loop_index--;
  163. }
  164. LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_1);
  165. LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SAMPLINGTIME_247CYCLES_5);
  166. LL_ADC_SetChannelSingleDiff(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SINGLE_ENDED);
  167. LL_ADC_EnableIT_OVR(ADC1);
  168. }
  169. static void MX_TIM2_Init(int freq) {
  170. uint32_t timer_clock_frequency = 0; /* Timer clock frequency */
  171. uint32_t timer_prescaler =
  172. 0; /* Time base prescaler to have timebase aligned on minimum frequency possible */
  173. uint32_t timer_reload =
  174. 0; /* Timer reload value in function of timer prescaler to achieve time base period */
  175. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  176. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  177. if(LL_RCC_GetAPB1Prescaler() == LL_RCC_APB1_DIV_1) {
  178. timer_clock_frequency =
  179. __LL_RCC_CALC_PCLK1_FREQ(SystemCoreClock, LL_RCC_GetAPB1Prescaler());
  180. } else {
  181. timer_clock_frequency =
  182. (__LL_RCC_CALC_PCLK1_FREQ(SystemCoreClock, LL_RCC_GetAPB1Prescaler()) * 2);
  183. }
  184. timer_prescaler =
  185. ((timer_clock_frequency / (TIMER_PRESCALER_MAX_VALUE * TIMER_FREQUENCY_RANGE_MIN)) + 1);
  186. timer_reload = (timer_clock_frequency / (timer_prescaler * freq));
  187. TIM_InitStruct.Prescaler = (timer_prescaler - 1);
  188. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  189. TIM_InitStruct.Autoreload = (timer_reload - 1);
  190. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  191. LL_TIM_Init(TIM2, &TIM_InitStruct);
  192. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_ITR0);
  193. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_DISABLED);
  194. LL_TIM_DisableIT_TRIG(TIM2);
  195. LL_TIM_DisableDMAReq_TRIG(TIM2);
  196. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE);
  197. LL_TIM_DisableMasterSlaveMode(TIM2);
  198. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE);
  199. LL_TIM_EnableCounter(TIM2);
  200. }
  201. static void MX_DMA_Init(void) {
  202. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
  203. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
  204. NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 0));
  205. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  206. }
  207. static void MX_GPIO_Init(void) {
  208. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  209. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  210. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
  211. }
  212. // Swap pointer addresses, used for double buffer
  213. void swap(__IO uint16_t** a, __IO uint16_t** b) {
  214. __IO uint16_t* tmp;
  215. tmp = *a;
  216. *a = *b;
  217. *b = tmp;
  218. }
  219. void AdcDmaTransferComplete_Callback() {
  220. uint32_t tmp_index = 0;
  221. for(tmp_index = (ADC_CONVERTED_DATA_BUFFER_SIZE / 2);
  222. tmp_index < ADC_CONVERTED_DATA_BUFFER_SIZE;
  223. tmp_index++) {
  224. mvoltWrite[tmp_index] = __LL_ADC_CALC_DATA_TO_VOLTAGE(
  225. VDDA_APPLI, aADCxConvertedData[tmp_index], LL_ADC_RESOLUTION_12B);
  226. }
  227. ubDmaTransferStatus = 1;
  228. if(!pause) swap(&mvoltWrite, &mvoltDisplay);
  229. }
  230. void AdcDmaTransferHalf_Callback() {
  231. uint32_t tmp_index = 0;
  232. for(tmp_index = 0; tmp_index < (ADC_CONVERTED_DATA_BUFFER_SIZE / 2); tmp_index++) {
  233. mvoltWrite[tmp_index] = __LL_ADC_CALC_DATA_TO_VOLTAGE(
  234. VDDA_APPLI, aADCxConvertedData[tmp_index], LL_ADC_RESOLUTION_12B);
  235. }
  236. ubDmaTransferStatus = 0;
  237. }
  238. void Activate_ADC(void) {
  239. __IO uint32_t wait_loop_index = 0U;
  240. #if(USE_TIMEOUT == 1)
  241. uint32_t Timeout = 0U; /* Variable used for timeout management */
  242. #endif /* USE_TIMEOUT */
  243. if(LL_ADC_IsEnabled(ADC1) == 0) {
  244. LL_ADC_DisableDeepPowerDown(ADC1);
  245. LL_ADC_EnableInternalRegulator(ADC1);
  246. wait_loop_index =
  247. ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
  248. while(wait_loop_index != 0) {
  249. wait_loop_index--;
  250. }
  251. LL_ADC_StartCalibration(ADC1, LL_ADC_SINGLE_ENDED);
  252. #if(USE_TIMEOUT == 1)
  253. Timeout = ADC_CALIBRATION_TIMEOUT_MS;
  254. #endif /* USE_TIMEOUT */
  255. while(LL_ADC_IsCalibrationOnGoing(ADC1) != 0) {
  256. #if(USE_TIMEOUT == 1)
  257. if(LL_SYSTICK_IsActiveCounterFlag()) {
  258. if(Timeout-- == 0) {
  259. }
  260. }
  261. #endif /* USE_TIMEOUT */
  262. }
  263. wait_loop_index = (ADC_DELAY_CALIB_ENABLE_CPU_CYCLES >> 1);
  264. while(wait_loop_index != 0) {
  265. wait_loop_index--;
  266. }
  267. LL_ADC_Enable(ADC1);
  268. #if(USE_TIMEOUT == 1)
  269. Timeout = ADC_ENABLE_TIMEOUT_MS;
  270. #endif /* USE_TIMEOUT */
  271. while(LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0) {
  272. #if(USE_TIMEOUT == 1)
  273. /* Check Systick counter flag to decrement the time-out value */
  274. if(LL_SYSTICK_IsActiveCounterFlag()) {
  275. if(Timeout-- == 0) {
  276. /* Time-out occurred. Set LED to blinking mode */
  277. LED_Blinking(LED_BLINK_ERROR);
  278. }
  279. }
  280. #endif /* USE_TIMEOUT */
  281. }
  282. }
  283. }
  284. // Used to draw to display
  285. static void app_draw_callback(Canvas* canvas, void* ctx) {
  286. UNUSED(ctx);
  287. static int16_t index[ADC_CONVERTED_DATA_BUFFER_SIZE];
  288. static float data[ADC_CONVERTED_DATA_BUFFER_SIZE];
  289. static float crossings[ADC_CONVERTED_DATA_BUFFER_SIZE];
  290. static char buf1[50];
  291. float max = 0.0;
  292. float min = FLT_MAX;
  293. int count = 0;
  294. // Calculate voltage measurements
  295. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  296. if(mvoltDisplay[x] < min) min = mvoltDisplay[x];
  297. if(mvoltDisplay[x] > max) max = mvoltDisplay[x];
  298. }
  299. max /= 1000;
  300. min /= 1000;
  301. switch(type) {
  302. case m_time: {
  303. // Display current time period
  304. snprintf(buf1, 50, "Time: %s", time);
  305. canvas_draw_str(canvas, 10, 10, buf1);
  306. // Shift waveform across a virtual 0 line, so it crosses 0
  307. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  308. index[x] = -1;
  309. crossings[x] = -1.0;
  310. data[x] = ((float)mvoltDisplay[x] / 1000) - min;
  311. data[x] = ((2 / (max - min)) * data[x]) - 1;
  312. }
  313. // Find points at which waveform crosses virtual 0 line
  314. for(uint32_t x = 1; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  315. if(data[x] >= 0 && data[x - 1] < 0) {
  316. index[count++] = x - 1;
  317. }
  318. }
  319. count = 0;
  320. // Linear interpolation to find zero crossings
  321. // see https://gist.github.com/endolith/255291 for Python version
  322. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  323. if(index[x] == -1) break;
  324. crossings[count++] =
  325. (float)index[x] - data[index[x]] / (data[index[x] + 1] - data[index[x]]);
  326. }
  327. float avg = 0.0;
  328. float countv = 0.0;
  329. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  330. if(x + 1 >= ADC_CONVERTED_DATA_BUFFER_SIZE) break;
  331. if(crossings[x] == -1 || crossings[x + 1] == -1) break;
  332. avg += crossings[x + 1] - crossings[x];
  333. countv += 1;
  334. }
  335. avg /= countv;
  336. // Display frequency of waveform
  337. snprintf(buf1, 50, "Freq: %.1f Hz", (double)((float)freq / avg));
  338. canvas_draw_str(canvas, 10, 20, buf1);
  339. } break;
  340. case m_voltage: {
  341. // Display max, min, peak-to-peak voltages
  342. snprintf(buf1, 50, "Max: %.2fV", (double)max);
  343. canvas_draw_str(canvas, 10, 10, buf1);
  344. snprintf(buf1, 50, "Min: %.2fV", (double)min);
  345. canvas_draw_str(canvas, 10, 20, buf1);
  346. snprintf(buf1, 50, "Vpp: %.2fV", (double)(max - min));
  347. canvas_draw_str(canvas, 10, 30, buf1);
  348. } break;
  349. default:
  350. break;
  351. }
  352. // Draw lines between each data point
  353. for(uint32_t x = 1; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  354. uint32_t prev = 64 - (mvoltDisplay[x - 1] / (VDDA_APPLI / 64));
  355. uint32_t cur = 64 - (mvoltDisplay[x] / (VDDA_APPLI / 64));
  356. canvas_draw_line(canvas, x - 1, prev, x, cur);
  357. }
  358. // Draw graph lines
  359. canvas_draw_line(canvas, 0, 0, 0, 63);
  360. canvas_draw_line(canvas, 0, 63, 128, 63);
  361. }
  362. static void app_input_callback(InputEvent* input_event, void* ctx) {
  363. furi_assert(ctx);
  364. FuriMessageQueue* event_queue = ctx;
  365. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  366. }
  367. void scope_scene_run_widget_callback(GuiButtonType result, InputType type, void* context) {
  368. ScopeApp* app = context;
  369. if(type == InputTypeShort) {
  370. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  371. }
  372. }
  373. void scope_scene_run_on_enter(void* context) {
  374. ScopeApp* app = context;
  375. // Find string representation of time period we're using
  376. for(uint32_t i = 0; i < COUNT_OF(time_list); i++) {
  377. if(time_list[i].time == app->time) {
  378. time = time_list[i].str;
  379. break;
  380. }
  381. }
  382. // Currently un-paused
  383. pause = 0;
  384. // What type of measurement are we performing
  385. type = app->measurement;
  386. // Copy vector table, modify to use our own IRQ handlers
  387. __disable_irq();
  388. memcpy(ramVector, (uint32_t*)(FLASH_BASE | SCB->VTOR), sizeof(uint32_t) * TABLE_SIZE);
  389. SCB->VTOR = (uint32_t)ramVector;
  390. ramVector[27] = (uint32_t)DMA1_Channel1_IRQHandler;
  391. ramVector[34] = (uint32_t)ADC1_IRQHandler;
  392. ramVector[44] = (uint32_t)TIM2_IRQHandler;
  393. __enable_irq();
  394. furi_hal_bus_enable(FuriHalBusTIM2);
  395. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  396. uint32_t tmp_index_adc_converted_data = 0;
  397. MX_GPIO_Init();
  398. MX_DMA_Init();
  399. freq = 1 / app->time;
  400. MX_TIM2_Init((int)freq);
  401. // Set VREFBUF to 2.5V, as vref isn't connected to 3.3V itself in the flipper zero
  402. VREFBUF->CSR |= VREFBUF_CSR_ENVR;
  403. VREFBUF->CSR &= ~VREFBUF_CSR_HIZ;
  404. VREFBUF->CSR |= VREFBUF_CSR_VRS;
  405. while(!(VREFBUF->CSR & VREFBUF_CSR_VRR)) {
  406. };
  407. MX_ADC1_Init();
  408. // Setup initial values from ADC
  409. for(tmp_index_adc_converted_data = 0;
  410. tmp_index_adc_converted_data < ADC_CONVERTED_DATA_BUFFER_SIZE;
  411. tmp_index_adc_converted_data++) {
  412. aADCxConvertedData[tmp_index_adc_converted_data] = VAR_CONVERTED_DATA_INIT_VALUE;
  413. aADCxConvertedData_Voltage_mVoltA[tmp_index_adc_converted_data] = 0;
  414. aADCxConvertedData_Voltage_mVoltB[tmp_index_adc_converted_data] = 0;
  415. }
  416. Activate_ADC();
  417. if((LL_ADC_IsEnabled(ADC1) == 1) && (LL_ADC_IsDisableOngoing(ADC1) == 0) &&
  418. (LL_ADC_REG_IsConversionOngoing(ADC1) == 0)) {
  419. LL_ADC_REG_StartConversion(ADC1);
  420. }
  421. ViewPort* view_port = view_port_alloc();
  422. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  423. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  424. // Register view port in GUI
  425. Gui* gui = furi_record_open(RECORD_GUI);
  426. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  427. InputEvent event;
  428. bool running = true;
  429. while(running) {
  430. if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
  431. if((event.type == InputTypePress) || (event.type == InputTypeRepeat)) {
  432. switch(event.key) {
  433. case InputKeyLeft:
  434. break;
  435. case InputKeyRight:
  436. break;
  437. case InputKeyUp:
  438. break;
  439. case InputKeyDown:
  440. break;
  441. case InputKeyOk:
  442. pause ^= 1;
  443. break;
  444. default:
  445. running = false;
  446. break;
  447. }
  448. }
  449. }
  450. view_port_update(view_port);
  451. }
  452. furi_hal_bus_disable(FuriHalBusTIM2);
  453. // Stop DMA and switch back to original vector table
  454. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  455. __disable_irq();
  456. SCB->VTOR = 0;
  457. __enable_irq();
  458. view_port_enabled_set(view_port, false);
  459. gui_remove_view_port(gui, view_port);
  460. view_port_free(view_port);
  461. // Switch back to original scene
  462. furi_record_close(RECORD_GUI);
  463. scene_manager_previous_scene(app->scene_manager);
  464. submenu_set_selected_item(app->submenu, 0);
  465. }
  466. bool scope_scene_run_on_event(void* context, SceneManagerEvent event) {
  467. ScopeApp* app = context;
  468. bool consumed = false;
  469. UNUSED(app);
  470. UNUSED(event);
  471. return consumed;
  472. }
  473. void scope_scene_run_on_exit(void* context) {
  474. ScopeApp* app = context;
  475. // Clear views
  476. widget_reset(app->widget);
  477. }