2012-04-24 06:57 AM
Recently migrate from 8bit MCU to 32bit STM32, I like the FW library, but there is a learning curve required. So I am trying to do a printf with USART, after i compiled the program, i cant receive anything. At this point i have no clue what went wrong, so i start debug the clock setting.
I found out my USART_BRR=0x138, which is 35.9424MHZ, while my USART2's PCLK2 is running at 36MHZ, not sure if this is a problem. Anything other than that, i cant think of any. Usually, even when the USART's timing is wrong, it will still receive some garbage at the other end, but i aren't receive anything. Could anyone take some time, read my code(actually is a reference code from the dev kit company), and kindly point me where i should looking for the bug....thanks!!!2012-04-24 07:36 AM
What IDE/Compiler?
What board? Have you selected the right board and defined the correct pins? The baud rate value seems correct for 115200 baud @ 36MHz. How about just outputting some characters directly to a USART function in a loop, without using printf(), putchar(), does that work?2012-04-24 08:30 AM
Thanks for your replaying. I am using keil as my IDE.
I will try your suggestion.2012-04-24 12:14 PM
For Keil, you want something like this to host the stdio on the target board.
#include <
stdio.h
>
/*----------------------------------------------------------------------------
Write character to Serial Port
*----------------------------------------------------------------------------*/
int ser_putchar (int c) {
while (!(USART1->SR & USART_FLAG_TXE));
USART1->DR = (c & 0x1FF);
return (c);
}
/*----------------------------------------------------------------------------
Read character from Serial Port (blocking read)
*----------------------------------------------------------------------------*/
int ser_getchar (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc (int ch, FILE *f) { return (ser_putchar(ch)); }
int fgetc (FILE *f) { return (ser_getchar()); }
int ferror(FILE *f) {
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch) { ser_putchar(ch); }
void _sys_exit(int return_code) {
label: goto label; /* endless loop */
}