scope_scene_run.c 20 KB

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