12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202 |
- /**
- ******************************************************************************
- * File Name : ADC.c
- * Description : This file provides code for the configuration
- * of the ADC instances.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under Ultimate Liberty license
- * SLA0044, the "License"; You may not use this file except in compliance with
- * the License. You may obtain a copy of the License at:
- * www.st.com/SLA0044
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "adc.h"
- /* USER CODE BEGIN 0 */
- uint32_t ADC1_Buffer[ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT];
- uint32_t ADC2_Buffer[ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT];
- uint16_t ADC2_Buffer_Each[ADC2_CHANEL_COUNT][ADC2_SAMPLE_COUNT];
- uint32_t ADC3_Buffer[ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT];
- uint16_t ADC3_Buffer_Each[ADC3_CHANEL_COUNT][ADC3_SAMPLE_COUNT];
- uint8_t isDMAEnd_ADC1;
- uint8_t isDMAEnd_ADC2;
- uint8_t isDMAEnd_ADC3;
- struct ADC_VALUE adc_value;
- uint16_t ADCWDGHighThreshold = 3200;
- uint16_t ADCWDGLowThreshold = 820;
- uint8_t is60Hz;
- /* USER CODE END 0 */
- ADC_HandleTypeDef hadc1;
- ADC_HandleTypeDef hadc2;
- ADC_HandleTypeDef hadc3;
- DMA_HandleTypeDef hdma_adc1;
- DMA_HandleTypeDef hdma_adc2;
- DMA_HandleTypeDef hdma_adc3;
- /* ADC1 init function */
- void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc1.Instance = ADC1;
- #ifdef MODIFY_ADC_INIT_CLOCK_PRESCALER_SAME_WITH_AW
- hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
- #else
- hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
- #endif
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- hadc1.Init.ScanConvMode = ENABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 1;
- hadc1.Init.DMAContinuousRequests = ENABLE;
- hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_4;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* ADC2 init function */
- void MX_ADC2_Init(void)
- {
- ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc2.Instance = ADC2;
- #ifdef MODIFY_ADC_INIT_CLOCK_PRESCALER_SAME_WITH_AW
- hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
- #else
- hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
- #endif
- hadc2.Init.Resolution = ADC_RESOLUTION_12B;
- hadc2.Init.ScanConvMode = ENABLE;
- hadc2.Init.ContinuousConvMode = ENABLE;
- hadc2.Init.DiscontinuousConvMode = DISABLE;
- hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc2.Init.NbrOfConversion = 2;
- hadc2.Init.DMAContinuousRequests = ENABLE;
- hadc2.Init.EOCSelection = ADC_EOC_SEQ_CONV;
- if (HAL_ADC_Init(&hadc2) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure the analog watchdog
- */
- AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
- AnalogWDGConfig.HighThreshold = 3722;
- AnalogWDGConfig.LowThreshold = 372;
- AnalogWDGConfig.Channel = ADC_CHANNEL_6;
- //AnalogWDGConfig.ITMode = DISABLE;
- #ifdef MODIFY_ADC_INIT_ANALOG_WDG_ITMODE_SAME_WITH_AW
- AnalogWDGConfig.ITMode = ENABLE;
- #else
- AnalogWDGConfig.ITMode = DISABLE;
- #endif
- if (HAL_ADC_AnalogWDGConfig(&hadc2, &AnalogWDGConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_5;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_6;
- sConfig.Rank = 2;
- if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* ADC3 init function */
- void MX_ADC3_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig = {0};
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc3.Instance = ADC3;
- #ifdef MODIFY_ADC_INIT_CLOCK_PRESCALER_SAME_WITH_AW
- hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
- #else
- hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
- #endif
- hadc3.Init.Resolution = ADC_RESOLUTION_12B;
- hadc3.Init.ScanConvMode = ENABLE;
- hadc3.Init.ContinuousConvMode = ENABLE;
- hadc3.Init.DiscontinuousConvMode = DISABLE;
- hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc3.Init.NbrOfConversion = 10;
- hadc3.Init.DMAContinuousRequests = ENABLE;
- hadc3.Init.EOCSelection = ADC_EOC_SEQ_CONV;
- if (HAL_ADC_Init(&hadc3) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_9;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_14;
- sConfig.Rank = 2;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_15;
- sConfig.Rank = 3;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_4;
- sConfig.Rank = 4;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_7;
- sConfig.Rank = 5;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_5;
- sConfig.Rank = 6;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_6;
- sConfig.Rank = 7;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_8;
- sConfig.Rank = 8;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_10;
- sConfig.Rank = 9;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
- */
- sConfig.Channel = ADC_CHANNEL_11;
- sConfig.Rank = 10;
- if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(adcHandle->Instance==ADC1)
- {
- /* USER CODE BEGIN ADC1_MspInit 0 */
- /* USER CODE END ADC1_MspInit 0 */
- /* ADC1 clock enable */
- __HAL_RCC_ADC1_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**ADC1 GPIO Configuration
- PA4 ------> ADC1_IN4
- */
- GPIO_InitStruct.Pin = ADC1_IN4_CP_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(ADC1_IN4_CP_GPIO_Port, &GPIO_InitStruct);
- /* ADC1 DMA Init */
- /* ADC1 Init */
- hdma_adc1.Instance = DMA2_Stream0;
- hdma_adc1.Init.Channel = DMA_CHANNEL_0;
- hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
- hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_adc1.Init.Mode = DMA_CIRCULAR;
- hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
- hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
- if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
- /* ADC1 interrupt Init */
- HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(ADC_IRQn);
- /* USER CODE BEGIN ADC1_MspInit 1 */
- /* USER CODE END ADC1_MspInit 1 */
- }
- else if(adcHandle->Instance==ADC2)
- {
- /* USER CODE BEGIN ADC2_MspInit 0 */
- /* USER CODE END ADC2_MspInit 0 */
- /* ADC2 clock enable */
- __HAL_RCC_ADC2_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**ADC2 GPIO Configuration
- PA5 ------> ADC2_IN5
- PA6 ------> ADC2_IN6
- */
- GPIO_InitStruct.Pin = ADC2_IN5_Welding_Pin|ADC2_IN6_GF_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* ADC2 DMA Init */
- /* ADC2 Init */
- hdma_adc2.Instance = DMA2_Stream3;
- hdma_adc2.Init.Channel = DMA_CHANNEL_1;
- hdma_adc2.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc2.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc2.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
- hdma_adc2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_adc2.Init.Mode = DMA_CIRCULAR;
- hdma_adc2.Init.Priority = DMA_PRIORITY_LOW;
- hdma_adc2.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
- if (HAL_DMA_Init(&hdma_adc2) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc2);
- /* ADC2 interrupt Init */
- HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(ADC_IRQn);
- /* USER CODE BEGIN ADC2_MspInit 1 */
- /* USER CODE END ADC2_MspInit 1 */
- }
- else if(adcHandle->Instance==ADC3)
- {
- /* USER CODE BEGIN ADC3_MspInit 0 */
- /* USER CODE END ADC3_MspInit 0 */
- /* ADC3 clock enable */
- __HAL_RCC_ADC3_CLK_ENABLE();
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- /**ADC3 GPIO Configuration
- PF3 ------> ADC3_IN9
- PF4 ------> ADC3_IN14
- PF5 ------> ADC3_IN15
- PF6 ------> ADC3_IN4
- PF7 ------> ADC3_IN5
- PF8 ------> ADC3_IN6
- PF9 ------> ADC3_IN7
- PF10 ------> ADC3_IN8
- PC0 ------> ADC3_IN10
- PC1 ------> ADC3_IN11
- */
- GPIO_InitStruct.Pin = ADC3_IN9_Voltage_L1_Pin|ADC3_IN14_1998_Pin|ADC3_IN15_Temp_Pin|ADC3_IN4_GMI_VL1_Pin
- |ADC3_IN5_Current_L2_Pin|ADC3_IN6_PP_GUN_DET_Pin|ADC3_IN7_Current_L1_Pin|ADC3_IN8_Voltage_L2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = ADC3_IN10_Voltage_L3_Pin|ADC3_IN11_Current_L3_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /* ADC3 DMA Init */
- /* ADC3 Init */
- hdma_adc3.Instance = DMA2_Stream1;
- hdma_adc3.Init.Channel = DMA_CHANNEL_2;
- hdma_adc3.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc3.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc3.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc3.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
- hdma_adc3.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_adc3.Init.Mode = DMA_CIRCULAR;
- hdma_adc3.Init.Priority = DMA_PRIORITY_LOW;
- hdma_adc3.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
- if (HAL_DMA_Init(&hdma_adc3) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc3);
- /* ADC3 interrupt Init */
- HAL_NVIC_SetPriority(ADC_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(ADC_IRQn);
- /* USER CODE BEGIN ADC3_MspInit 1 */
- /* USER CODE END ADC3_MspInit 1 */
- }
- }
- void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
- {
- if(adcHandle->Instance==ADC1)
- {
- /* USER CODE BEGIN ADC1_MspDeInit 0 */
- /* USER CODE END ADC1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_ADC1_CLK_DISABLE();
- /**ADC1 GPIO Configuration
- PA4 ------> ADC1_IN4
- */
- HAL_GPIO_DeInit(ADC1_IN4_CP_GPIO_Port, ADC1_IN4_CP_Pin);
- /* ADC1 DMA DeInit */
- HAL_DMA_DeInit(adcHandle->DMA_Handle);
- /* ADC1 interrupt Deinit */
- /* USER CODE BEGIN ADC1:ADC_IRQn disable */
- /**
- * Uncomment the line below to disable the "ADC_IRQn" interrupt
- * Be aware, disabling shared interrupt may affect other IPs
- */
- /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
- /* USER CODE END ADC1:ADC_IRQn disable */
- /* USER CODE BEGIN ADC1_MspDeInit 1 */
- /* USER CODE END ADC1_MspDeInit 1 */
- }
- else if(adcHandle->Instance==ADC2)
- {
- /* USER CODE BEGIN ADC2_MspDeInit 0 */
- /* USER CODE END ADC2_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_ADC2_CLK_DISABLE();
- /**ADC2 GPIO Configuration
- PA5 ------> ADC2_IN5
- PA6 ------> ADC2_IN6
- */
- HAL_GPIO_DeInit(GPIOA, ADC2_IN5_Welding_Pin|ADC2_IN6_GF_Pin);
- /* ADC2 DMA DeInit */
- HAL_DMA_DeInit(adcHandle->DMA_Handle);
- /* ADC2 interrupt Deinit */
- /* USER CODE BEGIN ADC2:ADC_IRQn disable */
- /**
- * Uncomment the line below to disable the "ADC_IRQn" interrupt
- * Be aware, disabling shared interrupt may affect other IPs
- */
- /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
- /* USER CODE END ADC2:ADC_IRQn disable */
- /* USER CODE BEGIN ADC2_MspDeInit 1 */
- /* USER CODE END ADC2_MspDeInit 1 */
- }
- else if(adcHandle->Instance==ADC3)
- {
- /* USER CODE BEGIN ADC3_MspDeInit 0 */
- /* USER CODE END ADC3_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_ADC3_CLK_DISABLE();
- /**ADC3 GPIO Configuration
- PF3 ------> ADC3_IN9
- PF4 ------> ADC3_IN14
- PF5 ------> ADC3_IN15
- PF6 ------> ADC3_IN4
- PF7 ------> ADC3_IN5
- PF8 ------> ADC3_IN6
- PF9 ------> ADC3_IN7
- PF10 ------> ADC3_IN8
- PC0 ------> ADC3_IN10
- PC1 ------> ADC3_IN11
- */
- HAL_GPIO_DeInit(GPIOF, ADC3_IN9_Voltage_L1_Pin|ADC3_IN14_1998_Pin|ADC3_IN15_Temp_Pin|ADC3_IN4_GMI_VL1_Pin
- |ADC3_IN5_Current_L2_Pin|ADC3_IN6_PP_GUN_DET_Pin|ADC3_IN7_Current_L1_Pin|ADC3_IN8_Voltage_L2_Pin);
- HAL_GPIO_DeInit(GPIOC, ADC3_IN10_Voltage_L3_Pin|ADC3_IN11_Current_L3_Pin);
- /* ADC3 DMA DeInit */
- HAL_DMA_DeInit(adcHandle->DMA_Handle);
- /* ADC3 interrupt Deinit */
- /* USER CODE BEGIN ADC3:ADC_IRQn disable */
- /**
- * Uncomment the line below to disable the "ADC_IRQn" interrupt
- * Be aware, disabling shared interrupt may affect other IPs
- */
- /* HAL_NVIC_DisableIRQ(ADC_IRQn); */
- /* USER CODE END ADC3:ADC_IRQn disable */
- /* USER CODE BEGIN ADC3_MspDeInit 1 */
- /* USER CODE END ADC3_MspDeInit 1 */
- }
- }
- /* USER CODE BEGIN 1 */
- //------------------------------------------------------------------------------
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
- {
- if(hadc->Instance == ADC1)
- {
- HAL_ADC_Stop_DMA(&hadc1);
- isDMAEnd_ADC1 = ON;
- }
- if(hadc->Instance == ADC2)
- {
- HAL_ADC_Stop_DMA(&hadc2);
- isDMAEnd_ADC2 = ON;
- }
- if(hadc->Instance == ADC3)
- {
- HAL_ADC_Stop_DMA(&hadc3);
- isDMAEnd_ADC3 = ON;
- }
- }
- //------------------------------------------------------------------------------
- uint8_t filter_move_avg(MOVE_AVG_FILTER *data, uint32_t value)
- {
- uint8_t result = FAIL;
- uint32_t buf = 0;
- data->buffer[data->idx_put] = value;
- if(++data->idx_put>=LIMIT_MOVE_FILTER)data->idx_put=0;
- for(int idx=0;idx<LIMIT_MOVE_FILTER;idx++)
- {
- buf += data->buffer[idx];
- }
- data->value = buf/LIMIT_MOVE_FILTER;
- return result;
- }
- //------------------------------------------------------------------------------
- uint8_t Current_filter_move_avg(MOVE_AVG_FILTER *data, uint32_t value)
- {
- uint8_t result = FAIL;
- uint32_t buf = 0;
- data->buffer[data->idx_put] = value;
- if(++data->idx_put>=CURRENT_LIMIT_MOVE_FILTER)data->idx_put=0;
- for(int idx=0;idx<CURRENT_LIMIT_MOVE_FILTER;idx++)
- {
- buf += data->buffer[idx];
- }
- data->value = buf/CURRENT_LIMIT_MOVE_FILTER;
- return result;
- }
- //------------------------------------------------------------------------------
- uint8_t Voltage_filter_move_avg(MOVE_AVG_FILTER *data, uint32_t value)
- {
- uint8_t result = FAIL;
- uint32_t buf = 0;
- data->buffer[data->idx_put] = value;
- if(++data->idx_put>=VOLTAGE_LIMIT_MOVE_FILTER)data->idx_put=0;
- for(int idx=0;idx<VOLTAGE_LIMIT_MOVE_FILTER;idx++)
- {
- buf += data->buffer[idx];
- }
- data->value = buf/VOLTAGE_LIMIT_MOVE_FILTER;
- return result;
- }
- //------------------------------------------------------------------------------
- #ifdef MODIFY_CAL_MALLOC
- void CalMalloc(float** pBuf, u16 Len, u16* pLastLen)
- {
- if (*pBuf == NULL || Len != *pLastLen)
- {
- if (*pBuf != NULL)
- {
- free(*pBuf);
- *pBuf = NULL;
- }
- *pLastLen = Len;
- *pBuf = (float*)malloc(sizeof(float) * Len);
- XP("#CalMalloc(%d): %s\r\n", Len, *pBuf == NULL ? "[NG]" : "OK");
- }
- }
- #endif
- //------------------------------------------------------------------------------
- /*
- uint16_t vRms_cal(uint16_t *data, uint16_t length)
- {
- float sum = 0;
- #ifdef MODIFY_CAL_MALLOC
- static float *buffer = NULL;
- static u16 LastLen = 0;
- CalMalloc(&buffer, length, &LastLen);
- #else
- float *buffer = (float*)malloc(sizeof(float) * length);
- #endif
- #ifdef MODIFY_CHECK_MALLOC_RESAULT
- if (buffer == NULL)
- {
- XP("\r\n*** MALLOC [NG] (vRms_cal: %d) ***\r\n\r\n", sizeof(float) * length);
- return HTK_U16_MAX;
- }
- #endif
- uint16_t result = 0;
- uint8_t dirUp = 0;
- uint8_t idx_Cnt = 0;
- uint16_t idx_1st, idx_2nd;
- for(uint16_t idx=1 ; idx<ADC3_SAMPLE_COUNT ; idx++)
- {
- if(data[idx] > data[idx-1])
- {
- dirUp = ON;
- }
- if(dirUp)
- {
- if(data[idx] < data[idx-1])
- {
- if(idx_Cnt == 0)
- {
- idx_1st = idx;
- }
- else
- {
- idx_2nd = idx;
- }
- idx_Cnt++;
- dirUp = OFF;
- }
- }
- if(idx_Cnt >= 2)
- break;
- }
- if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0))
- {
- idx_1st = 0;
- idx_2nd = length-1;
- }
- //edward
- ADCWDGHighThreshold = data[idx_1st]-50;
- ADCWDGLowThreshold = data[idx_1st+((idx_2nd-idx_1st)/2)]+50;
- is60Hz = (((idx_2nd-idx_1st)<43)?ON:OFF);
- for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
- {
- buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
- buffer[idx-idx_1st] -= 1.65;
- buffer[idx-idx_1st] /= 0.00322; //0.00324; »~®t§ó¦h
- sum += pow(buffer[idx-idx_1st],2);
- }
- sum /= (idx_2nd-idx_1st);
- result = (uint16_t) (sqrt(sum)*100);
- #ifndef MODIFY_CAL_MALLOC
- free(buffer);
- #endif
- return result;
- }
- */
- //------------------------------------------------------------------------------
- uint16_t cRms_cal(uint16_t *data, uint16_t length)
- {
- float sum = 0;
- #ifdef MODIFY_CAL_MALLOC
- static float *buffer = NULL;
- static u16 LastLen = 0;
- CalMalloc(&buffer, length, &LastLen);
- #else
- float *buffer = (float*)malloc(sizeof(float) * length);
- #endif
- #ifdef MODIFY_CHECK_MALLOC_RESAULT
- if (buffer == NULL)
- {
- XP("\r\n*** MALLOC [NG] (cRms_cal: %d) ***\r\n\r\n", sizeof(float) * length);
- return HTK_U16_MAX;
- }
- #endif
- uint16_t result = 0;
- uint8_t dirUp = 0;
- uint8_t idx_Cnt = 0;
- uint16_t idx_1st, idx_2nd;
- uint32_t Avg_offset_sum ;
- float Avg_offset ;
- #ifdef MODIFY_FUNC_CRMS_CAL
- for(uint16_t idx=3 ; idx<length ; idx++)
- #else
- for(uint16_t idx=3 ; idx<ADC3_SAMPLE_COUNT ; idx++)
- #endif
- {
- if((data[idx] < 2148) && (data[idx] > 1948) && (dirUp == OFF))
- {
- idx_1st = idx;
- idx_Cnt++;
- dirUp = ON ;
- }
- if((idx-idx_1st)>=69)
- {
- if((data[idx] < 2148) && (data[idx] > 1948))
- {
- idx_2nd = idx;
- idx_Cnt++;
- }
- }
- if(idx_Cnt >= 2)
- break;
- }
- if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0) || (idx_2nd-idx_1st < 69) || (idx_2nd-idx_1st> 90))
- {
- result = adc_value.ADC3_IN7_Current_L1.value;
- }
- else
- {
- //Avg offset
- Avg_offset_sum = 0 ;
- for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
- {
- Avg_offset_sum += data [idx] ;
- }
- Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
- for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
- {
- buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
- if (buffer[idx-idx_1st] > Avg_offset)
- buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
- else
- buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
- buffer[idx-idx_1st] *= 51.1 ; // /= 0.01957; , *= 51.1 ;
- sum += pow(buffer[idx-idx_1st],2);
- }
- sum /= (idx_2nd-idx_1st);
- result = (uint16_t) (sqrt(sum)*100);
- }
- #ifndef MODIFY_CAL_MALLOC
- free(buffer);
- #endif
- return result;
- }
- //------------------------------------------------------------------------------
- uint16_t GFRms_cal(uint16_t *data, uint16_t length)
- {
- float sum = 0;
- #ifdef MODIFY_CAL_MALLOC
- static float *buffer = NULL;
- static u16 LastLen = 0;
- CalMalloc(&buffer, length, &LastLen);
- #else
- float *buffer = (float*)malloc(sizeof(float) * length);
- #endif
- #ifdef MODIFY_CHECK_MALLOC_RESAULT
- if (buffer == NULL)
- {
- XP("\r\n*** MALLOC [NG] (GFRms_cal: %d) ***\r\n\r\n", sizeof(float) * length);
- return HTK_U16_MAX;
- }
- #endif
- uint16_t result = 0;
- uint8_t dirUp = 0;
- uint8_t idx_Cnt = 0;
- uint16_t idx_1st, idx_2nd;
- uint32_t Avg_offset_sum ;
- float Avg_offset ;
- #ifdef MODIFY_FUNC_GFRMS_CAL
- for(uint16_t idx=2 ; idx<length ; idx++)
- #else
- for(uint16_t idx=2 ; idx<ADC3_SAMPLE_COUNT ; idx++)
- #endif
- {
- if((data[idx] > data[idx-1]) && (data[idx-1] > data[idx-2]))
- {
- dirUp = ON;
- }
- if(dirUp)
- {
- if((data[idx] < data[idx-1]) && (data[idx-1] < data[idx-2]))
- {
- if(idx_Cnt == 0)
- {
- idx_1st = idx;
- }
- else
- {
- idx_2nd = idx;
- }
- idx_Cnt++;
- dirUp = OFF;
- }
- }
- if(idx_Cnt >= 2)
- break;
- }
- if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0))
- {
- idx_1st = 0;
- idx_2nd = length-1;
- }
- idx_1st = 0;
- idx_2nd = length-1;
- //Avg offset
- Avg_offset_sum = 0 ;
- for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
- {
- Avg_offset_sum += data [idx] ;
- }
- Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
- for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
- {
- buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
- if (buffer[idx-idx_1st] > Avg_offset)
- buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
- else
- buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
- buffer[idx-idx_1st] *= 1000;
- buffer[idx-idx_1st] /= 29.91;//27.21;//29.91;
- sum += pow(buffer[idx-idx_1st],2);
- }
- sum /= (idx_2nd-idx_1st);
- result = (uint16_t) (sqrt(sum)*100);
- // if (result > 4000)
- // {
- // DEBUG_INFO("Avg_offset: %f\r\n", Avg_offset);
- // DEBUG_INFO("idx_1st: %d\r\n", idx_1st);
- // DEBUG_INFO("idx_2nd: %d\r\n", idx_2nd);
- // DEBUG_INFO("sum: %f\r\n", sum);
- // }
- #ifndef MODIFY_CAL_MALLOC
- free(buffer);
- #endif
- return result;
- }
- //------------------------------------------------------------------------------
- uint16_t avg_cal(uint16_t *data, uint16_t length)
- {
- uint32_t result = 0;
- for(uint16_t idx=0;idx<length;idx++)
- result += data[idx];
- return (result/length);
- }
- //------------------------------------------------------------------------------
- #ifdef FUNC_TEMP_SENSOR_WITH_NEG_VALUE
- int16_t getTemperature(uint16_t adc_raw) //3438 0«× , 3376 2«×
- {
- 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);
- #ifdef FUNC_TEMP_SET_MAXMIN_WHEN_OUT_OF_SPEC
- if (rtn < -60) return -60; //NTC not mount, open circuit, adc_count => almost MAX
- if (rtn > 194) return 194; //sort circuit, adc_count => almost MIN
- return rtn;
- #else
- if (rtn >= -60 && rtn <= 194)
- return rtn;
- else
- return 0;
- #endif
- }
- #ifdef FUNC_AX48_NACS_TEMP_SENSOR_COMPENSATION_AND_MODIFY_FORMULA_20240522
- int16_t getTemperature_NACS(uint16_t adc_raw) //3438 0«× , 3376 2«×
- {
- 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);
- if (rtn >= -60 && rtn <= 194)
- return rtn;
- else
- return 0;
- }
- #endif
- #else //FUNC_TEMP_SENSOR_WITH_NEG_VALUE
- uint16_t getTemperature(uint16_t adc_raw) //3438 0«× , 3376 2«×
- {
- //uint16_t beta = 4050;
- //uint16_t r0 = 10000;
- //uint16_t Rntc = (uint16_t)((r0/(1-(adc_raw/4095.0)))-r0);
- //return (uint16_t)((1/((1/298.15)+((1/(beta*1.0))*log(Rntc/10000.0)))) - 273.15);
- if (adc_raw>3438) adc_raw = 3438 ;
- 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) ;
- }
- #endif //FUNC_TEMP_SENSOR_WITH_NEG_VALUE
- //--------------------------------------------------------------------------------------------------------------------
- uint16_t vRms_cal2(uint16_t *data, uint16_t length)
- {
- float sum = 0;
- #ifdef MODIFY_CAL_MALLOC
- static float *buffer = NULL;
- static u16 LastLen = 0;
- CalMalloc(&buffer, length, &LastLen);
- #else
- float *buffer = (float*)malloc(sizeof(float) * length);
- #endif
- #ifdef MODIFY_CHECK_MALLOC_RESAULT
- if (buffer == NULL)
- {
- XP("\r\n*** MALLOC [NG] (vRms_cal2: %d) ***\r\n\r\n", sizeof(float) * length);
- return HTK_U16_MAX;
- }
- #endif
- uint16_t result = 0;
- uint8_t dirUp = 0;
- uint8_t idx_Cnt = 0;
- uint16_t idx_1st, idx_2nd;
- uint32_t Avg_offset_sum ;
- float Avg_offset ;
- #ifdef MODIFY_FUNC_VRMS_CAL2
- for(uint16_t idx=3 ; idx<length ; idx++)
- #else
- for(uint16_t idx=3 ; idx<ADC3_SAMPLE_COUNT ; idx++)
- #endif
- {
- if((data[idx] < 2148) && (data[idx] > 1948) && (dirUp == OFF))
- {
- idx_1st = idx;
- idx_Cnt++;
- dirUp = ON ;
- }
- if((idx-idx_1st)>=69)
- {
- if((data[idx] < 2148) && (data[idx] > 1948))
- {
- idx_2nd = idx;
- idx_Cnt++;
- }
- }
- if(idx_Cnt >= 2)
- break;
- }
- if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0) || (idx_2nd-idx_1st < 69) || (idx_2nd-idx_1st> 90))
- {
- result = adc_value.ADC3_IN9_Voltage_L1.value;
- }
- else
- {
- //edward
- ADCWDGHighThreshold = data[idx_1st]-50;
- ADCWDGLowThreshold = data[idx_1st+((idx_2nd-idx_1st)/2)]+50;
- is60Hz = (((idx_2nd-idx_1st)<43)?ON:OFF);
- //Avg offset
- Avg_offset_sum = 0 ;
- for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
- {
- Avg_offset_sum += data [idx] ;
- }
- Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
- for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
- {
- buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
- //buffer[idx-idx_1st] -= 1.65;
- if (buffer[idx-idx_1st] > Avg_offset)
- buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
- else
- buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
- buffer[idx-idx_1st] *= 314.136 ; // /= 0.00324;//*= 314.136 ;
- sum += pow(buffer[idx-idx_1st],2);
- }
- sum /= (idx_2nd-idx_1st);
- result = (uint16_t) (sqrt(sum)*100);
- }
- #ifndef MODIFY_CAL_MALLOC
- free(buffer);
- #endif
- return result;
- }
- //------------------------------------------------------------------------------
- /*
- uint16_t cRms_cal2(uint16_t *data, uint16_t length)
- {
- float sum = 0;
- #ifdef MODIFY_CAL_MALLOC
- static float *buffer = NULL;
- static u16 LastLen = 0;
- CalMalloc(&buffer, length, &LastLen);
- #else
- float *buffer = (float*)malloc(sizeof(float) * length);
- #endif
- #ifdef MODIFY_CHECK_MALLOC_RESAULT
- if (buffer == NULL)
- {
- XP("\r\n*** MALLOC [NG] (cRms_cal2: %d) ***\r\n\r\n", sizeof(float) * length);
- return HTK_U16_MAX;
- }
- #endif
- uint16_t result = 0;
- uint8_t dirUp = 0;
- uint8_t idx_Cnt = 0;
- uint16_t idx_1st, idx_2nd;
- uint32_t Avg_offset_sum ;
- float Avg_offset ;
- for(uint16_t idx=3 ; idx<ADC2_SAMPLE_COUNT ; idx++)
- {
- if((data[idx] > data[idx-1]) && (data[idx] > data[idx-2]) && (data[idx] > data[idx-3]))
- {
- dirUp = ON;
- }
- if(dirUp)
- {
- //if(data[idx] < data[idx-1])
- if((data[idx] < data[idx-1]) && (data[idx] < data[idx-2]) && (data[idx] < data[idx-3]))
- {
- if(idx_Cnt == 0)
- {
- idx_1st = idx;
- }
- else
- {
- idx_2nd = idx;
- }
- idx_Cnt++;
- dirUp = OFF;
- }
- }
- if(idx_Cnt >= 2)
- break;
- }
- if((idx_1st>=length) || (idx_2nd>=length) || (idx_2nd==0))
- {
- idx_1st = 0;
- idx_2nd = length-1;
- }
- //Avg offset
- Avg_offset_sum = 0 ;
- for (uint16_t idx = idx_1st; idx < idx_2nd ; idx++)
- {
- Avg_offset_sum += data [idx] ;
- }
- Avg_offset = (float)(Avg_offset_sum/(idx_2nd-idx_1st))*3.3/4095 ;
- for(uint16_t idx=idx_1st ; idx<idx_2nd ; idx++)
- {
- buffer[idx-idx_1st] = (data[idx]/4095.0*3.3);
- //buffer[idx-idx_1st] -= 1.65;
- if (buffer[idx-idx_1st] > Avg_offset)
- buffer[idx-idx_1st] = buffer[idx-idx_1st] - Avg_offset ;
- else
- buffer[idx-idx_1st] = Avg_offset - buffer[idx-idx_1st] ;
- buffer[idx-idx_1st] /= 0.01957;
- sum += pow(buffer[idx-idx_1st],2);
- }
- sum /= (idx_2nd-idx_1st);
- result = (uint16_t) (sqrt(sum)*100);
- #ifndef MODIFY_CAL_MALLOC
- free(buffer);
- #endif
- return result;
- }
- */
- //------------------------------------------------------------------------------
- uint16_t RlyVmax_cal(uint16_t *data, uint16_t length)
- {
- uint16_t result = 0 ;
- uint16_t idx_max_value = 0 ;
- for(uint16_t idx=3 ; idx<ADC2_SAMPLE_COUNT-3 ; idx++)
- {
- //if((data[idx] > data[idx-1]) && (data[idx] > data[idx-2]) && (data[idx] > data[idx-3]))
- if((data[idx] < data[idx-1]) && (data[idx] < data[idx-2]) && (data[idx] < data[idx-3]))
- {
- if ( data[idx] > idx_max_value)
- {
- idx_max_value = data[idx] ;
- }
- }
- }
- result = (uint16_t) (idx_max_value*100 *3.3/4095 ); //unit: 0.01V
- return result;
- }
- //------------------------------------------------------------------------------
- uint16_t avg_v_cal(uint16_t *data, uint16_t length)
- {
- uint16_t result = 0;
- uint32_t idx_max_value = 0 ;
- for(uint16_t idx=0;idx<length;idx++)
- idx_max_value += data[idx];
- result = (uint16_t)(idx_max_value/length*100 *3.3/4095) ;
- return result;
- }
- /* USER CODE END 1 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|