2013-09-03 09:27 AM
Hi,
I'm using STM32F103VE. USART1(9600,n,8,1) for sending data in interrupt of TIM2.TIM2 occurs every 10ms. In TIM2 interrupt routine I call a function to transfer data via USART1: USART_SendData(USART1, x); (don't wait to TX complete)My problem is UASRT1 sometime stop transmit data suddenly. It may occurs in several hours or a day or a few days.Anybody know this problem ?USART1 configs:USART_DeInit(USART1); USART_Cmd(USART1, DISABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = Baud; 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 configuration */ USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* Enable USART */ USART_Cmd(USART1, ENABLE);2013-09-03 09:31 AM
Anybody know this problem ?
It's likely not with the initialization code presented here. Review the interrupt code, and how it behaves.2013-09-03 09:36 AM
Thanks clive1 for fast response.
Here is my code for TIM2: /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 10000 - 1; //10ms TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock) TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE);And code for Interrupt:void TIM2_IRQHandler(void){ static uint16_t Count = 0; if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); USART_SendData(USART1, 1); }}2013-09-03 09:41 AM
What about the RXNE code?
You should check that TXE is asserted before sending data to the USART. You don't have to wait for it, but you should check it.2013-09-03 09:43 AM
When USART1 stop transmit, I restarts the MCU, USART1 run again. I think my firmware have some errors, but I don't find any code that disable USART1.
2013-09-03 09:54 AM
Well, it probably isn't disabling the USART.
It is most likely stuck in some interrupt service routine, and the TIM2 Update isn't executed. Something you could readily check by toggling a GPIO pin when you send a character. If the GPIO keeps toggling, you'll need to breakpoint the code and examine the peripheral registers in the failure state. The USART would stop if you disable it's clock, or clocks related to the pins, or otherwise reconfigured the pins or peripheral. If I used the code presented and serviced or disabled the RXNE interrupt I don't think I'd see a problem. The problem is someplace else.