Question
STM32F4 problem with USART1 INTERRUPT
Posted on February 19, 2013 at 21:52
Hi guys,
I have a problem with my new schede STM32F407VG. Until now I have been used the STM32F100B so now I'm adapting the code that I wrote. Part of my code is USART with interrupt, so I have wrote a code that read from USART1 (pin rx P10 and pin tx P9) and when the buffer is full the tx interrupt is ENABLE. I think that the configuration GPIO is wrong because I have modified only this part. If I send a string by Terminal I receive different data for example if I send sasdas I receive {\xfe}{\xfe}{\xfe}{\xf0}{\xfe}, I checked the baudrate on the Terminal and it is the same of the USART.I prefix that the code works on the STM32F100B, where the GPIO configuration is:GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */ 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); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA.10 USART1.RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);How is the GPIO configuration for the STM32F4?? I'm very confused.The code write for the STM32F4 is this, where do I err???/* Includes ------------------------------------------------------------------*/
#include ''stm32f4xx.h''#include ''stm324xg_eval.h''char StringLoop[6];/**************************************************************************************/ void RCC_Configuration(void){ /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable UART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);} /**************************************************************************************/ void GPIO_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; // Pin 10 Rx GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // Pin 9 Tx GPIO_PinAFConfig( GPIOA, GPIO_PinSource9, GPIO_AF_USART1); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//
Is it right??
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;////Is it right?? Should it be IN FLOATING????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); } /**************************************************************************************/ void USART_Configuration(void){ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; 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 the USART1 */ USART_Cmd(USART1, ENABLE); } /**************************************************************************************/ void NVIC_Configuration(void){ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); NVIC_InitTypeDef NVIC_InitStructure; // Configure the NVIC Preemption Priority Bits NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);} /**************************************************************************************/int main(void){ RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); USART_Configuration(); while(1){ } }/**************************************************************************************/void USART1_IRQHandler(void){ static int tx_index = 0; static int rx_index = 0; // Interrupt rx if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { StringLoop[rx_index++] = USART_ReceiveData(USART1); if (rx_index >= (sizeof(StringLoop) - 1)) { rx_index = 0; USART_ITConfig(USART1, USART_IT_TXE, ENABLE); } } // Interrupt tx if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { USART_SendData(USART1, StringLoop[tx_index++]); if (tx_index >= (sizeof(StringLoop) - 1)) {tx_index = 0; USART_ITConfig(USART1, USART_IT_TXE, DISABLE);} }} /**************************************************************************************/#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) { }}#endifTHANK!!