AnsweredAssumed Answered

USART1 with missing bytes

Question asked by ru.r on May 27, 2013
Latest reply on May 27, 2013 by ru.r
I am trying for now to send 600 001 bytes to PC from stm32f4 discovery board.

below is how USART configured:
uint32_t config_usart3(void)
{
 
    //=============================================================================
    // USART3 Related configuration
    //=============================================================================
    // enable clock
    RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
 
 
    // 1) Setting UE, amd M bits
    USART3-> CR1 |= 0x2000; // UE = 1
 
    // 2) Programming number of stop bits if needed
 
 
    // 3) Enable DMA if needed
 
 
    // 4) set the Baud Rate
    // BAUD = fck / ( 8 * (2 - OVER8) * USARTDIV )
    // fck = 42MHz,
    // OVER8 = 0
    // Choose BAUD = 115200
    // then: USARTDIV = fck / ( 8 * (2  -OVER8) * BAUD = 22.75
    // BRR = (22 << 4) | ( 0.75 * 16) = 364,
    // or: BRR = fck / BAUD = 42MHz / 115200 = 364
    USART3->BRR = 364;
 
 
    // enable transmitter
    USART3->CR1 |= USART_CR1_TE;
 
    // enable receiver
    USART3->CR1 |= USART_CR1_RE;
 
    // enable CTS/RTS
    USART3->CR3 |=
    (
    USART_CR3_CTSE |
    USART_CR3_RTSE
    );
 
    return 0;
}

Below is how GPIO port relevant to USART1 configured:
//=============================================================================
// GPIOD configuration
//=============================================================================
// enable GPIOD clock
((RCC_TypeDef *)(RCC_BASE))->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
 
 
// Alternate Function
((GPIO_TypeDef *)(GPIOD_BASE))->MODER |=
(GPIO_MODER_MODER9_1  | // Alternate Function,  USART3_RX
GPIO_MODER_MODER8_1   | // Alternate Function,  USART3_TX
GPIO_MODER_MODER11_1  | // Alternate Function,  USART3_CTS
GPIO_MODER_MODER12_1    // Alternate Function,  USART3_RTS
);
 
// Output type
GPIOD->OTYPER = 0;
 
 
 
// Speed type
((GPIO_TypeDef *)(GPIOD))->OSPEEDR |=
(GPIO_OSPEEDER_OSPEEDR8_1 | // USART3 TX 50 MHz
GPIO_OSPEEDER_OSPEEDR9_1  |  // USART3 RX 50 MHz
GPIO_OSPEEDER_OSPEEDR11_1  |  // USART3 CTS 50 MHz
GPIO_OSPEEDER_OSPEEDR12_1    // USART3 RTS 50 MHz
);
 
 
// Push/Pull for USART3
GPIOD->PUPDR = 0;
((GPIO_TypeDef *)(GPIOD_BASE))->PUPDR |=
(
GPIO_PUPDR_PUPDR8_0 |  // Pull-Up, USART3 TX
GPIO_PUPDR_PUPDR9_0 |  // Pull-Up, USART3 RX
GPIO_PUPDR_PUPDR11_0 |  // Pull-Up, USART3 CTS
GPIO_PUPDR_PUPDR12_0    // Pull-Up, USART3 RTS
);
 
 
((GPIO_TypeDef *)(GPIOD_BASE))->AFR[1] |= (
(7 << ((8 - 8) << 2))  |  // USART3 TX, AF7
(7 << ((9 - 8) << 2))  |  // USART3 RX, AF7
(7 << ((11 - 8) << 2)) |  // USART3 CTS, AF7
(7 << ((12 - 8) << 2))    // USART3 RTS, AF7
);

and below is how I send my bytes:
uint8_t mem_read(USART_TypeDef *USART_ID )
{
    uint32_t i=0; // loop var
 
 
        //send read buffer to USART
        for(i=0;i<=600000;i++)
        {
        // check if TXE bit is set
        while((!(USART_ID->SR & USART_SR_TXE)) );
         
        USART_ID->DR = 0x5d; // sending a byte
 
        // check if TC bit is set
        while((!(USART_ID->SR & USART_SR_TC)) );
         
 
        }
 
return 0;
}


Using WinXP, with FTDI cable and its drivers, cable was loopbacked and seems to work.

What happens is, on the PC end I get my 600001 bytes 5-6 times out of 10, in other words, a lot of times I end up missing couple thousand bytes or s (I get something like 597 953 bytes etc).

Not sure what else did I do on the STM32 part?
as you see I check for TXE, and TC(although checking TC might not be needed that much)

I also set up the stm32 in CTS/RTS mode, as well as the PC terminal emulator program.

again,.. I do get 600001 bytes but not all the time! And I am curious what else could be wrong?

Outcomes