cancel
Showing results for 
Search instead for 
Did you mean: 

USART Problems

luis239955
Associate II
Posted on October 26, 2012 at 10:57

Hi Guys,

Been trying to get the USART to work for a couple of days now and is time to get some help... This is my code:

GPIO_InitTypeDef GPIO_InitStructure; 
USART_InitTypeDef USART_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured, 
this is done through SystemInit() function which is called from startup
file (startup_stm32f4xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f4xx.c file
*/ 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); 
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); 
/* GPIO Pins configuration for Alternative Function */
/* for TX Pin */ 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* for RX Pin */ 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 
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_NOPULL; 
GPIO_Init(GPIOD, &GPIO_InitStructure); 
/* USARTx configured as follow:
- BaudRate = 115200 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 = 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);
/* Output a message on Hyperterminal using printf function */
//printf(''\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r'');
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
USART_Cmd(USART2,ENABLE);
USART_SendData(USART2, (uint8_t) 0x31);
while (1)
{
}
}

I tried pretty much all the USARTs on the board and I can not send anything. I looped back the TX & RX pins on my USB to Serial converter and that works fine. The processor seems to be cycling fine thorough the code when on Debug. I am complety at a loss why the program will not work, Any assistance would be appreciated. Best Regards Luis
28 REPLIES 28
mohamedgl2003
Associate II
Posted on April 18, 2013 at 17:40

I have just touched the pin USBPWR by the probe of an oscilloscope.... my PC detected  the  MM232r.... STRANGE!!

mohamedgl2003
Associate II
Posted on April 19, 2013 at 13:56

i used your code to send 'I'....but i don t see it in the hyperteminal???

Posted on April 19, 2013 at 15:39

Get a scope, check the bit timings on USARTxTX, confirm valid.

Make sure the HSE_VALUE and crystal on you board match, and that the PLL settings in system_stm32f4xx.c reflect the correct HSE frequency. The STM32F4-Discovery boards use an 8 MHz crystal, a lot of the code in the firmware library expects the 25 MHz crystal of the EVAL series boards. If the software and hardware settings are incoherent serial comms will not work as expected.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mohamedgl2003
Associate II
Posted on April 19, 2013 at 17:18

Ok ;;;;thank you;

mohamedgl2003
Associate II
Posted on April 19, 2013 at 17:32

Sorry but in your code you cnfigure GPIOA not GPIOD;;;;I change it and my connexion is OK

Posted on April 19, 2013 at 17:45

Sorry but in your code you cnfigure GPIOA not GPIOD;;;;I change it and my connexion is OK

Ok, Fixed that.

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

 printf('' USART Test ''); ;;;i used this instruction to send a string to the hyperteminal but it doesn t work,,,,,

Posted on April 21, 2013 at 16:32

printf('' USART Test ''); ;;;i used this instruction to send a string to the hyperteminal but it doesn t work,,,,,

Ok, but how would the compiler know that STDIO data would get to your specific USART on your specific board unless you implement code to enable that?

In Keil you'd retarget to the serial port via routines you supply.

http://www.keil.com/support/man/docs/gsac/gsac_retargetcortex.htm

Alternatively you could sprintf() to a buffer, and then send that buffer to the USART.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mohamedgl2003
Associate II
Posted on April 28, 2013 at 12:44

even SPRINTF  doesn t allow me to display ....

The SNPRINT do that....