adc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /**
  2. ******************************************************************************
  3. * File Name : ADC.c
  4. * Description : This file provides code for the configuration
  5. * of the ADC instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "adc.h"
  21. /* USER CODE BEGIN 0 */
  22. uint32_t ADC1_Buffer[ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT];
  23. uint32_t ADC2_Buffer[ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT];
  24. uint16_t ADC2_Buffer_Each[ADC2_CHANEL_COUNT][ADC2_SAMPLE_COUNT];
  25. uint32_t ADC3_Buffer[ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT];
  26. uint16_t ADC3_Buffer_Each[ADC3_CHANEL_COUNT][ADC3_SAMPLE_COUNT];
  27. uint8_t isDMAEnd_ADC1;
  28. uint8_t isDMAEnd_ADC2;
  29. uint8_t isDMAEnd_ADC3;
  30. struct ADC_VALUE adc_value;
  31. uint16_t ADCWDGHighThreshold = 3200;
  32. uint16_t ADCWDGLowThreshold = 820;
  33. uint8_t is60Hz;
  34. /* USER CODE END 0 */
  35. ADC_HandleTypeDef hadc1;
  36. ADC_HandleTypeDef hadc2;
  37. ADC_HandleTypeDef hadc3;
  38. DMA_HandleTypeDef hdma_adc1;
  39. DMA_HandleTypeDef hdma_adc2;
  40. DMA_HandleTypeDef hdma_adc3;
  41. /* ADC1 init function */
  42. void MX_ADC1_Init(void)
  43. {
  44. ADC_ChannelConfTypeDef sConfig = {0};
  45. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  46. */
  47. hadc1.Instance = ADC1;
  48. #ifdef MODIFY_ADC_INIT_CLOCK_PRESCALER_SAME_WITH_AW
  49. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
  50. #else
  51. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  52. #endif
  53. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  54. hadc1.Init.ScanConvMode = ENABLE;
  55. hadc1.Init.ContinuousConvMode = ENABLE;
  56. hadc1.Init.DiscontinuousConvMode = DISABLE;
  57. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  58. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  59. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  60. hadc1.Init.NbrOfConversion = 1;
  61. hadc1.Init.DMAContinuousRequests = ENABLE;
  62. hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  63. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  64. {
  65. Error_Handler();
  66. }
  67. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  68. */
  69. sConfig.Channel = ADC_CHANNEL_4;
  70. sConfig.Rank = 1;
  71. sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
  72. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  73. {
  74. Error_Handler();
  75. }
  76. }
  77. /* ADC2 init function */
  78. void MX_ADC2_Init(void)
  79. {
  80. ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
  81. ADC_ChannelConfTypeDef sConfig = {0};
  82. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  83. */
  84. hadc2.Instance = ADC2;
  85. #ifdef MODIFY_ADC_INIT_CLOCK_PRESCALER_SAME_WITH_AW
  86. hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
  87. #else
  88. hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  89. #endif
  90. hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  91. hadc2.Init.ScanConvMode = ENABLE;
  92. hadc2.Init.ContinuousConvMode = ENABLE;
  93. hadc2.Init.DiscontinuousConvMode = DISABLE;
  94. hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  95. hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  96. hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  97. hadc2.Init.NbrOfConversion = 2;
  98. hadc2.Init.DMAContinuousRequests = ENABLE;
  99. hadc2.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  100. if (HAL_ADC_Init(&hadc2) != HAL_OK)
  101. {
  102. Error_Handler();
  103. }
  104. /** Configure the analog watchdog
  105. */
  106. AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
  107. AnalogWDGConfig.HighThreshold = 3722;
  108. AnalogWDGConfig.LowThreshold = 372;
  109. AnalogWDGConfig.Channel = ADC_CHANNEL_6;
  110. //AnalogWDGConfig.ITMode = DISABLE;
  111. #ifdef MODIFY_ADC_INIT_ANALOG_WDG_ITMODE_SAME_WITH_AW
  112. AnalogWDGConfig.ITMode = ENABLE;
  113. #else
  114. AnalogWDGConfig.ITMode = DISABLE;
  115. #endif
  116. if (HAL_ADC_AnalogWDGConfig(&hadc2, &AnalogWDGConfig) != HAL_OK)
  117. {
  118. Error_Handler();
  119. }
  120. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  121. */
  122. sConfig.Channel = ADC_CHANNEL_5;
  123. sConfig.Rank = 1;
  124. sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  125. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  126. {
  127. Error_Handler();
  128. }
  129. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  130. */
  131. sConfig.Channel = ADC_CHANNEL_6;
  132. sConfig.Rank = 2;
  133. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. }
  138. /* ADC3 init function */
  139. void MX_ADC3_Init(void)
  140. {
  141. ADC_ChannelConfTypeDef sConfig = {0};
  142. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  143. */
  144. hadc3.Instance = ADC3;
  145. #ifdef MODIFY_ADC_INIT_CLOCK_PRESCALER_SAME_WITH_AW
  146. hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
  147. #else
  148. hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  149. #endif
  150. hadc3.Init.Resolution = ADC_RESOLUTION_12B;
  151. hadc3.Init.ScanConvMode = ENABLE;
  152. hadc3.Init.ContinuousConvMode = ENABLE;
  153. hadc3.Init.DiscontinuousConvMode = DISABLE;
  154. hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  155. hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  156. hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  157. hadc3.Init.NbrOfConversion = 10;
  158. hadc3.Init.DMAContinuousRequests = ENABLE;
  159. hadc3.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  160. if (HAL_ADC_Init(&hadc3) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  165. */
  166. sConfig.Channel = ADC_CHANNEL_9;
  167. sConfig.Rank = 1;
  168. sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  169. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  174. */
  175. sConfig.Channel = ADC_CHANNEL_14;
  176. sConfig.Rank = 2;
  177. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  178. {
  179. Error_Handler();
  180. }
  181. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  182. */
  183. sConfig.Channel = ADC_CHANNEL_15;
  184. sConfig.Rank = 3;
  185. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  186. {
  187. Error_Handler();
  188. }
  189. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  190. */
  191. sConfig.Channel = ADC_CHANNEL_4;
  192. sConfig.Rank = 4;
  193. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  194. {
  195. Error_Handler();
  196. }
  197. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  198. */
  199. sConfig.Channel = ADC_CHANNEL_7;
  200. sConfig.Rank = 5;
  201. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  206. */
  207. sConfig.Channel = ADC_CHANNEL_5;
  208. sConfig.Rank = 6;
  209. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  210. {
  211. Error_Handler();
  212. }
  213. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  214. */
  215. sConfig.Channel = ADC_CHANNEL_6;
  216. sConfig.Rank = 7;
  217. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  218. {
  219. Error_Handler();
  220. }
  221. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  222. */
  223. sConfig.Channel = ADC_CHANNEL_8;
  224. sConfig.Rank = 8;
  225. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  226. {
  227. Error_Handler();
  228. }
  229. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  230. */
  231. sConfig.Channel = ADC_CHANNEL_10;
  232. sConfig.Rank = 9;
  233. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  234. {
  235. Error_Handler();
  236. }
  237. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  238. */
  239. sConfig.Channel = ADC_CHANNEL_11;
  240. sConfig.Rank = 10;
  241. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  242. {
  243. Error_Handler();
  244. }
  245. }
  246. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  247. {
  248. GPIO_InitTypeDef GPIO_InitStruct = {0};
  249. if(adcHandle->Instance==ADC1)
  250. {
  251. /* USER CODE BEGIN ADC1_MspInit 0 */
  252. /* USER CODE END ADC1_MspInit 0 */
  253. /* ADC1 clock enable */
  254. __HAL_RCC_ADC1_CLK_ENABLE();
  255. __HAL_RCC_GPIOA_CLK_ENABLE();
  256. /**ADC1 GPIO Configuration
  257. PA4 ------> ADC1_IN4
  258. */
  259. GPIO_InitStruct.Pin = ADC1_IN4_CP_Pin;
  260. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  261. GPIO_InitStruct.Pull = GPIO_NOPULL;
  262. HAL_GPIO_Init(ADC1_IN4_CP_GPIO_Port, &GPIO_InitStruct);
  263. /* ADC1 DMA Init */
  264. /* ADC1 Init */
  265. hdma_adc1.Instance = DMA2_Stream0;
  266. hdma_adc1.Init.Channel = DMA_CHANNEL_0;
  267. hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
  268. hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
  269. hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
  270. hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  271. hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  272. hdma_adc1.Init.Mode = DMA_CIRCULAR;
  273. hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
  274. hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  275. if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
  276. {
  277. Error_Handler();
  278. }
  279. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
  280. /* ADC1 interrupt Init */
  281. HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
  282. HAL_NVIC_EnableIRQ(ADC_IRQn);
  283. /* USER CODE BEGIN ADC1_MspInit 1 */
  284. /* USER CODE END ADC1_MspInit 1 */
  285. }
  286. else if(adcHandle->Instance==ADC2)
  287. {
  288. /* USER CODE BEGIN ADC2_MspInit 0 */
  289. /* USER CODE END ADC2_MspInit 0 */
  290. /* ADC2 clock enable */
  291. __HAL_RCC_ADC2_CLK_ENABLE();
  292. __HAL_RCC_GPIOA_CLK_ENABLE();
  293. /**ADC2 GPIO Configuration
  294. PA5 ------> ADC2_IN5
  295. PA6 ------> ADC2_IN6
  296. */
  297. GPIO_InitStruct.Pin = ADC2_IN5_Welding_Pin|ADC2_IN6_GF_Pin;
  298. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  299. GPIO_InitStruct.Pull = GPIO_NOPULL;
  300. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  301. /* ADC2 DMA Init */
  302. /* ADC2 Init */
  303. hdma_adc2.Instance = DMA2_Stream3;
  304. hdma_adc2.Init.Channel = DMA_CHANNEL_1;
  305. hdma_adc2.Init.Direction = DMA_PERIPH_TO_MEMORY;
  306. hdma_adc2.Init.PeriphInc = DMA_PINC_DISABLE;
  307. hdma_adc2.Init.MemInc = DMA_MINC_ENABLE;
  308. hdma_adc2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  309. hdma_adc2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  310. hdma_adc2.Init.Mode = DMA_CIRCULAR;
  311. hdma_adc2.Init.Priority = DMA_PRIORITY_LOW;
  312. hdma_adc2.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  313. if (HAL_DMA_Init(&hdma_adc2) != HAL_OK)
  314. {
  315. Error_Handler();
  316. }
  317. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc2);
  318. /* ADC2 interrupt Init */
  319. HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
  320. HAL_NVIC_EnableIRQ(ADC_IRQn);
  321. /* USER CODE BEGIN ADC2_MspInit 1 */
  322. /* USER CODE END ADC2_MspInit 1 */
  323. }
  324. else if(adcHandle->Instance==ADC3)
  325. {
  326. /* USER CODE BEGIN ADC3_MspInit 0 */
  327. /* USER CODE END ADC3_MspInit 0 */
  328. /* ADC3 clock enable */
  329. __HAL_RCC_ADC3_CLK_ENABLE();
  330. __HAL_RCC_GPIOF_CLK_ENABLE();
  331. __HAL_RCC_GPIOC_CLK_ENABLE();
  332. /**ADC3 GPIO Configuration
  333. PF3 ------> ADC3_IN9
  334. PF4 ------> ADC3_IN14
  335. PF5 ------> ADC3_IN15
  336. PF6 ------> ADC3_IN4
  337. PF7 ------> ADC3_IN5
  338. PF8 ------> ADC3_IN6
  339. PF9 ------> ADC3_IN7
  340. PF10 ------> ADC3_IN8
  341. PC0 ------> ADC3_IN10
  342. PC1 ------> ADC3_IN11
  343. */
  344. GPIO_InitStruct.Pin = ADC3_IN9_Voltage_L1_Pin|ADC3_IN14_1998_Pin|ADC3_IN15_Temp_Pin|ADC3_IN4_GMI_VL1_Pin
  345. |ADC3_IN5_Current_L2_Pin|ADC3_IN6_PP_GUN_DET_Pin|ADC3_IN7_Current_L1_Pin|ADC3_IN8_Voltage_L2_Pin;
  346. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  347. GPIO_InitStruct.Pull = GPIO_NOPULL;
  348. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  349. GPIO_InitStruct.Pin = ADC3_IN10_Voltage_L3_Pin|ADC3_IN11_Current_L3_Pin;
  350. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  351. GPIO_InitStruct.Pull = GPIO_NOPULL;
  352. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  353. /* ADC3 DMA Init */
  354. /* ADC3 Init */
  355. hdma_adc3.Instance = DMA2_Stream1;
  356. hdma_adc3.Init.Channel = DMA_CHANNEL_2;
  357. hdma_adc3.Init.Direction = DMA_PERIPH_TO_MEMORY;
  358. hdma_adc3.Init.PeriphInc = DMA_PINC_DISABLE;
  359. hdma_adc3.Init.MemInc = DMA_MINC_ENABLE;
  360. hdma_adc3.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  361. hdma_adc3.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  362. hdma_adc3.Init.Mode = DMA_CIRCULAR;
  363. hdma_adc3.Init.Priority = DMA_PRIORITY_LOW;
  364. hdma_adc3.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  365. if (HAL_DMA_Init(&hdma_adc3) != HAL_OK)
  366. {
  367. Error_Handler();
  368. }
  369. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc3);
  370. /* ADC3 interrupt Init */
  371. HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
  372. HAL_NVIC_EnableIRQ(ADC_IRQn);
  373. /* USER CODE BEGIN ADC3_MspInit 1 */
  374. /* USER CODE END ADC3_MspInit 1 */
  375. }
  376. }
  377. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  378. {
  379. if(adcHandle->Instance==ADC1)
  380. {
  381. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  382. /* USER CODE END ADC1_MspDeInit 0 */
  383. /* Peripheral clock disable */
  384. __HAL_RCC_ADC1_CLK_DISABLE();
  385. /**ADC1 GPIO Configuration
  386. PA4 ------> ADC1_IN4
  387. */
  388. HAL_GPIO_DeInit(ADC1_IN4_CP_GPIO_Port, ADC1_IN4_CP_Pin);
  389. /* ADC1 DMA DeInit */
  390. HAL_DMA_DeInit(adcHandle->DMA_Handle);
  391. /* ADC1 interrupt Deinit */
  392. /* USER CODE BEGIN ADC1:ADC_IRQn disable */
  393. /**
  394. * Uncomment the line below to disable the "ADC_IRQn" interrupt
  395. * Be aware, disabling shared interrupt may affect other IPs
  396. */
  397. /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
  398. /* USER CODE END ADC1:ADC_IRQn disable */
  399. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  400. /* USER CODE END ADC1_MspDeInit 1 */
  401. }
  402. else if(adcHandle->Instance==ADC2)
  403. {
  404. /* USER CODE BEGIN ADC2_MspDeInit 0 */
  405. /* USER CODE END ADC2_MspDeInit 0 */
  406. /* Peripheral clock disable */
  407. __HAL_RCC_ADC2_CLK_DISABLE();
  408. /**ADC2 GPIO Configuration
  409. PA5 ------> ADC2_IN5
  410. PA6 ------> ADC2_IN6
  411. */
  412. HAL_GPIO_DeInit(GPIOA, ADC2_IN5_Welding_Pin|ADC2_IN6_GF_Pin);
  413. /* ADC2 DMA DeInit */
  414. HAL_DMA_DeInit(adcHandle->DMA_Handle);
  415. /* ADC2 interrupt Deinit */
  416. /* USER CODE BEGIN ADC2:ADC_IRQn disable */
  417. /**
  418. * Uncomment the line below to disable the "ADC_IRQn" interrupt
  419. * Be aware, disabling shared interrupt may affect other IPs
  420. */
  421. /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
  422. /* USER CODE END ADC2:ADC_IRQn disable */
  423. /* USER CODE BEGIN ADC2_MspDeInit 1 */
  424. /* USER CODE END ADC2_MspDeInit 1 */
  425. }
  426. else if(adcHandle->Instance==ADC3)
  427. {
  428. /* USER CODE BEGIN ADC3_MspDeInit 0 */
  429. /* USER CODE END ADC3_MspDeInit 0 */
  430. /* Peripheral clock disable */
  431. __HAL_RCC_ADC3_CLK_DISABLE();
  432. /**ADC3 GPIO Configuration
  433. PF3 ------> ADC3_IN9
  434. PF4 ------> ADC3_IN14
  435. PF5 ------> ADC3_IN15
  436. PF6 ------> ADC3_IN4
  437. PF7 ------> ADC3_IN5
  438. PF8 ------> ADC3_IN6
  439. PF9 ------> ADC3_IN7
  440. PF10 ------> ADC3_IN8
  441. PC0 ------> ADC3_IN10
  442. PC1 ------> ADC3_IN11
  443. */
  444. HAL_GPIO_DeInit(GPIOF, ADC3_IN9_Voltage_L1_Pin|ADC3_IN14_1998_Pin|ADC3_IN15_Temp_Pin|ADC3_IN4_GMI_VL1_Pin
  445. |ADC3_IN5_Current_L2_Pin|ADC3_IN6_PP_GUN_DET_Pin|ADC3_IN7_Current_L1_Pin|ADC3_IN8_Voltage_L2_Pin);
  446. HAL_GPIO_DeInit(GPIOC, ADC3_IN10_Voltage_L3_Pin|ADC3_IN11_Current_L3_Pin);
  447. /* ADC3 DMA DeInit */
  448. HAL_DMA_DeInit(adcHandle->DMA_Handle);
  449. /* ADC3 interrupt Deinit */
  450. /* USER CODE BEGIN ADC3:ADC_IRQn disable */
  451. /**
  452. * Uncomment the line below to disable the "ADC_IRQn" interrupt
  453. * Be aware, disabling shared interrupt may affect other IPs
  454. */
  455. /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
  456. /* USER CODE END ADC3:ADC_IRQn disable */
  457. /* USER CODE BEGIN ADC3_MspDeInit 1 */
  458. /* USER CODE END ADC3_MspDeInit 1 */
  459. }
  460. }
  461. /* USER CODE BEGIN 1 */
  462. //------------------------------------------------------------------------------
  463. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  464. {
  465. if(hadc->Instance == ADC1)
  466. {
  467. HAL_ADC_Stop_DMA(&hadc1);
  468. isDMAEnd_ADC1 = ON;
  469. }
  470. if(hadc->Instance == ADC2)
  471. {
  472. HAL_ADC_Stop_DMA(&hadc2);
  473. isDMAEnd_ADC2 = ON;
  474. }
  475. if(hadc->Instance == ADC3)
  476. {
  477. HAL_ADC_Stop_DMA(&hadc3);
  478. isDMAEnd_ADC3 = ON;
  479. }
  480. }
  481. //------------------------------------------------------------------------------
  482. uint8_t filter_move_avg(MOVE_AVG_FILTER *data, uint32_t value)
  483. {
  484. uint8_t result = FAIL;
  485. uint32_t buf = 0;
  486. data->buffer[data->idx_put] = value;
  487. if(++data->idx_put>=LIMIT_MOVE_FILTER)data->idx_put=0;
  488. for(int idx=0;idx<LIMIT_MOVE_FILTER;idx++)
  489. {
  490. buf += data->buffer[idx];
  491. }
  492. data->value = buf/LIMIT_MOVE_FILTER;
  493. return result;
  494. }
  495. //------------------------------------------------------------------------------
  496. uint8_t Current_filter_move_avg(MOVE_AVG_FILTER *data, uint32_t value)
  497. {
  498. uint8_t result = FAIL;
  499. uint32_t buf = 0;
  500. data->buffer[data->idx_put] = value;
  501. if(++data->idx_put>=CURRENT_LIMIT_MOVE_FILTER)data->idx_put=0;
  502. for(int idx=0;idx<CURRENT_LIMIT_MOVE_FILTER;idx++)
  503. {
  504. buf += data->buffer[idx];
  505. }
  506. data->value = buf/CURRENT_LIMIT_MOVE_FILTER;
  507. return result;
  508. }
  509. //------------------------------------------------------------------------------
  510. uint8_t Voltage_filter_move_avg(MOVE_AVG_FILTER *data, uint32_t value)
  511. {
  512. uint8_t result = FAIL;
  513. uint32_t buf = 0;
  514. data->buffer[data->idx_put] = value;
  515. if(++data->idx_put>=VOLTAGE_LIMIT_MOVE_FILTER)data->idx_put=0;
  516. for(int idx=0;idx<VOLTAGE_LIMIT_MOVE_FILTER;idx++)
  517. {
  518. buf += data->buffer[idx];
  519. }
  520. data->value = buf/VOLTAGE_LIMIT_MOVE_FILTER;
  521. return result;
  522. }
  523. //------------------------------------------------------------------------------
  524. #ifdef MODIFY_CAL_MALLOC
  525. void CalMalloc(float** pBuf, u16 Len, u16* pLastLen)
  526. {
  527. if (*pBuf == NULL || Len != *pLastLen)
  528. {
  529. if (*pBuf != NULL)
  530. {
  531. free(*pBuf);
  532. *pBuf = NULL;
  533. }
  534. *pLastLen = Len;
  535. *pBuf = (float*)malloc(sizeof(float) * Len);
  536. XP("#CalMalloc(%d): %s\r\n", Len, *pBuf == NULL ? "[NG]" : "OK");
  537. }
  538. }
  539. #endif
  540. //------------------------------------------------------------------------------
  541. /*
  542. uint16_t vRms_cal(uint16_t *data, uint16_t length)
  543. {
  544. float sum = 0;
  545. #ifdef MODIFY_CAL_MALLOC
  546. static float *buffer = NULL;
  547. static u16 LastLen = 0;
  548. CalMalloc(&buffer, length, &LastLen);
  549. #else
  550. float *buffer = (float*)malloc(sizeof(float) * length);
  551. #endif
  552. #ifdef MODIFY_CHECK_MALLOC_RESAULT
  553. if (buffer == NULL)
  554. {
  555. XP("\r\n*** MALLOC [NG] (vRms_cal: %d) ***\r\n\r\n", sizeof(float) * length);
  556. return HTK_U16_MAX;
  557. }
  558. #endif
  559. uint16_t result = 0;
  560. uint8_t dirUp = 0;
  561. uint8_t idx_Cnt = 0;
  562. uint16_t idx_1st, idx_2nd;
  563. for(uint16_t idx=1 ; idx<ADC3_SAMPLE_COUNT ; idx++)
  564. {
  565. if(data[idx] > data[idx-1])
  566. {
  567. dirUp = ON;
  568. }
  569. if(dirUp)
  570. {
  571. if(data[idx] < data[idx-1])
  572. {
  573. if(idx_Cnt == 0)
  574. {
  575. idx_1st = idx;
  576. }
  577. else
  578. {
  579. idx_2nd = idx;
  580. }
  581. idx_Cnt++;
  582. dirUp = OFF;
  583. }
  584. }
  585. if(idx_Cnt >= 2)
  586. break;
  587. }
  588. if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0))
  589. {
  590. idx_1st = 0;
  591. idx_2nd = length-1;
  592. }
  593. //edward
  594. ADCWDGHighThreshold = data[idx_1st]-50;
  595. ADCWDGLowThreshold = data[idx_1st+((idx_2nd-idx_1st)/2)]+50;
  596. is60Hz = (((idx_2nd-idx_1st)<43)?ON:OFF);
  597. for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
  598. {
  599. buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
  600. buffer[idx-idx_1st] -= 1.65;
  601. buffer[idx-idx_1st] /= 0.00322; //0.00324; »~®t§ó¦h
  602. sum += pow(buffer[idx-idx_1st],2);
  603. }
  604. sum /= (idx_2nd-idx_1st);
  605. result = (uint16_t) (sqrt(sum)*100);
  606. #ifndef MODIFY_CAL_MALLOC
  607. free(buffer);
  608. #endif
  609. return result;
  610. }
  611. */
  612. //------------------------------------------------------------------------------
  613. uint16_t cRms_cal(uint16_t *data, uint16_t length)
  614. {
  615. float sum = 0;
  616. #ifdef MODIFY_CAL_MALLOC
  617. static float *buffer = NULL;
  618. static u16 LastLen = 0;
  619. CalMalloc(&buffer, length, &LastLen);
  620. #else
  621. float *buffer = (float*)malloc(sizeof(float) * length);
  622. #endif
  623. #ifdef MODIFY_CHECK_MALLOC_RESAULT
  624. if (buffer == NULL)
  625. {
  626. XP("\r\n*** MALLOC [NG] (cRms_cal: %d) ***\r\n\r\n", sizeof(float) * length);
  627. return HTK_U16_MAX;
  628. }
  629. #endif
  630. uint16_t result = 0;
  631. uint8_t dirUp = 0;
  632. uint8_t idx_Cnt = 0;
  633. uint16_t idx_1st, idx_2nd;
  634. uint32_t Avg_offset_sum ;
  635. float Avg_offset ;
  636. #ifdef MODIFY_FUNC_CRMS_CAL
  637. for(uint16_t idx=3 ; idx<length ; idx++)
  638. #else
  639. for(uint16_t idx=3 ; idx<ADC3_SAMPLE_COUNT ; idx++)
  640. #endif
  641. {
  642. if((data[idx] < 2148) && (data[idx] > 1948) && (dirUp == OFF))
  643. {
  644. idx_1st = idx;
  645. idx_Cnt++;
  646. dirUp = ON ;
  647. }
  648. if((idx-idx_1st)>=69)
  649. {
  650. if((data[idx] < 2148) && (data[idx] > 1948))
  651. {
  652. idx_2nd = idx;
  653. idx_Cnt++;
  654. }
  655. }
  656. if(idx_Cnt >= 2)
  657. break;
  658. }
  659. if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0) || (idx_2nd-idx_1st < 69) || (idx_2nd-idx_1st> 90))
  660. {
  661. result = adc_value.ADC3_IN7_Current_L1.value;
  662. }
  663. else
  664. {
  665. //Avg offset
  666. Avg_offset_sum = 0 ;
  667. for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
  668. {
  669. Avg_offset_sum += data [idx] ;
  670. }
  671. Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
  672. for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
  673. {
  674. buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
  675. if (buffer[idx-idx_1st] > Avg_offset)
  676. buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
  677. else
  678. buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
  679. buffer[idx-idx_1st] *= 51.1 ; // /= 0.01957; , *= 51.1 ;
  680. sum += pow(buffer[idx-idx_1st],2);
  681. }
  682. sum /= (idx_2nd-idx_1st);
  683. result = (uint16_t) (sqrt(sum)*100);
  684. }
  685. #ifndef MODIFY_CAL_MALLOC
  686. free(buffer);
  687. #endif
  688. return result;
  689. }
  690. //------------------------------------------------------------------------------
  691. uint16_t GFRms_cal(uint16_t *data, uint16_t length)
  692. {
  693. float sum = 0;
  694. #ifdef MODIFY_CAL_MALLOC
  695. static float *buffer = NULL;
  696. static u16 LastLen = 0;
  697. CalMalloc(&buffer, length, &LastLen);
  698. #else
  699. float *buffer = (float*)malloc(sizeof(float) * length);
  700. #endif
  701. #ifdef MODIFY_CHECK_MALLOC_RESAULT
  702. if (buffer == NULL)
  703. {
  704. XP("\r\n*** MALLOC [NG] (GFRms_cal: %d) ***\r\n\r\n", sizeof(float) * length);
  705. return HTK_U16_MAX;
  706. }
  707. #endif
  708. uint16_t result = 0;
  709. uint8_t dirUp = 0;
  710. uint8_t idx_Cnt = 0;
  711. uint16_t idx_1st, idx_2nd;
  712. uint32_t Avg_offset_sum ;
  713. float Avg_offset ;
  714. #ifdef MODIFY_FUNC_GFRMS_CAL
  715. for(uint16_t idx=2 ; idx<length ; idx++)
  716. #else
  717. for(uint16_t idx=2 ; idx<ADC3_SAMPLE_COUNT ; idx++)
  718. #endif
  719. {
  720. if((data[idx] > data[idx-1]) && (data[idx-1] > data[idx-2]))
  721. {
  722. dirUp = ON;
  723. }
  724. if(dirUp)
  725. {
  726. if((data[idx] < data[idx-1]) && (data[idx-1] < data[idx-2]))
  727. {
  728. if(idx_Cnt == 0)
  729. {
  730. idx_1st = idx;
  731. }
  732. else
  733. {
  734. idx_2nd = idx;
  735. }
  736. idx_Cnt++;
  737. dirUp = OFF;
  738. }
  739. }
  740. if(idx_Cnt >= 2)
  741. break;
  742. }
  743. if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0))
  744. {
  745. idx_1st = 0;
  746. idx_2nd = length-1;
  747. }
  748. idx_1st = 0;
  749. idx_2nd = length-1;
  750. //Avg offset
  751. Avg_offset_sum = 0 ;
  752. for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
  753. {
  754. Avg_offset_sum += data [idx] ;
  755. }
  756. Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
  757. for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
  758. {
  759. buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
  760. if (buffer[idx-idx_1st] > Avg_offset)
  761. buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
  762. else
  763. buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
  764. buffer[idx-idx_1st] *= 1000;
  765. buffer[idx-idx_1st] /= 29.91;//27.21;//29.91;
  766. sum += pow(buffer[idx-idx_1st],2);
  767. }
  768. sum /= (idx_2nd-idx_1st);
  769. result = (uint16_t) (sqrt(sum)*100);
  770. // if (result > 4000)
  771. // {
  772. // DEBUG_INFO("Avg_offset: %f\r\n", Avg_offset);
  773. // DEBUG_INFO("idx_1st: %d\r\n", idx_1st);
  774. // DEBUG_INFO("idx_2nd: %d\r\n", idx_2nd);
  775. // DEBUG_INFO("sum: %f\r\n", sum);
  776. // }
  777. #ifndef MODIFY_CAL_MALLOC
  778. free(buffer);
  779. #endif
  780. return result;
  781. }
  782. //------------------------------------------------------------------------------
  783. uint16_t avg_cal(uint16_t *data, uint16_t length)
  784. {
  785. uint32_t result = 0;
  786. for(uint16_t idx=0;idx<length;idx++)
  787. result += data[idx];
  788. return (result/length);
  789. }
  790. //------------------------------------------------------------------------------
  791. #ifdef FUNC_TEMP_SENSOR_WITH_NEG_VALUE
  792. int16_t getTemperature(uint16_t adc_raw) //3438 0«× , 3376 2«×
  793. {
  794. int16_t rtn = (int16_t)((1526319.45/((363.15* log((8.82353*(adc_raw*3.3/4095)/ (3.3-(adc_raw*3.3/4095)))))+4203))-273.15);
  795. #ifdef FUNC_TEMP_SET_MAXMIN_WHEN_OUT_OF_SPEC
  796. if (rtn < -60) return -60; //NTC not mount, open circuit, adc_count => almost MAX
  797. if (rtn > 194) return 194; //sort circuit, adc_count => almost MIN
  798. return rtn;
  799. #else
  800. if (rtn >= -60 && rtn <= 194)
  801. return rtn;
  802. else
  803. return 0;
  804. #endif
  805. }
  806. #ifdef FUNC_AX48_NACS_TEMP_SENSOR_COMPENSATION_AND_MODIFY_FORMULA_20240522
  807. int16_t getTemperature_NACS(uint16_t adc_raw) //3438 0«× , 3376 2«×
  808. {
  809. int16_t rtn = (int16_t)((1526319.45/((363.15* log((8.82353*(adc_raw*3.3/4095)/ (3.3-(adc_raw*3.3/4095)))))+3972))-273.15);
  810. if (rtn >= -60 && rtn <= 194)
  811. return rtn;
  812. else
  813. return 0;
  814. }
  815. #endif
  816. #else //FUNC_TEMP_SENSOR_WITH_NEG_VALUE
  817. uint16_t getTemperature(uint16_t adc_raw) //3438 0«× , 3376 2«×
  818. {
  819. //uint16_t beta = 4050;
  820. //uint16_t r0 = 10000;
  821. //uint16_t Rntc = (uint16_t)((r0/(1-(adc_raw/4095.0)))-r0);
  822. //return (uint16_t)((1/((1/298.15)+((1/(beta*1.0))*log(Rntc/10000.0)))) - 273.15);
  823. if (adc_raw>3438) adc_raw = 3438 ;
  824. return (uint16_t) ((1526319.45/((363.15* log((8.82353*(adc_raw*3.3/4095)/ (3.3-(adc_raw*3.3/4095)))))+4203))-273.15) ;
  825. }
  826. #endif //FUNC_TEMP_SENSOR_WITH_NEG_VALUE
  827. //--------------------------------------------------------------------------------------------------------------------
  828. uint16_t vRms_cal2(uint16_t *data, uint16_t length)
  829. {
  830. float sum = 0;
  831. #ifdef MODIFY_CAL_MALLOC
  832. static float *buffer = NULL;
  833. static u16 LastLen = 0;
  834. CalMalloc(&buffer, length, &LastLen);
  835. #else
  836. float *buffer = (float*)malloc(sizeof(float) * length);
  837. #endif
  838. #ifdef MODIFY_CHECK_MALLOC_RESAULT
  839. if (buffer == NULL)
  840. {
  841. XP("\r\n*** MALLOC [NG] (vRms_cal2: %d) ***\r\n\r\n", sizeof(float) * length);
  842. return HTK_U16_MAX;
  843. }
  844. #endif
  845. uint16_t result = 0;
  846. uint8_t dirUp = 0;
  847. uint8_t idx_Cnt = 0;
  848. uint16_t idx_1st, idx_2nd;
  849. uint32_t Avg_offset_sum ;
  850. float Avg_offset ;
  851. #ifdef MODIFY_FUNC_VRMS_CAL2
  852. for(uint16_t idx=3 ; idx<length ; idx++)
  853. #else
  854. for(uint16_t idx=3 ; idx<ADC3_SAMPLE_COUNT ; idx++)
  855. #endif
  856. {
  857. if((data[idx] < 2148) && (data[idx] > 1948) && (dirUp == OFF))
  858. {
  859. idx_1st = idx;
  860. idx_Cnt++;
  861. dirUp = ON ;
  862. }
  863. if((idx-idx_1st)>=69)
  864. {
  865. if((data[idx] < 2148) && (data[idx] > 1948))
  866. {
  867. idx_2nd = idx;
  868. idx_Cnt++;
  869. }
  870. }
  871. if(idx_Cnt >= 2)
  872. break;
  873. }
  874. if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0) || (idx_2nd-idx_1st < 69) || (idx_2nd-idx_1st> 90))
  875. {
  876. result = adc_value.ADC3_IN9_Voltage_L1.value;
  877. }
  878. else
  879. {
  880. //edward
  881. ADCWDGHighThreshold = data[idx_1st]-50;
  882. ADCWDGLowThreshold = data[idx_1st+((idx_2nd-idx_1st)/2)]+50;
  883. is60Hz = (((idx_2nd-idx_1st)<43)?ON:OFF);
  884. //Avg offset
  885. Avg_offset_sum = 0 ;
  886. for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
  887. {
  888. Avg_offset_sum += data [idx] ;
  889. }
  890. Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
  891. for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
  892. {
  893. buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
  894. //buffer[idx-idx_1st] -= 1.65;
  895. if (buffer[idx-idx_1st] > Avg_offset)
  896. buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
  897. else
  898. buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
  899. buffer[idx-idx_1st] *= 314.136 ; // /= 0.00324;//*= 314.136 ;
  900. sum += pow(buffer[idx-idx_1st],2);
  901. }
  902. sum /= (idx_2nd-idx_1st);
  903. result = (uint16_t) (sqrt(sum)*100);
  904. }
  905. #ifndef MODIFY_CAL_MALLOC
  906. free(buffer);
  907. #endif
  908. return result;
  909. }
  910. //------------------------------------------------------------------------------
  911. /*
  912. uint16_t cRms_cal2(uint16_t *data, uint16_t length)
  913. {
  914. float sum = 0;
  915. #ifdef MODIFY_CAL_MALLOC
  916. static float *buffer = NULL;
  917. static u16 LastLen = 0;
  918. CalMalloc(&buffer, length, &LastLen);
  919. #else
  920. float *buffer = (float*)malloc(sizeof(float) * length);
  921. #endif
  922. #ifdef MODIFY_CHECK_MALLOC_RESAULT
  923. if (buffer == NULL)
  924. {
  925. XP("\r\n*** MALLOC [NG] (cRms_cal2: %d) ***\r\n\r\n", sizeof(float) * length);
  926. return HTK_U16_MAX;
  927. }
  928. #endif
  929. uint16_t result = 0;
  930. uint8_t dirUp = 0;
  931. uint8_t idx_Cnt = 0;
  932. uint16_t idx_1st, idx_2nd;
  933. uint32_t Avg_offset_sum ;
  934. float Avg_offset ;
  935. for(uint16_t idx=3 ; idx<ADC2_SAMPLE_COUNT ; idx++)
  936. {
  937. if((data[idx] > data[idx-1]) && (data[idx] > data[idx-2]) && (data[idx] > data[idx-3]))
  938. {
  939. dirUp = ON;
  940. }
  941. if(dirUp)
  942. {
  943. //if(data[idx] < data[idx-1])
  944. if((data[idx] < data[idx-1]) && (data[idx] < data[idx-2]) && (data[idx] < data[idx-3]))
  945. {
  946. if(idx_Cnt == 0)
  947. {
  948. idx_1st = idx;
  949. }
  950. else
  951. {
  952. idx_2nd = idx;
  953. }
  954. idx_Cnt++;
  955. dirUp = OFF;
  956. }
  957. }
  958. if(idx_Cnt >= 2)
  959. break;
  960. }
  961. if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0))
  962. {
  963. idx_1st = 0;
  964. idx_2nd = length-1;
  965. }
  966. //Avg offset
  967. Avg_offset_sum = 0 ;
  968. for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
  969. {
  970. Avg_offset_sum += data [idx] ;
  971. }
  972. Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
  973. for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
  974. {
  975. buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
  976. //buffer[idx-idx_1st] -= 1.65;
  977. if (buffer[idx-idx_1st] > Avg_offset)
  978. buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
  979. else
  980. buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
  981. buffer[idx-idx_1st] /= 0.01957;
  982. sum += pow(buffer[idx-idx_1st],2);
  983. }
  984. sum /= (idx_2nd-idx_1st);
  985. result = (uint16_t) (sqrt(sum)*100);
  986. #ifndef MODIFY_CAL_MALLOC
  987. free(buffer);
  988. #endif
  989. return result;
  990. }
  991. */
  992. //------------------------------------------------------------------------------
  993. uint16_t RlyVmax_cal(uint16_t *data, uint16_t length)
  994. {
  995. uint16_t result = 0 ;
  996. uint16_t idx_max_value = 0 ;
  997. for(uint16_t idx=3 ; idx<ADC2_SAMPLE_COUNT-3 ; idx++)
  998. {
  999. //if((data[idx] > data[idx-1]) && (data[idx] > data[idx-2]) && (data[idx] > data[idx-3]))
  1000. if((data[idx] < data[idx-1]) && (data[idx] < data[idx-2]) && (data[idx] < data[idx-3]))
  1001. {
  1002. if ( data[idx] > idx_max_value)
  1003. {
  1004. idx_max_value = data[idx] ;
  1005. }
  1006. }
  1007. }
  1008. result = (uint16_t) (idx_max_value*100 *3.3/4095 ); //unit: 0.01V
  1009. return result;
  1010. }
  1011. //------------------------------------------------------------------------------
  1012. uint16_t avg_v_cal(uint16_t *data, uint16_t length)
  1013. {
  1014. uint16_t result = 0;
  1015. uint32_t idx_max_value = 0 ;
  1016. for(uint16_t idx=0;idx<length;idx++)
  1017. idx_max_value += data[idx];
  1018. result = (uint16_t)(idx_max_value/length*100 *3.3/4095) ;
  1019. return result;
  1020. }
  1021. /* USER CODE END 1 */
  1022. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/