AnsweredAssumed Answered

STM32F4 Usart1 problem

Question asked by Tom on Oct 18, 2012
Latest reply on Oct 18, 2012 by Clive One
Hi,

Im using a STM32f405RG and i am trying to get a simple USART example to work.
I load the following code to the mcu:

#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_rcc.h"
 
void init_usart(void){
 
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
 
    /* enable peripheral clock for USART2 */
    RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 
 
    /* GPIOA clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
 
    /* GPIOA Configuration:  USART1 TX on PA9 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    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);
 
    /* Connect USART1 pins to AF9 */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
 
    USART_InitStructure.USART_BaudRate = 57600;
    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_Tx;
    USART_Init(USART1, &USART_InitStructure);
 
    USART_Cmd(USART1, ENABLE); // enable USART2
 
}
 
void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}
 
int main(void){
 
    init_usart();
 
    while(1){
        USART_SendData(USART1, 'h');
        Delay(0x3FFFFF);
    }
 
}

when
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

is executed, the pin goes high, and nothing else happens.. The pin never resets and there is no data transmitted over the usart.

Have anyone seen anything like this before?

EDIT:
If I change the code to work with USART2 everything works.. I wonder if it may have something to do with the RCC_APB2Periph_USART1. Im not very familliar with this and how the USART behaves if it's incorrect set.  I'm also using CooCox.


/Tom

Outcomes