scope_scene_run.c 20 KB

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