cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 UART not sending data

mahfoudhi
Associate II
Posted on October 20, 2014 at 11:20

I'm new in ST world. I'm trying to work with UART, so I wrote this code to initializate UART and to send ''Hello''. I'am using STM32F0 board.

#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_misc.h''
#include ''stm32f0xx_usart.h''
#define UINT8 uint8_t
#define UINT32 uint32_t
#define MAX_BUFFER_SIZE 256
typedef struct Buffer_st
{
UINT8 size;
UINT8 data[MAX_BUFFER_SIZE];
}Buffer_st;
Buffer_st receivedDataUART1;
void initUART1(UINT32 baudRate)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//UART init
USART_InitStructure.USART_BaudRate = baudRate;
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;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable USART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART Tx, Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
resetReceivedDataBufferUART1();
}
void resetReceivedDataBufferUART1(void)
{
receivedDataUART1.size = 0;
memset(receivedDataUART1.data, MAX_BUFFER_SIZE, 0);
}
UINT8 sendDataUART1(Buffer_st buffer)
{
UINT8 cpt;
for (cpt = 0; cpt < buffer.size; cpt++)
{
USART_SendData(USART1, buffer.data[cpt]);
//Loop until the end of transmission
while (USART_GetFlagStatus(USART1, USART_IT_TXE) == RESET);
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
receivedDataUART1.data[receivedDataUART1.size++] = USART_ReceiveData(USART1);
}
}
int main(void)
{
Buffer_st buffer = {6,''Hello''};
initUART1(9600);
sendDataUART1(buffer);
while(1);
return 0;
}

I'am using UART/USB connector. The problem is that I don't receive any thing in minicom. When debugging I found that progrom enter in infinite loop

 //Loop until the end of transmission while (USART_GetFlagStatus(USART1, USART_IT_TXE) == RESET);

#usart #uart #stm32 #stm32f0 #discovery
4 REPLIES 4
chen
Associate II
Posted on October 20, 2014 at 12:06

Hi

''I'am using STM32F0 board.''

''I'am using UART/USB connector.''

Are you using this board :

http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF253215?sc=internet/evalboard/product/253215.jsp

If so - stop IMMEDIATELY.

You are putting RS232 voltages (+/-12V) into an IO pint that is only 3.3V (5V if it is a 5V tolerant IO) pin!

You MUST add a RS232 level converter, such as a MAX232.

I do not know if that is the reason for your lock up.

mahfoudhi
Associate II
Posted on October 20, 2014 at 12:22

Yes I'am using STM32F0 discovery board.

Also, I'am using RS232 to 3.3v TTL adapter

Posted on October 20, 2014 at 19:06

Simplify, generate a stream of characters, examine the signals with a scope, confirm the nature and speed, and sense of rx/tx with respect to the receiving device.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mahfoudhi
Associate II
Posted on October 22, 2014 at 21:09

Finally I resolved the problem.

I was mixing up interrupt status flags with the flags used for synchronous polling. To send data we must first poll the

USART_FLAG_TXE

flag for the

transmit data register empty

status and then we can send bytes. The

USART_IT_TXE

flag is for interrupt use only.

So the correction is

//Loop until the end of transmission
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=SET);
USART_SendData(USART1, buffer.data[cpt]);