cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 Timer input capture disables USART - problem

robertnoble9
Associate III
Posted on March 08, 2016 at 08:19

Hi all,

I am new this forum so bear with me. I am working on a project using an STM32F103RBT6 that incorporates a timer in input capture mode (TIM2) and a USART (USART3). I can successfully enable the USART and transmit data over a USB to TTLcable to a laptop. The problem is when I try to set up the timer. As soon a I include the timer initialisation code using TIM_ICInit, the USART stops working and nothing comes through to the computer. I'm hoping someone here could shed some light on it. I'm assuming I have made some school boy error and not realised. I have included some of my code below. Thanks in advance Look forward to hearing from you all. Rob USART set up:

USART_InitTypeDef USART_InitStructure; 
/* set up with baud rate at 115200, Tx and Rx mode */
USART_InitStructure.USART_BaudRate = 115200; 
USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
USART_InitStructure.USART_StopBits = USART_StopBits_1; 
USART_InitStructure.USART_Parity = USART_Parity_No ; 
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 
USART_Init(USART3, &USART_InitStructure); 
USART_Cmd(USART3,ENABLE);

Timer set up:

TIM_ICInitTypeDef TIM_ICInitStructure; 
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; 
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 
TIM_ICInitStructure.TIM_ICFilter = 0x0; 
// initialise channel 1 
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; 
TIM_ICInit(TIM2, &TIM_ICInitStructure); 
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);

16 REPLIES 16
Posted on March 09, 2016 at 17:09

Perhaps you should have picked a DISCO or NUCLEO board for your first outing? Both have a debugger built in, and SWV (Serial Wire Viewer) support, the latter also having a USART/VCP arrangement.

In your case you'll have to flag progress/location with GPIO, LEDs, USART, etc. Output an H character for the Hard Fault, other chars for different interrupt entries.

I still don't see the time base being initialized, or the IRQ Handlers, it's very hard to deduce what could be wrong peering through the keyhole.

If you enter the stock Hard Fault Handler will jam in a while(1) loop. If you have the USART working implement a better handler (see Joseph Yiu). If your IRQHandler don't actually remedy the source of the interrupt it will keep re entering and NEVER return to user space.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
robertnoble9
Associate III
Posted on March 09, 2016 at 17:55

The Maple board was a gift a while ago that I thought I would dust off and have a tinker with.  Perhaps a Nucleo or Discovery board will be the way forward.  My internet was playing up at home last night so couldn't post any code.  With some extra USART writes added in before and after the call to my TIM_Configuration function all I get is the stuff that is printed before the TIM_Configuration call, repeated over and over again, which is not in a while loop.  This doesn't tie up with the while(1) fault handler defaults. 

I will try and get some more code up for you guys tonight.

Just like to say thanks for taking the time to help.

Posted on March 09, 2016 at 18:32

Yes, sounds more like constantly resetting as described.

Is this code running directly, or via a Maple boot loader, or something with a watchdog? How are you getting the code on to the board?

You'd have to review the startup_stm32f1xx.s to see what vectors and handler are set up, and perhaps stm32f1xx_it.c for some of them, depending on how you are constructing and building the application code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Walid FTITI_O
Senior II
Posted on March 09, 2016 at 19:21

Hi minthemerciless, 

Your problem is coming mainly from your GPIOs Configuration: you should select which IOs will be dedicated for TIMx channels' pins and which ones for USART pins. You ought to choose certainly separate IOs for both peripherals. Then, make sure that you are selecting the right Alternate function for the Timers GPIOs and the floating input mode for some USART pins (GPIO_CTSPin and GPIO_RxPin).

Since , you are using the standard library , try to get inspired from these example under

-

        -  

As USART example: “HyperTerminal_HwFlowControl�

At this path:

STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\USART\HyperTerminal_HwFlowControl

-

         - 

As Timer example:  â€œInputCaptureâ€�

At this path: STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\TIM\InputCapture

Dowload the STM32F10x_StdPeriph_Lib from this

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1939/PF257890

-Hannibal-

robertnoble9
Associate III
Posted on March 10, 2016 at 00:25

Hannibal,

''Your problem is coming mainly from your GPIOs Configuration: you should select which IOs will be dedicated for TIMx channels' pins and which ones for USART pin'' My GPIO configuration is shown below. I am (at least I think I am) setting GPIOA pins 0,1,2,3 for TIM2 channels 1,2,3,4, GPIOA pins 6 and 7 for TIM3 channels 1 and 2, GPIOB pins 0 and 1 for TIM3 channels 3 and 4, GPIOB pins 6,7,8,9 for TIM4 channels 1,2,3,4 GPIOB pins 10 and for USART3 Tx and Rx respectively Am I wrong to think this was ok? Based on the examples I have seen, the AF modes I have set should be correct too. clive1, I am currently using the ST Flash loader demonstrator with an FTDI USB to TTL cable via USART1 to flash the chip. I believe there is no bootloader. I have found that when changing the TIM_ICPolarity from BothEdge to Rising, the code continues and does not keep resetting. Is there a know issue with the TIM_ICPolarity_BothEdge value in the standard peripheral library?

void
GPIO_Configuration(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM2 channel 1, 2, 3, 4 pin configuration for inputs */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM3 */
/* channel 1, 2 pin configuration for inputs */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* channel 3, 4 pin configuration for inputs */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* TIM 4 */
/* channel 1, 2, 3, 4 pin configuration as outputs for PWM mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* USART3 */
/* PB.10 is Tx on USART3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* PB.11 is Rx on USART3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

Walid FTITI_O
Senior II
Posted on March 10, 2016 at 15:08

Hi minthemerciless, 

Which STM32F103 die you are using and which hardware platform you are working with ? And tell me if there other external connections 

-Hannibal-

robertnoble9
Associate III
Posted on March 10, 2016 at 18:08

I am working with an STM32F103RBT6 at the moment that is on a Leaflabs Maple board.

External connections will be RC receiver, speeds controllers and 4 voltage comparators.  Eventually, currently I have no external connections.  With the rising edge set in the input capture polarity fields for TIM2 and TIM3 it continuously hits the TIM2_IRQHandler on channel for, (GPIOA pin 0), this is set as a floating input but is not connected to anything so I'm guessing this keeps triggering because of that.  I am I right in thinking a pull down resistor on the input pins should keep them all low and should stop the interrupts from firing all the time?

As far as TIM_ICPolarityBothEdge goes, I believe the STM32F1 series chips do not support this (despite it being and option in the standard peripherals library).  The RM0008 document shows only a single bits for input capture polarity for F1 chips giving only rising and falling compared to the F4 chips which have two bits that allow for rising, falling or both.

Feel free to correct me if I'm wrong.