Skip to main content
schluchti
Associate
January 14, 2010
Question

USART1 configuration (register)

  • January 14, 2010
  • 4 replies
  • 1078 views
Posted on January 14, 2010 at 03:54

USART1 configuration (register)

    This topic has been closed for replies.

    4 replies

    schluchti
    schluchtiAuthor
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:37

    Hi,

    i want to write my own USART1 initialization. The sourcecode is attached below:

    Code:

    void usartSetup (void) {

    GPIO_InitTypeDef GPIO_InitStructure;

    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // enable clock for GPIOA

    GPIOA->CRH |= (0x0BUL << 4); // Tx (PA9) alt. out push-pull

    GPIOA->CRH |= (0x04UL << 8); // Rx (PA10) in floating

    RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // enable clock for USART1

    USART1->BRR = 8000000L/9600L; // set baudrate

    USART1->CR1 |= (USART_CR1_RE | USART_CR1_TE); // RX, TX enable

    USART1->CR1 |= USART_CR1_UE; // USART enable

    /* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOA, &GPIO_InitStructure); */

    }

    The USART1 initialization only works, if I uncomment the code-block at the end of the subroutine. However, I want to get completely rid off the ST-Library... Does anyone know what I am missing?

    Thanks,

    Bernhard

    chikos33
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 13:37

    I'm glad for you :)

    schluchti
    schluchtiAuthor
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:37

    Hi,

    thanks a lot - it worked!

    chikos33
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 13:37

    Hi,

    The main difference between your code and library code, is that they clear the configuration bits before doing the |= .

    So try first to clear GPIOA->CRH register (or at least the PA9 & PA10 configuration bits):

    GPIOA->CRH &= ~(0x00F0); /* Clear PA9 configuration bits */

    GPIOA->CRH |= (0x00B0); /* Set AF Push Pull mode, 50 MHz for PA9 */

    GPIOA->CRH &= ~(0x0F00); /* Clear PA10 configuration bits */

    GPIOA->CRH |= (0x0400); /* Set Input floating mode for PA10 */

    Good luck.