2020-09-08 03:30 AM
I am using the STM32F407VET6 and SPL libraries. My goal is very straightforward, just send data over usart periodically.
But when I send data (buttoncheck function) inside the timer interrupt it stucks " while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)" while loop
I checked data status register SR its always zero.
Do you have any ideas or suggestions what to check more ?
thanks.
#include "math.h"
#include "float.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include <misc.h>
#include <stm32f4xx_usart.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_can.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_exti.h>
#include <stm32f4xx_syscfg.h>
GPIO_InitTypeDef GPIO_InitStructure;
void buttonCheck();
void USARTSend(char *pucBuffer);
void TIM5_IRQHandler();
int test=0;
void USARTSend(char *pucBuffer)
{
while (*pucBuffer)
{
USART_SendData(USART3, *pucBuffer++);
USART_ClearITPendingBit(USART3,USART_IT_TC);
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{
}
}
}
void TIM5_IRQHandler()
{
// Checks whether the TIM5 interrupt has occurred or not
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
buttonCheck();
}
}
void buttonCheck(){
//sprintf(Buffer2, "pi = %d \n",a);
USARTSend("hello\n");
}
int main(void)
{
SystemInit();
/*******************TIMER**********************/
// Enable clock for TIM5
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_Prescaler = 4199;
TIM_TimeBaseInitStruct.TIM_Period = 9999;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM5, ENABLE);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 5;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/*******************TIMER**********************/
/*******************USART3**********************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
NVIC_InitTypeDef NVIC_InitStructure5;
NVIC_InitStructure5.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure5.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure5.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure5.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure5);
GPIO_InitTypeDef GPIO_InitStructure3;
GPIO_InitStructure3.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // PC.6 USART6_TX, PC.7 USART6_RX
GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure3.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure3.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure3);
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
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_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
/*******************USART3**********************/
/* GPIOG Peripheral clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
/* Configure PG6 and PG8 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
while (1)
{
GPIOE->BSRRH = GPIO_Pin_13;
//USARTSend("USART3 is ready.\r\n");
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/