cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f10x Usart polling problem

lattner
Associate II
Posted on May 04, 2015 at 15:53

Hi,

I have a problem with usart2 with my stm32f10x. I want to send data using polling and the TXE Flag does not SET and i don't know why. This is the init of the GPIO Ports.

/** USART2 GPIO Configuration 
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
/*Enable or disable APB2 peripheral clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/*Configure GPIO pin : PA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStruct);

This is the init of the USART 2 and interrupt init for receive

USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
USART_Cmd(uart, DISABLE);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 38400;
USART_InitStructure.USART_WordLength = USART_WordLength;
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_Tx | USART_Mode_Rx;
USART_Init(uart, &USART_InitStructure);
USART_Cmd(uart, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

My putc funktion is

void Uart_putc(USART_TypeDef* uart, char c)
{
while (USART_GetFlagStatus(uart, USART_FLAG_TXE) == RESET);
USART_SendData(uart, (uint16_t)c);
}

I can send the first caracter but at the second one it goes into an infinite loope. I also checked the usart error flags but they never get set. I use the same settings for UART5 except another baudrate and there everything is ok. It would be very fine if somebody could help me 🙂
7 REPLIES 7
Posted on May 04, 2015 at 16:04

With an app this simple is there some impediment to posting something that's complete and compilable?

Code so far seem fair enough, perhaps it's some other part that's broken?

Do you have a debug view open over the USART2 peripheral registers?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lattner
Associate II
Posted on May 04, 2015 at 16:25

Yeah something other might be the answer but my problem is i searched my code up and down for errors and couldn't figure out anything.

What would you think other peripheral units could trigger problem like this (i know this is very very gerneral) I looked up everthing searched on the internet and nothing worked eccept a timeout function

uint8_t timeout;
void Uart_putc(USART_TypeDef* uart, char c)
{
timeout = 200;
while (USART_GetFlagStatus(uart, USART_FLAG_TXE) == RESET && timeout != 0) timeout--;
USART_SendData(uart, (uint16_t)c);
}

Yes i have debug view for usart2 but everything i see is that the txe is RESET and everything other is ok Sorry i didn't get what you mean by your first sentence, you may noticed that my englisch is not that good
Posted on May 04, 2015 at 16:32

Present a complete example.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lattner
Associate II
Posted on May 04, 2015 at 16:45

I'm really sorry but i'm not allowed to pleace my full code here. :\

But here is the code for the where all peipherall units are initialized and interrupt handlers.

________________

Attachments :

Board.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0r2&d=%2Fa%2F0X0000000beU%2FZSZeYvLn98AUBHZoq3KWQ8FH3QX7XbcT7UsKV_WNsKM&asPdf=false
Posted on May 04, 2015 at 17:54

Ok, the point is to post a complete example of the code using the USART2, not convenient snippets. Your whole application is not required to do that either. When big things don't work, take smaller bites, and verify that those work as expected.

This is what I mean, something complete, clean and concise, like this :

// STM32 USART2 (USART2 TX PA.02, RX PA.03) STM32F10x - sourcer32@gmail.com
#include ''stm32F10x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PA.02 USART2.TX
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_3; // PA.03 USART2.RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
/* USART configured as follow:
- BaudRate = 38400 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 38400;
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(USART2, &USART_InitStructure);
/* Enable the USART2 */
USART_Cmd(USART2, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
char str[] = ''Welcome to wherever you are

'';
char *s = str;
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, (u16)*s++);
}
}
while(1); // Don't want to exit
}
/**************************************************************************************/
#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

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**************************************************************************************/

Don't let the debugger sit over the USART2 peripheral registers, this can often break things, it's not transparent. If this code works, then step back and review yours. Does the use of TIM2 clash with the use of USART2 on these pins?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lattner
Associate II
Posted on May 05, 2015 at 07:03

Ok thanks.

I use TIM2 only for time calculating. So I looked up in Reference Manual and in Alternate Function GPIO there is TIM2_CH3 for PA2 and TIM2_CH4 for PA3.

I thought if i don't reemap these Ports TIM2 has no effect on these Pins.

Am I wrong?

Posted on May 05, 2015 at 20:26

I'm not using F1 parts, I was merely suggesting it as a line of inquiry based on the fact that pins are handled as a block to a peripheral, and not individually mapped like the F2/F4 and subsequent designs.

Does the supplied code behave as expected? If it does start merging back code until it fails.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..