scope_scene_run.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 (0xFFFF - 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. double abs_error(double num1, double num2) {
  170. return fabs((num1 - num2) / num1);
  171. }
  172. static void MX_TIM2_Init(int freq) {
  173. uint32_t timer_clock_frequency = 0; /* Timer clock frequency */
  174. uint32_t timer_prescaler =
  175. 0; /* Time base prescaler to have timebase aligned on minimum frequency possible */
  176. uint32_t timer_reload =
  177. 0; /* Timer reload value in function of timer prescaler to achieve time base period */
  178. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  179. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
  180. if(LL_RCC_GetAPB1Prescaler() == LL_RCC_APB1_DIV_1) {
  181. timer_clock_frequency =
  182. __LL_RCC_CALC_PCLK1_FREQ(SystemCoreClock, LL_RCC_GetAPB1Prescaler());
  183. } else {
  184. timer_clock_frequency =
  185. (__LL_RCC_CALC_PCLK1_FREQ(SystemCoreClock, LL_RCC_GetAPB1Prescaler()) * 2);
  186. }
  187. //(PSC+1) * (ARR+1)
  188. double calc = timer_clock_frequency / (1 / (1 / (double)freq));
  189. double PSC;
  190. double ARR;
  191. double minerr = 10000;
  192. for(int i = 1; i < 65536; i++) {
  193. PSC = i - 1;
  194. ARR = calc / (PSC + 1);
  195. double error = abs_error((int)(ARR), ARR);
  196. if(error < (double)0.001 && error < minerr && ARR - 1 > 0) {
  197. timer_prescaler = PSC;
  198. timer_reload = ARR - 1;
  199. minerr = error;
  200. break;
  201. }
  202. }
  203. TIM_InitStruct.Prescaler = timer_prescaler;
  204. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  205. TIM_InitStruct.Autoreload = timer_reload;
  206. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  207. LL_TIM_Init(TIM2, &TIM_InitStruct);
  208. LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_ITR0);
  209. LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_DISABLED);
  210. LL_TIM_DisableIT_TRIG(TIM2);
  211. LL_TIM_DisableDMAReq_TRIG(TIM2);
  212. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE);
  213. LL_TIM_DisableMasterSlaveMode(TIM2);
  214. LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE);
  215. LL_TIM_EnableCounter(TIM2);
  216. }
  217. static void MX_DMA_Init(void) {
  218. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
  219. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
  220. NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 0));
  221. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  222. }
  223. static void MX_GPIO_Init(void) {
  224. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  225. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  226. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
  227. }
  228. // Swap pointer addresses, used for double buffer
  229. void swap(__IO uint16_t** a, __IO uint16_t** b) {
  230. __IO uint16_t* tmp;
  231. tmp = *a;
  232. *a = *b;
  233. *b = tmp;
  234. }
  235. void AdcDmaTransferComplete_Callback() {
  236. uint32_t tmp_index = 0;
  237. for(tmp_index = (ADC_CONVERTED_DATA_BUFFER_SIZE / 2);
  238. tmp_index < ADC_CONVERTED_DATA_BUFFER_SIZE;
  239. tmp_index++) {
  240. mvoltWrite[tmp_index] = __LL_ADC_CALC_DATA_TO_VOLTAGE(
  241. VDDA_APPLI, aADCxConvertedData[tmp_index], LL_ADC_RESOLUTION_12B);
  242. }
  243. ubDmaTransferStatus = 1;
  244. if(!pause) swap(&mvoltWrite, &mvoltDisplay);
  245. }
  246. void AdcDmaTransferHalf_Callback() {
  247. uint32_t tmp_index = 0;
  248. for(tmp_index = 0; tmp_index < (ADC_CONVERTED_DATA_BUFFER_SIZE / 2); tmp_index++) {
  249. mvoltWrite[tmp_index] = __LL_ADC_CALC_DATA_TO_VOLTAGE(
  250. VDDA_APPLI, aADCxConvertedData[tmp_index], LL_ADC_RESOLUTION_12B);
  251. }
  252. ubDmaTransferStatus = 0;
  253. }
  254. void Activate_ADC(void) {
  255. __IO uint32_t wait_loop_index = 0U;
  256. #if(USE_TIMEOUT == 1)
  257. uint32_t Timeout = 0U; /* Variable used for timeout management */
  258. #endif /* USE_TIMEOUT */
  259. if(LL_ADC_IsEnabled(ADC1) == 0) {
  260. LL_ADC_DisableDeepPowerDown(ADC1);
  261. LL_ADC_EnableInternalRegulator(ADC1);
  262. wait_loop_index =
  263. ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (100000 * 2))) / 10);
  264. while(wait_loop_index != 0) {
  265. wait_loop_index--;
  266. }
  267. LL_ADC_StartCalibration(ADC1, LL_ADC_SINGLE_ENDED);
  268. #if(USE_TIMEOUT == 1)
  269. Timeout = ADC_CALIBRATION_TIMEOUT_MS;
  270. #endif /* USE_TIMEOUT */
  271. while(LL_ADC_IsCalibrationOnGoing(ADC1) != 0) {
  272. #if(USE_TIMEOUT == 1)
  273. if(LL_SYSTICK_IsActiveCounterFlag()) {
  274. if(Timeout-- == 0) {
  275. }
  276. }
  277. #endif /* USE_TIMEOUT */
  278. }
  279. wait_loop_index = (ADC_DELAY_CALIB_ENABLE_CPU_CYCLES >> 1);
  280. while(wait_loop_index != 0) {
  281. wait_loop_index--;
  282. }
  283. LL_ADC_Enable(ADC1);
  284. #if(USE_TIMEOUT == 1)
  285. Timeout = ADC_ENABLE_TIMEOUT_MS;
  286. #endif /* USE_TIMEOUT */
  287. while(LL_ADC_IsActiveFlag_ADRDY(ADC1) == 0) {
  288. #if(USE_TIMEOUT == 1)
  289. /* Check Systick counter flag to decrement the time-out value */
  290. if(LL_SYSTICK_IsActiveCounterFlag()) {
  291. if(Timeout-- == 0) {
  292. /* Time-out occurred. Set LED to blinking mode */
  293. LED_Blinking(LED_BLINK_ERROR);
  294. }
  295. }
  296. #endif /* USE_TIMEOUT */
  297. }
  298. }
  299. }
  300. // Used to draw to display
  301. static void app_draw_callback(Canvas* canvas, void* ctx) {
  302. UNUSED(ctx);
  303. static int16_t index[ADC_CONVERTED_DATA_BUFFER_SIZE];
  304. static float data[ADC_CONVERTED_DATA_BUFFER_SIZE];
  305. static float crossings[ADC_CONVERTED_DATA_BUFFER_SIZE];
  306. static char buf1[50];
  307. float max = 0.0;
  308. float min = FLT_MAX;
  309. int count = 0;
  310. // Calculate voltage measurements
  311. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  312. if(mvoltDisplay[x] < min) min = mvoltDisplay[x];
  313. if(mvoltDisplay[x] > max) max = mvoltDisplay[x];
  314. }
  315. max /= 1000;
  316. min /= 1000;
  317. switch(type) {
  318. case m_time: {
  319. // Display current time period
  320. snprintf(buf1, 50, "Time: %s", time);
  321. canvas_draw_str(canvas, 10, 10, buf1);
  322. // Shift waveform across a virtual 0 line, so it crosses 0
  323. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  324. index[x] = -1;
  325. crossings[x] = -1.0;
  326. data[x] = ((float)mvoltDisplay[x] / 1000) - min;
  327. data[x] = ((2 / (max - min)) * data[x]) - 1;
  328. }
  329. // Find points at which waveform crosses virtual 0 line
  330. for(uint32_t x = 1; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  331. if(data[x] >= 0 && data[x - 1] < 0) {
  332. index[count++] = x - 1;
  333. }
  334. }
  335. count = 0;
  336. // Linear interpolation to find zero crossings
  337. // see https://gist.github.com/endolith/255291 for Python version
  338. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  339. if(index[x] == -1) break;
  340. crossings[count++] =
  341. (float)index[x] - data[index[x]] / (data[index[x] + 1] - data[index[x]]);
  342. }
  343. float avg = 0.0;
  344. float countv = 0.0;
  345. for(uint32_t x = 0; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  346. if(x + 1 >= ADC_CONVERTED_DATA_BUFFER_SIZE) break;
  347. if(crossings[x] == -1 || crossings[x + 1] == -1) break;
  348. avg += crossings[x + 1] - crossings[x];
  349. countv += 1;
  350. }
  351. avg /= countv;
  352. // Display frequency of waveform
  353. snprintf(buf1, 50, "Freq: %.1f Hz", (double)((float)freq / avg));
  354. canvas_draw_str(canvas, 10, 20, buf1);
  355. } break;
  356. case m_voltage: {
  357. // Display max, min, peak-to-peak voltages
  358. snprintf(buf1, 50, "Max: %.2fV", (double)max);
  359. canvas_draw_str(canvas, 10, 10, buf1);
  360. snprintf(buf1, 50, "Min: %.2fV", (double)min);
  361. canvas_draw_str(canvas, 10, 20, buf1);
  362. snprintf(buf1, 50, "Vpp: %.2fV", (double)(max - min));
  363. canvas_draw_str(canvas, 10, 30, buf1);
  364. } break;
  365. default:
  366. break;
  367. }
  368. // Draw lines between each data point
  369. for(uint32_t x = 1; x < ADC_CONVERTED_DATA_BUFFER_SIZE; x++) {
  370. uint32_t prev = 64 - (mvoltDisplay[x - 1] / (VDDA_APPLI / 64));
  371. uint32_t cur = 64 - (mvoltDisplay[x] / (VDDA_APPLI / 64));
  372. canvas_draw_line(canvas, x - 1, prev, x, cur);
  373. }
  374. // Draw graph lines
  375. canvas_draw_line(canvas, 0, 0, 0, 63);
  376. canvas_draw_line(canvas, 0, 63, 128, 63);
  377. }
  378. static void app_input_callback(InputEvent* input_event, void* ctx) {
  379. furi_assert(ctx);
  380. FuriMessageQueue* event_queue = ctx;
  381. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  382. }
  383. void scope_scene_run_widget_callback(GuiButtonType result, InputType type, void* context) {
  384. ScopeApp* app = context;
  385. if(type == InputTypeShort) {
  386. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  387. }
  388. }
  389. void scope_scene_run_on_enter(void* context) {
  390. ScopeApp* app = context;
  391. // Find string representation of time period we're using
  392. for(uint32_t i = 0; i < COUNT_OF(time_list); i++) {
  393. if(time_list[i].time == app->time) {
  394. time = time_list[i].str;
  395. break;
  396. }
  397. }
  398. // Currently un-paused
  399. pause = 0;
  400. // What type of measurement are we performing
  401. type = app->measurement;
  402. // Copy vector table, modify to use our own IRQ handlers
  403. __disable_irq();
  404. memcpy(ramVector, (uint32_t*)(FLASH_BASE | SCB->VTOR), sizeof(uint32_t) * TABLE_SIZE);
  405. SCB->VTOR = (uint32_t)ramVector;
  406. ramVector[27] = (uint32_t)DMA1_Channel1_IRQHandler;
  407. ramVector[34] = (uint32_t)ADC1_IRQHandler;
  408. ramVector[44] = (uint32_t)TIM2_IRQHandler;
  409. __enable_irq();
  410. furi_hal_bus_enable(FuriHalBusTIM2);
  411. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  412. uint32_t tmp_index_adc_converted_data = 0;
  413. MX_GPIO_Init();
  414. MX_DMA_Init();
  415. freq = 1 / app->time;
  416. MX_TIM2_Init((int)freq);
  417. // Set VREFBUF to 2.5V, as vref isn't connected to 3.3V itself in the flipper zero
  418. VREFBUF->CSR |= VREFBUF_CSR_ENVR;
  419. VREFBUF->CSR &= ~VREFBUF_CSR_HIZ;
  420. VREFBUF->CSR |= VREFBUF_CSR_VRS;
  421. while(!(VREFBUF->CSR & VREFBUF_CSR_VRR)) {
  422. };
  423. MX_ADC1_Init();
  424. // Setup initial values from ADC
  425. for(tmp_index_adc_converted_data = 0;
  426. tmp_index_adc_converted_data < ADC_CONVERTED_DATA_BUFFER_SIZE;
  427. tmp_index_adc_converted_data++) {
  428. aADCxConvertedData[tmp_index_adc_converted_data] = VAR_CONVERTED_DATA_INIT_VALUE;
  429. aADCxConvertedData_Voltage_mVoltA[tmp_index_adc_converted_data] = 0;
  430. aADCxConvertedData_Voltage_mVoltB[tmp_index_adc_converted_data] = 0;
  431. }
  432. Activate_ADC();
  433. if((LL_ADC_IsEnabled(ADC1) == 1) && (LL_ADC_IsDisableOngoing(ADC1) == 0) &&
  434. (LL_ADC_REG_IsConversionOngoing(ADC1) == 0)) {
  435. LL_ADC_REG_StartConversion(ADC1);
  436. }
  437. ViewPort* view_port = view_port_alloc();
  438. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  439. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  440. // Register view port in GUI
  441. Gui* gui = furi_record_open(RECORD_GUI);
  442. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  443. InputEvent event;
  444. bool running = true;
  445. while(running) {
  446. if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
  447. if((event.type == InputTypePress) || (event.type == InputTypeRepeat)) {
  448. switch(event.key) {
  449. case InputKeyLeft:
  450. break;
  451. case InputKeyRight:
  452. break;
  453. case InputKeyUp:
  454. break;
  455. case InputKeyDown:
  456. break;
  457. case InputKeyOk:
  458. pause ^= 1;
  459. break;
  460. default:
  461. running = false;
  462. break;
  463. }
  464. }
  465. }
  466. view_port_update(view_port);
  467. }
  468. furi_hal_bus_disable(FuriHalBusTIM2);
  469. // Stop DMA and switch back to original vector table
  470. LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
  471. __disable_irq();
  472. SCB->VTOR = 0;
  473. __enable_irq();
  474. view_port_enabled_set(view_port, false);
  475. gui_remove_view_port(gui, view_port);
  476. view_port_free(view_port);
  477. // Switch back to original scene
  478. furi_record_close(RECORD_GUI);
  479. scene_manager_previous_scene(app->scene_manager);
  480. submenu_set_selected_item(app->submenu, 0);
  481. }
  482. bool scope_scene_run_on_event(void* context, SceneManagerEvent event) {
  483. ScopeApp* app = context;
  484. bool consumed = false;
  485. UNUSED(app);
  486. UNUSED(event);
  487. return consumed;
  488. }
  489. void scope_scene_run_on_exit(void* context) {
  490. ScopeApp* app = context;
  491. // Clear views
  492. widget_reset(app->widget);
  493. }