cancel
Showing results for 
Search instead for 
Did you mean: 

input capture lost edge with CDC STM32F103C8

kick
Associate II

hello, i have a input capture who lost edge when i plug USB.

when the USB cable is not plug or unpluged works perfectly.

i have a toggle led in the capture callback.

when i plug the frequency of the led is unstable.

when i unlug cable, toggle frequency become stable again.

i don't understand because the Priority is

0690X00000Bw9N4QAJ.png

have you an idea?

i will search...

thanks

7 REPLIES 7
JoniS
Senior

What else is the mcu doing? ​it might very well be that somewhere you do too much work in ISR which makes you miss input capture interrupts If those happens very frequently.

Edit: hal_drivers does make it very easy to code everything in ISR context without even realising it.​

kick
Associate II

for the moment i have nothing in my main.

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
  //g_grouping_it = NVIC_GetPriorityGrouping();
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_ADC1_Init();
  MX_TIM2_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  //ADC_Start(1,&hadc1);
  TIMER_START(1,&htim2, 0);
  HAL_Delay(5000);
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  uint8_t l_buffer[30];
//	  ADC_ChannelConfTypeDef sConfig ={0};
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  g_time =  HAL_GetTick();
 
//	  sConfig.Channel = ADC_CHANNEL_0;
//	  sConfig.Rank = ADC_REGULAR_RANK_1;
//	  sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
//	  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
//	  HAL_ADC_Start(&hadc1);
//	  if ( HAL_ADC_PollForConversion(&hadc1,20) == HAL_OK)
//		  g_adc[0] =   HAL_ADC_GetValue(&hadc1);
//
//	  sConfig.Channel = ADC_CHANNEL_1;
//	  sConfig.Rank = ADC_REGULAR_RANK_1;
//	  sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
//	  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
//
//	  HAL_ADC_Start(&hadc1);
//	  if ( HAL_ADC_PollForConversion(&hadc1,20) == HAL_OK)
//			  g_adc[1] =   HAL_ADC_GetValue(&hadc1);
//
//	  sConfig.Channel = ADC_CHANNEL_2;
//	  sConfig.Rank = ADC_REGULAR_RANK_1;
//	  sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
//      HAL_ADC_ConfigChannel(&hadc1, &sConfig);
//
//	  HAL_ADC_Start(&hadc1);
//	  if ( HAL_ADC_PollForConversion(&hadc1,20) == HAL_OK)
//			  g_adc[2] =   HAL_ADC_GetValue(&hadc1);
 
	  /* --------------ADC-------------*/
//	  for (uint8_t l_i=0;l_i<MAX_ADC1;l_i++)
//	  {
//		  sprintf(l_buffer,"adc[%d]:%u\n\r",l_i,g_adc[l_i]);
//		  CDC_Transmit_FS(l_buffer,sizeof(l_buffer));
//		  HAL_Delay(1000);
//          //faire fonction Transmit bloquante
//	  }
//
 
	  //------------- TIMER -------------
//	  for (uint8_t l_i=0;l_i<1;l_i++)
//	  {
//		  double periode = g_input_capture[l_i].periodCapture*BASE_COUNT;
//
//		  uint32_t l_freq = (1/(periode/1000000))+0.5;
//
//		  sprintf(l_buffer,"capture[%d]:%d\n\r",l_i,l_freq);
//		  CDC_Transmit_FS(l_buffer,strlen(l_buffer));
//		  HAL_Delay(1000);
//          //faire fonction Transmit bloquante
//	  }
 
 
 
 
  }
  /* USER CODE END 3 */
}

kick
Associate II
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
 
 
 
	if (g_input_capture[htim->Channel-1].first == 0)
	{
		g_input_capture[htim->Channel-1].countCpt = g_count_10micro;
        g_input_capture[htim->Channel-1].first = 1;
        g_input_capture[htim->Channel-1].countCapture = HAL_TIM_ReadCapturedValue(htim,htim->Channel-1);
        //__HAL_TIM_SET_COUNTER(htim,0);
 
	}
	else
	{
		//voir si count > g_cpt;
		g_input_capture[htim->Channel-1].periodeCpt = g_count_10micro - g_input_capture[htim->Channel-1].countCpt;
		g_input_capture[htim->Channel-1].countCpt = g_count_10micro;
		g_input_capture[htim->Channel-1].periodCapture = HAL_TIM_ReadCapturedValue(htim,htim->Channel-1) - g_input_capture[htim->Channel-1].countCapture;
		g_input_capture[htim->Channel-1].countCapture = HAL_TIM_ReadCapturedValue(htim,htim->Channel-1);
	}
 
}

diferent test in the callback put, no long time in interrupt.

kick
Associate II

after i would like use usb cdc to transmit my real time value.

Do you have other callbacks active? Those would be the ones that most likely make you skip interrupts.

I don't have enought knowledge about how the USB stack works to say anything about how much of processor time it will take, since it could be that alone which makes you miss interrupts

Michal Dudka
Senior III

Check Overcapture flag. That tells you if you'r program wasn't fast enought to readout capture register. Make sure that edges coming with reasonable delay between them. There is some minimum time you need to read capture register and depends on method how you are reading it. Other interrupts with higher priority can delay your timer ISR. Consider use DMA to read captures... it's the fastest way... but of course another DMA with higher priority can block you too...

kick
Associate II

okay but my signal is at 49Hertz..... ^^ is very slow.

TIMER2 channel 1, maybe is used by USB.

TIMER2 channel 2 is better but not good.

i can't be more quickly than read a register in ISR and i think if i delete all the code in the callback.

it's the same,just kept a toggle led. it's visual.