AnsweredAssumed Answered

UART PRBLEM with STM32F3

Question asked by khalo.hilwane on Jan 30, 2014
Latest reply on Jul 25, 2016 by Clive One
Hello,
I'm trying to use the UART communication between my PC and the DiscoveryF3.
I'm using the adapter USB-UART for that purpose.
The communication works but not what i expected because i received the wrong data.
I have also noticed that the "FE" bit is set even before sending anything.
Below is my code:

void USART1_Init(uint32_t speed, uint8_t inten)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;
//    NVIC_InitTypeDef NVIC_InitStruct;
    /* enable APB2 peripheral clock for USART1*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    
    /* enable the peripheral clock for the pins used by
     * USART1, PB6 for TX and PB7 for RX
     */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;                                                                 // Pins 6 (TX) and 7 (RX) are used
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;                                                                                   // the pins are configured as alternate function so the USART peripheral has access to them
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                                                            // this defines the IO speed
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;                                                                                    // this defines the output type as push pull mode
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;                                                                                        // this activates the pullup resistors on the IO pins
    GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_7);                                                     //this connect PB6 to usart1_tx pin
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_7);                                                        //this connect PB7 to usart1_rx_pin
    
    USART_InitStruct.USART_BaudRate = 115200;                                                                                         //this configure the baudrate at 9600
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;                                                        //length of data to send will be coded at 8 bits
    USART_InitStruct.USART_StopBits = USART_StopBits_1;                                                                    // we want 1 stop bit (standard)
    USART_InitStruct.USART_Parity = USART_Parity_No;                                                                        // we don't want a parity bit (standard)
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;                 // we don't want flow control (standard)
    USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;                                                 // we want to enable the transmitter and the receiver
    USART_Init(USART1, &USART_InitStruct);    
    if(inten)
    {
        /* Enable the USART2 Receive interrupt: this interrupt is generated when the
             USART2 receive data register is not empty */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

        /* Enable the USART2 Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
    }

    
    /* Enable USART */
    USART_Cmd(USART1, ENABLE);
}
/*--------------
void USART1_IRQHandler(void)
{
       while(USART_GetITStatus(USART1, USART_IT_RXNE) == RESET);
 
        /* Read one byte from the receive data register */
        rx_buffer[rx_counter] = (USART_ReceiveData(USART1) & 0x7F);
         /* Put character on the serial line */
        USART1->TDR = (25 & (uint16_t)0x01FF);

        /* Loop until transmit data register is empty */
        while ((USART1->ISR & USART_FLAG_TXE) == (uint16_t) RESET);
            
        if(rx_counter + 1 == RX_BUFFER_LENGTH ||
                rx_buffer[rx_counter] == '\n' || rx_buffer[rx_counter] == '\r')
   
            rx_counter = 0;
        else
        {
            rx_counter++;
                    //  STM_EVAL_LEDInit(LED4);
                      STM_EVAL_LEDOn(LED4);
        }
}

 

Outcomes