cancel
Showing results for 
Search instead for 
Did you mean: 

usart stm32f0

mr239955_stm1
Associate II
Posted on February 06, 2013 at 13:31

hello,

i'm trying to connect my stm32f0 discovery with the pc and set up a uart connection. I have found the following code (in this community) but i can't get it to work. Does anyone have a suggestion about what could be wrong. ( i have allready used a level shifter..)

int
main(
void
)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
/* Configure USART2 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_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_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2,ENABLE);
while
(1)
{
while
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
USART_SendData(USART2, 
'X'
);
}
}

6 REPLIES 6
Posted on February 06, 2013 at 14:20

while
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)

; // << SEMI COLON AT THE END THERE
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mr239955_stm1
Associate II
Posted on February 06, 2013 at 14:31

He Clive,

First off, thanks for the really quick response. Oke, i have put the semicollum there but there is still nothing happening. The pc terminal is set to :

8 bits

1 stopbit

9600 baud

no parity

no hardware control. 

just as i specify in my hardware. the tx of the level shifter is connected to PA3 and the rx to PA2. 

so i have no clue what to do next. because the pc terminal is not showing any sign of life. (not receiving anything from the usart. My processor is running at 8mhz (internal clock) could that have something to do with it?

Posted on February 06, 2013 at 16:11

At this point you might want to attach a scope and evaluate the signals and voltages you see.

Perhaps you could try some loop back testing and see if you can receive data.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mr239955_stm1
Associate II
Posted on February 07, 2013 at 11:11

Dear clive,

The problem was not the software afterall. I made a mistake in soldering my level shifter. So one of the ports was shortcircuited. 

Thanks for your help :)

mr239955_stm1
Associate II
Posted on February 08, 2013 at 08:57

Oke this is the next problem I run into. I need to convert a int to a string to send it by usart. So i do the following line and add the headers needed for this call:

char  str[20];

sprintf(str, ''%d'', timer);

UART_Puts(str); // a function that eventually sends the string (tested and it works with '' hello world''  string.

in which timer is the int that needs to be converted. This gives me the following error, and i have a hunch that my compiler doesn't support sprintf?

undefined reference to `_sbrk'

what to do?
Posted on February 08, 2013 at 13:33

GNU based?

I think you need to find the right library to link in. If it's just a matter of decimal, perhaps write a simple routine to decode or something like itoa()

char *dec32(uint32_t i)
{
static char string[16]; // enough for 10 digits + NUL
char *s = string + sizeof(string);
*--s = 0;
do
{
*--s = '0' + (char)(i % 10);
i = i / 10;
} while(i);
return(s);
}

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