cancel
Showing results for 
Search instead for 
Did you mean: 

timers IC

es131245
Associate II
Posted on January 24, 2017 at 17:38

Why timer IC mode doent work?

I've configured everything thround Cube, enabled interups and cant get it to work. Some channels just doent work.

__IO uint64_t impData=0;

__IO uint32_t icnt=0;

...

main part

 __HAL_RCC_TIM2_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_DMA1_CLK_ENABLE();

 __HAL_RCC_USART1_CLK_ENABLE();

 pin.Pin=GPIO_PIN_2;

 pin.Mode=GPIO_MODE_AF_PP;

 pin.Pull=GPIO_NOPULL;

 pin.Speed=GPIO_SPEED_FREQ_LOW;

 pin.Alternate=GPIO_AF1_TIM2;

 HAL_GPIO_Init(GPIOA,&pin);

 TIM_MasterConfigTypeDef smcfg;

 TIM_IC_InitTypeDef scfgIC;

 timIC.Instance=TIM2;

 timIC.Init.Prescaler=0;

 timIC.Init.CounterMode=TIM_COUNTERMODE_UP;

 timIC.Init.Period=0;

 timIC.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;

 if(HAL_TIM_IC_Init(&timIC)!=HAL_OK)err('v');

 smcfg.MasterOutputTrigger=TIM_TRGO_RESET;

 smcfg.MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE;

 if(HAL_TIMEx_MasterConfigSynchronization(&timIC,&smcfg)!=HAL_OK)err('z');

 scfgIC.ICPolarity=TIM_INPUTCHANNELPOLARITY_RISING;

 scfgIC.ICSelection=TIM_ICSELECTION_DIRECTTI;

 scfgIC.ICPrescaler=TIM_ICPSC_DIV1;

 scfgIC.ICFilter=0;

 if(HAL_TIM_IC_ConfigChannel(&timIC,&scfgIC,TIM_CHANNEL_3)!=HAL_OK)err('x');

 scfgIC.ICSelection=TIM_ICSELECTION_INDIRECTTI;

 if(HAL_TIM_IC_ConfigChannel(&timIC,&scfgIC,TIM_CHANNEL_4)!=HAL_OK)err('y');

 HAL_NVIC_SetPriority(DMA1_Channel4_IRQn,1,1); // dma uart T

 HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);

 HAL_NVIC_SetPriority(DMA1_Channel5_IRQn,1,1); // dma uart R

 HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);

 HAL_NVIC_SetPriority(USART1_IRQn,2,2); // uart interupts

 HAL_NVIC_EnableIRQ(USART1_IRQn);

 HAL_UART_Receive_DMA(&uart,rbuf,4); // uart recieve

 HAL_NVIC_SetPriority(TIM2_IRQn,3,3); // tim IC

 HAL_NVIC_EnableIRQ(TIM2_IRQn);

 HAL_NVIC_SetPriority(TIM3_IRQn,4,4); // ticking for uart

 HAL_NVIC_EnableIRQ(TIM3_IRQn);

and interupt itself

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef*timIC)

{if(ready!=SET)return;

 impData+=HAL_TIM_ReadCapturedValue(timIC,TIM_CHANNEL_3);

 icnt++;}

uart checkes for values every 0.1 secs.

What am i doing wrong?

MCU is STM32F373CC

9 REPLIES 9
Posted on January 24, 2017 at 17:59

What are the symptoms and how do they fail short of expectations?

 timIC.Init.Period=0;

Really?

JW

Khouloud GARSI
Lead II
Posted on January 24, 2017 at 18:17

Hi

sher.evgeni

,

timIC.Init.Period=0; //the automatic reload value ARR

The timer will count from 0 to the value stored in Auto-reload register. You should use a period value different from 0.

I advise you to refer to the 'TIM_InputCapture' example provided in STM32CubeF3 package.

You find the example under the path below:

STM32Cube_FW_F3_V1.7.0\Projects\STM32373C_EVAL\Examples\TIM

Khouloud.

es131245
Associate II
Posted on January 25, 2017 at 13:07

Thanks. That helped

Posted on January 25, 2017 at 14:55

Is it posible to combine 2 timers length for IC mode?

Posted on January 25, 2017 at 15:39

Please explain further what do you want to achieve.

JW

Posted on January 25, 2017 at 15:48

I'm trying to get timer period 32+16=48bit length

Posted on January 25, 2017 at 17:07

Hello

sher.evgeni

,

There is an example titled 'TIM_CascadeSynchro' under the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

package. Itshows how to synchronize TIM peripherals in cascade mode. I advise you to take it as a reference, it will be really helpful.

The example isfound under the path below:

STM32Cube_FW_F4_V1.0\Projects\STM324x9I_EVAL\Examples\TIM\

TIM_CascadeSynchro

Khouloud

Posted on February 20, 2017 at 16:24

 ,

 ,

Strange situation.

Controller on stm32f373cc cant measure signal under 0.13 mSec. It just gives out constant as 0.13

STM32F407VGT disco board measured frequancy limited hundrets of nano secs.

♯ include 'main.h'

 ,

♯ include 'stm32f3xx_hal.h'

 ,

DMA_HandleTypeDef dmaUT,

 ,

DMA_HandleTypeDef dmaA,

 ,

UART_HandleTypeDef uart,

 ,

TIM_HandleTypeDef tim,

 ,

TIM_HandleTypeDef timIC,

 ,

__IO ITStatus ready=SET,

 ,

__IO uint64_t impData=0,

 ,

__IO uint32_t impCnt=0,

 ,

uint8_t tbuf[12]='000000000000',

 ,

void err(char c){tbuf[0]=c,}

 ,

void NMI_Handler(void){}

 ,

void HardFault_Handler(void){err('1'),}

 ,

void MemManage_Handler(void){err('2'),}

 ,

void BusFault_Handler(void){err('3'),}

 ,

void UsageFault_Handler(void){err('4'),}

 ,

void SVC_Handler(void){}

 ,

void DebugMon_Handler(void){}

 ,

void PendSV_Handler(void){}

 ,

void SysTick_Handler(void)

 ,

{HAL_IncTick(),

 ,

 ,HAL_SYSTICK_IRQHandler(),}

 ,

void DMA1_Channel4_IRQHandler(void){HAL_DMA_IRQHandler(&,dmaUT),}

 ,

void USART1_IRQHandler(void){HAL_UART_IRQHandler(&,uart),}

 ,

void HAL_UART_ErrorCallback(UART_HandleTypeDef*UartHandle){err('5'),}

 ,

void HAL_UART_TxCpltCallback(UART_HandleTypeDef*UartHandle){ready=SET,}

 ,

void TIM3_IRQHandler(void)

 ,

{if(__HAL_TIM_GET_FLAG(&,tim,TIM_FLAG_UPDATE)!=RESET)

 ,

 ,{if(__HAL_TIM_GET_ITSTATUS(&,tim,TIM_IT_UPDATE)!=RESET)

 ,

 , {__HAL_TIM_CLEAR_FLAG(&,tim,TIM_FLAG_UPDATE),

 ,

 , , ready=RESET,

 ,

 , , uint8_t i,p,

 ,

 , , if(impCnt>,0)

 ,

 , , {impData/=impCnt,

 ,

 , , , for(p=8,i=impData%16,impData>,0,impData/=16)tbuf[p--]=i>,9?i+87:i+48,}

 ,

 , , if(HAL_UART_Transmit_DMA(&,uart,tbuf,12)!=HAL_OK)err('6'),

 ,

 , , while(ready!=SET),

 ,

 , , for(uint8_t i=1,i<,12,i++)tbuf[i]=48,

 ,

 , , impData=0,

 ,

 , , impCnt=0,}}}

 ,

void TIM2_IRQHandler(void){HAL_TIM_IRQHandler(&,timIC),}

 ,

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef*timIC)

 ,

{if(ready!=SET||impCnt>,999)return,

 ,

 ,impData+=__HAL_TIM_GetCompare(timIC,TIM_CHANNEL_4),

 ,

 ,impCnt++,

 ,

 ,__HAL_TIM_SetCounter(timIC,0),}int main(void)

 ,

{RCC_OscInitTypeDef RCC_OscInitStruct,

 ,

 ,RCC_ClkInitTypeDef RCC_ClkInitStruct,

 ,

 ,GPIO_InitTypeDef pin,

 ,

 ,TIM_IC_InitTypeDef scfgIC,

 ,

 ,TIM_MasterConfigTypeDef smst,

 ,

 ,HAL_Init(),

 ,

 ,__HAL_RCC_SYSCFG_CLK_ENABLE(),

 ,

 ,HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4),

 ,

 ,HAL_NVIC_SetPriority(MemoryManagement_IRQn,0,0),

 ,

 ,HAL_NVIC_SetPriority(BusFault_IRQn,0,0),

 ,

 ,HAL_NVIC_SetPriority(UsageFault_IRQn,0,0),

 ,

 ,HAL_NVIC_SetPriority(SVCall_IRQn,0,0),

 ,

 ,HAL_NVIC_SetPriority(DebugMonitor_IRQn,0,0),

 ,

 ,HAL_NVIC_SetPriority(PendSV_IRQn,0,0),

 ,

 ,HAL_NVIC_SetPriority(SysTick_IRQn,0,0),

 ,

 ,RCC_PeriphCLKInitTypeDef PeriphClkInit,

 ,

 ,RCC_OscInitStruct.OscillatorType=RCC_OSCILLATORTYPE_HSE,

 ,

 ,RCC_OscInitStruct.HSEState=RCC_HSE_ON,

 ,

 ,RCC_OscInitStruct.HSEPredivValue=RCC_HSE_PREDIV_DIV1,

 ,

 ,RCC_OscInitStruct.HSIState=RCC_HSI_ON,

 ,

 ,RCC_OscInitStruct.PLL.PLLState=RCC_PLL_ON,

 ,

 ,RCC_OscInitStruct.PLL.PLLSource=RCC_PLLSOURCE_HSE,

 ,

 ,RCC_OscInitStruct.PLL.PLLMUL=RCC_PLL_MUL9,

 ,

 ,if(HAL_RCC_OscConfig(&,RCC_OscInitStruct)!=HAL_OK)err('7'),

 ,

 ,RCC_ClkInitStruct.ClockType=RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2,

 ,

 ,RCC_ClkInitStruct.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK,

 ,

 ,RCC_ClkInitStruct.AHBCLKDivider=RCC_SYSCLK_DIV1,

 ,

 ,RCC_ClkInitStruct.APB1CLKDivider=RCC_HCLK_DIV2,

 ,

 ,RCC_ClkInitStruct.APB2CLKDivider=RCC_HCLK_DIV1,

 ,

 ,if(HAL_RCC_ClockConfig(&,RCC_ClkInitStruct,FLASH_LATENCY_2)!=HAL_OK)err('8'),

 ,

 ,PeriphClkInit.PeriphClockSelection=RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC1,

 ,

 ,PeriphClkInit.Usart1ClockSelection=RCC_USART1CLKSOURCE_PCLK2,

 ,

 ,PeriphClkInit.Adc1ClockSelection=RCC_ADC1PCLK2_DIV2,

 ,

 ,if(HAL_RCCEx_PeriphCLKConfig(&,PeriphClkInit)!=HAL_OK)err('9'),

 ,

 ,HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000),

 ,

 ,HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK),

 ,

 ,HAL_NVIC_SetPriority(SysTick_IRQn,0,0),

 ,

 ,__HAL_RCC_SYSCFG_CLK_ENABLE(),

 ,

 ,__HAL_RCC_GPIOF_CLK_ENABLE(),

 ,

 ,__HAL_RCC_GPIOA_CLK_ENABLE(), // tim IC + adc + dac

 ,

 ,__HAL_RCC_DMA1_CLK_ENABLE(),

 ,

 ,__HAL_RCC_USART1_CLK_ENABLE(),

 ,

 ,__HAL_RCC_TIM2_CLK_ENABLE(),

 ,

 ,__HAL_RCC_TIM3_CLK_ENABLE(), ,pin.Pin=GPIO_PIN_9,

 ,

 ,pin.Mode=GPIO_MODE_AF_PP,

 ,

 ,pin.Pull=GPIO_PULLUP,

 ,

 ,pin.Speed=GPIO_SPEED_FREQ_HIGH,

 ,

 ,pin.Alternate=GPIO_AF7_USART1,

 ,

 ,HAL_GPIO_Init(GPIOA,&,pin), ,dmaUT.Instance=DMA1_Channel4,

 ,

 ,dmaUT.Init.Direction=DMA_MEMORY_TO_PERIPH,

 ,

 ,dmaUT.Init.PeriphInc=DMA_PINC_DISABLE,

 ,

 ,dmaUT.Init.MemInc=DMA_MINC_ENABLE,

 ,

 ,dmaUT.Init.PeriphDataAlignment=DMA_PDATAALIGN_BYTE,

 ,

 ,dmaUT.Init.MemDataAlignment=DMA_MDATAALIGN_BYTE,

 ,

 ,dmaUT.Init.Mode=DMA_NORMAL,

 ,

 ,dmaUT.Init.Priority=DMA_PRIORITY_LOW,

 ,

 ,if(HAL_DMA_Init(&,dmaUT)!=HAL_OK)err('a'),

 ,

 ,__HAL_LINKDMA(&,uart,hdmatx,dmaUT),

 ,

 ,uart.Instance=USART1,

 ,

 ,uart.Init.BaudRate=115200,

 ,

 ,uart.Init.WordLength=UART_WORDLENGTH_8B,

 ,

 ,uart.Init.StopBits=UART_STOPBITS_1,

 ,

 ,uart.Init.Parity=UART_PARITY_NONE,

 ,

 ,uart.Init.Mode=UART_MODE_TX,

 ,

 ,uart.Init.HwFlowCtl=UART_HWCONTROL_NONE,

 ,

 ,uart.Init.OverSampling=UART_OVERSAMPLING_16,

 ,

 ,uart.Init.OneBitSampling=UART_ONE_BIT_SAMPLE_DISABLE,

 ,

 ,uart.AdvancedInit.AdvFeatureInit=UART_ADVFEATURE_NO_INIT,

 ,

 ,if(HAL_UART_Init(&,uart)!=HAL_OK)err('b'),

 ,

 ,tim.Instance=TIM3,

 ,

 ,tim.Init.Period=11999,

 ,

 ,tim.Init.Prescaler=999, // .25sec interupts

 ,

 ,tim.Init.ClockDivision=0,

 ,

 ,tim.Init.CounterMode=TIM_COUNTERMODE_UP,

 ,

 ,if(HAL_TIM_Base_Init(&,tim)!=HAL_OK)err('c'),

 ,

 ,if(HAL_TIM_Base_Start_IT(&,tim)!=HAL_OK)err('d'),

 ,

 ,

 ,

 ,pin.Pin=GPIO_PIN_2,

 ,

 ,pin.Mode=GPIO_MODE_AF_PP,

 ,

 ,pin.Pull=GPIO_NOPULL,

 ,

 ,pin.Speed=GPIO_SPEED_FREQ_HIGH,

 ,

 ,pin.Alternate=GPIO_AF1_TIM2,

 ,

 ,HAL_GPIO_Init(GPIOA,&,pin),

 ,

 ,timIC.Instance=TIM2,

 ,

 ,timIC.Init.Prescaler=0,

 ,

 ,timIC.Init.CounterMode=TIM_COUNTERMODE_UP,

 ,

 ,timIC.Init.Period=0xffffffff,

 ,

 ,timIC.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1,

 ,

 ,timIC.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE,

 ,

 ,if(HAL_TIM_IC_Init(&,timIC)!=HAL_OK)err('e'),

 ,

 ,smst.MasterOutputTrigger=TIM_TRGO_RESET,

 ,

 ,smst.MasterSlaveMode=TIM_MASTERSLAVEMODE_DISABLE,

 ,

 ,if(HAL_TIMEx_MasterConfigSynchronization(&,timIC,&,smst)!=HAL_OK)err('f'),

 ,

 ,scfgIC.ICPolarity=TIM_INPUTCHANNELPOLARITY_RISING,

 ,

 ,scfgIC.ICSelection=TIM_ICSELECTION_INDIRECTTI,

 ,

 ,scfgIC.ICPrescaler=TIM_ICPSC_DIV1,

 ,

 ,scfgIC.ICFilter=0,

 ,

 ,if(HAL_TIM_IC_ConfigChannel(&,timIC,&,scfgIC,TIM_CHANNEL_4)!=HAL_OK)err('g'),

 ,

 ,if(HAL_TIM_IC_Start_IT(&,timIC,TIM_CHANNEL_4)!=HAL_OK)err('h'), ,HAL_NVIC_SetPriority(TIM2_IRQn,1,1), // tim IC

 ,

 ,HAL_NVIC_EnableIRQ(TIM2_IRQn),

 ,

 ,HAL_NVIC_SetPriority(DMA1_Channel4_IRQn,2,2), // dma uart T

 ,

 ,HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn),

 ,

 ,HAL_NVIC_SetPriority(USART1_IRQn,3,3), // uart interupts

 ,

 ,HAL_NVIC_EnableIRQ(USART1_IRQn),

 ,

 ,HAL_NVIC_SetPriority(TIM3_IRQn,4,4), // ticking for uart

 ,

 ,HAL_NVIC_EnableIRQ(TIM3_IRQn), ,while(1),}

PS checked with 200MHz OSCI, nice squered signal.

Does not react to voltage source changes, current limitations or extra tantalum capacitors.

Posted on February 21, 2017 at 00:33

I don't use nor understand Cube, but can there be two functions with the same name HAL_TIM_IC_CaptureCallback() ?

Interrupts have a non-negligible latency and it takes time to enter, exit and process the interrupt. The layers in Cube won't help either. Particularities depend on used toolchain and its settings, mainly optimization settings.

I don't know what exactly __HAL_TIM_SetCounter(timIC,0); is supposed to do; but if it does what I think it does, well... I wouldn't clear or otherwise modify a timer in the ISR if I would want to measure time between edges.

Btw. it's not a good idea to transmit data through UART inside an ISR, but that does not appear to be the key problem here.

JW