adc.c 30 KB

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